瀏覽代碼

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();


Loading…
取消
儲存