using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using MQTTnet.Packets; namespace MQTTnet.Client { public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs { readonly Func _acknowledgeHandler; int _isAcknowledged; public MqttApplicationMessageReceivedEventArgs( string clientId, MqttApplicationMessage applicationMessage, MqttPublishPacket publishPacket, Func acknowledgeHandler) { ClientId = clientId; ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage)); PublishPacket = publishPacket; _acknowledgeHandler = acknowledgeHandler; } internal MqttPublishPacket PublishPacket { get; set; } /// /// 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 { get; } public MqttApplicationMessage ApplicationMessage { get; } public bool ProcessingFailed { get; set; } /// /// Gets or sets the reason code which will be sent to the server. /// public MqttApplicationMessageReceivedReasonCode ReasonCode { get; set; } = MqttApplicationMessageReceivedReasonCode.Success; /// /// Gets or sets the user properties which will be sent to the server in the ACK packet etc. /// public List ResponseUserProperties { get; } = new List(); /// /// Gets or sets the reason string which will be sent to the server in the ACK packet. /// public string ResponseReasonString { get; set; } /// /// Gets or sets whether this message was handled. /// This value can be used in user code for custom control flow. /// public bool IsHandled { get; set; } /// /// Gets ir sets whether the library should send MQTT ACK packets automatically if required. /// public bool AutoAcknowledge { get; set; } = true; public object Tag { get; set; } public Task AcknowledgeAsync(CancellationToken cancellationToken) { if (_acknowledgeHandler == null) { throw new NotSupportedException("Deferred acknowledgement of application message is not yet supported in MQTTnet server."); } if (Interlocked.CompareExchange(ref _isAcknowledged, 1, 0) == 0) { return _acknowledgeHandler(this, cancellationToken); } throw new InvalidOperationException("The application message is already acknowledged."); } } }