From a956de3df7e7345c47a59e3e0913d3de3c959802 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 13 Sep 2017 21:01:25 +0200 Subject: [PATCH] Refactorings --- MQTTnet.Core/Serializer/MqttPacketReader.cs | 7 ++++--- MQTTnet.Core/Serializer/MqttPacketSerializer.cs | 15 +++++---------- MQTTnet.Core/Serializer/MqttPacketWriter.cs | 8 ++++++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/MQTTnet.Core/Serializer/MqttPacketReader.cs b/MQTTnet.Core/Serializer/MqttPacketReader.cs index 38d3c7c..bdef7fa 100644 --- a/MQTTnet.Core/Serializer/MqttPacketReader.cs +++ b/MQTTnet.Core/Serializer/MqttPacketReader.cs @@ -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, diff --git a/MQTTnet.Core/Serializer/MqttPacketSerializer.cs b/MQTTnet.Core/Serializer/MqttPacketSerializer.cs index 064b79b..4de39af 100644 --- a/MQTTnet.Core/Serializer/MqttPacketSerializer.cs +++ b/MQTTnet.Core/Serializer/MqttPacketSerializer.cs @@ -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(); - header.Add(SerializePacket(packet, writer)); - + var header = new List { 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 diff --git a/MQTTnet.Core/Serializer/MqttPacketWriter.cs b/MQTTnet.Core/Serializer/MqttPacketWriter.cs index d5aa47a..ae9eef1 100644 --- a/MQTTnet.Core/Serializer/MqttPacketWriter.cs +++ b/MQTTnet.Core/Serializer/MqttPacketWriter.cs @@ -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));