Browse Source

更改QFrame的nameSpace

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
ef93cedfad
  1. 6
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs
  2. 4
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs
  3. 183
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs
  4. 6
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs
  5. 172
      Assets/MsgTransmitTools/UDPClient/UDPClientView.cs
  6. 7
      Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs
  7. 81
      Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs
  8. 2
      Assets/MsgTransmitTools/UDPClient/UDPUtility.cs
  9. 280
      Assets/MsgTransmitTools/src/DataEventModel.cs
  10. 6
      Assets/MsgTransmitTools/src/QFrameCopy.cs

6
Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs

@ -1,12 +1,12 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using QFramework; using QFrameworkCP;
namespace TCPClientTools namespace JXSoft
{ {
//统筹管理TCP的命令池以及相关链接信息 //统筹管理TCP的命令池以及相关链接信息
public class TCPEventModel : DataEventModel, ICanSendCommand,ICanRegisterEvent public class TCPEventModel : DataEventModel
{ {
private TCPLinkState tcpState = TCPLinkState.NoIp; private TCPLinkState tcpState = TCPLinkState.NoIp;
private string tcpAddress = ""; private string tcpAddress = "";

4
Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs

@ -1,4 +1,4 @@
using QFramework; using QFrameworkCP;
using UnityEngine; using UnityEngine;
using System; using System;
using System.Text; using System.Text;
@ -8,7 +8,7 @@ using System.Threading.Tasks;
using System.Threading; using System.Threading;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
namespace TCPClientTools namespace JXSoft
{ {
public class TCPUtility : IUtility public class TCPUtility : IUtility
{ {

183
Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs

@ -2,111 +2,114 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using QFramework; using QFrameworkCP;
using TCPClientTools; using JXSoft;
using System; using System;
public class TCPMangerArchitecture : Architecture<TCPMangerArchitecture>
{
protected override void Init()
{
this.RegisterUtility(new TCPUtility());
this.RegisterModel(new TCPEventModel());
}
}
public class TCPClientView : MonoBehaviour,IController,ICanSendEvent
{
public UnityEvent onRecievedOpenDevice;
public UnityEvent onTCPLinkSuccess;
public UnityEvent onTCPLinkFaild;
public UnityEvent onServerConnected;
public UnityEvent onTCPReLink;
private string tcpAddress; namespace JXSoft {
private int tcpPort; public class TCPMangerArchitecture : Architecture<TCPMangerArchitecture>
private int deviceId;
// Start is called before the first frame update
void Awake()
{ {
initTCPService(); protected override void Init()
DontDestroyOnLoad(this); {
this.RegisterUtility(new TCPUtility());
this.RegisterModel(new TCPEventModel());
}
} }
public class TCPClientView : MonoBehaviour,IController
{
public UnityEvent onRecievedOpenDevice;
public UnityEvent onTCPLinkSuccess;
public UnityEvent onTCPLinkFaild;
public UnityEvent onServerConnected;
public UnityEvent onTCPReLink;
public void initTCPService() { private string tcpAddress;
tcpAddress = "127.0.0.1"; private int tcpPort;
tcpPort = 20000; private int deviceId;
deviceId = 1; // Start is called before the first frame update
void Awake()
{
initTCPService();
DontDestroyOnLoad(this);
}
GetArchitecture().RegisterEvent<TCPStateChangedEvent>(e => { public void initTCPService() {
if (e.state == TCPLinkState.Linking) tcpAddress = "127.0.0.1";
{ tcpPort = 20000;
Debug.Log("TCP开始链接"); deviceId = 1;
this.GetModel<TCPEventModel>().linkServer();
}
if (e.state == TCPLinkState.LinkSucess)
{
Debug.Log("TCP链接成功");
this.GetModel<TCPEventModel>().onReceive<LinkSuccessResponse>();
this.GetModel<TCPEventModel>().sendRequestCommand(new LinkTCPRequest(deviceId));
onTCPLinkSuccess.Invoke();
}
if (e.state == TCPLinkState.LinkFaild)
{
Debug.Log("TCP连接失败,请联系设备服务管理员");
onTCPLinkFaild.Invoke();
}
});
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e => GetArchitecture().RegisterEvent<TCPStateChangedEvent>(e => {
{ if (e.state == TCPLinkState.Linking)
if (e.res.GetType() == typeof(LinkSuccessResponse)) {
Debug.Log("TCP开始链接");
this.GetModel<TCPEventModel>().linkServer();
}
if (e.state == TCPLinkState.LinkSucess)
{
Debug.Log("TCP链接成功");
this.GetModel<TCPEventModel>().onReceive<LinkSuccessResponse>();
this.GetModel<TCPEventModel>().sendRequestCommand(new LinkTCPRequest(deviceId));
onTCPLinkSuccess.Invoke();
}
if (e.state == TCPLinkState.LinkFaild)
{
Debug.Log("TCP连接失败,请联系设备服务管理员");
onTCPLinkFaild.Invoke();
}
});
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e =>
{ {
Debug.Log("Link Server success"); if (e.res.GetType() == typeof(LinkSuccessResponse))
this.GetModel<TCPEventModel>().offReceive<LinkSuccessResponse>(); {
onServerConnected.Invoke(); Debug.Log("Link Server success");
} this.GetModel<TCPEventModel>().offReceive<LinkSuccessResponse>();
}); onServerConnected.Invoke();
}
});
this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.NoIp); this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.NoIp);
this.GetModel<TCPEventModel>().setIP(tcpAddress, tcpPort); this.GetModel<TCPEventModel>().setIP(tcpAddress, tcpPort);
} }
public void restartTCPService() { public void restartTCPService() {
onTCPReLink.Invoke(); onTCPReLink.Invoke();
this.GetModel<TCPEventModel>().closeServer(); this.GetModel<TCPEventModel>().closeServer();
StartCoroutine(waitTwoSecond()); StartCoroutine(waitTwoSecond());
} }
public IEnumerator waitTwoSecond() { public IEnumerator waitTwoSecond() {
yield return new WaitForSeconds(2.0f); yield return new WaitForSeconds(2.0f);
this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.Linking); this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.Linking);
}
public IArchitecture GetArchitecture()
{
return TCPMangerArchitecture.Interface;
}
} }
public IArchitecture GetArchitecture() public class LinkSuccessResponse : AbstractResponse
{ {
return TCPMangerArchitecture.Interface; public string code;
} public string data;
} public string msg;
public class LinkSuccessResponse : AbstractResponse public LinkSuccessResponse() {
{ this.code = "";
public string code; this.data = "";
public string data; this.msg = "";
public string msg; }
public LinkSuccessResponse() {
this.code = "";
this.data = "";
this.msg = "";
} }
}
public class LinkTCPRequest :IRequest{ public class LinkTCPRequest :IRequest{
public string id; public string id;
public int type; public int type;
public LinkTCPRequest(int id) { public LinkTCPRequest(int id) {
this.id = id.ToString(); this.id = id.ToString();
this.type = 1; this.type = 1;
} }
public string toJson() public string toJson()
{ {
return JsonUtility.ToJson(this); return JsonUtility.ToJson(this);
}
} }
} }

6
Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs

@ -3,12 +3,12 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.UI; using UnityEngine.UI;
using QFramework; using QFrameworkCP;
using System.Threading; using System.Threading;
namespace TCPClientTools namespace JXSoft
{ {
public class TCPPrinter : MonoBehaviour,IController,ICanSendEvent public class TCPPrinter : MonoBehaviour,IController
{ {
private TCPUtility tcpUtil; private TCPUtility tcpUtil;
public Transform tcpMsgContent; public Transform tcpMsgContent;

172
Assets/MsgTransmitTools/UDPClient/UDPClientView.cs

@ -2,105 +2,107 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using QFramework; using QFrameworkCP;
using System; using System;
public class UDPMangerArchitecture : Architecture<UDPMangerArchitecture> namespace JXSoft {
{ public class UDPMangerArchitecture : Architecture<UDPMangerArchitecture>
protected override void Init()
{ {
this.RegisterUtility(new UDPUtility()); protected override void Init()
this.RegisterModel(new UDPEventModel()); {
} this.RegisterUtility(new UDPUtility());
} this.RegisterModel(new UDPEventModel());
public class UDPClientView : MonoBehaviour, IController, ICanSendEvent }
{
public UnityEvent onRecievedOpenDevice;
public UnityEvent onUDPLinkSuccess;
public UnityEvent onUDPLinkFaild;
public UnityEvent onServerConnected;
public UnityEvent onUDPReLink;
private string UDPAddress;
private int UDPPort;
private int deviceId;
// Start is called before the first frame update
void Awake()
{
initUDPService();
DontDestroyOnLoad(this);
} }
public class UDPClientView : MonoBehaviour, IController
public void initUDPService()
{ {
UDPAddress = "127.0.0.1"; public UnityEvent onRecievedOpenDevice;
UDPPort = 20000; public UnityEvent onUDPLinkSuccess;
deviceId = 1; public UnityEvent onUDPLinkFaild;
public UnityEvent onServerConnected;
public UnityEvent onUDPReLink;
GetArchitecture().RegisterEvent<UDPStateChangedEvent>(e => { private string UDPAddress;
if (e.state == UDPLinkState.Linking) private int UDPPort;
{ private int deviceId;
Debug.Log("UDP开始链接"); // Start is called before the first frame update
this.GetModel<UDPEventModel>().linkServer(); void Awake()
} {
if (e.state == UDPLinkState.LinkSucess) initUDPService();
{ DontDestroyOnLoad(this);
Debug.Log("UDP链接成功"); }
this.GetModel<UDPEventModel>().onReceive<LinkSuccessResponse>();
this.GetModel<UDPEventModel>().sendRequestCommand(new LinkUDPRequest(deviceId));
onUDPLinkSuccess.Invoke();
}
if (e.state == UDPLinkState.LinkFaild)
{
Debug.Log("UDP连接失败,请联系设备服务管理员");
onUDPLinkFaild.Invoke();
}
});
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e => public void initUDPService()
{ {
if (e.res.GetType() == typeof(LinkSuccessResponse)) UDPAddress = "127.0.0.1";
UDPPort = 20000;
deviceId = 1;
GetArchitecture().RegisterEvent<UDPStateChangedEvent>(e => {
if (e.state == UDPLinkState.Linking)
{
Debug.Log("UDP开始链接");
this.GetModel<UDPEventModel>().linkServer();
}
if (e.state == UDPLinkState.LinkSucess)
{
Debug.Log("UDP链接成功");
this.GetModel<UDPEventModel>().onReceive<LinkSuccessResponse>();
this.GetModel<UDPEventModel>().sendRequestCommand(new LinkUDPRequest(deviceId));
onUDPLinkSuccess.Invoke();
}
if (e.state == UDPLinkState.LinkFaild)
{
Debug.Log("UDP连接失败,请联系设备服务管理员");
onUDPLinkFaild.Invoke();
}
});
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e =>
{ {
Debug.Log("Link Server success"); if (e.res.GetType() == typeof(LinkSuccessResponse))
this.GetModel<UDPEventModel>().offReceive<LinkSuccessResponse>(); {
onServerConnected.Invoke(); Debug.Log("Link Server success");
} this.GetModel<UDPEventModel>().offReceive<LinkSuccessResponse>();
}); onServerConnected.Invoke();
}
});
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.NoIp); this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.NoIp);
this.GetModel<UDPEventModel>().setIP(UDPAddress, UDPPort); this.GetModel<UDPEventModel>().setIP(UDPAddress, UDPPort);
} }
public void restartUDPService() public void restartUDPService()
{ {
onUDPReLink.Invoke(); onUDPReLink.Invoke();
this.GetModel<UDPEventModel>().closeServer(); this.GetModel<UDPEventModel>().closeServer();
StartCoroutine(waitTwoSecond()); StartCoroutine(waitTwoSecond());
} }
public IEnumerator waitTwoSecond() public IEnumerator waitTwoSecond()
{ {
yield return new WaitForSeconds(2.0f); yield return new WaitForSeconds(2.0f);
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.Linking); this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.Linking);
} }
public IArchitecture GetArchitecture() public IArchitecture GetArchitecture()
{ {
return UDPMangerArchitecture.Interface; return UDPMangerArchitecture.Interface;
}
} }
}
public class LinkUDPRequest : IRequest public class LinkUDPRequest : IRequest
{
public string id;
public int type;
public LinkUDPRequest(int id)
{
this.id = id.ToString();
this.type = 1;
}
public string toJson()
{ {
return JsonUtility.ToJson(this); public string id;
public int type;
public LinkUDPRequest(int id)
{
this.id = id.ToString();
this.type = 1;
}
public string toJson()
{
return JsonUtility.ToJson(this);
}
} }
}
}

7
Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs

@ -1,8 +1,9 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using QFramework; using QFrameworkCP;
namespace JXSoft {
public class UDPEventModel : DataEventModel public class UDPEventModel : DataEventModel
{ {
private UDPLinkState udpState = UDPLinkState.NoIp; private UDPLinkState udpState = UDPLinkState.NoIp;
@ -114,4 +115,6 @@ using QFramework;
exceptionMsg = exceptionMsg_; exceptionMsg = exceptionMsg_;
} }
} }
#endregion #endregion
}

81
Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs

@ -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;
} }
} }

2
Assets/MsgTransmitTools/UDPClient/UDPUtility.cs

@ -1,5 +1,5 @@
using UnityEngine; using UnityEngine;
using QFramework; using QFrameworkCP;
using System; using System;
using System.Text; using System.Text;
using System.Net.Sockets; using System.Net.Sockets;

280
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -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; string toJson();
/// <summary>
/// 尝试填充数据,如果json不合法,则返回false,忽略该条数据响应
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
bool trySetData(string json);
string getException();
} }
protected override void OnExecute()
{ public interface IRequest
this.SendEvent(new RequestMsgEvent(request));
}
public void setRequest(TRequest request)
{ {
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>
{ {
}
} }

6
Assets/MsgTransmitTools/src/QFrameCopy.cs

@ -25,7 +25,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace QFramework namespace QFrameworkCP
{ {
#region Architecture #region Architecture
@ -205,7 +205,7 @@ namespace QFramework
#region Controller #region Controller
public interface IController : IBelongToArchitecture, ICanSendCommand, ICanGetModel, public interface IController : IBelongToArchitecture, ICanSendCommand, ICanGetModel,
ICanRegisterEvent, ICanSendQuery, ICanSendEvent ICanRegisterEvent, ICanSendQuery, ICanSendEvent, ICanGetUtility
{ {
} }
@ -213,7 +213,7 @@ namespace QFramework
#region Model #region Model
public interface IModel : IBelongToArchitecture, ICanSetArchitecture, ICanGetUtility, ICanSendEvent public interface IModel : IBelongToArchitecture, ICanSetArchitecture, ICanGetUtility, ICanSendEvent, ICanSendCommand, ICanRegisterEvent
{ {
void Init(); void Init();
} }

Loading…
Cancel
Save