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.

MqttClientPendingMessagesQueue.cs 3.8 KiB

7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
7 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Core.Adapter;
  6. using MQTTnet.Core.Exceptions;
  7. using MQTTnet.Core.Packets;
  8. using MQTTnet.Core.Protocol;
  9. using Microsoft.Extensions.Logging;
  10. namespace MQTTnet.Core.Server
  11. {
  12. public sealed class MqttClientPendingMessagesQueue
  13. {
  14. private readonly BlockingCollection<MqttPublishPacket> _pendingPublishPackets = new BlockingCollection<MqttPublishPacket>();
  15. private readonly MqttClientSession _session;
  16. private readonly MqttServerOptions _options;
  17. private readonly ILogger<MqttClientPendingMessagesQueue> _logger;
  18. public MqttClientPendingMessagesQueue(MqttServerOptions options, MqttClientSession session, ILogger<MqttClientPendingMessagesQueue> 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(IMqttCommunicationAdapter adapter, CancellationToken cancellationToken)
  25. {
  26. if (adapter == null) throw new ArgumentNullException(nameof(adapter));
  27. Task.Factory.StartNew(async () => await SendPendingPublishPacketsAsync(adapter, cancellationToken), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).ConfigureAwait(false);
  28. }
  29. public void Enqueue(MqttPublishPacket publishPacket)
  30. {
  31. if (publishPacket == null) throw new ArgumentNullException(nameof(publishPacket));
  32. _pendingPublishPackets.Add(publishPacket);
  33. }
  34. private async Task SendPendingPublishPacketsAsync(IMqttCommunicationAdapter adapter, CancellationToken cancellationToken)
  35. {
  36. try
  37. {
  38. while (!cancellationToken.IsCancellationRequested)
  39. {
  40. await SendPendingPublishPacketAsync(adapter, cancellationToken);
  41. }
  42. }
  43. catch (OperationCanceledException)
  44. {
  45. }
  46. catch (Exception exception)
  47. {
  48. _logger.LogError(new EventId(), exception, "Unhandled exception while sending pending publish packets.");
  49. }
  50. }
  51. private async Task SendPendingPublishPacketAsync(IMqttCommunicationAdapter adapter, CancellationToken cancellationToken)
  52. {
  53. var packet = _pendingPublishPackets.Take(cancellationToken);
  54. try
  55. {
  56. await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, packet).ConfigureAwait(false);
  57. }
  58. catch (Exception exception)
  59. {
  60. if (exception is MqttCommunicationTimedOutException)
  61. {
  62. _logger.LogWarning(new EventId(), exception, "Sending publish packet failed due to timeout.");
  63. }
  64. else if (exception is MqttCommunicationException)
  65. {
  66. _logger.LogWarning(new EventId(), exception, "Sending publish packet failed due to communication exception.");
  67. }
  68. else if (exception is OperationCanceledException)
  69. {
  70. }
  71. else
  72. {
  73. _logger.LogError(new EventId(), exception, "Sending publish packet failed.");
  74. }
  75. if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
  76. {
  77. packet.Dup = true;
  78. _pendingPublishPackets.Add(packet, cancellationToken);
  79. }
  80. _session.Stop();
  81. }
  82. }
  83. }
  84. }