2
0

UdpServerEntryPoint.cs 6.0 KB

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