fix SA1503 for one line if statements

This commit is contained in:
telans
2020-06-20 20:35:29 +12:00
parent 8e3d874802
commit 98db8f72e0
64 changed files with 843 additions and 193 deletions

View File

@@ -22,7 +22,10 @@ namespace Rssdp
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="discoveredDevice"/> parameter is null.</exception>
public DeviceAvailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool isNewlyDiscovered)
{
if (discoveredDevice == null) throw new ArgumentNullException(nameof(discoveredDevice));
if (discoveredDevice == null)
{
throw new ArgumentNullException(nameof(discoveredDevice));
}
_DiscoveredDevice = discoveredDevice;
_IsNewlyDiscovered = isNewlyDiscovered;

View File

@@ -16,7 +16,10 @@ namespace Rssdp
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="device"/> argument is null.</exception>
public DeviceEventArgs(SsdpDevice device)
{
if (device == null) throw new ArgumentNullException(nameof(device));
if (device == null)
{
throw new ArgumentNullException(nameof(device));
}
_Device = device;
}

View File

@@ -19,7 +19,10 @@ namespace Rssdp
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="discoveredDevice"/> parameter is null.</exception>
public DeviceUnavailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool expired)
{
if (discoveredDevice == null) throw new ArgumentNullException(nameof(discoveredDevice));
if (discoveredDevice == null)
{
throw new ArgumentNullException(nameof(discoveredDevice));
}
_DiscoveredDevice = discoveredDevice;
_Expired = expired;

View File

@@ -23,7 +23,10 @@ namespace Rssdp.Infrastructure
/// <seealso cref="Dispose()"/>
protected virtual void ThrowIfDisposed()
{
if (this.IsDisposed) throw new ObjectDisposedException(this.GetType().FullName);
if (this.IsDisposed)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
}
/// <summary>

View File

@@ -31,9 +31,20 @@ namespace Rssdp.Infrastructure
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Honestly, it's fine. MemoryStream doesn't mind.")]
protected virtual void Parse(T message, System.Net.Http.Headers.HttpHeaders headers, string data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (data.Length == 0) throw new ArgumentException("data cannot be an empty string.", nameof(data));
if (!LineTerminators.Any(data.Contains)) throw new ArgumentException("data is not a valid request, it does not contain any CRLF/LF terminators.", nameof(data));
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (data.Length == 0)
{
throw new ArgumentException("data cannot be an empty string.", nameof(data));
}
if (!LineTerminators.Any(data.Contains))
{
throw new ArgumentException("data is not a valid request, it does not contain any CRLF/LF terminators.", nameof(data));
}
using (var retVal = new ByteArrayContent(Array.Empty<byte>()))
{
@@ -66,10 +77,16 @@ namespace Rssdp.Infrastructure
/// <returns>A <see cref="Version"/> object containing the parsed version data.</returns>
protected Version ParseHttpVersion(string versionData)
{
if (versionData == null) throw new ArgumentNullException(nameof(versionData));
if (versionData == null)
{
throw new ArgumentNullException(nameof(versionData));
}
var versionSeparatorIndex = versionData.IndexOf('/');
if (versionSeparatorIndex <= 0 || versionSeparatorIndex == versionData.Length) throw new ArgumentException("request header line is invalid. Http Version not supplied or incorrect format.", nameof(versionData));
if (versionSeparatorIndex <= 0 || versionSeparatorIndex == versionData.Length)
{
throw new ArgumentException("request header line is invalid. Http Version not supplied or incorrect format.", nameof(versionData));
}
return Version.Parse(versionData.Substring(versionSeparatorIndex + 1));
}

View File

@@ -45,11 +45,21 @@ namespace Rssdp.Infrastructure
/// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
protected override void ParseStatusLine(string data, HttpRequestMessage message)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (message == null) throw new ArgumentNullException(nameof(message));
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
var parts = data.Split(' ');
if (parts.Length < 2) throw new ArgumentException("Status line is invalid. Insufficient status parts.", nameof(data));
if (parts.Length < 2)
{
throw new ArgumentException("Status line is invalid. Insufficient status parts.", nameof(data));
}
message.Method = new HttpMethod(parts[0].Trim());
Uri requestUri;

View File

@@ -57,11 +57,21 @@ namespace Rssdp.Infrastructure
/// <param name="message">Either a <see cref="HttpResponseMessage"/> or <see cref="HttpRequestMessage"/> to assign the parsed values to.</param>
protected override void ParseStatusLine(string data, HttpResponseMessage message)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (message == null) throw new ArgumentNullException(nameof(message));
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
var parts = data.Split(' ');
if (parts.Length < 2) throw new ArgumentException("data status line is invalid. Insufficient status parts.", nameof(data));
if (parts.Length < 2)
{
throw new ArgumentException("data status line is invalid. Insufficient status parts.", nameof(data));
}
message.Version = ParseHttpVersion(parts[0].Trim());

View File

@@ -8,8 +8,15 @@ namespace Rssdp.Infrastructure
{
public static IEnumerable<T> SelectManyRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (selector == null) throw new ArgumentNullException(nameof(selector));
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (selector == null)
{
throw new ArgumentNullException(nameof(selector));
}
return !source.Any() ? source :
source.Concat(

View File

@@ -80,8 +80,15 @@ 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) throw new ArgumentNullException(nameof(socketFactory));
if (multicastTimeToLive <= 0) throw new ArgumentOutOfRangeException(nameof(multicastTimeToLive), "multicastTimeToLive must be greater than zero.");
if (socketFactory == null)
{
throw new ArgumentNullException(nameof(socketFactory));
}
if (multicastTimeToLive <= 0)
{
throw new ArgumentOutOfRangeException(nameof(multicastTimeToLive), "multicastTimeToLive must be greater than zero.");
}
_BroadcastListenSocketSynchroniser = new object();
_SendSocketSynchroniser = new object();
@@ -151,7 +158,10 @@ namespace Rssdp.Infrastructure
/// </summary>
public async Task SendMessage(byte[] messageData, IPEndPoint destination, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
{
if (messageData == null) throw new ArgumentNullException(nameof(messageData));
if (messageData == null)
{
throw new ArgumentNullException(nameof(messageData));
}
ThrowIfDisposed();
@@ -234,7 +244,10 @@ namespace Rssdp.Infrastructure
/// </summary>
public async Task SendMulticastMessage(string message, int sendCount, IPAddress fromLocalIpAddress, CancellationToken cancellationToken)
{
if (message == null) throw new ArgumentNullException(nameof(message));
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
byte[] messageData = Encoding.UTF8.GetBytes(message);

View File

@@ -259,9 +259,20 @@ namespace Rssdp
/// <seealso cref="DeviceAdded"/>
public void AddDevice(SsdpEmbeddedDevice device)
{
if (device == null) throw new ArgumentNullException(nameof(device));
if (device.RootDevice != null && device.RootDevice != this.ToRootDevice()) throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch).");
if (device == this) throw new InvalidOperationException("Can't add device to itself.");
if (device == null)
{
throw new ArgumentNullException(nameof(device));
}
if (device.RootDevice != null && device.RootDevice != this.ToRootDevice())
{
throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch).");
}
if (device == this)
{
throw new InvalidOperationException("Can't add device to itself.");
}
bool wasAdded = false;
lock (_Devices)
@@ -287,7 +298,10 @@ namespace Rssdp
/// <seealso cref="DeviceRemoved"/>
public void RemoveDevice(SsdpEmbeddedDevice device)
{
if (device == null) throw new ArgumentNullException(nameof(device));
if (device == null)
{
throw new ArgumentNullException(nameof(device));
}
bool wasRemoved = false;
lock (_Devices)

View File

@@ -27,7 +27,10 @@ namespace Rssdp.Infrastructure
/// </summary>
public SsdpDeviceLocator(ISsdpCommunicationsServer communicationsServer)
{
if (communicationsServer == null) throw new ArgumentNullException(nameof(communicationsServer));
if (communicationsServer == null)
{
throw new ArgumentNullException(nameof(communicationsServer));
}
_CommunicationsServer = communicationsServer;
_CommunicationsServer.ResponseReceived += CommsServer_ResponseReceived;
@@ -140,10 +143,25 @@ namespace Rssdp.Infrastructure
private Task SearchAsync(string searchTarget, TimeSpan searchWaitTime, CancellationToken cancellationToken)
{
if (searchTarget == null) throw new ArgumentNullException(nameof(searchTarget));
if (searchTarget.Length == 0) throw new ArgumentException("searchTarget cannot be an empty string.", nameof(searchTarget));
if (searchWaitTime.TotalSeconds < 0) throw new ArgumentException("searchWaitTime must be a positive time.");
if (searchWaitTime.TotalSeconds > 0 && searchWaitTime.TotalSeconds <= 1) throw new ArgumentException("searchWaitTime must be zero (if you are not using the result and relying entirely in the events), or greater than one second.");
if (searchTarget == null)
{
throw new ArgumentNullException(nameof(searchTarget));
}
if (searchTarget.Length == 0)
{
throw new ArgumentException("searchTarget cannot be an empty string.", nameof(searchTarget));
}
if (searchWaitTime.TotalSeconds < 0)
{
throw new ArgumentException("searchWaitTime must be a positive time.");
}
if (searchWaitTime.TotalSeconds > 0 && searchWaitTime.TotalSeconds <= 1)
{
throw new ArgumentException("searchWaitTime must be zero (if you are not using the result and relying entirely in the events), or greater than one second.");
}
ThrowIfDisposed();
@@ -192,7 +210,10 @@ namespace Rssdp.Infrastructure
/// <seealso cref="DeviceAvailable"/>
protected virtual void OnDeviceAvailable(DiscoveredSsdpDevice device, bool isNewDevice, IPAddress localIpAddress)
{
if (this.IsDisposed) return;
if (this.IsDisposed)
{
return;
}
var handlers = this.DeviceAvailable;
if (handlers != null)
@@ -210,11 +231,16 @@ namespace Rssdp.Infrastructure
/// <seealso cref="DeviceUnavailable"/>
protected virtual void OnDeviceUnavailable(DiscoveredSsdpDevice device, bool expired)
{
if (this.IsDisposed) return;
if (this.IsDisposed)
{
return;
}
var handlers = this.DeviceUnavailable;
if (handlers != null)
{
handlers(this, new DeviceUnavailableEventArgs(device, expired));
}
}
/// <summary>
@@ -281,7 +307,10 @@ namespace Rssdp.Infrastructure
private void DeviceFound(DiscoveredSsdpDevice device, bool isNewDevice, IPAddress localIpAddress)
{
if (!NotificationTypeMatchesFilter(device)) return;
if (!NotificationTypeMatchesFilter(device))
{
return;
}
OnDeviceAvailable(device, isNewDevice, localIpAddress);
}
@@ -318,7 +347,10 @@ namespace Rssdp.Infrastructure
private void ProcessSearchResponseMessage(HttpResponseMessage message, IPAddress localIpAddress)
{
if (!message.IsSuccessStatusCode) return;
if (!message.IsSuccessStatusCode)
{
return;
}
var location = GetFirstHeaderUriValue("Location", message);
if (location != null)
@@ -339,13 +371,20 @@ namespace Rssdp.Infrastructure
private void ProcessNotificationMessage(HttpRequestMessage message, IPAddress localIpAddress)
{
if (String.Compare(message.Method.Method, "Notify", StringComparison.OrdinalIgnoreCase) != 0) return;
if (String.Compare(message.Method.Method, "Notify", StringComparison.OrdinalIgnoreCase) != 0)
{
return;
}
var notificationType = GetFirstHeaderStringValue("NTS", message);
if (String.Compare(notificationType, SsdpConstants.SsdpKeepAliveNotification, StringComparison.OrdinalIgnoreCase) == 0)
{
ProcessAliveNotification(message, localIpAddress);
}
else if (String.Compare(notificationType, SsdpConstants.SsdpByeByeNotification, StringComparison.OrdinalIgnoreCase) == 0)
{
ProcessByeByeNotification(message);
}
}
private void ProcessAliveNotification(HttpRequestMessage message, IPAddress localIpAddress)
@@ -454,14 +493,20 @@ namespace Rssdp.Infrastructure
private TimeSpan CacheAgeFromHeader(System.Net.Http.Headers.CacheControlHeaderValue headerValue)
{
if (headerValue == null) return TimeSpan.Zero;
if (headerValue == null)
{
return TimeSpan.Zero;
}
return (TimeSpan)(headerValue.MaxAge ?? headerValue.SharedMaxAge ?? TimeSpan.Zero);
}
private void RemoveExpiredDevicesFromCache()
{
if (this.IsDisposed) return;
if (this.IsDisposed)
{
return;
}
DiscoveredSsdpDevice[] expiredDevices = null;
lock (_Devices)
@@ -470,7 +515,10 @@ namespace Rssdp.Infrastructure
foreach (var device in expiredDevices)
{
if (this.IsDisposed) return;
if (this.IsDisposed)
{
return;
}
_Devices.Remove(device);
}
@@ -481,7 +529,10 @@ namespace Rssdp.Infrastructure
// problems.
foreach (var expiredUsn in (from expiredDevice in expiredDevices select expiredDevice.Usn).Distinct())
{
if (this.IsDisposed) return;
if (this.IsDisposed)
{
return;
}
DeviceDied(expiredUsn, true);
}
@@ -495,7 +546,10 @@ namespace Rssdp.Infrastructure
existingDevices = FindExistingDeviceNotifications(_Devices, deviceUsn);
foreach (var existingDevice in existingDevices)
{
if (this.IsDisposed) return true;
if (this.IsDisposed)
{
return true;
}
_Devices.Remove(existingDevice);
}

View File

@@ -40,12 +40,35 @@ namespace Rssdp.Infrastructure
public SsdpDevicePublisher(ISsdpCommunicationsServer communicationsServer, INetworkManager networkManager,
string osName, string osVersion, bool sendOnlyMatchedHost)
{
if (communicationsServer == null) throw new ArgumentNullException(nameof(communicationsServer));
if (networkManager == null) throw new ArgumentNullException(nameof(networkManager));
if (osName == null) throw new ArgumentNullException(nameof(osName));
if (osName.Length == 0) throw new ArgumentException("osName cannot be an empty string.", nameof(osName));
if (osVersion == null) throw new ArgumentNullException(nameof(osVersion));
if (osVersion.Length == 0) throw new ArgumentException("osVersion cannot be an empty string.", nameof(osName));
if (communicationsServer == null)
{
throw new ArgumentNullException(nameof(communicationsServer));
}
if (networkManager == null)
{
throw new ArgumentNullException(nameof(networkManager));
}
if (osName == null)
{
throw new ArgumentNullException(nameof(osName));
}
if (osName.Length == 0)
{
throw new ArgumentException("osName cannot be an empty string.", nameof(osName));
}
if (osVersion == null)
{
throw new ArgumentNullException(nameof(osVersion));
}
if (osVersion.Length == 0)
{
throw new ArgumentException("osVersion cannot be an empty string.", nameof(osName));
}
_SupportPnpRootDevice = true;
_Devices = new List<SsdpRootDevice>();
@@ -82,7 +105,10 @@ namespace Rssdp.Infrastructure
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capture task to local variable supresses compiler warning, but task is not really needed.")]
public void AddDevice(SsdpRootDevice device)
{
if (device == null) throw new ArgumentNullException(nameof(device));
if (device == null)
{
throw new ArgumentNullException(nameof(device));
}
ThrowIfDisposed();
@@ -115,7 +141,10 @@ 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) throw new ArgumentNullException(nameof(device));
if (device == null)
{
throw new ArgumentNullException(nameof(device));
}
bool wasRemoved = false;
lock (_Devices)
@@ -227,10 +256,15 @@ namespace Rssdp.Infrastructure
// return;
}
if (!Int32.TryParse(mx, out maxWaitInterval) || maxWaitInterval <= 0) return;
if (!Int32.TryParse(mx, out maxWaitInterval) || maxWaitInterval <= 0)
{
return;
}
if (maxWaitInterval > 120)
{
maxWaitInterval = _Random.Next(0, 120);
}
// Do not block synchronously as that may tie up a threadpool thread for several seconds.
Task.Delay(_Random.Next(16, (maxWaitInterval * 1000))).ContinueWith((parentTask) =>
@@ -240,13 +274,21 @@ namespace Rssdp.Infrastructure
lock (_Devices)
{
if (String.Compare(SsdpConstants.SsdpDiscoverAllSTHeader, searchTarget, StringComparison.OrdinalIgnoreCase) == 0)
{
devices = GetAllDevicesAsFlatEnumerable().ToArray();
}
else if (String.Compare(SsdpConstants.UpnpDeviceTypeRootDevice, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 || (this.SupportPnpRootDevice && String.Compare(SsdpConstants.PnpDeviceTypeRootDevice, searchTarget, StringComparison.OrdinalIgnoreCase) == 0))
{
devices = _Devices.ToArray();
}
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();
}
else if (searchTarget.StartsWith("urn:", StringComparison.OrdinalIgnoreCase))
{
devices = (from device in GetAllDevicesAsFlatEnumerable() where String.Compare(device.FullDeviceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 select device).ToArray();
}
}
if (devices != null)
@@ -382,7 +424,10 @@ namespace Rssdp.Infrastructure
{
try
{
if (IsDisposed) return;
if (IsDisposed)
{
return;
}
// WriteTrace("Begin Sending Alive Notifications For All Devices");
@@ -394,7 +439,10 @@ namespace Rssdp.Infrastructure
foreach (var device in devices)
{
if (IsDisposed) return;
if (IsDisposed)
{
return;
}
SendAliveNotifications(device, true, CancellationToken.None);
}
@@ -547,7 +595,10 @@ namespace Rssdp.Infrastructure
private void CommsServer_RequestReceived(object sender, RequestReceivedEventArgs e)
{
if (this.IsDisposed) return;
if (this.IsDisposed)
{
return;
}
if (string.Equals(e.Message.Method.Method, SsdpConstants.MSearchMethod, StringComparison.OrdinalIgnoreCase))
{