浏览代码

Allow to respond with a reason code in PUBACK/PUBREC

release/3.x.x
Muneeb Majeed 3 年前
父节点
当前提交
f8eaf02516
共有 2 个文件被更改,包括 19 次插入13 次删除
  1. +15
    -12
      Source/MQTTnet/Client/MqttClient.cs
  2. +4
    -1
      Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs

+ 15
- 12
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<bool> HandleReceivedApplicationMessageAsync(MqttPublishPacket publishPacket)
async Task<int?> 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)


+ 4
- 1
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; }
}
}

正在加载...
取消
保存