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.
 
 
 
 

158 lines
6.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Core.Adapter;
  6. using MQTTnet.Core.Diagnostics;
  7. using MQTTnet.Core.Exceptions;
  8. using MQTTnet.Core.Internal;
  9. using MQTTnet.Core.Packets;
  10. using MQTTnet.Core.Protocol;
  11. namespace MQTTnet.Core.Server
  12. {
  13. public sealed class MqttClientSessionsManager
  14. {
  15. private readonly object _syncRoot = new object();
  16. private readonly Dictionary<string, MqttClientSession> _clientSessions = new Dictionary<string, MqttClientSession>();
  17. private readonly MqttServerOptions _options;
  18. public MqttClientSessionsManager(MqttServerOptions options)
  19. {
  20. _options = options ?? throw new ArgumentNullException(nameof(options));
  21. }
  22. public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
  23. public async Task RunClientSessionAsync(MqttClientConnectedEventArgs eventArgs)
  24. {
  25. try
  26. {
  27. var connectPacket = await eventArgs.ClientAdapter.ReceivePacketAsync(_options.DefaultCommunicationTimeout).ConfigureAwait(false) as MqttConnectPacket;
  28. if (connectPacket == null)
  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.SendPacketAsync(new MqttConnAckPacket
  38. {
  39. ConnectReturnCode = connectReturnCode
  40. }, _options.DefaultCommunicationTimeout).ConfigureAwait(false);
  41. return;
  42. }
  43. var clientSession = GetOrCreateClientSession(connectPacket);
  44. await eventArgs.ClientAdapter.SendPacketAsync(new MqttConnAckPacket
  45. {
  46. ConnectReturnCode = connectReturnCode,
  47. IsSessionPresent = clientSession.IsExistingSession
  48. }, _options.DefaultCommunicationTimeout).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().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. }