Browse Source

reuse reader

release/3.x.x
JanEggers 5 years ago
parent
commit
73b051d159
3 changed files with 13 additions and 8 deletions
  1. +6
    -1
      Source/MQTTnet.AspnetCore/MqttConnectionContext.cs
  2. +1
    -3
      Source/MQTTnet.AspnetCore/ReaderExtensions.cs
  3. +6
    -4
      Tests/MQTTnet.AspNetCore.Tests/ReaderExtensionsTest.cs

+ 6
- 1
Source/MQTTnet.AspnetCore/MqttConnectionContext.cs View File

@@ -23,9 +23,14 @@ namespace MQTTnet.AspNetCore
_input = Connection.Transport.Input;
_output = Connection.Transport.Output;
}


_reader = new SpanBasedMqttPacketBodyReader();
}

private PipeReader _input;
private PipeWriter _output;
private readonly SpanBasedMqttPacketBodyReader _reader;

public string Endpoint => Connection.ConnectionId;
public bool IsSecureConnection => false; // TODO: Fix detection (WS vs. WSS).
@@ -88,7 +93,7 @@ namespace MQTTnet.AspNetCore
{
if (!buffer.IsEmpty)
{
if (PacketFormatterAdapter.TryDecode(buffer, out var packet, out consumed, out observed))
if (PacketFormatterAdapter.TryDecode(_reader, buffer, out var packet, out consumed, out observed))
{
return packet;
}


+ 1
- 3
Source/MQTTnet.AspnetCore/ReaderExtensions.cs View File

@@ -9,7 +9,7 @@ namespace MQTTnet.AspNetCore
{
public static class ReaderExtensions
{
public static bool TryDecode(this MqttPacketFormatterAdapter formatter, in ReadOnlySequence<byte> input, out MqttBasePacket packet, out SequencePosition consumed, out SequencePosition observed)
public static bool TryDecode(this MqttPacketFormatterAdapter formatter, SpanBasedMqttPacketBodyReader reader, in ReadOnlySequence<byte> input, out MqttBasePacket packet, out SequencePosition consumed, out SequencePosition observed)
{
if (formatter == null) throw new ArgumentNullException(nameof(formatter));

@@ -36,8 +36,6 @@ namespace MQTTnet.AspNetCore

var bodySlice = copy.Slice(0, bodyLength);
var buffer = bodySlice.GetMemory();

var reader = new SpanBasedMqttPacketBodyReader();
reader.SetBuffer(buffer);

var receivedMqttPacket = new ReceivedMqttPacket(fixedheader, reader, buffer.Length + 2);


+ 6
- 4
Tests/MQTTnet.AspNetCore.Tests/ReaderExtensionsTest.cs View File

@@ -24,21 +24,23 @@ namespace MQTTnet.AspNetCore.Tests
var observed = part.Start;
var result = false;

var reader = new SpanBasedMqttPacketBodyReader();

part = sequence.Slice(sequence.Start, 0); // empty message should fail
result = serializer.TryDecode(part, out packet, out consumed, out observed);
result = serializer.TryDecode(reader, part, out packet, out consumed, out observed);
Assert.IsFalse(result);


part = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
result = serializer.TryDecode(part, out packet, out consumed, out observed);
result = serializer.TryDecode(reader, part, out packet, out consumed, out observed);
Assert.IsFalse(result);

part = sequence.Slice(sequence.Start, 4); // partial body should fail
result = serializer.TryDecode(part, out packet, out consumed, out observed);
result = serializer.TryDecode(reader, part, out packet, out consumed, out observed);
Assert.IsFalse(result);

part = sequence; // complete msg should work
result = serializer.TryDecode(part, out packet, out consumed, out observed);
result = serializer.TryDecode(reader, part, out packet, out consumed, out observed);
Assert.IsTrue(result);
}
}


Loading…
Cancel
Save