Fix MQTT5 'IsPersisted' session flag handling (#1300)

This commit is contained in:
logicaloud
2021-11-28 12:51:29 +01:00
committed by GitHub
parent 25ddfc83a8
commit 731c89d2e9
3 changed files with 118 additions and 28 deletions
@@ -8,11 +8,6 @@ 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,
@@ -24,17 +19,19 @@ namespace MQTTnet.Server.Internal
{
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
Items = items ?? throw new ArgumentNullException(nameof(items));
IsPersistent = isPersistent;
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;
/// <summary>
/// Session should persist if CleanSession was set to false (Mqtt3) or if SessionExpiryInterval != 0 (Mqtt5)
/// </summary>
public bool IsPersistent { get; set; }
public MqttApplicationMessage WillMessage { get; set; }
@@ -507,7 +507,7 @@ namespace MQTTnet.Server.Internal
// in each time it connects.
// Persist if SessionExpiryInterval != 0, but may start with a clean session
sessionShouldPersist = context.SessionExpiryInterval != 0;
sessionShouldPersist = context.SessionExpiryInterval.GetValueOrDefault() != 0;
}
else
{
@@ -540,6 +540,8 @@ namespace MQTTnet.Server.Internal
else
{
_logger.Verbose("Reusing existing session of client '{0}'.", connectPacket.ClientId);
// Session persistence could change for MQTT 5 clients that reconnect with different SessionExpiryInterval
session.IsPersistent = sessionShouldPersist;
connAckPacket.IsSessionPresent = true;
}
}