diff --git a/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs b/Source/MQTTnet/Formatter/V5/MqttV5PacketDecoder.cs index c9010cab..e27f129f 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 74232e87..06863fa0 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);