Browse Source

Fix wrong remaining length calculation.

release/3.x.x
Christian Kratky 6 years ago
parent
commit
00ac9ce961
3 changed files with 13 additions and 18 deletions
  1. +3
    -3
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs
  2. +3
    -2
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs
  3. +7
    -13
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs

+ 3
- 3
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketReader.cs View File

@@ -93,11 +93,11 @@ namespace MQTTnet.Serializer
private static async Task<int> ReadBodyLengthAsync(IMqttChannel channel, byte initialEncodedByte, CancellationToken cancellationToken) private static async Task<int> 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. // 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; int encodedByte = initialEncodedByte;
var buffer = new byte[1]; var buffer = new byte[1];
while ((encodedByte & 128) != 0) while ((encodedByte & 128) != 0)
{ {
var readCount = await channel.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false); var readCount = await channel.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false);


+ 3
- 2
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs View File

@@ -26,14 +26,15 @@ namespace MQTTnet.Serializer
var fixedHeader = SerializePacket(packet, stream); var fixedHeader = SerializePacket(packet, stream);
var remainingLength = (int)stream.Length - 5; 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; var headerOffset = 5 - headerSize;


// Position cursor on correct offset on beginining of array (has leading 0x0) // Position cursor on correct offset on beginining of array (has leading 0x0)
stream.Seek(headerOffset, SeekOrigin.Begin); stream.Seek(headerOffset, SeekOrigin.Begin);
stream.WriteByte(fixedHeader); stream.WriteByte(fixedHeader);
stream.Write(remainingLengthBuffer.Array, remainingLengthBuffer.Offset, remainingLengthBuffer.Count);


#if NET461 || NET452 || NETSTANDARD2_0 #if NET461 || NET452 || NETSTANDARD2_0
var buffer = stream.GetBuffer(); var buffer = stream.GetBuffer();


+ 7
- 13
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs View File

@@ -41,20 +41,18 @@ namespace MQTTnet.Serializer
stream.Write(value, 0, length); stream.Write(value, 0, length);
} }


public static int WriteRemainingLength(int length, MemoryStream stream)
public static ArraySegment<byte> EncodeRemainingLength(int length)
{ {
// write the encoded remaining length right aligned on the 4 byte buffer // write the encoded remaining length right aligned on the 4 byte buffer
if (length <= 0) if (length <= 0)
{ {
stream.Seek(4, SeekOrigin.Begin);
stream.WriteByte(0);
return 1;
return new ArraySegment<byte>(new byte[1], 0, 1);
} }


var buffer = new byte[4]; 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; var x = length;
do do
{ {
@@ -65,15 +63,11 @@ namespace MQTTnet.Serializer
encodedByte = encodedByte | 128; encodedByte = encodedByte | 128;
} }


buffer[remainingLengthSize] = (byte)encodedByte;

remainingLengthSize++;
buffer[bufferOffset] = (byte)encodedByte;
bufferOffset++;
} while (x > 0); } while (x > 0);


stream.Seek(5 - remainingLengthSize, SeekOrigin.Begin);
stream.Write(buffer, 0, remainingLengthSize);

return remainingLengthSize;
return new ArraySegment<byte>(buffer, 0, bufferOffset);
} }
} }
} }

Loading…
Cancel
Save