UdpServerEntryPoint.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Emby.Server.Implementations.Udp;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Plugins;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Logging;
  8. namespace Emby.Server.Implementations.EntryPoints
  9. {
  10. /// <summary>
  11. /// Class UdpServerEntryPoint.
  12. /// </summary>
  13. public sealed class UdpServerEntryPoint : IServerEntryPoint
  14. {
  15. /// <summary>
  16. /// The port of the UDP server.
  17. /// </summary>
  18. public const int PortNumber = 7359;
  19. /// <summary>
  20. /// The logger.
  21. /// </summary>
  22. private readonly ILogger<UdpServerEntryPoint> _logger;
  23. private readonly IServerApplicationHost _appHost;
  24. private readonly IConfiguration _config;
  25. /// <summary>
  26. /// The UDP server.
  27. /// </summary>
  28. private UdpServer _udpServer;
  29. private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  30. private bool _disposed = false;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  33. /// </summary>
  34. public UdpServerEntryPoint(
  35. ILogger<UdpServerEntryPoint> logger,
  36. IServerApplicationHost appHost,
  37. IConfiguration configuration)
  38. {
  39. _logger = logger;
  40. _appHost = appHost;
  41. _config = configuration;
  42. }
  43. /// <inheritdoc />
  44. public Task RunAsync()
  45. {
  46. _udpServer = new UdpServer(_logger, _appHost, _config);
  47. _udpServer.Start(PortNumber, _cancellationTokenSource.Token);
  48. return Task.CompletedTask;
  49. }
  50. /// <inheritdoc />
  51. public void Dispose()
  52. {
  53. if (_disposed)
  54. {
  55. return;
  56. }
  57. _cancellationTokenSource.Cancel();
  58. _udpServer.Dispose();
  59. _cancellationTokenSource.Dispose();
  60. _cancellationTokenSource = null;
  61. _udpServer = null;
  62. _disposed = true;
  63. }
  64. }
  65. }