Просмотр исходного кода

Refactor removal from blocking queue.

release/3.x.x
Christian Kratky 6 лет назад
Родитель
Сommit
43105f71d8
2 измененных файлов: 6 добавлений и 9 удалений
  1. +2
    -7
      Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs
  2. +4
    -2
      Source/MQTTnet/Internal/BlockingQueue.cs

+ 2
- 7
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()
{


+ 4
- 2
Source/MQTTnet/Internal/BlockingQueue.cs Просмотреть файл

@@ -77,11 +77,13 @@ namespace MQTTnet.Internal
}
}

public void RemoveFirstIfEqual(TItem item, Func<TItem, TItem, bool> areEqual)
public void RemoveFirst(Predicate<TItem> 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();
}


Загрузка…
Отмена
Сохранить