Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

57 linhas
1.5 KiB

  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. namespace MQTTnet.Core.Tests
  8. {
  9. public class TestMqttCommunicationAdapter : IMqttCommunicationAdapter
  10. {
  11. private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
  12. public TestMqttCommunicationAdapter Partner { get; set; }
  13. public async Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
  14. {
  15. await Task.FromResult(0);
  16. }
  17. public async Task DisconnectAsync()
  18. {
  19. await Task.FromResult(0);
  20. }
  21. public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout)
  22. {
  23. ThrowIfPartnerIsNull();
  24. Partner.SendPacketInternal(packet);
  25. await Task.FromResult(0);
  26. }
  27. public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout)
  28. {
  29. ThrowIfPartnerIsNull();
  30. return await Task.Run(() => _incomingPackets.Take());
  31. }
  32. private void SendPacketInternal(MqttBasePacket packet)
  33. {
  34. if (packet == null) throw new ArgumentNullException(nameof(packet));
  35. _incomingPackets.Add(packet);
  36. }
  37. private void ThrowIfPartnerIsNull()
  38. {
  39. if (Partner == null)
  40. {
  41. throw new InvalidOperationException("Partner is not set.");
  42. }
  43. }
  44. }
  45. }