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.
 
 
 
 

92 lignes
2.6 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Security.Cryptography.X509Certificates;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Adapter;
  7. using MQTTnet.Formatter;
  8. using MQTTnet.Packets;
  9. namespace MQTTnet.Tests.Mockups
  10. {
  11. public class TestMqttCommunicationAdapter : IMqttChannelAdapter
  12. {
  13. private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
  14. public TestMqttCommunicationAdapter Partner { get; set; }
  15. public string Endpoint { get; } = string.Empty;
  16. public bool IsSecureConnection { get; } = false;
  17. public X509Certificate2 ClientCertificate { get; }
  18. public MqttPacketFormatterAdapter PacketFormatterAdapter { get; } = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
  19. public long BytesSent { get; }
  20. public long BytesReceived { get; }
  21. public Action ReadingPacketStartedCallback { get; set; }
  22. public Action ReadingPacketCompletedCallback { get; set; }
  23. public void Dispose()
  24. {
  25. }
  26. public Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
  27. {
  28. return Task.FromResult(0);
  29. }
  30. public Task DisconnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
  31. {
  32. return Task.FromResult(0);
  33. }
  34. public Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout, CancellationToken cancellationToken)
  35. {
  36. ThrowIfPartnerIsNull();
  37. Partner.EnqueuePacketInternal(packet);
  38. return Task.FromResult(0);
  39. }
  40. public Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
  41. {
  42. ThrowIfPartnerIsNull();
  43. return Task.Run(() =>
  44. {
  45. try
  46. {
  47. return _incomingPackets.Take(cancellationToken);
  48. }
  49. catch
  50. {
  51. return null;
  52. }
  53. }, cancellationToken);
  54. }
  55. public void ResetStatistics()
  56. {
  57. }
  58. private void EnqueuePacketInternal(MqttBasePacket packet)
  59. {
  60. if (packet == null) throw new ArgumentNullException(nameof(packet));
  61. _incomingPackets.Add(packet);
  62. }
  63. private void ThrowIfPartnerIsNull()
  64. {
  65. if (Partner == null)
  66. {
  67. throw new InvalidOperationException("Partner is not set.");
  68. }
  69. }
  70. }
  71. }