Search in sources :

Example 6 with DefaultHttp2FrameReader

use of io.netty.handler.codec.http2.DefaultHttp2FrameReader in project rest.li by linkedin.

the class Http2StreamCodecBuilder method build.

@Override
public Http2StreamCodec build() {
    ObjectUtil.checkNotNull(_connection, "connection");
    ObjectUtil.checkPositive(_maxHeaderSize, "maxHeaderSize");
    Http2HeadersDecoder headerDecoder = new DefaultHttp2HeadersDecoder(_maxHeaderSize, Http2CodecUtil.DEFAULT_HEADER_TABLE_SIZE, isValidateHeaders(), INITIAL_HUFFMAN_DECODE_CAPACITY);
    Http2FrameReader reader = new DefaultHttp2FrameReader(headerDecoder);
    Http2FrameWriter writer = new DefaultHttp2FrameWriter(headerSensitivityDetector());
    if (frameLogger() != null) {
        reader = new Http2InboundFrameLogger(reader, frameLogger());
        writer = new Http2OutboundFrameLogger(writer, frameLogger());
    }
    Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(_connection, writer);
    boolean encoderEnforceMaxConcurrentStreams = encoderEnforceMaxConcurrentStreams();
    if (encoderEnforceMaxConcurrentStreams) {
        if (_connection.isServer()) {
            encoder.close();
            reader.close();
            throw new IllegalArgumentException("encoderEnforceMaxConcurrentStreams: " + encoderEnforceMaxConcurrentStreams + " not supported for server");
        }
        encoder = new StreamBufferingEncoder(encoder);
    }
    Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(_connection, encoder, reader);
    super.codec(decoder, encoder);
    return super.build();
}
Also used : Http2InboundFrameLogger(io.netty.handler.codec.http2.Http2InboundFrameLogger) Http2FrameReader(io.netty.handler.codec.http2.Http2FrameReader) DefaultHttp2FrameReader(io.netty.handler.codec.http2.DefaultHttp2FrameReader) StreamBufferingEncoder(io.netty.handler.codec.http2.StreamBufferingEncoder) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) Http2OutboundFrameLogger(io.netty.handler.codec.http2.Http2OutboundFrameLogger) Http2HeadersDecoder(io.netty.handler.codec.http2.Http2HeadersDecoder) DefaultHttp2HeadersDecoder(io.netty.handler.codec.http2.DefaultHttp2HeadersDecoder) DefaultHttp2FrameWriter(io.netty.handler.codec.http2.DefaultHttp2FrameWriter) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) Http2FrameWriter(io.netty.handler.codec.http2.Http2FrameWriter) DefaultHttp2FrameWriter(io.netty.handler.codec.http2.DefaultHttp2FrameWriter) DefaultHttp2HeadersDecoder(io.netty.handler.codec.http2.DefaultHttp2HeadersDecoder) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) DefaultHttp2FrameReader(io.netty.handler.codec.http2.DefaultHttp2FrameReader)

Example 7 with DefaultHttp2FrameReader

use of io.netty.handler.codec.http2.DefaultHttp2FrameReader 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() {

                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt instanceof Http2ConnectionPrefaceWrittenEvent) {
                        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)

Aggregations

DefaultHttp2FrameReader (io.netty.handler.codec.http2.DefaultHttp2FrameReader)6 DefaultHttp2FrameWriter (io.netty.handler.codec.http2.DefaultHttp2FrameWriter)6 DefaultHttp2Connection (io.netty.handler.codec.http2.DefaultHttp2Connection)3 DefaultHttp2ConnectionDecoder (io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder)3 DefaultHttp2ConnectionEncoder (io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder)3 Http2Connection (io.netty.handler.codec.http2.Http2Connection)3 Http2ConnectionDecoder (io.netty.handler.codec.http2.Http2ConnectionDecoder)3 Http2ConnectionEncoder (io.netty.handler.codec.http2.Http2ConnectionEncoder)3 Http2FrameReader (io.netty.handler.codec.http2.Http2FrameReader)3 Http2FrameWriter (io.netty.handler.codec.http2.Http2FrameWriter)3 Http2HeadersDecoder (io.netty.handler.codec.http2.Http2HeadersDecoder)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 Channel (io.netty.channel.Channel)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 Http2ConnectionHandler (io.netty.handler.codec.http2.Http2ConnectionHandler)2 Http2ConnectionHandlerBuilder (io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder)2