Extend MqttPacketReader constructor to allow more unit tests.

This commit is contained in:
Christian Kratky
2018-07-04 22:00:34 +02:00
parent 7fd09009e7
commit 7d9326a147
8 changed files with 76 additions and 22 deletions
@@ -32,7 +32,8 @@ namespace MQTTnet.AspNetCore
}
var bodySlice = copy.Slice(0, bodyLength);
packet = serializer.Deserialize(new ReceivedMqttPacket(fixedheader, new MqttPacketBodyReader(bodySlice.GetArray(), 0)));
var buffer = bodySlice.GetArray();
packet = serializer.Deserialize(new ReceivedMqttPacket(fixedheader, new MqttPacketBodyReader(buffer, 0, buffer.Length)));
consumed = bodySlice.End;
observed = bodySlice.End;
return true;
+1 -1
View File
@@ -201,7 +201,7 @@ namespace MQTTnet.Adapter
bodyOffset += readBytes;
} while (bodyOffset < body.Length);
return new ReceivedMqttPacket(fixedHeader.Flags, new MqttPacketBodyReader(body, 0));
return new ReceivedMqttPacket(fixedHeader.Flags, new MqttPacketBodyReader(body, 0, body.Length));
}
finally
{
+5 -5
View File
@@ -14,11 +14,7 @@ namespace MQTTnet.Internal
_stream = stream;
}
public void Dispose()
{
}
public string Endpoint { get; }
public string Endpoint { get; } = "<Test channel>";
public Task ConnectAsync(CancellationToken cancellationToken)
{
@@ -39,5 +35,9 @@ namespace MQTTnet.Internal
{
return _stream.WriteAsync(buffer, offset, count, cancellationToken);
}
public void Dispose()
{
}
}
}
@@ -6,17 +6,20 @@ namespace MQTTnet.Serializer
public class MqttPacketBodyReader
{
private readonly byte[] _buffer;
private int _offset;
private readonly int _length;
public MqttPacketBodyReader(byte[] buffer, int offset)
private int _offset;
public MqttPacketBodyReader(byte[] buffer, int offset, int length)
{
_buffer = buffer;
_offset = offset;
_length = length;
}
public int Length => _buffer.Length - _offset;
public int Length => _length - _offset;
public bool EndOfStream => _offset == _buffer.Length;
public bool EndOfStream => _offset == _length;
public byte ReadByte()
{
@@ -26,7 +29,7 @@ namespace MQTTnet.Serializer
public ArraySegment<byte> ReadRemainingData()
{
return new ArraySegment<byte>(_buffer, _offset, _buffer.Length - _offset);
return new ArraySegment<byte>(_buffer, _offset, _length - _offset);
}
public ushort ReadUInt16()
@@ -53,9 +56,9 @@ namespace MQTTnet.Serializer
private void ValidateReceiveBuffer(ushort length)
{
if (_buffer.Length < _offset + length)
if (_length < _offset + length)
{
throw new ArgumentOutOfRangeException(nameof(_buffer), $"expected at least {_offset + length} bytes but there are only {_buffer.Length} bytes");
throw new ArgumentOutOfRangeException(nameof(_buffer), $"expected at least {_offset + length} bytes but there are only {_length} bytes");
}
}
@@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Channel;
using MQTTnet.Exceptions;