UdpServerEntryPoint.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.Common.Net;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Controller.Plugins;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.EntryPoints
  14. {
  15. /// <summary>
  16. /// Class UdpServerEntryPoint.
  17. /// </summary>
  18. public sealed class UdpServerEntryPoint : IServerEntryPoint
  19. {
  20. /// <summary>
  21. /// The port of the UDP server.
  22. /// </summary>
  23. public const int PortNumber = 7359;
  24. /// <summary>
  25. /// The logger.
  26. /// </summary>
  27. private readonly ILogger<UdpServerEntryPoint> _logger;
  28. private readonly IServerApplicationHost _appHost;
  29. private readonly IConfiguration _config;
  30. private readonly IConfigurationManager _configurationManager;
  31. private readonly INetworkManager _networkManager;
  32. private readonly bool _enableMultiSocketBinding;
  33. /// <summary>
  34. /// The UDP server.
  35. /// </summary>
  36. private UdpServer? _udpServer;
  37. private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  38. private bool _disposed = false;
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  41. /// </summary>
  42. /// <param name="logger">Instance of the <see cref="ILogger{UdpServerEntryPoint}"/> interface.</param>
  43. /// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
  44. /// <param name="configuration">Instance of the <see cref="IConfiguration"/> interface.</param>
  45. /// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
  46. /// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
  47. public UdpServerEntryPoint(
  48. ILogger<UdpServerEntryPoint> logger,
  49. IServerApplicationHost appHost,
  50. IConfiguration configuration,
  51. IConfigurationManager configurationManager,
  52. INetworkManager networkManager)
  53. {
  54. _logger = logger;
  55. _appHost = appHost;
  56. _config = configuration;
  57. _configurationManager = configurationManager;
  58. _networkManager = networkManager;
  59. _enableMultiSocketBinding = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
  60. }
  61. /// <inheritdoc />
  62. public Task RunAsync()
  63. {
  64. CheckDisposed();
  65. if (!_configurationManager.GetNetworkConfiguration().AutoDiscovery)
  66. {
  67. return Task.CompletedTask;
  68. }
  69. try
  70. {
  71. if (_enableMultiSocketBinding)
  72. {
  73. foreach (var bindAddress in _networkManager.GetInternalBindAddresses())
  74. {
  75. if (bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
  76. {
  77. // Not supporting IPv6 right now
  78. continue;
  79. }
  80. _udpServer = new UdpServer(_logger, _appHost, _config, bindAddress.Address, PortNumber);
  81. _udpServer.Start(_cancellationTokenSource.Token);
  82. }
  83. }
  84. else
  85. {
  86. _udpServer = new UdpServer(_logger, _appHost, _config, System.Net.IPAddress.Any, PortNumber);
  87. _udpServer.Start(_cancellationTokenSource.Token);
  88. }
  89. }
  90. catch (SocketException ex)
  91. {
  92. _logger.LogWarning(ex, "Unable to start AutoDiscovery listener on UDP port {PortNumber}", PortNumber);
  93. }
  94. return Task.CompletedTask;
  95. }
  96. private void CheckDisposed()
  97. {
  98. if (_disposed)
  99. {
  100. throw new ObjectDisposedException(this.GetType().Name);
  101. }
  102. }
  103. /// <inheritdoc />
  104. public void Dispose()
  105. {
  106. if (_disposed)
  107. {
  108. return;
  109. }
  110. _cancellationTokenSource.Cancel();
  111. _cancellationTokenSource.Dispose();
  112. _udpServer?.Dispose();
  113. _udpServer = null;
  114. _disposed = true;
  115. }
  116. }
  117. }