mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Refactor session and connection handling in server. Fix QoS level 2 issues.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.PacketDispatcher
|
||||
@@ -21,7 +22,7 @@ namespace MQTTnet.PacketDispatcher
|
||||
{
|
||||
using (var timeoutToken = new CancellationTokenSource(timeout))
|
||||
{
|
||||
timeoutToken.Token.Register(() => _taskCompletionSource.TrySetCanceled());
|
||||
timeoutToken.Token.Register(() => _taskCompletionSource.TrySetException(new MqttCommunicationTimedOutException()));
|
||||
|
||||
var packet = await _taskCompletionSource.Task.ConfigureAwait(false);
|
||||
return (TPacket)packet;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Packets;
|
||||
|
||||
namespace MQTTnet.PacketDispatcher
|
||||
@@ -9,8 +9,6 @@ namespace MQTTnet.PacketDispatcher
|
||||
{
|
||||
private readonly ConcurrentDictionary<Tuple<ushort, Type>, IMqttPacketAwaiter> _packetAwaiters = new ConcurrentDictionary<Tuple<ushort, Type>, IMqttPacketAwaiter>();
|
||||
|
||||
private BlockingCollection<MqttBasePacket> _inboundPackagesQueue = new BlockingCollection<MqttBasePacket>();
|
||||
|
||||
public void Dispatch(Exception exception)
|
||||
{
|
||||
foreach (var awaiter in _packetAwaiters)
|
||||
@@ -40,34 +38,14 @@ namespace MQTTnet.PacketDispatcher
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_inboundPackagesQueue)
|
||||
{
|
||||
_inboundPackagesQueue.Add(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public MqttBasePacket Take(CancellationToken cancellationToken)
|
||||
{
|
||||
BlockingCollection<MqttBasePacket> inboundPackagesQueue;
|
||||
lock (_inboundPackagesQueue)
|
||||
{
|
||||
inboundPackagesQueue = _inboundPackagesQueue;
|
||||
}
|
||||
|
||||
return inboundPackagesQueue.Take(cancellationToken);
|
||||
throw new MqttProtocolViolationException($"Received packet '{packet}' at an unexpected time.");
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
foreach (var awaiter in _packetAwaiters)
|
||||
{
|
||||
awaiter.Value.Cancel();
|
||||
}
|
||||
|
||||
lock (_inboundPackagesQueue)
|
||||
{
|
||||
_inboundPackagesQueue?.Dispose();
|
||||
_inboundPackagesQueue = new BlockingCollection<MqttBasePacket>();
|
||||
awaiter.Value.Cancel();
|
||||
}
|
||||
|
||||
_packetAwaiters.Clear();
|
||||
|
||||
Reference in New Issue
Block a user