Browse Source

基础类新增状态模式,编写了链接状态转换,注册连接状态两个方法

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
18cefe782f
  1. 22
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs
  2. 147
      Assets/MsgTransmitTools/src/DataEventModel.cs

22
Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs

@ -1,5 +1,6 @@
using UnityEngine; using UnityEngine;
using QFrameworkCP; using QFrameworkCP;
using JXSoft;
using System; using System;
using System.Text; using System.Text;
using System.Net.Sockets; using System.Net.Sockets;
@ -111,18 +112,6 @@ namespace JXSoft
LinkTimeOut = 4 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 #endregion
#region event #region event
@ -299,4 +288,13 @@ namespace JXSoft
} }
} }
#endregion #endregion
}
namespace TCPLinkState {
public class StateNoIp : BaseLinkState.StateNoLink {
public override bool linkToServer()
{
return false;
}
}
} }

147
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using JXSoft;
using QFrameworkCP; using QFrameworkCP;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
@ -13,7 +14,7 @@ namespace JXSoft {
//管理消息收发响应控制 //管理消息收发响应控制
public IOCContainer mCommandContainer = new IOCContainer(); public IOCContainer mCommandContainer = new IOCContainer();
//管理连接状态响应 //管理连接状态响应
public IOCContainer mLinkStateContainer = 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;
@ -68,30 +69,39 @@ namespace JXSoft {
public abstract void setLinkState(int linkState); public abstract void setLinkState(int linkState);
public abstract int getLinkState(); public abstract int getLinkState();
public void invokeLinkState<TLinkState>(int invokeState) where TLinkState : ILinkState ,new(){ 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()
public void invokeLinkState<TLinkState>(TLinkState linkState,int invokeState) where TLinkState : ILinkState, new()
{ {
TLinkState linkState = new TLinkState();
transfromLinkState(linkState);
} }
public void registerLinkState(ILinkState 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<TLinkState>() where TLinkState:ILinkState;
//public abstract void registerLinkState();
#endregion #endregion
} }
#region enum
public enum LinkStateDefault {
NoLink = 0,
LinkSuccess = 1,
LinkFaild = 2
}
#endregion
#region Command #region Command
/// <summary> /// <summary>
@ -176,10 +186,14 @@ namespace JXSoft {
public interface ILinkState { public interface ILinkState {
//获取当前链接状态 //当前链接状态
int getLinkState(); int getLinkState();
//执行当前链接 /// <summary>
bool invoke(int invokeState); /// 转换到xx状态
/// </summary>
/// <param name="invokeState">目标链接状态</param>
/// <returns>是否成功</returns>
bool transform(ILinkState invokeState);
} }
#endregion #endregion
@ -216,19 +230,7 @@ namespace JXSoft {
} }
#endregion #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 #region event
public struct ResponseMsgEvent public struct ResponseMsgEvent
@ -260,4 +262,85 @@ 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
} }
Loading…
Cancel
Save