private void onReceive(Socket sock, byte[] bytes, int len) { try { var clientInfo = clients[sock]; if (len > 0 && bytes != null) { if (len > clientInfo.mByteBuffer.canUse()) { MyByteBuffer byteBuffer = new MyByteBuffer(clientInfo.mByteBuffer.maxSize() * 2 + len, false); byteBuffer.copyFrom(clientInfo.mByteBuffer); clientInfo.mByteBuffer = byteBuffer; } clientInfo.mByteBuffer.put(bytes, len); do { if (clientInfo.mByteBuffer.remaining() < 4) { break; } int bufLen = clientInfo.mByteBuffer.readInt32(); if (bufLen > clientInfo.mByteBuffer.maxSize() || bufLen <= 0) { Logger.err("数据长度超过限制: " + bufLen); break; } if (clientInfo.mByteBuffer.remaining() < bufLen) { clientInfo.mByteBuffer.resetHead(4); //Log(EventId.Net_Error, "remaining 长度不够:" + bufLen); break; } int msgID = clientInfo.mByteBuffer.readInt32(); // byte[] content = null; if (bufLen - 4 > 0) { content = new byte[bufLen - 4]; clientInfo.mByteBuffer.readBytes(content); } if (msgID == FrameMsgID.FrameCMD) { var cmd = new MoveCMD(); cmd.Deserialize(content); lock (clientInfo.lockObj) { clientInfo.cmdList.AddLast(cmd); } } // Logger.err("收到客户端数据:" + BitConverter.ToInt32(content,0)); //////////MessageUtils.sendToGameLogic(msgId, content); }while (true); } } catch (Exception ex) { Logger.err(ex.ToString()); } }
private void onReceive(byte[] bytes, int len) { try { if (len > 0 && bytes != null) { if (len > mByteBuffer.canUse()) { MyByteBuffer byteBuffer = new MyByteBuffer(mByteBuffer.maxSize() * 2 + len, false); byteBuffer.copyFrom(mByteBuffer); mByteBuffer = byteBuffer; } mByteBuffer.put(bytes, len); do { if (mByteBuffer.remaining() < 4) { break; } int tmp = mByteBuffer.readInt32(); int bufLen = (tmp & 0x7FFFFF); if (bufLen > mByteBuffer.maxSize() || bufLen <= 0) { Debug.LogError("数据长度超过限制: " + bufLen); break; } if (mByteBuffer.remaining() < bufLen) { mByteBuffer.resetHead(4); //Log(EventId.Net_Error, "remaining 长度不够:" + bufLen); break; } int msgID = mByteBuffer.readInt32(); // byte[] content = null; if (bufLen - 4 > 0) { content = new byte[bufLen - 4]; mByteBuffer.readBytes(content); } lock (lockObj) { msgQueue.Enqueue(new MessageData { msgID = msgID, data = content }); } // Logger.err("收到服务器数据:" + BitConverter.ToInt32(content, 0)); //////////MessageUtils.sendToGameLogic(msgId, content); }while (true); } } catch (Exception ex) { Logger.err(ex.ToString()); } }