diff --git a/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs b/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs index c2db0e2..f381d47 100644 --- a/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs +++ b/Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs @@ -13,12 +13,18 @@ public class TCPClientExample : MonoBehaviour,ITCPClient Debug.Log("TestGetMsg:"+e.msg); Debug.Log("IpAddress:"+sender); //应答 - this.sendRequest(new StringRequest("Received!")); + if (sender == "127.0.0.1:20000") { + this.sendRequest(new StringRequest("Received!")); + } + if (sender == "TCP2号机") { + this.sendRequest(new StringRequest("Received2!")); + } //取消接收该数据 - this.offReceive(); + //this.offReceive(); }); //开启开数据类型接收 this.onReceive(); + this.onReceive(); } } #region ResReqData diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs b/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs index 5d3b4ce..8996192 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs @@ -15,22 +15,25 @@ namespace JXSoft private TCPLinkState tcpState = TCPLinkState.NoIp; private string tcpAddress = ""; private int tcpPort = 0; + private TCPClientUtility tcpClient = new TCPClientUtility(); public string myAddress = ""; protected override void OnInit() { this.RegisterEvent(e=> { setLinkState((int)TCPLinkState.LinkTimeOut); - this.GetUtility().CloseTCPClient(); + tcpClient.CloseTCPClient(); }); //注册重写发送事件 this.RegisterEvent(e => { - if (this.GetUtility().isOpenTCP) - { - this.GetUtility().sendData(e.req.toProtocolData()); - } - else - { - Debug.LogWarning("请先开启TCP链接"); + if (e.sender == getSender()) { + if (tcpClient.isOpenTCP) + { + tcpClient.sendData(e.req.toProtocolData()); + } + else + { + Debug.LogWarning("请先开启TCP链接"); + } } }); } @@ -70,10 +73,10 @@ namespace JXSoft { if (tcpState == TCPLinkState.Linking) { - if (!this.GetUtility().isOpenTCP) + if (!tcpClient.isOpenTCP) { - bool isSuccess = this.GetUtility().StartTCPClient(tcpAddress, tcpPort); + bool isSuccess = tcpClient.StartTCPClient(tcpAddress, tcpPort); if (isSuccess) { setLinkState((int)TCPLinkState.LinkSucess); @@ -95,10 +98,10 @@ namespace JXSoft { if (tcpState == TCPLinkState.LinkSucess) { - if (this.GetUtility().isOpenTCP) + if (tcpClient.isOpenTCP) { - this.GetUtility().CloseTCPClient(); + tcpClient.CloseTCPClient(); setLinkState((int)TCPLinkState.NoIp); return true; } @@ -107,7 +110,11 @@ namespace JXSoft } public override string getSender() { - return tcpAddress; + return tcpAddress + ":" + tcpPort; + } + + public TCPClientUtility getInstance() { + return tcpClient; } } diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs b/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs index aa7b756..7ab215d 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs @@ -11,8 +11,14 @@ namespace JXSoft { { protected override void Init() { - this.RegisterUtility(new TCPClientUtility()); this.RegisterModel(new TCPClientModel()); + this.RegisterModel(new TCPClient2Model()); + } + } + public class TCPClient2Model :TCPClientModel{ + public override string getSender() + { + return "TCP2Ż"; } } public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent @@ -20,79 +26,59 @@ namespace JXSoft { public string tcpAddress; public int tcpPort; //Printer&MsgGetter - private TCPClientUtility tcpUtil; public Transform tcpMsgContent; public GameObject tcpMsgItem; - - public UnityEvent onRecievedOpenDevice; - public UnityEvent onTCPLinkSuccess; - public UnityEvent onTCPLinkFaild; - public UnityEvent onServerConnected; - public UnityEvent onTCPReLink; - private int deviceId; // Start is called before the first frame update void Awake() { initTCPService(); - DontDestroyOnLoad(this); - tcpUtil = GetArchitecture().GetUtility(); } public void initTCPService() { - deviceId = 1; - - this.RegisterLinkStateEvent((int)TCPLinkState.Linking, () => - { - Debug.Log("TCPʼ"); - this.GetModel().Start(); - }); - this.RegisterLinkStateEvent((int)TCPLinkState.LinkSucess, () => - { - Debug.Log("TCPӳɹ"); - onTCPLinkSuccess.Invoke(); - }); - this.RegisterLinkStateEvent((int)TCPLinkState.LinkFaild, () => - { - Debug.Log("TCPʧܣϵ豸Ա"); - onTCPLinkFaild.Invoke(); - }); - - this.GetModel().setLinkState((int)TCPLinkState.NoIp); this.GetModel().setIP(tcpAddress, tcpPort); + this.GetModel().Start(); + + this.GetModel().setIP(tcpAddress, tcpPort+1); + this.GetModel().Start(); } + #region ʾ public void restartTCPService() { - onTCPReLink.Invoke(); this.GetModel().Close(); StartCoroutine(waitTwoSecond()); } public IEnumerator waitTwoSecond() { yield return new WaitForSeconds(2.0f); - this.GetModel().setLinkState((int)TCPLinkState.Linking); + this.GetModel().Start(); } + #endregion // Update is called once per frame void Update() { //Debug.Log(tcpUtil.Read_TCPClient()); - if (tcpUtil != null && !"".Equals(tcpUtil.getReceivedValue())) + recevieMsgUpdate(); + recevieMsgUpdate(); + } + + public void recevieMsgUpdate() where T:TCPClientModel { + if (this.GetModel().getInstance() != null && !"".Equals(this.GetModel().getInstance().getReceivedValue())) { GameObject item = Instantiate(tcpMsgItem, tcpMsgContent); - item.GetComponentInChildren().text = tcpUtil.receivedData; - this.GetModel().onDataRecived.Invoke(tcpUtil.receivedData); + item.GetComponentInChildren().text = this.GetModel().getInstance().receivedData; + this.GetModel().onDataRecived.Invoke(this.GetModel().getInstance().receivedData); } - if (tcpUtil.getTimeOutState() && tcpUtil.isOpenTCP) + if (this.GetModel().getInstance().getTimeOutState() && this.GetModel().getInstance().isOpenTCP) { - this.SendEvent(new onLinkException(tcpUtil.exceptionData)); - tcpUtil.isOpenTCP = false; + this.SendEvent(new onLinkException(this.GetModel().getInstance().exceptionData)); + this.GetModel().getInstance().isOpenTCP = false; } } + private void OnDestroy() { - if (tcpUtil.reciveT != null && tcpUtil.reciveT.ThreadState == ThreadState.Running) - { - tcpUtil.reciveT.Abort(); - } + this.GetModel().Close(); + this.GetModel().Close(); } public IArchitecture GetArchitecture() @@ -107,6 +93,15 @@ namespace JXSoft { } public static class TCPClientExtention { + public static IUnRegister RegisterMessageEvent(this ITCPClient self, Action onEvent) where TResponse : IResponse + { + return TCPMangerArchitecture.Interface.RegisterEvent(e => { + if (e.res.GetType() == typeof(TResponse)) + { + onEvent.Invoke((TResponse)e.res); + } + }); + } public static IUnRegister RegisterMessageEvent(this ITCPClient self, Action onEvent) where TResponse : IResponse { return TCPMangerArchitecture.Interface.RegisterEvent(e => { @@ -120,10 +115,18 @@ namespace JXSoft { { TCPMangerArchitecture.Interface.GetModel().onReceive(); } + public static void onReceive(this ITCPClient self) where TResponse : IResponse, new()where TTCPClientModel:TCPClientModel + { + TCPMangerArchitecture.Interface.GetModel().onReceive(); + } public static void offReceive(this ITCPClient self) where TResponse : IResponse, new() { TCPMangerArchitecture.Interface.GetModel().offReceive(); } + public static void offReceive(this ITCPClient self) where TResponse : IResponse, new() where TTCPClientModel : TCPClientModel + { + TCPMangerArchitecture.Interface.GetModel().offReceive(); + } public static void sendRequest(this ITCPClient self ,TRequest request) where TRequest : IRequest, new() { TCPMangerArchitecture.Interface.GetModel().sendRequest(request); diff --git a/Assets/MsgTransmitTools/src/DataEventModel.cs b/Assets/MsgTransmitTools/src/DataEventModel.cs index ab8cdb3..74752d0 100644 --- a/Assets/MsgTransmitTools/src/DataEventModel.cs +++ b/Assets/MsgTransmitTools/src/DataEventModel.cs @@ -59,9 +59,9 @@ namespace JXSoft { { if (mCommandContainer.Get>() == null) { - mCommandContainer.Register(new SendRequestCommand(request)); + mCommandContainer.Register(new SendRequestCommand(request,getSender())); } - mCommandContainer.Get>().setRequest(request); + mCommandContainer.Get>().setRequest(request,getSender()); this.SendCommand(mCommandContainer.Get>()); } #endregion @@ -140,17 +140,20 @@ namespace JXSoft { public class SendRequestCommand : AbstractCommand where TRequest : IRequest { public TRequest request; - public SendRequestCommand(TRequest request) + public string sender; + public SendRequestCommand(TRequest request,string sender) { this.request = request; + this.sender = sender; } protected override void OnExecute() { - this.SendEvent(new RequestMsgEvent(request)); + this.SendEvent(new RequestMsgEvent(request,sender)); } - public void setRequest(TRequest request) + public void setRequest(TRequest request,string sender) { this.request = request; + this.sender = sender; } } #endregion @@ -301,9 +304,11 @@ namespace JXSoft { public struct RequestMsgEvent { public IRequest req; - public RequestMsgEvent(IRequest req_) + public string sender; + public RequestMsgEvent(IRequest req_ ,string sender_) { req = req_; + sender = sender_; } } public struct LinkStateChangedEvent { diff --git a/Assets/MsgTransmitTools/src/QFrameCopy.cs b/Assets/MsgTransmitTools/src/QFrameCopy.cs index f728c5a..3e91a5a 100644 --- a/Assets/MsgTransmitTools/src/QFrameCopy.cs +++ b/Assets/MsgTransmitTools/src/QFrameCopy.cs @@ -17,6 +17,7 @@ * 猫叔(一只皮皮虾) https://space.bilibili.com/656352/ * * Latest Update: 2022.8.8 10:24 List=>HashSet + * 此为QFrame删减修改版,部分接口功能可能有所不同,有些原本的部分可能不可用。 ****************************************************************************/ using System;