Beispiel #1
0
 public SendBufferManager(int bufferSize)
 {
     _bufferManager           = new DynamicBufferManager(bufferSize);
     _sendBufferList          = new List <SendBufferPacket>();
     _sendBufferPacket.Offset = 0;
     _sendBufferPacket.Count  = 0;
 }
Beispiel #2
0
 protected AppBaseClient(int timeout = 1000, int receiveBufferSize = 1024 *4)
 {
     _client = new TcpClient();
     _client.Client.Blocking = true;
     Timeout     = timeout;
     _recvBuffer = new DynamicBufferManager(receiveBufferSize);
     _sendBuffer = new DynamicBufferManager(receiveBufferSize);
 }
Beispiel #3
0
 public UserToken(int asyncReceiveBufferSize, int initBufferSize = 1024 *4)
 {
     _connectSocket              = null;
     _protocol                   = null;
     _receiveEventArgs           = new SocketAsyncEventArgs();
     _receiveEventArgs.UserToken = this;
     _asyncReceiveBuffer         = new byte[asyncReceiveBufferSize];
     _receiveEventArgs.SetBuffer(_asyncReceiveBuffer, 0, _asyncReceiveBuffer.Length);
     _sendEventArgs           = new SocketAsyncEventArgs();
     _sendEventArgs.UserToken = this;
     _receiveBuffer           = new DynamicBufferManager(initBufferSize);
     _sendBuffer = new SendBufferManager(initBufferSize);;
 }
Beispiel #4
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);
        }