From 48b4771fd7dbcc732df8927c229c2085f4e90d6a Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Tue, 15 Nov 2022 18:24:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E8=A7=A3=E8=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Plugins/Android/BTPluginAndroid.aar.meta | 5 +- .../TCPClient/Script/Source/DataEventModel.cs | 288 +++++++++--------- .../TCPClient/Script/Source/TCPEventModel.cs | 22 +- Assets/TCPClient/Script/View/TCPClientView.cs | 8 + 4 files changed, 173 insertions(+), 150 deletions(-) diff --git a/Assets/Plugins/Android/BTPluginAndroid.aar.meta b/Assets/Plugins/Android/BTPluginAndroid.aar.meta index a5cc91a..3c16f55 100644 --- a/Assets/Plugins/Android/BTPluginAndroid.aar.meta +++ b/Assets/Plugins/Android/BTPluginAndroid.aar.meta @@ -1,14 +1,15 @@ fileFormatVersion: 2 guid: dbd87312a872da045b50fc427851aa45 -timeCreated: 1569408333 -licenseType: Store PluginImporter: externalObjects: {} serializedVersion: 2 iconMap: {} executionOrder: {} + defineConstraints: [] isPreloaded: 0 isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 platformData: - first: Android: Android diff --git a/Assets/TCPClient/Script/Source/DataEventModel.cs b/Assets/TCPClient/Script/Source/DataEventModel.cs index 7de187c..5757654 100644 --- a/Assets/TCPClient/Script/Source/DataEventModel.cs +++ b/Assets/TCPClient/Script/Source/DataEventModel.cs @@ -5,176 +5,190 @@ using QFramework; using UnityEngine; using UnityEngine.Events; -namespace TCPClientTools { - //任何通讯类围绕DataEventModel进行 - public abstract class DataEventModel : AbstractModel, ICanSendCommand, ICanRegisterEvent - { - public IOCContainer mCommandContainer = new IOCContainer(); - public UnityStringEvent onDataRecived = new UnityStringEvent(); +//任何通讯类围绕DataEventModel进行 +public abstract class DataEventModel : AbstractModel, ICanSendCommand, ICanRegisterEvent +{ + public IOCContainer mCommandContainer = new IOCContainer(); + public UnityStringEvent onDataRecived = new UnityStringEvent(); - public void sendResponseCommand(string json) where T : IResponse, new() - { - if (mCommandContainer.Get>() == null) - { - mCommandContainer.Register(new ExcuteResponseCommand(new T())); - } - mCommandContainer.Get>().setJson(json); - this.SendCommand(mCommandContainer.Get>()); - } - /// - /// 开启接收指定数据 - /// - /// 数据格式类型 - public void onReceive() where T : IResponse, new() - { - onDataRecived.AddListener(sendResponseCommand); - } - /// - /// 关闭接收指定数据 - /// - /// 数据格式类型 - public void offReceive() where T : IResponse, new() - { - onDataRecived.RemoveListener(sendResponseCommand); - } - /// - /// 发送数据 - /// - /// 数据类型 - /// 请求数据 - public void sendRequestCommand(T request) where T : IRequest + + public void excuteResponseCommand(string json) where T : IResponse, new() + { + if (mCommandContainer.Get>() == null) { - if (mCommandContainer.Get>() == null) - { - mCommandContainer.Register(new SendRequestCommand(request)); - } - mCommandContainer.Get>().setRequest(request); - this.SendCommand(mCommandContainer.Get>()); + mCommandContainer.Register(new ExcuteResponseCommand(new T())); } + mCommandContainer.Get>().setJson(json); + this.SendCommand(mCommandContainer.Get>()); } - - #region Command /// - /// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 + /// 开启接收指定数据 /// - /// 响应数据格式 - public class ExcuteResponseCommand : AbstractCommand where TResponse : IResponse + /// 数据格式类型 + public void onReceive() where T : IResponse, new() { - public string json; - private TResponse response; - public ExcuteResponseCommand(TResponse response) - { - if (this.response == null) - { - this.response = response; - } - } - protected override void OnExecute() - { - bool isSet = response.trySetData(json); - if (isSet) - { - Debug.Log("Received:" + response.toJson() + response.GetType()); - this.SendEvent(new ResponseMsgEvent(response)); - } - } - public void setJson(string json) + onDataRecived.AddListener(excuteResponseCommand); + } + /// + /// 关闭接收指定数据 + /// + /// 数据格式类型 + public void offReceive() where T : IResponse, new() + { + onDataRecived.RemoveListener(excuteResponseCommand); + } + /// + /// 发送数据 + /// + /// 数据类型 + /// 请求数据 + public void sendRequestCommand(T request) where T : IRequest + { + if (mCommandContainer.Get>() == null) { - this.json = json; + mCommandContainer.Register(new SendRequestCommand(request)); } + mCommandContainer.Get>().setRequest(request); + this.SendCommand(mCommandContainer.Get>()); } +} - public class SendRequestCommand : AbstractCommand where TRequest : IRequest +#region Command +/// +/// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 +/// +/// 响应数据格式 +public class ExcuteResponseCommand : AbstractCommand where TResponse : IResponse +{ + public string json; + private TResponse response; + public ExcuteResponseCommand(TResponse response) { - public TRequest request; - public SendRequestCommand(TRequest request) - { - this.request = request; - } - protected override void OnExecute() + if (this.response == null) { - if (this.GetUtility().isOpenTCP) - { - this.GetUtility().sendData(request.toJson()); - } - else - { - Debug.LogWarning("请先开启TCP链接"); - } - //throw new NotImplementedException(); + this.response = response; } - public void setRequest(TRequest request) + } + protected override void OnExecute() + { + bool isSet = response.trySetData(json); + if (isSet) { - this.request = request; + Debug.Log("Received:" + response.toJson() + response.GetType()); + this.SendEvent(new ResponseMsgEvent(response)); } } - #endregion - - #region interface - public interface IResponse + public void setJson(string json) { - string toJson(); - /// - /// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应 - /// - /// - /// - bool trySetData(string json); - string getException(); + this.json = json; } +} - public interface IRequest +public class SendRequestCommand : AbstractCommand where TRequest : IRequest +{ + public TRequest request; + public SendRequestCommand(TRequest request) { - string toJson(); + this.request = request; } - #endregion + protected override void OnExecute() + { + this.SendEvent(new RequestMsgEvent(request)); + } + public void setRequest(TRequest request) + { + this.request = request; + } +} +#endregion - #region AbstractClass +#region interface +public interface IResponse +{ + string toJson(); /// - /// 使用抽象类时,必须满足可序列化 + /// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应 /// - [Serializable] - public abstract class AbstractResponse : IResponse + /// + /// + bool trySetData(string json); + string getException(); +} + +public interface IRequest +{ + string toJson(); +} + +//用于标准化通讯方案(具体链接管理,由外部类决定,本接口只提供开启链接跟关闭链接) +public interface IProtocol { + void linkServer(); + void closeServer(); +} +#endregion + +#region Extention +/* +public static class CanGetStateExtention { + public static void GetState(this IProtocol self) { + + } +} +*/ +#endregion + +#region AbstractClass +/// +/// 使用抽象类时,必须满足可序列化 +/// +[Serializable] +public abstract class AbstractResponse : IResponse +{ + private string exceptionMsg; + public virtual string toJson() { - private string exceptionMsg; - public virtual string toJson() - { - return JsonUtility.ToJson(this, true); - } - public virtual bool trySetData(string json) + return JsonUtility.ToJson(this, true); + } + public virtual bool trySetData(string json) + { + try { - try - { - JsonUtility.FromJsonOverwrite(json, this); - return true; - } - catch (Exception e) - { - exceptionMsg = e.ToString(); - return false; - } + JsonUtility.FromJsonOverwrite(json, this); + return true; } - public string getException() + catch (Exception e) { - return exceptionMsg; + exceptionMsg = e.ToString(); + return false; } } - #endregion - - #region event - public struct ResponseMsgEvent - { - public IResponse res; - public ResponseMsgEvent(IResponse res_) - { - res = res_; - } - }; - #endregion - [Serializable] - public class UnityStringEvent : UnityEvent + public string getException() { + return exceptionMsg; + } +} +#endregion +#region event +public struct ResponseMsgEvent +{ + public IResponse res; + public ResponseMsgEvent(IResponse res_) + { + res = res_; + } +}; +public struct RequestMsgEvent { + public IRequest req; + public RequestMsgEvent(IRequest req_) { + req = req_; } } +#endregion + +[Serializable] +public class UnityStringEvent : UnityEvent +{ + +} \ No newline at end of file diff --git a/Assets/TCPClient/Script/Source/TCPEventModel.cs b/Assets/TCPClient/Script/Source/TCPEventModel.cs index 09f6528..5cb5327 100644 --- a/Assets/TCPClient/Script/Source/TCPEventModel.cs +++ b/Assets/TCPClient/Script/Source/TCPEventModel.cs @@ -1,21 +1,10 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -using UnityEngine.Events; using QFramework; -using System; namespace TCPClientTools { - public class TCPMangerArchitecture : Architecture - { - protected override void Init() - { - this.RegisterUtility(new TCPUtility()); - this.RegisterModel(new TCPEventModel()); - } - } - //统筹管理TCP的命令池以及相关链接信息 public class TCPEventModel : DataEventModel, ICanSendCommand,ICanRegisterEvent { @@ -30,6 +19,17 @@ namespace TCPClientTools setTCPState(TCPLinkState.LinkTimeOut); this.GetUtility().CloseTCPClient(); }); + //注册重写发送事件 + this.RegisterEvent(e => { + if (this.GetUtility().isOpenTCP) + { + this.GetUtility().sendData(e.req.toJson()); + } + else + { + Debug.LogWarning("请先开启TCP链接"); + } + }); } public void setTCPState(TCPLinkState state) { this.tcpState = state; diff --git a/Assets/TCPClient/Script/View/TCPClientView.cs b/Assets/TCPClient/Script/View/TCPClientView.cs index c976951..df8e9f8 100644 --- a/Assets/TCPClient/Script/View/TCPClientView.cs +++ b/Assets/TCPClient/Script/View/TCPClientView.cs @@ -6,6 +6,14 @@ using QFramework; using TCPClientTools; using System; +public class TCPMangerArchitecture : Architecture +{ + protected override void Init() + { + this.RegisterUtility(new TCPUtility()); + this.RegisterModel(new TCPEventModel()); + } +} public class TCPClientView : MonoBehaviour,IController,ICanSendEvent { public UnityEvent onRecievedOpenDevice;