UdpServerEntryPoint.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Emby.Server.Implementations.Udp;
  7. using Jellyfin.Networking.Configuration;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller;
  11. using MediaBrowser.Controller.Plugins;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.Logging;
  14. namespace Emby.Server.Implementations.EntryPoints
  15. {
  16. /// <summary>
  17. /// Class UdpServerEntryPoint.
  18. /// </summary>
  19. public sealed class UdpServerEntryPoint : IServerEntryPoint
  20. {
  21. /// <summary>
  22. /// The port of the UDP server.
  23. /// </summary>
  24. public const int PortNumber = 7359;
  25. /// <summary>
  26. /// The logger.
  27. /// </summary>
  28. private readonly ILogger<UdpServerEntryPoint> _logger;
  29. private readonly IServerApplicationHost _appHost;
  30. private readonly IConfiguration _config;
  31. private readonly IConfigurationManager _configurationManager;
  32. private readonly INetworkManager _networkManager;
  33. private readonly bool _enableMultiSocketBinding;
  34. /// <summary>
  35. /// The UDP server.
  36. /// </summary>
  37. private List<UdpServer> _udpServers;
  38. private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  39. private bool _disposed = false;
  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. _enableMultiSocketBinding = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
  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. if (_enableMultiSocketBinding)
  74. {
  75. // Add global broadcast socket
  76. _udpServers.Add(new UdpServer(_logger, _appHost, _config, System.Net.IPAddress.Broadcast, PortNumber));
  77. // Add bind address specific broadcast sockets
  78. foreach (var bindAddress in _networkManager.GetInternalBindAddresses())
  79. {
  80. if (bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
  81. {
  82. // Not supporting IPv6 right now
  83. continue;
  84. }
  85. var broadcastAddress = NetworkExtensions.GetBroadcastAddress(bindAddress.Subnet);
  86. _logger.LogDebug("Binding UDP server to {Address} on port {PortNumber}", broadcastAddress.ToString(), PortNumber);
  87. _udpServers.Add(new UdpServer(_logger, _appHost, _config, broadcastAddress, PortNumber));
  88. }
  89. }
  90. else
  91. {
  92. _udpServers.Add(new UdpServer(_logger, _appHost, _config, System.Net.IPAddress.Any, PortNumber));
  93. }
  94. _udpServers.ForEach(u => u.Start(_cancellationTokenSource.Token));
  95. }
  96. catch (SocketException ex)
  97. {
  98. _logger.LogWarning(ex, "Unable to start AutoDiscovery listener on UDP port {PortNumber}", PortNumber);
  99. }
  100. return Task.CompletedTask;
  101. }
  102. private void CheckDisposed()
  103. {
  104. if (_disposed)
  105. {
  106. throw new ObjectDisposedException(this.GetType().Name);
  107. }
  108. }
  109. /// <inheritdoc />
  110. public void Dispose()
  111. {
  112. if (_disposed)
  113. {
  114. return;
  115. }
  116. _cancellationTokenSource.Cancel();
  117. _cancellationTokenSource.Dispose();
  118. _udpServers.ForEach(s => s.Dispose());
  119. _udpServers.Clear();
  120. _disposed = true;
  121. }
  122. }
  123. }