static async Task Main(string[] args)
        {
            await using ConnectionFactory connectionFactory = new SocketConnectionFactory();
            await using Connection connection = await connectionFactory.ConnectAsync(new DnsEndPoint ("microsoft.com", 80));

            await using HttpConnection httpConnection = new Http1Connection(connection, HttpPrimitiveVersion.Version11);

            await using (ValueHttpRequest request = (await httpConnection.CreateNewRequestAsync(HttpPrimitiveVersion.Version11, HttpVersionPolicy.RequestVersionExact)).Value)
            {
                request.ConfigureRequest(contentLength: 0, hasTrailingHeaders: false);
                request.WriteRequest(HttpMethod.Get, new Uri("http://microsoft.com"));
                request.WriteHeader("Accept", "text/html");
                await request.CompleteRequestAsync();

                await request.ReadToFinalResponseAsync();

                Console.WriteLine($"Final response code: {request.StatusCode}");

                if (await request.ReadToHeadersAsync())
                {
                    await request.ReadHeadersAsync(new PrintingHeadersSink(), state : null);
                }
                else
                {
                    Console.WriteLine("No headers received.");
                }

                if (await request.ReadToContentAsync())
                {
                    long totalLen = 0;

                    var buffer = new byte[4096];
                    int readLen;

                    do
                    {
                        while ((readLen = await request.ReadContentAsync(buffer)) != 0)
                        {
                            totalLen += readLen;
                        }
                    }while (await request.ReadToNextContentAsync());

                    Console.WriteLine($"Received {totalLen} byte response.");
                }
                else
                {
                    Console.WriteLine("No content received.");
                }

                if (await request.ReadToTrailingHeadersAsync())
                {
                    await request.ReadHeadersAsync(new PrintingHeadersSink(), state : null);
                }
                else
                {
                    Console.WriteLine("No trailing headers received.");
                }
            }
        }
Exemple #2
0
        static async Task ProcessAddInteger()
        {
            await using var connection = await connectionFactory.ConnectAsync(new IPEndPoint (IPAddress.Loopback, 5005));

            MultiplexingSocketProtocol <int, AddIntegerRequest> protocol = new MultiplexingSocketProtocol <int, AddIntegerRequest>(connection, new AddIntegerResponseReader(), new AddIntegerRequestWritter(), new Int32MessageIdGenerator(), new Int32MessageIdParser());
            IMessageIdGenerator idGenerator = new Int32MessageIdGenerator();
            IMultiplexingSocketClientProtocol <int, AddIntegerRequest> clientProtocol = new MultiplexingSocketClientProtocol <int, AddIntegerRequest>(protocol, idGenerator);
            int cnt = 20;

            Task[] tasks = new Task[cnt];
            for (int i = 0; i < cnt; i++)
            {
                tasks[i] = PeformAddInteger(i, i + 1, clientProtocol);
            }
            await Task.WhenAll(tasks);
        }
Exemple #3
0
        public PingPongTest()
        {
            var endpoit = new IPEndPoint(IPAddress.Parse("123.57.78.153"), 9379);
            SocketConnectionFactory client = new SocketConnectionFactory(new SocketTransportOptions());

            connection = client.ConnectAsync(endpoit).Result;
            connection.Transport.Output.WriteAsync(Encoding.UTF8.GetBytes("AUTH 0f649985e1ae10a\r\n"));
            var result = connection.Transport.Output.WriteAsync(Encoding.UTF8.GetBytes("SELECT 15\r\n")).Result;

            Thread.Sleep(3000);
            var readResult = connection.Transport.Input.ReadAsync().Result;
            var data       = Encoding.UTF8.GetString(readResult.Buffer.FirstSpan);

            Console.WriteLine(data);
            connection.Transport.Input.AdvanceTo(readResult.Buffer.End);
        }
Exemple #4
0
        public PingPongTest()
        {
            using (StreamReader stream = new StreamReader("Redis.rsf"))
            {
                ip   = stream.ReadLine();
                port = int.Parse(stream.ReadLine());
                pwd  = stream.ReadLine();
            }
            var endpoit = new IPEndPoint(IPAddress.Parse("127"), 12);
            SocketConnectionFactory client = new SocketConnectionFactory(new SocketTransportOptions());

            connection = client.ConnectAsync(endpoit).Result;
            connection.Transport.Output.WriteAsync(Encoding.UTF8.GetBytes("AUTH \r\n"));
            var result = connection.Transport.Output.WriteAsync(Encoding.UTF8.GetBytes("SELECT 15\r\n")).Result;

            Thread.Sleep(3000);
            var readResult = connection.Transport.Input.ReadAsync().Result;
            var data       = Encoding.UTF8.GetString(readResult.Buffer.FirstSpan);

            Console.WriteLine(data);
            connection.Transport.Input.AdvanceTo(readResult.Buffer.End);
        }
        static async Task Main(string[] args)
        {
            // Manual wire up of the client
            var services = new ServiceCollection().AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Debug);
                builder.AddConsole();
            });

            services.AddScoped <MessageHandler <HandshakeMessage>, HandshakeMessageHandler>();
            services.Add <MessageMetadataParser, MessageMetadata>();
            services.AddSingleton <ClientConnectionHandler>();
            services.AddSingleton(sp =>
            {
                var builder = new ServerFramingBuilder(sp);
                builder.Mappings.Add((HandshakeMessage.Id, typeof(HandshakeMessage)));
                builder.MessageWriter = new MessageWriter(builder.Parser);
                builder.MessageReader = new MessageReader();
                builder.UseMessageEncoder();

                return(builder.Build());
            });

            var serviceProvider   = services.BuildServiceProvider();
            var connectionHandler = serviceProvider
                                    .GetRequiredService <ClientConnectionHandler>();
            var clientFactory = new SocketConnectionFactory();

            Console.WriteLine("Press any key to start connection.");
            Console.ReadKey();

            await using var connection = await clientFactory.ConnectAsync(
                            new IPEndPoint (IPAddress.Loopback, 5000));

            await connectionHandler.OnConnectedAsync(connection).ConfigureAwait(false);
        }