Replace ISocket and UdpSocket, fix DLNA and SSDP binding and discovery
This commit is contained in:
parent
3a91c37283
commit
42498194d9
|
@ -17,7 +17,7 @@ namespace Emby.Dlna.Configuration
|
|||
BlastAliveMessages = true;
|
||||
SendOnlyMatchedHost = true;
|
||||
ClientDiscoveryIntervalSeconds = 60;
|
||||
AliveMessageIntervalSeconds = 1800;
|
||||
AliveMessageIntervalSeconds = 180;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -7,7 +7,6 @@ using System.Globalization;
|
|||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Emby.Dlna.PlayTo;
|
||||
using Emby.Dlna.Ssdp;
|
||||
|
@ -201,8 +200,7 @@ namespace Emby.Dlna.Main
|
|||
{
|
||||
if (_communicationsServer is null)
|
||||
{
|
||||
var enableMultiSocketBinding = OperatingSystem.IsWindows() ||
|
||||
OperatingSystem.IsLinux();
|
||||
var enableMultiSocketBinding = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
|
||||
|
||||
_communicationsServer = new SsdpCommunicationsServer(_socketFactory, _networkManager, _logger, enableMultiSocketBinding)
|
||||
{
|
||||
|
@ -248,11 +246,6 @@ namespace Emby.Dlna.Main
|
|||
|
||||
public void StartDevicePublisher(Configuration.DlnaOptions options)
|
||||
{
|
||||
if (!options.BlastAliveMessages)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_publisher is not null)
|
||||
{
|
||||
return;
|
||||
|
@ -263,7 +256,8 @@ namespace Emby.Dlna.Main
|
|||
_publisher = new SsdpDevicePublisher(
|
||||
_communicationsServer,
|
||||
Environment.OSVersion.Platform.ToString(),
|
||||
Environment.OSVersion.VersionString,
|
||||
// Can not use VersionString here since that includes OS and version
|
||||
Environment.OSVersion.Version.ToString(),
|
||||
_config.GetDlnaConfiguration().SendOnlyMatchedHost)
|
||||
{
|
||||
LogFunction = (msg) => _logger.LogDebug("{Msg}", msg),
|
||||
|
@ -272,7 +266,10 @@ namespace Emby.Dlna.Main
|
|||
|
||||
RegisterServerEndpoints();
|
||||
|
||||
_publisher.StartBroadcastingAliveMessages(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
|
||||
if (options.BlastAliveMessages)
|
||||
{
|
||||
_publisher.StartSendingAliveNotifications(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -286,38 +283,32 @@ namespace Emby.Dlna.Main
|
|||
var descriptorUri = "/dlna/" + udn + "/description.xml";
|
||||
|
||||
// Only get bind addresses in LAN
|
||||
var bindAddresses = _networkManager
|
||||
.GetInternalBindAddresses()
|
||||
.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork
|
||||
|| (i.AddressFamily == AddressFamily.InterNetworkV6 && i.Address.ScopeId != 0))
|
||||
// IPv6 is currently unsupported
|
||||
var validInterfaces = _networkManager.GetInternalBindAddresses()
|
||||
.Where(x => x.Address is not null)
|
||||
.Where(x => x.AddressFamily != AddressFamily.InterNetworkV6)
|
||||
.ToList();
|
||||
|
||||
if (bindAddresses.Count == 0)
|
||||
if (validInterfaces.Count == 0)
|
||||
{
|
||||
// No interfaces returned, so use loopback.
|
||||
bindAddresses = _networkManager.GetLoopbacks().ToList();
|
||||
// No interfaces returned, fall back to loopback
|
||||
validInterfaces = _networkManager.GetLoopbacks().ToList();
|
||||
}
|
||||
|
||||
foreach (var address in bindAddresses)
|
||||
foreach (var intf in validInterfaces)
|
||||
{
|
||||
if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
// Not supporting IPv6 right now
|
||||
continue;
|
||||
}
|
||||
|
||||
var fullService = "urn:schemas-upnp-org:device:MediaServer:1";
|
||||
|
||||
_logger.LogInformation("Registering publisher for {ResourceName} on {DeviceAddress}", fullService, address.Address);
|
||||
_logger.LogInformation("Registering publisher for {ResourceName} on {DeviceAddress}", fullService, intf.Address);
|
||||
|
||||
var uri = new UriBuilder(_appHost.GetApiUrlForLocalAccess(address.Address, false) + descriptorUri);
|
||||
var uri = new UriBuilder(_appHost.GetApiUrlForLocalAccess(intf.Address, false) + descriptorUri);
|
||||
|
||||
var device = new SsdpRootDevice
|
||||
{
|
||||
CacheLifetime = TimeSpan.FromSeconds(1800), // How long SSDP clients can cache this info.
|
||||
Location = uri.Uri, // Must point to the URL that serves your devices UPnP description document.
|
||||
Address = address.Address,
|
||||
PrefixLength = NetworkExtensions.MaskToCidr(address.Subnet.Prefix),
|
||||
Address = intf.Address,
|
||||
PrefixLength = NetworkExtensions.MaskToCidr(intf.Subnet.Prefix),
|
||||
FriendlyName = "Jellyfin",
|
||||
Manufacturer = "Jellyfin",
|
||||
ModelName = "Jellyfin Server",
|
||||
|
|
|
@ -73,7 +73,11 @@ namespace Emby.Dlna.Ssdp
|
|||
{
|
||||
if (_listenerCount > 0 && _deviceLocator is null && _commsServer is not null)
|
||||
{
|
||||
_deviceLocator = new SsdpDeviceLocator(_commsServer);
|
||||
_deviceLocator = new SsdpDeviceLocator(
|
||||
_commsServer,
|
||||
Environment.OSVersion.Platform.ToString(),
|
||||
// Can not use VersionString here since that includes OS and version
|
||||
Environment.OSVersion.Version.ToString());
|
||||
|
||||
// (Optional) Set the filter so we only see notifications for devices we care about
|
||||
// (can be any search target value i.e device type, uuid value etc - any value that appears in the
|
||||
|
|
|
@ -1025,7 +1025,7 @@ namespace Emby.Server.Implementations
|
|||
return PublishedServerUrl.Trim('/');
|
||||
}
|
||||
|
||||
string smart = NetManager.GetBindInterface(hostname, out var port);
|
||||
string smart = NetManager.GetBindAddress(hostname, out var port);
|
||||
return GetLocalApiUrl(smart.Trim('/'), null, port);
|
||||
}
|
||||
|
||||
|
@ -1033,7 +1033,7 @@ namespace Emby.Server.Implementations
|
|||
public string GetApiUrlForLocalAccess(IPAddress ipAddress = null, bool allowHttps = true)
|
||||
{
|
||||
// With an empty source, the port will be null
|
||||
var smart = NetManager.GetBindAddress(ipAddress, out _);
|
||||
var smart = NetManager.GetBindAddress(ipAddress, out _, true);
|
||||
var scheme = !allowHttps ? Uri.UriSchemeHttp : null;
|
||||
int? port = !allowHttps ? HttpPort : null;
|
||||
return GetLocalApiUrl(smart, scheme, port);
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -80,31 +82,26 @@ namespace Emby.Server.Implementations.EntryPoints
|
|||
if (_enableMultiSocketBinding)
|
||||
{
|
||||
// Add global broadcast socket
|
||||
_udpServers.Add(new UdpServer(_logger, _appHost, _config, System.Net.IPAddress.Broadcast, PortNumber));
|
||||
_udpServers.Add(new UdpServer(_logger, _appHost, _config, IPAddress.Broadcast, PortNumber));
|
||||
|
||||
// Add bind address specific broadcast sockets
|
||||
foreach (var bindAddress in _networkManager.GetInternalBindAddresses())
|
||||
// IPv6 is currently unsupported
|
||||
var validInterfaces = _networkManager.GetInternalBindAddresses().Where(i => i.AddressFamily == AddressFamily.InterNetwork);
|
||||
foreach (var intf in validInterfaces)
|
||||
{
|
||||
if (bindAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
// Not supporting IPv6 right now
|
||||
continue;
|
||||
}
|
||||
|
||||
var broadcastAddress = NetworkExtensions.GetBroadcastAddress(bindAddress.Subnet);
|
||||
var broadcastAddress = NetworkExtensions.GetBroadcastAddress(intf.Subnet);
|
||||
_logger.LogDebug("Binding UDP server to {Address} on port {PortNumber}", broadcastAddress.ToString(), PortNumber);
|
||||
|
||||
_udpServers.Add(new UdpServer(_logger, _appHost, _config, broadcastAddress, PortNumber));
|
||||
var server = new UdpServer(_logger, _appHost, _config, broadcastAddress, PortNumber);
|
||||
server.Start(_cancellationTokenSource.Token);
|
||||
_udpServers.Add(server);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_udpServers.Add(new UdpServer(_logger, _appHost, _config, System.Net.IPAddress.Any, PortNumber));
|
||||
}
|
||||
|
||||
foreach (var server in _udpServers)
|
||||
{
|
||||
var server = new UdpServer(_logger, _appHost, _config, IPAddress.Any, PortNumber);
|
||||
server.Start(_cancellationTokenSource.Token);
|
||||
_udpServers.Add(server);
|
||||
}
|
||||
}
|
||||
catch (SocketException ex)
|
||||
|
@ -133,9 +130,12 @@ namespace Emby.Server.Implementations.EntryPoints
|
|||
|
||||
_cancellationTokenSource.Cancel();
|
||||
_cancellationTokenSource.Dispose();
|
||||
_udpServers.ForEach(s => s.Dispose());
|
||||
_udpServers.Clear();
|
||||
foreach (var server in _udpServers)
|
||||
{
|
||||
server.Dispose();
|
||||
}
|
||||
|
||||
_udpServers.Clear();
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -661,16 +661,16 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|||
// Need a way to set the Receive timeout on the socket otherwise this might never timeout?
|
||||
try
|
||||
{
|
||||
await udpClient.SendToAsync(discBytes, 0, discBytes.Length, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 65001), cancellationToken).ConfigureAwait(false);
|
||||
await udpClient.SendToAsync(discBytes, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 65001), cancellationToken).ConfigureAwait(false);
|
||||
var receiveBuffer = new byte[8192];
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var response = await udpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);
|
||||
var deviceIp = response.RemoteEndPoint.Address.ToString();
|
||||
var response = await udpClient.ReceiveMessageFromAsync(receiveBuffer, new IPEndPoint(IPAddress.Any, 0), cancellationToken).ConfigureAwait(false);
|
||||
var deviceIp = ((IPEndPoint)response.RemoteEndPoint).Address.ToString();
|
||||
|
||||
// check to make sure we have enough bytes received to be a valid message and make sure the 2nd byte is the discover reply byte
|
||||
if (response.ReceivedBytes > 13 && response.Buffer[1] == 3)
|
||||
// Check to make sure we have enough bytes received to be a valid message and make sure the 2nd byte is the discover reply byte
|
||||
if (response.ReceivedBytes > 13 && receiveBuffer[1] == 3)
|
||||
{
|
||||
var deviceAddress = "http://" + deviceIp;
|
||||
|
||||
|
|
|
@ -10,61 +10,63 @@ namespace Emby.Server.Implementations.Net
|
|||
public class SocketFactory : ISocketFactory
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public ISocket CreateUdpBroadcastSocket(int localPort)
|
||||
public Socket CreateUdpBroadcastSocket(int localPort)
|
||||
{
|
||||
if (localPort < 0)
|
||||
{
|
||||
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
|
||||
}
|
||||
|
||||
var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
try
|
||||
{
|
||||
retVal.EnableBroadcast = true;
|
||||
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
|
||||
socket.EnableBroadcast = true;
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
|
||||
socket.Bind(new IPEndPoint(IPAddress.Any, localPort));
|
||||
|
||||
return new UdpSocket(retVal, localPort, IPAddress.Any);
|
||||
return socket;
|
||||
}
|
||||
catch
|
||||
{
|
||||
retVal?.Dispose();
|
||||
socket?.Dispose();
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ISocket CreateSsdpUdpSocket(IPAddress localIp, int localPort)
|
||||
public Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(bindInterface.Address);
|
||||
|
||||
if (localPort < 0)
|
||||
{
|
||||
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
|
||||
}
|
||||
|
||||
var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
try
|
||||
{
|
||||
retVal.EnableBroadcast = true;
|
||||
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
socket.Bind(new IPEndPoint(bindInterface.Address, localPort));
|
||||
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), localIp));
|
||||
return new UdpSocket(retVal, localPort, localIp);
|
||||
return socket;
|
||||
}
|
||||
catch
|
||||
{
|
||||
retVal?.Dispose();
|
||||
socket?.Dispose();
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ISocket CreateUdpMulticastSocket(IPAddress ipAddress, IPAddress bindIpAddress, int multicastTimeToLive, int localPort)
|
||||
public Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(ipAddress);
|
||||
ArgumentNullException.ThrowIfNull(bindIpAddress);
|
||||
var bindIPAddress = bindInterface.Address;
|
||||
ArgumentNullException.ThrowIfNull(multicastAddress);
|
||||
ArgumentNullException.ThrowIfNull(bindIPAddress);
|
||||
|
||||
if (multicastTimeToLive <= 0)
|
||||
{
|
||||
|
@ -76,34 +78,25 @@ namespace Emby.Server.Implementations.Net
|
|||
throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort));
|
||||
}
|
||||
|
||||
var retVal = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
|
||||
retVal.ExclusiveAddressUse = false;
|
||||
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
|
||||
try
|
||||
{
|
||||
// seeing occasional exceptions thrown on qnap
|
||||
// System.Net.Sockets.SocketException (0x80004005): Protocol not available
|
||||
retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
}
|
||||
var interfaceIndex = (int)IPAddress.HostToNetworkOrder(bindInterface.Index);
|
||||
|
||||
try
|
||||
{
|
||||
retVal.EnableBroadcast = true;
|
||||
// retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
|
||||
socket.MulticastLoopback = false;
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
|
||||
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive);
|
||||
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, interfaceIndex);
|
||||
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, interfaceIndex));
|
||||
socket.Bind(new IPEndPoint(multicastAddress, localPort));
|
||||
|
||||
retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipAddress, bindIpAddress));
|
||||
retVal.MulticastLoopback = true;
|
||||
|
||||
return new UdpSocket(retVal, localPort, bindIpAddress);
|
||||
return socket;
|
||||
}
|
||||
catch
|
||||
{
|
||||
retVal?.Dispose();
|
||||
socket?.Dispose();
|
||||
|
||||
throw;
|
||||
}
|
||||
|
|
|
@ -1,267 +0,0 @@
|
|||
#nullable disable
|
||||
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.Net;
|
||||
|
||||
namespace Emby.Server.Implementations.Net
|
||||
{
|
||||
// THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS
|
||||
// Be careful to check any changes compile and work for all platform projects it is shared in.
|
||||
|
||||
public sealed class UdpSocket : ISocket, IDisposable
|
||||
{
|
||||
private readonly int _localPort;
|
||||
|
||||
private readonly SocketAsyncEventArgs _receiveSocketAsyncEventArgs = new SocketAsyncEventArgs()
|
||||
{
|
||||
SocketFlags = SocketFlags.None
|
||||
};
|
||||
|
||||
private readonly SocketAsyncEventArgs _sendSocketAsyncEventArgs = new SocketAsyncEventArgs()
|
||||
{
|
||||
SocketFlags = SocketFlags.None
|
||||
};
|
||||
|
||||
private Socket _socket;
|
||||
private bool _disposed = false;
|
||||
private TaskCompletionSource<SocketReceiveResult> _currentReceiveTaskCompletionSource;
|
||||
private TaskCompletionSource<int> _currentSendTaskCompletionSource;
|
||||
|
||||
public UdpSocket(Socket socket, int localPort, IPAddress ip)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(socket);
|
||||
|
||||
_socket = socket;
|
||||
_localPort = localPort;
|
||||
LocalIPAddress = ip;
|
||||
|
||||
_socket.Bind(new IPEndPoint(ip, _localPort));
|
||||
|
||||
InitReceiveSocketAsyncEventArgs();
|
||||
}
|
||||
|
||||
public UdpSocket(Socket socket, IPEndPoint endPoint)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(socket);
|
||||
|
||||
_socket = socket;
|
||||
_socket.Connect(endPoint);
|
||||
|
||||
InitReceiveSocketAsyncEventArgs();
|
||||
}
|
||||
|
||||
public Socket Socket => _socket;
|
||||
|
||||
public IPAddress LocalIPAddress { get; }
|
||||
|
||||
private void InitReceiveSocketAsyncEventArgs()
|
||||
{
|
||||
var receiveBuffer = new byte[8192];
|
||||
_receiveSocketAsyncEventArgs.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
|
||||
_receiveSocketAsyncEventArgs.Completed += OnReceiveSocketAsyncEventArgsCompleted;
|
||||
|
||||
var sendBuffer = new byte[8192];
|
||||
_sendSocketAsyncEventArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
|
||||
_sendSocketAsyncEventArgs.Completed += OnSendSocketAsyncEventArgsCompleted;
|
||||
}
|
||||
|
||||
private void OnReceiveSocketAsyncEventArgsCompleted(object sender, SocketAsyncEventArgs e)
|
||||
{
|
||||
var tcs = _currentReceiveTaskCompletionSource;
|
||||
if (tcs is not null)
|
||||
{
|
||||
_currentReceiveTaskCompletionSource = null;
|
||||
|
||||
if (e.SocketError == SocketError.Success)
|
||||
{
|
||||
tcs.TrySetResult(new SocketReceiveResult
|
||||
{
|
||||
Buffer = e.Buffer,
|
||||
ReceivedBytes = e.BytesTransferred,
|
||||
RemoteEndPoint = e.RemoteEndPoint as IPEndPoint,
|
||||
LocalIPAddress = LocalIPAddress
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.TrySetException(new SocketException((int)e.SocketError));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSendSocketAsyncEventArgsCompleted(object sender, SocketAsyncEventArgs e)
|
||||
{
|
||||
var tcs = _currentSendTaskCompletionSource;
|
||||
if (tcs is not null)
|
||||
{
|
||||
_currentSendTaskCompletionSource = null;
|
||||
|
||||
if (e.SocketError == SocketError.Success)
|
||||
{
|
||||
tcs.TrySetResult(e.BytesTransferred);
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.TrySetException(new SocketException((int)e.SocketError));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IAsyncResult BeginReceive(byte[] buffer, int offset, int count, AsyncCallback callback)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
return _socket.BeginReceiveFrom(buffer, offset, count, SocketFlags.None, ref receivedFromEndPoint, callback, buffer);
|
||||
}
|
||||
|
||||
public int Receive(byte[] buffer, int offset, int count)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
return _socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
|
||||
}
|
||||
|
||||
public SocketReceiveResult EndReceive(IAsyncResult result)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
var sender = new IPEndPoint(IPAddress.Any, 0);
|
||||
var remoteEndPoint = (EndPoint)sender;
|
||||
|
||||
var receivedBytes = _socket.EndReceiveFrom(result, ref remoteEndPoint);
|
||||
|
||||
var buffer = (byte[])result.AsyncState;
|
||||
|
||||
return new SocketReceiveResult
|
||||
{
|
||||
ReceivedBytes = receivedBytes,
|
||||
RemoteEndPoint = (IPEndPoint)remoteEndPoint,
|
||||
Buffer = buffer,
|
||||
LocalIPAddress = LocalIPAddress
|
||||
};
|
||||
}
|
||||
|
||||
public Task<SocketReceiveResult> ReceiveAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
var taskCompletion = new TaskCompletionSource<SocketReceiveResult>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
bool isResultSet = false;
|
||||
|
||||
Action<IAsyncResult> callback = callbackResult =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!isResultSet)
|
||||
{
|
||||
isResultSet = true;
|
||||
taskCompletion.TrySetResult(EndReceive(callbackResult));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
taskCompletion.TrySetException(ex);
|
||||
}
|
||||
};
|
||||
|
||||
var result = BeginReceive(buffer, offset, count, new AsyncCallback(callback));
|
||||
|
||||
if (result.CompletedSynchronously)
|
||||
{
|
||||
callback(result);
|
||||
return taskCompletion.Task;
|
||||
}
|
||||
|
||||
cancellationToken.Register(() => taskCompletion.TrySetCanceled());
|
||||
|
||||
return taskCompletion.Task;
|
||||
}
|
||||
|
||||
public Task SendToAsync(byte[] buffer, int offset, int bytes, IPEndPoint endPoint, CancellationToken cancellationToken)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
var taskCompletion = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
bool isResultSet = false;
|
||||
|
||||
Action<IAsyncResult> callback = callbackResult =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!isResultSet)
|
||||
{
|
||||
isResultSet = true;
|
||||
taskCompletion.TrySetResult(EndSendTo(callbackResult));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
taskCompletion.TrySetException(ex);
|
||||
}
|
||||
};
|
||||
|
||||
var result = BeginSendTo(buffer, offset, bytes, endPoint, new AsyncCallback(callback), null);
|
||||
|
||||
if (result.CompletedSynchronously)
|
||||
{
|
||||
callback(result);
|
||||
return taskCompletion.Task;
|
||||
}
|
||||
|
||||
cancellationToken.Register(() => taskCompletion.TrySetCanceled());
|
||||
|
||||
return taskCompletion.Task;
|
||||
}
|
||||
|
||||
public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, IPEndPoint endPoint, AsyncCallback callback, object state)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
return _socket.BeginSendTo(buffer, offset, size, SocketFlags.None, endPoint, callback, state);
|
||||
}
|
||||
|
||||
public int EndSendTo(IAsyncResult result)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
return _socket.EndSendTo(result);
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(UdpSocket));
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_socket?.Dispose();
|
||||
_receiveSocketAsyncEventArgs.Dispose();
|
||||
_sendSocketAsyncEventArgs.Dispose();
|
||||
_currentReceiveTaskCompletionSource?.TrySetCanceled();
|
||||
_currentSendTaskCompletionSource?.TrySetCanceled();
|
||||
|
||||
_socket = null;
|
||||
_currentReceiveTaskCompletionSource = null;
|
||||
_currentSendTaskCompletionSource = null;
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ using System.Threading;
|
|||
using Jellyfin.Networking.Configuration;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Model.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
@ -699,7 +700,7 @@ namespace Jellyfin.Networking.Manager
|
|||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string GetBindInterface(string source, out int? port)
|
||||
public string GetBindAddress(string source, out int? port)
|
||||
{
|
||||
if (!NetworkExtensions.TryParseHost(source, out var addresses, IsIPv4Enabled, IsIPv6Enabled))
|
||||
{
|
||||
|
@ -711,16 +712,16 @@ namespace Jellyfin.Networking.Manager
|
|||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string GetBindInterface(HttpRequest source, out int? port)
|
||||
public string GetBindAddress(HttpRequest source, out int? port)
|
||||
{
|
||||
var result = GetBindInterface(source.Host.Host, out port);
|
||||
var result = GetBindAddress(source.Host.Host, out port);
|
||||
port ??= source.Host.Port;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string GetBindAddress(IPAddress? source, out int? port)
|
||||
public string GetBindAddress(IPAddress? source, out int? port, bool skipOverrides = false)
|
||||
{
|
||||
port = null;
|
||||
|
||||
|
@ -741,7 +742,7 @@ namespace Jellyfin.Networking.Manager
|
|||
bool isExternal = !_lanSubnets.Any(network => network.Contains(source));
|
||||
_logger.LogDebug("Trying to get bind address for source {Source} - External: {IsExternal}", source, isExternal);
|
||||
|
||||
if (MatchesPublishedServerUrl(source, isExternal, out result))
|
||||
if (!skipOverrides && MatchesPublishedServerUrl(source, isExternal, out result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ public static class WebHostBuilderExtensions
|
|||
bool flagged = false;
|
||||
foreach (var netAdd in addresses)
|
||||
{
|
||||
logger.LogInformation("Kestrel listening on {Address}", IPAddress.IPv6Any.Equals(netAdd.Address) ? "All Addresses" : netAdd);
|
||||
logger.LogInformation("Kestrel is listening on {Address}", IPAddress.IPv6Any.Equals(netAdd.Address) ? "All IPv6 addresses" : netAdd.Address);
|
||||
options.Listen(netAdd.Address, appHost.HttpPort);
|
||||
if (appHost.ListenWithHttps)
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using MediaBrowser.Model.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace MediaBrowser.Common.Net
|
||||
|
@ -70,27 +71,28 @@ namespace MediaBrowser.Common.Net
|
|||
/// <param name="source">Source of the request.</param>
|
||||
/// <param name="port">Optional port returned, if it's part of an override.</param>
|
||||
/// <returns>IP address to use, or loopback address if all else fails.</returns>
|
||||
string GetBindInterface(HttpRequest source, out int? port);
|
||||
string GetBindAddress(HttpRequest source, out int? port);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
|
||||
/// If no bind addresses are specified, an internal interface address is selected.
|
||||
/// (See <see cref="GetBindAddress(IPAddress, out int?)"/>.
|
||||
/// (See <see cref="GetBindAddress(IPAddress, out int?, bool)"/>.
|
||||
/// </summary>
|
||||
/// <param name="source">IP address of the request.</param>
|
||||
/// <param name="port">Optional port returned, if it's part of an override.</param>
|
||||
/// <param name="skipOverrides">Optional boolean denoting if published server overrides should be ignored. Defaults to false.</param>
|
||||
/// <returns>IP address to use, or loopback address if all else fails.</returns>
|
||||
string GetBindAddress(IPAddress source, out int? port);
|
||||
string GetBindAddress(IPAddress source, out int? port, bool skipOverrides = false);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the bind address to use in system URLs. (Server Discovery, PlayTo, LiveTV, SystemInfo)
|
||||
/// If no bind addresses are specified, an internal interface address is selected.
|
||||
/// (See <see cref="GetBindAddress(IPAddress, out int?)"/>.
|
||||
/// (See <see cref="GetBindAddress(IPAddress, out int?, bool)"/>.
|
||||
/// </summary>
|
||||
/// <param name="source">Source of the request.</param>
|
||||
/// <param name="port">Optional port returned, if it's part of an override.</param>
|
||||
/// <returns>IP address to use, or loopback address if all else fails.</returns>
|
||||
string GetBindInterface(string source, out int? port);
|
||||
string GetBindAddress(string source, out int? port);
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of all the MAC addresses associated with active interfaces.
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
<PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
<ProjectReference Include="../Jellyfin.Data/Jellyfin.Data.csproj" />
|
||||
<ProjectReference Include="../src/Jellyfin.Extensions/Jellyfin.Extensions.csproj" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -2,7 +2,7 @@ using System.Net;
|
|||
using System.Net.Sockets;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
namespace MediaBrowser.Common.Net
|
||||
namespace MediaBrowser.Model.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Base network object class.
|
|
@ -1,34 +0,0 @@
|
|||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Model.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a common interface across platforms for UDP sockets used by this SSDP implementation.
|
||||
/// </summary>
|
||||
public interface ISocket : IDisposable
|
||||
{
|
||||
IPAddress LocalIPAddress { get; }
|
||||
|
||||
Task<SocketReceiveResult> ReceiveAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken);
|
||||
|
||||
IAsyncResult BeginReceive(byte[] buffer, int offset, int count, AsyncCallback callback);
|
||||
|
||||
SocketReceiveResult EndReceive(IAsyncResult result);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a UDP message to a particular end point (uni or multicast).
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of type <see cref="byte" /> that contains the data to send.</param>
|
||||
/// <param name="offset">The zero-based position in buffer at which to begin sending data.</param>
|
||||
/// <param name="bytes">The number of bytes to send.</param>
|
||||
/// <param name="endPoint">An <see cref="IPEndPoint" /> that represents the remote device.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
||||
/// <returns>The task object representing the asynchronous operation.</returns>
|
||||
Task SendToAsync(byte[] buffer, int offset, int bytes, IPEndPoint endPoint, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
|
@ -1,32 +1,36 @@
|
|||
#pragma warning disable CS1591
|
||||
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace MediaBrowser.Model.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Implemented by components that can create a platform specific UDP socket implementation, and wrap it in the cross platform <see cref="ISocket"/> interface.
|
||||
/// Implemented by components that can create specific socket configurations.
|
||||
/// </summary>
|
||||
public interface ISocketFactory
|
||||
{
|
||||
ISocket CreateUdpBroadcastSocket(int localPort);
|
||||
/// <summary>
|
||||
/// Creates a new unicast socket using the specified local port number.
|
||||
/// </summary>
|
||||
/// <param name="localPort">The local port to bind to.</param>
|
||||
/// <returns>A new unicast socket using the specified local port number.</returns>
|
||||
Socket CreateUdpBroadcastSocket(int localPort);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new unicast socket using the specified local port number.
|
||||
/// </summary>
|
||||
/// <param name="localIp">The local IP address to bind to.</param>
|
||||
/// <param name="bindInterface">The bind interface.</param>
|
||||
/// <param name="localPort">The local port to bind to.</param>
|
||||
/// <returns>A new unicast socket using the specified local port number.</returns>
|
||||
ISocket CreateSsdpUdpSocket(IPAddress localIp, int localPort);
|
||||
Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new multicast socket using the specified multicast IP address, multicast time to live and local port.
|
||||
/// </summary>
|
||||
/// <param name="ipAddress">The multicast IP address to bind to.</param>
|
||||
/// <param name="bindIpAddress">The bind IP address.</param>
|
||||
/// <param name="multicastAddress">The multicast IP address to bind to.</param>
|
||||
/// <param name="bindInterface">The bind interface.</param>
|
||||
/// <param name="multicastTimeToLive">The multicast time to live value. Actually a maximum number of network hops for UDP packets.</param>
|
||||
/// <param name="localPort">The local port to bind to.</param>
|
||||
/// <returns>A <see cref="ISocket"/> implementation.</returns>
|
||||
ISocket CreateUdpMulticastSocket(IPAddress ipAddress, IPAddress bindIpAddress, int multicastTimeToLive, int localPort);
|
||||
/// <returns>A new multicast socket using the specfied bind interface, multicast address, multicast time to live and port.</returns>
|
||||
Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,12 +23,12 @@ namespace Rssdp.Infrastructure
|
|||
/// <summary>
|
||||
/// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications.
|
||||
/// </summary>
|
||||
void BeginListeningForBroadcasts();
|
||||
void BeginListeningForMulticast();
|
||||
|
||||
/// <summary>
|
||||
/// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications.
|
||||
/// </summary>
|
||||
void StopListeningForBroadcasts();
|
||||
void StopListeningForMulticast();
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message to a particular address (uni or multicast) and port.
|
||||
|
|
|
@ -25,18 +25,18 @@ namespace Rssdp.Infrastructure
|
|||
* Since stopping the service would be a bad idea (might not be allowed security wise and might
|
||||
* break other apps running on the system) the only other work around is to use two sockets.
|
||||
*
|
||||
* We use one socket to listen for/receive notifications and search requests (_BroadcastListenSocket).
|
||||
* We use a second socket, bound to a different local port, to send search requests and listen for
|
||||
* responses (_SendSocket). The responses are sent to the local port this socket is bound to,
|
||||
* which isn't port 1900 so the MS service doesn't steal them. While the caller can specify a local
|
||||
* We use one group of sockets to listen for/receive notifications and search requests (_MulticastListenSockets).
|
||||
* We use a second group, bound to a different local port, to send search requests and listen for
|
||||
* responses (_SendSockets). The responses are sent to the local ports these sockets are bound to,
|
||||
* which aren't port 1900 so the MS service doesn't steal them. While the caller can specify a local
|
||||
* port to use, we will default to 0 which allows the underlying system to auto-assign a free port.
|
||||
*/
|
||||
|
||||
private object _BroadcastListenSocketSynchroniser = new object();
|
||||
private List<ISocket> _BroadcastListenSockets;
|
||||
private List<Socket> _MulticastListenSockets;
|
||||
|
||||
private object _SendSocketSynchroniser = new object();
|
||||
private List<ISocket> _sendSockets;
|
||||
private List<Socket> _sendSockets;
|
||||
|
||||
private HttpRequestParser _RequestParser;
|
||||
private HttpResponseParser _ResponseParser;
|
||||
|
@ -78,7 +78,7 @@ namespace Rssdp.Infrastructure
|
|||
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="multicastTimeToLive"/> argument is less than or equal to zero.</exception>
|
||||
public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort, int multicastTimeToLive, INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
|
||||
{
|
||||
if (socketFactory == null)
|
||||
if (socketFactory is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(socketFactory));
|
||||
}
|
||||
|
@ -107,25 +107,25 @@ namespace Rssdp.Infrastructure
|
|||
/// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications.
|
||||
/// </summary>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
|
||||
public void BeginListeningForBroadcasts()
|
||||
public void BeginListeningForMulticast()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
lock (_BroadcastListenSocketSynchroniser)
|
||||
{
|
||||
if (_BroadcastListenSockets == null)
|
||||
if (_MulticastListenSockets is null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_BroadcastListenSockets = ListenForBroadcasts();
|
||||
_MulticastListenSockets = CreateMulticastSocketsAndListen();
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
_logger.LogError("Failed to bind to port 1900: {Message}. DLNA will be unavailable", ex.Message);
|
||||
_logger.LogError("Failed to bind to multicast address: {Message}. DLNA will be unavailable", ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in BeginListeningForBroadcasts");
|
||||
_logger.LogError(ex, "Error in BeginListeningForMulticast");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -135,15 +135,19 @@ namespace Rssdp.Infrastructure
|
|||
/// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications.
|
||||
/// </summary>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
|
||||
public void StopListeningForBroadcasts()
|
||||
public void StopListeningForMulticast()
|
||||
{
|
||||
lock (_BroadcastListenSocketSynchroniser)
|
||||
{
|
||||
if (_BroadcastListenSockets != null)
|
||||
if (_MulticastListenSockets is not null)
|
||||
{
|
||||
_logger.LogInformation("{0} disposing _BroadcastListenSocket", GetType().Name);
|
||||
_BroadcastListenSockets.ForEach(s => s.Dispose());
|
||||
_BroadcastListenSockets = null;
|
||||
foreach (var socket in _MulticastListenSockets)
|
||||
{
|
||||
socket.Dispose();
|
||||
}
|
||||
|
||||
_MulticastListenSockets = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -153,7 +157,7 @@ namespace Rssdp.Infrastructure
|
|||
/// </summary>
|
||||
public async Task SendMessage(byte[] messageData, IPEndPoint destination, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
|
||||
{
|
||||
if (messageData == null)
|
||||
if (messageData is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(messageData));
|
||||
}
|
||||
|
@ -177,11 +181,11 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
}
|
||||
|
||||
private async Task SendFromSocket(ISocket socket, byte[] messageData, IPEndPoint destination, CancellationToken cancellationToken)
|
||||
private async Task SendFromSocket(Socket socket, byte[] messageData, IPEndPoint destination, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await socket.SendToAsync(messageData, 0, messageData.Length, destination, cancellationToken).ConfigureAwait(false);
|
||||
await socket.SendToAsync(messageData, destination, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
|
@ -191,37 +195,42 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error sending socket message from {0} to {1}", socket.LocalIPAddress.ToString(), destination.ToString());
|
||||
var localIP = ((IPEndPoint)socket.LocalEndPoint).Address;
|
||||
_logger.LogError(ex, "Error sending socket message from {0} to {1}", localIP.ToString(), destination.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private List<ISocket> GetSendSockets(IPAddress fromLocalIpAddress, IPEndPoint destination)
|
||||
private List<Socket> GetSendSockets(IPAddress fromLocalIpAddress, IPEndPoint destination)
|
||||
{
|
||||
EnsureSendSocketCreated();
|
||||
|
||||
lock (_SendSocketSynchroniser)
|
||||
{
|
||||
var sockets = _sendSockets.Where(i => i.LocalIPAddress.AddressFamily == fromLocalIpAddress.AddressFamily);
|
||||
var sockets = _sendSockets.Where(s => s.AddressFamily == fromLocalIpAddress.AddressFamily);
|
||||
|
||||
// Send from the Any socket and the socket with the matching address
|
||||
if (fromLocalIpAddress.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));
|
||||
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.Any)
|
||||
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(fromLocalIpAddress));
|
||||
|
||||
// If sending to the loopback address, filter the socket list as well
|
||||
if (destination.Address.Equals(IPAddress.Loopback))
|
||||
{
|
||||
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.Any) || i.LocalIPAddress.Equals(IPAddress.Loopback));
|
||||
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.Any)
|
||||
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.Loopback));
|
||||
}
|
||||
}
|
||||
else if (fromLocalIpAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.IPv6Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));
|
||||
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.IPv6Any)
|
||||
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(fromLocalIpAddress));
|
||||
|
||||
// If sending to the loopback address, filter the socket list as well
|
||||
if (destination.Address.Equals(IPAddress.IPv6Loopback))
|
||||
{
|
||||
sockets = sockets.Where(i => i.LocalIPAddress.Equals(IPAddress.IPv6Any) || i.LocalIPAddress.Equals(IPAddress.IPv6Loopback));
|
||||
sockets = sockets.Where(s => ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.IPv6Any)
|
||||
|| ((IPEndPoint)s.LocalEndPoint).Address.Equals(IPAddress.IPv6Loopback));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -239,7 +248,7 @@ namespace Rssdp.Infrastructure
|
|||
/// </summary>
|
||||
public async Task SendMulticastMessage(string message, int sendCount, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
|
||||
{
|
||||
if (message == null)
|
||||
if (message is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
|
@ -275,7 +284,7 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
lock (_SendSocketSynchroniser)
|
||||
{
|
||||
if (_sendSockets != null)
|
||||
if (_sendSockets is not null)
|
||||
{
|
||||
var sockets = _sendSockets.ToList();
|
||||
_sendSockets = null;
|
||||
|
@ -284,7 +293,8 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
foreach (var socket in sockets)
|
||||
{
|
||||
_logger.LogInformation("{0} disposing sendSocket from {1}", GetType().Name, socket.LocalIPAddress);
|
||||
var socketAddress = ((IPEndPoint)socket.LocalEndPoint).Address;
|
||||
_logger.LogInformation("{0} disposing sendSocket from {1}", GetType().Name, socketAddress);
|
||||
socket.Dispose();
|
||||
}
|
||||
}
|
||||
|
@ -312,7 +322,7 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
if (disposing)
|
||||
{
|
||||
StopListeningForBroadcasts();
|
||||
StopListeningForMulticast();
|
||||
|
||||
StopListeningForResponses();
|
||||
}
|
||||
|
@ -321,11 +331,11 @@ namespace Rssdp.Infrastructure
|
|||
private Task SendMessageIfSocketNotDisposed(byte[] messageData, IPEndPoint destination, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
|
||||
{
|
||||
var sockets = _sendSockets;
|
||||
if (sockets != null)
|
||||
if (sockets is not null)
|
||||
{
|
||||
sockets = sockets.ToList();
|
||||
|
||||
var tasks = sockets.Where(s => (fromLocalIpAddress == null || fromLocalIpAddress.Equals(s.LocalIPAddress)))
|
||||
var tasks = sockets.Where(s => (fromLocalIpAddress is null || fromLocalIpAddress.Equals(((IPEndPoint)s.LocalEndPoint).Address)))
|
||||
.Select(s => SendFromSocket(s, messageData, destination, cancellationToken));
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
@ -333,82 +343,78 @@ namespace Rssdp.Infrastructure
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private List<ISocket> ListenForBroadcasts()
|
||||
private List<Socket> CreateMulticastSocketsAndListen()
|
||||
{
|
||||
var sockets = new List<ISocket>();
|
||||
var nonNullBindAddresses = _networkManager.GetInternalBindAddresses().Where(x => x.Address != null);
|
||||
|
||||
var sockets = new List<Socket>();
|
||||
var multicastGroupAddress = IPAddress.Parse(SsdpConstants.MulticastLocalAdminAddress);
|
||||
if (_enableMultiSocketBinding)
|
||||
{
|
||||
foreach (var address in nonNullBindAddresses)
|
||||
{
|
||||
if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
// Not supporting IPv6 right now
|
||||
continue;
|
||||
}
|
||||
// IPv6 is currently unsupported
|
||||
var validInterfaces = _networkManager.GetInternalBindAddresses()
|
||||
.Where(x => x.Address is not null)
|
||||
.Where(x => x.AddressFamily == AddressFamily.InterNetwork)
|
||||
.DistinctBy(x => x.Index);
|
||||
|
||||
foreach (var intf in validInterfaces)
|
||||
{
|
||||
try
|
||||
{
|
||||
sockets.Add(_SocketFactory.CreateUdpMulticastSocket(IPAddress.Parse(SsdpConstants.MulticastLocalAdminAddress), address.Address, _MulticastTtl, SsdpConstants.MulticastPort));
|
||||
var socket = _SocketFactory.CreateUdpMulticastSocket(multicastGroupAddress, intf, _MulticastTtl, SsdpConstants.MulticastPort);
|
||||
_ = ListenToSocketInternal(socket);
|
||||
sockets.Add(socket);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in ListenForBroadcasts. IPAddress: {0}", address);
|
||||
_logger.LogError(ex, "Error in CreateMulticastSocketsAndListen. IP address: {0}", intf.Address);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sockets.Add(_SocketFactory.CreateUdpMulticastSocket(IPAddress.Parse(SsdpConstants.MulticastLocalAdminAddress), IPAddress.Any, _MulticastTtl, SsdpConstants.MulticastPort));
|
||||
}
|
||||
|
||||
foreach (var socket in sockets)
|
||||
{
|
||||
var socket = _SocketFactory.CreateUdpMulticastSocket(multicastGroupAddress, new IPData(IPAddress.Any, null), _MulticastTtl, SsdpConstants.MulticastPort);
|
||||
_ = ListenToSocketInternal(socket);
|
||||
sockets.Add(socket);
|
||||
}
|
||||
|
||||
return sockets;
|
||||
}
|
||||
|
||||
private List<ISocket> CreateSocketAndListenForResponsesAsync()
|
||||
private List<Socket> CreateSendSockets()
|
||||
{
|
||||
var sockets = new List<ISocket>();
|
||||
|
||||
var sockets = new List<Socket>();
|
||||
if (_enableMultiSocketBinding)
|
||||
{
|
||||
foreach (var address in _networkManager.GetInternalBindAddresses())
|
||||
{
|
||||
if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
// Not supporting IPv6 right now
|
||||
continue;
|
||||
}
|
||||
// IPv6 is currently unsupported
|
||||
var validInterfaces = _networkManager.GetInternalBindAddresses()
|
||||
.Where(x => x.Address is not null)
|
||||
.Where(x => x.AddressFamily == AddressFamily.InterNetwork);
|
||||
|
||||
foreach (var intf in validInterfaces)
|
||||
{
|
||||
try
|
||||
{
|
||||
sockets.Add(_SocketFactory.CreateSsdpUdpSocket(address.Address, _LocalPort));
|
||||
var socket = _SocketFactory.CreateSsdpUdpSocket(intf, _LocalPort);
|
||||
_ = ListenToSocketInternal(socket);
|
||||
sockets.Add(socket);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in CreateSsdpUdpSocket. IPAddress: {0}", address);
|
||||
_logger.LogError(ex, "Error in CreateSsdpUdpSocket. IPAddress: {0}", intf.Address);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sockets.Add(_SocketFactory.CreateSsdpUdpSocket(IPAddress.Any, _LocalPort));
|
||||
var socket = _SocketFactory.CreateSsdpUdpSocket(new IPData(IPAddress.Any, null), _LocalPort);
|
||||
_ = ListenToSocketInternal(socket);
|
||||
sockets.Add(socket);
|
||||
}
|
||||
|
||||
foreach (var socket in sockets)
|
||||
{
|
||||
_ = ListenToSocketInternal(socket);
|
||||
}
|
||||
|
||||
return sockets;
|
||||
}
|
||||
|
||||
private async Task ListenToSocketInternal(ISocket socket)
|
||||
private async Task ListenToSocketInternal(Socket socket)
|
||||
{
|
||||
var cancelled = false;
|
||||
var receiveBuffer = new byte[8192];
|
||||
|
@ -417,14 +423,17 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
try
|
||||
{
|
||||
var result = await socket.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, CancellationToken.None).ConfigureAwait(false);
|
||||
var result = await socket.ReceiveMessageFromAsync(receiveBuffer, SocketFlags.None, new IPEndPoint(IPAddress.Any, 0), CancellationToken.None).ConfigureAwait(false);;
|
||||
|
||||
if (result.ReceivedBytes > 0)
|
||||
{
|
||||
// Strange cannot convert compiler error here if I don't explicitly
|
||||
// assign or cast to Action first. Assignment is easier to read,
|
||||
// so went with that.
|
||||
ProcessMessage(UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.RemoteEndPoint, result.LocalIPAddress);
|
||||
var remoteEndpoint = (IPEndPoint)result.RemoteEndPoint;
|
||||
var localEndpointAddress = result.PacketInformation.Address;
|
||||
|
||||
ProcessMessage(
|
||||
UTF8Encoding.UTF8.GetString(receiveBuffer, 0, result.ReceivedBytes),
|
||||
remoteEndpoint,
|
||||
localEndpointAddress);
|
||||
}
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
|
@ -440,11 +449,11 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
private void EnsureSendSocketCreated()
|
||||
{
|
||||
if (_sendSockets == null)
|
||||
if (_sendSockets is null)
|
||||
{
|
||||
lock (_SendSocketSynchroniser)
|
||||
{
|
||||
_sendSockets ??= CreateSocketAndListenForResponsesAsync();
|
||||
_sendSockets ??= CreateSendSockets();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -455,6 +464,7 @@ namespace Rssdp.Infrastructure
|
|||
// requests start with a method which can vary and might be one we haven't
|
||||
// seen/don't know. We'll check if this message is a request or a response
|
||||
// by checking for the HTTP/ prefix on the start of the message.
|
||||
_logger.LogDebug("Received data from {From} on {Port} at {Address}:\n{Data}", endPoint.Address, endPoint.Port, receivedOnLocalIpAddress, data);
|
||||
if (data.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
HttpResponseMessage responseMessage = null;
|
||||
|
@ -467,7 +477,7 @@ namespace Rssdp.Infrastructure
|
|||
// Ignore invalid packets.
|
||||
}
|
||||
|
||||
if (responseMessage != null)
|
||||
if (responseMessage is not null)
|
||||
{
|
||||
OnResponseReceived(responseMessage, endPoint, receivedOnLocalIpAddress);
|
||||
}
|
||||
|
@ -484,7 +494,7 @@ namespace Rssdp.Infrastructure
|
|||
// Ignore invalid packets.
|
||||
}
|
||||
|
||||
if (requestMessage != null)
|
||||
if (requestMessage is not null)
|
||||
{
|
||||
OnRequestReceived(requestMessage, endPoint, receivedOnLocalIpAddress);
|
||||
}
|
||||
|
@ -502,7 +512,7 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
|
||||
var handlers = this.RequestReceived;
|
||||
if (handlers != null)
|
||||
if (handlers is not null)
|
||||
{
|
||||
handlers(this, new RequestReceivedEventArgs(data, remoteEndPoint, receivedOnLocalIpAddress));
|
||||
}
|
||||
|
@ -511,7 +521,7 @@ namespace Rssdp.Infrastructure
|
|||
private void OnResponseReceived(HttpResponseMessage data, IPEndPoint endPoint, IPAddress localIpAddress)
|
||||
{
|
||||
var handlers = this.ResponseReceived;
|
||||
if (handlers != null)
|
||||
if (handlers is not null)
|
||||
{
|
||||
handlers(this, new ResponseReceivedEventArgs(data, endPoint)
|
||||
{
|
||||
|
|
|
@ -26,6 +26,8 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
internal const string SsdpDeviceDescriptionXmlNamespace = "urn:schemas-upnp-org:device-1-0";
|
||||
|
||||
internal const string ServerVersion = "1.0";
|
||||
|
||||
/// <summary>
|
||||
/// Default buffer size for receiving SSDP broadcasts. Value is 8192 (bytes).
|
||||
/// </summary>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Rssdp.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
|
@ -19,19 +19,48 @@ namespace Rssdp.Infrastructure
|
|||
private Timer _BroadcastTimer;
|
||||
private object _timerLock = new object();
|
||||
|
||||
private string _OSName;
|
||||
|
||||
private string _OSVersion;
|
||||
|
||||
private readonly TimeSpan DefaultSearchWaitTime = TimeSpan.FromSeconds(4);
|
||||
private readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor.
|
||||
/// </summary>
|
||||
public SsdpDeviceLocator(ISsdpCommunicationsServer communicationsServer)
|
||||
public SsdpDeviceLocator(
|
||||
ISsdpCommunicationsServer communicationsServer,
|
||||
string osName,
|
||||
string osVersion)
|
||||
{
|
||||
if (communicationsServer == null)
|
||||
if (communicationsServer is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(communicationsServer));
|
||||
}
|
||||
|
||||
if (osName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(osName));
|
||||
}
|
||||
|
||||
if (osName.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("osName cannot be an empty string.", nameof(osName));
|
||||
}
|
||||
|
||||
if (osVersion is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(osVersion));
|
||||
}
|
||||
|
||||
if (osVersion.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("osVersion cannot be an empty string.", nameof(osName));
|
||||
}
|
||||
|
||||
_OSName = osName;
|
||||
_OSVersion = osVersion;
|
||||
_CommunicationsServer = communicationsServer;
|
||||
_CommunicationsServer.ResponseReceived += CommsServer_ResponseReceived;
|
||||
|
||||
|
@ -72,7 +101,7 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
lock (_timerLock)
|
||||
{
|
||||
if (_BroadcastTimer == null)
|
||||
if (_BroadcastTimer is null)
|
||||
{
|
||||
_BroadcastTimer = new Timer(OnBroadcastTimerCallback, null, dueTime, period);
|
||||
}
|
||||
|
@ -87,7 +116,7 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
lock (_timerLock)
|
||||
{
|
||||
if (_BroadcastTimer != null)
|
||||
if (_BroadcastTimer is not null)
|
||||
{
|
||||
_BroadcastTimer.Dispose();
|
||||
_BroadcastTimer = null;
|
||||
|
@ -148,7 +177,7 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
private Task SearchAsync(string searchTarget, TimeSpan searchWaitTime, CancellationToken cancellationToken)
|
||||
{
|
||||
if (searchTarget == null)
|
||||
if (searchTarget is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(searchTarget));
|
||||
}
|
||||
|
@ -187,7 +216,7 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
_CommunicationsServer.RequestReceived -= CommsServer_RequestReceived;
|
||||
_CommunicationsServer.RequestReceived += CommsServer_RequestReceived;
|
||||
_CommunicationsServer.BeginListeningForBroadcasts();
|
||||
_CommunicationsServer.BeginListeningForMulticast();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -219,7 +248,7 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
|
||||
var handlers = this.DeviceAvailable;
|
||||
if (handlers != null)
|
||||
if (handlers is not null)
|
||||
{
|
||||
handlers(this, new DeviceAvailableEventArgs(device, isNewDevice)
|
||||
{
|
||||
|
@ -242,7 +271,7 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
|
||||
var handlers = this.DeviceUnavailable;
|
||||
if (handlers != null)
|
||||
if (handlers is not null)
|
||||
{
|
||||
handlers(this, new DeviceUnavailableEventArgs(device, expired));
|
||||
}
|
||||
|
@ -281,7 +310,7 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
var commsServer = _CommunicationsServer;
|
||||
_CommunicationsServer = null;
|
||||
if (commsServer != null)
|
||||
if (commsServer is not null)
|
||||
{
|
||||
commsServer.ResponseReceived -= this.CommsServer_ResponseReceived;
|
||||
commsServer.RequestReceived -= this.CommsServer_RequestReceived;
|
||||
|
@ -295,7 +324,7 @@ namespace Rssdp.Infrastructure
|
|||
lock (_Devices)
|
||||
{
|
||||
var existingDevice = FindExistingDeviceNotification(_Devices, device.NotificationType, device.Usn);
|
||||
if (existingDevice == null)
|
||||
if (existingDevice is null)
|
||||
{
|
||||
_Devices.Add(device);
|
||||
isNewDevice = true;
|
||||
|
@ -329,12 +358,13 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
private Task BroadcastDiscoverMessage(string serviceType, TimeSpan mxValue, CancellationToken cancellationToken)
|
||||
{
|
||||
const string header = "M-SEARCH * HTTP/1.1";
|
||||
|
||||
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
values["HOST"] = "239.255.255.250:1900";
|
||||
values["USER-AGENT"] = "UPnP/1.0 DLNADOC/1.50 Platinum/1.0.4.2";
|
||||
// values["X-EMBY-SERVERID"] = _appHost.SystemId;
|
||||
|
||||
values["USER-AGENT"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
|
||||
values["MAN"] = "\"ssdp:discover\"";
|
||||
|
||||
// Search target
|
||||
|
@ -343,8 +373,6 @@ namespace Rssdp.Infrastructure
|
|||
// Seconds to delay response
|
||||
values["MX"] = "3";
|
||||
|
||||
var header = "M-SEARCH * HTTP/1.1";
|
||||
|
||||
var message = BuildMessage(header, values);
|
||||
|
||||
return _CommunicationsServer.SendMulticastMessage(message, null, cancellationToken);
|
||||
|
@ -358,7 +386,7 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
|
||||
var location = GetFirstHeaderUriValue("Location", message);
|
||||
if (location != null)
|
||||
if (location is not null)
|
||||
{
|
||||
var device = new DiscoveredSsdpDevice()
|
||||
{
|
||||
|
@ -395,7 +423,7 @@ namespace Rssdp.Infrastructure
|
|||
private void ProcessAliveNotification(HttpRequestMessage message, IPAddress IpAddress)
|
||||
{
|
||||
var location = GetFirstHeaderUriValue("Location", message);
|
||||
if (location != null)
|
||||
if (location is not null)
|
||||
{
|
||||
var device = new DiscoveredSsdpDevice()
|
||||
{
|
||||
|
@ -445,7 +473,7 @@ namespace Rssdp.Infrastructure
|
|||
if (message.Headers.Contains(headerName))
|
||||
{
|
||||
message.Headers.TryGetValues(headerName, out values);
|
||||
if (values != null)
|
||||
if (values is not null)
|
||||
{
|
||||
retVal = values.FirstOrDefault();
|
||||
}
|
||||
|
@ -461,7 +489,7 @@ namespace Rssdp.Infrastructure
|
|||
if (message.Headers.Contains(headerName))
|
||||
{
|
||||
message.Headers.TryGetValues(headerName, out values);
|
||||
if (values != null)
|
||||
if (values is not null)
|
||||
{
|
||||
retVal = values.FirstOrDefault();
|
||||
}
|
||||
|
@ -477,7 +505,7 @@ namespace Rssdp.Infrastructure
|
|||
if (request.Headers.Contains(headerName))
|
||||
{
|
||||
request.Headers.TryGetValues(headerName, out values);
|
||||
if (values != null)
|
||||
if (values is not null)
|
||||
{
|
||||
value = values.FirstOrDefault();
|
||||
}
|
||||
|
@ -495,7 +523,7 @@ namespace Rssdp.Infrastructure
|
|||
if (response.Headers.Contains(headerName))
|
||||
{
|
||||
response.Headers.TryGetValues(headerName, out values);
|
||||
if (values != null)
|
||||
if (values is not null)
|
||||
{
|
||||
value = values.FirstOrDefault();
|
||||
}
|
||||
|
@ -508,7 +536,7 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
private TimeSpan CacheAgeFromHeader(System.Net.Http.Headers.CacheControlHeaderValue headerValue)
|
||||
{
|
||||
if (headerValue == null)
|
||||
if (headerValue is null)
|
||||
{
|
||||
return TimeSpan.Zero;
|
||||
}
|
||||
|
@ -565,7 +593,7 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
}
|
||||
|
||||
if (existingDevices != null && existingDevices.Count > 0)
|
||||
if (existingDevices is not null && existingDevices.Count > 0)
|
||||
{
|
||||
foreach (var removedDevice in existingDevices)
|
||||
{
|
||||
|
|
|
@ -4,10 +4,9 @@ using System.Collections.ObjectModel;
|
|||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Net;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
namespace Rssdp.Infrastructure
|
||||
{
|
||||
|
@ -32,8 +31,6 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
private Random _Random;
|
||||
|
||||
private const string ServerVersion = "1.0";
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor.
|
||||
/// </summary>
|
||||
|
@ -43,12 +40,12 @@ namespace Rssdp.Infrastructure
|
|||
string osVersion,
|
||||
bool sendOnlyMatchedHost)
|
||||
{
|
||||
if (communicationsServer == null)
|
||||
if (communicationsServer is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(communicationsServer));
|
||||
}
|
||||
|
||||
if (osName == null)
|
||||
if (osName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(osName));
|
||||
}
|
||||
|
@ -58,7 +55,7 @@ namespace Rssdp.Infrastructure
|
|||
throw new ArgumentException("osName cannot be an empty string.", nameof(osName));
|
||||
}
|
||||
|
||||
if (osVersion == null)
|
||||
if (osVersion is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(osVersion));
|
||||
}
|
||||
|
@ -80,10 +77,13 @@ namespace Rssdp.Infrastructure
|
|||
_OSVersion = osVersion;
|
||||
_sendOnlyMatchedHost = sendOnlyMatchedHost;
|
||||
|
||||
_CommsServer.BeginListeningForBroadcasts();
|
||||
_CommsServer.BeginListeningForMulticast();
|
||||
|
||||
// Send alive notification once on creation
|
||||
SendAllAliveNotifications(null);
|
||||
}
|
||||
|
||||
public void StartBroadcastingAliveMessages(TimeSpan interval)
|
||||
public void StartSendingAliveNotifications(TimeSpan interval)
|
||||
{
|
||||
_RebroadcastAliveNotificationsTimer = new Timer(SendAllAliveNotifications, null, TimeSpan.FromSeconds(5), interval);
|
||||
}
|
||||
|
@ -99,10 +99,9 @@ namespace Rssdp.Infrastructure
|
|||
/// <param name="device">The <see cref="SsdpDevice"/> instance to add.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the <paramref name="device"/> contains property values that are not acceptable to the UPnP 1.0 specification.</exception>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capture task to local variable suppresses compiler warning, but task is not really needed.")]
|
||||
public void AddDevice(SsdpRootDevice device)
|
||||
{
|
||||
if (device == null)
|
||||
if (device is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(device));
|
||||
}
|
||||
|
@ -138,7 +137,7 @@ namespace Rssdp.Infrastructure
|
|||
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
|
||||
public async Task RemoveDevice(SsdpRootDevice device)
|
||||
{
|
||||
if (device == null)
|
||||
if (device is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(device));
|
||||
}
|
||||
|
@ -200,7 +199,7 @@ namespace Rssdp.Infrastructure
|
|||
DisposeRebroadcastTimer();
|
||||
|
||||
var commsServer = _CommsServer;
|
||||
if (commsServer != null)
|
||||
if (commsServer is not null)
|
||||
{
|
||||
commsServer.RequestReceived -= this.CommsServer_RequestReceived;
|
||||
}
|
||||
|
@ -209,7 +208,7 @@ namespace Rssdp.Infrastructure
|
|||
Task.WaitAll(tasks);
|
||||
|
||||
_CommsServer = null;
|
||||
if (commsServer != null)
|
||||
if (commsServer is not null)
|
||||
{
|
||||
if (!commsServer.IsShared)
|
||||
{
|
||||
|
@ -282,23 +281,23 @@ namespace Rssdp.Infrastructure
|
|||
}
|
||||
else if (searchTarget.Trim().StartsWith("uuid:", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
devices = (from device in GetAllDevicesAsFlatEnumerable() where String.Compare(device.Uuid, searchTarget.Substring(5), StringComparison.OrdinalIgnoreCase) == 0 select device).ToArray();
|
||||
devices = GetAllDevicesAsFlatEnumerable().Where(d => String.Compare(d.Uuid, searchTarget.Substring(5), StringComparison.OrdinalIgnoreCase) == 0).ToArray();
|
||||
}
|
||||
else if (searchTarget.StartsWith("urn:", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
devices = (from device in GetAllDevicesAsFlatEnumerable() where String.Compare(device.FullDeviceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 select device).ToArray();
|
||||
devices = GetAllDevicesAsFlatEnumerable().Where(d => String.Compare(d.FullDeviceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
if (devices != null)
|
||||
if (devices is not null)
|
||||
{
|
||||
var deviceList = devices.ToList();
|
||||
// WriteTrace(String.Format("Sending {0} search responses", deviceList.Count));
|
||||
|
||||
foreach (var device in deviceList)
|
||||
foreach (var device in devices)
|
||||
{
|
||||
var root = device.ToRootDevice();
|
||||
if (!_sendOnlyMatchedHost || root.Address.Equals(remoteEndPoint.Address))
|
||||
|
||||
if (!_sendOnlyMatchedHost || root.Address.Equals(receivedOnlocalIpAddress))
|
||||
{
|
||||
SendDeviceSearchResponses(device, remoteEndPoint, receivedOnlocalIpAddress, cancellationToken);
|
||||
}
|
||||
|
@ -318,7 +317,7 @@ namespace Rssdp.Infrastructure
|
|||
IPAddress receivedOnlocalIpAddress,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
bool isRootDevice = (device as SsdpRootDevice) != null;
|
||||
bool isRootDevice = (device as SsdpRootDevice) is not null;
|
||||
if (isRootDevice)
|
||||
{
|
||||
SendSearchResponse(SsdpConstants.UpnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.UpnpDeviceTypeRootDevice), endPoint, receivedOnlocalIpAddress, cancellationToken);
|
||||
|
@ -346,19 +345,17 @@ namespace Rssdp.Infrastructure
|
|||
IPAddress receivedOnlocalIpAddress,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var rootDevice = device.ToRootDevice();
|
||||
|
||||
// var additionalheaders = FormatCustomHeadersForResponse(device);
|
||||
|
||||
const string header = "HTTP/1.1 200 OK";
|
||||
|
||||
var rootDevice = device.ToRootDevice();
|
||||
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
values["EXT"] = "";
|
||||
values["DATE"] = DateTime.UtcNow.ToString("r");
|
||||
values["HOST"] = "239.255.255.250:1900";
|
||||
values["CACHE-CONTROL"] = "max-age = " + rootDevice.CacheLifetime.TotalSeconds;
|
||||
values["ST"] = searchTarget;
|
||||
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, ServerVersion);
|
||||
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
|
||||
values["USN"] = uniqueServiceName;
|
||||
values["LOCATION"] = rootDevice.Location.ToString();
|
||||
|
||||
|
@ -367,7 +364,7 @@ namespace Rssdp.Infrastructure
|
|||
try
|
||||
{
|
||||
await _CommsServer.SendMessage(
|
||||
System.Text.Encoding.UTF8.GetBytes(message),
|
||||
Encoding.UTF8.GetBytes(message),
|
||||
endPoint,
|
||||
receivedOnlocalIpAddress,
|
||||
cancellationToken)
|
||||
|
@ -492,7 +489,7 @@ namespace Rssdp.Infrastructure
|
|||
values["DATE"] = DateTime.UtcNow.ToString("r");
|
||||
values["CACHE-CONTROL"] = "max-age = " + rootDevice.CacheLifetime.TotalSeconds;
|
||||
values["LOCATION"] = rootDevice.Location.ToString();
|
||||
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, ServerVersion);
|
||||
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
|
||||
values["NTS"] = "ssdp:alive";
|
||||
values["NT"] = notificationType;
|
||||
values["USN"] = uniqueServiceName;
|
||||
|
@ -527,7 +524,6 @@ namespace Rssdp.Infrastructure
|
|||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "byebye", Justification = "Correct value for this type of notification in SSDP.")]
|
||||
private Task SendByeByeNotification(SsdpDevice device, string notificationType, string uniqueServiceName, CancellationToken cancellationToken)
|
||||
{
|
||||
const string header = "NOTIFY * HTTP/1.1";
|
||||
|
@ -537,7 +533,7 @@ namespace Rssdp.Infrastructure
|
|||
// If needed later for non-server devices, these headers will need to be dynamic
|
||||
values["HOST"] = "239.255.255.250:1900";
|
||||
values["DATE"] = DateTime.UtcNow.ToString("r");
|
||||
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, ServerVersion);
|
||||
values["SERVER"] = string.Format(CultureInfo.InvariantCulture, "{0}/{1} UPnP/1.0 RSSDP/{2}", _OSName, _OSVersion, SsdpConstants.ServerVersion);
|
||||
values["NTS"] = "ssdp:byebye";
|
||||
values["NT"] = notificationType;
|
||||
values["USN"] = uniqueServiceName;
|
||||
|
@ -553,7 +549,7 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
var timer = _RebroadcastAliveNotificationsTimer;
|
||||
_RebroadcastAliveNotificationsTimer = null;
|
||||
if (timer != null)
|
||||
if (timer is not null)
|
||||
{
|
||||
timer.Dispose();
|
||||
}
|
||||
|
@ -581,7 +577,7 @@ namespace Rssdp.Infrastructure
|
|||
{
|
||||
string retVal = null;
|
||||
IEnumerable<String> values = null;
|
||||
if (httpRequestHeaders.TryGetValues(headerName, out values) && values != null)
|
||||
if (httpRequestHeaders.TryGetValues(headerName, out values) && values is not null)
|
||||
{
|
||||
retVal = values.FirstOrDefault();
|
||||
}
|
||||
|
@ -593,7 +589,7 @@ namespace Rssdp.Infrastructure
|
|||
|
||||
private void WriteTrace(string text)
|
||||
{
|
||||
if (LogFunction != null)
|
||||
if (LogFunction is not null)
|
||||
{
|
||||
LogFunction(text);
|
||||
}
|
||||
|
@ -603,7 +599,7 @@ namespace Rssdp.Infrastructure
|
|||
private void WriteTrace(string text, SsdpDevice device)
|
||||
{
|
||||
var rootDevice = device as SsdpRootDevice;
|
||||
if (rootDevice != null)
|
||||
if (rootDevice is not null)
|
||||
{
|
||||
WriteTrace(text + " " + device.DeviceType + " - " + device.Uuid + " - " + rootDevice.Location);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ using Jellyfin.Networking.Configuration;
|
|||
using Jellyfin.Networking.Manager;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Model.Net;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
@ -210,7 +211,7 @@ namespace Jellyfin.Networking.Tests
|
|||
if (resultObj is not null && host.Length > 0)
|
||||
{
|
||||
result = resultObj.First().Address.ToString();
|
||||
var intf = nm.GetBindInterface(source, out _);
|
||||
var intf = nm.GetBindAddress(source, out _);
|
||||
|
||||
Assert.Equal(intf, result);
|
||||
}
|
||||
|
@ -271,7 +272,7 @@ namespace Jellyfin.Networking.Tests
|
|||
result = resultObj.First().Address.ToString();
|
||||
}
|
||||
|
||||
var intf = nm.GetBindInterface(source, out int? _);
|
||||
var intf = nm.GetBindAddress(source, out int? _);
|
||||
|
||||
Assert.Equal(result, intf);
|
||||
}
|
||||
|
@ -334,7 +335,7 @@ namespace Jellyfin.Networking.Tests
|
|||
NetworkManager.MockNetworkSettings = interfaces;
|
||||
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
|
||||
|
||||
var interfaceToUse = nm.GetBindInterface(string.Empty, out _);
|
||||
var interfaceToUse = nm.GetBindAddress(string.Empty, out _);
|
||||
|
||||
Assert.Equal(result, interfaceToUse);
|
||||
}
|
||||
|
@ -358,7 +359,7 @@ namespace Jellyfin.Networking.Tests
|
|||
NetworkManager.MockNetworkSettings = interfaces;
|
||||
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
|
||||
|
||||
var interfaceToUse = nm.GetBindInterface(source, out _);
|
||||
var interfaceToUse = nm.GetBindAddress(source, out _);
|
||||
|
||||
Assert.Equal(result, interfaceToUse);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user