From cf7b7191f8992e971d0c0bb9cdfba715b4f04957 Mon Sep 17 00:00:00 2001 From: JanEggers Date: Thu, 14 Sep 2017 21:48:38 +0200 Subject: [PATCH] if we wait before sending the packets we dont need to store the received packets at all because the awaiter will always be there or is already canceled --- MQTTnet.Core/Client/MqttPacketDispatcher.cs | 41 +-------------------- 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/MQTTnet.Core/Client/MqttPacketDispatcher.cs b/MQTTnet.Core/Client/MqttPacketDispatcher.cs index a825581..21c177c 100644 --- a/MQTTnet.Core/Client/MqttPacketDispatcher.cs +++ b/MQTTnet.Core/Client/MqttPacketDispatcher.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using MQTTnet.Core.Diagnostics; using MQTTnet.Core.Exceptions; @@ -11,8 +10,6 @@ namespace MQTTnet.Core.Client { public class MqttPacketDispatcher { - private readonly object _syncRoot = new object(); - private readonly HashSet _receivedPackets = new HashSet(); private readonly ConcurrentDictionary> _packetByResponseType = new ConcurrentDictionary>(); private readonly ConcurrentDictionary>> _packetByResponseTypeAndIdentifier = new ConcurrentDictionary>>(); @@ -21,8 +18,6 @@ namespace MQTTnet.Core.Client if (request == null) throw new ArgumentNullException(nameof(request)); var packetAwaiter = AddPacketAwaiter(request, responseType); - DispatchPendingPackets(); - try { return await packetAwaiter.Task.TimeoutAfter(timeout); @@ -43,8 +38,6 @@ namespace MQTTnet.Core.Client if (packet == null) throw new ArgumentNullException(nameof(packet)); var type = packet.GetType(); - var packetDispatched = false; - if (packet is IMqttPacketWithIdentifier withIdentifier) { if (_packetByResponseTypeAndIdentifier.TryGetValue(type, out var byid)) @@ -52,37 +45,19 @@ namespace MQTTnet.Core.Client if (byid.TryRemove( withIdentifier.PacketIdentifier, out var tcs)) { tcs.TrySetResult( packet ); - packetDispatched = true; } } } else if (_packetByResponseType.TryRemove(type, out var tcs)) { tcs.TrySetResult(packet); - packetDispatched = true; - } - - lock (_syncRoot) - { - if (!packetDispatched) - { - _receivedPackets.Add(packet); - } - else - { - _receivedPackets.Remove(packet); - } } } public void Reset() { - lock (_syncRoot) - { - _receivedPackets.Clear(); - } - _packetByResponseTypeAndIdentifier.Clear(); + _packetByResponseType.Clear(); } private TaskCompletionSource AddPacketAwaiter(MqttBasePacket request, Type responseType) @@ -113,19 +88,5 @@ namespace MQTTnet.Core.Client _packetByResponseType.TryRemove(responseType, out var _); } } - - private void DispatchPendingPackets() - { - List receivedPackets; - lock (_syncRoot) - { - receivedPackets = new List(_receivedPackets); - } - - foreach (var pendingPacket in receivedPackets) - { - Dispatch(pendingPacket); - } - } } } \ No newline at end of file