Browse Source

解决了多重发送的问题,增加了sender校验,可以单独定义使用send了

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
355e69c622
  1. 10
      Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs
  2. 33
      Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs
  3. 87
      Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs
  4. 17
      Assets/MsgTransmitTools/src/DataEventModel.cs
  5. 1
      Assets/MsgTransmitTools/src/QFrameCopy.cs

10
Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs

@ -13,12 +13,18 @@ public class TCPClientExample : MonoBehaviour,ITCPClient
Debug.Log("TestGetMsg:"+e.msg); Debug.Log("TestGetMsg:"+e.msg);
Debug.Log("IpAddress:"+sender); Debug.Log("IpAddress:"+sender);
//应答 //应答
this.sendRequest<StringRequest,TCPClientModel>(new StringRequest("Received!")); if (sender == "127.0.0.1:20000") {
this.sendRequest<StringRequest, TCPClientModel>(new StringRequest("Received!"));
}
if (sender == "TCP2号机") {
this.sendRequest<StringRequest, TCPClient2Model>(new StringRequest("Received2!"));
}
//取消接收该数据 //取消接收该数据
this.offReceive<YourResponse>(); //this.offReceive<YourResponse>();
}); });
//开启开数据类型接收 //开启开数据类型接收
this.onReceive<YourResponse>(); this.onReceive<YourResponse>();
this.onReceive<YourResponse,TCPClient2Model>();
} }
} }
#region ResReqData #region ResReqData

33
Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs

@ -15,22 +15,25 @@ namespace JXSoft
private TCPLinkState tcpState = TCPLinkState.NoIp; private TCPLinkState tcpState = TCPLinkState.NoIp;
private string tcpAddress = ""; private string tcpAddress = "";
private int tcpPort = 0; private int tcpPort = 0;
private TCPClientUtility tcpClient = new TCPClientUtility();
public string myAddress = ""; public string myAddress = "";
protected override void OnInit() protected override void OnInit()
{ {
this.RegisterEvent<onLinkException>(e=> { this.RegisterEvent<onLinkException>(e=> {
setLinkState((int)TCPLinkState.LinkTimeOut); setLinkState((int)TCPLinkState.LinkTimeOut);
this.GetUtility<TCPClientUtility>().CloseTCPClient(); tcpClient.CloseTCPClient();
}); });
//注册重写发送事件 //注册重写发送事件
this.RegisterEvent<RequestMsgEvent>(e => { this.RegisterEvent<RequestMsgEvent>(e => {
if (this.GetUtility<TCPClientUtility>().isOpenTCP) if (e.sender == getSender()) {
{ if (tcpClient.isOpenTCP)
this.GetUtility<TCPClientUtility>().sendData(e.req.toProtocolData()); {
} tcpClient.sendData(e.req.toProtocolData());
else }
{ else
Debug.LogWarning("请先开启TCP链接"); {
Debug.LogWarning("请先开启TCP链接");
}
} }
}); });
} }
@ -70,10 +73,10 @@ namespace JXSoft
{ {
if (tcpState == TCPLinkState.Linking) if (tcpState == TCPLinkState.Linking)
{ {
if (!this.GetUtility<TCPClientUtility>().isOpenTCP) if (!tcpClient.isOpenTCP)
{ {
bool isSuccess = this.GetUtility<TCPClientUtility>().StartTCPClient(tcpAddress, tcpPort); bool isSuccess = tcpClient.StartTCPClient(tcpAddress, tcpPort);
if (isSuccess) if (isSuccess)
{ {
setLinkState((int)TCPLinkState.LinkSucess); setLinkState((int)TCPLinkState.LinkSucess);
@ -95,10 +98,10 @@ namespace JXSoft
{ {
if (tcpState == TCPLinkState.LinkSucess) if (tcpState == TCPLinkState.LinkSucess)
{ {
if (this.GetUtility<TCPClientUtility>().isOpenTCP) if (tcpClient.isOpenTCP)
{ {
this.GetUtility<TCPClientUtility>().CloseTCPClient(); tcpClient.CloseTCPClient();
setLinkState((int)TCPLinkState.NoIp); setLinkState((int)TCPLinkState.NoIp);
return true; return true;
} }
@ -107,7 +110,11 @@ namespace JXSoft
} }
public override string getSender() public override string getSender()
{ {
return tcpAddress; return tcpAddress + ":" + tcpPort;
}
public TCPClientUtility getInstance() {
return tcpClient;
} }
} }

87
Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs

@ -11,8 +11,14 @@ namespace JXSoft {
{ {
protected override void Init() protected override void Init()
{ {
this.RegisterUtility(new TCPClientUtility());
this.RegisterModel(new TCPClientModel()); this.RegisterModel(new TCPClientModel());
this.RegisterModel(new TCPClient2Model());
}
}
public class TCPClient2Model :TCPClientModel{
public override string getSender()
{
return "TCP2ºÅ»ú";
} }
} }
public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent
@ -20,79 +26,59 @@ namespace JXSoft {
public string tcpAddress; public string tcpAddress;
public int tcpPort; public int tcpPort;
//Printer&MsgGetter //Printer&MsgGetter
private TCPClientUtility tcpUtil;
public Transform tcpMsgContent; public Transform tcpMsgContent;
public GameObject tcpMsgItem; public GameObject tcpMsgItem;
public UnityEvent onRecievedOpenDevice;
public UnityEvent onTCPLinkSuccess;
public UnityEvent onTCPLinkFaild;
public UnityEvent onServerConnected;
public UnityEvent onTCPReLink;
private int deviceId;
// Start is called before the first frame update // Start is called before the first frame update
void Awake() void Awake()
{ {
initTCPService(); initTCPService();
DontDestroyOnLoad(this);
tcpUtil = GetArchitecture().GetUtility<TCPClientUtility>();
} }
public void initTCPService() { public void initTCPService() {
deviceId = 1;
this.RegisterLinkStateEvent((int)TCPLinkState.Linking, () =>
{
Debug.Log("TCP开始链接");
this.GetModel<TCPClientModel>().Start();
});
this.RegisterLinkStateEvent((int)TCPLinkState.LinkSucess, () =>
{
Debug.Log("TCP链接成功");
onTCPLinkSuccess.Invoke();
});
this.RegisterLinkStateEvent((int)TCPLinkState.LinkFaild, () =>
{
Debug.Log("TCP连接失败,请联系设备服务管理员");
onTCPLinkFaild.Invoke();
});
this.GetModel<TCPClientModel>().setLinkState((int)TCPLinkState.NoIp);
this.GetModel<TCPClientModel>().setIP(tcpAddress, tcpPort); this.GetModel<TCPClientModel>().setIP(tcpAddress, tcpPort);
this.GetModel<TCPClientModel>().Start();
this.GetModel<TCPClient2Model>().setIP(tcpAddress, tcpPort+1);
this.GetModel<TCPClient2Model>().Start();
} }
#region ÖØÆôÁ´½ÓʾÀý
public void restartTCPService() { public void restartTCPService() {
onTCPReLink.Invoke();
this.GetModel<TCPClientModel>().Close(); this.GetModel<TCPClientModel>().Close();
StartCoroutine(waitTwoSecond()); StartCoroutine(waitTwoSecond());
} }
public IEnumerator waitTwoSecond() { public IEnumerator waitTwoSecond() {
yield return new WaitForSeconds(2.0f); yield return new WaitForSeconds(2.0f);
this.GetModel<TCPClientModel>().setLinkState((int)TCPLinkState.Linking); this.GetModel<TCPClientModel>().Start();
} }
#endregion
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
//Debug.Log(tcpUtil.Read_TCPClient()); //Debug.Log(tcpUtil.Read_TCPClient());
if (tcpUtil != null && !"".Equals(tcpUtil.getReceivedValue())) recevieMsgUpdate<TCPClientModel>();
recevieMsgUpdate<TCPClient2Model>();
}
public void recevieMsgUpdate<T>() where T:TCPClientModel {
if (this.GetModel<T>().getInstance() != null && !"".Equals(this.GetModel<T>().getInstance().getReceivedValue()))
{ {
GameObject item = Instantiate(tcpMsgItem, tcpMsgContent); GameObject item = Instantiate(tcpMsgItem, tcpMsgContent);
item.GetComponentInChildren<Text>().text = tcpUtil.receivedData; item.GetComponentInChildren<Text>().text = this.GetModel<T>().getInstance().receivedData;
this.GetModel<TCPClientModel>().onDataRecived.Invoke(tcpUtil.receivedData); this.GetModel<T>().onDataRecived.Invoke(this.GetModel<T>().getInstance().receivedData);
} }
if (tcpUtil.getTimeOutState() && tcpUtil.isOpenTCP) if (this.GetModel<T>().getInstance().getTimeOutState() && this.GetModel<T>().getInstance().isOpenTCP)
{ {
this.SendEvent(new onLinkException(tcpUtil.exceptionData)); this.SendEvent(new onLinkException(this.GetModel<T>().getInstance().exceptionData));
tcpUtil.isOpenTCP = false; this.GetModel<T>().getInstance().isOpenTCP = false;
} }
} }
private void OnDestroy() private void OnDestroy()
{ {
if (tcpUtil.reciveT != null && tcpUtil.reciveT.ThreadState == ThreadState.Running) this.GetModel<TCPClientModel>().Close();
{ this.GetModel<TCPClient2Model>().Close();
tcpUtil.reciveT.Abort();
}
} }
public IArchitecture GetArchitecture() public IArchitecture GetArchitecture()
@ -107,6 +93,15 @@ namespace JXSoft {
} }
public static class TCPClientExtention public static class TCPClientExtention
{ {
public static IUnRegister RegisterMessageEvent<TResponse>(this ITCPClient self, Action<TResponse> onEvent) where TResponse : IResponse
{
return TCPMangerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => {
if (e.res.GetType() == typeof(TResponse))
{
onEvent.Invoke((TResponse)e.res);
}
});
}
public static IUnRegister RegisterMessageEvent<TResponse>(this ITCPClient self, Action<TResponse,string> onEvent) where TResponse : IResponse public static IUnRegister RegisterMessageEvent<TResponse>(this ITCPClient self, Action<TResponse,string> onEvent) where TResponse : IResponse
{ {
return TCPMangerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => { return TCPMangerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => {
@ -120,10 +115,18 @@ namespace JXSoft {
{ {
TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().onReceive<TResponse>(); TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().onReceive<TResponse>();
} }
public static void onReceive<TResponse,TTCPClientModel>(this ITCPClient self) where TResponse : IResponse, new()where TTCPClientModel:TCPClientModel
{
TCPMangerArchitecture.Interface.GetModel<TTCPClientModel>().onReceive<TResponse>();
}
public static void offReceive<TResponse>(this ITCPClient self) where TResponse : IResponse, new() public static void offReceive<TResponse>(this ITCPClient self) where TResponse : IResponse, new()
{ {
TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().offReceive<TResponse>(); TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().offReceive<TResponse>();
} }
public static void offReceive<TResponse, TTCPClientModel>(this ITCPClient self) where TResponse : IResponse, new() where TTCPClientModel : TCPClientModel
{
TCPMangerArchitecture.Interface.GetModel<TTCPClientModel>().offReceive<TResponse>();
}
public static void sendRequest<TRequest>(this ITCPClient self ,TRequest request) where TRequest : IRequest, new() public static void sendRequest<TRequest>(this ITCPClient self ,TRequest request) where TRequest : IRequest, new()
{ {
TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().sendRequest(request); TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().sendRequest(request);

17
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -59,9 +59,9 @@ namespace JXSoft {
{ {
if (mCommandContainer.Get<SendRequestCommand<T>>() == null) if (mCommandContainer.Get<SendRequestCommand<T>>() == null)
{ {
mCommandContainer.Register(new SendRequestCommand<T>(request)); mCommandContainer.Register(new SendRequestCommand<T>(request,getSender()));
} }
mCommandContainer.Get<SendRequestCommand<T>>().setRequest(request); mCommandContainer.Get<SendRequestCommand<T>>().setRequest(request,getSender());
this.SendCommand(mCommandContainer.Get<SendRequestCommand<T>>()); this.SendCommand(mCommandContainer.Get<SendRequestCommand<T>>());
} }
#endregion #endregion
@ -140,17 +140,20 @@ namespace JXSoft {
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 string sender;
public SendRequestCommand(TRequest request,string sender)
{ {
this.request = request; this.request = request;
this.sender = sender;
} }
protected override void OnExecute() protected override void OnExecute()
{ {
this.SendEvent(new RequestMsgEvent(request)); this.SendEvent(new RequestMsgEvent(request,sender));
} }
public void setRequest(TRequest request) public void setRequest(TRequest request,string sender)
{ {
this.request = request; this.request = request;
this.sender = sender;
} }
} }
#endregion #endregion
@ -301,9 +304,11 @@ namespace JXSoft {
public struct RequestMsgEvent public struct RequestMsgEvent
{ {
public IRequest req; public IRequest req;
public RequestMsgEvent(IRequest req_) public string sender;
public RequestMsgEvent(IRequest req_ ,string sender_)
{ {
req = req_; req = req_;
sender = sender_;
} }
} }
public struct LinkStateChangedEvent { public struct LinkStateChangedEvent {

1
Assets/MsgTransmitTools/src/QFrameCopy.cs

@ -17,6 +17,7 @@
* () https://space.bilibili.com/656352/ * () https://space.bilibili.com/656352/
* *
* Latest Update: 2022.8.8 10:24 List=>HashSet * Latest Update: 2022.8.8 10:24 List=>HashSet
* QFrame删减修改版
****************************************************************************/ ****************************************************************************/
using System; using System;

Loading…
Cancel
Save