Add the ClientId to the application message interceptor.

This commit is contained in:
Christian Kratky
2017-12-11 17:10:27 +01:00
parent fb8b60fccc
commit f91d935708
4 changed files with 17 additions and 9 deletions
+1
View File
@@ -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 _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] 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 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] 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 a ConnectionValidator context to align with other APIs (BREAKING CHANGE! Please find new example in Wiki).
* [Server] Added an interface for the _MqttServerOptions_. * [Server] Added an interface for the _MqttServerOptions_.
@@ -145,7 +145,7 @@ namespace MQTTnet.Implementations
var sslStream = new SslStream(new NetworkStream(clientSocket)); var sslStream = new SslStream(new NetworkStream(clientSocket));
await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false).ConfigureAwait(false); await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false).ConfigureAwait(false);
var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(clientSocket, sslStream), new MqttPacketSerializer(), _logger); var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(clientSocket, sslStream), new MqttPacketSerializer(), _logger);
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter)); ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter));
} }
@@ -2,6 +2,14 @@
{ {
public class MqttApplicationMessageInterceptorContext public class MqttApplicationMessageInterceptorContext
{ {
public MqttApplicationMessageInterceptorContext(string clientId, MqttApplicationMessage applicationMessage)
{
ClientId = clientId;
ApplicationMessage = applicationMessage;
}
public string ClientId { get; }
public MqttApplicationMessage ApplicationMessage { get; set; } public MqttApplicationMessage ApplicationMessage { get; set; }
} }
} }
@@ -136,7 +136,7 @@ namespace MQTTnet.Server
{ {
try try
{ {
applicationMessage = InterceptApplicationMessage(applicationMessage); applicationMessage = InterceptApplicationMessage(senderClientSession, applicationMessage);
if (applicationMessage == null) if (applicationMessage == null)
{ {
return; return;
@@ -168,17 +168,16 @@ namespace MQTTnet.Server
} }
} }
private MqttApplicationMessage InterceptApplicationMessage(MqttApplicationMessage applicationMessage) private MqttApplicationMessage InterceptApplicationMessage(MqttClientSession senderClientSession, MqttApplicationMessage applicationMessage)
{ {
if (_options.ApplicationMessageInterceptor == null) if (_options.ApplicationMessageInterceptor == null)
{ {
return applicationMessage; return applicationMessage;
} }
var interceptorContext = new MqttApplicationMessageInterceptorContext var interceptorContext = new MqttApplicationMessageInterceptorContext(
{ senderClientSession.ClientId,
ApplicationMessage = applicationMessage applicationMessage);
};
_options.ApplicationMessageInterceptor(interceptorContext); _options.ApplicationMessageInterceptor(interceptorContext);
return interceptorContext.ApplicationMessage; return interceptorContext.ApplicationMessage;
@@ -190,7 +189,7 @@ namespace MQTTnet.Server
{ {
return MqttConnectReturnCode.ConnectionAccepted; return MqttConnectReturnCode.ConnectionAccepted;
} }
var context = new MqttConnectionValidatorContext( var context = new MqttConnectionValidatorContext(
connectPacket.ClientId, connectPacket.ClientId,
connectPacket.Username, connectPacket.Username,
@@ -210,7 +209,7 @@ namespace MQTTnet.Server
if (isSessionPresent) if (isSessionPresent)
{ {
if (connectPacket.CleanSession) if (connectPacket.CleanSession)
{ {
_sessions.Remove(connectPacket.ClientId); _sessions.Remove(connectPacket.ClientId);
await clientSession.StopAsync().ConfigureAwait(false); await clientSession.StopAsync().ConfigureAwait(false);