예제 #1
0
        public static void clientDisconnect()
        {
            listener.OnAccepted = (RioSocket s) =>
            {
                while (true)
                {
                    s.BeginReceive().GetResult().Dispose();
                }

                //s.Dispose();
            };
            while (running)
            {
                try
                {
                    //    Thread.Sleep(100);
                    var socket = clientPool.Connect(new Uri("http://localhost:5000/")).Result;
                    //socket.Dispose();
                    while (true)
                    {
                        socket.Send(new byte[] { 1, 2, 3 });
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
예제 #2
0
        public async static Task ResetConnection(ConnectionState state)
        {
            while (timer.Elapsed < span)
            {
                try
                {
                    state.socket = await clientPool.Connect(uri);

                    state.reader.Socket = state.socket;
                    state.reader.Start();
                    state.socket.Send(reqz[pipeLineDeph]);
                    return;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                    state.failedConnections++;
                    continue;
                }
            }

            state.tcs.SetResult(state);
        }
예제 #3
0
        public static async Task ClientTcp()
        {
            RioTcpClientPool l    = new RioTcpClientPool(new RioFixedBufferPool(Connections, Transfer), new RioFixedBufferPool(Connections, Transfer), (uint)Connections);
            int totalBytesRecived = 0;
            int currentRecived    = 0;
            TaskCompletionSource <object> tcs;

            for (int i = 0; i < Iterations; i++)
            {
                var s = await l.Connect(new Uri(Target));

                var reader = new RioSegmentReader(s);

                if (Pattern == "PushPull")
                {
                    while (totalBytesRecived < Transfer)
                    {
                        tcs = new TaskCompletionSource <object>();
                        s.Send(new byte[PushBytes]);
                        reader.OnIncommingSegment = seg =>
                        {
                            totalBytesRecived += seg.CurrentContentLength;
                            currentRecived    += seg.CurrentContentLength;
                            if (currentRecived >= PullBytes)
                            {
                                tcs.SetResult(null);
                            }
                        };
                        await tcs.Task;
                    }
                }
                else if (Pattern == "Push")
                {
                    s.Send(new byte[Transfer]);
                }
                else if (Pattern == "Pull")
                {
                    tcs = new TaskCompletionSource <object>();

                    reader.OnIncommingSegment = seg =>
                    {
                        totalBytesRecived += seg.CurrentContentLength;
                        if (totalBytesRecived >= Transfer)
                        {
                            tcs.SetResult(null);
                        }
                    };
                    await tcs.Task;
                }
                else if (Pattern == "Duplex")
                {
                    tcs = new TaskCompletionSource <object>();
                    s.Send(new byte[Transfer / 2]);
                    reader.OnIncommingSegment = seg =>
                    {
                        totalBytesRecived += seg.CurrentContentLength;
                        if (totalBytesRecived >= Transfer / 2)
                        {
                            tcs.SetResult(null);
                        }
                    };
                }
            }
        }
예제 #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);
            }
        }