@ -11,8 +11,14 @@ namespace JXSoft {
@@ -11,8 +11,14 @@ namespace JXSoft {
{
protected override void Init ( )
{
this . RegisterUtility ( new TCPClientUtility ( ) ) ;
this . RegisterModel ( new TCPClientModel ( ) ) ;
this . RegisterModel ( new TCPClient2Model ( ) ) ;
}
}
public class TCPClient2Model : TCPClientModel {
public override string getSender ( )
{
return "TCP2ºÅ»ú" ;
}
}
public class TCPClientView : MonoBehaviour , IController , ICanRegisterEvent
@ -20,79 +26,59 @@ namespace JXSoft {
@@ -20,79 +26,59 @@ namespace JXSoft {
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开始链接" ) ;
this . GetModel < TCPClientModel > ( ) . setIP ( tcpAddress , tcpPort ) ;
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 < TCPClient2Model > ( ) . setIP ( tcpAddress , tcpPort + 1 ) ;
this . GetModel < TCPClient2Model > ( ) . Start ( ) ;
}
#region ÖØÆôÁ´½ÓʾÀý
public void restartTCPService ( ) {
onTCPReLink . Invoke ( ) ;
this . GetModel < TCPClientModel > ( ) . Close ( ) ;
StartCoroutine ( waitTwoSecond ( ) ) ;
}
public IEnumerator waitTwoSecond ( ) {
yield return new WaitForSeconds ( 2.0f ) ;
this . GetModel < TCPClientModel > ( ) . setLink State ( ( int ) TCPLinkState . Linking ) ;
this . GetModel < TCPClientModel > ( ) . Star t ( ) ;
}
# endregion
// Update is called once per frame
void Update ( )
{
//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 ) ;
item . GetComponentInChildren < Text > ( ) . text = tcpUtil . receivedData ;
this . GetModel < TCPClientModel > ( ) . onDataRecived . Invoke ( tcpUtil . receivedData ) ;
item . GetComponentInChildren < Text > ( ) . text = this . GetModel < T > ( ) . getInstance ( ) . 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 ) ) ;
tcpUtil . isOpenTCP = false ;
this . SendEvent ( new onLinkException ( this . GetModel < T > ( ) . getInstance ( ) . exceptionData ) ) ;
this . GetModel < T > ( ) . getInstance ( ) . isOpenTCP = false ;
}
}
private void OnDestroy ( )
{
if ( tcpUtil . reciveT ! = null & & tcpUtil . reciveT . ThreadState = = ThreadState . Running )
{
tcpUtil . reciveT . Abort ( ) ;
}
this . GetModel < TCPClientModel > ( ) . Close ( ) ;
this . GetModel < TCPClient2Model > ( ) . Close ( ) ;
}
public IArchitecture GetArchitecture ( )
@ -107,6 +93,15 @@ namespace JXSoft {
@@ -107,6 +93,15 @@ namespace JXSoft {
}
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 = > {
@ -120,10 +115,18 @@ namespace JXSoft {
@@ -120,10 +115,18 @@ namespace JXSoft {
{
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 ) ;