From b2feb0022f763840b8e23fb30518956328c3657c Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Fri, 3 Mar 2023 17:07:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E5=87=8F=E8=BF=9E=E6=8E=A5=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=EF=BC=8C=E4=BF=9D=E7=95=99Start=E8=B7=9FClose?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E9=80=9A=E7=94=A8=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpServer/HttpServerModel.cs | 38 ++-- .../HttpServer/HttpServerView.cs | 1 + .../TCPClient/Script/TCPClientModel.cs | 9 - Assets/MsgTransmitTools/src/DataEventModel.cs | 168 ++++-------------- 4 files changed, 58 insertions(+), 158 deletions(-) diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs index 4c46663..9b3fa91 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs @@ -11,26 +11,34 @@ using System.IO; namespace JXSoft { public class HttpServerModel : DataEventModel { - public ServerState httpServerState = ServerState.Close; + public LinkStateDefault httpServerState = LinkStateDefault.Close; protected override void OnInit() { } - public override void setLinkState(int linkState) { - this.httpServerState = (ServerState)linkState; + this.httpServerState = (LinkStateDefault)linkState; } public override int getLinkState() { return (int)httpServerState; } - } - public enum ServerState - { - Close = 0, - Open = 1 + public override bool Start() + { + bool result = this.GetUtility().startServer(); + if (result) { + setLinkState((int)LinkStateDefault.Open); + } + return result; + } + public override bool Close() + { + this.GetUtility().closeServer(); + setLinkState((int)LinkStateDefault.Close); + return base.Close(); + } } public class HttpServerUtility:IUtility @@ -42,7 +50,6 @@ namespace JXSoft { public bool isReceivedValue = false; public string receivedData = ""; public string exceptionData = ""; - public Thread reciveT; #region OpenServer @@ -74,19 +81,24 @@ namespace JXSoft { } return false; } - public void startServer(string prefix) { + public bool startServer(string prefix) { string[] prefixes = {prefix}; - startServer(prefixes); + bool isSuccess = startServer(prefixes); + return isSuccess; } - public void startServer() + public bool startServer() { string[] prefixes = { address }; - startServer(prefixes); + bool isSuccess = startServer(prefixes); + return isSuccess; } #endregion #region CloseServer public void closeServer() { + if (httpServer == null) { + return; + } if (httpServer.IsListening) { reciveT.Abort(); httpServer.Close(); diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs index 2c622b8..7b137c6 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs @@ -23,6 +23,7 @@ namespace JXSoft { // Start is called before the first frame update void Start() { + //Debug.Log(int.Parse("AA",System.Globalization.NumberStyles.HexNumber).ToString()); this.httpServer = this.GetUtility(); this.RegisterMessageEvent(e => { Debug.Log("收到消息:"+e.ip); diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs b/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs index a058dda..1d287c3 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs @@ -287,13 +287,4 @@ 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 f11712c..305a1b9 100644 --- a/Assets/MsgTransmitTools/src/DataEventModel.cs +++ b/Assets/MsgTransmitTools/src/DataEventModel.cs @@ -13,9 +13,9 @@ namespace JXSoft { { //管理消息收发响应控制 public IOCContainer mCommandContainer = new IOCContainer(); - //管理连接状态响应 - public Dictionary mLinkStateContainer = new Dictionary(); + //消息接收响应 public UnityStringEvent onDataRecived = new UnityStringEvent(); + //存放颜色信息(打印结果用) public Color printColor = Color.white; #region ReceiveFunctions @@ -50,10 +50,10 @@ namespace JXSoft { #region RequestFunctions /// - /// 发送数据 + /// sendDataRequest,data must be implements IRequest /// - /// 数据类型 - /// 请求数据 + /// RequestType + /// Data public void sendRequest(T request) where T : IRequest { if (mCommandContainer.Get>() == null) @@ -69,38 +69,25 @@ namespace JXSoft { public abstract void setLinkState(int linkState); public abstract int getLinkState(); - 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 transfromLinkState() where TLinkState : ILinkState, new() - { - TLinkState linkState = new TLinkState(); - transfromLinkState(linkState); + /// + /// Start a link & server, you can overwrite by you own fucntions + /// + /// is link success & server start success + public virtual bool Start() { + return true; } - public void registerLinkState(ILinkState linkState) { - if (mLinkStateContainer.ContainsKey(linkState.getLinkState())) - { - mLinkStateContainer[linkState.getLinkState()] = linkState; - } - else { - mLinkStateContainer.Add(linkState.getLinkState(), linkState); - } + public virtual bool Close() { + return true; } #endregion } + public enum LinkStateDefault + { + Close = 0, + Open = 1 + } + #region Command /// /// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 @@ -182,19 +169,6 @@ namespace JXSoft { string toProtocolData(); } - public interface ILinkState { - //当前链接状态 - int getLinkState(); - /// - /// 转换到xx状态 - /// - /// 目标链接状态 - /// 是否成功 - bool transform(ILinkState invokeState); - - //DataEventModel getEventModel(); - } - #endregion #region AbstractClass @@ -231,7 +205,7 @@ namespace JXSoft { #region Extention public static class CanRegistMessageExtention { - public static IUnRegister RegisterMessageEvent(this ICanRegisterEvent self, Action onEvent) + public static IUnRegister RegisterMessageEvent(this ICanRegisterEvent self, Action onEvent)where TResponse : IResponse { return self.GetArchitecture().RegisterEvent(e=> { if (e.res.GetType() == typeof(TResponse)) { @@ -240,6 +214,18 @@ namespace JXSoft { }); } } + public static class CanRegistLinkStateExtention + { + public static IUnRegister RegisterLinkStateEvent(this ICanRegisterEvent self,int linkState, Action onEvent) + { + return self.GetArchitecture().RegisterEvent(e => { + if (e.linkState == linkState) + { + onEvent.Invoke(); + } + }); + } + } #endregion #region event @@ -272,94 +258,4 @@ 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 - -} - - -/// -/// this Package improved a base example of server open & close -/// you can also create class implements ILinkState. -/// -namespace BaseServerState { - } \ No newline at end of file