private void RecvFromSocket() { while (true) { if (!connected()) { Debug.LogError("not connected!"); break; } if (_msgLen == 0) { //header if (_socket.Available < 4) { continue; } _socket.Receive(_headBuffer); Int32 value = (Int32)BitConverter.ToUInt32(_headBuffer, 0); _msgLen = (Int32)IPAddress.NetworkToHostOrder(value); if (_msgLen > MAX_RECEIVE_SIZE) { Debug.LogError("package length exception :" + _msgLen); return; } _bodyBuffer = new byte[_msgLen]; } else { //body if (_socket.Available < _msgLen) { continue; } _socket.Receive(_bodyBuffer); ByteArray byteArray = NetMgr.GetInstance().byteArrayPool.Pop(); //is Zip byte isZip = _bodyBuffer[0]; if (isZip == 1) { int zipBuffSize = _bodyBuffer.Length - 1; byte[] zipBuff = new byte[zipBuffSize]; Array.Copy(_headBuffer, 1, zipBuff, 0, zipBuff.Length); byte[] unZipBuff = Ionic.Zlib.ZlibStream.UncompressBuffer(zipBuff); byteArray.CreateFromBuff(unZipBuff, false); } else { byteArray.CreateFromBuff(_bodyBuffer, true); } _bodyBuffer = null; _msgLen = 0; //recv success recvQueue.Enqueue(byteArray); } } }
private void RecvFromSocket() { byte[] bytes = _socket.Recv(); if (bytes != null) { Buffer.BlockCopy(bytes, 0, _headBuffer, 0, 4); Int32 value = (Int32)BitConverter.ToUInt32(_headBuffer, 0); Int32 _msgLen = (Int32)IPAddress.NetworkToHostOrder(value) - 1; _bodyBuffer = new byte[_msgLen]; Buffer.BlockCopy(bytes, 5, _bodyBuffer, 0, _msgLen); byte isZip = bytes[4]; ByteArray byteArray = NetMgr.GetInstance().byteArrayPool.Pop(); if (isZip == 1) { byte[] unZipBuff = Ionic.Zlib.ZlibStream.UncompressBuffer(_bodyBuffer); byteArray.CreateFromBuff(unZipBuff, false); } else { byteArray.CreateFromBuff(_bodyBuffer, false); } this.recvQueue.Enqueue(byteArray); } }