Explorar el Código

Fix wrong remaining length calculation.

release/3.x.x
Christian Kratky hace 6 años
padre
commit
00ac9ce961
Se han modificado 3 ficheros con 13 adiciones y 18 borrados
  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 Ver fichero

@@ -93,11 +93,11 @@ namespace MQTTnet.Serializer
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.
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);


+ 3
- 2
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs Ver fichero

@@ -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();


+ 7
- 13
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs Ver fichero

@@ -41,20 +41,18 @@ namespace MQTTnet.Serializer
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
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 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<byte>(buffer, 0, bufferOffset);
}
}
}

Cargando…
Cancelar
Guardar