Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

281 rader
11 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Core.Adapter;
  6. using MQTTnet.Core.Exceptions;
  7. using MQTTnet.Core.Internal;
  8. using MQTTnet.Core.Packets;
  9. using MQTTnet.Core.Protocol;
  10. using MQTTnet.Core.Serializer;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. namespace MQTTnet.Core.Server
  14. {
  15. public sealed class MqttClientSession : IDisposable
  16. {
  17. private readonly HashSet<ushort> _unacknowledgedPublishPackets = new HashSet<ushort>();
  18. private readonly MqttClientSubscriptionsManager _subscriptionsManager;
  19. private readonly MqttClientSessionsManager _sessionsManager;
  20. private readonly MqttClientPendingMessagesQueue _pendingMessagesQueue;
  21. private readonly MqttServerOptions _options;
  22. private readonly ILogger<MqttClientSession> _logger;
  23. private IMqttCommunicationAdapter _adapter;
  24. private CancellationTokenSource _cancellationTokenSource;
  25. private MqttApplicationMessage _willMessage;
  26. public MqttClientSession(
  27. string clientId,
  28. IOptions<MqttServerOptions> options,
  29. MqttClientSessionsManager sessionsManager,
  30. MqttClientSubscriptionsManager subscriptionsManager,
  31. ILogger<MqttClientSession> logger,
  32. ILogger<MqttClientPendingMessagesQueue> messageQueueLogger)
  33. {
  34. _sessionsManager = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));
  35. _subscriptionsManager = subscriptionsManager ?? throw new ArgumentNullException(nameof(sessionsManager));
  36. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  37. ClientId = clientId;
  38. _options = options.Value;
  39. _pendingMessagesQueue = new MqttClientPendingMessagesQueue(_options, this, messageQueueLogger);
  40. }
  41. public string ClientId { get; }
  42. public MqttProtocolVersion? ProtocolVersion => _adapter?.PacketSerializer.ProtocolVersion;
  43. public bool IsConnected => _adapter != null;
  44. public async Task RunAsync(MqttApplicationMessage willMessage, IMqttCommunicationAdapter adapter)
  45. {
  46. if (adapter == null) throw new ArgumentNullException(nameof(adapter));
  47. try
  48. {
  49. _willMessage = willMessage;
  50. _adapter = adapter;
  51. _cancellationTokenSource = new CancellationTokenSource();
  52. _pendingMessagesQueue.Start(adapter, _cancellationTokenSource.Token);
  53. await ReceivePacketsAsync(adapter, _cancellationTokenSource.Token);
  54. }
  55. catch (OperationCanceledException)
  56. {
  57. }
  58. catch (MqttCommunicationException exception)
  59. {
  60. _logger.LogWarning(new EventId(), exception, "Client '{0}': Communication exception while processing client packets.", ClientId);
  61. }
  62. catch (Exception exception)
  63. {
  64. _logger.LogError(new EventId(), exception, "Client '{0}': Unhandled exception while processing client packets.", ClientId);
  65. }
  66. }
  67. public void Stop()
  68. {
  69. try
  70. {
  71. _cancellationTokenSource?.Cancel(false);
  72. _cancellationTokenSource?.Dispose();
  73. _cancellationTokenSource = null;
  74. _adapter = null;
  75. _logger.LogInformation("Client '{0}': Disconnected.", ClientId);
  76. }
  77. finally
  78. {
  79. if (_willMessage != null)
  80. {
  81. _sessionsManager.DispatchApplicationMessage(this, _willMessage);
  82. }
  83. }
  84. }
  85. public void EnqueuePublishPacket(MqttPublishPacket publishPacket)
  86. {
  87. if (publishPacket == null) throw new ArgumentNullException(nameof(publishPacket));
  88. if (!_subscriptionsManager.IsSubscribed(publishPacket))
  89. {
  90. return;
  91. }
  92. _pendingMessagesQueue.Enqueue(publishPacket);
  93. _logger.LogTrace("Client '{0}': Enqueued pending publish packet.", ClientId);
  94. }
  95. public void Dispose()
  96. {
  97. _cancellationTokenSource?.Cancel();
  98. _cancellationTokenSource?.Dispose();
  99. }
  100. private async Task ReceivePacketsAsync(IMqttCommunicationAdapter adapter, CancellationToken cancellationToken)
  101. {
  102. try
  103. {
  104. while (!cancellationToken.IsCancellationRequested)
  105. {
  106. var packet = await adapter.ReceivePacketAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false);
  107. await ProcessReceivedPacketAsync(adapter, packet, cancellationToken).ConfigureAwait(false);
  108. }
  109. }
  110. catch (OperationCanceledException)
  111. {
  112. }
  113. catch (MqttCommunicationException exception)
  114. {
  115. _logger.LogWarning(new EventId(), exception, "Client '{0}': Communication exception while processing client packets.", ClientId);
  116. Stop();
  117. }
  118. catch (Exception exception)
  119. {
  120. _logger.LogError(new EventId(), exception, "Client '{0}': Unhandled exception while processing client packets.", ClientId);
  121. Stop();
  122. }
  123. }
  124. private Task ProcessReceivedPacketAsync(IMqttCommunicationAdapter adapter, MqttBasePacket packet, CancellationToken cancellationToken)
  125. {
  126. if (packet is MqttPingReqPacket)
  127. {
  128. return adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, new MqttPingRespPacket());
  129. }
  130. if (packet is MqttPublishPacket publishPacket)
  131. {
  132. return HandleIncomingPublishPacketAsync(adapter, publishPacket, cancellationToken);
  133. }
  134. if (packet is MqttPubRelPacket pubRelPacket)
  135. {
  136. return HandleIncomingPubRelPacketAsync(adapter, pubRelPacket, cancellationToken);
  137. }
  138. if (packet is MqttPubRecPacket pubRecPacket)
  139. {
  140. return adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, pubRecPacket.CreateResponse<MqttPubRelPacket>());
  141. }
  142. if (packet is MqttPubAckPacket || packet is MqttPubCompPacket)
  143. {
  144. // Discard message.
  145. return Task.FromResult(0);
  146. }
  147. if (packet is MqttSubscribePacket subscribePacket)
  148. {
  149. return HandleIncomingSubscribePacketAsync(adapter, subscribePacket, cancellationToken);
  150. }
  151. if (packet is MqttUnsubscribePacket unsubscribePacket)
  152. {
  153. return adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, _subscriptionsManager.Unsubscribe(unsubscribePacket));
  154. }
  155. if (packet is MqttDisconnectPacket || packet is MqttConnectPacket)
  156. {
  157. Stop();
  158. return Task.FromResult(0);
  159. }
  160. _logger.LogWarning("Client '{0}': Received not supported packet ({1}). Closing connection.", ClientId, packet);
  161. Stop();
  162. return Task.FromResult(0);
  163. }
  164. private async Task HandleIncomingSubscribePacketAsync(IMqttCommunicationAdapter adapter, MqttSubscribePacket subscribePacket, CancellationToken cancellationToken)
  165. {
  166. var subscribeResult = _subscriptionsManager.Subscribe(subscribePacket, ClientId);
  167. await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, subscribeResult.ResponsePacket);
  168. EnqueueRetainedMessages(subscribePacket);
  169. if (subscribeResult.CloseConnection)
  170. {
  171. await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, new MqttDisconnectPacket());
  172. Stop();
  173. }
  174. }
  175. private void EnqueueRetainedMessages(MqttSubscribePacket subscribePacket)
  176. {
  177. var retainedMessages = _sessionsManager.RetainedMessagesManager.GetMessages(subscribePacket);
  178. foreach (var publishPacket in retainedMessages)
  179. {
  180. EnqueuePublishPacket(publishPacket.ToPublishPacket());
  181. }
  182. }
  183. private async Task HandleIncomingPublishPacketAsync(IMqttCommunicationAdapter adapter, MqttPublishPacket publishPacket, CancellationToken cancellationToken)
  184. {
  185. var applicationMessage = publishPacket.ToApplicationMessage();
  186. var interceptorContext = new MqttApplicationMessageInterceptorContext
  187. {
  188. ApplicationMessage = applicationMessage
  189. };
  190. _options.ApplicationMessageInterceptor?.Invoke(interceptorContext);
  191. applicationMessage = interceptorContext.ApplicationMessage;
  192. if (applicationMessage.Retain)
  193. {
  194. await _sessionsManager.RetainedMessagesManager.HandleMessageAsync(ClientId, applicationMessage);
  195. }
  196. switch (applicationMessage.QualityOfServiceLevel)
  197. {
  198. case MqttQualityOfServiceLevel.AtMostOnce:
  199. {
  200. _sessionsManager.DispatchApplicationMessage(this, applicationMessage);
  201. return;
  202. }
  203. case MqttQualityOfServiceLevel.AtLeastOnce:
  204. {
  205. _sessionsManager.DispatchApplicationMessage(this, applicationMessage);
  206. await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken,
  207. new MqttPubAckPacket { PacketIdentifier = publishPacket.PacketIdentifier });
  208. return;
  209. }
  210. case MqttQualityOfServiceLevel.ExactlyOnce:
  211. {
  212. // QoS 2 is implement as method "B" [4.3.3 QoS 2: Exactly once delivery]
  213. lock (_unacknowledgedPublishPackets)
  214. {
  215. _unacknowledgedPublishPackets.Add(publishPacket.PacketIdentifier);
  216. }
  217. _sessionsManager.DispatchApplicationMessage(this, applicationMessage);
  218. await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken,
  219. new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier });
  220. return;
  221. }
  222. default:
  223. throw new MqttCommunicationException("Received a not supported QoS level.");
  224. }
  225. }
  226. private Task HandleIncomingPubRelPacketAsync(IMqttCommunicationAdapter adapter, MqttPubRelPacket pubRelPacket, CancellationToken cancellationToken)
  227. {
  228. lock (_unacknowledgedPublishPackets)
  229. {
  230. _unacknowledgedPublishPackets.Remove(pubRelPacket.PacketIdentifier);
  231. }
  232. return adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, new MqttPubCompPacket { PacketIdentifier = pubRelPacket.PacketIdentifier });
  233. }
  234. }
  235. }