コード例 #1
0
        public void ReceiveRstStream()
        {
            _frameInboundWriter.WriteInboundHeaders(3, _request, 31, false);

            IHttp2Stream stream = _frameCodec.Connection.Stream(3);

            Assert.NotNull(stream);
            Assert.Equal(Http2StreamState.Open, stream.State);

            IHttp2HeadersFrame expectedHeaders = new DefaultHttp2HeadersFrame(_request, false, 31);
            IHttp2HeadersFrame actualHeaders   = _inboundHandler.ReadInbound <IHttp2HeadersFrame>();

            expectedHeaders.Stream = actualHeaders.Stream;
            Assert.Equal(expectedHeaders, actualHeaders);

            _frameInboundWriter.WriteInboundRstStream(3, Http2Error.NoError);

            IHttp2ResetFrame expectedRst = new DefaultHttp2ResetFrame(Http2Error.NoError)
            {
                Stream = actualHeaders.Stream
            };
            IHttp2ResetFrame actualRst = _inboundHandler.ReadInbound <IHttp2ResetFrame>();

            Assert.Equal(expectedRst, actualRst);

            Assert.Null(_inboundHandler.ReadInbound());
        }
コード例 #2
0
        private void TestDecodeFullResponseHeaders0(bool withStreamId)
        {
            EmbeddedChannel ch      = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
            IHttp2Headers   headers = new DefaultHttp2Headers();

            headers.Scheme = HttpScheme.Http.Name;
            headers.Status = HttpResponseStatus.OK.CodeAsText;

            IHttp2HeadersFrame frame = new DefaultHttp2HeadersFrame(headers, true);

            if (withStreamId)
            {
                frame.Stream = new TestHttp2FrameStream();
            }

            Assert.True(ch.WriteInbound(frame));

            IFullHttpResponse response = ch.ReadInbound <IFullHttpResponse>();

            try
            {
                Assert.Equal(HttpResponseStatus.OK, response.Status);
                Assert.Equal(HttpVersion.Http11, response.ProtocolVersion);
                Assert.Equal(0, response.Content.ReadableBytes);
                Assert.True(response.TrailingHeaders.IsEmpty);
                Assert.False(HttpUtil.IsTransferEncodingChunked(response));
                if (withStreamId)
                {
                    Assert.Equal(1, response.Headers.GetInt(HttpConversionUtil.ExtensionHeaderNames.StreamId, 0));
                }
            }
            finally
            {
                response.Release();
            }

            Assert.Null(ch.ReadInbound <object>());
            Assert.False(ch.Finish());
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: ywscr/SpanNetty
        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)
                .Option(ChannelOption.SoKeepalive, true);
                if (useLibuv)
                {
                    bootstrap.Channel <TcpChannel>();
                }
                else
                {
                    bootstrap.Channel <TcpSocketChannel>();
                }

                bootstrap.Handler(new Http2ClientFrameInitializer(cert, targetHost));

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

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

                    Http2ClientStreamFrameResponseHandler streamFrameResponseHandler =
                        new Http2ClientStreamFrameResponseHandler();

                    Http2StreamChannelBootstrap streamChannelBootstrap = new Http2StreamChannelBootstrap(channel);
                    IHttp2StreamChannel         streamChannel          = await streamChannelBootstrap.OpenAsync();

                    streamChannel.Pipeline.AddLast(streamFrameResponseHandler);

                    // Send request (a HTTP/2 HEADERS frame - with ':method = GET' in this case)
                    var                 path    = ExampleHelper.Configuration["path"];
                    HttpScheme          scheme  = ClientSettings.IsSsl ? HttpScheme.Https : HttpScheme.Http;
                    DefaultHttp2Headers headers = new DefaultHttp2Headers
                    {
                        Method = HttpMethod.Get.AsciiName,
                        Path   = AsciiString.Of(path),
                        Scheme = scheme.Name
                    };
                    IHttp2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers);
                    await streamChannel.WriteAndFlushAsync(headersFrame);

                    Console.WriteLine("Sent HTTP/2 GET request to " + path);

                    // Wait for the responses (or for the latch to expire), then clean up the connections
                    if (!streamFrameResponseHandler.ResponseSuccessfullyCompleted())
                    {
                        Console.WriteLine("Did not get HTTP/2 response in expected time.");
                    }

                    Console.WriteLine("Finished HTTP/2 request, will close the connection.");
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.WriteLine("按任意键退出");
                    Console.ReadKey();
                }
                finally
                {
                    // Wait until the connection is closed.
                    await channel.CloseAsync();
                }
            }
            finally
            {
                await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            }
        }