Search in sources :

Example 41 with ChunkedWriteHandler

use of 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 42 with ChunkedWriteHandler

use of 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 43 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project vert.x by eclipse.

the class NetServerImpl method initChannel.

protected void initChannel(ChannelPipeline pipeline, boolean ssl) {
    if (options.getLogActivity()) {
        pipeline.addLast("logging", new LoggingHandler(options.getActivityLogDataFormat()));
    }
    if (ssl) {
        // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used.
        // For large file / sendfile support
        pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    }
    int idleTimeout = options.getIdleTimeout();
    int readIdleTimeout = options.getReadIdleTimeout();
    int writeIdleTimeout = options.getWriteIdleTimeout();
    if (idleTimeout > 0 || readIdleTimeout > 0 || writeIdleTimeout > 0) {
        pipeline.addLast("idle", new IdleStateHandler(readIdleTimeout, writeIdleTimeout, idleTimeout, options.getIdleTimeoutUnit()));
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler)

Example 44 with ChunkedWriteHandler

use of 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 45 with ChunkedWriteHandler

use of 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)

Aggregations

ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)57 ChannelPipeline (io.netty.channel.ChannelPipeline)28 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)18 SslHandler (io.netty.handler.ssl.SslHandler)13 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)12 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)12 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)11 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)11 ByteBuf (io.netty.buffer.ByteBuf)10 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)10 LoggingHandler (io.netty.handler.logging.LoggingHandler)10 SocketChannel (io.netty.channel.socket.SocketChannel)8 ChannelFuture (io.netty.channel.ChannelFuture)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 Test (org.junit.Test)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)5 SslContext (io.netty.handler.ssl.SslContext)5 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)4