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.

MqttClientMessageQueue.cs 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Core.Adapter;
  7. using MQTTnet.Core.Diagnostics;
  8. using MQTTnet.Core.Exceptions;
  9. using MQTTnet.Core.Internal;
  10. using MQTTnet.Core.Packets;
  11. namespace MQTTnet.Core.Server
  12. {
  13. public sealed class MqttClientMessageQueue
  14. {
  15. private readonly List<MqttClientPublishPacketContext> _pendingPublishPackets = new List<MqttClientPublishPacketContext>();
  16. private readonly AsyncGate _gate = new AsyncGate();
  17. private readonly MqttServerOptions _options;
  18. private CancellationTokenSource _cancellationTokenSource;
  19. private IMqttCommunicationAdapter _adapter;
  20. public MqttClientMessageQueue(MqttServerOptions options)
  21. {
  22. _options = options ?? throw new ArgumentNullException(nameof(options));
  23. }
  24. public void Start(IMqttCommunicationAdapter adapter)
  25. {
  26. if (_cancellationTokenSource != null)
  27. {
  28. throw new InvalidOperationException($"{nameof(MqttClientMessageQueue)} already started.");
  29. }
  30. _adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
  31. _cancellationTokenSource = new CancellationTokenSource();
  32. Task.Run(() => SendPendingPublishPacketsAsync(_cancellationTokenSource.Token));
  33. }
  34. public void Stop()
  35. {
  36. _adapter = null;
  37. _cancellationTokenSource?.Cancel();
  38. _cancellationTokenSource = null;
  39. }
  40. public void Enqueue(MqttClientSession senderClientSession, MqttPublishPacket publishPacket)
  41. {
  42. if (senderClientSession == null) throw new ArgumentNullException(nameof(senderClientSession));
  43. if (publishPacket == null) throw new ArgumentNullException(nameof(publishPacket));
  44. lock (_pendingPublishPackets)
  45. {
  46. _pendingPublishPackets.Add(new MqttClientPublishPacketContext(senderClientSession, publishPacket));
  47. _gate.Set();
  48. }
  49. }
  50. private async Task SendPendingPublishPacketsAsync(CancellationToken cancellationToken)
  51. {
  52. while (!cancellationToken.IsCancellationRequested)
  53. {
  54. try
  55. {
  56. await _gate.WaitOneAsync();
  57. if (cancellationToken.IsCancellationRequested)
  58. {
  59. return;
  60. }
  61. if (_adapter == null)
  62. {
  63. continue;
  64. }
  65. List<MqttClientPublishPacketContext> pendingPublishPackets;
  66. lock (_pendingPublishPackets)
  67. {
  68. pendingPublishPackets = _pendingPublishPackets.ToList();
  69. }
  70. foreach (var publishPacket in pendingPublishPackets)
  71. {
  72. await TrySendPendingPublishPacketAsync(publishPacket);
  73. }
  74. }
  75. catch (Exception e)
  76. {
  77. MqttTrace.Error(nameof(MqttClientMessageQueue), e, "Error while sending pending publish packets.");
  78. }
  79. finally
  80. {
  81. Cleanup();
  82. }
  83. }
  84. }
  85. private async Task TrySendPendingPublishPacketAsync(MqttClientPublishPacketContext publishPacketContext)
  86. {
  87. try
  88. {
  89. if (_adapter == null)
  90. {
  91. return;
  92. }
  93. publishPacketContext.PublishPacket.Dup = publishPacketContext.SendTries > 0;
  94. await _adapter.SendPacketAsync(publishPacketContext.PublishPacket, _options.DefaultCommunicationTimeout);
  95. publishPacketContext.IsSent = true;
  96. }
  97. catch (MqttCommunicationException exception)
  98. {
  99. MqttTrace.Warning(nameof(MqttClientMessageQueue), exception, "Sending publish packet failed.");
  100. }
  101. catch (Exception exception)
  102. {
  103. MqttTrace.Error(nameof(MqttClientMessageQueue), exception, "Sending publish packet failed.");
  104. }
  105. finally
  106. {
  107. publishPacketContext.SendTries++;
  108. }
  109. }
  110. private void Cleanup()
  111. {
  112. lock (_pendingPublishPackets)
  113. {
  114. _pendingPublishPackets.RemoveAll(p => p.IsSent);
  115. }
  116. }
  117. }
  118. }