From 7e487469442631ccad35c56d4888059bc6abdffa Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 8 Nov 2017 00:49:49 +0100 Subject: [PATCH] Fix cleanup of client sessions --- .../Adapter/MqttChannelCommunicationAdapter.cs | 4 ++-- .../Server/MqttClientRetainedMessagesManager.cs | 1 - MQTTnet.Core/Server/MqttClientSession.cs | 10 ++-------- MQTTnet.Core/Server/MqttClientSessionsManager.cs | 4 ++-- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs b/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs index 04857b5..b31a870 100644 --- a/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs +++ b/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs @@ -14,10 +14,10 @@ namespace MQTTnet.Core.Adapter { public class MqttChannelCommunicationAdapter : IMqttCommunicationAdapter { + private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); private readonly ILogger _logger; private readonly IMqttCommunicationChannel _channel; - private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); - + public MqttChannelCommunicationAdapter(IMqttCommunicationChannel channel, IMqttPacketSerializer serializer, ILogger logger) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); diff --git a/MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs b/MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs index 2213360..d299daa 100644 --- a/MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs +++ b/MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs @@ -68,7 +68,6 @@ namespace MQTTnet.Core.Server try { - // ReSharper disable once UseNullPropagation if (_options.Storage != null) { await _options.Storage.SaveRetainedMessagesAsync(allRetainedMessages); diff --git a/MQTTnet.Core/Server/MqttClientSession.cs b/MQTTnet.Core/Server/MqttClientSession.cs index 00f906f..7ec312a 100644 --- a/MQTTnet.Core/Server/MqttClientSession.cs +++ b/MQTTnet.Core/Server/MqttClientSession.cs @@ -13,11 +13,11 @@ using Microsoft.Extensions.Options; namespace MQTTnet.Core.Server { - public sealed class MqttClientSession : IDisposable + public sealed class MqttClientSession { - private readonly IMqttClientRetainedMessageManager _clientRetainedMessageManager; private readonly HashSet _unacknowledgedPublishPackets = new HashSet(); + private readonly IMqttClientRetainedMessageManager _clientRetainedMessageManager; private readonly MqttClientSubscriptionsManager _subscriptionsManager; private readonly MqttClientSessionsManager _sessionsManager; private readonly MqttClientPendingMessagesQueue _pendingMessagesQueue; @@ -113,12 +113,6 @@ namespace MQTTnet.Core.Server _pendingMessagesQueue.Enqueue(publishPacket); } - public void Dispose() - { - _cancellationTokenSource?.Cancel(); - _cancellationTokenSource?.Dispose(); - } - private async Task ReceivePacketsAsync(IMqttCommunicationAdapter adapter, CancellationToken cancellationToken) { try diff --git a/MQTTnet.Core/Server/MqttClientSessionsManager.cs b/MQTTnet.Core/Server/MqttClientSessionsManager.cs index b2cbdc0..48fd33e 100644 --- a/MQTTnet.Core/Server/MqttClientSessionsManager.cs +++ b/MQTTnet.Core/Server/MqttClientSessionsManager.cs @@ -164,10 +164,10 @@ namespace MQTTnet.Core.Server if (connectPacket.CleanSession) { _clientSessions.Remove(connectPacket.ClientId); - clientSession.Dispose(); + clientSession.Stop(); clientSession = null; - _logger.LogTrace("Disposed existing session of client '{0}'.", connectPacket.ClientId); + _logger.LogTrace("Stopped existing session of client '{0}'.", connectPacket.ClientId); } else {