UdpServerEntryPoint.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Emby.Server.Implementations.Udp;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Common.Net;
  11. using MediaBrowser.Controller;
  12. using MediaBrowser.Controller.Plugins;
  13. using Microsoft.Extensions.Configuration;
  14. using Microsoft.Extensions.Logging;
  15. namespace Emby.Server.Implementations.EntryPoints
  16. {
  17. /// <summary>
  18. /// Class responsible for registering all UDP broadcast endpoints and their handlers.
  19. /// </summary>
  20. public sealed class UdpServerEntryPoint : IServerEntryPoint
  21. {
  22. /// <summary>
  23. /// The port of the UDP server.
  24. /// </summary>
  25. public const int PortNumber = 7359;
  26. /// <summary>
  27. /// The logger.
  28. /// </summary>
  29. private readonly ILogger<UdpServerEntryPoint> _logger;
  30. private readonly IServerApplicationHost _appHost;
  31. private readonly IConfiguration _config;
  32. private readonly IConfigurationManager _configurationManager;
  33. private readonly INetworkManager _networkManager;
  34. /// <summary>
  35. /// The UDP server.
  36. /// </summary>
  37. private readonly List<UdpServer> _udpServers;
  38. private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  39. private bool _disposed;
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
  42. /// </summary>
  43. /// <param name="logger">Instance of the <see cref="ILogger{UdpServerEntryPoint}"/> interface.</param>
  44. /// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
  45. /// <param name="configuration">Instance of the <see cref="IConfiguration"/> interface.</param>
  46. /// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
  47. /// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
  48. public UdpServerEntryPoint(
  49. ILogger<UdpServerEntryPoint> logger,
  50. IServerApplicationHost appHost,
  51. IConfiguration configuration,
  52. IConfigurationManager configurationManager,
  53. INetworkManager networkManager)
  54. {
  55. _logger = logger;
  56. _appHost = appHost;
  57. _config = configuration;
  58. _configurationManager = configurationManager;
  59. _networkManager = networkManager;
  60. _udpServers = new List<UdpServer>();
  61. }
  62. /// <inheritdoc />
  63. public Task RunAsync()
  64. {
  65. CheckDisposed();
  66. if (!_configurationManager.GetNetworkConfiguration().AutoDiscovery)
  67. {
  68. return Task.CompletedTask;
  69. }
  70. try
  71. {
  72. // Linux needs to bind to the broadcast addresses to get broadcast traffic
  73. // Windows receives broadcast fine when binding to just the interface, it is unable to bind to broadcast addresses
  74. if (OperatingSystem.IsLinux())
  75. {
  76. // Add global broadcast listener
  77. var server = new UdpServer(_logger, _appHost, _config, IPAddress.Broadcast, PortNumber);
  78. server.Start(_cancellationTokenSource.Token);
  79. _udpServers.Add(server);
  80. // Add bind address specific broadcast listeners
  81. // IPv6 is currently unsupported
  82. var validInterfaces = _networkManager.GetInternalBindAddresses().Where(i => i.AddressFamily == AddressFamily.InterNetwork);
  83. foreach (var intf in validInterfaces)
  84. {
  85. var broadcastAddress = NetworkUtils.GetBroadcastAddress(intf.Subnet);
  86. _logger.LogDebug("Binding UDP server to {Address} on port {PortNumber}", broadcastAddress, PortNumber);
  87. server = new UdpServer(_logger, _appHost, _config, broadcastAddress, PortNumber);
  88. server.Start(_cancellationTokenSource.Token);
  89. _udpServers.Add(server);
  90. }
  91. }
  92. else
  93. {
  94. // Add bind address specific broadcast listeners
  95. // IPv6 is currently unsupported
  96. var validInterfaces = _networkManager.GetInternalBindAddresses().Where(i => i.AddressFamily == AddressFamily.InterNetwork);
  97. foreach (var intf in validInterfaces)
  98. {
  99. var intfAddress = intf.Address;
  100. _logger.LogDebug("Binding UDP server to {Address} on port {PortNumber}", intfAddress, PortNumber);
  101. var server = new UdpServer(_logger, _appHost, _config, intfAddress, PortNumber);
  102. server.Start(_cancellationTokenSource.Token);
  103. _udpServers.Add(server);
  104. }
  105. }
  106. }
  107. catch (SocketException ex)
  108. {
  109. _logger.LogWarning(ex, "Unable to start AutoDiscovery listener on UDP port {PortNumber}", PortNumber);
  110. }
  111. return Task.CompletedTask;
  112. }
  113. private void CheckDisposed()
  114. {
  115. if (_disposed)
  116. {
  117. throw new ObjectDisposedException(GetType().Name);
  118. }
  119. }
  120. /// <inheritdoc />
  121. public void Dispose()
  122. {
  123. if (_disposed)
  124. {
  125. return;
  126. }
  127. _cancellationTokenSource.Cancel();
  128. _cancellationTokenSource.Dispose();
  129. foreach (var server in _udpServers)
  130. {
  131. server.Dispose();
  132. }
  133. _udpServers.Clear();
  134. _disposed = true;
  135. }
  136. }
  137. }