Pārlūkot izejas kodu

improve error messages

release/3.x.x
Jan Eggers pirms 6 gadiem
vecāks
revīzija
4e9abd41c0
1 mainītis faili ar 13 papildinājumiem un 0 dzēšanām
  1. +13
    -0
      Source/MQTTnet/Serializer/MqttPacketBodyReader.cs

+ 13
- 0
Source/MQTTnet/Serializer/MqttPacketBodyReader.cs Parādīt failu

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


Notiek ielāde…
Atcelt
Saglabāt