Browse Source

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

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

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

@ -11,26 +11,34 @@ using System.IO; @@ -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 override bool Start()
{
bool result = this.GetUtility<HttpServerUtility>().startServer();
if (result) {
setLinkState((int)LinkStateDefault.Open);
}
return result;
}
public enum ServerState
public override bool Close()
{
Close = 0,
Open = 1
this.GetUtility<HttpServerUtility>().closeServer();
setLinkState((int)LinkStateDefault.Close);
return base.Close();
}
}
public class HttpServerUtility:IUtility
@ -42,7 +50,6 @@ namespace JXSoft { @@ -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 { @@ -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();

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

@ -23,6 +23,7 @@ namespace JXSoft { @@ -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<HttpServerUtility>();
this.RegisterMessageEvent<DYData.BluetoothGatewayData>(e => {
Debug.Log("收到消息:"+e.ip);

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

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

166
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -13,9 +13,9 @@ namespace JXSoft { @@ -13,9 +13,9 @@ namespace JXSoft {
{
//管理消息收发响应控制
public IOCContainer mCommandContainer = new IOCContainer();
//管理连接状态响应
public Dictionary<int,ILinkState> mLinkStateContainer = new Dictionary<int,ILinkState>();
//消息接收响应
public UnityStringEvent onDataRecived = new UnityStringEvent();
//存放颜色信息(打印结果用)
public Color printColor = Color.white;
#region ReceiveFunctions
@ -50,10 +50,10 @@ namespace JXSoft { @@ -50,10 +50,10 @@ namespace JXSoft {
#region RequestFunctions
/// <summary>
/// 发送数据
/// sendDataRequest,data must be implements IRequest
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="request">请求数据</param>
/// <typeparam name="T">RequestType</typeparam>
/// <param name="request">Data</param>
public void sendRequest<T>(T request) where T : IRequest
{
if (mCommandContainer.Get<SendRequestCommand<T>>() == null)
@ -69,36 +69,23 @@ namespace JXSoft { @@ -69,36 +69,23 @@ namespace JXSoft {
public abstract void setLinkState(int linkState);
public abstract int getLinkState();
public void transfromLinkState<TLinkState>(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!");
}
/// <summary>
/// Start a link & server, you can overwrite by you own fucntions
/// </summary>
/// <returns>is link success & server start success</returns>
public virtual bool Start() {
return true;
}
public virtual bool Close() {
return true;
}
public void transfromLinkState<TLinkState>() where TLinkState : ILinkState, new()
{
TLinkState linkState = new TLinkState();
transfromLinkState(linkState);
#endregion
}
public void registerLinkState(ILinkState linkState) {
if (mLinkStateContainer.ContainsKey(linkState.getLinkState()))
public enum LinkStateDefault
{
mLinkStateContainer[linkState.getLinkState()] = linkState;
}
else {
mLinkStateContainer.Add(linkState.getLinkState(), linkState);
}
}
#endregion
Close = 0,
Open = 1
}
#region Command
@ -182,19 +169,6 @@ namespace JXSoft { @@ -182,19 +169,6 @@ namespace JXSoft {
string toProtocolData();
}
public interface ILinkState {
//当前链接状态
int getLinkState();
/// <summary>
/// 转换到xx状态
/// </summary>
/// <param name="invokeState">目标链接状态</param>
/// <returns>是否成功</returns>
bool transform(ILinkState invokeState);
//DataEventModel getEventModel();
}
#endregion
#region AbstractClass
@ -231,7 +205,7 @@ namespace JXSoft { @@ -231,7 +205,7 @@ namespace JXSoft {
#region Extention
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=> {
if (e.res.GetType() == typeof(TResponse)) {
@ -240,6 +214,18 @@ namespace JXSoft { @@ -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
#region event
@ -273,93 +259,3 @@ namespace JXSoft { @@ -273,93 +259,3 @@ 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