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.

MqttClientSessionsManager.cs 6.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. using MQTTnet.Core.Protocol;
  12. namespace MQTTnet.Core.Server
  13. {
  14. public sealed class MqttClientSessionsManager
  15. {
  16. private readonly object _syncRoot = new object();
  17. private readonly Dictionary<string, MqttClientSession> _clientSessions = new Dictionary<string, MqttClientSession>();
  18. private readonly MqttServerOptions _options;
  19. public MqttClientSessionsManager(MqttServerOptions options)
  20. {
  21. _options = options ?? throw new ArgumentNullException(nameof(options));
  22. }
  23. public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
  24. public async Task RunClientSessionAsync(MqttClientConnectedEventArgs eventArgs)
  25. {
  26. try
  27. {
  28. if (!(await eventArgs.ClientAdapter.ReceivePacketAsync(_options.DefaultCommunicationTimeout, CancellationToken.None).ConfigureAwait(false) is MqttConnectPacket connectPacket))
  29. {
  30. throw new MqttProtocolViolationException("The first packet from a client must be a 'CONNECT' packet [MQTT-3.1.0-1].");
  31. }
  32. // Switch to the required protocol version before sending any response.
  33. eventArgs.ClientAdapter.PacketSerializer.ProtocolVersion = connectPacket.ProtocolVersion;
  34. var connectReturnCode = ValidateConnection(connectPacket);
  35. if (connectReturnCode != MqttConnectReturnCode.ConnectionAccepted)
  36. {
  37. await eventArgs.ClientAdapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, CancellationToken.None, new MqttConnAckPacket
  38. {
  39. ConnectReturnCode = connectReturnCode
  40. }).ConfigureAwait(false);
  41. return;
  42. }
  43. var clientSession = GetOrCreateClientSession(connectPacket);
  44. await eventArgs.ClientAdapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, CancellationToken.None, new MqttConnAckPacket
  45. {
  46. ConnectReturnCode = connectReturnCode,
  47. IsSessionPresent = clientSession.IsExistingSession
  48. }).ConfigureAwait(false);
  49. await clientSession.Session.RunAsync(eventArgs.Identifier, connectPacket.WillMessage, eventArgs.ClientAdapter).ConfigureAwait(false);
  50. }
  51. catch (Exception exception)
  52. {
  53. MqttTrace.Error(nameof(MqttServer), exception, exception.Message);
  54. }
  55. finally
  56. {
  57. await eventArgs.ClientAdapter.DisconnectAsync(_options.DefaultCommunicationTimeout).ConfigureAwait(false);
  58. }
  59. }
  60. public void Clear()
  61. {
  62. lock (_syncRoot)
  63. {
  64. _clientSessions.Clear();
  65. }
  66. }
  67. public IList<ConnectedMqttClient> GetConnectedClients()
  68. {
  69. lock (_syncRoot)
  70. {
  71. return _clientSessions.Where(s => s.Value.IsConnected).Select(s => new ConnectedMqttClient
  72. {
  73. ClientId = s.Value.ClientId,
  74. ProtocolVersion = s.Value.Adapter.PacketSerializer.ProtocolVersion
  75. }).ToList();
  76. }
  77. }
  78. private MqttConnectReturnCode ValidateConnection(MqttConnectPacket connectPacket)
  79. {
  80. if (_options.ConnectionValidator != null)
  81. {
  82. return _options.ConnectionValidator(connectPacket);
  83. }
  84. return MqttConnectReturnCode.ConnectionAccepted;
  85. }
  86. private GetOrCreateClientSessionResult GetOrCreateClientSession(MqttConnectPacket connectPacket)
  87. {
  88. lock (_syncRoot)
  89. {
  90. MqttClientSession clientSession;
  91. var isSessionPresent = _clientSessions.TryGetValue(connectPacket.ClientId, out clientSession);
  92. if (isSessionPresent)
  93. {
  94. if (connectPacket.CleanSession)
  95. {
  96. _clientSessions.Remove(connectPacket.ClientId);
  97. clientSession.Dispose();
  98. clientSession = null;
  99. MqttTrace.Verbose(nameof(MqttClientSessionsManager), "Disposed existing session of client '{0}'.", connectPacket.ClientId);
  100. }
  101. else
  102. {
  103. MqttTrace.Verbose(nameof(MqttClientSessionsManager), "Reusing existing session of client '{0}'.", connectPacket.ClientId);
  104. }
  105. }
  106. var isExistingSession = true;
  107. if (clientSession == null)
  108. {
  109. isExistingSession = false;
  110. clientSession = new MqttClientSession(connectPacket.ClientId, _options, DispatchPublishPacket);
  111. _clientSessions[connectPacket.ClientId] = clientSession;
  112. MqttTrace.Verbose(nameof(MqttClientSessionsManager), "Created a new session for client '{0}'.", connectPacket.ClientId);
  113. }
  114. return new GetOrCreateClientSessionResult { IsExistingSession = isExistingSession, Session = clientSession };
  115. }
  116. }
  117. public void DispatchPublishPacket(MqttClientSession senderClientSession, MqttPublishPacket publishPacket)
  118. {
  119. try
  120. {
  121. var eventArgs = new MqttApplicationMessageReceivedEventArgs(senderClientSession?.ClientId, publishPacket.ToApplicationMessage());
  122. ApplicationMessageReceived?.Invoke(this, eventArgs);
  123. }
  124. catch (Exception exception)
  125. {
  126. MqttTrace.Error(nameof(MqttClientSessionsManager), exception, "Error while processing application message");
  127. }
  128. lock (_syncRoot)
  129. {
  130. foreach (var clientSession in _clientSessions.Values.ToList())
  131. {
  132. clientSession.EnqueuePublishPacket(publishPacket);
  133. }
  134. }
  135. }
  136. }
  137. }