DESKTOP-B25GA9E\W35
2 years ago
10 changed files with 377 additions and 370 deletions
@ -1,57 +1,54 @@ |
|||||||
using System; |
using System.Threading; |
||||||
using System.Collections; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.Net; |
|
||||||
using System.Net.Sockets; |
|
||||||
using System.Text; |
|
||||||
using System.Threading; |
|
||||||
using UnityEngine; |
using UnityEngine; |
||||||
using UnityEngine.UI; |
using UnityEngine.UI; |
||||||
using QFramework; |
using QFrameworkCP; |
||||||
|
|
||||||
public class UDPPrinter : MonoBehaviour, IController, ICanSendEvent, ICanGetUtility |
|
||||||
{ |
|
||||||
private UDPUtility udpUtil; |
|
||||||
public Transform udpMsgContent; |
|
||||||
public GameObject udpMsgItem; |
|
||||||
|
|
||||||
public InputField InputSendMsg; |
namespace JXSoft { |
||||||
private bool isUDPInit; |
public class UDPPrinter : MonoBehaviour, IController |
||||||
// Start is called before the first frame update |
|
||||||
void Start() |
|
||||||
{ |
{ |
||||||
udpUtil = GetArchitecture().GetUtility<UDPUtility>(); |
private UDPUtility udpUtil; |
||||||
} |
public Transform udpMsgContent; |
||||||
|
public GameObject udpMsgItem; |
||||||
|
|
||||||
// Update is called once per frame |
public InputField InputSendMsg; |
||||||
void Update() |
private bool isUDPInit; |
||||||
{ |
// Start is called before the first frame update |
||||||
if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue())) |
void Start() |
||||||
{ |
{ |
||||||
GameObject item = Instantiate(udpMsgItem, udpMsgContent); |
udpUtil = GetArchitecture().GetUtility<UDPUtility>(); |
||||||
item.GetComponentInChildren<Text>().text = udpUtil.receivedData; |
|
||||||
this.GetModel<UDPEventModel>().onDataRecived.Invoke(udpUtil.receivedData); |
|
||||||
} |
} |
||||||
|
|
||||||
if (udpUtil.getTimeOutState() && udpUtil.isOpenUDP) |
// Update is called once per frame |
||||||
|
void Update() |
||||||
{ |
{ |
||||||
this.SendEvent(new onUDPLinkException(udpUtil.exceptionData)); |
if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue())) |
||||||
udpUtil.isOpenUDP = false; |
{ |
||||||
|
GameObject item = Instantiate(udpMsgItem, udpMsgContent); |
||||||
|
item.GetComponentInChildren<Text>().text = udpUtil.receivedData; |
||||||
|
this.GetModel<UDPEventModel>().onDataRecived.Invoke(udpUtil.receivedData); |
||||||
|
} |
||||||
|
|
||||||
|
if (udpUtil.getTimeOutState() && udpUtil.isOpenUDP) |
||||||
|
{ |
||||||
|
this.SendEvent(new onUDPLinkException(udpUtil.exceptionData)); |
||||||
|
udpUtil.isOpenUDP = false; |
||||||
|
} |
||||||
} |
} |
||||||
} |
private void OnDestroy() |
||||||
private void OnDestroy() |
|
||||||
{ |
|
||||||
if (udpUtil.reciveT != null && udpUtil.reciveT.ThreadState == ThreadState.Running) |
|
||||||
{ |
{ |
||||||
udpUtil.reciveT.Abort(); |
if (udpUtil.reciveT != null && udpUtil.reciveT.ThreadState == ThreadState.Running) |
||||||
|
{ |
||||||
|
udpUtil.reciveT.Abort(); |
||||||
|
} |
||||||
|
} |
||||||
|
public void sendMsg() |
||||||
|
{ |
||||||
|
this.GetUtility<UDPUtility>().sendData(InputSendMsg.text); |
||||||
|
} |
||||||
|
public IArchitecture GetArchitecture() |
||||||
|
{ |
||||||
|
return UDPMangerArchitecture.Interface; |
||||||
} |
} |
||||||
} |
|
||||||
public void sendMsg() |
|
||||||
{ |
|
||||||
this.GetUtility<UDPUtility>().sendData(InputSendMsg.text); |
|
||||||
} |
|
||||||
public IArchitecture GetArchitecture() |
|
||||||
{ |
|
||||||
return UDPMangerArchitecture.Interface; |
|
||||||
} |
} |
||||||
} |
} |
@ -1,188 +1,190 @@ |
|||||||
using System; |
using System; |
||||||
using System.Collections; |
using System.Collections; |
||||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||||
using QFramework; |
using QFrameworkCP; |
||||||
using UnityEngine; |
using UnityEngine; |
||||||
using UnityEngine.Events; |
using UnityEngine.Events; |
||||||
|
|
||||||
|
|
||||||
//任何通讯类围绕DataEventModel进行 |
namespace JXSoft { |
||||||
public abstract class DataEventModel : AbstractModel, ICanSendCommand, ICanRegisterEvent |
//任何通讯类围绕DataEventModel进行 |
||||||
{ |
public abstract class DataEventModel : AbstractModel |
||||||
public IOCContainer mCommandContainer = new IOCContainer(); |
|
||||||
public UnityStringEvent onDataRecived = new UnityStringEvent(); |
|
||||||
public Color printColor = Color.white; |
|
||||||
public void sendResponseCommand<T>(string json) where T : IResponse, new() |
|
||||||
{ |
{ |
||||||
//如果找不到对应命令,则添加命令 |
public IOCContainer mCommandContainer = new IOCContainer(); |
||||||
if (mCommandContainer.Get<ExcuteResponseCommand<T>>() == null) |
public UnityStringEvent onDataRecived = new UnityStringEvent(); |
||||||
|
public Color printColor = Color.white; |
||||||
|
public void sendResponseCommand<T>(string json) where T : IResponse, new() |
||||||
{ |
{ |
||||||
mCommandContainer.Register(new ExcuteResponseCommand<T>(new T())); |
//如果找不到对应命令,则添加命令 |
||||||
|
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>>()); |
||||||
} |
} |
||||||
//找到命令后添加命令 |
|
||||||
mCommandContainer.Get<ExcuteResponseCommand<T>>().setJson(json); |
|
||||||
this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>()); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
/// <summary> |
||||||
/// 开启接收指定数据 |
/// 开启接收指定数据 |
||||||
/// </summary> |
/// </summary> |
||||||
/// <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(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 |
||||||
|
{ |
||||||
|
if (mCommandContainer.Get<SendRequestCommand<T>>() == null) |
||||||
|
{ |
||||||
|
mCommandContainer.Register(new SendRequestCommand<T>(request)); |
||||||
|
} |
||||||
|
mCommandContainer.Get<SendRequestCommand<T>>().setRequest(request); |
||||||
|
this.SendCommand(mCommandContainer.Get<SendRequestCommand<T>>()); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
|
#region Command |
||||||
/// <summary> |
/// <summary> |
||||||
/// 关闭接收指定数据 |
/// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 |
||||||
/// </summary> |
/// </summary> |
||||||
/// <typeparam name="T">数据格式类型</typeparam> |
/// <typeparam name="TResponse">响应数据格式</typeparam> |
||||||
public void offReceive<T>() where T : IResponse, new() |
public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse |
||||||
{ |
{ |
||||||
onDataRecived.RemoveListener(sendResponseCommand<T>); |
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.toJson() + response.GetType()); |
||||||
|
this.SendEvent(new ResponseMsgEvent(response)); |
||||||
|
} |
||||||
|
} |
||||||
|
public void setJson(string json) |
||||||
|
{ |
||||||
|
this.json = json; |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
/// <summary> |
/// <summary> |
||||||
/// 发送数据 |
/// 发送数据 |
||||||
/// </summary> |
/// </summary> |
||||||
/// <typeparam name="T">数据类型</typeparam> |
/// <typeparam name="TRequest">发送数据格式</typeparam> |
||||||
/// <param name="request">请求数据</param> |
public class SendRequestCommand<TRequest> : AbstractCommand where TRequest : IRequest |
||||||
public void sendRequestCommand<T>(T request) where T : IRequest |
|
||||||
{ |
{ |
||||||
if (mCommandContainer.Get<SendRequestCommand<T>>() == null) |
public TRequest request; |
||||||
|
public SendRequestCommand(TRequest request) |
||||||
{ |
{ |
||||||
mCommandContainer.Register(new SendRequestCommand<T>(request)); |
this.request = request; |
||||||
} |
} |
||||||
mCommandContainer.Get<SendRequestCommand<T>>().setRequest(request); |
protected override void OnExecute() |
||||||
this.SendCommand(mCommandContainer.Get<SendRequestCommand<T>>()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
#region Command |
|
||||||
/// <summary> |
|
||||||
/// 处理响应数据,添加对应的Command进行处理,需要提前生成命令池 |
|
||||||
/// </summary> |
|
||||||
/// <typeparam name="TResponse">响应数据格式</typeparam> |
|
||||||
public class ExcuteResponseCommand<TResponse> : 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; |
this.SendEvent(new RequestMsgEvent(request)); |
||||||
} |
} |
||||||
} |
public void setRequest(TRequest request) |
||||||
protected override void OnExecute() |
|
||||||
{ |
|
||||||
bool isSet = response.trySetData(json); |
|
||||||
if (isSet) |
|
||||||
{ |
{ |
||||||
Debug.Log("Received:" + response.toJson() + response.GetType()); |
this.request = request; |
||||||
this.SendEvent(new ResponseMsgEvent(response)); |
|
||||||
} |
} |
||||||
} |
} |
||||||
public void setJson(string json) |
#endregion |
||||||
{ |
|
||||||
this.json = json; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
#region interface |
||||||
/// 发送数据 |
public interface IResponse |
||||||
/// </summary> |
|
||||||
/// <typeparam name="TRequest">发送数据格式</typeparam> |
|
||||||
public class SendRequestCommand<TRequest> : AbstractCommand where TRequest : IRequest |
|
||||||
{ |
|
||||||
public TRequest request; |
|
||||||
public SendRequestCommand(TRequest request) |
|
||||||
{ |
|
||||||
this.request = request; |
|
||||||
} |
|
||||||
protected override void OnExecute() |
|
||||||
{ |
{ |
||||||
this.SendEvent(new RequestMsgEvent(request)); |
string toJson(); |
||||||
|
/// <summary> |
||||||
|
/// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应 |
||||||
|
/// </summary> |
||||||
|
/// <param name="json"></param> |
||||||
|
/// <returns></returns> |
||||||
|
bool trySetData(string json); |
||||||
|
string getException(); |
||||||
} |
} |
||||||
public void setRequest(TRequest request) |
|
||||||
|
public interface IRequest |
||||||
{ |
{ |
||||||
this.request = request; |
string toJson(); |
||||||
} |
} |
||||||
} |
#endregion |
||||||
#endregion |
|
||||||
|
|
||||||
#region interface |
#region AbstractClass |
||||||
public interface IResponse |
|
||||||
{ |
|
||||||
string toJson(); |
|
||||||
/// <summary> |
/// <summary> |
||||||
/// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应 |
/// 使用抽象类时,必须满足可序列化 |
||||||
/// </summary> |
/// </summary> |
||||||
/// <param name="json"></param> |
[Serializable] |
||||||
/// <returns></returns> |
public abstract class AbstractResponse : IResponse |
||||||
bool trySetData(string json); |
|
||||||
string getException(); |
|
||||||
} |
|
||||||
|
|
||||||
public interface IRequest |
|
||||||
{ |
|
||||||
string toJson(); |
|
||||||
} |
|
||||||
#endregion |
|
||||||
|
|
||||||
#region AbstractClass |
|
||||||
/// <summary> |
|
||||||
/// 使用抽象类时,必须满足可序列化 |
|
||||||
/// </summary> |
|
||||||
[Serializable] |
|
||||||
public abstract class AbstractResponse : IResponse |
|
||||||
{ |
|
||||||
private string exceptionMsg; |
|
||||||
public virtual string toJson() |
|
||||||
{ |
{ |
||||||
return JsonUtility.ToJson(this, true); |
private string exceptionMsg; |
||||||
} |
public virtual string toJson() |
||||||
public virtual bool trySetData(string json) |
|
||||||
{ |
|
||||||
try |
|
||||||
{ |
{ |
||||||
JsonUtility.FromJsonOverwrite(json, this); |
return JsonUtility.ToJson(this, true); |
||||||
return true; |
|
||||||
} |
} |
||||||
catch (Exception e) |
public virtual bool trySetData(string json) |
||||||
{ |
{ |
||||||
exceptionMsg = e.ToString(); |
try |
||||||
return false; |
{ |
||||||
|
JsonUtility.FromJsonOverwrite(json, this); |
||||||
|
return true; |
||||||
|
} |
||||||
|
catch (Exception e) |
||||||
|
{ |
||||||
|
exceptionMsg = e.ToString(); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
public string getException() |
||||||
|
{ |
||||||
|
return exceptionMsg; |
||||||
} |
} |
||||||
} |
} |
||||||
public string getException() |
#endregion |
||||||
{ |
|
||||||
return exceptionMsg; |
|
||||||
} |
|
||||||
} |
|
||||||
#endregion |
|
||||||
|
|
||||||
#region event |
#region event |
||||||
public struct ResponseMsgEvent |
public struct ResponseMsgEvent |
||||||
{ |
|
||||||
public IResponse res; |
|
||||||
public ResponseMsgEvent(IResponse res_) |
|
||||||
{ |
{ |
||||||
res = res_; |
public IResponse res; |
||||||
} |
public ResponseMsgEvent(IResponse res_) |
||||||
}; |
{ |
||||||
public struct RequestMsgEvent |
res = res_; |
||||||
{ |
} |
||||||
public IRequest req; |
}; |
||||||
public RequestMsgEvent(IRequest req_) |
public struct RequestMsgEvent |
||||||
{ |
{ |
||||||
req = req_; |
public IRequest req; |
||||||
|
public RequestMsgEvent(IRequest req_) |
||||||
|
{ |
||||||
|
req = req_; |
||||||
|
} |
||||||
} |
} |
||||||
} |
#endregion |
||||||
#endregion |
|
||||||
|
|
||||||
[Serializable] |
[Serializable] |
||||||
public class UnityStringEvent : UnityEvent<string> |
public class UnityStringEvent : UnityEvent<string> |
||||||
{ |
{ |
||||||
|
|
||||||
|
} |
||||||
} |
} |
Loading…
Reference in new issue