|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using MQTTnet.Client;
- using MQTTnet.Diagnostics;
- using MQTTnet.Exceptions;
- using MQTTnet.Protocol;
-
- namespace MQTTnet.ManagedClient
- {
- public class ManagedMqttClient : IManagedMqttClient
- {
- private readonly BlockingCollection<MqttApplicationMessage> _messageQueue = new BlockingCollection<MqttApplicationMessage>();
- private readonly Dictionary<string, MqttQualityOfServiceLevel> _subscriptions = new Dictionary<string, MqttQualityOfServiceLevel>();
- private readonly SemaphoreSlim _subscriptionsSemaphore = new SemaphoreSlim(1, 1);
-
- private readonly IMqttClient _mqttClient;
- private readonly IMqttNetLogger _logger;
-
- private CancellationTokenSource _connectionCancellationToken;
- private CancellationTokenSource _publishingCancellationToken;
-
- private ManagedMqttClientStorageManager _storageManager;
- private IManagedMqttClientOptions _options;
-
- private bool _subscriptionsNotPushed;
-
- public ManagedMqttClient(IMqttClient mqttClient, IMqttNetLogger logger)
- {
- _logger = logger ?? throw new ArgumentNullException(nameof(logger));
- _mqttClient = mqttClient ?? throw new ArgumentNullException(nameof(mqttClient));
-
- _mqttClient.Connected += OnConnected;
- _mqttClient.Disconnected += OnDisconnected;
- _mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;
- }
-
- public bool IsConnected => _mqttClient.IsConnected;
-
- public event EventHandler<MqttClientConnectedEventArgs> Connected;
- public event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
- public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
-
- public async Task StartAsync(IManagedMqttClientOptions options)
- {
- if (options == null) throw new ArgumentNullException(nameof(options));
- if (options.ClientOptions == null) throw new ArgumentException("The client options are not set.", nameof(options));
-
- if (!options.ClientOptions.CleanSession)
- {
- throw new NotSupportedException("The managed client does not support existing sessions.");
- }
-
- if (_connectionCancellationToken != null) throw new InvalidOperationException("The managed client is already started.");
-
- _options = options;
-
- if (_options.Storage != null)
- {
- _storageManager = new ManagedMqttClientStorageManager(_options.Storage);
- await _storageManager.LoadQueuedMessagesAsync().ConfigureAwait(false);
- }
-
- _connectionCancellationToken = new CancellationTokenSource();
-
- #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 MaintainConnectionAsync(_connectionCancellationToken.Token), _connectionCancellationToken.Token).ConfigureAwait(false);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-
- _logger.Info<ManagedMqttClient>("Started");
- }
-
- public Task StopAsync()
- {
- StopPublishing();
- StopMaintainingConnection();
-
- while (_messageQueue.Any())
- {
- _messageQueue.Take();
- }
-
- return Task.FromResult(0);
- }
-
- public async Task PublishAsync(IEnumerable<MqttApplicationMessage> applicationMessages)
- {
- if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
-
- foreach (var applicationMessage in applicationMessages)
- {
- if (_storageManager != null)
- {
- await _storageManager.AddAsync(applicationMessage).ConfigureAwait(false);
- }
-
- _messageQueue.Add(applicationMessage);
- }
- }
-
- public async Task SubscribeAsync(IEnumerable<TopicFilter> topicFilters)
- {
- if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
-
- await _subscriptionsSemaphore.WaitAsync().ConfigureAwait(false);
- try
- {
- foreach (var topicFilter in topicFilters)
- {
- _subscriptions[topicFilter.Topic] = topicFilter.QualityOfServiceLevel;
- _subscriptionsNotPushed = true;
- }
- }
- finally
- {
- _subscriptionsSemaphore.Release();
- }
- }
-
- public async Task UnsubscribeAsync(IEnumerable<string> topics)
- {
- await _subscriptionsSemaphore.WaitAsync().ConfigureAwait(false);
- try
- {
- foreach (var topic in topics)
- {
- if (_subscriptions.Remove(topic))
- {
- _subscriptionsNotPushed = true;
- }
- }
- }
- finally
- {
- _subscriptionsSemaphore.Release();
- }
- }
-
- public void Dispose()
- {
- _messageQueue?.Dispose();
- _subscriptionsSemaphore?.Dispose();
- _connectionCancellationToken?.Dispose();
- _publishingCancellationToken?.Dispose();
- }
-
- private async Task MaintainConnectionAsync(CancellationToken cancellationToken)
- {
- try
- {
- while (!cancellationToken.IsCancellationRequested)
- {
- await TryMaintainConnectionAsync(cancellationToken);
- }
- }
- catch (OperationCanceledException)
- {
- }
- catch (Exception exception)
- {
- _logger.Error<ManagedMqttClient>(exception, "Unhandled exception while maintaining connection.");
- }
- finally
- {
- await _mqttClient.DisconnectAsync().ConfigureAwait(false);
- _logger.Info<ManagedMqttClient>("Stopped");
- }
- }
-
- private async Task TryMaintainConnectionAsync(CancellationToken cancellationToken)
- {
- try
- {
- var connectionState = await ReconnectIfRequiredAsync().ConfigureAwait(false);
- if (connectionState == ReconnectionResult.NotConnected)
- {
- StopPublishing();
- await Task.Delay(_options.AutoReconnectDelay, cancellationToken).ConfigureAwait(false);
- return;
- }
-
- if (connectionState == ReconnectionResult.Reconnected || _subscriptionsNotPushed)
- {
- await PushSubscriptionsAsync().ConfigureAwait(false);
-
- StartPublishing();
-
-
- return;
- }
-
- if (connectionState == ReconnectionResult.StillConnected)
- {
- await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);
- }
- }
- catch (OperationCanceledException)
- {
- }
- catch (MqttCommunicationException exception)
- {
- _logger.Warning<ManagedMqttClient>(exception, "Communication exception while maintaining connection.");
- }
- catch (Exception exception)
- {
- _logger.Error<ManagedMqttClient>(exception, "Unhandled exception while maintaining connection.");
- }
- }
-
- private async Task PublishQueuedMessagesAsync(CancellationToken cancellationToken)
- {
- try
- {
- while (!cancellationToken.IsCancellationRequested)
- {
- var message = _messageQueue.Take(cancellationToken);
- if (message == null)
- {
- continue;
- }
-
- if (cancellationToken.IsCancellationRequested)
- {
- continue;
- }
-
- await TryPublishQueuedMessageAsync(message).ConfigureAwait(false);
- }
- }
- catch (OperationCanceledException)
- {
- }
- catch (Exception exception)
- {
- _logger.Error<ManagedMqttClient>(exception, "Unhandled exception while publishing queued application messages.");
- }
- finally
- {
- _logger.Trace<ManagedMqttClient>("Stopped publishing messages.");
- }
- }
-
- private async Task TryPublishQueuedMessageAsync(MqttApplicationMessage message)
- {
- try
- {
- await _mqttClient.PublishAsync(message).ConfigureAwait(false);
-
- if (_storageManager != null)
- {
- await _storageManager.RemoveAsync(message).ConfigureAwait(false);
- }
- }
- catch (MqttCommunicationException exception)
- {
- _logger.Warning<ManagedMqttClient>(exception, "Publishing application message failed.");
-
- if (message.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
- {
- _messageQueue.Add(message);
- }
- }
- catch (Exception exception)
- {
- _logger.Error<ManagedMqttClient>(exception, "Unhandled exception while publishing queued application message.");
- }
- }
-
- private async Task PushSubscriptionsAsync()
- {
- _logger.Info<ManagedMqttClient>(nameof(ManagedMqttClient), "Synchronizing subscriptions");
-
- List<TopicFilter> subscriptions;
- await _subscriptionsSemaphore.WaitAsync().ConfigureAwait(false);
- try
- {
- subscriptions = _subscriptions.Select(i => new TopicFilter(i.Key, i.Value)).ToList();
- _subscriptionsNotPushed = false;
- }
- finally
- {
- _subscriptionsSemaphore.Release();
- }
-
- if (!subscriptions.Any())
- {
- return;
- }
-
- try
- {
- await _mqttClient.SubscribeAsync(subscriptions).ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- _logger.Warning<ManagedMqttClient>(exception, "Synchronizing subscriptions failed");
- _subscriptionsNotPushed = true;
- }
- }
-
- private async Task<ReconnectionResult> ReconnectIfRequiredAsync()
- {
- if (_mqttClient.IsConnected)
- {
- return ReconnectionResult.StillConnected;
- }
-
- try
- {
- await _mqttClient.ConnectAsync(_options.ClientOptions).ConfigureAwait(false);
- return ReconnectionResult.Reconnected;
- }
- catch (Exception)
- {
- return ReconnectionResult.NotConnected;
- }
- }
-
- private void OnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs eventArgs)
- {
- ApplicationMessageReceived?.Invoke(this, eventArgs);
- }
-
- private void OnDisconnected(object sender, MqttClientDisconnectedEventArgs eventArgs)
- {
- Disconnected?.Invoke(this, eventArgs);
- }
-
- private void OnConnected(object sender, MqttClientConnectedEventArgs eventArgs)
- {
- Connected?.Invoke(this, eventArgs);
- }
-
- private void StartPublishing()
- {
- if (_publishingCancellationToken != null)
- {
- StopPublishing();
- }
-
- var cts = new CancellationTokenSource();
-
- _publishingCancellationToken = cts;
-
- #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 PublishQueuedMessagesAsync(cts.Token).ConfigureAwait(false), cts.Token).ConfigureAwait(false);
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- }
-
- private void StopPublishing()
- {
- _publishingCancellationToken?.Cancel(false);
- _publishingCancellationToken?.Dispose();
- _publishingCancellationToken = null;
- }
-
- private void StopMaintainingConnection()
- {
- _connectionCancellationToken?.Cancel(false);
- _connectionCancellationToken?.Dispose();
- _connectionCancellationToken = null;
- }
- }
- }
|