/// <summary> /// 接收异步事件返回的数据,用于对数据进行缓存和分包,这是原始数据的包 /// </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) { ActiveDT = DateTime.UtcNow; DynamicBufferManager receiveBuffer = 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) { Analysis.IntTotalMsg++; //处理包 result = ProcessPacket(receiveBuffer.Buffer, sizeof(int), packetLength); if (result)//处理完成,从缓存中清理 { receiveBuffer.Clear(packetLength + sizeof(int)); } else { return(result); } } else { return(true); } } return(true); }
public virtual bool ProcessReceive(byte[] buffer, int offset, int count) { m_activeDateTime = 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 (m_netByteOrder) { packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); } if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 1024 * 1024)) { return(false); } if ((receiveBuffer.DataCount - sizeof(int)) >= packetLength) { //解析数据 result = ProcessPacket(receiveBuffer.Buffer, sizeof(int), packetLength); if (result) { receiveBuffer.Clear(sizeof(int) + packetLength); } else { return(result); } } else { return(true); } } return(true); }
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 > 100 * 1024 * 1024) || (receiveBuffer.DataCount > 100 * 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); }