mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Refactoring
This commit is contained in:
+2
-2
@@ -3,7 +3,7 @@ using MQTTnet.Core.Serializer;
|
||||
|
||||
namespace MQTTnet.Core.Client
|
||||
{
|
||||
public abstract class MqttClientOptions
|
||||
public abstract class BaseMqttClientOptions : IMqttClientOptions
|
||||
{
|
||||
public MqttClientTlsOptions TlsOptions { get; set; } = new MqttClientTlsOptions();
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace MQTTnet.Core.Client
|
||||
|
||||
public string Password { get; set; }
|
||||
|
||||
public string ClientId { get; set; } = Guid.NewGuid().ToString().Replace("-", string.Empty);
|
||||
public string ClientId { get; set; } = Guid.NewGuid().ToString("N");
|
||||
|
||||
public bool CleanSession { get; set; } = true;
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace MQTTnet.Core.Client
|
||||
|
||||
event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
|
||||
event EventHandler Connected;
|
||||
event EventHandler Disconnected;
|
||||
event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
|
||||
|
||||
Task ConnectAsync(MqttClientOptions options);
|
||||
Task ConnectAsync(IMqttClientOptions options);
|
||||
Task DisconnectAsync();
|
||||
|
||||
Task<IList<MqttSubscribeResult>> SubscribeAsync(IEnumerable<TopicFilter> topicFilters);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Serializer;
|
||||
|
||||
namespace MQTTnet.Core.Client
|
||||
{
|
||||
public interface IMqttClientOptions
|
||||
{
|
||||
bool CleanSession { get; }
|
||||
string ClientId { get; }
|
||||
TimeSpan DefaultCommunicationTimeout { get; }
|
||||
TimeSpan KeepAlivePeriod { get; }
|
||||
string Password { get; }
|
||||
MqttProtocolVersion ProtocolVersion { get; }
|
||||
MqttClientTlsOptions TlsOptions { get; }
|
||||
string UserName { get; }
|
||||
MqttApplicationMessage WillMessage { get; }
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,6 @@ namespace MQTTnet.Core.Client
|
||||
{
|
||||
public interface IMqttCommunicationAdapterFactory
|
||||
{
|
||||
IMqttCommunicationAdapter CreateMqttCommunicationAdapter(MqttClientOptions options);
|
||||
IMqttCommunicationAdapter CreateMqttCommunicationAdapter(IMqttClientOptions options);
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ namespace MQTTnet.Core.Client
|
||||
private readonly MqttPacketDispatcher _packetDispatcher = new MqttPacketDispatcher();
|
||||
private readonly IMqttCommunicationAdapterFactory _communicationChannelFactory;
|
||||
|
||||
private MqttClientOptions _options;
|
||||
private IMqttClientOptions _options;
|
||||
private bool _isReceivingPackets;
|
||||
private int _latestPacketIdentifier;
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
@@ -30,12 +30,12 @@ namespace MQTTnet.Core.Client
|
||||
}
|
||||
|
||||
public event EventHandler Connected;
|
||||
public event EventHandler Disconnected;
|
||||
public event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
|
||||
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
|
||||
|
||||
public bool IsConnected => _cancellationTokenSource != null && !_cancellationTokenSource.IsCancellationRequested;
|
||||
public bool IsConnected { get; private set; }
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
public async Task ConnectAsync(IMqttClientOptions options)
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
@@ -64,6 +64,7 @@ namespace MQTTnet.Core.Client
|
||||
StartSendKeepAliveMessages(_cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
IsConnected = true;
|
||||
Connected?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -222,6 +223,9 @@ namespace MQTTnet.Core.Client
|
||||
|
||||
private async Task DisconnectInternalAsync()
|
||||
{
|
||||
var clientWasConnected = IsConnected;
|
||||
IsConnected = false;
|
||||
|
||||
var cts = _cancellationTokenSource;
|
||||
if (cts == null || cts.IsCancellationRequested)
|
||||
{
|
||||
@@ -243,7 +247,7 @@ namespace MQTTnet.Core.Client
|
||||
}
|
||||
finally
|
||||
{
|
||||
Disconnected?.Invoke(this, EventArgs.Empty);
|
||||
Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Core.Client
|
||||
{
|
||||
public class MqttClientDisconnectedEventArgs : EventArgs
|
||||
{
|
||||
public MqttClientDisconnectedEventArgs(bool clientWasConnected)
|
||||
{
|
||||
ClientWasConnected = clientWasConnected;
|
||||
}
|
||||
|
||||
public bool ClientWasConnected { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace MQTTnet.Core.Client
|
||||
{
|
||||
public class MqttClientTcpOptions : MqttClientOptions
|
||||
public class MqttClientTcpOptions : BaseMqttClientOptions
|
||||
{
|
||||
public string Server { get; set; }
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace MQTTnet.Core.Client
|
||||
{
|
||||
public class MqttClientWebSocketOptions : MqttClientOptions
|
||||
public class MqttClientWebSocketOptions : BaseMqttClientOptions
|
||||
{
|
||||
public string Uri { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user