Ejemplo n.º 1
0
 public AsyncSendBufferManager(int bufferSize)
 {
     m_dynamicBufferManager    = new DynamicBufferManager(bufferSize);
     m_sendBufferList          = new List <SendBufferPacket>();
     m_sendBufferPacket.Offset = 0;
     m_sendBufferPacket.Count  = 0;
 }
Ejemplo n.º 2
0
 public AsyncSocketUserToken(int asyncReceiveBufferSize)
 {
     m_connectSocket              = null;
     m_receiveEventArgs           = new SocketAsyncEventArgs();
     m_receiveEventArgs.UserToken = this;
     m_asyncReceiveBuffer         = new byte[asyncReceiveBufferSize];
     m_receiveEventArgs.SetBuffer(m_asyncReceiveBuffer, 0, m_asyncReceiveBuffer.Length);
     m_sendEventArgs           = new SocketAsyncEventArgs();
     m_sendEventArgs.UserToken = this;
     m_receiveBuffer           = new DynamicBufferManager(Setting.BufferSize);
     m_sendBuffer = new AsyncSendBufferManager(Setting.BufferSize);;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 接收异步事件返回的数据,用于对数据进行缓存和分包
        /// 把收到数据先写入一个缓存,然后进行分包,
        /// 分包后压给包处理函数ProcessPacket,
        /// ProcessPacket函数然后调用ProcessCommand处理具体的命令
        /// </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) //
        {
            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 > 10 * 1024 * 1024) | (receiveBuffer.DataCount > 10 * 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);
        }