Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Source/MQTTnet.Server/Internal/MqttClientSessionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public sealed class MqttClientSessionsManager : ISubscriptionChangedNotification
// See the MqttSubscription object for a detailed explanation.
readonly MqttSessionsStorage _sessionsStorage = new();
readonly HashSet<MqttSession> _subscriberSessions = [];
readonly MqttWillMessagesManager _willMessages;

public MqttClientSessionsManager(MqttServerOptions options, MqttRetainedMessagesManager retainedMessagesManager, MqttServerEventContainer eventContainer, IMqttNetLogger logger)
{
Expand All @@ -42,8 +43,15 @@ public MqttClientSessionsManager(MqttServerOptions options, MqttRetainedMessages
_options = options ?? throw new ArgumentNullException(nameof(options));
_retainedMessagesManager = retainedMessagesManager ?? throw new ArgumentNullException(nameof(retainedMessagesManager));
_eventContainer = eventContainer ?? throw new ArgumentNullException(nameof(eventContainer));
_willMessages = new MqttWillMessagesManager(this, logger);
}

internal void EndWillSession(MqttSession session) => _willMessages.SessionEnded(session);

internal Task StartWillsAsync() => _willMessages.StartAsync();

internal Task StopWillsAsync(bool endSessions) => _willMessages.StopAsync(endSessions);

public async Task CloseAllConnections(MqttServerClientDisconnectOptions options)
{
ArgumentNullException.ThrowIfNull(options);
Expand Down Expand Up @@ -248,6 +256,8 @@ public async Task<DispatchApplicationMessageResult> DispatchApplicationMessage(

public void Dispose()
{
CloseAllConnections(new MqttServerClientDisconnectOptions()).GetAwaiter().GetResult();
_willMessages.DisposeAsync().AsTask().GetAwaiter().GetResult();
_createConnectionSyncRoot.Dispose();

_sessionsManagementLock.EnterWriteLock();
Expand Down Expand Up @@ -399,6 +409,7 @@ public async Task HandleClientConnectionAsync(IMqttChannelAdapter channelAdapter
{
if (connectedClient != null)
{
_willMessages.Disconnected(connectedClient);
if (connectedClient.Id != null)
{
// in case it is a takeover _clientConnections already contains the new connection
Expand Down Expand Up @@ -593,6 +604,7 @@ async Task<MqttConnectedClient> CreateClientConnection(

connectedClient = CreateClient(connectPacket, channelAdapter, session);
_clients[connectPacket.ClientId] = connectedClient;
_willMessages.Connected(connectedClient);
}
}
finally
Expand Down
15 changes: 1 addition & 14 deletions Source/MQTTnet.Server/Internal/MqttConnectedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,6 @@ public async Task RunAsync()
_cancellationToken = null;
}

var isCleanDisconnect = DisconnectPacket != null;

if (!IsTakenOver && !isCleanDisconnect && Session.LatestConnectPacket.WillFlag && !Session.WillMessageSent)
{
var willPublishPacket = MqttPublishPacketFactory.Create(Session.LatestConnectPacket);
var willApplicationMessage = MqttApplicationMessageFactory.Create(willPublishPacket);

_ = _sessionsManager.DispatchApplicationMessage(Id, UserName, Session.Items, willApplicationMessage, CancellationToken.None);
Session.WillMessageSent = true;

_logger.Info("Client '{0}': Published will message", Id);
}

_logger.Info("Client '{0}': Connection stopped", Id);
}

Expand Down Expand Up @@ -581,4 +568,4 @@ async Task TrySendDisconnectPacket(MqttServerClientDisconnectOptions options)
_logger.Warning(exception, "Client '{0}': Error while sending DISCONNECT packet (ReasonCode = {1})", Id, options.ReasonCode);
}
}
}
}
1 change: 1 addition & 0 deletions Source/MQTTnet.Server/Internal/MqttSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public Task<MqttPacketBusItem> DequeuePacketAsync(CancellationToken cancellation

public void Dispose()
{
_clientSessionsManager.EndWillSession(this);
_packetBus.Dispose();
_subscriptionsManager.Dispose();
}
Expand Down
Loading
Loading