Refactor thread management for incoming packets.

This commit is contained in:
Christian Kratky
2018-06-04 22:19:07 +02:00
parent afe25db0d3
commit 39edd22a9d
8 changed files with 31 additions and 81 deletions
@@ -13,7 +13,7 @@ namespace MQTTnet.Client
{
foreach (var awaiter in _awaiters)
{
awaiter.Value.SetException(exception);
Task.Run(() => awaiter.Value.SetException(exception)); // Task.Run fixes a dead lock. Without this the client only receives one message.
}
}
@@ -30,9 +30,10 @@ namespace MQTTnet.Client
var type = packet.GetType();
var key = new Tuple<ushort, Type>(identifier, type);
if (_awaiters.TryRemove(key, out var tcs))
if (_awaiters.TryRemove(key, out var awaiter))
{
tcs.TrySetResult(packet);
awaiter.SetResult(packet);
Task.Run(() => awaiter.SetResult(packet)); // Task.Run fixes a dead lock. Without this the client only receives one message.
return;
}
@@ -70,7 +71,7 @@ namespace MQTTnet.Client
}
var key = new Tuple<ushort, Type>(identifier ?? 0, typeof(TResponsePacket));
_awaiters.TryRemove(key, out var _);
_awaiters.TryRemove(key, out _);
}
}
}