Convert null checks to modern ThrowIfNull checks (#2089)

Convert null check to modern ThrowIfNull checks
This commit is contained in:
Andrea Vouk
2024-10-26 11:41:58 +02:00
committed by GitHub
parent 0506857f0d
commit 63d85184e1
85 changed files with 209 additions and 783 deletions
+1 -4
View File
@@ -38,10 +38,7 @@ public sealed class MqttChannelAdapter : Disposable, IMqttChannelAdapter
PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullException.ThrowIfNull(logger);
_logger = logger.WithSource(nameof(MqttChannelAdapter));
}
@@ -23,10 +23,7 @@ public sealed class MqttPacketInspector
{
_asyncEvent = asyncEvent ?? throw new ArgumentNullException(nameof(asyncEvent));
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullException.ThrowIfNull(logger);
_logger = logger.WithSource(nameof(MqttPacketInspector));
}
@@ -14,10 +14,7 @@ public sealed class MqttClientConnectResultFactory
{
public MqttClientConnectResult Create(MqttConnAckPacket connAckPacket, MqttProtocolVersion protocolVersion)
{
if (connAckPacket == null)
{
throw new ArgumentNullException(nameof(connAckPacket));
}
ArgumentNullException.ThrowIfNull(connAckPacket);
if (protocolVersion == MqttProtocolVersion.V500)
{
@@ -68,10 +65,8 @@ public sealed class MqttClientConnectResultFactory
static MqttClientConnectResult CreateForMqtt311(MqttConnAckPacket connAckPacket)
{
if (connAckPacket == null)
{
throw new ArgumentNullException(nameof(connAckPacket));
}
ArgumentNullException.ThrowIfNull(connAckPacket);
return new MqttClientConnectResult
{
@@ -84,10 +79,7 @@ public sealed class MqttClientConnectResultFactory
static MqttClientConnectResult CreateForMqtt500(MqttConnAckPacket connAckPacket)
{
if (connAckPacket == null)
{
throw new ArgumentNullException(nameof(connAckPacket));
}
ArgumentNullException.ThrowIfNull(connAckPacket);
return new MqttClientConnectResult
{
@@ -9,7 +9,7 @@ namespace MQTTnet.Diagnostics.Logger
/*
* The logger uses generic parameters in order to avoid boxing of parameter values like integers etc.
*/
public static class MqttNetSourceLoggerExtensions
{
public static void Error<TParameter1>(this MqttNetSourceLogger logger, Exception exception, string message, TParameter1 parameter1)
@@ -91,7 +91,7 @@ namespace MQTTnet.Diagnostics.Logger
logger.Publish(logLevel, message, new object[] { parameter1 }, exception);
}
public static void Publish<TParameter1, TParameter2>(this MqttNetSourceLogger logger, MqttNetLogLevel logLevel, Exception exception, string message, TParameter1 parameter1, TParameter2 parameter2)
{
if (!logger.IsEnabled)
@@ -209,10 +209,7 @@ namespace MQTTnet.Diagnostics.Logger
public static MqttNetSourceLogger WithSource(this IMqttNetLogger logger, string source)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullException.ThrowIfNull(logger);
return new MqttNetSourceLogger(logger, source);
}
@@ -12,10 +12,7 @@ public static class MqttClientDisconnectOptionsValidator
{
public static void ThrowIfNotSupported(MqttClientDisconnectOptions options, MqttProtocolVersion protocolVersion)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
if (protocolVersion == MqttProtocolVersion.V500)
{
@@ -13,10 +13,7 @@ public class MqttExtendedAuthenticationExchangeContext
{
public MqttExtendedAuthenticationExchangeContext(MqttAuthPacket authPacket, MqttClient client)
{
if (authPacket == null)
{
throw new ArgumentNullException(nameof(authPacket));
}
ArgumentNullException.ThrowIfNull(authPacket);
ReasonCode = authPacket.ReasonCode;
ReasonString = authPacket.ReasonString;
@@ -11,10 +11,7 @@ namespace MQTTnet.Formatter
{
public static MqttApplicationMessage Create(MqttPublishPacket publishPacket)
{
if (publishPacket == null)
{
throw new ArgumentNullException(nameof(publishPacket));
}
ArgumentNullException.ThrowIfNull(publishPacket);
return new MqttApplicationMessage
{
+3 -9
View File
@@ -105,10 +105,7 @@ namespace MQTTnet.Formatter
public void Write(MqttBufferWriter propertyWriter)
{
if (propertyWriter == null)
{
throw new ArgumentNullException(nameof(propertyWriter));
}
ArgumentNullException.ThrowIfNull(propertyWriter);
WriteBinary(propertyWriter._buffer, 0, propertyWriter.Length);
}
@@ -140,10 +137,7 @@ namespace MQTTnet.Formatter
public void WriteBinary(byte[] buffer, int offset, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
ArgumentNullException.ThrowIfNull(buffer);
if (count == 0)
{
@@ -292,7 +286,7 @@ namespace MQTTnet.Formatter
if (_position > Length)
{
// Also extend the position because we reached the end of the
// Also extend the position because we reached the end of the
// pre allocated buffer.
Length = _position;
}
@@ -11,10 +11,7 @@ public static class MqttConnectPacketFactory
{
public static MqttConnectPacket Create(MqttClientOptions clientOptions)
{
if (clientOptions == null)
{
throw new ArgumentNullException(nameof(clientOptions));
}
ArgumentNullException.ThrowIfNull(clientOptions);
var connectPacket = new MqttConnectPacket
{
@@ -12,10 +12,7 @@ public static class MqttPubAckPacketFactory
{
public static MqttPubAckPacket Create(MqttApplicationMessageReceivedEventArgs applicationMessageReceivedEventArgs)
{
if (applicationMessageReceivedEventArgs == null)
{
throw new ArgumentNullException(nameof(applicationMessageReceivedEventArgs));
}
ArgumentNullException.ThrowIfNull(applicationMessageReceivedEventArgs);
var pubAckPacket = new MqttPubAckPacket
{
@@ -12,10 +12,7 @@ public static class MqttPubCompPacketFactory
{
public static MqttPubCompPacket Create(MqttPubRelPacket pubRelPacket, MqttApplicationMessageReceivedReasonCode reasonCode)
{
if (pubRelPacket == null)
{
throw new ArgumentNullException(nameof(pubRelPacket));
}
ArgumentNullException.ThrowIfNull(pubRelPacket);
return new MqttPubCompPacket
{
@@ -12,10 +12,7 @@ public static class MqttPubRecPacketFactory
{
public static MqttPubRecPacket Create(MqttApplicationMessageReceivedEventArgs applicationMessageReceivedEventArgs)
{
if (applicationMessageReceivedEventArgs == null)
{
throw new ArgumentNullException(nameof(applicationMessageReceivedEventArgs));
}
ArgumentNullException.ThrowIfNull(applicationMessageReceivedEventArgs);
var pubRecPacket = Create(applicationMessageReceivedEventArgs.PublishPacket, applicationMessageReceivedEventArgs.ReasonCode);
pubRecPacket.UserProperties = applicationMessageReceivedEventArgs.ResponseUserProperties;
@@ -12,10 +12,7 @@ public static class MqttPubRelPacketFactory
{
public static MqttPubRelPacket Create(MqttPubRecPacket pubRecPacket, MqttApplicationMessageReceivedReasonCode reasonCode)
{
if (pubRecPacket == null)
{
throw new ArgumentNullException(nameof(pubRecPacket));
}
ArgumentNullException.ThrowIfNull(pubRecPacket);
return new MqttPubRelPacket
{
@@ -12,10 +12,7 @@ namespace MQTTnet.Formatter
{
public static MqttPublishPacket Create(MqttApplicationMessage applicationMessage)
{
if (applicationMessage == null)
{
throw new ArgumentNullException(nameof(applicationMessage));
}
ArgumentNullException.ThrowIfNull(applicationMessage);
// Copy all values to their matching counterparts.
// The not supported values in MQTT 3.1.1 are not serialized (excluded) later.
@@ -11,10 +11,7 @@ public static class MqttSubscribePacketFactory
{
public static MqttSubscribePacket Create(MqttClientSubscribeOptions clientSubscribeOptions)
{
if (clientSubscribeOptions == null)
{
throw new ArgumentNullException(nameof(clientSubscribeOptions));
}
ArgumentNullException.ThrowIfNull(clientSubscribeOptions);
var packet = new MqttSubscribePacket
{
@@ -11,10 +11,7 @@ public static class MqttUnsubscribePacketFactory
{
public static MqttUnsubscribePacket Create(MqttClientUnsubscribeOptions clientUnsubscribeOptions)
{
if (clientUnsubscribeOptions == null)
{
throw new ArgumentNullException(nameof(clientUnsubscribeOptions));
}
ArgumentNullException.ThrowIfNull(clientUnsubscribeOptions);
var packet = new MqttUnsubscribePacket
{
@@ -91,10 +91,7 @@ namespace MQTTnet.Formatter.V3
public MqttPacketBuffer Encode(MqttPacket packet)
{
if (packet == null)
{
throw new ArgumentNullException(nameof(packet));
}
ArgumentNullException.ThrowIfNull(packet);
// Leave enough head space for max header size (fixed + 4 variable remaining length = 5 bytes)
_bufferWriter.Reset(5);
@@ -797,10 +794,7 @@ namespace MQTTnet.Formatter.V3
void ValidateConnectPacket(MqttConnectPacket packet)
{
if (packet == null)
{
throw new ArgumentNullException(nameof(packet));
}
ArgumentNullException.ThrowIfNull(packet);
if (string.IsNullOrEmpty(packet.ClientId) && !packet.CleanSession)
{
@@ -25,10 +25,7 @@ public sealed class MqttV5PacketEncoder
public MqttPacketBuffer Encode(MqttPacket packet)
{
if (packet == null)
{
throw new ArgumentNullException(nameof(packet));
}
ArgumentNullException.ThrowIfNull(packet);
// Leave enough head space for max header size (fixed + 4 variable remaining length = 5 bytes)
const int ReservedHeaderSize = 5;
@@ -221,7 +221,7 @@ namespace MQTTnet.Formatter.V5
{
if (value)
{
// Absence of the flag means it is supported!
// Absence of the flag means it is supported!
return;
}
@@ -230,10 +230,7 @@ namespace MQTTnet.Formatter.V5
public void WriteTo(MqttBufferWriter target)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
ArgumentNullException.ThrowIfNull(target);
target.WriteVariableByteInteger((uint)_bufferWriter.Length);
target.Write(_bufferWriter);
@@ -124,20 +124,14 @@ public sealed class CrossPlatformSocket : IDisposable
public void Bind(EndPoint localEndPoint)
{
if (localEndPoint is null)
{
throw new ArgumentNullException(nameof(localEndPoint));
}
ArgumentNullException.ThrowIfNull(localEndPoint);
_socket.Bind(localEndPoint);
}
public async Task ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)
{
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}
ArgumentNullException.ThrowIfNull(endPoint);
cancellationToken.ThrowIfCancellationRequested();
@@ -14,7 +14,7 @@ namespace MQTTnet.Implementations
{
public IMqttChannelAdapter CreateClientAdapter(MqttClientOptions options, MqttPacketInspector packetInspector, IMqttNetLogger logger)
{
if (options == null) throw new ArgumentNullException(nameof(options));
ArgumentNullException.ThrowIfNull(options);
IMqttChannel channel;
switch (options.ChannelOptions)
+6 -25
View File
@@ -26,10 +26,7 @@ namespace MQTTnet.Internal
public void AddHandler(Func<TEventArgs, Task> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(handler);
lock (_handlers)
{
@@ -42,10 +39,7 @@ namespace MQTTnet.Internal
public void AddHandler(Action<TEventArgs> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(handler);
lock (_handlers)
{
@@ -75,10 +69,7 @@ namespace MQTTnet.Internal
public void RemoveHandler(Func<TEventArgs, Task> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(handler);
lock (_handlers)
{
@@ -91,10 +82,7 @@ namespace MQTTnet.Internal
public void RemoveHandler(Action<TEventArgs> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
ArgumentNullException.ThrowIfNull(handler);
lock (_handlers)
{
@@ -107,15 +95,8 @@ namespace MQTTnet.Internal
public async Task TryInvokeAsync(TEventArgs eventArgs, MqttNetSourceLogger logger)
{
if (eventArgs == null)
{
throw new ArgumentNullException(nameof(eventArgs));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullException.ThrowIfNull(eventArgs);
ArgumentNullException.ThrowIfNull(logger);
try
{
+1 -4
View File
@@ -116,10 +116,7 @@ namespace MQTTnet.Internal
public bool Approve(IDisposable scope)
{
if (scope == null)
{
throw new ArgumentNullException(nameof(scope));
}
ArgumentNullException.ThrowIfNull(scope);
if (_promise.Task.IsCompleted)
{
+2 -2
View File
@@ -28,7 +28,7 @@ namespace MQTTnet.Internal
public void Enqueue(TItem item)
{
if (item == null) throw new ArgumentNullException(nameof(item));
ArgumentNullException.ThrowIfNull(item);
lock (_syncRoot)
{
@@ -88,7 +88,7 @@ namespace MQTTnet.Internal
public void RemoveFirst(Predicate<TItem> match)
{
if (match == null) throw new ArgumentNullException(nameof(match));
ArgumentNullException.ThrowIfNull(match);
lock (_syncRoot)
{
+1 -4
View File
@@ -124,10 +124,7 @@ namespace MQTTnet.Internal
public void EnqueueItem(MqttPacketBusItem item, MqttPacketBusPartition partition)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
ArgumentNullException.ThrowIfNull(item);
lock (_syncRoot)
{
+1 -4
View File
@@ -24,10 +24,7 @@ namespace MQTTnet.Internal
public static async Task WaitAsync(this Task task, Task sender, MqttNetSourceLogger logger)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullException.ThrowIfNull(logger);
if (task == null)
{
@@ -41,10 +41,7 @@ namespace MQTTnet.LowLevelClient
public async Task ConnectAsync(MqttClientOptions options, CancellationToken cancellationToken)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
if (_adapter != null)
{
@@ -127,10 +124,7 @@ namespace MQTTnet.LowLevelClient
public async Task SendAsync(MqttPacket packet, CancellationToken cancellationToken)
{
if (packet is null)
{
throw new ArgumentNullException(nameof(packet));
}
ArgumentNullException.ThrowIfNull(packet);
var adapter = _adapter;
if (adapter == null)
@@ -11,10 +11,7 @@ public static class MqttApplicationMessageExtensions
{
public static string ConvertPayloadToString(this MqttApplicationMessage applicationMessage)
{
if (applicationMessage == null)
{
throw new ArgumentNullException(nameof(applicationMessage));
}
ArgumentNullException.ThrowIfNull(applicationMessage);
if (applicationMessage.Payload.Length == 0)
{
@@ -13,10 +13,7 @@ namespace MQTTnet
{
public static void ThrowIfNotSupported(MqttApplicationMessage applicationMessage, MqttProtocolVersion protocolVersion)
{
if (applicationMessage == null)
{
throw new ArgumentNullException(nameof(applicationMessage));
}
ArgumentNullException.ThrowIfNull(applicationMessage);
if (protocolVersion == MqttProtocolVersion.V500)
{
@@ -28,38 +25,38 @@ namespace MQTTnet
{
Throw(nameof(applicationMessage.ContentType));
}
if (applicationMessage.UserProperties?.Any() == true)
{
Throw(nameof(applicationMessage.UserProperties));
}
if (applicationMessage.CorrelationData?.Any() == true)
{
Throw(nameof(applicationMessage.CorrelationData));
}
if (applicationMessage.ResponseTopic?.Any() == true)
{
Throw(nameof(applicationMessage.ResponseTopic));
}
if (applicationMessage.SubscriptionIdentifiers?.Any() == true)
{
Throw(nameof(applicationMessage.SubscriptionIdentifiers));
}
if (applicationMessage.TopicAlias > 0)
{
Throw(nameof(applicationMessage.TopicAlias));
}
if (applicationMessage.PayloadFormatIndicator != MqttPayloadFormatIndicator.Unspecified)
{
Throw(nameof(applicationMessage.PayloadFormatIndicator));
}
}
static void Throw(string featureName)
{
throw new NotSupportedException($"Feature {featureName} requires MQTT version 5.0.0.");
+5 -20
View File
@@ -183,10 +183,7 @@ public sealed class MqttClient : Disposable, IMqttClient
public async Task DisconnectAsync(MqttClientDisconnectOptions options, CancellationToken cancellationToken = default)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
ThrowIfDisposed();
@@ -293,10 +290,7 @@ public sealed class MqttClient : Disposable, IMqttClient
public Task SendExtendedAuthenticationExchangeDataAsync(MqttExtendedAuthenticationExchangeData data, CancellationToken cancellationToken = default)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
ArgumentNullException.ThrowIfNull(data);
ThrowIfDisposed();
ThrowIfNotConnected();
@@ -316,10 +310,7 @@ public sealed class MqttClient : Disposable, IMqttClient
public async Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken = default)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
foreach (var topicFilter in options.TopicFilters)
{
@@ -355,10 +346,7 @@ public sealed class MqttClient : Disposable, IMqttClient
public async Task<MqttClientUnsubscribeResult> UnsubscribeAsync(MqttClientUnsubscribeOptions options, CancellationToken cancellationToken = default)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
foreach (var topicFilter in options.TopicFilters)
{
@@ -919,10 +907,7 @@ public sealed class MqttClient : Disposable, IMqttClient
static void ThrowIfOptionsInvalid(MqttClientOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
if (options.ChannelOptions == null)
{
+14 -61
View File
@@ -23,10 +23,7 @@ public static class MqttClientExtensions
List<MqttUserProperty> userProperties = null,
CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
ArgumentNullException.ThrowIfNull(client);
var disconnectOptions = new MqttClientDisconnectOptions
{
@@ -47,15 +44,8 @@ public static class MqttClientExtensions
bool retain = false,
CancellationToken cancellationToken = default)
{
if (mqttClient == null)
{
throw new ArgumentNullException(nameof(mqttClient));
}
if (topic == null)
{
throw new ArgumentNullException(nameof(topic));
}
ArgumentNullException.ThrowIfNull(mqttClient);
ArgumentNullException.ThrowIfNull(topic);
var applicationMessage = new MqttApplicationMessageBuilder().WithTopic(topic)
.WithPayload(payload)
@@ -74,15 +64,8 @@ public static class MqttClientExtensions
bool retain = false,
CancellationToken cancellationToken = default)
{
if (mqttClient == null)
{
throw new ArgumentNullException(nameof(mqttClient));
}
if (topic == null)
{
throw new ArgumentNullException(nameof(topic));
}
ArgumentNullException.ThrowIfNull(mqttClient);
ArgumentNullException.ThrowIfNull(topic);
var applicationMessage = new MqttApplicationMessageBuilder().WithTopic(topic)
.WithPayload(payload)
@@ -118,25 +101,15 @@ public static class MqttClientExtensions
public static Task SendExtendedAuthenticationExchangeDataAsync(this IMqttClient client, MqttExtendedAuthenticationExchangeData data)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
ArgumentNullException.ThrowIfNull(client);
return client.SendExtendedAuthenticationExchangeDataAsync(data, CancellationToken.None);
}
public static Task<MqttClientSubscribeResult> SubscribeAsync(this IMqttClient mqttClient, MqttTopicFilter topicFilter, CancellationToken cancellationToken = default)
{
if (mqttClient == null)
{
throw new ArgumentNullException(nameof(mqttClient));
}
if (topicFilter == null)
{
throw new ArgumentNullException(nameof(topicFilter));
}
ArgumentNullException.ThrowIfNull(mqttClient);
ArgumentNullException.ThrowIfNull(topicFilter);
var subscribeOptions = new MqttClientSubscribeOptionsBuilder().WithTopicFilter(topicFilter).Build();
@@ -149,15 +122,8 @@ public static class MqttClientExtensions
MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
CancellationToken cancellationToken = default)
{
if (mqttClient == null)
{
throw new ArgumentNullException(nameof(mqttClient));
}
if (topic == null)
{
throw new ArgumentNullException(nameof(topic));
}
ArgumentNullException.ThrowIfNull(mqttClient);
ArgumentNullException.ThrowIfNull(topic);
var subscribeOptions = new MqttClientSubscribeOptionsBuilder().WithTopicFilter(topic, qualityOfServiceLevel).Build();
@@ -169,10 +135,7 @@ public static class MqttClientExtensions
MqttClientDisconnectOptionsReason reason = MqttClientDisconnectOptionsReason.NormalDisconnection,
string reasonString = null)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
ArgumentNullException.ThrowIfNull(client);
try
{
@@ -189,10 +152,7 @@ public static class MqttClientExtensions
public static async Task<bool> TryPingAsync(this IMqttClient client, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
ArgumentNullException.ThrowIfNull(client);
try
{
@@ -209,15 +169,8 @@ public static class MqttClientExtensions
public static Task<MqttClientUnsubscribeResult> UnsubscribeAsync(this IMqttClient mqttClient, string topic, CancellationToken cancellationToken = default)
{
if (mqttClient == null)
{
throw new ArgumentNullException(nameof(mqttClient));
}
if (topic == null)
{
throw new ArgumentNullException(nameof(topic));
}
ArgumentNullException.ThrowIfNull(mqttClient);
ArgumentNullException.ThrowIfNull(topic);
var unsubscribeOptions = new MqttClientUnsubscribeOptionsBuilder().WithTopicFilter(topic).Build();
+8 -34
View File
@@ -52,35 +52,22 @@ public sealed class MqttClientFactory
public ILowLevelMqttClient CreateLowLevelMqttClient(IMqttNetLogger logger)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullException.ThrowIfNull(logger);
return new LowLevelMqttClient(_clientAdapterFactory, logger);
}
public ILowLevelMqttClient CreateLowLevelMqttClient(IMqttClientAdapterFactory clientAdapterFactory)
{
if (clientAdapterFactory == null)
{
throw new ArgumentNullException(nameof(clientAdapterFactory));
}
ArgumentNullException.ThrowIfNull(clientAdapterFactory);
return new LowLevelMqttClient(_clientAdapterFactory, DefaultLogger);
}
public ILowLevelMqttClient CreateLowLevelMqttClient(IMqttNetLogger logger, IMqttClientAdapterFactory clientAdapterFactory)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
if (clientAdapterFactory == null)
{
throw new ArgumentNullException(nameof(clientAdapterFactory));
}
ArgumentNullException.ThrowIfNull(logger);
ArgumentNullException.ThrowIfNull(clientAdapterFactory);
return new LowLevelMqttClient(_clientAdapterFactory, logger);
}
@@ -92,35 +79,22 @@ public sealed class MqttClientFactory
public IMqttClient CreateMqttClient(IMqttNetLogger logger)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
ArgumentNullException.ThrowIfNull(logger);
return new MqttClient(_clientAdapterFactory, logger);
}
public IMqttClient CreateMqttClient(IMqttClientAdapterFactory clientAdapterFactory)
{
if (clientAdapterFactory == null)
{
throw new ArgumentNullException(nameof(clientAdapterFactory));
}
ArgumentNullException.ThrowIfNull(clientAdapterFactory);
return new MqttClient(clientAdapterFactory, DefaultLogger);
}
public IMqttClient CreateMqttClient(IMqttNetLogger logger, IMqttClientAdapterFactory clientAdapterFactory)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
if (clientAdapterFactory == null)
{
throw new ArgumentNullException(nameof(clientAdapterFactory));
}
ArgumentNullException.ThrowIfNull(logger);
ArgumentNullException.ThrowIfNull(clientAdapterFactory);
return new MqttClient(clientAdapterFactory, logger);
}
@@ -131,10 +131,7 @@ public sealed class MqttClientOptionsBuilder
public MqttClientOptionsBuilder WithConnectionUri(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
ArgumentNullException.ThrowIfNull(uri);
var port = uri.IsDefaultPort ? null : (int?)uri.Port;
switch (uri.Scheme.ToLower())
@@ -286,10 +283,7 @@ public sealed class MqttClientOptionsBuilder
public MqttClientOptionsBuilder WithTcpServer(string host, int? port = null, AddressFamily addressFamily = AddressFamily.Unspecified)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
ArgumentNullException.ThrowIfNull(host);
_tcpOptions = new MqttClientTcpOptions();
@@ -303,10 +297,7 @@ public sealed class MqttClientOptionsBuilder
public MqttClientOptionsBuilder WithTcpServer(Action<MqttClientTcpOptions> optionsBuilder)
{
if (optionsBuilder == null)
{
throw new ArgumentNullException(nameof(optionsBuilder));
}
ArgumentNullException.ThrowIfNull(optionsBuilder);
_tcpOptions = new MqttClientTcpOptions();
optionsBuilder.Invoke(_tcpOptions);
@@ -332,10 +323,7 @@ public sealed class MqttClientOptionsBuilder
public MqttClientOptionsBuilder WithTlsOptions(Action<MqttClientTlsOptionsBuilder> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(configure);
var builder = new MqttClientTlsOptionsBuilder();
configure.Invoke(builder);
@@ -377,10 +365,7 @@ public sealed class MqttClientOptionsBuilder
public MqttClientOptionsBuilder WithWebSocketServer(Action<MqttClientWebSocketOptionsBuilder> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(configure);
var webSocketOptionsBuilder = new MqttClientWebSocketOptionsBuilder();
configure.Invoke(webSocketOptionsBuilder);
@@ -13,10 +13,7 @@ public static class MqttClientOptionsValidator
{
public static void ThrowIfNotSupported(MqttClientOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
if (options.ProtocolVersion == MqttProtocolVersion.V500)
{
@@ -38,10 +38,7 @@ public sealed class MqttClientTlsOptionsBuilder
public MqttClientTlsOptionsBuilder WithCertificateValidationHandler(Func<MqttClientCertificateValidationEventArgs, bool> certificateValidationHandler)
{
if (certificateValidationHandler == null)
{
throw new ArgumentNullException(nameof(certificateValidationHandler));
}
ArgumentNullException.ThrowIfNull(certificateValidationHandler);
_tlsOptions.CertificateValidationHandler = certificateValidationHandler;
return this;
@@ -49,10 +46,7 @@ public sealed class MqttClientTlsOptionsBuilder
public MqttClientTlsOptionsBuilder WithCertificateSelectionHandler(Func<MqttClientCertificateSelectionEventArgs, X509Certificate> certificateSelectionHandler)
{
if (certificateSelectionHandler == null)
{
throw new ArgumentNullException(nameof(certificateSelectionHandler));
}
ArgumentNullException.ThrowIfNull(certificateSelectionHandler);
_tlsOptions.CertificateSelectionHandler = certificateSelectionHandler;
return this;
@@ -60,10 +54,7 @@ public sealed class MqttClientTlsOptionsBuilder
public MqttClientTlsOptionsBuilder WithClientCertificates(IEnumerable<X509Certificate2> certificates)
{
if (certificates == null)
{
throw new ArgumentNullException(nameof(certificates));
}
ArgumentNullException.ThrowIfNull(certificates);
_tlsOptions.ClientCertificatesProvider = new DefaultMqttCertificatesProvider(certificates);
return this;
@@ -71,10 +62,7 @@ public sealed class MqttClientTlsOptionsBuilder
public MqttClientTlsOptionsBuilder WithClientCertificates(X509Certificate2Collection certificates)
{
if (certificates == null)
{
throw new ArgumentNullException(nameof(certificates));
}
ArgumentNullException.ThrowIfNull(certificates);
_tlsOptions.ClientCertificatesProvider = new DefaultMqttCertificatesProvider(certificates);
return this;
@@ -43,10 +43,7 @@ public sealed class MqttClientWebSocketOptionsBuilder
public MqttClientWebSocketOptionsBuilder WithProxyOptions(Action<MqttClientWebSocketProxyOptionsBuilder> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(configure);
var proxyOptionsBuilder = new MqttClientWebSocketProxyOptionsBuilder();
configure.Invoke(proxyOptionsBuilder);
@@ -46,15 +46,12 @@ namespace MQTTnet.PacketDispatcher
public void Dispose(Exception exception)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
ArgumentNullException.ThrowIfNull(exception);
lock (_waiters)
{
FailAll(exception);
// Make sure that no task can start waiting after this instance is already disposed.
// This will prevent unexpected freezes.
_isDisposed = true;
@@ -63,10 +60,7 @@ namespace MQTTnet.PacketDispatcher
public void FailAll(Exception exception)
{
if (exception == null)
{
throw new ArgumentNullException(nameof(exception));
}
ArgumentNullException.ThrowIfNull(exception);
lock (_waiters)
{
@@ -81,10 +75,7 @@ namespace MQTTnet.PacketDispatcher
public void RemoveAwaitable(IMqttPacketAwaitable awaitable)
{
if (awaitable == null)
{
throw new ArgumentNullException(nameof(awaitable));
}
ArgumentNullException.ThrowIfNull(awaitable);
lock (_waiters)
{
@@ -94,10 +85,7 @@ namespace MQTTnet.PacketDispatcher
public bool TryDispatch(MqttPacket packet)
{
if (packet == null)
{
throw new ArgumentNullException(nameof(packet));
}
ArgumentNullException.ThrowIfNull(packet);
ushort identifier = 0;
if (packet is MqttPacketWithIdentifier packetWithIdentifier)
@@ -111,7 +99,7 @@ namespace MQTTnet.PacketDispatcher
lock (_waiters)
{
ThrowIfDisposed();
for (var i = _waiters.Count - 1; i >= 0; i--)
{
var entry = _waiters[i];
@@ -10,10 +10,7 @@ public static class MqttPacketExtensions
{
public static string GetRfcName(this MqttPacket packet)
{
if (packet == null)
{
throw new ArgumentNullException(nameof(packet));
}
ArgumentNullException.ThrowIfNull(packet);
switch (packet)
{
@@ -11,10 +11,7 @@ namespace MQTTnet.Protocol
{
public static void ThrowIfInvalid(MqttApplicationMessage applicationMessage)
{
if (applicationMessage == null)
{
throw new ArgumentNullException(nameof(applicationMessage));
}
ArgumentNullException.ThrowIfNull(applicationMessage);
if (applicationMessage.TopicAlias == 0)
{
@@ -50,10 +50,7 @@ public sealed class MqttClientSubscribeOptionsBuilder
public MqttClientSubscribeOptionsBuilder WithTopicFilter(Action<MqttTopicFilterBuilder> topicFilterBuilder)
{
if (topicFilterBuilder == null)
{
throw new ArgumentNullException(nameof(topicFilterBuilder));
}
ArgumentNullException.ThrowIfNull(topicFilterBuilder);
var internalTopicFilterBuilder = new MqttTopicFilterBuilder();
topicFilterBuilder(internalTopicFilterBuilder);
@@ -63,20 +60,14 @@ public sealed class MqttClientSubscribeOptionsBuilder
public MqttClientSubscribeOptionsBuilder WithTopicFilter(MqttTopicFilterBuilder topicFilterBuilder)
{
if (topicFilterBuilder == null)
{
throw new ArgumentNullException(nameof(topicFilterBuilder));
}
ArgumentNullException.ThrowIfNull(topicFilterBuilder);
return WithTopicFilter(topicFilterBuilder.Build());
}
public MqttClientSubscribeOptionsBuilder WithTopicFilter(MqttTopicFilter topicFilter)
{
if (topicFilter == null)
{
throw new ArgumentNullException(nameof(topicFilter));
}
ArgumentNullException.ThrowIfNull(topicFilter);
if (_subscribeOptions.TopicFilters == null)
{
@@ -13,10 +13,7 @@ public static class MqttClientSubscribeOptionsValidator
{
public static void ThrowIfNotSupported(MqttClientSubscribeOptions options, MqttProtocolVersion protocolVersion)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
if (protocolVersion == MqttProtocolVersion.V500)
{
@@ -16,15 +16,8 @@ public sealed class MqttClientSubscribeResultFactory
public MqttClientSubscribeResult Create(MqttSubscribePacket subscribePacket, MqttSubAckPacket subAckPacket)
{
if (subscribePacket == null)
{
throw new ArgumentNullException(nameof(subscribePacket));
}
if (subAckPacket == null)
{
throw new ArgumentNullException(nameof(subAckPacket));
}
ArgumentNullException.ThrowIfNull(subscribePacket);
ArgumentNullException.ThrowIfNull(subAckPacket);
// MQTTv5.0.0 handling.
if (subAckPacket.ReasonCodes.Any() && subAckPacket.ReasonCodes.Count != subscribePacket.TopicFilters.Count)
@@ -19,10 +19,7 @@ public sealed class MqttClientUnsubscribeOptionsBuilder
public MqttClientUnsubscribeOptionsBuilder WithTopicFilter(string topic)
{
if (topic is null)
{
throw new ArgumentNullException(nameof(topic));
}
ArgumentNullException.ThrowIfNull(topic);
if (_unsubscribeOptions.TopicFilters is null)
{
@@ -36,10 +33,7 @@ public sealed class MqttClientUnsubscribeOptionsBuilder
public MqttClientUnsubscribeOptionsBuilder WithTopicFilter(MqttTopicFilter topicFilter)
{
if (topicFilter is null)
{
throw new ArgumentNullException(nameof(topicFilter));
}
ArgumentNullException.ThrowIfNull(topicFilter);
return WithTopicFilter(topicFilter.Topic);
}
@@ -59,10 +53,7 @@ public sealed class MqttClientUnsubscribeOptionsBuilder
/// </summary>
public MqttClientUnsubscribeOptionsBuilder WithUserProperty(MqttUserProperty userProperty)
{
if (userProperty is null)
{
throw new ArgumentNullException(nameof(userProperty));
}
ArgumentNullException.ThrowIfNull(userProperty);
if (_unsubscribeOptions.UserProperties is null)
{
@@ -12,10 +12,7 @@ public static class MqttClientUnsubscribeOptionsValidator
{
public static void ThrowIfNotSupported(MqttClientUnsubscribeOptions options, MqttProtocolVersion protocolVersion)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);
if (protocolVersion == MqttProtocolVersion.V500)
{
@@ -15,15 +15,8 @@ public sealed class MqttClientUnsubscribeResultFactory
public MqttClientUnsubscribeResult Create(MqttUnsubscribePacket unsubscribePacket, MqttUnsubAckPacket unsubAckPacket)
{
if (unsubscribePacket == null)
{
throw new ArgumentNullException(nameof(unsubscribePacket));
}
if (unsubAckPacket == null)
{
throw new ArgumentNullException(nameof(unsubAckPacket));
}
ArgumentNullException.ThrowIfNull(unsubscribePacket);
ArgumentNullException.ThrowIfNull(unsubAckPacket);
// MQTTv3.1.1 has no reason code at all!
if (unsubAckPacket.ReasonCodes != null && unsubAckPacket.ReasonCodes.Count != unsubscribePacket.TopicFilters.Count)