Search in sources :

Example 46 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project vert.x by eclipse.

the class VertxHttp2ConnectionHandler method _writeData.

private void _writeData(Http2Stream stream, ByteBuf chunk, boolean end) {
    encoder().writeData(ctx, stream.id(), chunk, 0, end, ctx.newPromise());
    Http2RemoteFlowController controller = encoder().flowController();
    if (!controller.isWritable(stream) || end) {
        try {
            encoder().flowController().writePendingBytes();
        } catch (Http2Exception e) {
            onError(ctx, e);
        }
    }
    ctx.channel().flush();
}
Also used : Http2Exception(io.netty.handler.codec.http2.Http2Exception) Http2RemoteFlowController(io.netty.handler.codec.http2.Http2RemoteFlowController)

Example 47 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project grpc-java by grpc.

the class NettyClientHandler method goingAway.

/**
   * Handler for a GOAWAY being either sent or received. Fails any streams created after the
   * last known stream.
   */
private void goingAway(Status status) {
    lifecycleManager.notifyShutdown(status);
    final Status goAwayStatus = lifecycleManager.getShutdownStatus();
    final int lastKnownStream = connection().local().lastStreamKnownByPeer();
    try {
        connection().forEachActiveStream(new Http2StreamVisitor() {

            @Override
            public boolean visit(Http2Stream stream) throws Http2Exception {
                if (stream.id() > lastKnownStream) {
                    NettyClientStream.TransportState clientStream = clientStream(stream);
                    if (clientStream != null) {
                        clientStream.transportReportStatus(goAwayStatus, false, new Metadata());
                    }
                    stream.close();
                }
                return true;
            }
        });
    } catch (Http2Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Status(io.grpc.Status) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Http2StreamVisitor(io.netty.handler.codec.http2.Http2StreamVisitor) Metadata(io.grpc.Metadata) Http2Stream(io.netty.handler.codec.http2.Http2Stream)

Example 48 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project netty by netty.

the class Http2FrameCodecTest method streamErrorShouldFireUserEvent.

@Test
public void streamErrorShouldFireUserEvent() throws Exception {
    frameListener.onHeadersRead(http2HandlerCtx, 3, request, 31, false);
    Http2Stream stream = framingCodec.connectionHandler().connection().stream(3);
    assertNotNull(stream);
    Http2StreamActiveEvent activeEvent = inboundHandler.readInboundMessageOrUserEvent();
    assertNotNull(activeEvent);
    assertEquals(stream.id(), activeEvent.streamId());
    StreamException streamEx = new StreamException(3, Http2Error.INTERNAL_ERROR, "foo");
    framingCodec.connectionHandler().onError(http2HandlerCtx, streamEx);
    Http2HeadersFrame headersFrame = inboundHandler.readInboundMessageOrUserEvent();
    assertNotNull(headersFrame);
    try {
        inboundHandler.checkException();
        fail("stream exception expected");
    } catch (StreamException e) {
        assertEquals(streamEx, e);
    }
    Http2StreamClosedEvent closedEvent = inboundHandler.readInboundMessageOrUserEvent();
    assertNotNull(closedEvent);
    assertEquals(stream.id(), closedEvent.streamId());
    assertNull(inboundHandler.readInboundMessageOrUserEvent());
}
Also used : StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) Test(org.junit.Test)

Example 49 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project netty by netty.

the class DataCompressionHttp2Test method bootstrapEnv.

private void bootstrapEnv(int serverOutSize) throws Exception {
    final CountDownLatch prefaceWrittenLatch = new CountDownLatch(1);
    serverOut = new ByteArrayOutputStream(serverOutSize);
    serverLatch = new CountDownLatch(1);
    sb = new ServerBootstrap();
    cb = new Bootstrap();
    // Streams are created before the normal flow for this test, so these connection must be initialized up front.
    serverConnection = new DefaultHttp2Connection(true);
    clientConnection = new DefaultHttp2Connection(false);
    serverConnection.addListener(new Http2ConnectionAdapter() {

        @Override
        public void onStreamClosed(Http2Stream stream) {
            serverLatch.countDown();
        }
    });
    doAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock in) throws Throwable {
            ByteBuf buf = (ByteBuf) in.getArguments()[2];
            int padding = (Integer) in.getArguments()[3];
            int processedBytes = buf.readableBytes() + padding;
            buf.readBytes(serverOut, buf.readableBytes());
            if (in.getArgument(4)) {
                serverConnection.stream((Integer) in.getArgument(1)).close();
            }
            return processedBytes;
        }
    }).when(serverListener).onDataRead(any(ChannelHandlerContext.class), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean());
    final CountDownLatch serverChannelLatch = new CountDownLatch(1);
    sb.group(new NioEventLoopGroup(), new NioEventLoopGroup());
    sb.channel(NioServerSocketChannel.class);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            serverConnectedChannel = ch;
            ChannelPipeline p = ch.pipeline();
            Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
            serverConnection.remote().flowController(new DefaultHttp2RemoteFlowController(serverConnection));
            serverConnection.local().flowController(new DefaultHttp2LocalFlowController(serverConnection).frameWriter(frameWriter));
            Http2ConnectionEncoder encoder = new CompressorHttp2ConnectionEncoder(new DefaultHttp2ConnectionEncoder(serverConnection, frameWriter));
            Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(serverConnection, encoder, new DefaultHttp2FrameReader());
            Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().frameListener(new DelegatingDecompressorFrameListener(serverConnection, serverListener)).codec(decoder, encoder).build();
            p.addLast(connectionHandler);
            serverChannelLatch.countDown();
        }
    });
    cb.group(new NioEventLoopGroup());
    cb.channel(NioSocketChannel.class);
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
            clientConnection.remote().flowController(new DefaultHttp2RemoteFlowController(clientConnection));
            clientConnection.local().flowController(new DefaultHttp2LocalFlowController(clientConnection).frameWriter(frameWriter));
            clientEncoder = new CompressorHttp2ConnectionEncoder(new DefaultHttp2ConnectionEncoder(clientConnection, frameWriter));
            Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(clientConnection, clientEncoder, new DefaultHttp2FrameReader());
            clientHandler = new Http2ConnectionHandlerBuilder().frameListener(new DelegatingDecompressorFrameListener(clientConnection, clientListener)).gracefulShutdownTimeoutMillis(0).codec(decoder, clientEncoder).build();
            p.addLast(clientHandler);
            p.addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt == Http2ConnectionPrefaceAndSettingsFrameWrittenEvent.INSTANCE) {
                        prefaceWrittenLatch.countDown();
                        ctx.pipeline().remove(this);
                    }
                }
            });
        }
    });
    serverChannel = sb.bind(new InetSocketAddress(0)).sync().channel();
    int port = ((InetSocketAddress) serverChannel.localAddress()).getPort();
    ChannelFuture ccf = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port));
    assertTrue(ccf.awaitUninterruptibly().isSuccess());
    clientChannel = ccf.channel();
    assertTrue(prefaceWrittenLatch.await(5, SECONDS));
    assertTrue(serverChannelLatch.await(5, SECONDS));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Http2TestUtil.runInChannel(io.netty.handler.codec.http2.Http2TestUtil.runInChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 50 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project netty by netty.

the class AbstractWeightedFairQueueByteDistributorDependencyTest method writeAnswer.

Answer<Void> writeAnswer(final boolean closeIfNoFrame) {
    return new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock in) throws Throwable {
            Http2Stream stream = in.getArgument(0);
            int numBytes = in.getArgument(1);
            TestStreamByteDistributorStreamState state = stateMap.get(stream.id());
            state.pendingBytes -= numBytes;
            state.hasFrame = state.pendingBytes > 0;
            state.isWriteAllowed = state.hasFrame;
            if (closeIfNoFrame && !state.hasFrame) {
                stream.close();
            }
            distributor.updateStreamableBytes(state);
            return null;
        }
    };
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestStreamByteDistributorStreamState(io.netty.handler.codec.http2.Http2TestUtil.TestStreamByteDistributorStreamState)

Aggregations

Http2Stream (io.netty.handler.codec.http2.Http2Stream)44 Http2Exception (io.netty.handler.codec.http2.Http2Exception)15 ByteBuf (io.netty.buffer.ByteBuf)12 Test (org.junit.Test)12 Test (org.junit.jupiter.api.Test)9 Http2Connection (io.netty.handler.codec.http2.Http2Connection)8 ChannelFuture (io.netty.channel.ChannelFuture)7 Http2StreamVisitor (io.netty.handler.codec.http2.Http2StreamVisitor)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 Metadata (io.grpc.Metadata)6 Http2LocalFlowController (io.netty.handler.codec.http2.Http2LocalFlowController)6 Status (io.grpc.Status)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)5 Channel (io.netty.channel.Channel)4 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)4 Http2Headers (io.netty.handler.codec.http2.Http2Headers)4 AsyncResult (io.vertx.core.AsyncResult)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Bootstrap (io.netty.bootstrap.Bootstrap)3