public UvTcpConnection(UvThread thread, UvTcpHandle handle)
        {
            _thread = thread;
            _handle = handle;

            _input = _thread.PipelineFactory.Create();
            _output = _thread.PipelineFactory.Create();

            StartReading();
            _sendingTask = ProcessWrites();
        }
Exemple #2
0
        public UvTcpConnection(UvThread thread, UvTcpHandle handle)
        {
            _thread = thread;
            _handle = handle;

            _input  = _thread.PipelineFactory.Create();
            _output = _thread.PipelineFactory.Create();

            StartReading();
            _sendingTask = ProcessWrites();
        }
Exemple #3
0
        public UvTcpConnection(UvThread thread, UvTcpHandle handle)
        {
            _thread = thread;
            _handle = handle;

            _input = new Pipe(new PipeOptions(thread.Pool,
                                              // resume from back pressure on the uv thread
                                              writerScheduler: thread));

            _output = new Pipe(new PipeOptions(thread.Pool,
                                               // user code will dispatch back to the uv thread for writes,
                                               readerScheduler: thread));

            StartReading();
            _sendingTask = ProcessWrites();
        }
        public async Task CanCreateWorkingEchoServer_PipelineLibuvServer_NonPipelineClient()
        {
            var endpoint = new IPEndPoint(IPAddress.Loopback, 5010);
            const string MessageToSend = "Hello world!";
            string reply = null;

            using (var thread = new UvThread())
            using (var server = new UvTcpListener(thread, endpoint))
            {
                server.OnConnection(Echo);
                await server.StartAsync();

                reply = SendBasicSocketMessage(endpoint, MessageToSend);
            }
            Assert.Equal(MessageToSend, reply);
        }
        public UvTcpConnection(UvThread thread, UvTcpHandle handle)
        {
            _thread = thread;
            _handle = handle;

            _input = _thread.PipeFactory.Create(new PipeOptions
            {
                // ReaderScheduler = TaskRunScheduler.Default, // execute user code on the thread pool
                // ReaderScheduler = thread,
                WriterScheduler = thread // resume from back pressure on the uv thread
            });

            _output = _thread.PipeFactory.Create(new PipeOptions
            {
                // WriterScheduler = TaskRunScheduler.Default, // Execute the flush callback on the thread pool
                // WriterScheduler = thread,
                ReaderScheduler = thread // user code will dispatch back to the uv thread for writes,
            });

            StartReading();
            _sendingTask = ProcessWrites();
        }
        public static async Task Run()
        {
            var thread = new UvThread();
            var client = new UvTcpClient(thread, new IPEndPoint(IPAddress.Loopback, 5000));

            var consoleOutput = thread.PipelineFactory.CreateWriter(Console.OpenStandardOutput());

            var connection = await client.ConnectAsync();

            while (true)
            {
                var buffer = connection.Output.Alloc();

                buffer.Append("GET / HTTP/1.1", TextEncoding.Utf8);
                buffer.Append("\r\n\r\n", TextEncoding.Utf8);

                await buffer.FlushAsync();

                // Write the client output to the console
                await CopyCompletedAsync(connection.Input, consoleOutput);

                await Task.Delay(1000);
            }
        }
 public UvTcpListener(UvThread thread, IPEndPoint endpoint)
 {
     _thread = thread;
     _endpoint = endpoint;
 }
 public UvTcpClient(UvThread thread, IPEndPoint endPoint)
 {
     _thread = thread;
     _ipEndPoint = endPoint;
 }
Exemple #9
0
 public UvTcpClient(UvThread thread, IPEndPoint endPoint)
 {
     _thread     = thread;
     _ipEndPoint = endPoint;
 }
Exemple #10
0
 public UvTcpListener(UvThread thread, IPEndPoint endpoint)
 {
     _thread   = thread;
     _endpoint = endpoint;
 }
        public async Task RunStressPingPongTest_Libuv()
        {
            var endpoint = new IPEndPoint(IPAddress.Loopback, 5020);

            using (var thread = new UvThread())
            using (var server = new UvTcpListener(thread, endpoint))
            {
                server.OnConnection(PongServer);
                await server.StartAsync();

                const int SendCount = 500, ClientCount = 5;
                for (int loop = 0; loop < ClientCount; loop++)
                {
                    using (var client = await new UvTcpClient(thread, endpoint).ConnectAsync())
                    {
                        var tuple = await PingClient(client, SendCount);
                        Assert.Equal(SendCount, tuple.Item1);
                        Assert.Equal(SendCount, tuple.Item2);
                        Console.WriteLine($"Ping: {tuple.Item1}; Pong: {tuple.Item2}; Time: {tuple.Item3}ms");
                    }
                }
            }
        }
        public static void Run()
        {
            var ip = IPAddress.Any;
            int port = 5000;
            var thread = new UvThread();
            var listener = new UvTcpListener(thread, new IPEndPoint(ip, port));
            listener.OnConnection(async connection =>
            {
                var httpParser = new HttpRequestParser();

                while (true)
                {
                    // Wait for data
                    var result = await connection.Input.ReadAsync();
                    var input = result.Buffer;

                    try
                    {
                        if (input.IsEmpty && result.IsCompleted)
                        {
                            // No more data
                            break;
                        }

                        // Parse the input http request
                        var parseResult = httpParser.ParseRequest(ref input);

                        switch (parseResult)
                        {
                            case HttpRequestParser.ParseResult.Incomplete:
                                if (result.IsCompleted)
                                {
                                    // Didn't get the whole request and the connection ended
                                    throw new EndOfStreamException();
                                }
                                // Need more data
                                continue;
                            case HttpRequestParser.ParseResult.Complete:
                                break;
                            case HttpRequestParser.ParseResult.BadRequest:
                                throw new Exception();
                            default:
                                break;
                        }

                        // Writing directly to pooled buffers
                        var output = connection.Output.Alloc();
                        var formatter = new OutputFormatter<WritableBuffer>(output, EncodingData.InvariantUtf8);
                        formatter.Append("HTTP/1.1 200 OK");
                        formatter.Append("\r\nContent-Length: 13");
                        formatter.Append("\r\nContent-Type: text/plain");
                        formatter.Append("\r\n\r\n");
                        formatter.Append("Hello, World!");
                        await output.FlushAsync();

                        httpParser.Reset();
                    }
                    finally
                    {
                        // Consume the input
                        connection.Input.Advance(input.Start, input.End);
                    }
                }
            });

            listener.StartAsync().GetAwaiter().GetResult();

            Console.WriteLine($"Listening on {ip} on port {port}");
            var wh = new ManualResetEventSlim();
            Console.CancelKeyPress += (sender, e) =>
            {
                wh.Set();
            };

            wh.Wait();

            listener.Dispose();
            thread.Dispose();
        }