From 18cefe782f5675fc6385a432699df7a47d97eaea Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Mon, 20 Feb 2023 11:04:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=B1=BB=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A8=A1=E5=BC=8F=EF=BC=8C=E7=BC=96=E5=86=99?= =?UTF-8?q?=E4=BA=86=E9=93=BE=E6=8E=A5=E7=8A=B6=E6=80=81=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=EF=BC=8C=E6=B3=A8=E5=86=8C=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TCPClient/Script/Source/TCPEventModel.cs | 22 ++- Assets/MsgTransmitTools/src/DataEventModel.cs | 147 ++++++++++++++---- 2 files changed, 125 insertions(+), 44 deletions(-) diff --git a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs index 2b0a775..bd93875 100644 --- a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs +++ b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs @@ -1,5 +1,6 @@ using UnityEngine; using QFrameworkCP; +using JXSoft; using System; using System.Text; using System.Net.Sockets; @@ -111,18 +112,6 @@ namespace JXSoft LinkTimeOut = 4 } - public class TCPLinkStateManage : ILinkState{ - private TCPLinkState tcpState = TCPLinkState.NoIp; - public int getLinkState() - { - return (int)tcpState; - } - - public bool invoke(int invokeState) - { - throw new NotImplementedException(); - } - } #endregion #region event @@ -299,4 +288,13 @@ namespace JXSoft } } #endregion +} + +namespace TCPLinkState { + public class StateNoIp : BaseLinkState.StateNoLink { + public override bool linkToServer() + { + return false; + } + } } \ No newline at end of file diff --git a/Assets/MsgTransmitTools/src/DataEventModel.cs b/Assets/MsgTransmitTools/src/DataEventModel.cs index ab98a86..5c022b7 100644 --- a/Assets/MsgTransmitTools/src/DataEventModel.cs +++ b/Assets/MsgTransmitTools/src/DataEventModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using JXSoft; using QFrameworkCP; using UnityEngine; using UnityEngine.Events; @@ -13,7 +14,7 @@ namespace JXSoft { //管理消息收发响应控制 public IOCContainer mCommandContainer = new IOCContainer(); //管理连接状态响应 - public IOCContainer mLinkStateContainer = new IOCContainer(); + public Dictionary mLinkStateContainer = new Dictionary(); public UnityStringEvent onDataRecived = new UnityStringEvent(); public Color printColor = Color.white; @@ -68,30 +69,39 @@ namespace JXSoft { public abstract void setLinkState(int linkState); public abstract int getLinkState(); - public void invokeLinkState(int invokeState) where TLinkState : ILinkState ,new(){ + public void transfromLinkState(TLinkState linkState) where TLinkState : ILinkState, new() + { + if (mLinkStateContainer.ContainsKey(getLinkState())) + { + bool isSuccess = mLinkStateContainer[getLinkState()].transform(linkState); + if (isSuccess) + { + this.setLinkState(linkState.getLinkState()); + } + else + { + Debug.LogWarning("Transform Faild!"); + } + } } - - public void invokeLinkState(TLinkState linkState,int invokeState) where TLinkState : ILinkState, new() + public void transfromLinkState() where TLinkState : ILinkState, new() { - + TLinkState linkState = new TLinkState(); + transfromLinkState(linkState); } - public void registerLinkState(ILinkState linkState) { - mLinkStateContainer.Register(linkState); + if (mLinkStateContainer.ContainsKey(linkState.getLinkState())) + { + mLinkStateContainer[linkState.getLinkState()] = linkState; + } + else { + mLinkStateContainer.Add(linkState.getLinkState(), linkState); + } } - - //public abstract void nodifyLinkState() where TLinkState:ILinkState; - //public abstract void registerLinkState(); #endregion } - #region enum - public enum LinkStateDefault { - NoLink = 0, - LinkSuccess = 1, - LinkFaild = 2 - } - #endregion + #region Command /// @@ -176,10 +186,14 @@ namespace JXSoft { public interface ILinkState { - //获取当前链接状态 + //当前链接状态 int getLinkState(); - //执行当前链接 - bool invoke(int invokeState); + /// + /// 转换到xx状态 + /// + /// 目标链接状态 + /// 是否成功 + bool transform(ILinkState invokeState); } #endregion @@ -216,19 +230,7 @@ namespace JXSoft { } #endregion - #region LinkState - public class StateLinkToServer : ILinkState { - public int getLinkState() - { - return (int)LinkStateDefault.NoLink; - } - public bool invoke(int invokeState) - { - throw new NotImplementedException(); - } - } - #endregion #region event public struct ResponseMsgEvent @@ -260,4 +262,85 @@ namespace JXSoft { { } +} + + +/// +/// this Package improved a base example of link to server +/// you can also create class implements ILinkState. +/// +namespace BaseLinkState +{ + #region enum + public enum LinkStateDefault + { + NoLink = 0, + LinkSuccess = 1, + LinkFaild = 2, + LinkTimeOut = 3 + } + #endregion + + #region LinkState + public abstract class StateNoLink : ILinkState + { + public int getLinkState() + { + return (int)LinkStateDefault.NoLink; + } + public bool transform(ILinkState invokeState) + { + bool transformResult = false; + switch (invokeState.getLinkState()) + { + case (int)LinkStateDefault.LinkSuccess: + linkToServer(); + break; + default: + Debug.LogWarning("No such transform fucntion!"); + break; + } + return transformResult; + } + public abstract bool linkToServer(); + } + public class StateLinkSuccess : ILinkState + { + public int getLinkState() + { + return (int)LinkStateDefault.LinkSuccess; + } + + public bool transform(ILinkState invokeState) + { + throw new NotImplementedException(); + } + } + + public class StateLinkFaild :ILinkState{ + public int getLinkState() + { + return (int)LinkStateDefault.LinkFaild; + } + + public bool transform(ILinkState invokeState) + { + throw new NotImplementedException(); + } + } + + public class StateLinkTimeOut :ILinkState{ + public int getLinkState() + { + return (int)LinkStateDefault.LinkTimeOut; + } + + public bool transform(ILinkState invokeState) + { + throw new NotImplementedException(); + } + } + + #endregion + } \ No newline at end of file