Search in sources :

Example 36 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project BRFS by zhangnianli.

the class NettyChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
    pipeline.addLast(new HttpResponseEncoder());
    // server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    contextHandlers.forEach((NettyHttpContextHandler handler) -> pipeline.addLast(handler));
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 37 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project BRFS by zhangnianli.

the class NettyChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
    pipeline.addLast(new HttpResponseEncoder());
    // server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    contextHandlers.forEach((NettyHttpContextHandler handler) -> pipeline.addLast(handler));
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 38 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project jdepth by Crab2died.

the class WebSocketServer method run.

private void run(int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().channel(NioServerSocketChannel.class).group(bossGroup, workerGroup).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("http-codec", new HttpServerCodec()).addLast("aggregator", new HttpObjectAggregator(65536)).addLast("http-chunked", new ChunkedWriteHandler()).addLast("handler", new WebSocketServerHandler());
            }
        });
        Channel channel = bootstrap.bind(port).sync().channel();
        channel.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 39 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project duangframework by tcrct.

the class HttpChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    if (bootStrap.isSslEnabled()) {
        sslContext = bootStrap.getSslContext();
    }
    // HttpServerCodec包含了默认的HttpRequestDecoder(请求消息解码器)和HttpResponseEncoder(响应解码器)
    p.addLast(new HttpServerCodec());
    // 为http响应内容添加gizp压缩器
    p.addLast(new HttpContentCompressor());
    // 目的是将多个消息转换为单一的request或者response对象
    p.addLast(new HttpObjectAggregator(1048576));
    // 目的是支持异步大文件传输
    p.addLast(new ChunkedWriteHandler());
    // 真正处理HTTP业务逻辑的地方,针对每个TCP连接创建一个新的ChannelHandler实例
    p.addLast(new HttpBaseHandler(bootStrap));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpBaseHandler(com.duangframework.server.netty.handler.HttpBaseHandler)

Example 40 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project jackrabbit-oak by apache.

the class GetBlobResponseEncoderTest method shouldEncodeTwoChunksResponse.

@Test
public void shouldEncodeTwoChunksResponse() throws Exception {
    byte[] blobData = new byte[] { 1, 2, 3, 4 };
    byte[] firstChunkData = new byte[] { 1, 2 };
    byte[] secondChunkbData = new byte[] { 3, 4 };
    String blobId = "blobId";
    EmbeddedChannel channel = new EmbeddedChannel(new ChunkedWriteHandler(), new GetBlobResponseEncoder(2));
    channel.writeOutbound(new GetBlobResponse("clientId", blobId, new ByteArrayInputStream(blobData), blobData.length));
    ByteBuf firstBuffer = (ByteBuf) channel.readOutbound();
    byte firstMask = createMask(1, 2);
    ByteBuf firstExpected = createBlobChunkBuffer(Messages.HEADER_BLOB, 4L, blobId, firstChunkData, firstMask);
    assertEquals(firstExpected, firstBuffer);
    ByteBuf secondBuffer = (ByteBuf) channel.readOutbound();
    byte secondMask = createMask(2, 2);
    ByteBuf secondExpected = createBlobChunkBuffer(Messages.HEADER_BLOB, 4L, blobId, secondChunkbData, secondMask);
    assertEquals(secondExpected, secondBuffer);
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

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