2021-02-23 10:45:10 -05:00
|
|
|
using System;
|
2020-07-13 11:33:39 -04:00
|
|
|
using System.Net.Sockets;
|
2020-01-12 12:59:10 -05:00
|
|
|
using System.Threading;
|
2019-01-27 09:40:37 -05:00
|
|
|
using System.Threading.Tasks;
|
2019-01-13 14:20:41 -05:00
|
|
|
using Emby.Server.Implementations.Udp;
|
2022-02-14 08:39:33 -05:00
|
|
|
using Jellyfin.Networking.Configuration;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2022-07-21 03:42:45 -04:00
|
|
|
using MediaBrowser.Common.Net;
|
2014-07-27 18:01:29 -04:00
|
|
|
using MediaBrowser.Controller;
|
2013-05-18 18:07:59 -04:00
|
|
|
using MediaBrowser.Controller.Plugins;
|
2020-05-02 12:56:09 -04:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-01-13 14:20:41 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-05-18 18:07:59 -04:00
|
|
|
|
2016-11-04 14:56:47 -04:00
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2013-05-18 18:07:59 -04:00
|
|
|
{
|
2013-05-27 14:34:03 -04:00
|
|
|
/// <summary>
|
2019-11-01 13:38:54 -04:00
|
|
|
/// Class UdpServerEntryPoint.
|
2013-05-27 14:34:03 -04:00
|
|
|
/// </summary>
|
2020-01-12 12:59:10 -05:00
|
|
|
public sealed class UdpServerEntryPoint : IServerEntryPoint
|
2013-05-18 18:07:59 -04:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 13:38:54 -04:00
|
|
|
/// The port of the UDP server.
|
2013-05-18 18:07:59 -04:00
|
|
|
/// </summary>
|
2019-11-01 13:38:54 -04:00
|
|
|
public const int PortNumber = 7359;
|
2013-05-18 18:07:59 -04:00
|
|
|
|
2013-05-27 14:34:03 -04:00
|
|
|
/// <summary>
|
2019-12-10 10:22:03 -05:00
|
|
|
/// The logger.
|
2013-05-27 14:34:03 -04:00
|
|
|
/// </summary>
|
2020-06-05 20:15:56 -04:00
|
|
|
private readonly ILogger<UdpServerEntryPoint> _logger;
|
2014-07-27 18:01:29 -04:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2020-05-02 12:56:09 -04:00
|
|
|
private readonly IConfiguration _config;
|
2022-02-14 08:39:33 -05:00
|
|
|
private readonly IConfigurationManager _configurationManager;
|
2022-07-21 03:42:45 -04:00
|
|
|
private readonly INetworkManager _networkManager;
|
2013-05-18 18:07:59 -04:00
|
|
|
|
2019-11-01 13:38:54 -04:00
|
|
|
/// <summary>
|
|
|
|
/// The UDP server.
|
|
|
|
/// </summary>
|
2021-02-22 07:13:31 -05:00
|
|
|
private UdpServer? _udpServer;
|
2020-01-12 12:59:10 -05:00
|
|
|
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
private bool _disposed = false;
|
2013-09-24 20:54:51 -04:00
|
|
|
|
2013-05-27 14:34:03 -04:00
|
|
|
/// <summary>
|
2014-08-19 18:28:35 -04:00
|
|
|
/// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
|
2013-05-27 14:34:03 -04:00
|
|
|
/// </summary>
|
2021-08-28 18:32:50 -04:00
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{UdpServerEntryPoint}"/> interface.</param>
|
|
|
|
/// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
|
|
|
|
/// <param name="configuration">Instance of the <see cref="IConfiguration"/> interface.</param>
|
2022-02-14 08:39:33 -05:00
|
|
|
/// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
|
2022-07-21 03:42:45 -04:00
|
|
|
/// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
|
2019-11-01 13:38:54 -04:00
|
|
|
public UdpServerEntryPoint(
|
2020-01-12 12:59:10 -05:00
|
|
|
ILogger<UdpServerEntryPoint> logger,
|
2020-05-02 12:56:09 -04:00
|
|
|
IServerApplicationHost appHost,
|
2022-02-14 08:39:33 -05:00
|
|
|
IConfiguration configuration,
|
2022-07-21 03:42:45 -04:00
|
|
|
IConfigurationManager configurationManager,
|
|
|
|
INetworkManager networkManager)
|
2013-05-18 18:07:59 -04:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2014-07-27 18:01:29 -04:00
|
|
|
_appHost = appHost;
|
2020-05-02 12:56:09 -04:00
|
|
|
_config = configuration;
|
2022-02-14 08:39:33 -05:00
|
|
|
_configurationManager = configurationManager;
|
2022-07-21 03:42:45 -04:00
|
|
|
_networkManager = networkManager;
|
2013-05-18 18:07:59 -04:00
|
|
|
}
|
|
|
|
|
2019-11-01 13:38:54 -04:00
|
|
|
/// <inheritdoc />
|
2020-04-05 12:10:56 -04:00
|
|
|
public Task RunAsync()
|
2013-05-18 18:07:59 -04:00
|
|
|
{
|
2021-02-23 10:45:10 -05:00
|
|
|
CheckDisposed();
|
|
|
|
|
2022-03-08 16:11:06 -05:00
|
|
|
if (!_configurationManager.GetNetworkConfiguration().AutoDiscovery)
|
2022-02-14 08:39:33 -05:00
|
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2020-07-13 10:12:51 -04:00
|
|
|
try
|
|
|
|
{
|
2022-07-21 03:42:45 -04:00
|
|
|
foreach (var bindAddress in _networkManager.GetInternalBindAddresses())
|
|
|
|
{
|
|
|
|
if (bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
|
|
|
{
|
|
|
|
// Not supporting IPv6 right now
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
_udpServer = new UdpServer(_logger, _appHost, _config, bindAddress.Address, PortNumber);
|
|
|
|
_udpServer.Start(_cancellationTokenSource.Token);
|
|
|
|
}
|
2020-07-13 10:12:51 -04:00
|
|
|
}
|
2020-07-13 11:33:39 -04:00
|
|
|
catch (SocketException ex)
|
2020-07-13 10:12:51 -04:00
|
|
|
{
|
2020-07-13 10:39:14 -04:00
|
|
|
_logger.LogWarning(ex, "Unable to start AutoDiscovery listener on UDP port {PortNumber}", PortNumber);
|
2020-07-13 10:12:51 -04:00
|
|
|
}
|
|
|
|
|
2020-04-05 12:10:56 -04:00
|
|
|
return Task.CompletedTask;
|
2013-05-18 18:07:59 -04:00
|
|
|
}
|
|
|
|
|
2021-02-23 10:45:10 -05:00
|
|
|
private void CheckDisposed()
|
|
|
|
{
|
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(this.GetType().Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:59:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
public void Dispose()
|
2013-05-18 18:07:59 -04:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
if (_disposed)
|
2013-05-18 18:07:59 -04:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
return;
|
2013-05-18 18:07:59 -04:00
|
|
|
}
|
2020-01-12 12:59:10 -05:00
|
|
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
2020-02-06 09:20:23 -05:00
|
|
|
_cancellationTokenSource.Dispose();
|
2021-02-22 07:13:31 -05:00
|
|
|
_udpServer?.Dispose();
|
2020-01-12 12:59:10 -05:00
|
|
|
_udpServer = null;
|
|
|
|
|
|
|
|
_disposed = true;
|
2013-05-18 18:07:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|