UdpServerEntryPoint.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.Logging;
  7. namespace Emby.Server.Implementations.EntryPoints
  8. {
  9. /// <summary>
  10. /// Class UdpServerEntryPoint.
  11. /// </summary>
  12. public sealed class UdpServerEntryPoint : IServerEntryPoint
  13. {
  14. /// <summary>
  15. /// The port of the UDP server.
  16. /// </summary>
  17. public const int PortNumber = 7359;
  18. /// <summary>
  19. /// The logger.
  20. /// </summary>
  21. private readonly ILogger _logger;
  22. private readonly IServerApplicationHost _appHost;
  23. /// <summary>
  24. /// The UDP server.
  25. /// </summary>
  26. private UdpServer _udpServer;
  27. private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  28. private bool _disposed = false;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  31. /// </summary>
  32. public UdpServerEntryPoint(
  33. ILogger<UdpServerEntryPoint> logger,
  34. IServerApplicationHost appHost)
  35. {
  36. _logger = logger;
  37. _appHost = appHost;
  38. }
  39. /// <inheritdoc />
  40. public async Task RunAsync()
  41. {
  42. _udpServer = new UdpServer(_logger, _appHost);
  43. _udpServer.Start(PortNumber, _cancellationTokenSource.Token);
  44. }
  45. /// <inheritdoc />
  46. public void Dispose()
  47. {
  48. if (_disposed)
  49. {
  50. return;
  51. }
  52. _cancellationTokenSource.Cancel();
  53. _udpServer.Dispose();
  54. _cancellationTokenSource.Dispose();
  55. _cancellationTokenSource = null;
  56. _udpServer = null;
  57. _disposed = true;
  58. }
  59. }
  60. }