UdpServerEntryPoint.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. foreach (var bindAddress in _networkManager.GetInternalBindAddresses())
  76. {
  77. if (bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
  78. {
  79. // Not supporting IPv6 right now
  80. continue;
  81. }
  82. _udpServers.Add(new UdpServer(_logger, _appHost, _config, bindAddress.Address, PortNumber));
  83. }
  84. }
  85. else
  86. {
  87. _udpServers.Add(new UdpServer(_logger, _appHost, _config, System.Net.IPAddress.Any, PortNumber));
  88. }
  89. _udpServers.ForEach(u => u.Start(_cancellationTokenSource.Token));
  90. }
  91. catch (SocketException ex)
  92. {
  93. _logger.LogWarning(ex, "Unable to start AutoDiscovery listener on UDP port {PortNumber}", PortNumber);
  94. }
  95. return Task.CompletedTask;
  96. }
  97. private void CheckDisposed()
  98. {
  99. if (_disposed)
  100. {
  101. throw new ObjectDisposedException(this.GetType().Name);
  102. }
  103. }
  104. /// <inheritdoc />
  105. public void Dispose()
  106. {
  107. if (_disposed)
  108. {
  109. return;
  110. }
  111. _cancellationTokenSource.Cancel();
  112. _cancellationTokenSource.Dispose();
  113. _udpServers.ForEach(s => s.Dispose());
  114. _udpServers.Clear();
  115. _disposed = true;
  116. }
  117. }
  118. }