mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-26 16:24:10 +08:00
slight improvements
This commit is contained in:
@@ -18,6 +18,8 @@ namespace MQTTnet.AspNetCore
|
|||||||
PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));
|
PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));
|
||||||
Connection = connection ?? throw new ArgumentNullException(nameof(connection));
|
Connection = connection ?? throw new ArgumentNullException(nameof(connection));
|
||||||
}
|
}
|
||||||
|
private PipeReader _input;
|
||||||
|
private PipeWriter _output;
|
||||||
|
|
||||||
public string Endpoint => Connection.ConnectionId;
|
public string Endpoint => Connection.ConnectionId;
|
||||||
public bool IsSecureConnection => false; // TODO: Fix detection (WS vs. WSS).
|
public bool IsSecureConnection => false; // TODO: Fix detection (WS vs. WSS).
|
||||||
@@ -33,20 +35,21 @@ namespace MQTTnet.AspNetCore
|
|||||||
|
|
||||||
private readonly SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);
|
private readonly SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);
|
||||||
|
|
||||||
public Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
public async Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (Connection is TcpConnection tcp && !tcp.IsConnected)
|
if (Connection is TcpConnection tcp && !tcp.IsConnected)
|
||||||
{
|
{
|
||||||
return tcp.StartAsync();
|
await tcp.StartAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
_input = Connection.Transport.Input;
|
||||||
|
_output = Connection.Transport.Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DisconnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
public Task DisconnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Connection.Transport.Input.Complete();
|
_input?.Complete();
|
||||||
Connection.Transport.Output.Complete();
|
_output?.Complete();
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@@ -54,7 +57,6 @@ namespace MQTTnet.AspNetCore
|
|||||||
public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var input = Connection.Transport.Input;
|
var input = Connection.Transport.Input;
|
||||||
var reader = new SpanBasedMqttPacketBodyReader();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ namespace MQTTnet.AspNetCore
|
|||||||
|
|
||||||
public byte[] ReadRemainingData()
|
public byte[] ReadRemainingData()
|
||||||
{
|
{
|
||||||
return _buffer.ToArray();
|
return _buffer.Slice(_offset).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ushort ReadUInt16()
|
public ushort ReadUInt16()
|
||||||
@@ -47,13 +47,13 @@ namespace MQTTnet.AspNetCore
|
|||||||
return ReadSegmentWithLengthPrefix().ToArray();
|
return ReadSegmentWithLengthPrefix().ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ReadOnlyMemory<byte> ReadSegmentWithLengthPrefix()
|
private ReadOnlySpan<byte> ReadSegmentWithLengthPrefix()
|
||||||
{
|
{
|
||||||
var length = ReadUInt16();
|
var length = ReadUInt16();
|
||||||
|
|
||||||
ValidateReceiveBuffer(length);
|
ValidateReceiveBuffer(length);
|
||||||
|
|
||||||
var result = _buffer.Slice(_offset, length);
|
var result = _buffer.Slice(_offset, length).Span;
|
||||||
_offset += length;
|
_offset += length;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ namespace MQTTnet.AspNetCore
|
|||||||
public unsafe string ReadStringWithLengthPrefix()
|
public unsafe string ReadStringWithLengthPrefix()
|
||||||
{
|
{
|
||||||
var buffer = ReadSegmentWithLengthPrefix();
|
var buffer = ReadSegmentWithLengthPrefix();
|
||||||
fixed (byte* bytes = &buffer.Span.GetPinnableReference())
|
fixed (byte* bytes = &buffer.GetPinnableReference())
|
||||||
{
|
{
|
||||||
var result = Encoding.UTF8.GetString(bytes, buffer.Length);
|
var result = Encoding.UTF8.GetString(bytes, buffer.Length);
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user