|
|
|
@ -5,176 +5,190 @@ using QFramework;
@@ -5,176 +5,190 @@ 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(); |
|
|
|
|
//任何通讯类围绕DataEventModel进行 |
|
|
|
|
public abstract class DataEventModel : AbstractModel, ICanSendCommand, ICanRegisterEvent |
|
|
|
|
{ |
|
|
|
|
public IOCContainer mCommandContainer = new IOCContainer(); |
|
|
|
|
public UnityStringEvent onDataRecived = new UnityStringEvent(); |
|
|
|
|
|
|
|
|
|
public void sendResponseCommand<T>(string json) where T : IResponse, new() |
|
|
|
|
{ |
|
|
|
|
if (mCommandContainer.Get<ExcuteResponseCommand<T>>() == null) |
|
|
|
|
{ |
|
|
|
|
mCommandContainer.Register(new ExcuteResponseCommand<T>(new T())); |
|
|
|
|
} |
|
|
|
|
mCommandContainer.Get<ExcuteResponseCommand<T>>().setJson(json); |
|
|
|
|
this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>()); |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 开启接收指定数据 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="T">数据格式类型</typeparam> |
|
|
|
|
public void onReceive<T>() where T : IResponse, new() |
|
|
|
|
{ |
|
|
|
|
onDataRecived.AddListener(sendResponseCommand<T>); |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 关闭接收指定数据 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="T">数据格式类型</typeparam> |
|
|
|
|
public void offReceive<T>() where T : IResponse, new() |
|
|
|
|
{ |
|
|
|
|
onDataRecived.RemoveListener(sendResponseCommand<T>); |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 发送数据 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="T">数据类型</typeparam> |
|
|
|
|
/// <param name="request">请求数据</param> |
|
|
|
|
public void sendRequestCommand<T>(T request) where T : IRequest |
|
|
|
|
|
|
|
|
|
public void excuteResponseCommand<T>(string json) where T : IResponse, new() |
|
|
|
|
{ |
|
|
|
|
if (mCommandContainer.Get<ExcuteResponseCommand<T>>() == null) |
|
|
|
|
{ |
|
|
|
|
if (mCommandContainer.Get<SendRequestCommand<T>>() == null) |
|
|
|
|
{ |
|
|
|
|
mCommandContainer.Register(new SendRequestCommand<T>(request)); |
|
|
|
|
} |
|
|
|
|
mCommandContainer.Get<SendRequestCommand<T>>().setRequest(request); |
|
|
|
|
this.SendCommand(mCommandContainer.Get<SendRequestCommand<T>>()); |
|
|
|
|
mCommandContainer.Register(new ExcuteResponseCommand<T>(new T())); |
|
|
|
|
} |
|
|
|
|
mCommandContainer.Get<ExcuteResponseCommand<T>>().setJson(json); |
|
|
|
|
this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#region Command |
|
|
|
|
/// <summary> |
|
|
|
|
/// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 |
|
|
|
|
/// 开启接收指定数据 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="TResponse">响应数据格式</typeparam> |
|
|
|
|
public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse |
|
|
|
|
/// <typeparam name="T">数据格式类型</typeparam> |
|
|
|
|
public void onReceive<T>() where T : IResponse, new() |
|
|
|
|
{ |
|
|
|
|
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) |
|
|
|
|
onDataRecived.AddListener(excuteResponseCommand<T>); |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 关闭接收指定数据 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="T">数据格式类型</typeparam> |
|
|
|
|
public void offReceive<T>() where T : IResponse, new() |
|
|
|
|
{ |
|
|
|
|
onDataRecived.RemoveListener(excuteResponseCommand<T>); |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 发送数据 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="T">数据类型</typeparam> |
|
|
|
|
/// <param name="request">请求数据</param> |
|
|
|
|
public void sendRequestCommand<T>(T request) where T : IRequest |
|
|
|
|
{ |
|
|
|
|
if (mCommandContainer.Get<SendRequestCommand<T>>() == null) |
|
|
|
|
{ |
|
|
|
|
this.json = json; |
|
|
|
|
mCommandContainer.Register(new SendRequestCommand<T>(request)); |
|
|
|
|
} |
|
|
|
|
mCommandContainer.Get<SendRequestCommand<T>>().setRequest(request); |
|
|
|
|
this.SendCommand(mCommandContainer.Get<SendRequestCommand<T>>()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public class SendRequestCommand<TRequest> : AbstractCommand where TRequest : IRequest |
|
|
|
|
#region Command |
|
|
|
|
/// <summary> |
|
|
|
|
/// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="TResponse">响应数据格式</typeparam> |
|
|
|
|
public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse |
|
|
|
|
{ |
|
|
|
|
public string json; |
|
|
|
|
private TResponse response; |
|
|
|
|
public ExcuteResponseCommand(TResponse response) |
|
|
|
|
{ |
|
|
|
|
public TRequest request; |
|
|
|
|
public SendRequestCommand(TRequest request) |
|
|
|
|
{ |
|
|
|
|
this.request = request; |
|
|
|
|
} |
|
|
|
|
protected override void OnExecute() |
|
|
|
|
if (this.response == null) |
|
|
|
|
{ |
|
|
|
|
if (this.GetUtility<TCPUtility>().isOpenTCP) |
|
|
|
|
{ |
|
|
|
|
this.GetUtility<TCPUtility>().sendData(request.toJson()); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
Debug.LogWarning("请先开启TCP链接"); |
|
|
|
|
} |
|
|
|
|
//throw new NotImplementedException(); |
|
|
|
|
this.response = response; |
|
|
|
|
} |
|
|
|
|
public void setRequest(TRequest request) |
|
|
|
|
} |
|
|
|
|
protected override void OnExecute() |
|
|
|
|
{ |
|
|
|
|
bool isSet = response.trySetData(json); |
|
|
|
|
if (isSet) |
|
|
|
|
{ |
|
|
|
|
this.request = request; |
|
|
|
|
Debug.Log("Received:" + response.toJson() + response.GetType()); |
|
|
|
|
this.SendEvent(new ResponseMsgEvent(response)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region interface |
|
|
|
|
public interface IResponse |
|
|
|
|
public void setJson(string json) |
|
|
|
|
{ |
|
|
|
|
string toJson(); |
|
|
|
|
/// <summary> |
|
|
|
|
/// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="json"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
bool trySetData(string json); |
|
|
|
|
string getException(); |
|
|
|
|
this.json = json; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public interface IRequest |
|
|
|
|
public class SendRequestCommand<TRequest> : AbstractCommand where TRequest : IRequest |
|
|
|
|
{ |
|
|
|
|
public TRequest request; |
|
|
|
|
public SendRequestCommand(TRequest request) |
|
|
|
|
{ |
|
|
|
|
string toJson(); |
|
|
|
|
this.request = request; |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
protected override void OnExecute() |
|
|
|
|
{ |
|
|
|
|
this.SendEvent(new RequestMsgEvent(request)); |
|
|
|
|
} |
|
|
|
|
public void setRequest(TRequest request) |
|
|
|
|
{ |
|
|
|
|
this.request = request; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region AbstractClass |
|
|
|
|
#region interface |
|
|
|
|
public interface IResponse |
|
|
|
|
{ |
|
|
|
|
string toJson(); |
|
|
|
|
/// <summary> |
|
|
|
|
/// 使用抽象类时,必须满足可序列化 |
|
|
|
|
/// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应 |
|
|
|
|
/// </summary> |
|
|
|
|
[Serializable] |
|
|
|
|
public abstract class AbstractResponse : IResponse |
|
|
|
|
/// <param name="json"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
bool trySetData(string json); |
|
|
|
|
string getException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public interface IRequest |
|
|
|
|
{ |
|
|
|
|
string toJson(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//用于标准化通讯方案(具体链接管理,由外部类决定,本接口只提供开启链接跟关闭链接) |
|
|
|
|
public interface IProtocol { |
|
|
|
|
void linkServer(); |
|
|
|
|
void closeServer(); |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region Extention |
|
|
|
|
/* |
|
|
|
|
public static class CanGetStateExtention { |
|
|
|
|
public static void GetState<TLinkStatus>(this IProtocol self) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region AbstractClass |
|
|
|
|
/// <summary> |
|
|
|
|
/// 使用抽象类时,必须满足可序列化 |
|
|
|
|
/// </summary> |
|
|
|
|
[Serializable] |
|
|
|
|
public abstract class AbstractResponse : IResponse |
|
|
|
|
{ |
|
|
|
|
private string exceptionMsg; |
|
|
|
|
public virtual string toJson() |
|
|
|
|
{ |
|
|
|
|
private string exceptionMsg; |
|
|
|
|
public virtual string toJson() |
|
|
|
|
{ |
|
|
|
|
return JsonUtility.ToJson(this, true); |
|
|
|
|
} |
|
|
|
|
public virtual bool trySetData(string json) |
|
|
|
|
return JsonUtility.ToJson(this, true); |
|
|
|
|
} |
|
|
|
|
public virtual bool trySetData(string json) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
JsonUtility.FromJsonOverwrite(json, this); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
catch (Exception e) |
|
|
|
|
{ |
|
|
|
|
exceptionMsg = e.ToString(); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
JsonUtility.FromJsonOverwrite(json, this); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
public string getException() |
|
|
|
|
catch (Exception e) |
|
|
|
|
{ |
|
|
|
|
return exceptionMsg; |
|
|
|
|
exceptionMsg = e.ToString(); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region event |
|
|
|
|
public struct ResponseMsgEvent |
|
|
|
|
{ |
|
|
|
|
public IResponse res; |
|
|
|
|
public ResponseMsgEvent(IResponse res_) |
|
|
|
|
{ |
|
|
|
|
res = res_; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
#endregion |
|
|
|
|
[Serializable] |
|
|
|
|
public class UnityStringEvent : UnityEvent<string> |
|
|
|
|
public string getException() |
|
|
|
|
{ |
|
|
|
|
return exceptionMsg; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#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_; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
[Serializable] |
|
|
|
|
public class UnityStringEvent : UnityEvent<string> |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
} |