Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestMqttCommunicationAdapter.cs 2.1 KiB

pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 7 gadiem
pirms 6 gadiem
pirms 7 gadiem
pirms 7 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Adapter;
  7. using MQTTnet.Packets;
  8. using MQTTnet.Serializer;
  9. namespace MQTTnet.Core.Tests
  10. {
  11. public class TestMqttCommunicationAdapter : IMqttChannelAdapter
  12. {
  13. private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
  14. public TestMqttCommunicationAdapter Partner { get; set; }
  15. public IMqttPacketSerializer PacketSerializer { get; } = new MqttPacketSerializer();
  16. public void Dispose()
  17. {
  18. }
  19. public Task ConnectAsync(TimeSpan timeout)
  20. {
  21. return Task.FromResult(0);
  22. }
  23. public Task DisconnectAsync(TimeSpan timeout)
  24. {
  25. return Task.FromResult(0);
  26. }
  27. public Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, IEnumerable<MqttBasePacket> packets)
  28. {
  29. ThrowIfPartnerIsNull();
  30. foreach (var packet in packets)
  31. {
  32. Partner.EnqueuePacketInternal(packet);
  33. }
  34. return Task.FromResult(0);
  35. }
  36. public Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
  37. {
  38. ThrowIfPartnerIsNull();
  39. return Task.Run(() =>
  40. {
  41. try
  42. {
  43. return _incomingPackets.Take(cancellationToken);
  44. }
  45. catch
  46. {
  47. return null;
  48. }
  49. }, cancellationToken);
  50. }
  51. private void EnqueuePacketInternal(MqttBasePacket packet)
  52. {
  53. if (packet == null) throw new ArgumentNullException(nameof(packet));
  54. _incomingPackets.Add(packet);
  55. }
  56. private void ThrowIfPartnerIsNull()
  57. {
  58. if (Partner == null)
  59. {
  60. throw new InvalidOperationException("Partner is not set.");
  61. }
  62. }
  63. }
  64. }