using System; using System.Collections; using System.Collections.Generic; using QFramework; using UnityEngine; using UnityEngine.Events; namespace TCPClientTools { //任何通讯类围绕DataEventModel进行 public abstract class DataEventModel : AbstractModel, ICanSendCommand, ICanRegisterEvent { public IOCContainer mCommandContainer = new IOCContainer(); public UnityStringEvent onDataRecived = new UnityStringEvent(); public void sendResponseCommand(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(sendResponseCommand); } /// /// 关闭接收指定数据 /// /// 数据格式类型 public void offReceive() where T : IResponse, new() { onDataRecived.RemoveListener(sendResponseCommand); } /// /// 发送数据 /// /// 数据类型 /// 请求数据 public void sendRequestCommand(T request) where T : IRequest { if (mCommandContainer.Get>() == null) { mCommandContainer.Register(new SendRequestCommand(request)); } mCommandContainer.Get>().setRequest(request); this.SendCommand(mCommandContainer.Get>()); } } #region Command /// /// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 /// /// 响应数据格式 public class ExcuteResponseCommand : AbstractCommand where TResponse : IResponse { public string json; private TResponse response; 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.toJson() + 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() { if (this.GetUtility().isOpenTCP) { this.GetUtility().sendData(request.toJson()); } else { Debug.LogWarning("请先开启TCP链接"); } //throw new NotImplementedException(); } public void setRequest(TRequest request) { this.request = request; } } #endregion #region interface public interface IResponse { string toJson(); /// /// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应 /// /// /// bool trySetData(string json); string getException(); } public interface IRequest { string toJson(); } #endregion #region AbstractClass /// /// 使用抽象类时,必须满足可序列化 /// [Serializable] public abstract class AbstractResponse : IResponse { private string exceptionMsg; public virtual string toJson() { 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; } } #endregion #region event public struct ResponseMsgEvent { public IResponse res; public ResponseMsgEvent(IResponse res_) { res = res_; } }; #endregion [Serializable] public class UnityStringEvent : UnityEvent { } }