Browse Source

删减连接流程,保留Start跟Close两个通用函数

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
b2feb0022f
  1. 38
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs
  2. 1
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs
  3. 9
      Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs
  4. 168
      Assets/MsgTransmitTools/src/DataEventModel.cs

38
Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs

@ -11,26 +11,34 @@ using System.IO;
namespace JXSoft { namespace JXSoft {
public class HttpServerModel : DataEventModel public class HttpServerModel : DataEventModel
{ {
public ServerState httpServerState = ServerState.Close; public LinkStateDefault httpServerState = LinkStateDefault.Close;
protected override void OnInit() protected override void OnInit()
{ {
} }
public override void setLinkState(int linkState) public override void setLinkState(int linkState)
{ {
this.httpServerState = (ServerState)linkState; this.httpServerState = (LinkStateDefault)linkState;
} }
public override int getLinkState() public override int getLinkState()
{ {
return (int)httpServerState; return (int)httpServerState;
} }
} public override bool Start()
public enum ServerState {
{ bool result = this.GetUtility<HttpServerUtility>().startServer();
Close = 0, if (result) {
Open = 1 setLinkState((int)LinkStateDefault.Open);
}
return result;
}
public override bool Close()
{
this.GetUtility<HttpServerUtility>().closeServer();
setLinkState((int)LinkStateDefault.Close);
return base.Close();
}
} }
public class HttpServerUtility:IUtility public class HttpServerUtility:IUtility
@ -42,7 +50,6 @@ namespace JXSoft {
public bool isReceivedValue = false; public bool isReceivedValue = false;
public string receivedData = ""; public string receivedData = "";
public string exceptionData = ""; public string exceptionData = "";
public Thread reciveT; public Thread reciveT;
#region OpenServer #region OpenServer
@ -74,19 +81,24 @@ namespace JXSoft {
} }
return false; return false;
} }
public void startServer(string prefix) { public bool startServer(string prefix) {
string[] prefixes = {prefix}; string[] prefixes = {prefix};
startServer(prefixes); bool isSuccess = startServer(prefixes);
return isSuccess;
} }
public void startServer() public bool startServer()
{ {
string[] prefixes = { address }; string[] prefixes = { address };
startServer(prefixes); bool isSuccess = startServer(prefixes);
return isSuccess;
} }
#endregion #endregion
#region CloseServer #region CloseServer
public void closeServer() { public void closeServer() {
if (httpServer == null) {
return;
}
if (httpServer.IsListening) { if (httpServer.IsListening) {
reciveT.Abort(); reciveT.Abort();
httpServer.Close(); httpServer.Close();

1
Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs

@ -23,6 +23,7 @@ namespace JXSoft {
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
//Debug.Log(int.Parse("AA",System.Globalization.NumberStyles.HexNumber).ToString());
this.httpServer = this.GetUtility<HttpServerUtility>(); this.httpServer = this.GetUtility<HttpServerUtility>();
this.RegisterMessageEvent<DYData.BluetoothGatewayData>(e => { this.RegisterMessageEvent<DYData.BluetoothGatewayData>(e => {
Debug.Log("收到消息:"+e.ip); Debug.Log("收到消息:"+e.ip);

9
Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs

@ -287,13 +287,4 @@ namespace JXSoft
} }
} }
#endregion #endregion
}
namespace TCPLinkState {
public class StateNoIp : BaseLinkState.StateNoLink {
public override bool linkToServer()
{
return false;
}
}
} }

168
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -13,9 +13,9 @@ namespace JXSoft {
{ {
//管理消息收发响应控制 //管理消息收发响应控制
public IOCContainer mCommandContainer = new IOCContainer(); public IOCContainer mCommandContainer = new IOCContainer();
//管理连接状态响应 //消息接收响应
public Dictionary<int,ILinkState> mLinkStateContainer = new Dictionary<int,ILinkState>();
public UnityStringEvent onDataRecived = new UnityStringEvent(); public UnityStringEvent onDataRecived = new UnityStringEvent();
//存放颜色信息(打印结果用)
public Color printColor = Color.white; public Color printColor = Color.white;
#region ReceiveFunctions #region ReceiveFunctions
@ -50,10 +50,10 @@ namespace JXSoft {
#region RequestFunctions #region RequestFunctions
/// <summary> /// <summary>
/// 发送数据 /// sendDataRequest,data must be implements IRequest
/// </summary> /// </summary>
/// <typeparam name="T">数据类型</typeparam> /// <typeparam name="T">RequestType</typeparam>
/// <param name="request">请求数据</param> /// <param name="request">Data</param>
public void sendRequest<T>(T request) where T : IRequest public void sendRequest<T>(T request) where T : IRequest
{ {
if (mCommandContainer.Get<SendRequestCommand<T>>() == null) if (mCommandContainer.Get<SendRequestCommand<T>>() == null)
@ -69,38 +69,25 @@ namespace JXSoft {
public abstract void setLinkState(int linkState); public abstract void setLinkState(int linkState);
public abstract int getLinkState(); public abstract int getLinkState();
public void transfromLinkState<TLinkState>(TLinkState linkState) where TLinkState : ILinkState, new() /// <summary>
{ /// Start a link & server, you can overwrite by you own fucntions
if (mLinkStateContainer.ContainsKey(getLinkState())) /// </summary>
{ /// <returns>is link success & server start success</returns>
bool isSuccess = mLinkStateContainer[getLinkState()].transform(linkState); public virtual bool Start() {
if (isSuccess) return true;
{
this.setLinkState(linkState.getLinkState());
}
else
{
Debug.LogWarning("Transform Faild!");
}
}
}
public void transfromLinkState<TLinkState>() where TLinkState : ILinkState, new()
{
TLinkState linkState = new TLinkState();
transfromLinkState(linkState);
} }
public void registerLinkState(ILinkState linkState) { public virtual bool Close() {
if (mLinkStateContainer.ContainsKey(linkState.getLinkState())) return true;
{
mLinkStateContainer[linkState.getLinkState()] = linkState;
}
else {
mLinkStateContainer.Add(linkState.getLinkState(), linkState);
}
} }
#endregion #endregion
} }
public enum LinkStateDefault
{
Close = 0,
Open = 1
}
#region Command #region Command
/// <summary> /// <summary>
/// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 /// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池
@ -182,19 +169,6 @@ namespace JXSoft {
string toProtocolData(); string toProtocolData();
} }
public interface ILinkState {
//当前链接状态
int getLinkState();
/// <summary>
/// 转换到xx状态
/// </summary>
/// <param name="invokeState">目标链接状态</param>
/// <returns>是否成功</returns>
bool transform(ILinkState invokeState);
//DataEventModel getEventModel();
}
#endregion #endregion
#region AbstractClass #region AbstractClass
@ -231,7 +205,7 @@ namespace JXSoft {
#region Extention #region Extention
public static class CanRegistMessageExtention { public static class CanRegistMessageExtention {
public static IUnRegister RegisterMessageEvent<TResponse>(this ICanRegisterEvent self, Action<TResponse> onEvent) public static IUnRegister RegisterMessageEvent<TResponse>(this ICanRegisterEvent self, Action<TResponse> onEvent)where TResponse : IResponse
{ {
return self.GetArchitecture().RegisterEvent<ResponseMsgEvent>(e=> { return self.GetArchitecture().RegisterEvent<ResponseMsgEvent>(e=> {
if (e.res.GetType() == typeof(TResponse)) { 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<LinkStateChangedEvent>(e => {
if (e.linkState == linkState)
{
onEvent.Invoke();
}
});
}
}
#endregion #endregion
#region event #region event
@ -272,94 +258,4 @@ namespace JXSoft {
{ {
} }
}
/// <summary>
/// this Package improved a base example of link to server
/// you can also create class implements ILinkState.
/// </summary>
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
}
/// <summary>
/// this Package improved a base example of server open & close
/// you can also create class implements ILinkState.
/// </summary>
namespace BaseServerState {
} }
Loading…
Cancel
Save