From 49bd2e99fbbe9d9fe926c79bc78fded426974a67 Mon Sep 17 00:00:00 2001 From: christian <6939810+chkr1011@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:25:08 +0200 Subject: [PATCH] Fix issue --- Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs | 4 ++-- Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs b/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs index c9010cabe..e27f129fc 100644 --- a/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs +++ b/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs @@ -68,12 +68,12 @@ public MqttPacket Decode(ReceivedMqttPacket receivedMqttPacket) MqttPacket DecodeAuthPacket(ArraySegment body) { - ThrowIfBodyIsEmpty(body); - _bufferReader.SetBuffer(body.Array, body.Offset, body.Count); var packet = new MqttAuthPacket(); + // MQTT spec: The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success) and there are no Properties. + // In this case the AUTH has a Remaining Length of 0. if (_bufferReader.EndOfStream) { packet.ReasonCode = MqttAuthenticateReasonCode.Success; diff --git a/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs b/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs index 74232e877..06863fa06 100644 --- a/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs +++ b/Source/MQTTnet/Formatter/V5/MqttV5PacketEncoder.cs @@ -67,6 +67,13 @@ public MqttPacketBuffer Encode(MqttPacket packet) byte EncodeAuthPacket(MqttAuthPacket packet) { + // MQTT spec: The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success) and there are no Properties. + // In this case the AUTH has a Remaining Length of 0. + if (packet.ReasonCode == MqttAuthenticateReasonCode.Success && _propertiesWriter.Length == 0) + { + return MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Auth); + } + _bufferWriter.WriteByte((byte)packet.ReasonCode); _propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod);