Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

MqttConnectionContext.cs 4.8 KiB

6 lat temu
6 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 bool IsSecureConnection => false; // TODO: Fix detection (WS vs. WSS).
  22. public ConnectionContext Connection { get; }
  23. public MqttPacketFormatterAdapter PacketFormatterAdapter { get; }
  24. public long BytesSent { get; } // TODO: Fix calculation.
  25. public long BytesReceived { get; } // TODO: Fix calculation.
  26. public Action ReadingPacketStartedCallback { get; set; }
  27. public Action ReadingPacketCompletedCallback { get; set; }
  28. private readonly SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);
  29. public Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
  30. {
  31. if (Connection is TcpConnection tcp && !tcp.IsConnected)
  32. {
  33. return tcp.StartAsync();
  34. }
  35. return Task.CompletedTask;
  36. }
  37. public Task DisconnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
  38. {
  39. Connection.Transport.Input.Complete();
  40. Connection.Transport.Output.Complete();
  41. return Task.CompletedTask;
  42. }
  43. public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken)
  44. {
  45. var input = Connection.Transport.Input;
  46. try
  47. {
  48. while (!cancellationToken.IsCancellationRequested)
  49. {
  50. ReadResult readResult;
  51. var readTask = input.ReadAsync(cancellationToken);
  52. if (readTask.IsCompleted)
  53. {
  54. readResult = readTask.Result;
  55. }
  56. else
  57. {
  58. readResult = await readTask.ConfigureAwait(false);
  59. }
  60. var buffer = readResult.Buffer;
  61. var consumed = buffer.Start;
  62. var observed = buffer.Start;
  63. try
  64. {
  65. if (!buffer.IsEmpty)
  66. {
  67. if (PacketFormatterAdapter.TryDecode(buffer, out var packet, out consumed, out observed))
  68. {
  69. return packet;
  70. }
  71. else
  72. {
  73. // we did receive something but the message is not yet complete
  74. ReadingPacketStartedCallback?.Invoke();
  75. }
  76. }
  77. else if (readResult.IsCompleted)
  78. {
  79. throw new MqttCommunicationException("Connection Aborted");
  80. }
  81. }
  82. finally
  83. {
  84. // The buffer was sliced up to where it was consumed, so we can just advance to the start.
  85. // We mark examined as buffer.End so that if we didn't receive a full frame, we'll wait for more data
  86. // before yielding the read again.
  87. input.AdvanceTo(consumed, observed);
  88. }
  89. }
  90. }
  91. finally
  92. {
  93. ReadingPacketCompletedCallback?.Invoke();
  94. }
  95. cancellationToken.ThrowIfCancellationRequested();
  96. return null;
  97. }
  98. public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout, CancellationToken cancellationToken)
  99. {
  100. var buffer = PacketFormatterAdapter.Encode(packet).AsMemory();
  101. var output = Connection.Transport.Output;
  102. await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  103. try
  104. {
  105. await output.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
  106. }
  107. finally
  108. {
  109. _writerSemaphore.Release();
  110. }
  111. }
  112. public void Dispose()
  113. {
  114. }
  115. }
  116. }