From 43105f71d826e3a42e9a4f5836c08c1cd7aa097b Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Fri, 23 Nov 2018 22:56:10 +0100 Subject: [PATCH] Refactor removal from blocking queue. --- .../ManagedMqttClient.cs | 9 ++------- Source/MQTTnet/Internal/BlockingQueue.cs | 6 ++++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs index 7647fc7..29251c6 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs @@ -300,7 +300,7 @@ namespace MQTTnet.Extensions.ManagedClient //in the queue is equal to this message, then it's safe to remove //it from the queue. If not, that means this.PublishAsync has already //removed it, in which case we don't want to do anything. - _messageQueue.RemoveFirstIfEqual(message, IdsAreEqual); + _messageQueue.RemoveFirst(i => i.Id.Equals(message.Id)); } _storageManager?.RemoveAsync(message).GetAwaiter().GetResult(); } @@ -321,7 +321,7 @@ namespace MQTTnet.Extensions.ManagedClient //with prior behavior in that way. lock (_messageQueue) //lock to avoid conflict with this.PublishAsync { - _messageQueue.RemoveFirstIfEqual(message, IdsAreEqual); + _messageQueue.RemoveFirst(i => i.Id.Equals(message.Id)); } } } @@ -335,11 +335,6 @@ namespace MQTTnet.Extensions.ManagedClient ApplicationMessageProcessed?.Invoke(this, new ApplicationMessageProcessedEventArgs(message, transmitException)); } } - - private static bool IdsAreEqual(ManagedMqttApplicationMessage message1, ManagedMqttApplicationMessage message2) - { - return message1.Id.Equals(message2.Id); - } private async Task SynchronizeSubscriptionsAsync() { diff --git a/Source/MQTTnet/Internal/BlockingQueue.cs b/Source/MQTTnet/Internal/BlockingQueue.cs index 2812349..485f644 100644 --- a/Source/MQTTnet/Internal/BlockingQueue.cs +++ b/Source/MQTTnet/Internal/BlockingQueue.cs @@ -77,11 +77,13 @@ namespace MQTTnet.Internal } } - public void RemoveFirstIfEqual(TItem item, Func areEqual) + public void RemoveFirst(Predicate match) { + if (match == null) throw new ArgumentNullException(nameof(match)); + lock (_syncRoot) { - if (_items.Count > 0 && areEqual(_items.First.Value, item)) + if (_items.Count > 0 && match(_items.First.Value)) { _items.RemoveFirst(); }