Пример #1
0
        static async Task ServeFixed(RioConnectionOrientedSocket socket)
        {
            try
            {
                var buffer = new byte[128 * pipeLineDeph];
                var leftoverLength = 0;
                var oldleftoverLength = 0;
                uint endOfRequest = 0x0a0d0a0d;
                uint current = 0;
                var stream = new RioStream(socket);

                while (true)
                {
                    int r = await stream.ReadAsync(buffer, 0, buffer.Length);
                    if (r == 0)
                        break;


                    for (int i = 0; leftoverLength != 0 && i < 4 - leftoverLength; i++)
                    {
                        current += buffer[i];
                        current = current << 8;
                        if (current == endOfRequest)
                            socket.WritePreAllocated(currentSegment);
                    }

                    leftoverLength = r % 4;
                    var length = r - leftoverLength;

                    unsafe
                    {
                        fixed (byte* currentPtr = &buffer[oldleftoverLength])
                        {
                            var start = currentPtr;
                            var end = currentPtr + length;

                            for (; start <= end; start++)
                            {
                                if (*(uint*)start == endOfRequest)
                                    socket.WritePreAllocated(currentSegment);
                            }
                        }
                    }

                    oldleftoverLength = leftoverLength;

                    for (int i = r - leftoverLength; i < r; i++)
                    {
                        current += buffer[i];
                        current = current << 4;
                    }
                    stream.Flush();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                socket.Dispose();
            }
        }
Пример #2
0
        public async static Task<int> doit()
        {
            {
                var buffer = new byte[140 * pipeLineDeph];
                var leftoverLength = 0;
                var oldleftoverLength = 0;
                uint endOfRequest = 0x0a0d0a0d;
                uint current = 0;
                int responses = 0;
                int total = 0;

                RioConnectionOrientedSocket connection;
                connection = await clientPool.Connect(uri);
                var stream = new RioStream(connection);
                while (timer.Elapsed < span)
                {
                    //check if connection is valid?                
                    connection.WriteFixed(rb);

                    while (responses < pipeLineDeph)
                    {
                        int r = await stream.ReadAsync(buffer, 0, buffer.Length);
                        if (r == 0)
                            break;

                        for (int i = 0; leftoverLength != 0 && i < 4 - leftoverLength; i++)
                        {
                            current += buffer[i];
                            current = current << 8;
                            if (current == endOfRequest)
                                responses++;
                        }

                        leftoverLength = r % 4;
                        var length = r - leftoverLength;

                        unsafe
                        {
                            fixed (byte* apa = &buffer[oldleftoverLength])
                            {
                                var start = apa;
                                var end = apa + length;

                                for (; start <= end; start++)
                                {
                                    if (*(uint*)start == endOfRequest)
                                        responses++;
                                }
                            }
                        }

                        oldleftoverLength = leftoverLength;

                        for (int i = r - leftoverLength; i < r; i++)
                        {
                            current += buffer[i];
                            current = current << 4;
                        }

                    }
                    total += responses;
                    responses = 0;

                    if (!keepAlive)
                        connection.Dispose();
                }
                connection.Dispose();
                return total;
            }
        }