mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 09:13:02 +08:00
Fix MQTT 3.1.1 session persistence when CleanSession=1 (#1294)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Server.Status;
|
||||
|
||||
@@ -8,24 +8,34 @@ namespace MQTTnet.Server.Internal
|
||||
{
|
||||
readonly DateTime _createdTimestamp = DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// Session should persist if CleanSession was set to false (Mqtt3) or if SessionExpiryInterval != 0 (Mqtt5)
|
||||
/// </summary>
|
||||
readonly bool _isPersistent;
|
||||
|
||||
public MqttClientSession(
|
||||
string clientId,
|
||||
IDictionary<object, object> items,
|
||||
MqttServerEventDispatcher eventDispatcher,
|
||||
IMqttServerOptions serverOptions,
|
||||
IMqttRetainedMessagesManager retainedMessagesManager)
|
||||
IMqttRetainedMessagesManager retainedMessagesManager,
|
||||
bool isPersistent
|
||||
)
|
||||
{
|
||||
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
|
||||
Items = items ?? throw new ArgumentNullException(nameof(items));
|
||||
|
||||
SubscriptionsManager = new MqttClientSubscriptionsManager(this, serverOptions, eventDispatcher, retainedMessagesManager);
|
||||
ApplicationMessagesQueue = new MqttClientSessionApplicationMessagesQueue(serverOptions);
|
||||
_isPersistent = isPersistent;
|
||||
}
|
||||
|
||||
public string ClientId { get; }
|
||||
|
||||
public bool IsCleanSession { get; set; } = true;
|
||||
|
||||
public bool IsPersistent => _isPersistent;
|
||||
|
||||
public MqttApplicationMessage WillMessage { get; set; }
|
||||
|
||||
public MqttClientSubscriptionsManager SubscriptionsManager { get; }
|
||||
|
||||
@@ -126,7 +126,7 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
// Pass connAckPacket so that IsSessionPresent flag can be set if the client session already exists
|
||||
clientConnection = await CreateClientConnection(connectPacket, connAckPacket, channelAdapter,
|
||||
connectionValidatorContext.SessionItems).ConfigureAwait(false);
|
||||
connectionValidatorContext).ConfigureAwait(false);
|
||||
|
||||
await channelAdapter.SendPacketAsync(connAckPacket, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
@@ -156,7 +156,7 @@ namespace MQTTnet.Server.Internal
|
||||
_clientConnections.Remove(clientConnection.ClientId);
|
||||
}
|
||||
|
||||
if (!_options.EnablePersistentSessions)
|
||||
if ((!_options.EnablePersistentSessions) || (!clientConnection.Session.IsPersistent))
|
||||
{
|
||||
await DeleteSessionAsync(clientConnection.ClientId).ConfigureAwait(false);
|
||||
}
|
||||
@@ -491,10 +491,35 @@ namespace MQTTnet.Server.Internal
|
||||
MqttConnectPacket connectPacket,
|
||||
MqttConnAckPacket connAckPacket,
|
||||
IMqttChannelAdapter channelAdapter,
|
||||
IDictionary<object, object> sessionItems)
|
||||
MqttConnectionValidatorContext context)
|
||||
{
|
||||
MqttClientConnection connection;
|
||||
|
||||
bool sessionShouldPersist;
|
||||
|
||||
if (context.ProtocolVersion == MqttProtocolVersion.V500)
|
||||
{
|
||||
// MQTT 5.0 section 3.1.2.11.2
|
||||
// The Client and Server MUST store the Session State after the Network Connection is closed if the Session Expiry Interval is greater than 0 [MQTT-3.1.2-23].
|
||||
//
|
||||
// A Client that only wants to process messages while connected will set the Clean Start to 1 and set the Session Expiry Interval to 0.
|
||||
// It will not receive Application Messages published before it connected and has to subscribe afresh to any topics that it is interested
|
||||
// in each time it connects.
|
||||
|
||||
// Persist if SessionExpiryInterval != 0, but may start with a clean session
|
||||
sessionShouldPersist = context.SessionExpiryInterval != 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// MQTT 3.1.1 section 3.1.2.4: persist only if 'not CleanSession'
|
||||
//
|
||||
// If CleanSession is set to 1, the Client and Server MUST discard any previous Session and start a new one.
|
||||
// This Session lasts as long as the Network Connection. State data associated with this Session MUST NOT be
|
||||
// reused in any subsequent Session [MQTT-3.1.2-6].
|
||||
|
||||
sessionShouldPersist = !connectPacket.CleanSession;
|
||||
}
|
||||
|
||||
using (await _createConnectionSyncRoot.WaitAsync(CancellationToken.None).ConfigureAwait(false))
|
||||
{
|
||||
MqttClientSession session;
|
||||
@@ -503,14 +528,14 @@ namespace MQTTnet.Server.Internal
|
||||
if (!_clientSessions.TryGetValue(connectPacket.ClientId, out session))
|
||||
{
|
||||
_logger.Verbose("Created a new session for client '{0}'.", connectPacket.ClientId);
|
||||
session = CreateSession(connectPacket.ClientId, sessionItems);
|
||||
session = CreateSession(connectPacket.ClientId, context.SessionItems, sessionShouldPersist);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (connectPacket.CleanSession)
|
||||
{
|
||||
_logger.Verbose("Deleting existing session of client '{0}'.", connectPacket.ClientId);
|
||||
session = CreateSession(connectPacket.ClientId, sessionItems);
|
||||
session = CreateSession(connectPacket.ClientId, context.SessionItems, sessionShouldPersist);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -604,14 +629,16 @@ namespace MQTTnet.Server.Internal
|
||||
_rootLogger);
|
||||
}
|
||||
|
||||
MqttClientSession CreateSession(string clientId, IDictionary<object, object> sessionItems)
|
||||
MqttClientSession CreateSession(string clientId, IDictionary<object, object> sessionItems, bool isPersistent)
|
||||
{
|
||||
return new MqttClientSession(
|
||||
clientId,
|
||||
sessionItems,
|
||||
_eventDispatcher,
|
||||
_options,
|
||||
_retainedMessagesManager);
|
||||
_retainedMessagesManager,
|
||||
isPersistent
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user