public bool ClearFirstPacket() { if (m_sendBufferList.Count <= 0) { return(false); } int count = m_sendBufferList[0].Count; m_dynamicBufferManager.Clear(count); m_sendBufferList.RemoveAt(0); return(true); }
/// <summary> /// 接收异步事件返回的数据,用于对数据进行缓存和分包 /// 把收到数据先写入一个缓存,然后进行分包, /// 分包后压给包处理函数ProcessPacket, /// ProcessPacket函数然后调用ProcessCommand处理具体的命令 /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public virtual bool ProcessReceive(byte[] buffer, int offset, int count) // { m_activeDT = DateTime.UtcNow; DynamicBufferManager receiveBuffer = m_asyncSocketUserToken.ReceiveBuffer; receiveBuffer.WriteBuffer(buffer, offset, count); bool result = true; while (receiveBuffer.DataCount > sizeof(int)) { //按照长度分包 int packetLength = BitConverter.ToInt32(receiveBuffer.Buffer, 0); //获取包长度 if (NetByteOrder) { packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序 } if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) //最大Buffer异常保护 { return(false); } if ((receiveBuffer.DataCount - sizeof(int)) >= packetLength) //收到的数据达到包长度 { result = ProcessPacket(receiveBuffer.Buffer, sizeof(int), packetLength); if (result) { receiveBuffer.Clear(packetLength + sizeof(int)); //从缓存中清理 } else { return(result); } } else { return(true); } } return(true); }