private bool ProcessReceiveBuffer(byte[] receiveBuffer, int offset, int count) { while (count > 0) { if (needReceivePacketLenght > 0 && alreadyReceivePacketLength + count >= needReceivePacketLenght)//说明包已获取完成 { if (InterimPacketBuffer != null) { ReceiveBuffers.Enqueue(InterimPacketBuffer); InterimPacketBuffer = null; } if (ReceiveBuffers.Count > 0) { int getLenght = 0;//已取出数据 var cacheBuffer = ReceiveBuffers.Dequeue(); if (cacheBuffer.DataSize > checkandCmdLength) { int cachePacketLength = BitConverter.ToInt32(cacheBuffer.Buffer, 0); //获取包长度 short commondId = BitConverter.ToInt16(cacheBuffer.Buffer, intByteLength); var data = new byte[cachePacketLength - shortByteLength]; getLenght = cacheBuffer.DataSize - checkandCmdLength; System.Buffer.BlockCopy(cacheBuffer.Buffer, checkandCmdLength, data, 0, getLenght); cacheBuffer.Clear();//清理数据并装入池中 BufferPool.Push(cacheBuffer); while (ReceiveBuffers.Count > 0) { var popBuffer = ReceiveBuffers.Dequeue(); System.Buffer.BlockCopy(popBuffer.Buffer, 0, data, getLenght, popBuffer.DataSize); getLenght += popBuffer.DataSize; popBuffer.Clear();//清理数据并装入池中 BufferPool.Push(popBuffer); } var needLenght = needReceivePacketLenght - getLenght - checkandCmdLength; System.Buffer.BlockCopy(receiveBuffer, offset, data, getLenght, needLenght); offset += needLenght; count -= needLenght; //触发获取指令事件 Session.Server.ReceiveCommond(Session, new ReceiveCommond() { CommondId = commondId, Data = data }); //清理合包数据 needReceivePacketLenght = 0; alreadyReceivePacketLength = 0; } else { InterimPacketBuffer = cacheBuffer; } } } //按照长度分包 int packetLength = BitConverter.ToInt32(receiveBuffer, offset); //获取包长度 if (NetByteOrder) { packetLength = IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序 } if ((count - intByteLength) >= packetLength) //如果数据包达到长度则马上进行解析 { short commondId = BitConverter.ToInt16(receiveBuffer, offset + intByteLength); //获取指令编号 int dataLength = packetLength - shortByteLength; var data = new byte[dataLength]; System.Buffer.BlockCopy(receiveBuffer, offset + checkandCmdLength, data, 0, dataLength); //触发获取指令事件 Session.Server.ReceiveCommond(Session, new ReceiveCommond() { CommondId = commondId, Data = data }); int processLenght = packetLength + intByteLength; offset += processLenght; count -= processLenght; } else { //存入分包缓存池 needReceivePacketLenght = packetLength + intByteLength; //记录当前包总共需要多少的数据 while (count > 0) //遍历把数据放入缓冲器中 { if (InterimPacketBuffer == null) { InterimPacketBuffer = BufferPool.Pop(); } var surpos = InterimPacketBuffer.Buffer.Length - InterimPacketBuffer.DataSize;//中间buffer剩余空间 if (count > surpos) { InterimPacketBuffer.WriteBuffer(receiveBuffer, offset, surpos); ReceiveBuffers.Enqueue(InterimPacketBuffer); InterimPacketBuffer = null; alreadyReceivePacketLength += surpos;//记录已接收的数据 offset += surpos; count -= surpos; } else { InterimPacketBuffer.WriteBuffer(receiveBuffer, offset, count); alreadyReceivePacketLength += count;//记录已接收的数据 offset = 0; count = 0; } } } } return(count == 0); }
public bool ProcessReceiveBuffer(byte[] receiveBuffer, int offset, int count) { while (count > 0) { if (needReceivePacketLenght > 0 && alreadyReceivePacketLength + count >= needReceivePacketLenght)//说明包已获取完成 { if (InterimPacketBuffer != null) { ReceiveBuffers.Enqueue(InterimPacketBuffer); InterimPacketBuffer = null; } if (ReceiveBuffers.Count > 0) { int getLenght = 0;//已取出数据 var cacheBuffer = ReceiveBuffers.Dequeue(); getLenght = cacheBuffer.DataSize - intByteLength; ReceiveDataBuffer.WriteBuffer(cacheBuffer.Buffer, intByteLength, getLenght); BufferPool.Push(cacheBuffer); while (ReceiveBuffers.Count > 0) { var popBuffer = ReceiveBuffers.Dequeue(); ReceiveDataBuffer.WriteBuffer(popBuffer.Buffer, 0, popBuffer.DataSize); getLenght += popBuffer.DataSize; BufferPool.Push(popBuffer); } var needLenght = needReceivePacketLenght - getLenght - intByteLength; ReceiveDataBuffer.WriteBuffer(receiveBuffer, offset, needLenght); offset += needLenght; count -= needLenght; //触发获取指令事件 ReceiveData(); //清理合包数据 needReceivePacketLenght = 0; alreadyReceivePacketLength = 0; } } else { if (InterimPacketBuffer != null && alreadyReceivePacketLength == 0) { var diff = intByteLength - InterimPacketBuffer.DataSize; InterimPacketBuffer.WriteBuffer(receiveBuffer, offset, diff); offset += diff; count -= diff; var packetLength = BitConverter.ToInt32(InterimPacketBuffer.Buffer, 0); if (NetByteOrder) { packetLength = IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序 } if (count >= packetLength) { BufferPool.Push(InterimPacketBuffer); InterimPacketBuffer = null; ReceiveDataBuffer.WriteBuffer(receiveBuffer, offset, packetLength); ReceiveData(); offset += packetLength; count -= packetLength; } else { needReceivePacketLenght = packetLength + intByteLength; alreadyReceivePacketLength = intByteLength; } } } if (needReceivePacketLenght > 0) { while (count > 0)//遍历把数据放入缓冲器中 { if (InterimPacketBuffer == null) { InterimPacketBuffer = BufferPool.Pop(); } var surpos = InterimPacketBuffer.Buffer.Length - InterimPacketBuffer.DataSize;//中间buffer剩余空间 if (count > surpos) { InterimPacketBuffer.WriteBuffer(receiveBuffer, offset, surpos); ReceiveBuffers.Enqueue(InterimPacketBuffer); InterimPacketBuffer = null; alreadyReceivePacketLength += surpos;//记录已接收的数据 offset += surpos; count -= surpos; } else { InterimPacketBuffer.WriteBuffer(receiveBuffer, offset, count); alreadyReceivePacketLength += count;//记录已接收的数据 count = 0; } } } else { if (count > intByteLength) { //按照长度分包 int packetLength = BitConverter.ToInt32(receiveBuffer, offset); //获取包长度 if (NetByteOrder) { packetLength = IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序 } if ((count - intByteLength) >= packetLength) //如果数据包达到长度则马上进行解析 { ReceiveDataBuffer.WriteBuffer(receiveBuffer, offset + intByteLength, packetLength); //触发获取指令事件 ReceiveData(); int processLenght = packetLength + intByteLength; offset += processLenght; count -= processLenght; } else { needReceivePacketLenght = packetLength + intByteLength;//记录当前包总共需要多少的数据 } } else { if (InterimPacketBuffer == null) { InterimPacketBuffer = BufferPool.Pop(); } InterimPacketBuffer.WriteBuffer(receiveBuffer, offset, count); count = 0; } } } return(count == 0); }