using System; using System.Threading.Tasks; using MQTTnet.Core.Packets; namespace MQTTnet.Core.Client { public class MqttPacketAwaiter { private readonly TaskCompletionSource _taskCompletionSource = new TaskCompletionSource(); private readonly Func _packetSelector; public MqttPacketAwaiter(Func packetSelector) { if (packetSelector == null) throw new ArgumentNullException(nameof(packetSelector)); _packetSelector = packetSelector; } public Task Task => _taskCompletionSource.Task; public bool CheckPacket(MqttBasePacket packet) { if (!_packetSelector(packet)) { return false; } _taskCompletionSource.SetResult(packet); return true; } } }