|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using MQTTnet.Adapter;
- using MQTTnet.Diagnostics;
- using MQTTnet.Exceptions;
- using MQTTnet.Internal;
- using MQTTnet.Packets;
- using MQTTnet.Protocol;
-
- namespace MQTTnet.Client
- {
- public class MqttClient : IMqttClient
- {
- private readonly IMqttClientAdapterFactory _adapterFactory;
- private readonly MqttPacketDispatcher _packetDispatcher;
- private readonly IMqttNetLogger _logger;
-
- private IMqttClientOptions _options;
- private bool _isReceivingPackets;
- private int _latestPacketIdentifier;
- private CancellationTokenSource _cancellationTokenSource;
- private IMqttChannelAdapter _adapter;
-
- public MqttClient(IMqttClientAdapterFactory channelFactory, IMqttNetLogger logger)
- {
- _adapterFactory = channelFactory ?? throw new ArgumentNullException(nameof(channelFactory));
- _logger = logger ?? throw new ArgumentNullException(nameof(logger));
-
- _packetDispatcher = new MqttPacketDispatcher(logger);
- }
-
- public event EventHandler<MqttClientConnectedEventArgs> Connected;
- public event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
- public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
-
- public bool IsConnected { get; private set; }
-
- public async Task<MqttClientConnectResult> ConnectAsync(IMqttClientOptions options)
- {
- if (options == null) throw new ArgumentNullException(nameof(options));
- if (options.ChannelOptions == null) throw new ArgumentException("ChannelOptions are not set.");
-
- ThrowIfConnected("It is not allowed to connect with a server after the connection is established.");
-
- try
- {
- _options = options;
- _cancellationTokenSource = new CancellationTokenSource();
- _latestPacketIdentifier = 0;
- _packetDispatcher.Reset();
-
- _adapter = _adapterFactory.CreateClientAdapter(options.ChannelOptions, _logger);
-
- _logger.Trace<MqttClient>("Trying to connect with server.");
- await _adapter.ConnectAsync(_options.CommunicationTimeout).ConfigureAwait(false);
- _logger.Trace<MqttClient>("Connection with server established.");
-
- await StartReceivingPacketsAsync(_cancellationTokenSource.Token).ConfigureAwait(false);
- var connectResponse = await AuthenticateAsync(options.WillMessage).ConfigureAwait(false);
-
- _logger.Trace<MqttClient>("MQTT connection with server established.");
-
- if (_options.KeepAlivePeriod != TimeSpan.Zero)
- {
- StartSendingKeepAliveMessages(_cancellationTokenSource.Token);
- }
-
- IsConnected = true;
- Connected?.Invoke(this, new MqttClientConnectedEventArgs(connectResponse.IsSessionPresent));
- return new MqttClientConnectResult(connectResponse.IsSessionPresent);
- }
- catch (Exception exception)
- {
- _logger.Error<MqttClient>(exception, "Error while connecting with server.");
- await DisconnectInternalAsync(exception).ConfigureAwait(false);
-
- throw;
- }
- }
-
- public async Task DisconnectAsync()
- {
- if (!IsConnected)
- {
- return;
- }
-
- try
- {
- await SendAsync(new MqttDisconnectPacket()).ConfigureAwait(false);
- }
- finally
- {
- await DisconnectInternalAsync(null).ConfigureAwait(false);
- }
- }
-
- public async Task<IList<MqttSubscribeResult>> SubscribeAsync(IEnumerable<TopicFilter> topicFilters)
- {
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
-
- ThrowIfNotConnected();
-
- var subscribePacket = new MqttSubscribePacket
- {
- PacketIdentifier = GetNewPacketIdentifier(),
- TopicFilters = topicFilters.ToList()
- };
-
- var response = await SendAndReceiveAsync<MqttSubAckPacket>(subscribePacket).ConfigureAwait(false);
-
- if (response.SubscribeReturnCodes.Count != subscribePacket.TopicFilters.Count)
- {
- throw new MqttProtocolViolationException("The return codes are not matching the topic filters [MQTT-3.9.3-1].");
- }
-
- return subscribePacket.TopicFilters.Select((t, i) => new MqttSubscribeResult(t, response.SubscribeReturnCodes[i])).ToList();
- }
-
- public async Task UnsubscribeAsync(IEnumerable<string> topicFilters)
- {
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
-
- ThrowIfNotConnected();
-
- var unsubscribePacket = new MqttUnsubscribePacket
- {
- PacketIdentifier = GetNewPacketIdentifier(),
- TopicFilters = topicFilters.ToList()
- };
-
- await SendAndReceiveAsync<MqttUnsubAckPacket>(unsubscribePacket).ConfigureAwait(false);
- }
-
- public async Task PublishAsync(IEnumerable<MqttApplicationMessage> applicationMessages)
- {
- ThrowIfNotConnected();
-
- var publishPackets = applicationMessages.Select(m => m.ToPublishPacket());
- var packetGroups = publishPackets.GroupBy(p => p.QualityOfServiceLevel).OrderBy(g => g.Key);
-
- foreach (var qosGroup in packetGroups)
- {
- switch (qosGroup.Key)
- {
- case MqttQualityOfServiceLevel.AtMostOnce:
- {
- // No packet identifier is used for QoS 0 [3.3.2.2 Packet Identifier]
- await _adapter.SendPacketsAsync(_options.CommunicationTimeout, _cancellationTokenSource.Token, qosGroup).ConfigureAwait(false);
- break;
- }
- case MqttQualityOfServiceLevel.AtLeastOnce:
- {
- foreach (var publishPacket in qosGroup)
- {
- publishPacket.PacketIdentifier = GetNewPacketIdentifier();
- await SendAndReceiveAsync<MqttPubAckPacket>(publishPacket).ConfigureAwait(false);
- }
-
- break;
- }
- case MqttQualityOfServiceLevel.ExactlyOnce:
- {
- foreach (var publishPacket in qosGroup)
- {
- publishPacket.PacketIdentifier = GetNewPacketIdentifier();
- var pubRecPacket = await SendAndReceiveAsync<MqttPubRecPacket>(publishPacket).ConfigureAwait(false);
- await SendAndReceiveAsync<MqttPubCompPacket>(pubRecPacket.CreateResponse<MqttPubRelPacket>()).ConfigureAwait(false);
- }
-
- break;
- }
- default:
- {
- throw new InvalidOperationException();
- }
- }
- }
- }
-
- public void Dispose()
- {
- _cancellationTokenSource?.Dispose();
- }
-
- private async Task<MqttConnAckPacket> AuthenticateAsync(MqttApplicationMessage willApplicationMessage)
- {
- var connectPacket = new MqttConnectPacket
- {
- ClientId = _options.ClientId,
- Username = _options.Credentials?.Username,
- Password = _options.Credentials?.Password,
- CleanSession = _options.CleanSession,
- KeepAlivePeriod = (ushort)_options.KeepAlivePeriod.TotalSeconds,
- WillMessage = willApplicationMessage
- };
-
- var response = await SendAndReceiveAsync<MqttConnAckPacket>(connectPacket).ConfigureAwait(false);
- if (response.ConnectReturnCode != MqttConnectReturnCode.ConnectionAccepted)
- {
- throw new MqttConnectingFailedException(response.ConnectReturnCode);
- }
-
- return response;
- }
-
- private void ThrowIfNotConnected()
- {
- if (!IsConnected) throw new MqttCommunicationException("The client is not connected.");
- }
-
- private void ThrowIfConnected(string message)
- {
- if (IsConnected) throw new MqttProtocolViolationException(message);
- }
-
- private async Task DisconnectInternalAsync(Exception exception)
- {
- var clientWasConnected = IsConnected;
- IsConnected = false;
-
- var cts = _cancellationTokenSource;
- if (cts == null || cts.IsCancellationRequested)
- {
- return;
- }
-
- cts.Cancel(false);
- cts.Dispose();
- _cancellationTokenSource = null;
-
- try
- {
- await _adapter.DisconnectAsync(_options.CommunicationTimeout).ConfigureAwait(false);
- _logger.Info<MqttClient>("Disconnected from adapter.");
- }
- catch (Exception adapterException)
- {
- _logger.Warning<MqttClient>(adapterException, "Error while disconnecting from adapter.");
- }
- finally
- {
- _logger.Info<MqttClient>("Disconnected.");
- Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected, exception));
- }
- }
-
- private async Task ProcessReceivedPacketAsync(MqttBasePacket packet)
- {
- try
- {
- _logger.Info<MqttClient>("Received <<< {0}", packet);
-
- if (packet is MqttPublishPacket publishPacket)
- {
- await ProcessReceivedPublishPacketAsync(publishPacket).ConfigureAwait(false);
- return;
- }
-
- if (packet is MqttPingReqPacket)
- {
- await SendAsync(new MqttPingRespPacket()).ConfigureAwait(false);
- return;
- }
-
- if (packet is MqttDisconnectPacket)
- {
- await DisconnectAsync().ConfigureAwait(false);
- return;
- }
-
- if (packet is MqttPubRelPacket pubRelPacket)
- {
- await ProcessReceivedPubRelPacket(pubRelPacket).ConfigureAwait(false);
- return;
- }
-
- _packetDispatcher.Dispatch(packet);
- }
- catch (Exception exception)
- {
- _logger.Error<MqttClient>(exception, "Unhandled exception while processing received packet.");
- }
- }
-
- private void FireApplicationMessageReceivedEvent(MqttPublishPacket publishPacket)
- {
- try
- {
- var applicationMessage = publishPacket.ToApplicationMessage();
- ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(_options.ClientId, applicationMessage));
- }
- catch (Exception exception)
- {
- _logger.Error<MqttClient>(exception, "Unhandled exception while handling application message.");
- }
- }
-
- private Task ProcessReceivedPublishPacketAsync(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]
- FireApplicationMessageReceivedEvent(publishPacket);
- return SendAsync(new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier });
- }
-
- throw new MqttCommunicationException("Received a not supported QoS level.");
- }
-
- private Task ProcessReceivedPubRelPacket(MqttPubRelPacket pubRelPacket)
- {
- return SendAsync(pubRelPacket.CreateResponse<MqttPubCompPacket>());
- }
-
- private Task SendAsync(MqttBasePacket packet)
- {
- return _adapter.SendPacketsAsync(_options.CommunicationTimeout, _cancellationTokenSource.Token, packet);
- }
-
- private async Task<TResponsePacket> SendAndReceiveAsync<TResponsePacket>(MqttBasePacket requestPacket) where TResponsePacket : MqttBasePacket
- {
- var packetAwaiter = _packetDispatcher.WaitForPacketAsync(requestPacket, typeof(TResponsePacket), _options.CommunicationTimeout);
- await _adapter.SendPacketsAsync(_options.CommunicationTimeout, _cancellationTokenSource.Token, requestPacket).ConfigureAwait(false);
- return (TResponsePacket)await packetAwaiter.ConfigureAwait(false);
- }
-
- private ushort GetNewPacketIdentifier()
- {
- return (ushort)Interlocked.Increment(ref _latestPacketIdentifier);
- }
-
- private async Task SendKeepAliveMessagesAsync(CancellationToken cancellationToken)
- {
- _logger.Info<MqttClient>("Start sending keep alive packets.");
-
- try
- {
- while (!cancellationToken.IsCancellationRequested)
- {
- await SendAndReceiveAsync<MqttPingRespPacket>(new MqttPingReqPacket()).ConfigureAwait(false);
- await Task.Delay(_options.KeepAlivePeriod, cancellationToken).ConfigureAwait(false);
- }
- }
- catch (OperationCanceledException)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
-
- await DisconnectInternalAsync(null).ConfigureAwait(false);
- }
- catch (MqttCommunicationException exception)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
-
- _logger.Warning<MqttClient>(exception, "MQTT communication exception while sending/receiving keep alive packets.");
- await DisconnectInternalAsync(exception).ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- _logger.Warning<MqttClient>(exception, "Unhandled exception while sending/receiving keep alive packets.");
- await DisconnectInternalAsync(exception).ConfigureAwait(false);
- }
- finally
- {
- _logger.Info<MqttClient>("Stopped sending keep alive packets.");
- }
- }
-
- private async Task ReceivePacketsAsync(CancellationToken cancellationToken)
- {
- _logger.Info<MqttClient>("Start receiving packets.");
-
- try
- {
- while (!cancellationToken.IsCancellationRequested)
- {
- _isReceivingPackets = true;
-
- var packet = await _adapter.ReceivePacketAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false);
-
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
-
- StartProcessReceivedPacket(packet, cancellationToken);
- }
- }
- catch (OperationCanceledException)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
-
- await DisconnectInternalAsync(null).ConfigureAwait(false);
- }
- catch (MqttCommunicationException exception)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
-
- _logger.Warning<MqttClient>(exception, "MQTT communication exception while receiving packets.");
- await DisconnectInternalAsync(exception).ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- _logger.Error<MqttClient>(exception, "Unhandled exception while receiving packets.");
- await DisconnectInternalAsync(exception).ConfigureAwait(false);
- }
- finally
- {
- _logger.Info<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(
- async () => await ProcessReceivedPacketAsync(packet).ConfigureAwait(false),
- cancellationToken).ConfigureAwait(false);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- }
-
- private async Task StartReceivingPacketsAsync(CancellationToken cancellationToken)
- {
- _isReceivingPackets = false;
-
- #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- Task.Run(
- async () => await ReceivePacketsAsync(cancellationToken).ConfigureAwait(false),
- cancellationToken).ConfigureAwait(false);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-
- while (!_isReceivingPackets && !cancellationToken.IsCancellationRequested)
- {
- await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken).ConfigureAwait(false);
- }
- }
-
- private void StartSendingKeepAliveMessages(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(
- async () => await SendKeepAliveMessagesAsync(cancellationToken).ConfigureAwait(false),
- cancellationToken).ConfigureAwait(false);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- }
- }
- }
|