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 _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_.
@@ -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));
}
@@ -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; }
}
}
@@ -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);