protected override void InitChannel(IChannel channel)
        {
            IHttp2Connection connection = new DefaultHttp2Connection(false);

            _connectionHandler = new HttpToHttp2ConnectionHandlerBuilder()
            {
                FrameListener = new DelegatingDecompressorFrameListener(
                    connection,
                    new InboundHttp2ToHttpAdapterBuilder(connection)
                {
                    MaxContentLength    = _maxContentLength,
                    IsPropagateSettings = true
                }.Build()),
                FrameLogger = Logger,
                Connection  = connection
            }.Build();
            _responseHandler = new HttpResponseHandler();
            _settingsHandler = new Http2SettingsHandler(channel.NewPromise());
            if (_cert != null)
            {
                ConfigureSsl(channel);
            }
            else
            {
                ConfigureClearText(channel);
            }
        }
Esempio n. 2
0
        static async Task Main(string[] args)
        {
            ExampleHelper.SetConsoleLogger();

            bool useLibuv = ClientSettings.UseLibuv;

            Console.WriteLine($"Transport type : {(useLibuv ? "Libuv" : "Socket")}");

            IEventLoopGroup group;

            if (useLibuv)
            {
                group = new EventLoopGroup();
            }
            else
            {
                group = new MultithreadEventLoopGroup();
            }

            X509Certificate2 cert       = null;
            string           targetHost = null;

            if (ClientSettings.IsSsl)
            {
                cert       = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
                targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
            }

            try
            {
                var bootstrap = new Bootstrap();
                bootstrap
                .Group(group)
                .Option(ChannelOption.TcpNodelay, true);

                if (useLibuv)
                {
                    bootstrap.Channel <TcpChannel>();
                }
                else
                {
                    bootstrap.Channel <TcpSocketChannel>();
                }

                Http2ClientInitializer initializer = new Http2ClientInitializer(cert, targetHost, int.MaxValue);
                bootstrap.Handler(initializer);

                IChannel channel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port));

                try
                {
                    Console.WriteLine($"Connected to [{ClientSettings.Host}:{ClientSettings.Port}]");

                    // Wait for the HTTP/2 upgrade to occur.
                    Http2SettingsHandler http2SettingsHandler = initializer.SettingsHandler;
                    await http2SettingsHandler.AwaitSettings(TimeSpan.FromSeconds(5));

                    HttpResponseHandler responseHandler = initializer.ResponseHandler;
                    int         streamId = 3;
                    HttpScheme  scheme   = ClientSettings.IsSsl ? HttpScheme.Https : HttpScheme.Http;
                    AsciiString hostName = new AsciiString(ClientSettings.Host.ToString() + ':' + ClientSettings.Port);
                    Console.WriteLine("Sending request(s)...");

                    var url      = ExampleHelper.Configuration["url"];
                    var url2     = ExampleHelper.Configuration["url2"];
                    var url2Data = ExampleHelper.Configuration["url2data"];

                    if (!string.IsNullOrEmpty(url))
                    {
                        // Create a simple GET request.
                        IFullHttpRequest request = new DefaultFullHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Get, url, Unpooled.Empty);
                        request.Headers.Add(HttpHeaderNames.Host, hostName);
                        request.Headers.Add(HttpConversionUtil.ExtensionHeaderNames.Scheme, scheme.Name);
                        request.Headers.Add(HttpHeaderNames.AcceptEncoding, HttpHeaderValues.Gzip);
                        request.Headers.Add(HttpHeaderNames.AcceptEncoding, HttpHeaderValues.Deflate);
                        responseHandler.Put(streamId, channel.WriteAsync(request), channel.NewPromise());
                        streamId += 2;
                    }

                    if (!string.IsNullOrEmpty(url2))
                    {
                        // Create a simple POST request with a body.
                        IFullHttpRequest request = new DefaultFullHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Post, url2,
                                                                              Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(url2Data)));
                        request.Headers.Add(HttpHeaderNames.Host, hostName);
                        request.Headers.Add(HttpConversionUtil.ExtensionHeaderNames.Scheme, scheme.Name);
                        request.Headers.Add(HttpHeaderNames.AcceptEncoding, HttpHeaderValues.Gzip);
                        request.Headers.Add(HttpHeaderNames.AcceptEncoding, HttpHeaderValues.Deflate);
                        responseHandler.Put(streamId, channel.WriteAsync(request), channel.NewPromise());
                    }

                    channel.Flush();
                    await responseHandler.AwaitResponses(TimeSpan.FromSeconds(5));

                    Console.WriteLine("Finished HTTP/2 request(s)");
                    Console.ReadKey();
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.ToString());
                    Console.WriteLine("Press any key to exit");
                    Console.ReadKey();
                }
                finally
                {
                    // Wait until the connection is closed.
                    await channel.CloseAsync();
                }
            }
            finally
            {
                await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            }
        }