From f8eaf025165544b26b6e2bd4928cf46719c75157 Mon Sep 17 00:00:00 2001 From: Muneeb Majeed Date: Tue, 26 Jan 2021 17:18:52 -0500 Subject: [PATCH] Allow to respond with a reason code in PUBACK/PUBREC --- Source/MQTTnet/Client/MqttClient.cs | 27 ++++++++++--------- ...MqttApplicationMessageReceivedEventArgs.cs | 5 +++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index a6901fe..a415c43 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -637,26 +637,26 @@ namespace MQTTnet.Client } else if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce) { - if (await HandleReceivedApplicationMessageAsync(publishPacket).ConfigureAwait(false)) + var reasonCode = await HandleReceivedApplicationMessageAsync(publishPacket).ConfigureAwait(false); + if (reasonCode != null && Enum.IsDefined(typeof(MqttPubAckReasonCode), reasonCode.Value)) { await SendAsync(new MqttPubAckPacket { PacketIdentifier = publishPacket.PacketIdentifier, - ReasonCode = MqttPubAckReasonCode.Success + ReasonCode = (MqttPubAckReasonCode)reasonCode.Value }, cancellationToken).ConfigureAwait(false); } } else if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce) { - if (await HandleReceivedApplicationMessageAsync(publishPacket).ConfigureAwait(false)) + var reasonCode = await HandleReceivedApplicationMessageAsync(publishPacket).ConfigureAwait(false); + if (reasonCode != null && Enum.IsDefined(typeof(MqttPubRecReasonCode), reasonCode.Value)) { - var pubRecPacket = new MqttPubRecPacket + await SendAsync(new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier, - ReasonCode = MqttPubRecReasonCode.Success - }; - - await SendAsync(pubRecPacket, cancellationToken).ConfigureAwait(false); + ReasonCode = (MqttPubRecReasonCode)reasonCode.Value + }, cancellationToken).ConfigureAwait(false); } } else @@ -756,8 +756,9 @@ namespace MQTTnet.Client return _adapter.PacketFormatterAdapter.DataConverter.CreatePublishResult(pubRecPacket, pubCompPacket); } - async Task HandleReceivedApplicationMessageAsync(MqttPublishPacket publishPacket) + async Task HandleReceivedApplicationMessageAsync(MqttPublishPacket publishPacket) { + int? reasonCode = 0; var applicationMessage = _adapter.PacketFormatterAdapter.DataConverter.CreateApplicationMessage(publishPacket); var handler = ApplicationMessageReceivedHandler; @@ -765,10 +766,12 @@ namespace MQTTnet.Client { var eventArgs = new MqttApplicationMessageReceivedEventArgs(Options.ClientId, applicationMessage); await handler.HandleApplicationMessageReceivedAsync(eventArgs).ConfigureAwait(false); - return !eventArgs.ProcessingFailed; + if (eventArgs.ProcessingFailed) + { + reasonCode = eventArgs.ReasonCode; + } } - - return true; + return reasonCode; } async Task WaitForTaskAsync(Task task, Task sender) diff --git a/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs b/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs index ed46305..8bbc571 100644 --- a/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs +++ b/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs @@ -1,4 +1,5 @@ -using System; +using MQTTnet.Protocol; +using System; namespace MQTTnet { @@ -15,5 +16,7 @@ namespace MQTTnet public MqttApplicationMessage ApplicationMessage { get; } public bool ProcessingFailed { get; set; } + + public int? ReasonCode { get; set; } } }