mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 10:26:11 +08:00
Optimize package serializing and dispatching.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
namespace MQTTnet.Serializer
|
||||
{
|
||||
public struct MqttFixedHeader
|
||||
{
|
||||
public MqttFixedHeader(byte flags, int remainingLength)
|
||||
{
|
||||
Flags = flags;
|
||||
RemainingLength = remainingLength;
|
||||
}
|
||||
|
||||
public byte Flags { get; }
|
||||
|
||||
public int RemainingLength { get; }
|
||||
}
|
||||
}
|
||||
@@ -11,19 +11,32 @@ namespace MQTTnet.Serializer
|
||||
{
|
||||
public static class MqttPacketReader
|
||||
{
|
||||
public static async Task<byte> ReadFixedHeaderAsync(IMqttChannel channel, CancellationToken cancellationToken)
|
||||
public static async Task<MqttFixedHeader> ReadFixedHeaderAsync(IMqttChannel channel, CancellationToken cancellationToken)
|
||||
{
|
||||
// Wait for the next package which starts with the header. At this point there will probably
|
||||
// some large delay and thus the thread should be put back to the pool (await). So ReadByte()
|
||||
// is not an option here.
|
||||
var buffer = new byte[1];
|
||||
var readCount = await channel.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
|
||||
if (readCount <= 0)
|
||||
// The MQTT fixed header contains 1 byte of flags and at least 1 byte for the remaining data length.
|
||||
// So in all cases at least 2 bytes must be read for a complete MQTT packet.
|
||||
var buffer = new byte[2];
|
||||
var totalBytesRead = 0;
|
||||
|
||||
while (totalBytesRead < buffer.Length)
|
||||
{
|
||||
ExceptionHelper.ThrowGracefulSocketClose();
|
||||
var bytesRead = await channel.ReadAsync(buffer, 0, buffer.Length - totalBytesRead, cancellationToken).ConfigureAwait(false);
|
||||
if (bytesRead <= 0)
|
||||
{
|
||||
ExceptionHelper.ThrowGracefulSocketClose();
|
||||
}
|
||||
|
||||
totalBytesRead += bytesRead;
|
||||
}
|
||||
|
||||
return buffer[0];
|
||||
var hasRemainingLength = buffer[1] != 0;
|
||||
if (!hasRemainingLength)
|
||||
{
|
||||
return new MqttFixedHeader(buffer[0], 0);
|
||||
}
|
||||
|
||||
var bodyLength = await ReadBodyLengthAsync(channel, buffer[1], cancellationToken);
|
||||
return new MqttFixedHeader(buffer[0], bodyLength);
|
||||
}
|
||||
|
||||
public static ushort ReadUInt16(this Stream stream)
|
||||
@@ -64,21 +77,29 @@ namespace MQTTnet.Serializer
|
||||
return stream.ReadBytes((int)(stream.Length - stream.Position));
|
||||
}
|
||||
|
||||
public static async Task<int> ReadBodyLengthAsync(IMqttChannel channel, CancellationToken cancellationToken)
|
||||
private static byte[] ReadBytes(this Stream stream, int count)
|
||||
{
|
||||
var buffer = new byte[count];
|
||||
var readBytes = stream.Read(buffer, 0, count);
|
||||
|
||||
if (readBytes != count)
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to read {count} bytes from the stream.");
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
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 = 0;
|
||||
int encodedByte;
|
||||
var value = (byte)(initialEncodedByte & 127) * multiplier;
|
||||
int encodedByte = initialEncodedByte;
|
||||
var buffer = new byte[1];
|
||||
|
||||
do
|
||||
while ((encodedByte & 128) != 0)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw new TaskCanceledException();
|
||||
}
|
||||
|
||||
var readCount = await channel.ReadAsync(buffer, 0, 1, cancellationToken).ConfigureAwait(false);
|
||||
if (readCount <= 0)
|
||||
{
|
||||
@@ -94,16 +115,9 @@ namespace MQTTnet.Serializer
|
||||
}
|
||||
|
||||
multiplier *= 128;
|
||||
} while ((encodedByte & 128) != 0);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private static byte[] ReadBytes(this Stream stream, int count)
|
||||
{
|
||||
var buffer = new byte[count];
|
||||
stream.Read(buffer, 0, count);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ namespace MQTTnet.Serializer
|
||||
{
|
||||
public class MqttPacketSerializer : IMqttPacketSerializer
|
||||
{
|
||||
private const int FixedHeaderSize = 1;
|
||||
|
||||
public MqttProtocolVersion ProtocolVersion { get; set; } = MqttProtocolVersion.V311;
|
||||
|
||||
public ArraySegment<byte> Serialize(MqttBasePacket packet)
|
||||
@@ -18,27 +20,33 @@ namespace MQTTnet.Serializer
|
||||
|
||||
using (var stream = new MemoryStream(128))
|
||||
{
|
||||
// Leave enough head space for max header size (fixed + 4 variable remaining length)
|
||||
stream.Position = 5;
|
||||
// Leave enough head space for max header size (fixed + 4 variable remaining length = 5 bytes)
|
||||
stream.Seek(5, SeekOrigin.Begin);
|
||||
|
||||
var fixedHeader = SerializePacket(packet, stream);
|
||||
var remainingLength = (int)stream.Length - 5;
|
||||
|
||||
stream.Position = 1;
|
||||
var remainingLength = MqttPacketWriter.EncodeRemainingLength((int)stream.Length - 5, stream);
|
||||
var remainingLengthSize = MqttPacketWriter.WriteRemainingLength(remainingLength, stream);
|
||||
|
||||
var headerSize = remainingLength + 1;
|
||||
var headerSize = FixedHeaderSize + remainingLengthSize;
|
||||
var headerOffset = 5 - headerSize;
|
||||
|
||||
// Position cursor on correct offset on beginining of array (has leading 0x0)
|
||||
stream.Position = headerOffset;
|
||||
|
||||
stream.Seek(headerOffset, SeekOrigin.Begin);
|
||||
stream.WriteByte(fixedHeader);
|
||||
|
||||
#if NET461 || NET452 || NETSTANDARD2_0
|
||||
var buffer = stream.GetBuffer();
|
||||
#else
|
||||
var buffer = stream.ToArray();
|
||||
#endif
|
||||
return new ArraySegment<byte>(buffer, headerOffset, (int)stream.Length - headerOffset);
|
||||
#else
|
||||
if (stream.TryGetBuffer(out var segment))
|
||||
{
|
||||
return new ArraySegment<byte>(segment.Array, headerOffset, segment.Count - headerOffset);
|
||||
}
|
||||
|
||||
var buffer = stream.ToArray();
|
||||
return new ArraySegment<byte>(buffer, headerOffset, buffer.Length - headerOffset);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,19 +41,18 @@ namespace MQTTnet.Serializer
|
||||
stream.Write(value, 0, length);
|
||||
}
|
||||
|
||||
public static int EncodeRemainingLength(int length, MemoryStream stream)
|
||||
public static int WriteRemainingLength(int length, MemoryStream stream)
|
||||
{
|
||||
// write the encoded remaining length right aligned on the 4 byte buffer
|
||||
|
||||
if (length <= 0)
|
||||
{
|
||||
stream.Seek(3, SeekOrigin.Current);
|
||||
stream.Seek(4, SeekOrigin.Begin);
|
||||
stream.WriteByte(0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
var buffer = new byte[4];
|
||||
var offset = 0;
|
||||
var remainingLengthSize = 0;
|
||||
|
||||
// Alorithm taken from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html.
|
||||
var x = length;
|
||||
@@ -66,15 +65,15 @@ namespace MQTTnet.Serializer
|
||||
encodedByte = encodedByte | 128;
|
||||
}
|
||||
|
||||
buffer[offset] = (byte)encodedByte;
|
||||
buffer[remainingLengthSize] = (byte)encodedByte;
|
||||
|
||||
offset++;
|
||||
remainingLengthSize++;
|
||||
} while (x > 0);
|
||||
|
||||
stream.Seek(4 - offset, SeekOrigin.Current);
|
||||
stream.Write(buffer, 0, offset);
|
||||
stream.Seek(5 - remainingLengthSize, SeekOrigin.Begin);
|
||||
stream.Write(buffer, 0, remainingLengthSize);
|
||||
|
||||
return offset;
|
||||
return remainingLengthSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user