From 3e7e10ef5d3fff1936ce7fe69aa1555af00e8047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sat, 29 Jun 2024 22:46:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8IPacketEncoder=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E5=99=A8=E6=9D=A5=E5=BA=8F=E5=88=97=E5=8C=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=BC=96=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.MQTT/Handlers/IMqttHandler.cs | 20 ++++--------------- NewLife.MQTT/MqttClient.cs | 21 ++++---------------- NewLife.MQTT/MqttServer.cs | 17 +++++++++++----- NewLife.MQTT/NewLife.MQTT.csproj | 4 ++-- NewLife.MqttServer/NewLife.MqttServer.csproj | 4 ++-- Test/Test.csproj | 2 +- XUnitTestClient/XUnitTestClient.csproj | 2 +- 7 files changed, 26 insertions(+), 44 deletions(-) diff --git a/NewLife.MQTT/Handlers/IMqttHandler.cs b/NewLife.MQTT/Handlers/IMqttHandler.cs index 8edf2c0..2232569 100644 --- a/NewLife.MQTT/Handlers/IMqttHandler.cs +++ b/NewLife.MQTT/Handlers/IMqttHandler.cs @@ -56,8 +56,8 @@ public class MqttHandler : IMqttHandler, ITracerFeature, ILogFeature /// 集群消息交换机 public ClusterExchange? ClusterExchange { get; set; } - /// Json序列化主机 - public IJsonHost JsonHost { get; set; } = null!; + /// 编码器。决定对象存储序列化格式 + public IPacketEncoder Encoder { get; set; } = null!; #region 接收消息 /// 处理消息 @@ -194,7 +194,7 @@ protected virtual UnsubAck OnUnsubscribe(UnsubscribeMessage message) public async Task PublishAsync(String topic, Object data, QualityOfService qos = QualityOfService.AtMostOnce) { var pk = data as Packet; - if (pk == null && data != null) pk = Serialize(data); + if (pk == null && data != null) pk = Encoder.Encode(data); if (pk == null) throw new ArgumentNullException(nameof(data)); var message = new PublishMessage @@ -216,7 +216,7 @@ protected virtual UnsubAck OnUnsubscribe(UnsubscribeMessage message) public async Task PublishAsync(String topic, Object data, Boolean allowExchange, QualityOfService qos = QualityOfService.AtMostOnce) { var pk = data as Packet; - if (pk == null && data != null) pk = Serialize(data); + if (pk == null && data != null) pk = Encoder.Encode(data); if (pk == null) throw new ArgumentNullException(nameof(data)); var message = new PublishMessage @@ -302,18 +302,6 @@ protected virtual UnsubAck OnUnsubscribe(UnsubscribeMessage message) throw; } } - - /// 把对象序列化为数据,字节数组和字符串以外的复杂类型,走Json序列化 - /// - /// - protected virtual Packet Serialize(Object data) - { - if (data is Packet pk) return pk; - if (data is Byte[] buf) return buf; - if (data is String str) return str.GetBytes(); - - return JsonHost.Write(data).GetBytes(); - } #endregion #region 辅助 diff --git a/NewLife.MQTT/MqttClient.cs b/NewLife.MQTT/MqttClient.cs index b4a8c46..65e982e 100644 --- a/NewLife.MQTT/MqttClient.cs +++ b/NewLife.MQTT/MqttClient.cs @@ -57,8 +57,8 @@ public class MqttClient : DisposeBase /// public Boolean CleanSession { get; set; } = true; - /// Json序列化主机 - public IJsonHost JsonHost { get; set; } = JsonHelper.Default; + /// 编码器。决定对象存储序列化格式,默认json + public IPacketEncoder Encoder { get; set; } = new DefaultPacketEncoder(); /// /// 断开后是否自动重连 @@ -209,8 +209,7 @@ private void Init() Init(); - var client = _Client; - if (client == null) throw new ArgumentNullException(nameof(_Client)); + var client = _Client ?? throw new ArgumentNullException(nameof(_Client)); try { // 断开消息没有响应 @@ -455,7 +454,7 @@ private void Client_Closed(Object sender, EventArgs e) public async Task PublishAsync(String topic, Object data, QualityOfService qos = QualityOfService.AtMostOnce) { var pk = data as Packet; - if (pk == null && data != null) pk = Serialize(data); + if (pk == null && data != null) pk = Encoder.Encode(data); if (pk == null) throw new ArgumentNullException(nameof(data)); var message = new PublishMessage @@ -486,18 +485,6 @@ private void Client_Closed(Object sender, EventArgs e) return rs; } - - /// 把对象序列化为数据,字节数组和字符串以外的复杂类型,走Json序列化 - /// - /// - protected virtual Packet Serialize(Object data) - { - if (data is Packet pk) return pk; - if (data is Byte[] buf) return buf; - if (data is String str) return str.GetBytes(); - - return JsonHost.Write(data).GetBytes(); - } #endregion #region 订阅 diff --git a/NewLife.MQTT/MqttServer.cs b/NewLife.MQTT/MqttServer.cs index 84bdacf..1dd745f 100644 --- a/NewLife.MQTT/MqttServer.cs +++ b/NewLife.MQTT/MqttServer.cs @@ -1,4 +1,5 @@ -using NewLife.Log; +using NewLife.Data; +using NewLife.Log; using NewLife.Model; using NewLife.MQTT.Clusters; using NewLife.MQTT.Handlers; @@ -23,8 +24,8 @@ public class MqttServer : NetServer /// 集群服务端 public ClusterServer? Cluster { get; set; } - /// Json序列化主机 - public IJsonHost? JsonHost { get; set; } + /// 编码器。决定对象存储序列化格式,默认json + public IPacketEncoder Encoder { get; set; } = null!; /// 实例化MQTT服务器 public MqttServer() => Port = 1883; @@ -40,7 +41,13 @@ protected override void OnStart() Name = $"Mqtt{Port}"; - JsonHost ??= ServiceProvider.GetService() ?? JsonHelper.Default; + //JsonHost ??= ServiceProvider.GetService() ?? JsonHelper.Default; + Encoder ??= ServiceProvider.GetService() ?? new DefaultPacketEncoder(); + if (Encoder is DefaultPacketEncoder encoder) + { + var jsonHost = ServiceProvider.GetService(); + if (jsonHost != null) encoder.JsonHost = jsonHost; + } var exchange = Exchange; exchange ??= ServiceProvider.GetService(); @@ -130,7 +137,7 @@ protected override void OnConnected() mqttHandler.Session = this; mqttHandler.Exchange = Host.Exchange; mqttHandler.ClusterExchange = Host.Cluster?.ClusterExchange; - mqttHandler.JsonHost = Host.JsonHost ?? ServiceProvider?.GetService() ?? JsonHelper.Default; + mqttHandler.Encoder = Host.Encoder; } MqttHandler = handler; diff --git a/NewLife.MQTT/NewLife.MQTT.csproj b/NewLife.MQTT/NewLife.MQTT.csproj index d51ebe9..f967072 100644 --- a/NewLife.MQTT/NewLife.MQTT.csproj +++ b/NewLife.MQTT/NewLife.MQTT.csproj @@ -65,8 +65,8 @@ - - + + diff --git a/NewLife.MqttServer/NewLife.MqttServer.csproj b/NewLife.MqttServer/NewLife.MqttServer.csproj index ec20d1f..9ed3d4e 100644 --- a/NewLife.MqttServer/NewLife.MqttServer.csproj +++ b/NewLife.MqttServer/NewLife.MqttServer.csproj @@ -21,8 +21,8 @@ - - + + diff --git a/Test/Test.csproj b/Test/Test.csproj index 03ad244..5b334ad 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ latest - + diff --git a/XUnitTestClient/XUnitTestClient.csproj b/XUnitTestClient/XUnitTestClient.csproj index 498366d..7765af9 100644 --- a/XUnitTestClient/XUnitTestClient.csproj +++ b/XUnitTestClient/XUnitTestClient.csproj @@ -10,7 +10,7 @@ - +