/// <summary> /// Tries the connect. /// </summary> /// <exception cref='Exception'> /// Represents errors that occur during application execution. /// </exception> public void TryConnect() { try { IPEndPoint _ep = null; // if (m_bSecurityPolicy) { } else { _ep = this.GetServerAddress(); if (null == _ep) { ConnectState = EClientConnectState.CONNECT_STATE_FAILED; ClientLog.LogError("Connect timeout : no valid server ip or port"); return; } } ConnectState = EClientConnectState.CONNECT_STATE_TRY_CONNECT; _socketClient.BeginConnect(_ep, new AsyncCallback(ConnectCallback), _socketClient); ClientLog.Log("Connect server : " + _ep.Address.ToString() + ":" + _ep.Port.ToString()); } catch (Exception ex) { ClientConnectState = EClientConnectState.CONNECT_STATE_FAILED; ClientLog.LogError(ex.ToString()); } }
void ProcessConnect(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { m_eNetWorkState = EClientNetWorkState.E_CNWS_NORMAL; mReceiveArgs = new SocketAsyncEventArgs(); mReceiveArgs.SetBuffer(new byte[4096], 0, 4096); mReceiveArgs.Completed += SocketEventArg_Completed; /*if (m_ClientSocket != null) * { * m_ClientSocket.ReceiveTimeout = PluginTool.SharedInstance().ReceiveTimeout; * m_ClientSocket.SendTimeout = PluginTool.SharedInstance().SendTimeout; * }*/ connectState = EClientConnectState.E_CONNECT; connect_timeout = 0; Receive(); } else { DidConnectError(); } }
public void TryConnect() { if (socket == null) { socket = new SocketClient(SERVER_ADDRESS, SERVER_PORT, new ProtobufDecoder(), new ProtobufEncoder()); connectState = socket.ConnectState; socket.TryConnect(); } }
/** * */ public void ResetServerAddressStatus() { for (int i = 0; i < _ipAddressArry.Length; i++) { IPaddressWrapper _wrapper = _ipAddressArry [i]; _wrapper.isTried = false; _ipAddressArry [i] = _wrapper; } ConnectState = EClientConnectState.CONNECT_STATE_NONE; }
public bool Connect(string a_strRomoteIP, ushort a_uPort) { AddressFamily ipType = AddressFamily.InterNetwork; string ipStr = GetIPv6(a_strRomoteIP, a_uPort.ToString()); if (!string.IsNullOrEmpty(ipStr)) { string[] ipAndType = ipStr.Split(new char[] { '=' }); if (ipAndType != null && ipAndType.Length == 2) { string ipTypeStr = ipAndType[1]; if (ipTypeStr.Equals("ipv6")) { a_strRomoteIP = ipAndType[0]; ipType = AddressFamily.InterNetworkV6; } } } if (m_ClientSocket == null) { try { m_ClientSocket = new Socket(ipType, SocketType.Stream, ProtocolType.Tcp); } catch (Exception e) { MonoBehaviour.print(e); return(false); } m_strRomoteIP = a_strRomoteIP; m_uRemotePort = a_uPort; m_eNetWorkState = EClientNetWorkState.E_CNWS_NORMAL; connect_timeout = 0; //mByteToSend.Clear(); isSending = false; IPAddress ip = IPAddress.Parse(a_strRomoteIP); mAsyncArgs = new SocketAsyncEventArgs(); mAsyncArgs.RemoteEndPoint = new IPEndPoint(ip, a_uPort); mAsyncArgs.UserToken = m_ClientSocket; mAsyncArgs.Completed += SocketEventArg_Completed; m_ClientSocket.ConnectAsync(mAsyncArgs); connectState = EClientConnectState.E_NOT_CONNECT; return(true); } return(false); }
public void Update() { lock (m_ComunicationMem) { if (m_ComunicationMem.Length > 0) { if (m_Reader != null) { m_Reader.DidReadData(m_ComunicationMem.GetBuffer(), (int)(m_ComunicationMem.Length)); } m_ComunicationMem.Clear(); } } lock (m_eNetWorkState) { EClientNetWorkState eState = (EClientNetWorkState)m_eNetWorkState; if (eState > EClientNetWorkState.E_CNWS_NORMAL) { if (m_ClientSocket != null) { ReleaseSocket(); CallBackNetState(eState); eState = EClientNetWorkState.E_CNWS_NOT_UNABLE; } } else if (connectState == EClientConnectState.E_NOT_CONNECT) { connect_timeout += Time.deltaTime; if (connect_timeout > CONNECT_TIME_OUT) { connect_timeout = 0; DidDisconnect(); } } else if (connectState == EClientConnectState.E_CONNECT) { // µ÷ÓÃlua½Ó¿Ú CallBackNetState(EClientNetWorkState.E_CNWS_NORMAL); connectState = EClientConnectState.E_NOTICE_CONNECT; } } }
/** * */ private void ConnectCallback(IAsyncResult ar) { try { Socket _socket = (Socket)ar.AsyncState; if (!_socket.Connected) { ConnectState = EClientConnectState.CONNECT_STATE_FAILED; ClientLog.LogError(_socket.LocalEndPoint + " connect failed!, try connect again"); // this.DoRetryConnect (); } else { _socket.EndConnect(ar); this.StartRecevieMsg(); ConnectState = EClientConnectState.CONNECT_STATE_CONNECTED; ClientLog.Log(_socket.LocalEndPoint + " connect successful!"); } } catch (Exception e) { ConnectState = EClientConnectState.CONNECT_STATE_FAILED; // do something Console.WriteLine(e.ToString()); } }
// public void DoRetryConnect() { ConnectState = EClientConnectState.CONNECT_STATE_DO_TRY_CONNECT; }
/// <summary> /// Tries the connect. /// </summary> /// <exception cref='Exception'> /// Represents errors that occur during application execution. /// </exception> public void TryConnect() { try { IPEndPoint ep; if (SecurityPolicy) { while (true) { ep = this.GetServerAddress(); if (null == ep) { connectState = EClientConnectState.ConnectStateTimeOut; Logger.LogError("Connect timeout : no valid server ip or port"); return; } Logger.LogError("WebPlayer : Security Prefetch Socket Policy Failed : IP = " + ep.Address.ToString() + " Port = " + ep.Port.ToString() + ". To next port..."); } } else { ep = GetServerAddress(); if (null == ep) { connectState = EClientConnectState.ConnectStateTimeOut; Logger.LogError("Connect timeout : no valid server ip or port"); return; } } connectState = EClientConnectState.ConnectStateTryConnect; socketClient.BeginConnect(ep, ConnectCallback, socketClient); Logger.LogError("Connect server : " + ep.Address + ":" + ep.Port); } catch (Exception ex) { Logger.LogError(ex.ToString()); } }
/// <summary> /// socket connect call back, if connect fail, try again. if connect successful, prepare to receive msg from server /// </summary> /// <param name="ar"></param> private void ConnectCallback(IAsyncResult ar) { try { var socket = (Socket)ar.AsyncState; if (!socket.Connected) { Logger.LogError(socket.LocalEndPoint + " connect failed!, try connect again"); DoRetryConnect(); } else { connectState = EClientConnectState.ConnectStateConnected; socket.EndConnect(ar); Logger.LogError(socket.LocalEndPoint + " connect successful!"); StartRecevieMsg(); } } catch (Exception e) { connectState = EClientConnectState.ConnectStateTimeOut; Logger.LogError(e.ToString()); } }
public void ResetServerAddressStatus() { connectState = EClientConnectState.ConnectStateNone; for (var i = 0; i < ipAddressArry.Length; i++) { var wrapper = ipAddressArry[i]; wrapper.IsTried = false; ipAddressArry[i] = wrapper; } }
public void DoRetryConnect() { connectState = EClientConnectState.ConnectStateDoTryConnect; }