mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using MQTTnet.Core.Adapter;
|
|
using MQTTnet.Core.Client;
|
|
using MQTTnet.Core.Packets;
|
|
using MQTTnet.Core.Serializer;
|
|
|
|
namespace MQTTnet.Core.Tests
|
|
{
|
|
public class TestMqttCommunicationAdapter : IMqttCommunicationAdapter
|
|
{
|
|
private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
|
|
|
|
public TestMqttCommunicationAdapter Partner { get; set; }
|
|
|
|
public IMqttPacketSerializer PacketSerializer { get; } = new MqttPacketSerializer();
|
|
|
|
public Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
|
|
{
|
|
return Task.FromResult(0);
|
|
}
|
|
|
|
public Task DisconnectAsync()
|
|
{
|
|
return Task.FromResult(0);
|
|
}
|
|
|
|
public Task SendPacketsAsync(TimeSpan timeout, IEnumerable<MqttBasePacket> packets)
|
|
{
|
|
ThrowIfPartnerIsNull();
|
|
|
|
foreach (var packet in packets)
|
|
{
|
|
Partner.SendPacketInternal(packet);
|
|
}
|
|
|
|
return Task.FromResult(0);
|
|
}
|
|
|
|
public Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout)
|
|
{
|
|
ThrowIfPartnerIsNull();
|
|
|
|
return Task.Run(() => _incomingPackets.Take());
|
|
}
|
|
|
|
public IEnumerable<MqttBasePacket> ReceivePackets( CancellationToken cancellationToken )
|
|
{
|
|
return _incomingPackets.GetConsumingEnumerable();
|
|
}
|
|
|
|
private void SendPacketInternal(MqttBasePacket packet)
|
|
{
|
|
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
|
|
|
_incomingPackets.Add(packet);
|
|
}
|
|
|
|
private void ThrowIfPartnerIsNull()
|
|
{
|
|
if (Partner == null)
|
|
{
|
|
throw new InvalidOperationException("Partner is not set.");
|
|
}
|
|
}
|
|
}
|
|
}
|