From e587a864c3098c1558e298df26557cd2de746bdc Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Wed, 8 Mar 2023 17:01:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86json=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Example/Script/TCPClientExample.cs | 5 -- .../Example/Script/UDPExample.cs | 20 ++++++-- Assets/MsgTransmitTools/src/DataEventModel.cs | 46 ++++++++++++++++--- .../src/ProtocolRulesExtention.cs | 1 + README.md | 17 +------ 5 files changed, 57 insertions(+), 32 deletions(-) diff --git a/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs b/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs index 42400eb..0038a78 100644 --- a/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs +++ b/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs @@ -48,11 +48,6 @@ public struct YourNumberFilterResponse : IResponse } return isNumber; } - - public string getException() - { - return exceptionMsg; - } } public class YourRequest:IRequest { diff --git a/Assets/MsgTransmitTools/Example/Script/UDPExample.cs b/Assets/MsgTransmitTools/Example/Script/UDPExample.cs index 77fde34..4a244da 100644 --- a/Assets/MsgTransmitTools/Example/Script/UDPExample.cs +++ b/Assets/MsgTransmitTools/Example/Script/UDPExample.cs @@ -23,15 +23,20 @@ public class UDPExample : MonoBehaviour,IUDPClient } } -public class UResponseTest:AbstractJsonResponse { +public struct UResponseTest:IResponse { public string msg; - public UResponseTest() { } - public override bool trySetData(string json) + public bool trySetData(string protolData) { try { - UResponseTest res = JsonMapper.ToObject(json); - msg = res.msg; + UResponseTest res = JsonMapper.ToObject(protolData); + if (!this.isTypeOfJson(protolData)) + { + return false; + } + else { + this.msg = res.msg; + } return true; } catch (Exception e) { @@ -39,4 +44,9 @@ public class UResponseTest:AbstractJsonResponse { return false; } } + + public string toProtocolData() + { + return ""; + } } diff --git a/Assets/MsgTransmitTools/src/DataEventModel.cs b/Assets/MsgTransmitTools/src/DataEventModel.cs index 4b8b991..9c8ede7 100644 --- a/Assets/MsgTransmitTools/src/DataEventModel.cs +++ b/Assets/MsgTransmitTools/src/DataEventModel.cs @@ -19,7 +19,7 @@ namespace JXSoft { public Color printColor = Color.white; #region ReceiveFunctions - public void receiveResponse(string json) where T : IResponse, new() + public void receiveResponse(string protocolData) where T : IResponse, new() { //如果找不到对应命令,则添加命令 if (mCommandContainer.Get>() == null) @@ -27,7 +27,7 @@ namespace JXSoft { mCommandContainer.Register(new ExcuteResponseCommand(new T())); } //找到命令后添加命令 - mCommandContainer.Get>().setJson(json); + mCommandContainer.Get>().setProtocolData(protocolData); this.SendCommand(mCommandContainer.Get>()); } /// @@ -97,7 +97,7 @@ namespace JXSoft { /// 响应数据格式 public class ExcuteResponseCommand : AbstractCommand where TResponse : IResponse { - public string json; + public string protocolData; private TResponse response; private Color printColor = Color.white; public ExcuteResponseCommand(TResponse response) @@ -109,7 +109,7 @@ namespace JXSoft { } protected override void OnExecute() { - bool isSet = response.trySetData(json); + bool isSet = response.trySetData(protocolData); if (isSet) { if (response.toProtocolData() != "") { @@ -118,9 +118,9 @@ namespace JXSoft { this.SendEvent(new ResponseMsgEvent(response)); } } - public void setJson(string json) + public void setProtocolData(string protocolData) { - this.json = json; + this.protocolData = protocolData; } } @@ -150,7 +150,7 @@ namespace JXSoft { public interface IResponse { /// - /// 此处用于截获接收到的数据,建议作为调试依据 + /// 可以用于打印调试信息 /// /// string toProtocolData(); @@ -244,6 +244,38 @@ namespace JXSoft { }); } } + + /// + /// 用于扩展类型结构 + /// + public static class IResponseExtention { + + public static bool isTypeOfJson(this IResponse self, string json) + { + JsonData protocolData = new JsonData(); + try + { + protocolData = JsonMapper.ToObject(json); + } + catch { + return false; + } + + JsonData classData = JsonMapper.ToObject(JsonMapper.ToJson(self)); + if (protocolData.Keys.Count != classData.Keys.Count) + { + return false; + } + foreach (string key in classData.Keys) + { + if (!protocolData.ContainsKey(key)) + { + return false; + } + } + return true; + } + } #endregion #region event diff --git a/Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs b/Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs index 8267f89..e592470 100644 --- a/Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs +++ b/Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs @@ -48,5 +48,6 @@ namespace JXSoft { public static TObject toObjectByJson(this string self) { return JsonMapper.ToObject(self); } + } } diff --git a/README.md b/README.md index c534ac9..24d535e 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,6 @@ UDP虽然是无连接协议,但通讯包是基于UDPClient这个类编写的 详情参考 [UdpClient 类官方说明](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.udpclient?view=netframework-4.8) - - 目前支持的协议格式一览,后续会增加更多协议格式转换 | Protocol | @@ -40,10 +38,6 @@ UDP虽然是无连接协议,但通讯包是基于UDPClient这个类编写的 协议转换方案后续由协议API提供参考跟封装(此处用了litjson作为协议转换方案)。 - - - - # 使用方法 ## 1.定义响应&请求消息结构 @@ -66,9 +60,7 @@ UDP虽然是无连接协议,但通讯包是基于UDPClient这个类编写的 ```cs public struct YourNumberFilterResponse : IResponse { - public string code; - private int codeNum; - private string exceptionMsg; + public int codeNum; public string toProtocolData() { return "你想要打印的调试信息,无需打印返回空字符串即可"; @@ -76,18 +68,13 @@ public struct YourNumberFilterResponse : IResponse public bool trySetData(string protocolData) { - bool isNumber = int.TryParse(code, out codeNum); + bool isNumber = int.TryParse(protocolData, out codeNum); if (!isNumber) { exceptionMsg = "这不是数字"; } return isNumber; } - - public string getException() - { - return exceptionMsg; - } } ```