From f91d935708f43b486f36623c60579570f2b91a9a Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Mon, 11 Dec 2017 17:10:27 +0100 Subject: [PATCH] Add the ClientId to the application message interceptor. --- Build/MQTTnet.nuspec | 1 + .../Implementations/MqttServerAdapter.cs | 2 +- .../MqttApplicationMessageInterceptorContext.cs | 8 ++++++++ .../Server/MqttClientSessionsManager.cs | 15 +++++++-------- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index e7ed1cd6..d1074617 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -20,6 +20,7 @@ * [Client] Fixed an issue in _ManagedClient_ which prevents changing the QoS of an existing subscription (BREAKING CHANGE!). * [Client] Fixed an issue in _MqttClientOptionsBuilder_ which prevents adding TLS options to the client options when calling _Build()_. (Thanks to @cvellan). * [Client] Added an overload for subscribing at both clients which reduces required code. +* [Client] Added the _ClientId_ to the application message interceptor context. * [Server] The application message interceptor can now delete any received application message. * [Server] Added a ConnectionValidator context to align with other APIs (BREAKING CHANGE! Please find new example in Wiki). * [Server] Added an interface for the _MqttServerOptions_. diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs index a0441bcc..c991c23a 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs @@ -145,7 +145,7 @@ namespace MQTTnet.Implementations var sslStream = new SslStream(new NetworkStream(clientSocket)); await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false).ConfigureAwait(false); - + var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(clientSocket, sslStream), new MqttPacketSerializer(), _logger); ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter)); } diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttApplicationMessageInterceptorContext.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttApplicationMessageInterceptorContext.cs index e5f8c3f7..8074d527 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttApplicationMessageInterceptorContext.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttApplicationMessageInterceptorContext.cs @@ -2,6 +2,14 @@ { public class MqttApplicationMessageInterceptorContext { + public MqttApplicationMessageInterceptorContext(string clientId, MqttApplicationMessage applicationMessage) + { + ClientId = clientId; + ApplicationMessage = applicationMessage; + } + + public string ClientId { get; } + public MqttApplicationMessage ApplicationMessage { get; set; } } } diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs index 7b4d6d14..28dac0d7 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttClientSessionsManager.cs @@ -136,7 +136,7 @@ namespace MQTTnet.Server { try { - applicationMessage = InterceptApplicationMessage(applicationMessage); + applicationMessage = InterceptApplicationMessage(senderClientSession, applicationMessage); if (applicationMessage == null) { return; @@ -168,17 +168,16 @@ namespace MQTTnet.Server } } - private MqttApplicationMessage InterceptApplicationMessage(MqttApplicationMessage applicationMessage) + private MqttApplicationMessage InterceptApplicationMessage(MqttClientSession senderClientSession, MqttApplicationMessage applicationMessage) { if (_options.ApplicationMessageInterceptor == null) { return applicationMessage; } - var interceptorContext = new MqttApplicationMessageInterceptorContext - { - ApplicationMessage = applicationMessage - }; + var interceptorContext = new MqttApplicationMessageInterceptorContext( + senderClientSession.ClientId, + applicationMessage); _options.ApplicationMessageInterceptor(interceptorContext); return interceptorContext.ApplicationMessage; @@ -190,7 +189,7 @@ namespace MQTTnet.Server { return MqttConnectReturnCode.ConnectionAccepted; } - + var context = new MqttConnectionValidatorContext( connectPacket.ClientId, connectPacket.Username, @@ -210,7 +209,7 @@ namespace MQTTnet.Server if (isSessionPresent) { if (connectPacket.CleanSession) - { + { _sessions.Remove(connectPacket.ClientId); await clientSession.StopAsync().ConfigureAwait(false);