UdpServerEntryPoint.cs 2.3 KB

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