Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

112 рядки
4.3 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Adapter;
  6. using MQTTnet.Diagnostics;
  7. using MQTTnet.Exceptions;
  8. using MQTTnet.Packets;
  9. using MQTTnet.Protocol;
  10. namespace MQTTnet.Server
  11. {
  12. public sealed class MqttClientPendingMessagesQueue : IDisposable
  13. {
  14. private readonly BlockingCollection<MqttBasePacket> _queue = new BlockingCollection<MqttBasePacket>();
  15. private readonly IMqttServerOptions _options;
  16. private readonly MqttClientSession _session;
  17. private readonly IMqttNetLogger _logger;
  18. public MqttClientPendingMessagesQueue(IMqttServerOptions options, MqttClientSession session, IMqttNetLogger logger)
  19. {
  20. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  21. _session = session ?? throw new ArgumentNullException(nameof(session));
  22. _options = options ?? throw new ArgumentNullException(nameof(options));
  23. }
  24. public void Start(IMqttChannelAdapter adapter, CancellationToken cancellationToken)
  25. {
  26. if (adapter == null) throw new ArgumentNullException(nameof(adapter));
  27. if (cancellationToken.IsCancellationRequested)
  28. {
  29. return;
  30. }
  31. Task.Run(async () => await SendQueuedPacketsAsync(adapter, cancellationToken), cancellationToken).ConfigureAwait(false);
  32. }
  33. public void Enqueue(MqttBasePacket packet)
  34. {
  35. if (packet == null) throw new ArgumentNullException(nameof(packet));
  36. _queue.Add(packet);
  37. _logger.Trace<MqttClientPendingMessagesQueue>("Enqueued packet (ClientId: {0}).", _session.ClientId);
  38. }
  39. private async Task SendQueuedPacketsAsync(IMqttChannelAdapter adapter, CancellationToken cancellationToken)
  40. {
  41. try
  42. {
  43. while (!cancellationToken.IsCancellationRequested)
  44. {
  45. await SendQueuedPacketAsync(adapter, cancellationToken);
  46. }
  47. }
  48. catch (OperationCanceledException)
  49. {
  50. }
  51. catch (Exception exception)
  52. {
  53. _logger.Error<MqttClientPendingMessagesQueue>(exception, "Unhandled exception while sending enqueued packet (ClientId: {0}).", _session.ClientId);
  54. }
  55. }
  56. private async Task SendQueuedPacketAsync(IMqttChannelAdapter adapter, CancellationToken cancellationToken)
  57. {
  58. MqttBasePacket packet = null;
  59. try
  60. {
  61. packet = _queue.Take(cancellationToken);
  62. await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, packet).ConfigureAwait(false);
  63. _logger.Trace<MqttClientPendingMessagesQueue>("Enqueued packet sent (ClientId: {0}).", _session.ClientId);
  64. }
  65. catch (Exception exception)
  66. {
  67. if (exception is MqttCommunicationTimedOutException)
  68. {
  69. _logger.Warning<MqttClientPendingMessagesQueue>(exception, "Sending publish packet failed due to timeout (ClientId: {0}).", _session.ClientId);
  70. }
  71. else if (exception is MqttCommunicationException)
  72. {
  73. _logger.Warning<MqttClientPendingMessagesQueue>(exception, "Sending publish packet failed due to communication exception (ClientId: {0}).", _session.ClientId);
  74. }
  75. else if (exception is OperationCanceledException)
  76. {
  77. }
  78. else
  79. {
  80. _logger.Error<MqttClientPendingMessagesQueue>(exception, "Sending publish packet failed (ClientId: {0}).", _session.ClientId);
  81. }
  82. if (packet is MqttPublishPacket publishPacket)
  83. {
  84. if (publishPacket.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
  85. {
  86. publishPacket.Dup = true;
  87. _queue.Add(packet, CancellationToken.None);
  88. }
  89. }
  90. await _session.StopAsync();
  91. }
  92. }
  93. public void Dispose()
  94. {
  95. _queue?.Dispose();
  96. }
  97. }
  98. }