mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
32 lines
921 B
C#
32 lines
921 B
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using MQTTnet.Core.Packets;
|
|
|
|
namespace MQTTnet.Core.Client
|
|
{
|
|
public class MqttPacketAwaiter
|
|
{
|
|
private readonly TaskCompletionSource<MqttBasePacket> _taskCompletionSource = new TaskCompletionSource<MqttBasePacket>();
|
|
private readonly Func<MqttBasePacket, bool> _packetSelector;
|
|
|
|
public MqttPacketAwaiter(Func<MqttBasePacket, bool> packetSelector)
|
|
{
|
|
if (packetSelector == null) throw new ArgumentNullException(nameof(packetSelector));
|
|
|
|
_packetSelector = packetSelector;
|
|
}
|
|
|
|
public Task<MqttBasePacket> Task => _taskCompletionSource.Task;
|
|
|
|
public bool CheckPacket(MqttBasePacket packet)
|
|
{
|
|
if (!_packetSelector(packet))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_taskCompletionSource.SetResult(packet);
|
|
return true;
|
|
}
|
|
}
|
|
} |