From 322067466be41c8b904b80086a7c87241b0b40af Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 25 Nov 2020 21:31:26 +0100 Subject: [PATCH] Fix wrong usage of client session items for undelivered messages. --- Build/MQTTnet.nuspec | 1 + ...MqttApplicationMessageReceivedEventArgs.cs | 2 +- .../Server/MqttClientSessionsManager.cs | 21 ++++++++++--------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index e2bfb867..afcf1e8d 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -22,6 +22,7 @@ * [Server] Session takeover and keep alive timeout are now properly set in DISCONNECT packet. * [Server] Fixed bug in PubRel packet generation (MQTTv5 only). * [Server] Improved message processing performance (+ ~5%). +* [Server] Fix wrong usage of client session items for undelivered messages. Copyright Christian Kratky 2016-2020 MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor diff --git a/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs b/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs index 6fbaa933..ed46305c 100644 --- a/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs +++ b/Source/MQTTnet/MqttApplicationMessageReceivedEventArgs.cs @@ -2,7 +2,7 @@ namespace MQTTnet { - public class MqttApplicationMessageReceivedEventArgs : EventArgs + public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs { public MqttApplicationMessageReceivedEventArgs(string clientId, MqttApplicationMessage applicationMessage) { diff --git a/Source/MQTTnet/Server/MqttClientSessionsManager.cs b/Source/MQTTnet/Server/MqttClientSessionsManager.cs index 0dc95190..18b529e4 100644 --- a/Source/MQTTnet/Server/MqttClientSessionsManager.cs +++ b/Source/MQTTnet/Server/MqttClientSessionsManager.cs @@ -287,21 +287,21 @@ namespace MQTTnet.Server return; } - var sender = queuedApplicationMessage.Sender; - var senderClientId = sender?.ClientId ?? _options.ClientId; + var clientConnection = queuedApplicationMessage.Sender; + var senderClientId = clientConnection?.ClientId ?? _options.ClientId; var applicationMessage = queuedApplicationMessage.ApplicationMessage; var interceptor = _options.ApplicationMessageInterceptor; if (interceptor != null) { - var interceptorContext = await InterceptApplicationMessageAsync(interceptor, sender, applicationMessage).ConfigureAwait(false); + var interceptorContext = await InterceptApplicationMessageAsync(interceptor, clientConnection, applicationMessage).ConfigureAwait(false); if (interceptorContext != null) { if (interceptorContext.CloseConnection) { - if (sender != null) + if (clientConnection != null) { - await sender.StopAsync(MqttDisconnectReasonCode.NormalDisconnection).ConfigureAwait(false); + await clientConnection.StopAsync(MqttDisconnectReasonCode.NormalDisconnection).ConfigureAwait(false); } } @@ -343,7 +343,8 @@ namespace MQTTnet.Server return; } - await undeliveredMessageInterceptor.InterceptApplicationMessagePublishAsync(new MqttApplicationMessageInterceptorContext(senderClientId, sender?.Session?.Items, applicationMessage)); + // The delegate signature is the same as for regular message interceptor. So the call is fine and just uses a different interceptor. + await InterceptApplicationMessageAsync(undeliveredMessageInterceptor, clientConnection, applicationMessage).ConfigureAwait(false); } } catch (OperationCanceledException) @@ -427,12 +428,12 @@ namespace MQTTnet.Server } } - async Task InterceptApplicationMessageAsync(IMqttServerApplicationMessageInterceptor interceptor, MqttClientConnection senderConnection, MqttApplicationMessage applicationMessage) + async Task InterceptApplicationMessageAsync(IMqttServerApplicationMessageInterceptor interceptor, MqttClientConnection clientConnection, MqttApplicationMessage applicationMessage) { string senderClientId; IDictionary sessionItems; - var messageIsFromServer = senderConnection == null; + var messageIsFromServer = clientConnection == null; if (messageIsFromServer) { senderClientId = _options.ClientId; @@ -440,8 +441,8 @@ namespace MQTTnet.Server } else { - senderClientId = senderConnection.ClientId; - sessionItems = senderConnection.Session.Items; + senderClientId = clientConnection.ClientId; + sessionItems = clientConnection.Session.Items; } var interceptorContext = new MqttApplicationMessageInterceptorContext(senderClientId, sessionItems, applicationMessage);