2021-02-22 05:13:31 -07:00
|
|
|
#nullable enable
|
|
|
|
|
2020-07-13 08:33:39 -07:00
|
|
|
using System.Net.Sockets;
|
2020-01-12 10:59:10 -07:00
|
|
|
using System.Threading;
|
2019-01-27 07:40:37 -07:00
|
|
|
using System.Threading.Tasks;
|
2019-01-13 12:20:41 -07:00
|
|
|
using Emby.Server.Implementations.Udp;
|
2014-07-27 15:01:29 -07:00
|
|
|
using MediaBrowser.Controller;
|
2013-05-18 15:07:59 -07:00
|
|
|
using MediaBrowser.Controller.Plugins;
|
2020-05-02 09:56:09 -07:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-01-13 12:20:41 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-05-18 15:07:59 -07:00
|
|
|
|
2016-11-04 11:56:47 -07:00
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2013-05-18 15:07:59 -07:00
|
|
|
{
|
2013-05-27 11:34:03 -07:00
|
|
|
/// <summary>
|
2019-11-01 10:38:54 -07:00
|
|
|
/// Class UdpServerEntryPoint.
|
2013-05-27 11:34:03 -07:00
|
|
|
/// </summary>
|
2020-01-12 10:59:10 -07:00
|
|
|
public sealed class UdpServerEntryPoint : IServerEntryPoint
|
2013-05-18 15:07:59 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 10:38:54 -07:00
|
|
|
/// The port of the UDP server.
|
2013-05-18 15:07:59 -07:00
|
|
|
/// </summary>
|
2019-11-01 10:38:54 -07:00
|
|
|
public const int PortNumber = 7359;
|
2013-05-18 15:07:59 -07:00
|
|
|
|
2013-05-27 11:34:03 -07:00
|
|
|
/// <summary>
|
2019-12-10 08:22:03 -07:00
|
|
|
/// The logger.
|
2013-05-27 11:34:03 -07:00
|
|
|
/// </summary>
|
2020-06-05 17:15:56 -07:00
|
|
|
private readonly ILogger<UdpServerEntryPoint> _logger;
|
2014-07-27 15:01:29 -07:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2020-05-02 09:56:09 -07:00
|
|
|
private readonly IConfiguration _config;
|
2013-05-18 15:07:59 -07:00
|
|
|
|
2019-11-01 10:38:54 -07:00
|
|
|
/// <summary>
|
|
|
|
/// The UDP server.
|
|
|
|
/// </summary>
|
2021-02-22 05:13:31 -07:00
|
|
|
private UdpServer? _udpServer;
|
2020-01-12 10:59:10 -07:00
|
|
|
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
private bool _disposed = false;
|
2013-09-24 17:54:51 -07:00
|
|
|
|
2013-05-27 11:34:03 -07:00
|
|
|
/// <summary>
|
2014-08-19 15:28:35 -07:00
|
|
|
/// Initializes a new instance of the <see cref="UdpServerEntryPoint" /> class.
|
2013-05-27 11:34:03 -07:00
|
|
|
/// </summary>
|
2019-11-01 10:38:54 -07:00
|
|
|
public UdpServerEntryPoint(
|
2020-01-12 10:59:10 -07:00
|
|
|
ILogger<UdpServerEntryPoint> logger,
|
2020-05-02 09:56:09 -07:00
|
|
|
IServerApplicationHost appHost,
|
|
|
|
IConfiguration configuration)
|
2013-05-18 15:07:59 -07:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2014-07-27 15:01:29 -07:00
|
|
|
_appHost = appHost;
|
2020-05-02 09:56:09 -07:00
|
|
|
_config = configuration;
|
2013-05-18 15:07:59 -07:00
|
|
|
}
|
|
|
|
|
2019-11-01 10:38:54 -07:00
|
|
|
/// <inheritdoc />
|
2020-04-05 09:10:56 -07:00
|
|
|
public Task RunAsync()
|
2013-05-18 15:07:59 -07:00
|
|
|
{
|
2020-07-13 07:12:51 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_udpServer = new UdpServer(_logger, _appHost, _config);
|
|
|
|
_udpServer.Start(PortNumber, _cancellationTokenSource.Token);
|
|
|
|
}
|
2020-07-13 08:33:39 -07:00
|
|
|
catch (SocketException ex)
|
2020-07-13 07:12:51 -07:00
|
|
|
{
|
2020-07-13 07:39:14 -07:00
|
|
|
_logger.LogWarning(ex, "Unable to start AutoDiscovery listener on UDP port {PortNumber}", PortNumber);
|
2020-07-13 07:12:51 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 09:10:56 -07:00
|
|
|
return Task.CompletedTask;
|
2013-05-18 15:07:59 -07:00
|
|
|
}
|
|
|
|
|
2020-01-12 10:59:10 -07:00
|
|
|
/// <inheritdoc />
|
|
|
|
public void Dispose()
|
2013-05-18 15:07:59 -07:00
|
|
|
{
|
2020-01-12 10:59:10 -07:00
|
|
|
if (_disposed)
|
2013-05-18 15:07:59 -07:00
|
|
|
{
|
2020-01-12 10:59:10 -07:00
|
|
|
return;
|
2013-05-18 15:07:59 -07:00
|
|
|
}
|
2020-01-12 10:59:10 -07:00
|
|
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
2020-02-06 07:20:23 -07:00
|
|
|
_cancellationTokenSource.Dispose();
|
2021-02-22 05:13:31 -07:00
|
|
|
_udpServer?.Dispose();
|
2020-01-12 10:59:10 -07:00
|
|
|
_udpServer = null;
|
|
|
|
|
|
|
|
_disposed = true;
|
2013-05-18 15:07:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|