UdpServerEntryPoint.cs 3.4 KB

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