Просмотр исходного кода

Merge branch 'develop' of https://github.com/chkr1011/MQTTnet into develop

release/3.x.x
Christian 6 лет назад
Родитель
Сommit
6448b217d5
2 измененных файлов: 15 добавлений и 10 удалений
  1. +3
    -4
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs
  2. +12
    -6
      Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs

+ 3
- 4
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketSerializer.cs Просмотреть файл

@@ -24,19 +24,18 @@ namespace MQTTnet.Serializer
{ {
// Leave enough head space for max header size (fixed + 4 variable remaining length) // Leave enough head space for max header size (fixed + 4 variable remaining length)
stream.Position = 5; stream.Position = 5;

var fixedHeader = SerializePacket(packet, writer); var fixedHeader = SerializePacket(packet, writer);


var remainingLength = MqttPacketWriter.EncodeRemainingLength((int)stream.Length - 5);
stream.Position = 1;
var remainingLength = MqttPacketWriter.EncodeRemainingLength((int)stream.Length - 5, stream);


var headerSize = remainingLength.Length + 1;
var headerSize = remainingLength + 1;
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.Position = headerOffset; stream.Position = headerOffset;


writer.Write(fixedHeader); writer.Write(fixedHeader);
writer.Write(remainingLength, 0, remainingLength.Length);


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


+ 12
- 6
Frameworks/MQTTnet.NetStandard/Serializer/MqttPacketWriter.cs Просмотреть файл

@@ -56,14 +56,18 @@ namespace MQTTnet.Serializer
Write(value); Write(value);
} }


public static byte[] EncodeRemainingLength(int length)
public static int EncodeRemainingLength(int length,MemoryStream stream)
{ {
//write the encoded remaining length right aligned on the 4 byte buffer

if (length <= 0) if (length <= 0)
{ {
return new[] { (byte)0 };
stream.Seek(3, SeekOrigin.Current);
stream.WriteByte(0);
return 1;
} }


var bytes = new byte[4];
byte[] buffer = new byte[4];
var offset = 0; var offset = 0;


// Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html. // Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html.
@@ -77,13 +81,15 @@ namespace MQTTnet.Serializer
encodedByte = encodedByte | 128; encodedByte = encodedByte | 128;
} }


bytes[offset] = (byte)encodedByte;
buffer[offset] = (byte)encodedByte;


offset++; offset++;
} while (x > 0); } while (x > 0);


Array.Resize(ref bytes, offset);
return bytes;
stream.Seek(4 - offset, SeekOrigin.Current);
stream.Write(buffer, 0, offset);

return offset;
} }
} }
} }

Загрузка…
Отмена
Сохранить