Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

TestMqttCommunicationAdapter.cs 1.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Threading.Tasks;
  4. using MQTTnet.Core.Adapter;
  5. using MQTTnet.Core.Client;
  6. using MQTTnet.Core.Packets;
  7. using MQTTnet.Core.Serializer;
  8. namespace MQTTnet.Core.Tests
  9. {
  10. public class TestMqttCommunicationAdapter : IMqttCommunicationAdapter
  11. {
  12. private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
  13. public TestMqttCommunicationAdapter Partner { get; set; }
  14. public IMqttPacketSerializer PacketSerializer { get; } = new MqttPacketSerializer();
  15. public async Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
  16. {
  17. await Task.FromResult(0).ConfigureAwait(false);
  18. }
  19. public async Task DisconnectAsync()
  20. {
  21. await Task.FromResult(0).ConfigureAwait(false);
  22. }
  23. public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout)
  24. {
  25. ThrowIfPartnerIsNull();
  26. Partner.SendPacketInternal(packet);
  27. await Task.FromResult(0).ConfigureAwait(false);
  28. }
  29. public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout)
  30. {
  31. ThrowIfPartnerIsNull();
  32. return await Task.Run(() => _incomingPackets.Take()).ConfigureAwait(false);
  33. }
  34. private void SendPacketInternal(MqttBasePacket packet)
  35. {
  36. if (packet == null) throw new ArgumentNullException(nameof(packet));
  37. _incomingPackets.Add(packet);
  38. }
  39. private void ThrowIfPartnerIsNull()
  40. {
  41. if (Partner == null)
  42. {
  43. throw new InvalidOperationException("Partner is not set.");
  44. }
  45. }
  46. }
  47. }