aboutsummaryrefslogtreecommitdiff
path: root/RSSDP/SsdpDevicePublisher.cs
diff options
context:
space:
mode:
Diffstat (limited to 'RSSDP/SsdpDevicePublisher.cs')
-rw-r--r--RSSDP/SsdpDevicePublisher.cs85
1 files changed, 40 insertions, 45 deletions
diff --git a/RSSDP/SsdpDevicePublisher.cs b/RSSDP/SsdpDevicePublisher.cs
index a7767b3c0..8b5551899 100644
--- a/RSSDP/SsdpDevicePublisher.cs
+++ b/RSSDP/SsdpDevicePublisher.cs
@@ -4,9 +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;
namespace Rssdp.Infrastructure
{
@@ -31,8 +31,6 @@ namespace Rssdp.Infrastructure
private Random _Random;
- private const string ServerVersion = "1.0";
-
/// <summary>
/// Default constructor.
/// </summary>
@@ -42,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));
}
@@ -57,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));
}
@@ -79,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);
}
@@ -98,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));
}
@@ -137,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));
}
@@ -199,7 +199,7 @@ namespace Rssdp.Infrastructure
DisposeRebroadcastTimer();
var commsServer = _CommsServer;
- if (commsServer != null)
+ if (commsServer is not null)
{
commsServer.RequestReceived -= this.CommsServer_RequestReceived;
}
@@ -208,7 +208,7 @@ namespace Rssdp.Infrastructure
Task.WaitAll(tasks);
_CommsServer = null;
- if (commsServer != null)
+ if (commsServer is not null)
{
if (!commsServer.IsShared)
{
@@ -224,7 +224,7 @@ namespace Rssdp.Infrastructure
string mx,
string searchTarget,
IPEndPoint remoteEndPoint,
- IPAddress receivedOnlocalIpAddress,
+ IPAddress receivedOnlocalIPAddress,
CancellationToken cancellationToken)
{
if (String.IsNullOrEmpty(searchTarget))
@@ -281,27 +281,25 @@ 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();
- var source = new IPNetAddress(root.Address, root.PrefixLength);
- var destination = new IPNetAddress(remoteEndPoint.Address, root.PrefixLength);
- if (!_sendOnlyMatchedHost || source.NetworkAddress.Equals(destination.NetworkAddress))
+
+ if (!_sendOnlyMatchedHost || root.Address.Equals(receivedOnlocalIPAddress))
{
- SendDeviceSearchResponses(device, remoteEndPoint, receivedOnlocalIpAddress, cancellationToken);
+ SendDeviceSearchResponses(device, remoteEndPoint, receivedOnlocalIPAddress, cancellationToken);
}
}
}
@@ -316,22 +314,22 @@ namespace Rssdp.Infrastructure
private void SendDeviceSearchResponses(
SsdpDevice device,
IPEndPoint endPoint,
- IPAddress receivedOnlocalIpAddress,
+ 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);
+ SendSearchResponse(SsdpConstants.UpnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.UpnpDeviceTypeRootDevice), endPoint, receivedOnlocalIPAddress, cancellationToken);
if (this.SupportPnpRootDevice)
{
- SendSearchResponse(SsdpConstants.PnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.PnpDeviceTypeRootDevice), endPoint, receivedOnlocalIpAddress, cancellationToken);
+ SendSearchResponse(SsdpConstants.PnpDeviceTypeRootDevice, device, GetUsn(device.Udn, SsdpConstants.PnpDeviceTypeRootDevice), endPoint, receivedOnlocalIPAddress, cancellationToken);
}
}
- SendSearchResponse(device.Udn, device, device.Udn, endPoint, receivedOnlocalIpAddress, cancellationToken);
+ SendSearchResponse(device.Udn, device, device.Udn, endPoint, receivedOnlocalIPAddress, cancellationToken);
- SendSearchResponse(device.FullDeviceType, device, GetUsn(device.Udn, device.FullDeviceType), endPoint, receivedOnlocalIpAddress, cancellationToken);
+ SendSearchResponse(device.FullDeviceType, device, GetUsn(device.Udn, device.FullDeviceType), endPoint, receivedOnlocalIPAddress, cancellationToken);
}
private string GetUsn(string udn, string fullDeviceType)
@@ -344,22 +342,20 @@ namespace Rssdp.Infrastructure
SsdpDevice device,
string uniqueServiceName,
IPEndPoint endPoint,
- IPAddress receivedOnlocalIpAddress,
+ 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();
@@ -368,9 +364,9 @@ namespace Rssdp.Infrastructure
try
{
await _CommsServer.SendMessage(
- System.Text.Encoding.UTF8.GetBytes(message),
+ Encoding.UTF8.GetBytes(message),
endPoint,
- receivedOnlocalIpAddress,
+ receivedOnlocalIPAddress,
cancellationToken)
.ConfigureAwait(false);
}
@@ -493,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;
@@ -528,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";
@@ -538,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;
@@ -554,7 +549,7 @@ namespace Rssdp.Infrastructure
{
var timer = _RebroadcastAliveNotificationsTimer;
_RebroadcastAliveNotificationsTimer = null;
- if (timer != null)
+ if (timer is not null)
{
timer.Dispose();
}
@@ -582,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();
}
@@ -594,7 +589,7 @@ namespace Rssdp.Infrastructure
private void WriteTrace(string text)
{
- if (LogFunction != null)
+ if (LogFunction is not null)
{
LogFunction(text);
}
@@ -604,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);
}
@@ -630,7 +625,7 @@ namespace Rssdp.Infrastructure
// else if (!e.Message.Headers.Contains("MAN"))
// WriteTrace("Ignoring search request - missing MAN header.");
// else
- ProcessSearchRequest(GetFirstHeaderValue(e.Message.Headers, "MX"), GetFirstHeaderValue(e.Message.Headers, "ST"), e.ReceivedFrom, e.LocalIpAddress, CancellationToken.None);
+ ProcessSearchRequest(GetFirstHeaderValue(e.Message.Headers, "MX"), GetFirstHeaderValue(e.Message.Headers, "ST"), e.ReceivedFrom, e.LocalIPAddress, CancellationToken.None);
}
}