Browse Source

Fix handling of closed streams

release/3.x.x
Christian Kratky 7 years ago
parent
commit
9c46b8fd75
2 changed files with 16 additions and 6 deletions
  1. +5
    -1
      MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs
  2. +11
    -5
      MQTTnet.Core/Serializer/MqttPacketReader.cs

+ 5
- 1
MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs View File

@@ -151,7 +151,7 @@ namespace MQTTnet.Core.Adapter
receivedMqttPacket = await ReceiveAsync(_channel.ReceiveStream, cancellationToken).ConfigureAwait(false); receivedMqttPacket = await ReceiveAsync(_channel.ReceiveStream, cancellationToken).ConfigureAwait(false);
} }


if (cancellationToken.IsCancellationRequested)
if (receivedMqttPacket == null || cancellationToken.IsCancellationRequested)
{ {
throw new TaskCanceledException(); throw new TaskCanceledException();
} }
@@ -190,6 +190,10 @@ namespace MQTTnet.Core.Adapter
private static async Task<ReceivedMqttPacket> ReceiveAsync(Stream stream, CancellationToken cancellationToken) private static async Task<ReceivedMqttPacket> ReceiveAsync(Stream stream, CancellationToken cancellationToken)
{ {
var header = MqttPacketReader.ReadHeaderFromSource(stream, cancellationToken); var header = MqttPacketReader.ReadHeaderFromSource(stream, cancellationToken);
if (header == null)
{
return null;
}


if (header.BodyLength == 0) if (header.BodyLength == 0)
{ {


+ 11
- 5
MQTTnet.Core/Serializer/MqttPacketReader.cs View File

@@ -25,7 +25,13 @@ namespace MQTTnet.Core.Serializer


public static MqttPacketHeader ReadHeaderFromSource(Stream stream, CancellationToken cancellationToken) public static MqttPacketHeader ReadHeaderFromSource(Stream stream, CancellationToken cancellationToken)
{ {
var fixedHeader = (byte)stream.ReadByte();
var buffer = stream.ReadByte();
if (buffer == -1)
{
return null;
}

var fixedHeader = (byte)buffer;
var controlPacketType = (MqttControlPacketType)(fixedHeader >> 4); var controlPacketType = (MqttControlPacketType)(fixedHeader >> 4);
var bodyLength = ReadBodyLengthFromSource(stream, cancellationToken); var bodyLength = ReadBodyLengthFromSource(stream, cancellationToken);


@@ -83,10 +89,10 @@ namespace MQTTnet.Core.Serializer
var buffer = stream.ReadByte(); var buffer = stream.ReadByte();
readBytes.Add(buffer); readBytes.Add(buffer);


////if (buffer == -1)
////{
//// break;
////}
if (buffer == -1)
{
break;
}


encodedByte = (byte)buffer; encodedByte = (byte)buffer;
value += (byte)(encodedByte & 127) * multiplier; value += (byte)(encodedByte & 127) * multiplier;


Loading…
Cancel
Save