This commit is contained in:
Christian
2023-05-29 13:47:09 +02:00
3 changed files with 29 additions and 10 deletions
+1
View File
@@ -8,3 +8,4 @@
* [Client] Exposed more TLS options (#1729). * [Client] Exposed more TLS options (#1729).
* [Client] Fixed wrong return code conversion (#1729). * [Client] Fixed wrong return code conversion (#1729).
* [Server] Improved performance by changing internal locking strategy for subscriptions (#1716, thanks to @zeheng). * [Server] Improved performance by changing internal locking strategy for subscriptions (#1716, thanks to @zeheng).
* [Server] Exposed more properties in _ClientConnectedEventArgs_ (#1738).
@@ -4,26 +4,33 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using MQTTnet.Formatter; using MQTTnet.Formatter;
using MQTTnet.Packets;
namespace MQTTnet.Server namespace MQTTnet.Server
{ {
public sealed class ClientConnectedEventArgs : EventArgs 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)); _connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket));
UserName = userName;
ProtocolVersion = protocolVersion; ProtocolVersion = protocolVersion;
Endpoint = endpoint; Endpoint = endpoint;
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems)); SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
} }
public byte[] AuthenticationData => _connectPacket.AuthenticationData;
public string AuthenticationMethod => _connectPacket.AuthenticationMethod;
/// <summary> /// <summary>
/// Gets the client identifier of the connected client. /// 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. /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
/// </summary> /// </summary>
public string ClientId { get; } public string ClientId => _connectPacket.ClientId;
/// <summary> /// <summary>
/// Gets the endpoint of the connected client. /// Gets the endpoint of the connected client.
@@ -43,6 +50,12 @@ namespace MQTTnet.Server
/// <summary> /// <summary>
/// Gets the user name of the connected client. /// Gets the user name of the connected client.
/// </summary> /// </summary>
public string UserName { get; } public string UserName => _connectPacket.Username;
/// <summary>
/// Gets the user properties sent by the client.
/// <remarks>MQTT 5.0.0+ feature.</remarks>
/// </summary>
public List<MqttUserProperty> UserProperties => _connectPacket?.UserProperties;
} }
} }
@@ -377,9 +377,7 @@ namespace MQTTnet.Server
if (_eventContainer.ClientConnectedEvent.HasHandlers) if (_eventContainer.ClientConnectedEvent.HasHandlers)
{ {
var eventArgs = new ClientConnectedEventArgs( var eventArgs = new ClientConnectedEventArgs(connectPacket,
connectPacket.ClientId,
connectPacket.Username,
channelAdapter.PacketFormatterAdapter.ProtocolVersion, channelAdapter.PacketFormatterAdapter.ProtocolVersion,
channelAdapter.Endpoint, channelAdapter.Endpoint,
client.Session.Items); client.Session.Items);
@@ -614,7 +612,11 @@ namespace MQTTnet.Server
_sessions[connectPacket.ClientId] = session; _sessions[connectPacket.ClientId] = session;
// Create a new client (always required). // Create a new client (always required).
_clients.TryGetValue(connectPacket.ClientId, out oldClient); lock (_clients)
{
_clients.TryGetValue(connectPacket.ClientId, out oldClient);
}
if (oldClient != null) if (oldClient != null)
{ {
// This will stop the current client from sending and receiving but remains the connection active // 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); client = CreateClient(connectPacket, channelAdapter, session);
_clients[connectPacket.ClientId] = client; lock (_clients)
{
_clients[connectPacket.ClientId] = client;
}
} }
finally finally
{ {