|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using MQTTnet.Core.Adapter;
- using MQTTnet.Core.Diagnostics;
- using MQTTnet.Core.Exceptions;
- using MQTTnet.Core.Internal;
- using MQTTnet.Core.Packets;
- using MQTTnet.Core.Protocol;
-
- namespace MQTTnet.Core.Client
- {
- public class MqttClient : IMqttClient
- {
- private readonly HashSet<ushort> _unacknowledgedPublishPackets = new HashSet<ushort>();
-
- private readonly MqttPacketDispatcher _packetDispatcher = new MqttPacketDispatcher();
- private readonly MqttClientOptions _options;
- private readonly IMqttCommunicationAdapter _adapter;
-
- private bool _disconnectedEventSuspended;
- private int _latestPacketIdentifier;
- private CancellationTokenSource _cancellationTokenSource;
-
- public MqttClient(MqttClientOptions options, IMqttCommunicationAdapter adapter)
- {
- _options = options ?? throw new ArgumentNullException(nameof(options));
- _adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
-
- _adapter.PacketSerializer.ProtocolVersion = options.ProtocolVersion;
- }
-
- public event EventHandler Connected;
-
- public event EventHandler Disconnected;
-
- public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
-
- public bool IsConnected { get; private set; }
-
- public async Task ConnectAsync(MqttApplicationMessage willApplicationMessage = null)
- {
- MqttTrace.Verbose(nameof(MqttClient), "Trying to connect.");
-
- if (IsConnected)
- {
- throw new MqttProtocolViolationException("It is not allowed to connect with a server after the connection is established.");
- }
-
- try
- {
- _disconnectedEventSuspended = false;
-
- await _adapter.ConnectAsync(_options, _options.DefaultCommunicationTimeout).ConfigureAwait(false);
-
- MqttTrace.Verbose(nameof(MqttClient), "Connection with server established.");
-
- var connectPacket = new MqttConnectPacket
- {
- ClientId = _options.ClientId,
- Username = _options.UserName,
- Password = _options.Password,
- CleanSession = _options.CleanSession,
- KeepAlivePeriod = (ushort)_options.KeepAlivePeriod.TotalSeconds,
- WillMessage = willApplicationMessage
- };
-
- _cancellationTokenSource = new CancellationTokenSource();
- _latestPacketIdentifier = 0;
- _packetDispatcher.Reset();
-
- StartReceivePackets();
-
- var response = await SendAndReceiveAsync<MqttConnAckPacket>(connectPacket).ConfigureAwait(false);
- if (response.ConnectReturnCode != MqttConnectReturnCode.ConnectionAccepted)
- {
- await DisconnectInternalAsync().ConfigureAwait(false);
- throw new MqttConnectingFailedException(response.ConnectReturnCode);
- }
-
- if (_options.KeepAlivePeriod != TimeSpan.Zero)
- {
- StartSendKeepAliveMessages();
- }
-
- MqttTrace.Verbose(nameof(MqttClient), "MQTT connection with server established.");
-
- IsConnected = true;
- Connected?.Invoke(this, EventArgs.Empty);
- }
- catch (Exception)
- {
- await DisconnectInternalAsync().ConfigureAwait(false);
- throw;
- }
- }
-
- public async Task DisconnectAsync()
- {
- try
- {
- await SendAsync(new MqttDisconnectPacket()).ConfigureAwait(false);
- }
- finally
- {
- await DisconnectInternalAsync().ConfigureAwait(false);
- }
- }
-
- public Task<IList<MqttSubscribeResult>> SubscribeAsync(params TopicFilter[] topicFilters)
- {
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
-
- return SubscribeAsync(topicFilters.ToList());
- }
-
- public async Task<IList<MqttSubscribeResult>> SubscribeAsync(IList<TopicFilter> topicFilters)
- {
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
- if (!topicFilters.Any()) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.8.3-3].");
-
- ThrowIfNotConnected();
-
- var subscribePacket = new MqttSubscribePacket
- {
- PacketIdentifier = GetNewPacketIdentifier(),
- TopicFilters = topicFilters
- };
-
- var response = await SendAndReceiveAsync<MqttSubAckPacket>(subscribePacket);
-
- if (response.SubscribeReturnCodes.Count != topicFilters.Count)
- {
- throw new MqttProtocolViolationException("The return codes are not matching the topic filters [MQTT-3.9.3-1].");
- }
-
- return topicFilters.Select((t, i) => new MqttSubscribeResult(t, response.SubscribeReturnCodes[i])).ToList();
- }
-
- public Task Unsubscribe(params string[] topicFilters)
- {
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
-
- return Unsubscribe(topicFilters.ToList());
- }
-
- public async Task Unsubscribe(IList<string> topicFilters)
- {
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
- if (!topicFilters.Any()) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2].");
- ThrowIfNotConnected();
-
- var unsubscribePacket = new MqttUnsubscribePacket
- {
- PacketIdentifier = GetNewPacketIdentifier(),
- TopicFilters = topicFilters
- };
-
- await SendAndReceiveAsync<MqttUnsubAckPacket>(unsubscribePacket).ConfigureAwait(false);
- }
-
- public async Task PublishAsync(MqttApplicationMessage applicationMessage)
- {
- if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
- ThrowIfNotConnected();
-
- var publishPacket = applicationMessage.ToPublishPacket();
-
- if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce)
- {
- // No packet identifier is used for QoS 0 [3.3.2.2 Packet Identifier]
- await SendAsync(publishPacket).ConfigureAwait(false);
- }
- else if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce)
- {
- publishPacket.PacketIdentifier = GetNewPacketIdentifier();
- await SendAndReceiveAsync<MqttPubAckPacket>(publishPacket).ConfigureAwait(false);
- }
- else if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce)
- {
- publishPacket.PacketIdentifier = GetNewPacketIdentifier();
- var pubRecPacket = await SendAndReceiveAsync<MqttPubRecPacket>(publishPacket).ConfigureAwait(false);
- await SendAndReceiveAsync<MqttPubCompPacket>(pubRecPacket.CreateResponse<MqttPubRelPacket>()).ConfigureAwait(false);
- }
- }
-
- private void ThrowIfNotConnected()
- {
- if (!IsConnected) throw new MqttCommunicationException("The client is not connected.");
- }
-
- private async Task DisconnectInternalAsync()
- {
- try
- {
- await _adapter.DisconnectAsync().ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- MqttTrace.Warning(nameof(MqttClient), exception, "Error while disconnecting.");
- }
- finally
- {
- _cancellationTokenSource?.Cancel(false);
- _cancellationTokenSource?.Dispose();
- _cancellationTokenSource = null;
-
- IsConnected = false;
-
- if (!_disconnectedEventSuspended)
- {
- _disconnectedEventSuspended = true;
- Disconnected?.Invoke(this, EventArgs.Empty);
- }
- }
- }
-
- private Task ProcessReceivedPacketAsync(MqttBasePacket mqttPacket)
- {
- try
- {
- if (mqttPacket is MqttPingReqPacket)
- {
- return SendAsync(new MqttPingRespPacket());
- }
-
- if (mqttPacket is MqttDisconnectPacket)
- {
- return DisconnectAsync();
- }
-
- if (mqttPacket is MqttPublishPacket publishPacket)
- {
- return ProcessReceivedPublishPacket(publishPacket);
- }
-
- if (mqttPacket is MqttPubRelPacket pubRelPacket)
- {
- return ProcessReceivedPubRelPacket(pubRelPacket);
- }
-
- _packetDispatcher.Dispatch(mqttPacket);
- }
- catch (Exception exception)
- {
- MqttTrace.Error(nameof(MqttClient), exception, "Error while processing received packet.");
- }
-
- return Task.FromResult(0);
- }
-
- private void FireApplicationMessageReceivedEvent(MqttPublishPacket publishPacket)
- {
- var applicationMessage = publishPacket.ToApplicationMessage();
-
- try
- {
- ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(applicationMessage));
- }
- catch (Exception exception)
- {
- MqttTrace.Error(nameof(MqttClient), exception, "Unhandled exception while handling application message.");
- }
- }
-
- private Task ProcessReceivedPublishPacket(MqttPublishPacket publishPacket)
- {
- if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce)
- {
- FireApplicationMessageReceivedEvent(publishPacket);
- return Task.FromResult(0);
- }
-
- if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce)
- {
- FireApplicationMessageReceivedEvent(publishPacket);
- return SendAsync(new MqttPubAckPacket { PacketIdentifier = publishPacket.PacketIdentifier });
- }
-
- if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce)
- {
- // QoS 2 is implement as method "B" [4.3.3 QoS 2: Exactly once delivery]
- lock (_unacknowledgedPublishPackets)
- {
- _unacknowledgedPublishPackets.Add(publishPacket.PacketIdentifier);
- }
-
- FireApplicationMessageReceivedEvent(publishPacket);
- return SendAsync(new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier });
- }
-
- throw new MqttCommunicationException("Received a not supported QoS level.");
- }
-
- private async Task ProcessReceivedPubRelPacket(MqttPubRelPacket pubRelPacket)
- {
- lock (_unacknowledgedPublishPackets)
- {
- _unacknowledgedPublishPackets.Remove(pubRelPacket.PacketIdentifier);
- }
-
- await SendAsync(pubRelPacket.CreateResponse<MqttPubCompPacket>()).ConfigureAwait(false);
- }
-
- private Task SendAsync(MqttBasePacket packet)
- {
- return _adapter.SendPacketAsync(packet, _options.DefaultCommunicationTimeout);
- }
-
- private async Task<TResponsePacket> SendAndReceiveAsync<TResponsePacket>(MqttBasePacket requestPacket) where TResponsePacket : MqttBasePacket
- {
- bool ResponsePacketSelector(MqttBasePacket p)
- {
- if (!(p is TResponsePacket p1))
- {
- return false;
- }
-
- if (!(requestPacket is IMqttPacketWithIdentifier pi1) || !(p is IMqttPacketWithIdentifier pi2))
- {
- return true;
- }
-
- return pi1.PacketIdentifier == pi2.PacketIdentifier;
- }
-
- await _adapter.SendPacketAsync(requestPacket, _options.DefaultCommunicationTimeout).ConfigureAwait(false);
- return (TResponsePacket)await _packetDispatcher.WaitForPacketAsync(ResponsePacketSelector, _options.DefaultCommunicationTimeout).ConfigureAwait(false);
- }
-
- private ushort GetNewPacketIdentifier()
- {
- return (ushort)Interlocked.Increment(ref _latestPacketIdentifier);
- }
-
- private async Task SendKeepAliveMessagesAsync(CancellationToken cancellationToken)
- {
- MqttTrace.Information(nameof(MqttClient), "Start sending keep alive packets.");
-
- try
- {
- while (!cancellationToken.IsCancellationRequested)
- {
- await Task.Delay(_options.KeepAlivePeriod, cancellationToken).ConfigureAwait(false);
- await SendAndReceiveAsync<MqttPingRespPacket>(new MqttPingReqPacket()).ConfigureAwait(false);
- }
- }
- catch (MqttCommunicationException exception)
- {
- MqttTrace.Warning(nameof(MqttClient), exception, "MQTT communication error while receiving packets.");
- await DisconnectInternalAsync().ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- MqttTrace.Warning(nameof(MqttClient), exception, "Error while sending/receiving keep alive packets.");
- await DisconnectInternalAsync().ConfigureAwait(false);
- }
- finally
- {
- MqttTrace.Information(nameof(MqttClient), "Stopped sending keep alive packets.");
- }
- }
-
- private async Task ReceivePackets(CancellationToken cancellationToken)
- {
- MqttTrace.Information(nameof(MqttClient), "Start receiving packets.");
- try
- {
- while (!cancellationToken.IsCancellationRequested)
- {
- var packet = await _adapter.ReceivePacketAsync(TimeSpan.Zero).ConfigureAwait(false);
- MqttTrace.Information(nameof(MqttClient), $"Received <<< {packet}");
-
- StartProcessReceivedPacket(packet, cancellationToken);
- }
- }
- catch (MqttCommunicationException exception)
- {
- MqttTrace.Warning(nameof(MqttClient), exception, "MQTT communication exception while receiving packets.");
- await DisconnectInternalAsync().ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- MqttTrace.Error(nameof(MqttClient), exception, "Unhandled exception while receiving packets.");
- await DisconnectInternalAsync().ConfigureAwait(false);
- }
- finally
- {
- MqttTrace.Information(nameof(MqttClient), "Stopped receiving packets.");
- }
- }
-
- private void StartProcessReceivedPacket(MqttBasePacket packet, CancellationToken cancellationToken)
- {
- #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- Task.Run(() => ProcessReceivedPacketAsync(packet), cancellationToken);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- }
-
- private void StartReceivePackets()
- {
- #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- Task.Run(() => ReceivePackets(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- }
-
- private void StartSendKeepAliveMessages()
- {
- #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- Task.Run(() => SendKeepAliveMessagesAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- }
- }
- }
|