Пример #1
0
 public AsyncSendBufferManager(int bufferSize)
 {
     m_dynamicBufferManager    = new DynamicBufferManager(bufferSize);
     m_sendBufferList          = new List <SendBufferPacket>();
     m_sendBufferPacket.Offset = 0;
     m_sendBufferPacket.Count  = 0;
 }
Пример #2
0
 public AsyncSocketUserToken(int asyncReceiveBufferSize)
 {
     m_connectSocket              = null;
     m_asyncSocketInvokeElement   = 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(ProtocolConst.InitBufferSize);
     m_sendBuffer = new AsyncSendBufferManager(ProtocolConst.InitBufferSize);;
 }
Пример #3
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="asyncReceiveBufferSize"></param>
        public AsyncSocketUserToken(int asyncReceiveBufferSize)
        {
            m_connectSocket = null;

            ReceiveEventArgs           = new SocketAsyncEventArgs();
            ReceiveEventArgs.UserToken = this;
            m_asyncReceiveBuffer       = new byte[asyncReceiveBufferSize];
            ReceiveEventArgs.SetBuffer(m_asyncReceiveBuffer, 0, m_asyncReceiveBuffer.Length);//设置要用于异步套接字方法的数据缓冲区。

            SendEventArgs           = new SocketAsyncEventArgs();
            SendEventArgs.UserToken = this;
            //SendEventArgs.SetBuffer(m_asyncSendBuffer, 0, m_asyncReceiveBuffer.Length);

            ReceiveBufferManager = new DynamicBufferManager(ProtocolConst.InitBufferSize);
            SendBufferManager    = new AsyncSendBufferManager(ProtocolConst.InitBufferSize);;
        }
        /// <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);
        }
Пример #6
0
        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);
        }