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.
 
 
 
 

129 lines
4.6 KiB

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