/// <summary> /// Ons the connect state chg. 当连接状态发生变化时 /// </summary> /// <param name="s">S.</param> /// <param name="result">Result. 其实是bool类型, /// 当为true表示连接成功,false时表示没有连接成功或连接断开</param> public virtual void onConnectStateChg(USocket s, object result) { ArrayList list = result as ArrayList; bool isConnected = (bool)list[0]; int retCode = (int)list[1]; string msg = (string)list[2]; if (isConnected) { #if UNITY_EDITOR Debug.Log("connectCallback success"); #endif connected = true; reConnectTimes = 0; socket.ReceiveAsync(onReceive); enqueueData(CONST_Connect); } else { Debug.LogWarning("connectCallback fail" + host + ":" + port + "," + isStopping); connected = false; if (!isStopping) { outofNetConnect(retCode, msg); } } }
public void connectCallback(USocket s, object result) { if (this.socket == null || (this.socket != null && !this.socket.Equals(s))) { return; } if ((bool)result) { //connectCallback #if UNITY_EDITOR Debug.Log("connectCallback success"); #endif connected = true; reConnectTimes = 0; if (mDispatcher != null) { mDispatcher(CONST_Connect, this); } socket.ReceiveAsync(onReceive); } else { Debug.LogWarning("connectCallback fail" + host + ":" + port + "," + isStopping); connected = false; if (!isStopping) { outofNetConnect(); } } }
private void ReceiveCallback(IAsyncResult ar) { USocket client = (USocket)ar.AsyncState; try { if (client.timeoutCheckTimer != null) { client.timeoutCheckTimer.Dispose(); client.timeoutCheckTimer = null; } if (client.isActive) { client.failTimes = 0; //从远程设备读取Number据 int bytesRead = client.mSocket.EndReceive(ar); if (bytesRead > 0) { //Debug.Log("receive len==" + bytesRead); // 有Number据,存储. onReceiveCallback(client, client.mTmpBuffer, bytesRead); } else if (bytesRead < 0) { client.onConnectStateChg(false, Errors.receivebytesLenError); } else { // 所有Number据读取完毕. Debug.Log("receive zero=====" + bytesRead); client.onConnectStateChg(false, Errors.serverClosedConnection); return; } // 继续读取. client.ReceiveAsync(); } else { client.onConnectStateChg(false, Errors.connectionIsClosed); } } catch (Exception e) { Debug.Log(e); } }