Files
MQTTnet/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs
T
Christian 6bad9f183c Merge branch 'master' of https://github.com/chkr1011/MQTTnet into feature/Reason_Code_In_Subscription_Interceptor
# Conflicts:
#	Build/MQTTnet.nuspec
#	Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs
2021-10-29 21:58:57 +02:00

83 lines
3.1 KiB
C#

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<MqttApplicationMessageReceivedEventArgs, CancellationToken, Task> _acknowledgeHandler;
int _isAcknowledged;
public MqttApplicationMessageReceivedEventArgs(
string clientId,
MqttApplicationMessage applicationMessage,
MqttPublishPacket publishPacket,
Func<MqttApplicationMessageReceivedEventArgs, CancellationToken, Task> acknowledgeHandler)
{
ClientId = clientId;
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage));
PublishPacket = publishPacket;
_acknowledgeHandler = acknowledgeHandler;
}
internal MqttPublishPacket PublishPacket { get; set; }
/// <summary>
/// Gets the client identifier.
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
/// </summary>
public string ClientId { get; }
public MqttApplicationMessage ApplicationMessage { get; }
public bool ProcessingFailed { get; set; }
/// <summary>
/// Gets or sets the reason code which will be sent to the server.
/// </summary>
public MqttApplicationMessageReceivedReasonCode ReasonCode { get; set; } = MqttApplicationMessageReceivedReasonCode.Success;
/// <summary>
/// Gets or sets the user properties which will be sent to the server in the ACK packet etc.
/// </summary>
public List<MqttUserProperty> ResponseUserProperties { get; } = new List<MqttUserProperty>();
/// <summary>
/// Gets or sets the reason string which will be sent to the server in the ACK packet.
/// </summary>
public string ResponseReasonString { get; set; }
/// <summary>
/// Gets or sets whether this message was handled.
/// This value can be used in user code for custom control flow.
/// </summary>
public bool IsHandled { get; set; }
/// <summary>
/// Gets ir sets whether the library should send MQTT ACK packets automatically if required.
/// </summary>
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.");
}
}
}