From 2767d897a4af9084dd95e258c1ef687e38650602 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 14 Aug 2019 22:45:15 +0200 Subject: [PATCH] Fix serialization issue of MQTTv5 PubRec packet. --- Source/MQTTnet/Formatter/IMqttPacketWriter.cs | 26 +++++++++++++------ .../Formatter/V5/MqttV500PacketEncoder.cs | 7 +++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Source/MQTTnet/Formatter/IMqttPacketWriter.cs b/Source/MQTTnet/Formatter/IMqttPacketWriter.cs index 5ea6ca3..bae6dec 100644 --- a/Source/MQTTnet/Formatter/IMqttPacketWriter.cs +++ b/Source/MQTTnet/Formatter/IMqttPacketWriter.cs @@ -5,15 +5,25 @@ int Length { get; } void WriteWithLengthPrefix(string value); - void Write(byte returnCode); - void WriteWithLengthPrefix(byte[] payload); - void Write(ushort keepAlivePeriod); - void Write(IMqttPacketWriter propertyWriter); - void WriteVariableLengthInteger(uint length); - void Write(byte[] payload, int v, int length); - void Reset(int v); - void Seek(int v); + + void Write(byte value); + + void WriteWithLengthPrefix(byte[] value); + + void Write(ushort value); + + void Write(IMqttPacketWriter value); + + void WriteVariableLengthInteger(uint value); + + void Write(byte[] value, int offset, int length); + + void Reset(int length); + + void Seek(int offset); + void FreeBuffer(); + byte[] GetBuffer(); } } diff --git a/Source/MQTTnet/Formatter/V5/MqttV500PacketEncoder.cs b/Source/MQTTnet/Formatter/V5/MqttV500PacketEncoder.cs index 23b7f1a..9ebc740 100644 --- a/Source/MQTTnet/Formatter/V5/MqttV500PacketEncoder.cs +++ b/Source/MQTTnet/Formatter/V5/MqttV500PacketEncoder.cs @@ -328,10 +328,7 @@ namespace MQTTnet.Formatter.V5 { ThrowReasonCodeNotSetException(); } - - packetWriter.Write(packet.PacketIdentifier.Value); - packetWriter.Write((byte)packet.ReasonCode.Value); - + var propertiesWriter = new MqttV500PropertiesWriter(); if (packet.Properties != null) { @@ -339,6 +336,8 @@ namespace MQTTnet.Formatter.V5 propertiesWriter.WriteUserProperties(packet.Properties.UserProperties); } + packetWriter.Write(packet.PacketIdentifier.Value); + if (packetWriter.Length > 0 || packet.ReasonCode.Value != MqttPubRecReasonCode.Success) { packetWriter.Write((byte)packet.ReasonCode.Value);