/// <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 onReceive(USocket s, byte[] bytes, int len) { MemoryStream buffer = memorystreamPool.borrowObject(); buffer.Write(bytes, 0, len); buffer.SetLength(len); enqueueData(buffer); }
public void connect(object obj = null) { isStopping = false; socket = new USocket(host, port); #if UNITY_EDITOR Debug.Log("connect ==" + host + ":" + port); #endif socket.connectAsync(connectCallback, outofLine); }
public void outofLine(USocket s, object obj) { if (!isStopping) { stop(); //CLMainBase.self.onOffline(); enqueueData(CONST_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据,存储. client.mBuffer.Write (client.mTmpBuffer, 0, bytesRead); OnReceiveCallback (client, client.mBuffer); } else if (bytesRead < 0) { if (client.offLineCallback != null) { client.offLineCallback (client, null); } client.connectCallbackFunc (client, false); client.close (); } else { // 所有Number据读取完毕. Debug.Log ("receive zero=====" + bytesRead); if (client.offLineCallback != null) { client.offLineCallback (client, null); } client.connectCallbackFunc (client, false); client.close (); return; } // 继续读取. if (client.mSocket.Connected) { client.mSocket.BeginReceive (client.mTmpBuffer, 0, client.mTmpBufferSize, 0, (AsyncCallback)ReceiveCallback, client); } } else { if (client.offLineCallback != null) { client.offLineCallback (client, null); } client.connectCallbackFunc (client, false); client.close (); } } catch (Exception e) { if (client.offLineCallback != null) { client.offLineCallback (client, null); } client.connectCallbackFunc (client, false); client.close (); Debug.Log (e); } }
public void stop() { isStopping = true; connected = false; if (socket != null) { socket.close(); } socket = null; }
//========================================== void onReceive(USocket s, object obj) { if (this.socket == null || (this.socket != null && !this.socket.Equals(s))) { return; } try { unpackMsg(s, s.mBuffer, mDispatcher); } catch (System.Exception e) { Debug.Log(e); } }
private void SendCallback (IAsyncResult ar) { USocket client = (USocket)ar.AsyncState; // 完成Number据发送. int bytesSent = client.mSocket.EndSend (ar); if (bytesSent <= 0) { //发送失败 if (client.offLineCallback != null) { client.offLineCallback (client, null); } client.close (); } client.failTimes = 0; }
public void connect(object obj) { if (socket != null) { stop(); } isStopping = false; socket = new USocket(host, port); #if UNITY_EDITOR Debug.Log("connect ==" + host + ":" + port); #endif //异步连接 socket.connectAsync(onConnectStateChg); }
private void SendCallback(IAsyncResult ar) { USocket client = (USocket)ar.AsyncState; // 完成Number据发送. int bytesSent = client.mSocket.EndSend(ar); if (bytesSent <= 0) //发送失败 { sendTimeOut(null); } else { client.failTimes = 0; } }
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); } }
void outofLine(USocket s, object obj) { if (this.socket == null || (this.socket != null && !this.socket.Equals(s))) { return; } if (!isStopping) { CLMainBase.self.onOffline(); try { if (mDispatcher != null) { mDispatcher(CONST_OutofNetConnect, this); } } catch (System.Exception e1) { Debug.Log(e1); } } }
private void connectCallback(IAsyncResult ar) { // 从stateobject获取socket. USocket client = (USocket)ar.AsyncState; if (client.mSocket.Connected) { // 完成连接. client.mSocket.EndConnect(ar); client.isActive = true; client.failTimes = 0; client.onConnectStateChg(true, Errors.success); } else { client.onConnectStateChg(false, Errors.connectFailed); } if (connectTimeout != null) { connectTimeout.Dispose(); connectTimeout = null; } }
public void unpackMsg(USocket s, MemoryStream buffer, TcpDispatchCallback dispatcher) { try { bool isLoop = true; while (isLoop) { long totalLen = buffer.Position; if (totalLen > 2) { buffer.SetLength(totalLen); object o = null; buffer.Position = 0; if (msgUnpackFunc != null) { object[] objs = Utl.doCallback(msgUnpackFunc, s, buffer); if (objs != null && objs.Length > 0) { o = objs[0]; } } else { o = defaultUnpackMsg(s, buffer); } if (o != null && dispatcher != null) { dispatcher(o, this); } long usedLen = buffer.Position; if (usedLen > 0) { long leftLen = totalLen - usedLen; if (leftLen > 0) { byte[] lessBuff = new byte[leftLen]; buffer.Read(lessBuff, 0, (int)leftLen); buffer.Position = 0; buffer.Write(lessBuff, 0, (int)leftLen); buffer.SetLength((int)leftLen); } else { buffer.Position = 0; buffer.SetLength(0); isLoop = false; } } else { buffer.Position = totalLen; isLoop = false; } } else { isLoop = false; } } } catch (System.Exception e) { Debug.LogError(e); } }