Squashed commit of the following:

commit 2d94abdde3
Author: Christian <christian.kratky@googlemail.com>
Date:   Sat Nov 13 10:09:59 2021 +0100

    Update MQTTnet.nuspec

    Update docs.

commit f0ea13c07f
Author: logicaloud <47704763+logicaloud@users.noreply.github.com>
Date:   Sat Nov 13 22:05:54 2021 +1300

    Fix MQTT 3.1.1 session persistence when CleanSession=1 (#1294)

commit 47cd5aa3cd
Author: Volker Milbrandt <milbrandt@users.noreply.github.com>
Date:   Sat Nov 13 10:02:55 2021 +0100

    test ResultCode for invalid credentials (no userName but password) (#1270)

    fix order of parameters in exceptions AreEqual(expected, actual)

commit 258df6bb14
Author: HansM <HansM2013@gmx.de>
Date:   Thu Nov 11 20:10:35 2021 +0100

    Removed IDisposable from MqttClientConnection class. (#1288)

commit 85c3c0b352
Author: SeppPenner <HansM2013@gmx.de>
Date:   Mon Nov 8 08:50:59 2021 +0100

    MQTTnet.noUWP.sln: Removed no longer present MQTTnet.Server project.
This commit is contained in:
Christian
2021-11-13 10:24:46 +01:00
parent 7993db2d82
commit b5b713570f
9 changed files with 106 additions and 48 deletions
+1 -6
View File
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@@ -96,11 +96,6 @@ namespace MQTTnet.Server
ChannelAdapter.ResetStatistics();
}
public void Dispose()
{
_cancellationToken.Dispose();
}
public async Task RunAsync()
{
_logger.Info("Client '{0}': Session started.", Id);
@@ -103,7 +103,7 @@ namespace MQTTnet.Server
connAckPacket = _connAckPacketFactory.Create(connectionValidatorContext);
// Pass connAckPacket so that IsSessionPresent flag can be set if the client session already exists
client = await CreateClientConnection(connectPacket, connAckPacket, channelAdapter, connectionValidatorContext.SessionItems).ConfigureAwait(false);
client = await CreateClientConnection(connectPacket, connAckPacket, channelAdapter, connectionValidatorContext).ConfigureAwait(false);
await client.SendPacketAsync(connAckPacket, cancellationToken).ConfigureAwait(false);
@@ -138,7 +138,7 @@ namespace MQTTnet.Server
_clients.Remove(client.Id);
}
if (!_options.EnablePersistentSessions)
if ((!_options.EnablePersistentSessions) || (!clientConnection.Session.IsPersistent))
{
await DeleteSessionAsync(client.Id).ConfigureAwait(false);
}
@@ -422,11 +422,36 @@ namespace MQTTnet.Server
async Task<MqttClient> CreateClientConnection(
MqttConnectPacket connectPacket,
MqttConnAckPacket connAckPacket,
IMqttChannelAdapter channelAdapter,
IDictionary<object, object> sessionItems)
IMqttChannelAdapter channelAdapter,
MqttConnectionValidatorContext context)
{
MqttClient 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))
{
MqttSession session;
@@ -434,14 +459,14 @@ namespace MQTTnet.Server
{
if (!_sessions.TryGetValue(connectPacket.ClientId, out session))
{
session = CreateSession(connectPacket.ClientId, sessionItems);
session = CreateSession(connectPacket.ClientId, sessionItems, 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
{
@@ -548,7 +573,7 @@ namespace MQTTnet.Server
_rootLogger);
}
MqttSession CreateSession(string clientId, IDictionary<object, object> sessionItems)
MqttClientSession CreateSession(string clientId, IDictionary<object, object> sessionItems, bool isPersistent)
{
_logger.Verbose("Created a new session for client '{0}'.", clientId);
+11 -3
View File
@@ -20,23 +20,31 @@ namespace MQTTnet.Server
readonly MqttClientSessionsManager _clientSessionsManager;
public MqttSession(string clientId,
bool isPersistent,
IDictionary<object, object> items,
MqttServerOptions serverOptions,
MqttServerEventContainer eventContainer,
MqttRetainedMessagesManager retainedMessagesManager,
MqttClientSessionsManager clientSessionsManager)
{
Id = clientId ?? throw new ArgumentNullException(nameof(clientId));
IsPersistent = isPersistent;
Items = items ?? throw new ArgumentNullException(nameof(items));
_serverOptions = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
_clientSessionsManager = clientSessionsManager ?? throw new ArgumentNullException(nameof(clientSessionsManager));
Id = clientId ?? throw new ArgumentNullException(nameof(clientId));
Items = items ?? throw new ArgumentNullException(nameof(items));
SubscriptionsManager = new MqttClientSubscriptionsManager(this, serverOptions, eventContainer, retainedMessagesManager);
}
public event EventHandler Deleted;
public string Id { get; }
/// <summary>
/// Session should persist if CleanSession was set to false (Mqtt3) or if SessionExpiryInterval != 0 (Mqtt5)
/// </summary>
public bool IsPersistent { get; }
public MqttPacketIdentifierProvider PacketIdentifierProvider { get; } = new MqttPacketIdentifierProvider();