mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 19:58:57 +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; }
|
||||
}
|
||||
|
||||
@@ -17,10 +17,5 @@ namespace MQTTnet.Core.Exceptions
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public MqttCommunicationException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,4 +32,8 @@
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Any CPU|x86'" />
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="ManagedClient\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -8,13 +8,6 @@ namespace MQTTnet.Core
|
||||
{
|
||||
public class MqttApplicationMessageFactory
|
||||
{
|
||||
public MqttApplicationMessage CreateApplicationMessage(string topic, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, bool retain = false)
|
||||
{
|
||||
if (topic == null) throw new ArgumentNullException(nameof(topic));
|
||||
|
||||
return CreateApplicationMessage(topic, new byte[0], qualityOfServiceLevel, retain);
|
||||
}
|
||||
|
||||
public MqttApplicationMessage CreateApplicationMessage(string topic, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, bool retain = false)
|
||||
{
|
||||
if (topic == null) throw new ArgumentNullException(nameof(topic));
|
||||
|
||||
@@ -12,8 +12,7 @@ namespace MQTTnet.Core.Packets
|
||||
|
||||
if (responsePacket is IMqttPacketWithIdentifier responsePacketWithIdentifier)
|
||||
{
|
||||
var requestPacketWithIdentifier = packet as IMqttPacketWithIdentifier;
|
||||
if (requestPacketWithIdentifier == null)
|
||||
if (!(packet is IMqttPacketWithIdentifier requestPacketWithIdentifier))
|
||||
{
|
||||
throw new InvalidOperationException("Response packet has PacketIdentifier but request packet does not.");
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace MQTTnet.Core.Packets
|
||||
{
|
||||
public ushort PacketIdentifier { get; set; }
|
||||
|
||||
public IList<MqttSubscribeReturnCode> SubscribeReturnCodes { get; set; } = new List<MqttSubscribeReturnCode>();
|
||||
public IList<MqttSubscribeReturnCode> SubscribeReturnCodes { get; } = new List<MqttSubscribeReturnCode>();
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
@@ -58,19 +58,19 @@ namespace MQTTnet.Core.Serializer
|
||||
return Serialize(connAckPacket, writer);
|
||||
}
|
||||
|
||||
if (packet is MqttDisconnectPacket disconnectPacket)
|
||||
if (packet is MqttDisconnectPacket)
|
||||
{
|
||||
return Serialize(disconnectPacket);
|
||||
return SerializeEmptyPacket(MqttControlPacketType.Disconnect);
|
||||
}
|
||||
|
||||
if (packet is MqttPingReqPacket pingReqPacket)
|
||||
if (packet is MqttPingReqPacket)
|
||||
{
|
||||
return Serialize(pingReqPacket);
|
||||
return SerializeEmptyPacket(MqttControlPacketType.PingReq);
|
||||
}
|
||||
|
||||
if (packet is MqttPingRespPacket pingRespPacket)
|
||||
if (packet is MqttPingRespPacket)
|
||||
{
|
||||
return Serialize(pingRespPacket);
|
||||
return SerializeEmptyPacket(MqttControlPacketType.PingResp);
|
||||
}
|
||||
|
||||
if (packet is MqttPublishPacket publishPacket)
|
||||
@@ -338,6 +338,8 @@ namespace MQTTnet.Core.Serializer
|
||||
|
||||
private static void ValidateConnectPacket(MqttConnectPacket packet)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
|
||||
if (string.IsNullOrEmpty(packet.ClientId) && !packet.CleanSession)
|
||||
{
|
||||
throw new MqttProtocolViolationException("CleanSession must be set if ClientId is empty [MQTT-3.1.3-7].");
|
||||
@@ -346,6 +348,8 @@ namespace MQTTnet.Core.Serializer
|
||||
|
||||
private static void ValidatePublishPacket(MqttPublishPacket packet)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
|
||||
if (packet.QualityOfServiceLevel == 0 && packet.Dup)
|
||||
{
|
||||
throw new MqttProtocolViolationException("Dup flag must be false for QoS 0 packets [MQTT-3.3.1-2].");
|
||||
@@ -433,21 +437,6 @@ namespace MQTTnet.Core.Serializer
|
||||
return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRel, 0x02);
|
||||
}
|
||||
|
||||
private static byte Serialize(MqttDisconnectPacket packet)
|
||||
{
|
||||
return SerializeEmptyPacket(MqttControlPacketType.Disconnect);
|
||||
}
|
||||
|
||||
private static byte Serialize(MqttPingReqPacket packet)
|
||||
{
|
||||
return SerializeEmptyPacket(MqttControlPacketType.PingReq);
|
||||
}
|
||||
|
||||
private static byte Serialize(MqttPingRespPacket packet)
|
||||
{
|
||||
return SerializeEmptyPacket(MqttControlPacketType.PingResp);
|
||||
}
|
||||
|
||||
private static byte Serialize(MqttPublishPacket packet, MqttPacketWriter writer)
|
||||
{
|
||||
ValidatePublishPacket(packet);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Adapter;
|
||||
|
||||
namespace MQTTnet.Core.Server
|
||||
{
|
||||
@@ -11,7 +10,7 @@ namespace MQTTnet.Core.Server
|
||||
event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
|
||||
event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
|
||||
|
||||
IReadOnlyList<ConnectedMqttClient> GetConnectedClients();
|
||||
IList<ConnectedMqttClient> GetConnectedClients();
|
||||
void Publish(MqttApplicationMessage applicationMessage);
|
||||
|
||||
Task StartAsync();
|
||||
|
||||
@@ -103,7 +103,7 @@ namespace MQTTnet.Core.Server
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<ConnectedMqttClient> GetConnectedClients()
|
||||
public IList<ConnectedMqttClient> GetConnectedClients()
|
||||
{
|
||||
lock (_clientSessions)
|
||||
{
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace MQTTnet.Core.Server
|
||||
_clientSessionsManager.ClientDisconnected += OnClientDisconnected;
|
||||
}
|
||||
|
||||
public IReadOnlyList<ConnectedMqttClient> GetConnectedClients()
|
||||
public IList<ConnectedMqttClient> GetConnectedClients()
|
||||
{
|
||||
return _clientSessionsManager.GetConnectedClients();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user