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