Search in sources :

Example 36 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project reactor-netty by reactor.

the class NettyOutboundTest method sendFileWithForceChunkedFileUsesStrategyChunks.

@Test
public void sendFileWithForceChunkedFileUsesStrategyChunks() throws URISyntaxException, IOException {
    List<Class<?>> messageWritten = new ArrayList<>(2);
    EmbeddedChannel channel = new EmbeddedChannel(// transform the ByteBuf chunks into Strings:
    new MessageToMessageEncoder<ByteBuf>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
            out.add(msg.toString(CharsetUtil.UTF_8));
        }
    }, // transform the ChunkedFile into ByteBuf chunks:
    new ChunkedWriteHandler(), // helps to ensure a ChunkedFile was written outs
    new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
            messageWritten.add(msg.getClass());
            out.add(msg);
        }
    });
    NettyContext mockContext = () -> channel;
    NettyOutbound outbound = new NettyOutbound() {

        @Override
        public NettyContext context() {
            return mockContext;
        }

        @Override
        public FileChunkedStrategy getFileChunkedStrategy() {
            return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
        }
    };
    Path path = Paths.get(getClass().getResource("/largeFile.txt").toURI());
    channel.writeOneOutbound(1);
    outbound.sendFileChunked(path, 0, Files.size(path)).then().block();
    assertThat(channel.inboundMessages()).isEmpty();
    assertThat(messageWritten).containsExactly(Integer.class, ChunkedNioFile.class);
    assertThat(channel.outboundMessages()).hasSize(3).element(1).asString().startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE").endsWith("1024 mark here ->");
    assertThat(channel.outboundMessages()).last().asString().startsWith("<- 1024 mark here").endsWith("End of File");
    assertThat(channel.finishAndReleaseAll()).isTrue();
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) Test(org.junit.Test)

Example 37 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project reactor-netty by reactor.

the class NettyOutboundTest method sendFileWithTlsUsesChunkedFile.

@Test
public void sendFileWithTlsUsesChunkedFile() throws URISyntaxException, SSLException, CertificateException {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    final SslHandler sslHandler = sslCtx.newHandler(ByteBufAllocator.DEFAULT);
    List<Class<?>> messageWritten = new ArrayList<>(2);
    List<Object> clearMessages = new ArrayList<>(2);
    EmbeddedChannel channel = new EmbeddedChannel(// bytes are encrypted
    sslHandler, // capture the chunks unencrypted, transform as Strings:
    new MessageToMessageEncoder<ByteBuf>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
            clearMessages.add(msg.toString(CharsetUtil.UTF_8));
            // the encoder will release the buffer, make sure it is retained for SslHandler
            out.add(msg.retain());
        }
    }, // transform the ChunkedFile into ByteBuf chunks:
    new ChunkedWriteHandler(), // helps to ensure a ChunkedFile was written outs
    new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
            messageWritten.add(msg.getClass());
            // passing the ChunkedFile through this method releases it, which is undesired
            ReferenceCountUtil.retain(msg);
            out.add(msg);
        }
    });
    NettyContext mockContext = () -> channel;
    NettyOutbound outbound = new NettyOutbound() {

        @Override
        public NettyContext context() {
            return mockContext;
        }

        @Override
        public FileChunkedStrategy getFileChunkedStrategy() {
            return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
        }
    };
    channel.writeOneOutbound(1);
    try {
        outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then().block(// TODO investigate why this hangs
        Duration.ofSeconds(1));
    } catch (IllegalStateException e) {
        if (!"Timeout on blocking read for 1000 MILLISECONDS".equals(e.getMessage()))
            throw e;
        System.err.println(e);
    }
    assertThat(messageWritten).containsExactly(Integer.class, ChunkedNioFile.class);
    assertThat(clearMessages).hasSize(2).element(0).asString().startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE").endsWith("1024 mark here ->");
    assertThat(clearMessages).element(1).asString().startsWith("<- 1024 mark here").endsWith("End of File");
    assertThat(channel.finishAndReleaseAll()).isTrue();
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) SslHandler(io.netty.handler.ssl.SslHandler) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 38 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project benchmark by seelunzi.

the class ChildChannelHandler method initChannel.

@Override
protected void initChannel(SocketChannel e) throws Exception {
    // 设置30秒没有读到数据,则触发一个READER_IDLE事件。
    // pipeline.addLast(new IdleStateHandler(30, 0, 0));
    // HttpServerCodec:将请求和应答消息解码为HTTP消息
    e.pipeline().addLast("http-codec", new HttpServerCodec());
    // HttpObjectAggregator:将HTTP消息的多个部分合成一条完整的HTTP消息
    e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
    // ChunkedWriteHandler:向客户端发送HTML5文件
    e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
    // 在管道中添加我们自己的接收数据实现方法
    e.pipeline().addLast("handler", new MyWebSocketServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec)

Example 39 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project benchmark by seelunzi.

the class WebsocketChatServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // 2
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(64 * 1024));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new HttpRequestHandler("/ws"));
    pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
    pipeline.addLast(new TextWebSocketFrameHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 40 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project async-http-client by AsyncHttpClient.

the class ChannelManager method configureBootstraps.

public void configureBootstraps(NettyRequestSender requestSender) {
    final AsyncHttpClientHandler httpHandler = new HttpHandler(config, this, requestSender);
    wsHandler = new WebSocketHandler(config, this, requestSender);
    final LoggingHandler loggingHandler = new LoggingHandler(LogLevel.TRACE);
    httpBootstrap.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            ChannelPipeline pipeline = ch.pipeline().addLast(HTTP_CLIENT_CODEC, newHttpClientCodec()).addLast(INFLATER_HANDLER, newHttpContentDecompressor()).addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler()).addLast(AHC_HTTP_HANDLER, httpHandler);
            if (LOGGER.isTraceEnabled()) {
                pipeline.addFirst(LOGGING_HANDLER, loggingHandler);
            }
            if (config.getHttpAdditionalChannelInitializer() != null)
                config.getHttpAdditionalChannelInitializer().accept(ch);
        }
    });
    wsBootstrap.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            ChannelPipeline pipeline = ch.pipeline().addLast(HTTP_CLIENT_CODEC, newHttpClientCodec()).addLast(AHC_WS_HANDLER, wsHandler);
            if (config.isEnableWebSocketCompression()) {
                pipeline.addBefore(AHC_WS_HANDLER, WS_COMPRESSOR_HANDLER, WebSocketClientCompressionHandler.INSTANCE);
            }
            if (LOGGER.isDebugEnabled()) {
                pipeline.addFirst(LOGGING_HANDLER, loggingHandler);
            }
            if (config.getWsAdditionalChannelInitializer() != null)
                config.getWsAdditionalChannelInitializer().accept(ch);
        }
    });
}
Also used : HttpHandler(org.asynchttpclient.netty.handler.HttpHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) WebSocketHandler(org.asynchttpclient.netty.handler.WebSocketHandler) AsyncHttpClientHandler(org.asynchttpclient.netty.handler.AsyncHttpClientHandler)

Aggregations

ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)46 ChannelPipeline (io.netty.channel.ChannelPipeline)24 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)16 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)11 SslHandler (io.netty.handler.ssl.SslHandler)10 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)9 ByteBuf (io.netty.buffer.ByteBuf)8 SocketChannel (io.netty.channel.socket.SocketChannel)8 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)8 LoggingHandler (io.netty.handler.logging.LoggingHandler)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)5 IOException (java.io.IOException)5 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)4 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)4