Parcourir la source

Refactorings

release/3.x.x
Christian Kratky il y a 7 ans
Parent
révision
a956de3df7
3 fichiers modifiés avec 15 ajouts et 15 suppressions
  1. +4
    -3
      MQTTnet.Core/Serializer/MqttPacketReader.cs
  2. +5
    -10
      MQTTnet.Core/Serializer/MqttPacketSerializer.cs
  3. +6
    -2
      MQTTnet.Core/Serializer/MqttPacketWriter.cs

+ 4
- 3
MQTTnet.Core/Serializer/MqttPacketReader.cs Voir le fichier

@@ -13,8 +13,8 @@ namespace MQTTnet.Core.Serializer
{
private readonly MqttPacketHeader _header;

public MqttPacketReader(Stream stream, MqttPacketHeader header)
: base(stream)
public MqttPacketReader(MqttPacketHeader header, Stream body)
: base(body)
{
_header = header;
}
@@ -54,10 +54,11 @@ namespace MQTTnet.Core.Serializer
var fixedHeader = await ReadStreamByteAsync(source, buffer).ConfigureAwait(false);
var byteReader = new ByteReader(fixedHeader);
byteReader.Read(4);

var controlPacketType = (MqttControlPacketType)byteReader.Read(4);
var bodyLength = await ReadBodyLengthFromSourceAsync(source, buffer).ConfigureAwait(false);

return new MqttPacketHeader()
return new MqttPacketHeader
{
FixedHeader = fixedHeader,
ControlPacketType = controlPacketType,


+ 5
- 10
MQTTnet.Core/Serializer/MqttPacketSerializer.cs Voir le fichier

@@ -17,7 +17,7 @@ namespace MQTTnet.Core.Serializer
private static byte[] ProtocolVersionV310Name { get; } = Encoding.UTF8.GetBytes("MQIs");

public MqttProtocolVersion ProtocolVersion { get; set; } = MqttProtocolVersion.V311;
private byte[] _readBuffer = new byte[BufferConstants.Size];
private byte[] _readBuffer = new byte[BufferConstants.Size]; // TODO: What happens if the message is bigger?

public async Task SerializeAsync(MqttBasePacket packet, IMqttCommunicationChannel destination)
{
@@ -27,9 +27,7 @@ namespace MQTTnet.Core.Serializer
using (var stream = new MemoryStream())
using (var writer = new MqttPacketWriter(stream))
{
var header = new List<byte>();
header.Add(SerializePacket(packet, writer));

var header = new List<byte> { SerializePacket(packet, writer) };
var body = stream.ToArray();
MqttPacketWriter.BuildLengthHeader(body.Length, header);
@@ -118,10 +116,9 @@ namespace MQTTnet.Core.Serializer
if (source == null) throw new ArgumentNullException(nameof(source));

var header = await MqttPacketReader.ReadHeaderFromSourceAsync(source, _readBuffer).ConfigureAwait(false);

var body = await GetBody(source, header).ConfigureAwait(false);

using (var mqttPacketReader = new MqttPacketReader(body, header))
using (var mqttPacketReader = new MqttPacketReader(header, body))
{
switch (header.ControlPacketType)
{
@@ -225,7 +222,7 @@ namespace MQTTnet.Core.Serializer
var segment = await MqttPacketReader.ReadFromSourceAsync(source, header.BodyLength, _readBuffer).ConfigureAwait(false);
return new MemoryStream(segment.Array, segment.Offset, segment.Count);
}
return new MemoryStream();
}

@@ -412,9 +409,7 @@ namespace MQTTnet.Core.Serializer
else
{
writer.Write(ProtocolVersionV310Name);
writer.Write(0x64);
writer.Write(0x70);
writer.Write(0x03); // Protocol Level (3)
writer.Write(0x64, 0x70, 0x03); // Protocol Level (0x03)
}

var connectFlags = new ByteWriter(); // 3.1.2.3 Connect Flags


+ 6
- 2
MQTTnet.Core/Serializer/MqttPacketWriter.cs Voir le fichier

@@ -24,8 +24,7 @@ namespace MQTTnet.Core.Serializer
public override void Write(ushort value)
{
var buffer = BitConverter.GetBytes(value);
Write(buffer[1]);
Write(buffer[0]);
Write(buffer[1], buffer[0]);
}

public new void Write(params byte[] values)
@@ -33,6 +32,11 @@ namespace MQTTnet.Core.Serializer
base.Write(values);
}

public new void Write(byte value)
{
base.Write(value);
}

public void Write(ByteWriter value)
{
if (value == null) throw new ArgumentNullException(nameof(value));


Chargement…
Annuler
Enregistrer