示例#1
0
        public static void ListenTcp()
        {
            RioTcpListener l = new RioTcpListener(new RioFixedBufferPool(16000, 65536), new RioFixedBufferPool(16000, 65536), (uint)Connections * 2, 16000, 16000);

            l.OnAccepted = s =>
            {
                RioStream r              = new RioStream(s);
                int       totalRecived   = 0;
                int       currentRecived = 0;
                var       reader         = new RioSegmentReader(s);

                var pb = new byte[PullBytes];
                if (Pattern == "PushPull")
                {
                    reader.OnIncommingSegment = seg =>
                    {
                        totalRecived   += seg.CurrentContentLength;
                        currentRecived += seg.CurrentContentLength;
                        if (currentRecived >= PushBytes)
                        {
                            r.Write(pb, 0, pb.Length);
                            //s.WriteFixed(pb);
                            currentRecived = 0;
                        }
                    };
                    reader.Start();
                }
                else if (Pattern == "Pull")
                {
                    r.Write(new byte[Transfer], 0, Transfer);
                }
                //s.WriteFixed(new byte[Transfer]);
                else if (Pattern == "Push")
                {
                    reader.OnIncommingSegment = seg =>
                    {
                        totalRecived += seg.CurrentContentLength;
                    };
                    reader.Start();
                }
                else if (Pattern == "Duplex")
                {
                    s.Send(new byte[Transfer / 2]);
                    reader.OnIncommingSegment = seg =>
                    {
                        totalRecived += seg.CurrentContentLength;
                        //if (apa >= Transfer / 2)
                        //    tcs.SetResult(null);
                    };
                }
            };
            l.Listen(new IPEndPoint(new IPAddress(new byte[] { 0, 0, 0, 0 }), Port), 1024);
        }
示例#2
0
        public async static Task<ConnectionState> ExecuteStream()
        {
            var buffer = new byte[256 * pipeLineDeph];
            var leftoverLength = 0;
            var oldleftoverLength = 0;
            uint endOfRequest = 0x0a0d0a0d;
            uint current = 0;
            int responses = 0;

            ConnectionState state = new ConnectionState();

            RioSocket connection = null;
            RioStream stream = null;

            while (timer.Elapsed < span)
            {
                if (connection == null)
                {
                    try
                    {
                        connection = await clientPool.Connect(uri);
                        connection.SetLinger(0);
                        stream = new RioStream(connection);

                        //stream.Dispose();
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                        continue;
                    }
                }

                //check if connection is valid?                
                connection.Send(reqz[pipeLineDeph]);

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

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

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

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

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

                    oldleftoverLength = leftoverLength;

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

                }
                state.Requests += responses;
                responses = 0;

                if (!keepAlive)
                {
                    stream.Dispose();
                    connection.Dispose();
                    connection = null;
                    stream = null;
                    Thread.Sleep(100);
                }
            }
            stream?.Dispose();
            connection?.Dispose();
            return state;
        }
示例#3
0
        static async Task Servebuff(RioSocket 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)
                        {
                            stream.Write(responseBytes, 0, responseBytes.Length);
                        }
                    }

                    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)
                                {
                                    stream.Write(responseBytes, 0, responseBytes.Length);
                                }
                            }
                        }
                    }

                    stream.Flush();
                    oldleftoverLength = leftoverLength;

                    for (int i = r - leftoverLength; i < r; i++)
                    {
                        current += buffer[i];
                        current  = current << 4;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                socket.Dispose();
            }
        }
示例#4
0
        public async static Task <int> Execute()
        {
            {
                var  buffer            = new byte[256 * pipeLineDeph];
                var  leftoverLength    = 0;
                var  oldleftoverLength = 0;
                uint endOfRequest      = 0x0a0d0a0d;
                uint current           = 0;
                int  responses         = 0;
                int  total             = 0;

                RioSocket connection = null;
                RioStream stream     = null;


                while (timer.Elapsed < span)
                {
                    if (connection == null)
                    {
                        try
                        {
                            connection = await clientPool.Connect(uri);

                            stream = new RioStream(connection);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }

                    //check if connection is valid?
                    connection.WriteFixed(requestBytes);

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

                        if (bytesRead == 0)
                        {
                            break;
                        }

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

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

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

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

                        oldleftoverLength = leftoverLength;

                        for (int i = bytesRead - leftoverLength; i < bytesRead; i++)
                        {
                            current += buffer[i];
                            current  = current << 4;
                        }
                    }
                    total    += responses;
                    responses = 0;

                    if (!keepAlive)
                    {
                        stream.Dispose();
                        connection.Dispose();
                        connection = null;
                    }
                }

                connection?.Dispose();
                return(total);
            }
        }