diff --git a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs index 4e84dd7..a599395 100644 --- a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs +++ b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs @@ -1,7 +1,10 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; using QFrameworkCP; +using System; +using System.Text; +using System.Net.Sockets; +using System.Threading; +using System.Net.NetworkInformation; namespace JXSoft { @@ -14,9 +17,8 @@ namespace JXSoft public string myAddress = ""; protected override void OnInit() { - myAddress = this.GetUtility().GetIP(); this.RegisterEvent(e=> { - setTCPState(TCPLinkState.LinkTimeOut); + setLinkState((int)TCPLinkState.LinkTimeOut); this.GetUtility().CloseTCPClient(); }); //注册重写发送事件 @@ -31,13 +33,17 @@ namespace JXSoft } }); } - public void setTCPState(TCPLinkState state) { - this.tcpState = state; - this.SendEvent(new TCPStateChangedEvent(state)); + public override void setLinkState(int linkStatus) + { + this.tcpState = (TCPLinkState)linkStatus; + this.SendEvent(new LinkStateChangedEvent(linkStatus)); } - public TCPLinkState getState() { - return this.tcpState; + + public override int getLinkState() + { + return (int)this.tcpState; } + /// /// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器) /// @@ -48,7 +54,7 @@ namespace JXSoft this.tcpPort = port; //此处可以加ip校验 Debug.LogWarning("此处未进行ip以及端口号校验,日后有需求可以增加"); - setTCPState(TCPLinkState.Linking); + setLinkState((int)TCPLinkState.Linking); } /// /// 与服务端建立链接 @@ -60,11 +66,11 @@ namespace JXSoft bool isSuccess = this.GetUtility().StartTCPClient(tcpAddress, tcpPort); if (isSuccess) { - setTCPState(TCPLinkState.LinkSucess); + setLinkState((int)TCPLinkState.LinkSucess); } else { - setTCPState(TCPLinkState.LinkFaild); + setLinkState((int)TCPLinkState.LinkFaild); } } } @@ -80,10 +86,12 @@ namespace JXSoft { this.GetUtility().CloseTCPClient(); - setTCPState(TCPLinkState.NoIp); + setLinkState((int)TCPLinkState.NoIp); } } } + + } #region enum @@ -98,14 +106,6 @@ namespace JXSoft #endregion #region event - public struct TCPStateChangedEvent - { - public TCPLinkState state; - public TCPStateChangedEvent(TCPLinkState state_) - { - state = state_; - } - } public struct onLinkException { public string exceptionMsg; @@ -115,4 +115,168 @@ namespace JXSoft } } #endregion + + #region util + public class TCPUtility : IUtility + { + public string tcpAddress; + public int tcpPort; + public TcpClient tcpClient; + public NetworkStream sendStream; + public bool isOpenTCP = false; + public bool isReceivedValue = false; + public string receivedData = ""; + public string exceptionData = ""; + + public Thread reciveT; + public bool isTimeOut = false; + + public TCPUtility() + { + Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务"); + + } + public TCPUtility(string tcpAddress, int tcpPort) + { + this.tcpAddress = tcpAddress; + this.tcpPort = tcpPort; + this.isTimeOut = false; + StartTCPClient(); + } + public bool StartTCPClient() + { + return StartTCPClient(tcpAddress, tcpPort); + } + public bool StartTCPClient(string ip, int port) + { + isTimeOut = false; + if (!isOpenTCP) + { + tcpAddress = ip; + tcpPort = port; + try + { + tcpClient = new TcpClient(tcpAddress, tcpPort); + sendStream = tcpClient.GetStream(); + } + catch (Exception e) + { + exceptionData = e.ToString(); + return false; + } + isOpenTCP = true; + reciveT = new Thread(RecciveMsg); + reciveT.IsBackground = true; + reciveT.Start(); + return true; + } + return false; + } + + public void CloseTCPClient() + { + if (tcpClient.Connected) + { + sendStream.Close(); + tcpClient.Close(); + isOpenTCP = false; + reciveT.Abort(); + } + } + + public void sendData(string data) + { + if (isOpenTCP == false) + return; + byte[] sendBytes = Encoding.Default.GetBytes(data); + sendStream.Write(sendBytes, 0, sendBytes.Length); + } + + public void RecciveMsg() + { + byte[] receiveBuff = new byte[1024]; + int reviceLength = 0; + string msg = ""; + while (isOpenTCP) + { + try + { + reviceLength = tcpClient.Client.Receive(receiveBuff); + msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength); + if (msg != "") + { + receivedData = msg; + isReceivedValue = true; + } + } + catch (Exception e) + { + Debug.LogWarning(e); + //断线发送异常 + isTimeOut = true; + exceptionData = e.ToString(); + break; + } + } + Debug.Log("------------end While-------------"); + } + + /// + /// 线程接收到消息后 + /// + /// 接收到的消息 + public string getReceivedValue() + { + if (isReceivedValue) + { + isReceivedValue = false; + return receivedData; + } + else + { + return ""; + } + } + + public bool getTimeOutState() + { + return isTimeOut; + } + + public bool IsReceivedMsg() + { + return sendStream.Length != 0; + } + public bool IsConnected() + { + return tcpClient.Connected; + } + + /// + /// 获取本机IP + /// + /// string :ip地址 + public string GetIP() + { + string output = ""; + + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; //无线局域网适配器 + + if ((item.NetworkInterfaceType == _type1) && item.OperationalStatus == OperationalStatus.Up) + { + foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork) + { + output = ip.Address.ToString(); + } + } + } + } + return output; + } + } + #endregion } \ No newline at end of file diff --git a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs deleted file mode 100644 index c2f6c11..0000000 --- a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs +++ /dev/null @@ -1,184 +0,0 @@ -using QFrameworkCP; -using UnityEngine; -using System; -using System.Text; -using System.Net.Sockets; -using System.IO; -using System.Threading.Tasks; -using System.Threading; -using System.Net.NetworkInformation; - -namespace JXSoft -{ - public class TCPUtility : IUtility - { - public string tcpAddress; - public int tcpPort; - public TcpClient tcpClient; - public NetworkStream sendStream; - public bool isOpenTCP = false; - public bool isReceivedValue = false; - public string receivedData = ""; - public string exceptionData = ""; - - public Thread reciveT; - public bool isTimeOut = false; - - public TCPUtility() { - Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务"); - - } - public TCPUtility(string tcpAddress, int tcpPort) - { - this.tcpAddress = tcpAddress; - this.tcpPort = tcpPort; - this.isTimeOut = false; - StartTCPClient(); - } - public bool StartTCPClient() - { - isTimeOut = false; - if (!isOpenTCP) - { - try { - tcpClient = new TcpClient(tcpAddress, tcpPort); - sendStream = tcpClient.GetStream(); - } - catch (Exception e) - { - exceptionData = e.ToString(); - return false; - } - isOpenTCP = true; - reciveT = new Thread(RecciveMsg); - reciveT.IsBackground = true; - reciveT.Start(); - return true; - } - return false; - } - public bool StartTCPClient(string ip, int port) { - isTimeOut = false; - if (!isOpenTCP) { - tcpAddress = ip; - tcpPort = port; - try - { - tcpClient = new TcpClient(tcpAddress, tcpPort); - sendStream = tcpClient.GetStream(); - } - catch (Exception e) - { - exceptionData = e.ToString(); - return false; - } - isOpenTCP = true; - reciveT = new Thread(RecciveMsg); - reciveT.IsBackground = true; - reciveT.Start(); - return true; - } - return false; - } - - public void CloseTCPClient() { - if (tcpClient.Connected) { - sendStream.Close(); - tcpClient.Close(); - isOpenTCP = false; - reciveT.Abort(); - } - } - - public void sendData(string data) - { - if (isOpenTCP == false) - return; - byte[] sendBytes = Encoding.Default.GetBytes(data); - sendStream.Write(sendBytes, 0, sendBytes.Length); - } - - public void RecciveMsg() - { - byte[] receiveBuff = new byte[1024]; - int reviceLength = 0; - string msg = ""; - while (isOpenTCP) - { - try - { - reviceLength = tcpClient.Client.Receive(receiveBuff); - msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength); - if (msg != "") - { - receivedData = msg; - isReceivedValue = true; - } - } - catch (Exception e) - { - Debug.LogWarning(e); - //断线发送异常 - isTimeOut = true; - exceptionData = e.ToString(); - break; - } - } - Debug.Log("------------end While-------------"); - } - - /// - /// 线程接收到消息后 - /// - /// 接收到的消息 - public string getReceivedValue() { - if (isReceivedValue) - { - isReceivedValue = false; - return receivedData; - } - else { - return ""; - } - } - - public bool getTimeOutState() { - return isTimeOut; - } - - public bool IsReceivedMsg() { - return sendStream.Length != 0; - } - public bool IsConnected() { - return tcpClient.Connected; - } - - /// - /// 获取本机IP - /// - /// string :ip地址 - public string GetIP() - { - string output = ""; - - foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) - { - NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; //无线局域网适配器 - - if ((item.NetworkInterfaceType == _type1) && item.OperationalStatus == OperationalStatus.Up) - { - foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) - { - if (ip.Address.AddressFamily == AddressFamily.InterNetwork) - { - output = ip.Address.ToString(); - } - } - } - } - return output; - } - } - - -} diff --git a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta b/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta deleted file mode 100644 index fa9414b..0000000 --- a/Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 58c08e7452ec0014ab1aedcf80bddd95 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs b/Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs index 055ac48..3a79624 100644 --- a/Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs +++ b/Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs @@ -16,7 +16,7 @@ namespace JXSoft { this.RegisterModel(new TCPEventModel()); } } - public class TCPClientView : MonoBehaviour,IController + public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent { public UnityEvent onRecievedOpenDevice; public UnityEvent onTCPLinkSuccess; @@ -39,27 +39,27 @@ namespace JXSoft { tcpPort = 20000; deviceId = 1; - GetArchitecture().RegisterEvent(e => { - if (e.state == TCPLinkState.Linking) + this.RegisterEvent(e => { + if (e.linkState == (int)TCPLinkState.Linking) { Debug.Log("TCPʼ"); this.GetModel().linkServer(); } - if (e.state == TCPLinkState.LinkSucess) + if (e.linkState == (int)TCPLinkState.LinkSucess) { Debug.Log("TCPӳɹ"); this.GetModel().onReceive(); this.GetModel().sendRequestCommand(new LinkTCPRequest(deviceId)); onTCPLinkSuccess.Invoke(); } - if (e.state == TCPLinkState.LinkFaild) + if (e.linkState == (int)TCPLinkState.LinkFaild) { Debug.Log("TCPʧܣϵ豸Ա"); onTCPLinkFaild.Invoke(); } }); - GetArchitecture().RegisterEvent(e => + this.RegisterEvent(e => { if (e.res.GetType() == typeof(LinkSuccessResponse)) { @@ -69,7 +69,7 @@ namespace JXSoft { } }); - this.GetModel().setTCPState(TCPLinkState.NoIp); + this.GetModel().setLinkState((int)TCPLinkState.NoIp); this.GetModel().setIP(tcpAddress, tcpPort); } @@ -80,7 +80,7 @@ namespace JXSoft { } public IEnumerator waitTwoSecond() { yield return new WaitForSeconds(2.0f); - this.GetModel().setTCPState(TCPLinkState.Linking); + this.GetModel().setLinkState((int)TCPLinkState.Linking); } public IArchitecture GetArchitecture() { diff --git a/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs b/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs index 3524716..f4197d2 100644 --- a/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs +++ b/Assets/MsgTransmitTools/UDPClient/UDPClientView.cs @@ -68,7 +68,7 @@ namespace JXSoft { } }); - this.GetModel().setUDPState(UDPLinkState.NoIp); + this.GetModel().setLinkState((int)UDPLinkState.NoIp); this.GetModel().setIP(UDPAddress, UDPPort); } @@ -81,7 +81,7 @@ namespace JXSoft { public IEnumerator waitTwoSecond() { yield return new WaitForSeconds(2.0f); - this.GetModel().setUDPState(UDPLinkState.Linking); + this.GetModel().setLinkState((int)UDPLinkState.Linking); } public IArchitecture GetArchitecture() { diff --git a/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs b/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs index 4928e47..7738a30 100644 --- a/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs +++ b/Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs @@ -13,7 +13,7 @@ namespace JXSoft { protected override void OnInit() { this.RegisterEvent(e => { - setUDPState(UDPLinkState.LinkTimeOut); + setLinkState((int)UDPLinkState.LinkTimeOut); this.GetUtility().CloseUDPClient(); }); this.RegisterEvent(e => { @@ -27,14 +27,16 @@ namespace JXSoft { } }); } - public void setUDPState(UDPLinkState state) + + public override void setLinkState(int linkStatus) { - this.udpState = state; - this.SendEvent(new UDPStateChangedEvent(state)); + this.udpState = (UDPLinkState)linkStatus; + this.SendEvent(new LinkStateChangedEvent(linkStatus)); } - public UDPLinkState getState() + + public override int getLinkState() { - return this.udpState; + return (int)this.udpState; } /// /// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器) @@ -47,7 +49,7 @@ namespace JXSoft { this.udpPort = port; //此处可以加ip校验 Debug.LogWarning("此处未进行ip以及端口号校验,日后有需求可以增加"); - setUDPState(UDPLinkState.Linking); + setLinkState((int)UDPLinkState.Linking); } /// /// 与服务端建立链接 @@ -62,11 +64,11 @@ namespace JXSoft { bool isSuccess = this.GetUtility().StartUDPClient(udpAddress, udpPort); if (isSuccess) { - setUDPState(UDPLinkState.LinkSucess); + setLinkState((int)UDPLinkState.LinkSucess); } else { - setUDPState(UDPLinkState.LinkFaild); + setLinkState((int)UDPLinkState.LinkFaild); } } } @@ -82,10 +84,12 @@ namespace JXSoft { { this.GetUtility().CloseUDPClient(); - setUDPState(UDPLinkState.NoIp); + setLinkState((int)UDPLinkState.NoIp); } } } + + } #region enum public enum UDPLinkState diff --git a/Assets/MsgTransmitTools/src/DataEventModel.cs b/Assets/MsgTransmitTools/src/DataEventModel.cs index 56f4c33..e5da919 100644 --- a/Assets/MsgTransmitTools/src/DataEventModel.cs +++ b/Assets/MsgTransmitTools/src/DataEventModel.cs @@ -63,14 +63,11 @@ namespace JXSoft { #endregion #region LinkStatusManage - public virtual void setLinkStatus(int linkStatus) { - this.linkStatus = linkStatus; - this.SendEvent(new LinkStateChangedEvent(linkStatus)); - } + public abstract void setLinkState(int linkStatus); + public abstract int getLinkState(); - public virtual int getLinkStatus() { - return linkStatus; - } + //public abstract void nodifyLinkState() where TLinkState:ILinkState; + //public abstract void registerLinkState(); #endregion } @@ -145,6 +142,10 @@ namespace JXSoft { { string toJson(); } + + public interface ILinkState { + int getLinkStatus(); + } #endregion #region AbstractClass