using System; using System.Collections; using System.Collections.Generic; using JXSoft; using QFrameworkCP; using UnityEngine; using UnityEngine.Events; namespace JXSoft { //任何通讯类围绕DataEventModel进行 public abstract class DataEventModel : AbstractModel { //管理消息收发响应控制 public IOCContainer mCommandContainer = new IOCContainer(); //消息接收响应 public UnityStringEvent onDataRecived = new UnityStringEvent(); //存放颜色信息(打印结果用) public Color printColor = Color.white; #region ReceiveFunctions public void receiveResponse(string json) where T : IResponse, new() { //如果找不到对应命令,则添加命令 if (mCommandContainer.Get>() == null) { mCommandContainer.Register(new ExcuteResponseCommand(new T())); } //找到命令后添加命令 mCommandContainer.Get>().setJson(json); this.SendCommand(mCommandContainer.Get>()); } /// /// 开启接收指定数据 /// /// 数据格式类型 public void onReceive() where T : IResponse, new() { onDataRecived.AddListener(receiveResponse); } /// /// 关闭接收指定数据 /// /// 数据格式类型 public void offReceive() where T : IResponse, new() { onDataRecived.RemoveListener(receiveResponse); } #endregion #region RequestFunctions /// /// sendDataRequest,data must be implements IRequest /// /// RequestType /// Data public void sendRequest(T request) where T : IRequest { if (mCommandContainer.Get>() == null) { mCommandContainer.Register(new SendRequestCommand(request)); } mCommandContainer.Get>().setRequest(request); this.SendCommand(mCommandContainer.Get>()); } #endregion #region LinkStatusManage public abstract void setLinkState(int linkState); public abstract int getLinkState(); /// /// Start a link & server, you can override by you own fucntions /// /// is link success & server start success public virtual bool Start() { setLinkState((int)LinkStateDefault.Open); return true; } public virtual bool Close() { setLinkState((int)LinkStateDefault.Close); return true; } #endregion } public enum LinkStateDefault { Close = 0, Open = 1 } #region Command /// /// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 /// /// 响应数据格式 public class ExcuteResponseCommand : AbstractCommand where TResponse : IResponse { public string json; private TResponse response; private Color printColor = Color.white; public ExcuteResponseCommand(TResponse response) { if (this.response == null) { this.response = response; } } protected override void OnExecute() { bool isSet = response.trySetData(json); if (isSet) { Debug.Log("Received:" + response.toProtocolData() + response.GetType()); this.SendEvent(new ResponseMsgEvent(response)); } } public void setJson(string json) { this.json = json; } } /// /// 发送数据 /// /// 发送数据格式 public class SendRequestCommand : AbstractCommand where TRequest : IRequest { public TRequest request; public SendRequestCommand(TRequest request) { this.request = request; } protected override void OnExecute() { this.SendEvent(new RequestMsgEvent(request)); } public void setRequest(TRequest request) { this.request = request; } } #endregion #region interface public interface IResponse { /// /// 此处用于截获接收到的数据,建议作为调试依据 /// /// string toProtocolData(); /// /// 尝试填充数据,如果不符合相关协议规则,建议返回false,如果此处返回为false,则忽略该条数据响应。 /// /// 协议数据格式 /// bool trySetData(string protocolData); /// /// 如果接收到信息定性为异常信息,获取异常信息 /// /// 异常信息结果 string getException(); } public interface IRequest { //按照协议定义发送格式,转换为字符串发送 string toProtocolData(); } #endregion #region AbstractClass /// /// 使用抽象类时,必须满足可序列化 /// [Serializable] public abstract class AbstractJsonResponse : IResponse { private string exceptionMsg; public virtual string toProtocolData() { return JsonUtility.ToJson(this, true); } public virtual bool trySetData(string json) { try { JsonUtility.FromJsonOverwrite(json, this); return true; } catch (Exception e) { exceptionMsg = e.ToString(); return false; } } public string getException() { return exceptionMsg; } } public abstract class AbstractJsonRequest : IRequest{ public string toProtocolData() { return JsonUtility.ToJson(this, true); } } public class StringRequest : IRequest { public string requestStr; public StringRequest() { requestStr = "No message"; } public StringRequest(string str) { requestStr = str; } public string toProtocolData() { return requestStr; } } #endregion #region Extention public static class CanRegistMessageExtention { public static IUnRegister RegisterMessageEvent(this ICanRegisterEvent self, Action onEvent)where TResponse : IResponse { return self.GetArchitecture().RegisterEvent(e=> { if (e.res.GetType() == typeof(TResponse)) { onEvent.Invoke((TResponse)e.res); } }); } } 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 public struct ResponseMsgEvent { public IResponse res; public ResponseMsgEvent(IResponse res_) { res = res_; } }; public struct RequestMsgEvent { public IRequest req; public RequestMsgEvent(IRequest req_) { req = req_; } } public struct LinkStateChangedEvent { public int linkState; public LinkStateChangedEvent(int linkState_) { linkState = linkState_; } } #endregion [Serializable] public class UnityStringEvent : UnityEvent { } }