You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttConnectionContext.cs 4.4 KiB

преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Microsoft.AspNetCore.Connections;
  2. using MQTTnet.Adapter;
  3. using MQTTnet.AspNetCore.Client.Tcp;
  4. using MQTTnet.Exceptions;
  5. using MQTTnet.Packets;
  6. using MQTTnet.Serializer;
  7. using System;
  8. using System.IO.Pipelines;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MQTTnet.AspNetCore
  12. {
  13. public class MqttConnectionContext : IMqttChannelAdapter
  14. {
  15. public MqttConnectionContext(
  16. IMqttPacketSerializer packetSerializer,
  17. ConnectionContext connection)
  18. {
  19. PacketSerializer = packetSerializer;
  20. Connection = connection;
  21. }
  22. public string Endpoint => Connection.ConnectionId;
  23. public ConnectionContext Connection { get; }
  24. public IMqttPacketSerializer PacketSerializer { get; }
  25. public event EventHandler ReadingPacketStarted;
  26. public event EventHandler ReadingPacketCompleted;
  27. private readonly SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);
  28. public Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
  29. {
  30. if (Connection is TcpConnection tcp && !tcp.IsConnected)
  31. {
  32. return tcp.StartAsync();
  33. }
  34. return Task.CompletedTask;
  35. }
  36. public Task DisconnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
  37. {
  38. Connection.Transport.Input.Complete();
  39. Connection.Transport.Output.Complete();
  40. return Task.CompletedTask;
  41. }
  42. public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
  43. {
  44. var input = Connection.Transport.Input;
  45. try
  46. {
  47. while (!cancellationToken.IsCancellationRequested)
  48. {
  49. ReadResult readResult;
  50. var readTask = input.ReadAsync(cancellationToken);
  51. if (readTask.IsCompleted)
  52. {
  53. readResult = readTask.Result;
  54. }
  55. else
  56. {
  57. readResult = await readTask.ConfigureAwait(false);
  58. }
  59. var buffer = readResult.Buffer;
  60. var consumed = buffer.Start;
  61. var observed = buffer.Start;
  62. try
  63. {
  64. if (!buffer.IsEmpty)
  65. {
  66. if (PacketSerializer.TryDeserialize(buffer, out var packet, out consumed, out observed))
  67. {
  68. return packet;
  69. }
  70. else
  71. {
  72. // we did receive something but the message is not yet complete
  73. ReadingPacketStarted?.Invoke(this, EventArgs.Empty);
  74. }
  75. }
  76. else if (readResult.IsCompleted)
  77. {
  78. throw new MqttCommunicationException("Connection Aborted");
  79. }
  80. }
  81. finally
  82. {
  83. // The buffer was sliced up to where it was consumed, so we can just advance to the start.
  84. // We mark examined as buffer.End so that if we didn't receive a full frame, we'll wait for more data
  85. // before yielding the read again.
  86. input.AdvanceTo(consumed, observed);
  87. }
  88. }
  89. }
  90. finally
  91. {
  92. ReadingPacketCompleted?.Invoke(this, EventArgs.Empty);
  93. }
  94. cancellationToken.ThrowIfCancellationRequested();
  95. return null;
  96. }
  97. public async Task SendPacketAsync(MqttBasePacket packet, CancellationToken cancellationToken)
  98. {
  99. var buffer = PacketSerializer.Serialize(packet).AsMemory();
  100. var output = Connection.Transport.Output;
  101. await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  102. try
  103. {
  104. await output.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
  105. }
  106. finally
  107. {
  108. _writerSemaphore.Release();
  109. }
  110. }
  111. public void Dispose()
  112. {
  113. }
  114. }
  115. }