|
|
|
|
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.RegisterUtility(new TCPClientUtility());
|
|
|
|
|
this.RegisterModel(new TCPClientModel());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent
|
|
|
|
|
{
|
|
|
|
|
public string tcpAddress;
|
|
|
|
|
public int tcpPort;
|
|
|
|
|
//Printer&MsgGetter
|
|
|
|
|
private TCPClientUtility tcpUtil;
|
|
|
|
|
public Transform tcpMsgContent;
|
|
|
|
|
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
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
initTCPService();
|
|
|
|
|
DontDestroyOnLoad(this);
|
|
|
|
|
tcpUtil = GetArchitecture().GetUtility<TCPClientUtility>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initTCPService() {
|
|
|
|
|
deviceId = 1;
|
|
|
|
|
|
|
|
|
|
this.RegisterLinkStateEvent((int)TCPLinkState.Linking, () =>
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("TCP<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
|
this.GetModel<TCPClientModel>().linkServer();
|
|
|
|
|
});
|
|
|
|
|
this.RegisterLinkStateEvent((int)TCPLinkState.LinkSucess, () =>
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("TCP<EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD>");
|
|
|
|
|
onTCPLinkSuccess.Invoke();
|
|
|
|
|
});
|
|
|
|
|
this.RegisterLinkStateEvent((int)TCPLinkState.LinkFaild, () =>
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>豸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա");
|
|
|
|
|
onTCPLinkFaild.Invoke();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.GetModel<TCPClientModel>().setLinkState((int)TCPLinkState.NoIp);
|
|
|
|
|
this.GetModel<TCPClientModel>().setIP(tcpAddress, tcpPort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void restartTCPService() {
|
|
|
|
|
onTCPReLink.Invoke();
|
|
|
|
|
this.GetModel<TCPClientModel>().closeServer();
|
|
|
|
|
StartCoroutine(waitTwoSecond());
|
|
|
|
|
}
|
|
|
|
|
public IEnumerator waitTwoSecond() {
|
|
|
|
|
yield return new WaitForSeconds(2.0f);
|
|
|
|
|
this.GetModel<TCPClientModel>().setLinkState((int)TCPLinkState.Linking);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
//Debug.Log(tcpUtil.Read_TCPClient());
|
|
|
|
|
if (tcpUtil != null && !"".Equals(tcpUtil.getReceivedValue()))
|
|
|
|
|
{
|
|
|
|
|
GameObject item = Instantiate(tcpMsgItem, tcpMsgContent);
|
|
|
|
|
item.GetComponentInChildren<Text>().text = tcpUtil.receivedData;
|
|
|
|
|
this.GetModel<TCPClientModel>().onDataRecived.Invoke(tcpUtil.receivedData);
|
|
|
|
|
}
|
|
|
|
|
if (tcpUtil.getTimeOutState() && tcpUtil.isOpenTCP)
|
|
|
|
|
{
|
|
|
|
|
this.SendEvent(new onLinkException(tcpUtil.exceptionData));
|
|
|
|
|
tcpUtil.isOpenTCP = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
if (tcpUtil.reciveT != null && tcpUtil.reciveT.ThreadState == ThreadState.Running)
|
|
|
|
|
{
|
|
|
|
|
tcpUtil.reciveT.Abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 void onReceive<TResponse>(this ITCPClient self) where TResponse : IResponse, new()
|
|
|
|
|
{
|
|
|
|
|
TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().onReceive<TResponse>();
|
|
|
|
|
}
|
|
|
|
|
public static void offReceive<TResponse>(this ITCPClient self) where TResponse : IResponse, new()
|
|
|
|
|
{
|
|
|
|
|
TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().offReceive<TResponse>();
|
|
|
|
|
}
|
|
|
|
|
public static void sendRequest<TRequest>(this ITCPClient self ,TRequest request) where TRequest : IRequest, new()
|
|
|
|
|
{
|
|
|
|
|
TCPMangerArchitecture.Interface.GetModel<TCPClientModel>().sendRequest(request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|