|
|
|
|
using System.Collections;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
using QFrameworkCP;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace JXSoft {
|
|
|
|
|
public class TCPMangerArchitecture : Architecture<TCPMangerArchitecture>
|
|
|
|
|
{
|
|
|
|
|
protected override void Init()
|
|
|
|
|
{
|
|
|
|
|
this.RegisterModel(new TCPClientModel());
|
|
|
|
|
this.RegisterModel(new TCPClient2Model());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class TCPClient2Model :TCPClientModel{
|
|
|
|
|
public override string getSender()
|
|
|
|
|
{
|
|
|
|
|
return "TCP2<EFBFBD>Ż<EFBFBD>";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent
|
|
|
|
|
{
|
|
|
|
|
public string tcpAddress;
|
|
|
|
|
public int tcpPort;
|
|
|
|
|
//Printer&MsgGetter
|
|
|
|
|
public Transform tcpMsgContent;
|
|
|
|
|
public GameObject tcpMsgItem;
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
initTCPService();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initTCPService() {
|
|
|
|
|
this.GetModel<TCPClientModel>().setIP(tcpAddress, tcpPort);
|
|
|
|
|
this.GetModel<TCPClientModel>().Start();
|
|
|
|
|
|
|
|
|
|
this.GetModel<TCPClient2Model>().setIP(tcpAddress, tcpPort+1);
|
|
|
|
|
this.GetModel<TCPClient2Model>().Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>
|
|
|
|
|
public void restartTCPService() {
|
|
|
|
|
this.GetModel<TCPClientModel>().Close();
|
|
|
|
|
StartCoroutine(waitTwoSecond());
|
|
|
|
|
}
|
|
|
|
|
public IEnumerator waitTwoSecond() {
|
|
|
|
|
yield return new WaitForSeconds(2.0f);
|
|
|
|
|
this.GetModel<TCPClientModel>().Start();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
//Debug.Log(tcpUtil.Read_TCPClient());
|
|
|
|
|
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);
|
|
|
|
|
item.GetComponentInChildren<Text>().text = this.GetModel<T>().getInstance().receivedData;
|
|
|
|
|
this.GetModel<T>().onDataRecived.Invoke(this.GetModel<T>().getInstance().receivedData);
|
|
|
|
|
}
|
|
|
|
|
if (this.GetModel<T>().getInstance().getTimeOutState() && this.GetModel<T>().getInstance().isOpenTCP)
|
|
|
|
|
{
|
|
|
|
|
this.SendEvent(new onLinkException(this.GetModel<T>().getInstance().exceptionData));
|
|
|
|
|
this.GetModel<T>().getInstance().isOpenTCP = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
this.GetModel<TCPClientModel>().Close();
|
|
|
|
|
this.GetModel<TCPClient2Model>().Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IArchitecture GetArchitecture()
|
|
|
|
|
{
|
|
|
|
|
return TCPMangerArchitecture.Interface;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface ITCPClient
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
return TCPMangerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => {
|
|
|
|
|
if (e.res.GetType() == typeof(TResponse))
|
|
|
|
|
{
|
|
|
|
|
onEvent.Invoke((TResponse)e.res,e.sender);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public static void onReceive<TResponse>(this ITCPClient self) where TResponse : IResponse, new()
|
|
|
|
|
{
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().sendRequest(request);
|
|
|
|
|
}
|
|
|
|
|
public static void sendRequest<TRequest,TDataModel>(this ITCPClient self, TRequest request) where TRequest : IRequest, new() where TDataModel:DataEventModel
|
|
|
|
|
{
|
|
|
|
|
TCPMangerArchitecture.Interface.GetModel<TDataModel>().sendRequest(request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|