public void Clear() { SendBuffer.Clear(); lock (clearLock) { isSend = false; if (sendDataQueue.Count > 0) { SendData cmd; while (sendDataQueue.TryDequeue(out cmd)) { } } if (InterimPacketBuffer != null) { BufferPool.Push(InterimPacketBuffer); InterimPacketBuffer = null; } while (ReceiveBuffers.Count > 0) { var packetBuffer = ReceiveBuffers.Dequeue(); BufferPool.Push(packetBuffer); } } NoComplateCmd = null; alreadyReceivePacketLength = 0; needReceivePacketLenght = 0; }
public void Clear() { lock (clearLock) { isSend = false; if (sendDataQueue.Count > 0) { SendData cmd; if (!sendDataQueue.TryDequeue(out cmd)) { SpinWait spinWait = new SpinWait(); while (sendDataQueue.TryDequeue(out cmd)) { spinWait.SpinOnce(); } } } if (InterimPacketBuffer != null) { Session.Pool.FixedBufferPool.Push(InterimPacketBuffer); InterimPacketBuffer = null; } while (ReceiveBuffers.Count > 0) { var packetBuffer = ReceiveBuffers.Dequeue(); Session.Pool.FixedBufferPool.Push(packetBuffer); } } SendBuffer.Clear(); NoComplateCmd = null; alreadyReceivePacketLength = 0; needReceivePacketLenght = 0; }
public void Clear() { SendBuffer.Clear(); lock (clearLock) { if (InterimPacketBuffer != null) { InterimPacketBuffer.Clear(); BufferPool.Push(InterimPacketBuffer); InterimPacketBuffer = null; } while (ReceiveBuffers.Count > 0) { var packetBuffer = ReceiveBuffers.Dequeue(); packetBuffer.Clear(); BufferPool.Push(packetBuffer); } } NoComplateCmd = null; alreadyReceivePacketLength = 0; needReceivePacketLenght = 0; }
private void ProcessReceiveQueue() { RUdpBuffer buffer; if (!receiveQueue.TryDequeue(out buffer)) { isReceive = false; return; } byte[] receiveBuffer = buffer.FixedBuffer.Buffer; int offset = buffer.Offset; int count = buffer.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 - uIntByteLength; ReceiveDataBuffer.WriteBuffer(cacheBuffer.Buffer, uIntByteLength, getLenght); Session.Pool.FixedBufferPool.Push(cacheBuffer); while (ReceiveBuffers.Count > 0) { var popBuffer = ReceiveBuffers.Dequeue(); ReceiveDataBuffer.WriteBuffer(popBuffer.Buffer, 0, popBuffer.DataSize); getLenght += popBuffer.DataSize; Session.Pool.FixedBufferPool.Push(popBuffer); } var needLenght = needReceivePacketLenght - getLenght - uIntByteLength; ReceiveDataBuffer.WriteBuffer(receiveBuffer, offset, needLenght); offset += needLenght; count -= needLenght; //触发获取指令事件 ReceiveData(); //清理合包数据 needReceivePacketLenght = 0; alreadyReceivePacketLength = 0; } } else { if (InterimPacketBuffer != null && alreadyReceivePacketLength == 0) { var diff = uIntByteLength - 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) { Session.Pool.FixedBufferPool.Push(InterimPacketBuffer); InterimPacketBuffer = null; ReceiveDataBuffer.WriteBuffer(receiveBuffer, offset, packetLength); ReceiveData(); offset += packetLength; count -= packetLength; } else { needReceivePacketLenght = packetLength + uIntByteLength; alreadyReceivePacketLength = uIntByteLength; } } } if (needReceivePacketLenght > 0) { while (count > 0)//遍历把数据放入缓冲器中 { if (InterimPacketBuffer == null) { InterimPacketBuffer = Session.Pool.FixedBufferPool.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 >= uIntByteLength) { //按照长度分包 int packetLength = BitConverter.ToInt32(receiveBuffer, offset); //获取包长度 if (NetByteOrder) { packetLength = IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序 } if (packetLength < 0) { var ddd = packetLength; } if ((count - uIntByteLength) >= packetLength) //如果数据包达到长度则马上进行解析 { ReceiveDataBuffer.WriteBuffer(receiveBuffer, offset + uIntByteLength, packetLength); //触发获取指令事件 ReceiveData(); int processLenght = packetLength + uIntByteLength; offset += processLenght; count -= processLenght; } else { needReceivePacketLenght = packetLength + uIntByteLength;//记录当前包总共需要多少的数据 } } else { if (InterimPacketBuffer == null) { InterimPacketBuffer = Session.Pool.FixedBufferPool.Pop(); } InterimPacketBuffer.WriteBuffer(receiveBuffer, offset, count); count = 0; } } } Session.Pool.RUdpServer.BufferPool.Push(buffer.FixedBuffer);//把缓冲块放入池中 ProcessReceiveQueue(); }
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; }
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.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.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; }
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); }