/// <summary> /// 从RecvBuffer 中切分出多个Packets, 不足一个 Packet 的部分, 存留在 Buffer 中留待下次Split /// </summary> private void SplitPackets() { try { int offset = 0; while (m_nRecvBufferSize > m_headLengthConverter.VTypeLength) { try { int nLength = (int)(uint)m_headLengthConverter.Decode(m_recvBuffer, ref offset); if (m_nRecvBufferSize >= nLength) { int realLength = nLength - m_headLengthConverter.VTypeLength - RESERVE_SIZE; offset += RESERVE_SIZE; byte[] packet = new byte[realLength]; Buffer.BlockCopy(m_recvBuffer, offset, packet, 0, realLength); lock (m_recvQueueLocker)//此处理为独立线程处理,需加锁,否则会出现丢包 { m_recvQueue.Enqueue(packet); } m_nRecvBufferSize -= nLength; offset += realLength; } else { offset -= m_headLengthConverter.VTypeLength;//m_headLengthConverter.Decode()自带偏移,需要调整偏移 break; } } catch (Exception ex) { //LoggerHelper.Except(ex); Debug.LogException(ex); break; } } // 整理 RecvBuffer, 将buffer 内容前移 Buffer.BlockCopy(m_recvBuffer, offset, m_recvBuffer, 0, m_nRecvBufferSize); } catch (Exception ex) { //LoggerHelper.Except(ex); //LoggerHelper.Critical("SplitPackets error."); Debug.LogException(ex); Close(); } }