From 00ac9ce961ba974b2fd1b505e44c7d56c2ce824c Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Tue, 5 Jun 2018 23:39:59 +0200 Subject: [PATCH] Fix wrong remaining length calculation. --- .../Serializer/MqttPacketReader.cs | 6 +++--- .../Serializer/MqttPacketSerializer.cs | 5 +++-- .../Serializer/MqttPacketWriter.cs | 20 +++++++------------ 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs index a7b0f9d..110407e 100644 --- a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs +++ b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs @@ -93,11 +93,11 @@ namespace MQTTnet.Serializer private static async Task ReadBodyLengthAsync(IMqttChannel channel, byte initialEncodedByte, CancellationToken cancellationToken) { // Alorithm taken from https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html. - var multiplier = 1; - var value = (byte)(initialEncodedByte & 127) * multiplier; + var multiplier = 128; + var value = initialEncodedByte & 127; int encodedByte = initialEncodedByte; var buffer = new byte[1]; - + while ((encodedByte & 128) != 0) { var readCount = await channel.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false); diff --git a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs index f397c0c..32001db 100644 --- a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs +++ b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs @@ -26,14 +26,15 @@ namespace MQTTnet.Serializer var fixedHeader = SerializePacket(packet, stream); var remainingLength = (int)stream.Length - 5; - var remainingLengthSize = MqttPacketWriter.WriteRemainingLength(remainingLength, stream); + var remainingLengthBuffer = MqttPacketWriter.EncodeRemainingLength(remainingLength); - var headerSize = FixedHeaderSize + remainingLengthSize; + var headerSize = FixedHeaderSize + remainingLengthBuffer.Count; var headerOffset = 5 - headerSize; // Position cursor on correct offset on beginining of array (has leading 0x0) stream.Seek(headerOffset, SeekOrigin.Begin); stream.WriteByte(fixedHeader); + stream.Write(remainingLengthBuffer.Array, remainingLengthBuffer.Offset, remainingLengthBuffer.Count); #if NET461 || NET452 || NETSTANDARD2_0 var buffer = stream.GetBuffer(); diff --git a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs index 3206145..2cc9d7b 100644 --- a/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs +++ b/Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs @@ -41,20 +41,18 @@ namespace MQTTnet.Serializer stream.Write(value, 0, length); } - public static int WriteRemainingLength(int length, MemoryStream stream) + public static ArraySegment EncodeRemainingLength(int length) { // write the encoded remaining length right aligned on the 4 byte buffer if (length <= 0) { - stream.Seek(4, SeekOrigin.Begin); - stream.WriteByte(0); - return 1; + return new ArraySegment(new byte[1], 0, 1); } var buffer = new byte[4]; - var remainingLengthSize = 0; + var bufferOffset = 0; - // Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html. + // Algorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html. var x = length; do { @@ -65,15 +63,11 @@ namespace MQTTnet.Serializer encodedByte = encodedByte | 128; } - buffer[remainingLengthSize] = (byte)encodedByte; - - remainingLengthSize++; + buffer[bufferOffset] = (byte)encodedByte; + bufferOffset++; } while (x > 0); - stream.Seek(5 - remainingLengthSize, SeekOrigin.Begin); - stream.Write(buffer, 0, remainingLengthSize); - - return remainingLengthSize; + return new ArraySegment(buffer, 0, bufferOffset); } } }