|
|
|
@ -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,38 +69,25 @@ namespace JXSoft {
@@ -69,38 +69,25 @@ 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!"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
public void transfromLinkState<TLinkState>() where TLinkState : ILinkState, new() |
|
|
|
|
{ |
|
|
|
|
TLinkState linkState = new TLinkState(); |
|
|
|
|
transfromLinkState(linkState); |
|
|
|
|
/// <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 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 |
|
|
|
|
/// <summary> |
|
|
|
|
/// 处理响应数据,添加对应的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 { |
|
|
|
|
|
|
|
|
|
} |