From ab5264c8ca5832a2462c5044a4a7580244184240 Mon Sep 17 00:00:00 2001
From: Christian <6939810+chkr1011@users.noreply.github.com>
Date: Mon, 29 May 2023 13:45:44 +0200
Subject: [PATCH] 1738 userproperties in clientconnectedeventargs (#1747)
---
.github/workflows/ReleaseNotes.md | 1 +
.../Server/Events/ClientConnectedEventArgs.cs | 23 +++++++++++++++----
.../Internal/MqttClientSessionsManager.cs | 15 ++++++++----
3 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md
index 835f5477..f548733b 100644
--- a/.github/workflows/ReleaseNotes.md
+++ b/.github/workflows/ReleaseNotes.md
@@ -7,3 +7,4 @@
* [Client] Exposed more TLS options (#1729).
* [Client] Fixed wrong return code conversion (#1729).
* [Server] Improved performance by changing internal locking strategy for subscriptions (#1716, thanks to @zeheng).
+* [Server] Exposed more properties in _ClientConnectedEventArgs_ (#1738).
diff --git a/Source/MQTTnet/Server/Events/ClientConnectedEventArgs.cs b/Source/MQTTnet/Server/Events/ClientConnectedEventArgs.cs
index 39042738..216f96ef 100644
--- a/Source/MQTTnet/Server/Events/ClientConnectedEventArgs.cs
+++ b/Source/MQTTnet/Server/Events/ClientConnectedEventArgs.cs
@@ -4,26 +4,33 @@
using System;
using System.Collections;
+using System.Collections.Generic;
using MQTTnet.Formatter;
+using MQTTnet.Packets;
namespace MQTTnet.Server
{
public sealed class ClientConnectedEventArgs : EventArgs
{
- public ClientConnectedEventArgs(string clientId, string userName, MqttProtocolVersion protocolVersion, string endpoint, IDictionary sessionItems)
+ readonly MqttConnectPacket _connectPacket;
+
+ public ClientConnectedEventArgs(MqttConnectPacket connectPacket, MqttProtocolVersion protocolVersion, string endpoint, IDictionary sessionItems)
{
- ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
- UserName = userName;
+ _connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket));
ProtocolVersion = protocolVersion;
Endpoint = endpoint;
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
}
+ public byte[] AuthenticationData => _connectPacket.AuthenticationData;
+
+ public string AuthenticationMethod => _connectPacket.AuthenticationMethod;
+
///
/// Gets the client identifier of the connected client.
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
///
- public string ClientId { get; }
+ public string ClientId => _connectPacket.ClientId;
///
/// Gets the endpoint of the connected client.
@@ -43,6 +50,12 @@ namespace MQTTnet.Server
///
/// Gets the user name of the connected client.
///
- public string UserName { get; }
+ public string UserName => _connectPacket.Username;
+
+ ///
+ /// Gets the user properties sent by the client.
+ /// MQTT 5.0.0+ feature.
+ ///
+ public List UserProperties => _connectPacket?.UserProperties;
}
}
\ No newline at end of file
diff --git a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs
index 4053e438..0c16fd4b 100644
--- a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs
+++ b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs
@@ -377,9 +377,7 @@ namespace MQTTnet.Server
if (_eventContainer.ClientConnectedEvent.HasHandlers)
{
- var eventArgs = new ClientConnectedEventArgs(
- connectPacket.ClientId,
- connectPacket.Username,
+ var eventArgs = new ClientConnectedEventArgs(connectPacket,
channelAdapter.PacketFormatterAdapter.ProtocolVersion,
channelAdapter.Endpoint,
client.Session.Items);
@@ -614,7 +612,11 @@ namespace MQTTnet.Server
_sessions[connectPacket.ClientId] = session;
// Create a new client (always required).
- _clients.TryGetValue(connectPacket.ClientId, out oldClient);
+ lock (_clients)
+ {
+ _clients.TryGetValue(connectPacket.ClientId, out oldClient);
+ }
+
if (oldClient != null)
{
// This will stop the current client from sending and receiving but remains the connection active
@@ -624,7 +626,10 @@ namespace MQTTnet.Server
client = CreateClient(connectPacket, channelAdapter, session);
- _clients[connectPacket.ClientId] = client;
+ lock (_clients)
+ {
+ _clients[connectPacket.ClientId] = client;
+ }
}
finally
{