Преглед на файлове

improve error messages

release/3.x.x
Jan Eggers преди 6 години
родител
ревизия
4e9abd41c0
променени са 1 файла, в които са добавени 13 реда и са изтрити 0 реда
  1. +13
    -0
      Source/MQTTnet/Serializer/MqttPacketBodyReader.cs

+ 13
- 0
Source/MQTTnet/Serializer/MqttPacketBodyReader.cs Целия файл

@@ -20,6 +20,7 @@ namespace MQTTnet.Serializer

public byte ReadByte()
{
ValidateReceiveBuffer(1);
return _buffer[_offset++];
}

@@ -30,6 +31,8 @@ namespace MQTTnet.Serializer

public ushort ReadUInt16()
{
ValidateReceiveBuffer(2);

var msb = _buffer[_offset++];
var lsb = _buffer[_offset++];
@@ -40,12 +43,22 @@ namespace MQTTnet.Serializer
{
var length = ReadUInt16();

ValidateReceiveBuffer(length);

var result = new ArraySegment<byte>(_buffer, _offset, length);
_offset += length;

return result;
}

private void ValidateReceiveBuffer(ushort length)
{
if (_buffer.Length < _offset + length)
{
throw new ArgumentOutOfRangeException(nameof(_buffer), $"expected at least {_offset + length} bytes but there are only {_buffer.Length} bytes");
}
}

public string ReadStringWithLengthPrefix()
{
var buffer = ReadWithLengthPrefix();


Зареждане…
Отказ
Запис