mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Refactor managed MQTT client
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Client;
|
||||
|
||||
namespace MQTTnet.Core.ManagedClient
|
||||
{
|
||||
public interface IManagedMqttClientOptions
|
||||
{
|
||||
IMqttClientOptions ClientOptions { get; }
|
||||
|
||||
bool UseAutoReconnect { get; }
|
||||
TimeSpan AutoReconnectDelay { get; }
|
||||
|
||||
IManagedMqttClientStorage Storage { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MQTTnet.Core.ManagedClient
|
||||
{
|
||||
public interface IManagedMqttClientStorage
|
||||
{
|
||||
Task SaveQueuedMessagesAsync(IList<MqttApplicationMessage> messages);
|
||||
|
||||
Task<IList<MqttApplicationMessage>> LoadQueuedMessagesAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Exceptions;
|
||||
using MQTTnet.Core.Packets;
|
||||
|
||||
namespace MQTTnet.Core.ManagedClient
|
||||
{
|
||||
public class ManagedMqttClient
|
||||
{
|
||||
private readonly List<MqttApplicationMessage> _messageQueue = new List<MqttApplicationMessage>();
|
||||
private readonly AutoResetEvent _messageQueueGate = new AutoResetEvent(false);
|
||||
private readonly MqttClient _mqttClient;
|
||||
|
||||
private IManagedMqttClientOptions _options;
|
||||
|
||||
public ManagedMqttClient(IMqttCommunicationAdapterFactory communicationChannelFactory)
|
||||
{
|
||||
if (communicationChannelFactory == null) throw new ArgumentNullException(nameof(communicationChannelFactory));
|
||||
|
||||
_mqttClient = new MqttClient(communicationChannelFactory);
|
||||
_mqttClient.Connected += OnConnected;
|
||||
_mqttClient.Disconnected += OnDisconnected;
|
||||
_mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;
|
||||
}
|
||||
|
||||
private void OnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
|
||||
{
|
||||
ApplicationMessageReceived?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void OnDisconnected(object sender, MqttClientDisconnectedEventArgs eventArgs)
|
||||
{
|
||||
//Disconnected?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void OnConnected(object sender, EventArgs e)
|
||||
{
|
||||
Connected?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public event EventHandler Connected;
|
||||
public event EventHandler Disconnected;
|
||||
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
|
||||
|
||||
public bool IsConnected => _mqttClient.IsConnected;
|
||||
|
||||
|
||||
public void Start(IManagedMqttClientOptions options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public async Task ConnectAsync(IManagedMqttClientOptions options)
|
||||
{
|
||||
//////TODO VERY BAD
|
||||
////_options = options as ManagedMqttClientTcpOptions;
|
||||
////this._usePersistance = _options.Storage != null;
|
||||
////await _mqttClient.ConnectAsync(options);
|
||||
////SetupOutgoingPacketProcessingAsync();
|
||||
|
||||
//////load persistentMessages
|
||||
////////if (_usePersistance)
|
||||
////////{
|
||||
//////// if (_persistentMessagesManager == null)
|
||||
//////// _persistentMessagesManager = new ManagedMqttClientMessagesManager(_options);
|
||||
//////// await _persistentMessagesManager.LoadMessagesAsync();
|
||||
//////// await InternalPublishAsync(_persistentMessagesManager.GetMessages(), false);
|
||||
////////}
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync()
|
||||
{
|
||||
await _mqttClient.DisconnectAsync();
|
||||
}
|
||||
|
||||
public async Task UnsubscribeAsync(IEnumerable<string> topicFilters)
|
||||
{
|
||||
// TODO: Move all subscriptions to list an subscribe after connection has lost. But only if server session is new.
|
||||
await _mqttClient.UnsubscribeAsync(topicFilters);
|
||||
}
|
||||
|
||||
public void Enqueue(IEnumerable<MqttApplicationMessage> applicationMessages)
|
||||
{
|
||||
ThrowIfNotConnected();
|
||||
|
||||
_messageQueue.AddRange(applicationMessages);
|
||||
_options.Storage?.SaveQueuedMessagesAsync(_messageQueue.ToList());
|
||||
|
||||
_messageQueueGate.Set();
|
||||
}
|
||||
|
||||
public async Task<IList<MqttSubscribeResult>> SubscribeAsync(IEnumerable<TopicFilter> topicFilters)
|
||||
{
|
||||
return await _mqttClient.SubscribeAsync(topicFilters);
|
||||
}
|
||||
|
||||
private void ThrowIfNotConnected()
|
||||
{
|
||||
if (!IsConnected) throw new MqttCommunicationException("The client is not connected.");
|
||||
}
|
||||
|
||||
private void SetupOutgoingPacketProcessingAsync()
|
||||
{
|
||||
//Task.Factory.StartNew(
|
||||
// () => SendPackets(_mqttClient._cancellationTokenSource.Token),
|
||||
// _mqttClient._cancellationTokenSource.Token,
|
||||
// TaskCreationOptions.LongRunning,
|
||||
// TaskScheduler.Default).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task SendPackets(CancellationToken cancellationToken)
|
||||
{
|
||||
//MqttNetTrace.Information(nameof(MqttClientManaged), "Start sending packets.");
|
||||
//MqttApplicationMessage messageInQueue = null;
|
||||
|
||||
//try
|
||||
//{
|
||||
// while (!cancellationToken.IsCancellationRequested)
|
||||
// {
|
||||
// messageInQueue = _inflightQueue.Take();
|
||||
// await _mqttClient.PublishAsync(new List<MqttApplicationMessage>() { messageInQueue });
|
||||
// if (_usePersistance)
|
||||
// await _persistentMessagesManager.Remove(messageInQueue);
|
||||
// }
|
||||
//}
|
||||
//catch (OperationCanceledException)
|
||||
//{
|
||||
//}
|
||||
//catch (MqttCommunicationException exception)
|
||||
//{
|
||||
// MqttNetTrace.Warning(nameof(MqttClient), exception, "MQTT communication exception while sending packets.");
|
||||
// //message not send, equeue it again
|
||||
// if (messageInQueue != null)
|
||||
// _inflightQueue.Add(messageInQueue);
|
||||
//}
|
||||
//catch (Exception exception)
|
||||
//{
|
||||
// MqttNetTrace.Error(nameof(MqttClient), exception, "Unhandled exception while sending packets.");
|
||||
// await DisconnectAsync().ConfigureAwait(false);
|
||||
//}
|
||||
//finally
|
||||
//{
|
||||
// MqttNetTrace.Information(nameof(MqttClient), "Stopped sending packets.");
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
|
||||
namespace MQTTnet.Core.ManagedClient
|
||||
{
|
||||
public class ManagedMqttClientMessagesManager
|
||||
{
|
||||
private readonly IList<MqttApplicationMessage> _persistedMessages = new List<MqttApplicationMessage>();
|
||||
private readonly ManagedMqttClientOptions _options;
|
||||
|
||||
public ManagedMqttClientMessagesManager(ManagedMqttClientOptions options)
|
||||
{
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
public async Task LoadMessagesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var persistentMessages = await _options.Storage.LoadQueuedMessagesAsync();
|
||||
lock (_persistedMessages)
|
||||
{
|
||||
_persistedMessages.Clear();
|
||||
foreach (var persistentMessage in persistentMessages)
|
||||
{
|
||||
_persistedMessages.Add(persistentMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MqttNetTrace.Error(nameof(ManagedMqttClientMessagesManager), exception, "Unhandled exception while loading persistent messages.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveMessageAsync(MqttApplicationMessage applicationMessage)
|
||||
{
|
||||
if (applicationMessage != null)
|
||||
{
|
||||
lock (_persistedMessages)
|
||||
{
|
||||
_persistedMessages.Add(applicationMessage);
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
if (_options.Storage != null)
|
||||
{
|
||||
await _options.Storage.SaveQueuedMessagesAsync(_persistedMessages);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MqttNetTrace.Error(nameof(ManagedMqttClientMessagesManager), exception, "Unhandled exception while saving persistent messages.");
|
||||
}
|
||||
}
|
||||
|
||||
public List<MqttApplicationMessage> GetMessages()
|
||||
{
|
||||
var persistedMessages = new List<MqttApplicationMessage>();
|
||||
lock (_persistedMessages)
|
||||
{
|
||||
foreach (var persistedMessage in _persistedMessages)
|
||||
{
|
||||
persistedMessages.Add(persistedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
return persistedMessages;
|
||||
}
|
||||
|
||||
public async Task Remove(MqttApplicationMessage message)
|
||||
{
|
||||
lock (_persistedMessages)
|
||||
_persistedMessages.Remove(message);
|
||||
|
||||
await SaveMessageAsync(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Client;
|
||||
|
||||
namespace MQTTnet.Core.ManagedClient
|
||||
{
|
||||
public class ManagedMqttClientOptions : IManagedMqttClientOptions
|
||||
{
|
||||
public IMqttClientOptions ClientOptions { get; set; }
|
||||
|
||||
public bool UseAutoReconnect { get; set; } = true;
|
||||
public TimeSpan AutoReconnectDelay { get; set; } = TimeSpan.FromSeconds(5);
|
||||
|
||||
public IManagedMqttClientStorage Storage { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user