コード例 #1
0
        public bool ClearFirstPacket()
        {
            if (_sendBufferList.Count <= 0)
            {
                return(false);
            }
            int count = _sendBufferList[0].Count;

            _bufferManager.Clear(count);
            _sendBufferList.RemoveAt(0);
            return(true);
        }
コード例 #2
0
        protected virtual bool Receive(out byte[] buffer, out int offset, out int count)
        {
            _recvBuffer.Clear();
            Client.Receive(_recvBuffer.Buffer, sizeof(int), SocketFlags.None);
            int packetLength = BitConverter.ToInt32(_recvBuffer.Buffer, 0); //获取包长度

            if (NetByteOrder)
            {
                packetLength = IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序
            }
            //保证接收有足够的空间
            _recvBuffer.SetBufferSize(sizeof(int) + packetLength);
            Client.Receive(_recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None);

            OnReceive(_recvBuffer, packetLength, out buffer, out offset, out count);
            return(true);
        }
コード例 #3
0
        //接收异步事件返回的数据,用于对数据进行缓存和分包
        internal bool ProcessReceive(byte[] buffer, int offset, int count)
        {
            _activeTime = DateTime.UtcNow;
            DynamicBufferManager receiveBuffer = _token.ReceiveBuffer;

            receiveBuffer.WriteBuffer(buffer, offset, count);
            bool result = true;

            while (receiveBuffer.Count > sizeof(int))
            {
                //按照长度分包
                int packetLength = BitConverter.ToInt32(receiveBuffer.Buffer, 0); //获取包长度
                if (NetByteOrder)
                {
                    packetLength = IPAddress.NetworkToHostOrder(packetLength);                    //把网络字节顺序转为本地字节顺序
                }
                if ((packetLength > 10 * 1024 * 1024) | (receiveBuffer.Count > 10 * 1024 * 1024)) //最大Buffer异常保护
                {
                    return(false);
                }

                if ((receiveBuffer.Count - 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);
        }
コード例 #4
0
 public void Send(byte[] buffer, int offset, int count)
 {
     _sendBuffer.Clear();
     OnSend(_sendBuffer, buffer, offset, count);
     Client.Send(_sendBuffer.Buffer, 0, _sendBuffer.Count, SocketFlags.None);
 }