Refactor session and connection handling in server. Fix QoS level 2 issues.

This commit is contained in:
Christian Kratky
2019-01-27 11:15:32 +01:00
parent ebf0e91ab8
commit 6a2bded181
72 changed files with 2919 additions and 2461 deletions
@@ -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();