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.
 
 
 
 

118 line
4.0 KiB

  1. using Microsoft.AspNetCore.Connections;
  2. using MQTTnet.Adapter;
  3. using MQTTnet.AspNetCore.Client.Tcp;
  4. using MQTTnet.Packets;
  5. using MQTTnet.Serializer;
  6. using System;
  7. using System.IO.Pipelines;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MQTTnet.AspNetCore
  11. {
  12. public class MqttConnectionContext : IMqttChannelAdapter
  13. {
  14. public MqttConnectionContext(
  15. IMqttPacketSerializer packetSerializer,
  16. ConnectionContext connection)
  17. {
  18. PacketSerializer = packetSerializer;
  19. Connection = connection;
  20. }
  21. public string Endpoint => Connection.ConnectionId;
  22. public ConnectionContext Connection { get; }
  23. public IMqttPacketSerializer PacketSerializer { get; }
  24. public event EventHandler ReadingPacketStarted;
  25. public event EventHandler ReadingPacketCompleted;
  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;
  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 (PacketSerializer.TryDeserialize(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. break;
  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 Task SendPacketAsync(MqttBasePacket packet, CancellationToken cancellationToken)
  96. {
  97. var buffer = PacketSerializer.Serialize(packet);
  98. return Connection.Transport.Output.WriteAsync(buffer.AsMemory(), cancellationToken).AsTask();
  99. }
  100. public void Dispose()
  101. {
  102. }
  103. }
  104. }