選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

104 行
4.2 KiB

  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 MqttServerOptions _options;
  16. private readonly MqttClientSession _session;
  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. if (cancellationToken.IsCancellationRequested)
  28. {
  29. return;
  30. }
  31. Task.Run(async () => await SendPendingPublishPacketsAsync(adapter, cancellationToken), cancellationToken).ConfigureAwait(false);
  32. }
  33. public void Enqueue(MqttPublishPacket publishPacket)
  34. {
  35. if (publishPacket == null) throw new ArgumentNullException(nameof(publishPacket));
  36. _pendingPublishPackets.Add(publishPacket);
  37. _logger.LogTrace("Enqueued packet (ClientId: {0}).", _session.ClientId);
  38. }
  39. private async Task SendPendingPublishPacketsAsync(IMqttCommunicationAdapter adapter, CancellationToken cancellationToken)
  40. {
  41. try
  42. {
  43. while (!cancellationToken.IsCancellationRequested)
  44. {
  45. await SendPendingPublishPacketAsync(adapter, cancellationToken);
  46. }
  47. }
  48. catch (OperationCanceledException)
  49. {
  50. }
  51. catch (Exception exception)
  52. {
  53. _logger.LogError(new EventId(), exception, "Unhandled exception while sending enqueued packet (ClientId: {0}).", _session.ClientId);
  54. }
  55. }
  56. private async Task SendPendingPublishPacketAsync(IMqttCommunicationAdapter adapter, CancellationToken cancellationToken)
  57. {
  58. MqttPublishPacket packet = null;
  59. try
  60. {
  61. packet = _pendingPublishPackets.Take(cancellationToken);
  62. await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, packet).ConfigureAwait(false);
  63. _logger.LogTrace("Enqueued packet sent (ClientId: {0}).", _session.ClientId);
  64. }
  65. catch (Exception exception)
  66. {
  67. if (exception is MqttCommunicationTimedOutException)
  68. {
  69. _logger.LogWarning(new EventId(), exception, "Sending publish packet failed due to timeout (ClientId: {0}).", _session.ClientId);
  70. }
  71. else if (exception is MqttCommunicationException)
  72. {
  73. _logger.LogWarning(new EventId(), 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.LogError(new EventId(), exception, "Sending publish packet failed (ClientId: {0}).", _session.ClientId);
  81. }
  82. if (packet != null && packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
  83. {
  84. packet.Dup = true;
  85. _pendingPublishPackets.Add(packet, CancellationToken.None);
  86. }
  87. _session.Stop();
  88. }
  89. }
  90. }
  91. }