mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
74 lines
2.6 KiB
C#
74 lines
2.6 KiB
C#
using MQTTnet.Client;
|
|
using MQTTnet.Implementations;
|
|
using MQTTnet.Packets;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MQTTnet
|
|
{
|
|
public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs
|
|
{
|
|
public MqttApplicationMessageReceivedEventArgs(MqttClient client, MqttPublishPacket publishPacket, CancellationToken cancellationToken, string clientId, MqttApplicationMessage applicationMessage)
|
|
{
|
|
Client = client ?? throw new ArgumentNullException(nameof(client));
|
|
PublishPacket = publishPacket ?? throw new ArgumentNullException(nameof(publishPacket));
|
|
CancellationToken = cancellationToken;
|
|
acknowledged = 0;
|
|
ClientId = clientId;
|
|
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage));
|
|
AutoAcknowledge = true;
|
|
}
|
|
|
|
public MqttApplicationMessageReceivedEventArgs(string clientId, MqttApplicationMessage applicationMessage)
|
|
{
|
|
acknowledged = 1;
|
|
ClientId = clientId;
|
|
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage));
|
|
AutoAcknowledge = true;
|
|
}
|
|
|
|
internal MqttClient Client { get; }
|
|
|
|
internal CancellationToken CancellationToken { get; }
|
|
|
|
internal MqttPublishPacket PublishPacket { get; }
|
|
|
|
int acknowledged;
|
|
|
|
/// <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; }
|
|
|
|
public MqttApplicationMessageReceivedReasonCode ReasonCode { get; set; } = MqttApplicationMessageReceivedReasonCode.Success;
|
|
|
|
/// <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; }
|
|
|
|
public object Tag { get; set; }
|
|
|
|
public bool AutoAcknowledge { get; set; }
|
|
|
|
public Task Acknowledge()
|
|
{
|
|
if (Interlocked.CompareExchange(ref acknowledged, 1, 0) == 0)
|
|
{
|
|
return Client.AcknowledgeReceivedPublishPacket(this);
|
|
}
|
|
else
|
|
{
|
|
return PlatformAbstractionLayer.CompletedTask;
|
|
}
|
|
}
|
|
}
|
|
}
|