/// <summary> /// 协议组装/继续接收 /// </summary> /// <param name="buffer">byte[]</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_socketUserToken.ReceiveBuffer; receiveBuffer.WriteBuffer(buffer, offset, count);//把刚刚接收的信息加入到缓存中 bool result = true; //按照长度分包 int packetLength = BitConverter.ToInt32(receiveBuffer.Buffer, 0); //获取包长度 if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) //最大Buffer异常保护 { return(false); } if (receiveBuffer.DataCount >= packetLength) //packetLength收到的数据达到包长度 { //命令 组装-如果组装失败则继续接收:如果组装成功-则删除 result = ProcessPacket(receiveBuffer.Buffer, offset, packetLength); if (result) { receiveBuffer.Clear(packetLength);//清理已经处理的协议 } else { return(result); } } return(true); }
/// <summary> /// 协议组装/继续接收 /// </summary> /// <param name="buffer">byte[]</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_socketUserToken.ReceiveBuffer; //receiveBuffer.WriteBuffer(buffer, offset, count);//把刚刚接收的信息加入到缓存中 //bool result = true; ////按照长度分包 //int packetLength = BitConverter.ToInt32(receiveBuffer.Buffer, 0); //获取包长度 //if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) //最大Buffer异常保护 // return false; //if (receiveBuffer.DataCount >= packetLength) //packetLength收到的数据达到包长度 //{ // //命令 组装-如果组装失败则继续接收:如果组装成功-则删除 // result = ProcessPacket(receiveBuffer.Buffer, offset, packetLength); // if (result) // receiveBuffer.Clear(packetLength);//清理已经处理的协议 // else // return result; //} //return true; ///////////******下面的程序考虑粘包的情况。******////// m_activeDT = DateTime.UtcNow; DynamicBufferManager receiveBuffer = m_socketUserToken.ReceiveBuffer; receiveBuffer.WriteBuffer(buffer, offset, count); bool result = true; while (true) { if (receiveBuffer.Buffer.Length > 11) { // int packetLength = (int)((receiveBuffer.Buffer[1] << 8) | receiveBuffer.Buffer[2]); int packetLength = getLength(receiveBuffer.Buffer[11]); // Console.WriteLine(packetLength+"-------------------"); //if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) if ((packetLength > 16064)) { DelegateState.ServerStateInfo("(packetLength > 16064 packetLength= " + packetLength.ToString()); return(false); } if ((receiveBuffer.DataCount > 10 * 1024 * 1024)) { DelegateState.ServerStateInfo("(receiveBuffer.DataCount> 10 * 1024* 1024 receiveBuffer.DataCount > 10 * 1024* 1024 "); return(false); } if (packetLength == 0) { return(false); } if (receiveBuffer.DataCount >= packetLength) { // while (1) { result = ProcessPacket(receiveBuffer.Buffer, offset, packetLength); if (result) { receiveBuffer.Clear(packetLength); if (receiveBuffer.DataCount > 0) { continue; } else { break; } } else { return(result); } } } else { return(true); } } else { return(true); } } return(true); }