Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestMqttCommunicationAdapter.cs 1.5 KiB

il y a 7 ans
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }