From f36f49cb3ed6ec3035121d957a2426cf6ee17306 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat, 13 Aug 2022 12:38:36 +0200 Subject: [PATCH] Add parameter validations. --- MQTTnet.sln.DotSettings | 1 + ...MqttApplicationMessageReceivedEventArgs.cs | 83 ++++++++++--------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/MQTTnet.sln.DotSettings b/MQTTnet.sln.DotSettings index dfaffacc..bf4f67af 100644 --- a/MQTTnet.sln.DotSettings +++ b/MQTTnet.sln.DotSettings @@ -210,6 +210,7 @@ </Or> </Entry.Match> <Entry.SortBy> + <Access Order="Public Internal Protected Private" /> <Name /> </Entry.SortBy> </Entry> diff --git a/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs b/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs index 4bf1c17c..3a699197 100644 --- a/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs +++ b/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs @@ -13,80 +13,83 @@ namespace MQTTnet.Client public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs { readonly Func _acknowledgeHandler; - + int _isAcknowledged; - + public MqttApplicationMessageReceivedEventArgs( - string clientId, + string clientId, MqttApplicationMessage applicationMessage, MqttPublishPacket publishPacket, Func acknowledgeHandler) { ClientId = clientId; ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage)); - PublishPacket = publishPacket; - PacketIdentifier = publishPacket.PacketIdentifier; + PublishPacket = publishPacket ?? throw new ArgumentNullException(nameof(publishPacket)); _acknowledgeHandler = acknowledgeHandler; } - internal MqttPublishPacket PublishPacket { get; set; } - + public MqttApplicationMessage ApplicationMessage { get; } + /// - /// Gets the client identifier. - /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. + /// Gets or sets whether the library should send MQTT ACK packets automatically if required. + /// + public bool AutoAcknowledge { get; set; } = true; + + /// + /// Gets the client identifier. + /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. /// public string ClientId { get; } - public MqttApplicationMessage ApplicationMessage { get; } - - public bool ProcessingFailed { get; set; } - /// - /// Gets or sets the reason code which will be sent to the server. - /// - public MqttApplicationMessageReceivedReasonCode ReasonCode { get; set; } = MqttApplicationMessageReceivedReasonCode.Success; - - /// - /// Gets or sets the user properties which will be sent to the server in the ACK packet etc. - /// - public List ResponseUserProperties { get; } = new List(); - - /// - /// Gets or sets the reason string which will be sent to the server in the ACK packet. - /// - public string ResponseReasonString { get; set; } - - /// - /// Gets or sets whether this message was handled. - /// This value can be used in user code for custom control flow. + /// Gets or sets whether this message was handled. + /// This value can be used in user code for custom control flow. /// public bool IsHandled { get; set; } /// - /// Gets or sets whether the library should send MQTT ACK packets automatically if required. + /// Gets the identifier of the MQTT packet /// - public bool AutoAcknowledge { get; set; } = true; - - public object Tag { get; set; } + public ushort PacketIdentifier => PublishPacket.PacketIdentifier; /// - /// Gets the indentifier of MQTT packet + /// Indicates if the processing of this PUBLISH packet has failed. + /// If the processing has failed the client will not send an ACK packet etc. /// - public ushort PacketIdentifier { get; } + public bool ProcessingFailed { get; set; } - public Task AcknowledgeAsync(CancellationToken cancellationToken) + /// + /// Gets or sets the reason code which will be sent to the server. + /// + public MqttApplicationMessageReceivedReasonCode ReasonCode { get; set; } = MqttApplicationMessageReceivedReasonCode.Success; + + /// + /// Gets or sets the reason string which will be sent to the server in the ACK packet. + /// + public string ResponseReasonString { get; set; } + + /// + /// Gets or sets the user properties which will be sent to the server in the ACK packet etc. + /// + public List ResponseUserProperties { get; } = new List(); + + public object Tag { get; set; } + + internal MqttPublishPacket PublishPacket { get; set; } + + public Task AcknowledgeAsync(CancellationToken cancellationToken) { if (_acknowledgeHandler == null) { throw new NotSupportedException("Deferred acknowledgement of application message is not yet supported in MQTTnet server."); } - + if (Interlocked.CompareExchange(ref _isAcknowledged, 1, 0) == 0) { return _acknowledgeHandler(this, cancellationToken); } - + throw new InvalidOperationException("The application message is already acknowledged."); } } -} +} \ No newline at end of file