// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Text; using MQTTnet.Packets; using MQTTnet.Protocol; namespace MQTTnet.Server { public sealed class ClientDisconnectedEventArgs : EventArgs { readonly MqttConnectPacket _connectPacket; readonly MqttDisconnectPacket _disconnectPacket; public ClientDisconnectedEventArgs( MqttConnectPacket connectPacket, MqttDisconnectPacket disconnectPacket, MqttClientDisconnectType disconnectType, EndPoint remoteEndPoint, IDictionary sessionItems) { DisconnectType = disconnectType; RemoteEndPoint = remoteEndPoint; SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems)); _connectPacket = connectPacket; // The DISCONNECT packet can be null in case of a non clean disconnect or session takeover. _disconnectPacket = disconnectPacket; } /// /// Gets the client identifier. /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. /// public string ClientId => _connectPacket.ClientId; /// /// Gets the user name of the client. /// public string UserName => _connectPacket.Username; /// /// Gets the password of the client. /// public string Password => Encoding.UTF8.GetString(_connectPacket.Password.AsSpan()); public MqttClientDisconnectType DisconnectType { get; } public EndPoint RemoteEndPoint { get; } [Obsolete("Use RemoteEndPoint instead.")] public string Endpoint => RemoteEndPoint?.ToString(); /// /// Gets the reason code sent by the client. /// Only available for clean disconnects. /// MQTT 5.0.0+ feature. /// public MqttDisconnectReasonCode? ReasonCode => _disconnectPacket?.ReasonCode; /// /// Gets the reason string sent by the client. /// Only available for clean disconnects. /// MQTT 5.0.0+ feature. /// public string ReasonString => _disconnectPacket?.ReasonString; /// /// Gets the session expiry interval sent by the client. /// Only available for clean disconnects. /// MQTT 5.0.0+ feature. /// public uint SessionExpiryInterval => _disconnectPacket?.SessionExpiryInterval ?? 0; /// /// Gets or sets a key/value collection that can be used to share data within the scope of this session. /// public IDictionary SessionItems { get; } /// /// Gets the user properties sent by the client. /// Only available for clean disconnects. /// MQTT 5.0.0+ feature. /// public List UserProperties => _disconnectPacket?.UserProperties; } }