private void Connect() { lock (this) { try { IPAddress[] ipAdds = Dns.GetHostAddresses(_tcpData.IP); UnityEngine.Debug.Log(string.Format("StartConnect {0}:{1} host address:", _tcpData.IP, _tcpData.Port)); for (int i = 0; i < ipAdds.Length; i++) { UnityEngine.Debug.Log(ipAdds[i].ToString()); } if (ipAdds.Length > 0) { IPAddress ipAdrress = ipAdds[0]; AddressFamily addressFamiliy = ipAdrress.AddressFamily; TcpClient = new TcpClient(addressFamiliy); TCPStatus = TCPStatus.Connecting; TcpClient.BeginConnect(ipAdds, _tcpData.Port, OnTcpConnected, this); } else { } } catch { } } }
private void StopPreConnect() { if (_connectThread != null) { UnityEngine.Debug.Log("【NetConnector】Stop connect @thread(" + System.Threading.Thread.CurrentThread.ManagedThreadId + ")"); _connectThread.Abort(); _connectThread = null; } if (TcpClient != null) { TcpClient.Close(); TcpClient = null; TCPStatus = TCPStatus.Offline; } }
private void OnTcpConnected(IAsyncResult asyncresult) { lock (this) { //UnityEngine.Debug.Log(string.Format("【NetConnector】OnTcpConnected @thread({0}) ", System.Threading.Thread.CurrentThread.ManagedThreadId)); TCPConnector connector = asyncresult.AsyncState as TCPConnector; if (connector != null && TCPStatus == TCPStatus.Connecting && connector.TcpClient == this.TcpClient && connector.TcpClient.Connected) { TCPStatus = TCPStatus.Connected; } else { UnityEngine.Debug.LogError(string.Format("【NetConnector】OnTcpConnected Error @thread({0}) !!!!!!!!!!!!!!", System.Threading.Thread.CurrentThread.ManagedThreadId)); } } }
private void OnTCPStatusChanged(TCPStatus tcpStatus) { }