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