mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 18:43:10 +08:00
Convert null checks to modern ThrowIfNull checks (#2089)
Convert null check to modern ThrowIfNull checks
This commit is contained in:
@@ -12,10 +12,7 @@ public static class MqttConnAckPacketFactory
|
||||
{
|
||||
public static MqttConnAckPacket Create(ValidatingConnectionEventArgs validatingConnectionEventArgs)
|
||||
{
|
||||
if (validatingConnectionEventArgs == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(validatingConnectionEventArgs));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(validatingConnectionEventArgs);
|
||||
|
||||
var connAckPacket = new MqttConnAckPacket
|
||||
{
|
||||
|
||||
@@ -11,15 +11,8 @@ public static class MqttPubAckPacketFactory
|
||||
{
|
||||
public static MqttPubAckPacket Create(MqttPublishPacket publishPacket, DispatchApplicationMessageResult dispatchApplicationMessageResult)
|
||||
{
|
||||
if (publishPacket == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(publishPacket));
|
||||
}
|
||||
|
||||
if (dispatchApplicationMessageResult == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(dispatchApplicationMessageResult));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(publishPacket);
|
||||
ArgumentNullException.ThrowIfNull(dispatchApplicationMessageResult);
|
||||
|
||||
var pubAckPacket = new MqttPubAckPacket
|
||||
{
|
||||
|
||||
@@ -11,10 +11,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
|
||||
{
|
||||
|
||||
@@ -11,10 +11,7 @@ public static class MqttPubRecPacketFactory
|
||||
{
|
||||
public static MqttPacket Create(MqttPublishPacket publishPacket, DispatchApplicationMessageResult dispatchApplicationMessageResult)
|
||||
{
|
||||
if (publishPacket == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(publishPacket));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(publishPacket);
|
||||
|
||||
var pubRecPacket = new MqttPubRecPacket
|
||||
{
|
||||
|
||||
@@ -11,10 +11,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
|
||||
{
|
||||
|
||||
@@ -11,10 +11,7 @@ public static class MqttPublishPacketFactory
|
||||
{
|
||||
public static MqttPublishPacket Create(MqttConnectPacket connectPacket)
|
||||
{
|
||||
if (connectPacket == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(connectPacket));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(connectPacket);
|
||||
|
||||
if (!connectPacket.WillFlag)
|
||||
{
|
||||
@@ -46,10 +43,7 @@ public static class MqttPublishPacketFactory
|
||||
|
||||
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.
|
||||
@@ -75,10 +69,7 @@ public static class MqttPublishPacketFactory
|
||||
|
||||
public static MqttPublishPacket Create(MqttRetainedMessageMatch retainedMessage)
|
||||
{
|
||||
if (retainedMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(retainedMessage));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(retainedMessage);
|
||||
|
||||
var publishPacket = Create(retainedMessage.ApplicationMessage);
|
||||
publishPacket.QualityOfServiceLevel = retainedMessage.SubscriptionQualityOfServiceLevel;
|
||||
|
||||
@@ -10,15 +10,8 @@ public static class MqttSubAckPacketFactory
|
||||
{
|
||||
public static MqttSubAckPacket Create(MqttSubscribePacket subscribePacket, SubscribeResult subscribeResult)
|
||||
{
|
||||
if (subscribePacket == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(subscribePacket));
|
||||
}
|
||||
|
||||
if (subscribeResult == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(subscribeResult));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(subscribePacket);
|
||||
ArgumentNullException.ThrowIfNull(subscribeResult);
|
||||
|
||||
var subAckPacket = new MqttSubAckPacket
|
||||
{
|
||||
|
||||
@@ -10,15 +10,8 @@ namespace MQTTnet.Server.Internal.Formatter
|
||||
{
|
||||
public static MqttUnsubAckPacket Create(MqttUnsubscribePacket unsubscribePacket, UnsubscribeResult unsubscribeResult)
|
||||
{
|
||||
if (unsubscribePacket == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(unsubscribePacket));
|
||||
}
|
||||
|
||||
if (unsubscribeResult == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(unsubscribeResult));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(unsubscribePacket);
|
||||
ArgumentNullException.ThrowIfNull(unsubscribeResult);
|
||||
|
||||
var unsubAckPacket = new MqttUnsubAckPacket
|
||||
{
|
||||
|
||||
@@ -38,10 +38,7 @@ public sealed class MqttClientSessionsManager : ISubscriptionChangedNotification
|
||||
|
||||
public MqttClientSessionsManager(MqttServerOptions options, MqttRetainedMessagesManager retainedMessagesManager, MqttServerEventContainer eventContainer, IMqttNetLogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
_logger = logger.WithSource(nameof(MqttClientSessionsManager));
|
||||
_rootLogger = logger;
|
||||
@@ -53,10 +50,7 @@ public sealed class MqttClientSessionsManager : ISubscriptionChangedNotification
|
||||
|
||||
public async Task CloseAllConnections(MqttServerClientDisconnectOptions options)
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
List<MqttConnectedClient> connections;
|
||||
lock (_clients)
|
||||
@@ -481,15 +475,8 @@ public sealed class MqttClientSessionsManager : ISubscriptionChangedNotification
|
||||
|
||||
public async Task SubscribeAsync(string clientId, ICollection<MqttTopicFilter> topicFilters)
|
||||
{
|
||||
if (clientId == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientId));
|
||||
}
|
||||
|
||||
if (topicFilters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topicFilters));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(clientId);
|
||||
ArgumentNullException.ThrowIfNull(topicFilters);
|
||||
|
||||
var fakeSubscribePacket = new MqttSubscribePacket();
|
||||
fakeSubscribePacket.TopicFilters.AddRange(topicFilters);
|
||||
@@ -510,15 +497,8 @@ public sealed class MqttClientSessionsManager : ISubscriptionChangedNotification
|
||||
|
||||
public Task UnsubscribeAsync(string clientId, ICollection<string> topicFilters)
|
||||
{
|
||||
if (clientId == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientId));
|
||||
}
|
||||
|
||||
if (topicFilters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topicFilters));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(clientId);
|
||||
ArgumentNullException.ThrowIfNull(topicFilters);
|
||||
|
||||
var fakeUnsubscribePacket = new MqttUnsubscribePacket();
|
||||
fakeUnsubscribePacket.TopicFilters.AddRange(topicFilters);
|
||||
|
||||
@@ -50,10 +50,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public void HandleReceivedPacket(MqttPacket packet)
|
||||
{
|
||||
if (packet == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(packet));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(packet);
|
||||
|
||||
// This class is tracking all values from Clients perspective!
|
||||
LastPacketSentTimestamp = DateTime.UtcNow;
|
||||
@@ -74,10 +71,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public void HandleSentPacket(MqttPacket packet)
|
||||
{
|
||||
if (packet == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(packet));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(packet);
|
||||
|
||||
// This class is tracking all values from Clients perspective!
|
||||
LastPacketReceivedTimestamp = DateTime.UtcNow;
|
||||
|
||||
@@ -161,10 +161,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public async Task<SubscribeResult> Subscribe(MqttSubscribePacket subscribePacket, CancellationToken cancellationToken)
|
||||
{
|
||||
if (subscribePacket == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(subscribePacket));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(subscribePacket);
|
||||
|
||||
var retainedApplicationMessages = await _retainedMessagesManager.GetMessages().ConfigureAwait(false);
|
||||
var result = new SubscribeResult(subscribePacket.TopicFilters.Count);
|
||||
@@ -222,10 +219,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public async Task<UnsubscribeResult> Unsubscribe(MqttUnsubscribePacket unsubscribePacket, CancellationToken cancellationToken)
|
||||
{
|
||||
if (unsubscribePacket == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(unsubscribePacket));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(unsubscribePacket);
|
||||
|
||||
var result = new UnsubscribeResult();
|
||||
|
||||
@@ -440,7 +434,7 @@ namespace MQTTnet.Server.Internal
|
||||
var retainedMessageMatch = new MqttRetainedMessageMatch(retainedMessage, createSubscriptionResult.Subscription.GrantedQualityOfServiceLevel);
|
||||
if (retainedMessageMatch.SubscriptionQualityOfServiceLevel > retainedMessageMatch.ApplicationMessage.QualityOfServiceLevel)
|
||||
{
|
||||
// UPGRADING the QoS is not allowed!
|
||||
// UPGRADING the QoS is not allowed!
|
||||
// From MQTT spec: Subscribing to a Topic Filter at QoS 2 is equivalent to saying
|
||||
// "I would like to receive Messages matching this filter at the QoS with which they were published".
|
||||
// This means a publisher is responsible for determining the maximum QoS a Message can be delivered at,
|
||||
@@ -455,7 +449,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
subscribeResult.RetainedMessages.Add(retainedMessageMatch);
|
||||
|
||||
// Clear the retained message from the list because the client should receive every message only
|
||||
// Clear the retained message from the list because the client should receive every message only
|
||||
// one time even if multiple subscriptions affect them.
|
||||
retainedMessages[index] = null;
|
||||
}
|
||||
|
||||
@@ -48,10 +48,7 @@ public sealed class MqttConnectedClient : IDisposable
|
||||
Endpoint = channelAdapter.Endpoint;
|
||||
Session = session ?? throw new ArgumentNullException(nameof(session));
|
||||
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
_logger = logger.WithSource(nameof(MqttConnectedClient));
|
||||
}
|
||||
|
||||
@@ -19,8 +19,7 @@ namespace MQTTnet.Server.Internal
|
||||
{
|
||||
_eventContainer = eventContainer ?? throw new ArgumentNullException(nameof(eventContainer));
|
||||
|
||||
if (logger == null)
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
_logger = logger.WithSource(nameof(MqttRetainedMessagesManager));
|
||||
}
|
||||
@@ -53,10 +52,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public async Task UpdateMessage(string clientId, MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(applicationMessage);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -27,10 +27,7 @@ namespace MQTTnet.Server.Internal
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
_sessionsManager = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));
|
||||
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
_logger = logger.WithSource(nameof(MqttServerKeepAliveMonitor));
|
||||
}
|
||||
|
||||
@@ -34,10 +34,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public bool TryGetSession(string id, out MqttSession session)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(id);
|
||||
|
||||
if (!_sessions.TryGetValue(id, out session))
|
||||
{
|
||||
@@ -63,10 +60,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public bool TryRemoveSession(string id, out MqttSession session)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(id);
|
||||
|
||||
if (_sessions.TryGetValue(id, out session))
|
||||
{
|
||||
@@ -79,10 +73,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public void UpdateSession(string id, MqttSession session)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(id);
|
||||
|
||||
_sessions[id] = session;
|
||||
}
|
||||
|
||||
@@ -31,10 +31,7 @@ public class MqttServer : Disposable
|
||||
{
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
|
||||
if (adapters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(adapters));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(adapters);
|
||||
|
||||
_adapters = adapters.ToList();
|
||||
|
||||
@@ -202,15 +199,8 @@ public class MqttServer : Disposable
|
||||
|
||||
public Task DisconnectClientAsync(string id, MqttServerClientDisconnectOptions options)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(id);
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
ThrowIfNotStarted();
|
||||
|
||||
@@ -226,10 +216,7 @@ public class MqttServer : Disposable
|
||||
|
||||
public Task<MqttApplicationMessage> GetRetainedMessageAsync(string topic)
|
||||
{
|
||||
if (topic == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topic));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(topic);
|
||||
|
||||
ThrowIfNotStarted();
|
||||
|
||||
@@ -252,15 +239,8 @@ public class MqttServer : Disposable
|
||||
|
||||
public Task InjectApplicationMessage(InjectedMqttApplicationMessage injectedApplicationMessage, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (injectedApplicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(injectedApplicationMessage));
|
||||
}
|
||||
|
||||
if (injectedApplicationMessage.ApplicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(injectedApplicationMessage.ApplicationMessage));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(injectedApplicationMessage);
|
||||
ArgumentNullException.ThrowIfNull(injectedApplicationMessage.ApplicationMessage);
|
||||
|
||||
MqttTopicValidator.ThrowIfInvalid(injectedApplicationMessage.ApplicationMessage.Topic);
|
||||
|
||||
@@ -306,10 +286,7 @@ public class MqttServer : Disposable
|
||||
|
||||
public async Task StopAsync(MqttServerStopOptions options)
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -343,15 +320,8 @@ public class MqttServer : Disposable
|
||||
|
||||
public Task SubscribeAsync(string clientId, ICollection<MqttTopicFilter> topicFilters)
|
||||
{
|
||||
if (clientId == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientId));
|
||||
}
|
||||
|
||||
if (topicFilters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topicFilters));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(clientId);
|
||||
ArgumentNullException.ThrowIfNull(topicFilters);
|
||||
|
||||
foreach (var topicFilter in topicFilters)
|
||||
{
|
||||
@@ -366,15 +336,8 @@ public class MqttServer : Disposable
|
||||
|
||||
public Task UnsubscribeAsync(string clientId, ICollection<string> topicFilters)
|
||||
{
|
||||
if (clientId == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientId));
|
||||
}
|
||||
|
||||
if (topicFilters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topicFilters));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(clientId);
|
||||
ArgumentNullException.ThrowIfNull(topicFilters);
|
||||
|
||||
ThrowIfDisposed();
|
||||
ThrowIfNotStarted();
|
||||
@@ -384,10 +347,7 @@ public class MqttServer : Disposable
|
||||
|
||||
public Task UpdateRetainedMessageAsync(MqttApplicationMessage retainedMessage)
|
||||
{
|
||||
if (retainedMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(retainedMessage));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(retainedMessage);
|
||||
|
||||
ThrowIfDisposed();
|
||||
ThrowIfNotStarted();
|
||||
|
||||
@@ -13,10 +13,7 @@ public static class MqttServerExtensions
|
||||
{
|
||||
public static Task DisconnectClientAsync(this MqttServer server, string id, MqttDisconnectReasonCode reasonCode = MqttDisconnectReasonCode.NormalDisconnection)
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(server));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(server);
|
||||
|
||||
return server.DisconnectClientAsync(id, new MqttServerClientDisconnectOptions { ReasonCode = reasonCode });
|
||||
}
|
||||
@@ -28,15 +25,8 @@ public static class MqttServerExtensions
|
||||
MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
|
||||
bool retain = false)
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(server));
|
||||
}
|
||||
|
||||
if (topic == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topic));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(server);
|
||||
ArgumentNullException.ThrowIfNull(topic);
|
||||
|
||||
var payloadBuffer = EmptyBuffer.Array;
|
||||
if (payload is string stringPayload)
|
||||
@@ -57,50 +47,25 @@ public static class MqttServerExtensions
|
||||
|
||||
public static Task StopAsync(this MqttServer server)
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(server));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(server);
|
||||
|
||||
return server.StopAsync(new MqttServerStopOptions());
|
||||
}
|
||||
|
||||
public static Task SubscribeAsync(this MqttServer server, string clientId, params MqttTopicFilter[] topicFilters)
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(server));
|
||||
}
|
||||
|
||||
if (clientId == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientId));
|
||||
}
|
||||
|
||||
if (topicFilters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topicFilters));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(server);
|
||||
ArgumentNullException.ThrowIfNull(clientId);
|
||||
ArgumentNullException.ThrowIfNull(topicFilters);
|
||||
|
||||
return server.SubscribeAsync(clientId, topicFilters);
|
||||
}
|
||||
|
||||
public static Task SubscribeAsync(this MqttServer server, string clientId, string topic)
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(server));
|
||||
}
|
||||
|
||||
if (clientId == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientId));
|
||||
}
|
||||
|
||||
if (topic == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(topic));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(server);
|
||||
ArgumentNullException.ThrowIfNull(clientId);
|
||||
ArgumentNullException.ThrowIfNull(topic);
|
||||
|
||||
var topicFilters = new MqttTopicFilterBuilder().WithTopic(topic).Build();
|
||||
return server.SubscribeAsync(clientId, topicFilters);
|
||||
|
||||
@@ -39,10 +39,7 @@ public sealed class MqttServerFactory
|
||||
|
||||
public MqttServer CreateMqttServer(MqttServerOptions options, IMqttNetLogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
var serverAdapters = DefaultServerAdapters.Select(a => a.Invoke(this));
|
||||
return CreateMqttServer(options, serverAdapters, logger);
|
||||
@@ -50,25 +47,15 @@ public sealed class MqttServerFactory
|
||||
|
||||
public MqttServer CreateMqttServer(MqttServerOptions options, IEnumerable<IMqttServerAdapter> serverAdapters, IMqttNetLogger logger)
|
||||
{
|
||||
if (serverAdapters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serverAdapters));
|
||||
}
|
||||
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(serverAdapters);
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
|
||||
return new MqttServer(options, serverAdapters, logger);
|
||||
}
|
||||
|
||||
public MqttServer CreateMqttServer(MqttServerOptions options, IEnumerable<IMqttServerAdapter> serverAdapters)
|
||||
{
|
||||
if (serverAdapters == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serverAdapters));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(serverAdapters);
|
||||
|
||||
return new MqttServer(options, serverAdapters, DefaultLogger);
|
||||
}
|
||||
|
||||
@@ -180,10 +180,7 @@ namespace MQTTnet.Server
|
||||
|
||||
public MqttServerOptionsBuilder WithEncryptionCertificate(byte[] value, IMqttServerCertificateCredentials credentials = null)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
|
||||
_options.TlsEndpointOptions.CertificateProvider = new BlobCertificateProvider(value)
|
||||
{
|
||||
@@ -195,10 +192,7 @@ namespace MQTTnet.Server
|
||||
|
||||
public MqttServerOptionsBuilder WithEncryptionCertificate(X509Certificate2 certificate)
|
||||
{
|
||||
if (certificate == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(certificate));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(certificate);
|
||||
|
||||
_options.TlsEndpointOptions.CertificateProvider = new X509CertificateProvider(certificate);
|
||||
return this;
|
||||
@@ -206,10 +200,7 @@ namespace MQTTnet.Server
|
||||
|
||||
public MqttServerOptionsBuilder WithEncryptionCertificate(ICertificateProvider certificateProvider)
|
||||
{
|
||||
if (certificateProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(certificateProvider));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(certificateProvider);
|
||||
|
||||
_options.TlsEndpointOptions.CertificateProvider = certificateProvider;
|
||||
|
||||
|
||||
@@ -50,10 +50,7 @@ public sealed class MqttClientStatus
|
||||
|
||||
public Task DisconnectAsync(MqttServerClientDisconnectOptions options)
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
return _client.StopAsync(options);
|
||||
}
|
||||
|
||||
@@ -18,10 +18,7 @@ public static class MqttClientStatusExtensions
|
||||
|
||||
public static Task DisconnectAsync(this MqttClientStatus clientStatus)
|
||||
{
|
||||
if (clientStatus == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientStatus));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(clientStatus);
|
||||
|
||||
return clientStatus.DisconnectAsync(DefaultDisconnectOptions);
|
||||
}
|
||||
|
||||
@@ -42,10 +42,7 @@ public sealed class MqttSessionStatus
|
||||
|
||||
public Task DeliverApplicationMessageAsync(MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(applicationMessage);
|
||||
|
||||
var packetBusItem = new MqttPacketBusItem(MqttPublishPacketFactory.Create(applicationMessage));
|
||||
_session.EnqueueDataPacket(packetBusItem);
|
||||
@@ -55,10 +52,7 @@ public sealed class MqttSessionStatus
|
||||
|
||||
public Task EnqueueApplicationMessageAsync(MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(applicationMessage));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(applicationMessage);
|
||||
|
||||
_session.EnqueueDataPacket(new MqttPacketBusItem(MqttPublishPacketFactory.Create(applicationMessage)));
|
||||
|
||||
|
||||
@@ -15,21 +15,18 @@ namespace MQTTnet.Server
|
||||
_options.DefaultClientDisconnectOptions = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public MqttServerStopOptionsBuilder WithDefaultClientDisconnectOptions(Action<MqttServerClientDisconnectOptionsBuilder> builder)
|
||||
{
|
||||
if (builder == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(builder));
|
||||
}
|
||||
ArgumentNullException.ThrowIfNull(builder);
|
||||
|
||||
var optionsBuilder = new MqttServerClientDisconnectOptionsBuilder();
|
||||
builder.Invoke(optionsBuilder);
|
||||
|
||||
|
||||
_options.DefaultClientDisconnectOptions = optionsBuilder.Build();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public MqttServerStopOptions Build()
|
||||
{
|
||||
return _options;
|
||||
|
||||
Reference in New Issue
Block a user