From 719838de3be0fa4b442df6d51813065a4a4d8e25 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 15 May 2018 22:04:05 +0200 Subject: [PATCH] Add new server option to disable/enable persistent sessions. --- .../Server/IMqttServerOptions.cs | 3 +++ .../Server/MqttClientSessionsManager.cs | 19 +++++++++++++++++++ .../Server/MqttServerOptions.cs | 3 ++- .../Server/MqttServerOptionsBuilder.cs | 6 ++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs b/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs index 3637319..54e037d 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs @@ -5,6 +5,9 @@ namespace MQTTnet.Server public interface IMqttServerOptions { int ConnectionBacklog { get; } + + bool EnablePersistentSessions { get; } + int MaxPendingMessagesPerClient { get; } MqttPendingMessagesOverflowStrategy PendingMessagesOverflowStrategy { get; } diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs index e50b8c2..267c8e8 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs @@ -107,6 +107,11 @@ namespace MQTTnet.Server _logger.Error(exception, exception.Message); } + if (!_options.EnablePersistentSessions) + { + await DeleteSessionAsync(clientId).ConfigureAwait(false); + } + Server.OnClientDisconnected(new ConnectedMqttClient { ClientId = clientId, @@ -224,6 +229,20 @@ namespace MQTTnet.Server return context.ReturnCode; } + private async Task DeleteSessionAsync(string clientId) + { + await _sessionsLock.EnterAsync(CancellationToken.None); + try + { + _sessions.Remove(clientId); + _logger.Verbose("Session for client '{0}' deleted.", clientId); + } + finally + { + _sessionsLock.Exit(); + } + } + private async Task PrepareClientSessionAsync(MqttConnectPacket connectPacket) { await _sessionsLock.EnterAsync(CancellationToken.None).ConfigureAwait(false); diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptions.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptions.cs index e6117fd..e848555 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptions.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptions.cs @@ -8,10 +8,11 @@ namespace MQTTnet.Server public MqttServerTlsEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsEndpointOptions(); + public bool EnablePersistentSessions { get; set; } + public int ConnectionBacklog { get; set; } = 10; public int MaxPendingMessagesPerClient { get; set; } = 250; - public MqttPendingMessagesOverflowStrategy PendingMessagesOverflowStrategy { get; set; } = MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage; public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(15); diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs index 2c86512..a8800d0 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs @@ -103,6 +103,12 @@ namespace MQTTnet.Server return this; } + public MqttServerOptionsBuilder WithPersistentSessions() + { + _options.EnablePersistentSessions = true; + return this; + } + public IMqttServerOptions Build() { return _options;