Search in sources :

Example 46 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project motan by weibocom.

the class Netty4HttpServer method open.

@Override
public boolean open() {
    if (isAvailable()) {
        return true;
    }
    if (channel != null) {
        channel.close();
    }
    if (bossGroup == null) {
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
    }
    boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
    // TODO max connection protect
    int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue());
    int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), 500);
    int minWorkerThread = 0, maxWorkerThread = 0;
    if (shareChannel) {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
    } else {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
    }
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    final NettyHttpRequestHandler handler = new NettyHttpRequestHandler(this, messageHandler, new ThreadPoolExecutor(minWorkerThread, maxWorkerThread, 15, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(workerQueueSize)));
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
            ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(maxContentLength));
            ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
            ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
            ch.pipeline().addLast("serverHandler", handler);
        }
    }).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, false);
    ChannelFuture f;
    try {
        f = b.bind(url.getPort()).sync();
        channel = f.channel();
    } catch (InterruptedException e) {
        LoggerUtil.error("init http server fail.", e);
        return false;
    }
    state = ChannelState.ALIVE;
    StatsUtil.registryStatisticCallback(this);
    LoggerUtil.info("Netty4HttpServer ServerChannel finish Open: url=" + url);
    return true;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) TransportException(com.weibo.api.motan.transport.TransportException) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 47 with ChunkedWriteHandler

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

the class SocketSslEchoTest method testSslEcho.

public void testSslEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final ExecutorService delegatedTaskExecutor = Executors.newCachedThreadPool();
    reset();
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) {
            serverChannel = sch;
            if (serverUsesDelegatedTaskExecutor) {
                SSLEngine sse = serverCtx.newEngine(sch.alloc());
                serverSslHandler = new SslHandler(sse, delegatedTaskExecutor);
            } else {
                serverSslHandler = serverCtx.newHandler(sch.alloc());
            }
            serverSslHandler.setHandshakeTimeoutMillis(0);
            sch.pipeline().addLast("ssl", serverSslHandler);
            if (useChunkedWriteHandler) {
                sch.pipeline().addLast(new ChunkedWriteHandler());
            }
            sch.pipeline().addLast("serverHandler", serverHandler);
        }
    });
    final CountDownLatch clientHandshakeEventLatch = new CountDownLatch(1);
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) {
            clientChannel = sch;
            if (clientUsesDelegatedTaskExecutor) {
                SSLEngine cse = clientCtx.newEngine(sch.alloc());
                clientSslHandler = new SslHandler(cse, delegatedTaskExecutor);
            } else {
                clientSslHandler = clientCtx.newHandler(sch.alloc());
            }
            clientSslHandler.setHandshakeTimeoutMillis(0);
            sch.pipeline().addLast("ssl", clientSslHandler);
            if (useChunkedWriteHandler) {
                sch.pipeline().addLast(new ChunkedWriteHandler());
            }
            sch.pipeline().addLast("clientHandler", clientHandler);
            sch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                    if (evt instanceof SslHandshakeCompletionEvent) {
                        clientHandshakeEventLatch.countDown();
                    }
                    ctx.fireUserEventTriggered(evt);
                }
            });
        }
    });
    final Channel sc = sb.bind().sync().channel();
    cb.connect(sc.localAddress()).sync();
    final Future<Channel> clientHandshakeFuture = clientSslHandler.handshakeFuture();
    // Wait for the handshake to complete before we flush anything. SslHandler should flush non-application data.
    clientHandshakeFuture.sync();
    clientHandshakeEventLatch.await();
    clientChannel.writeAndFlush(Unpooled.wrappedBuffer(data, 0, FIRST_MESSAGE_SIZE));
    clientSendCounter.set(FIRST_MESSAGE_SIZE);
    boolean needsRenegotiation = renegotiation.type == RenegotiationType.CLIENT_INITIATED;
    Future<Channel> renegoFuture = null;
    while (clientSendCounter.get() < data.length) {
        int clientSendCounterVal = clientSendCounter.get();
        int length = Math.min(random.nextInt(1024 * 64), data.length - clientSendCounterVal);
        ByteBuf buf = Unpooled.wrappedBuffer(data, clientSendCounterVal, length);
        if (useCompositeByteBuf) {
            buf = Unpooled.compositeBuffer().addComponent(true, buf);
        }
        ChannelFuture future = clientChannel.writeAndFlush(buf);
        clientSendCounter.set(clientSendCounterVal += length);
        future.sync();
        if (needsRenegotiation && clientSendCounterVal >= data.length / 2) {
            needsRenegotiation = false;
            clientSslHandler.engine().setEnabledCipherSuites(new String[] { renegotiation.cipherSuite });
            renegoFuture = clientSslHandler.renegotiate();
            logStats("CLIENT RENEGOTIATES");
            assertThat(renegoFuture, is(not(sameInstance(clientHandshakeFuture))));
        }
    }
    // Ensure all data has been exchanged.
    while (clientRecvCounter.get() < data.length) {
        if (serverException.get() != null) {
            break;
        }
        if (serverException.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    while (serverRecvCounter.get() < data.length) {
        if (serverException.get() != null) {
            break;
        }
        if (clientException.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    // Wait until renegotiation is done.
    if (renegoFuture != null) {
        renegoFuture.sync();
    }
    if (serverHandler.renegoFuture != null) {
        serverHandler.renegoFuture.sync();
    }
    serverChannel.close().awaitUninterruptibly();
    clientChannel.close().awaitUninterruptibly();
    sc.close().awaitUninterruptibly();
    delegatedTaskExecutor.shutdown();
    if (serverException.get() != null && !(serverException.get() instanceof IOException)) {
        throw serverException.get();
    }
    if (clientException.get() != null && !(clientException.get() instanceof IOException)) {
        throw clientException.get();
    }
    if (serverException.get() != null) {
        throw serverException.get();
    }
    if (clientException.get() != null) {
        throw clientException.get();
    }
    // When renegotiation is done, at least the initiating side should be notified.
    try {
        switch(renegotiation.type) {
            case SERVER_INITIATED:
                assertThat(serverSslHandler.engine().getSession().getCipherSuite(), is(renegotiation.cipherSuite));
                assertThat(serverNegoCounter.get(), is(2));
                assertThat(clientNegoCounter.get(), anyOf(is(1), is(2)));
                break;
            case CLIENT_INITIATED:
                assertThat(serverNegoCounter.get(), anyOf(is(1), is(2)));
                assertThat(clientSslHandler.engine().getSession().getCipherSuite(), is(renegotiation.cipherSuite));
                assertThat(clientNegoCounter.get(), is(2));
                break;
            case NONE:
                assertThat(serverNegoCounter.get(), is(1));
                assertThat(clientNegoCounter.get(), is(1));
        }
    } finally {
        logStats("STATS");
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) SSLEngine(javax.net.ssl.SSLEngine) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) SslHandler(io.netty.handler.ssl.SslHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) ExecutorService(java.util.concurrent.ExecutorService) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 48 with ChunkedWriteHandler

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

the class HttpChunkedInputTest method check.

private static void check(ChunkedInput<?>... inputs) {
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    for (ChunkedInput<?> input : inputs) {
        ch.writeOutbound(input);
    }
    assertTrue(ch.finish());
    int i = 0;
    int read = 0;
    HttpContent lastHttpContent = null;
    for (; ; ) {
        HttpContent httpContent = ch.readOutbound();
        if (httpContent == null) {
            break;
        }
        if (lastHttpContent != null) {
            assertTrue(lastHttpContent instanceof DefaultHttpContent, "Chunk must be DefaultHttpContent");
        }
        ByteBuf buffer = httpContent.content();
        while (buffer.isReadable()) {
            assertEquals(BYTES[i++], buffer.readByte());
            read++;
            if (i == BYTES.length) {
                i = 0;
            }
        }
        buffer.release();
        // Save last chunk
        lastHttpContent = httpContent;
    }
    assertEquals(BYTES.length * inputs.length, read);
    assertSame(LastHttpContent.EMPTY_LAST_CONTENT, lastHttpContent, "Last chunk must be LastHttpContent.EMPTY_LAST_CONTENT");
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 49 with ChunkedWriteHandler

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

the class Http2DataChunkedInputTest method check.

private static void check(ChunkedInput<?>... inputs) {
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    for (ChunkedInput<?> input : inputs) {
        ch.writeOutbound(input);
    }
    assertTrue(ch.finish());
    int i = 0;
    int read = 0;
    Http2DataFrame http2DataFrame = null;
    for (; ; ) {
        Http2DataFrame dataFrame = ch.readOutbound();
        if (dataFrame == null) {
            break;
        }
        ByteBuf buffer = dataFrame.content();
        while (buffer.isReadable()) {
            assertEquals(BYTES[i++], buffer.readByte());
            read++;
            if (i == BYTES.length) {
                i = 0;
            }
        }
        buffer.release();
        // Save last chunk
        http2DataFrame = dataFrame;
    }
    assertEquals(BYTES.length * inputs.length, read);
    assertNotNull(http2DataFrame);
    assertTrue(http2DataFrame.isEndStream(), "Last chunk must be Http2DataFrame#isEndStream() set to true");
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 50 with ChunkedWriteHandler

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

the class HttpUploadClientInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast("codec", new HttpClientCodec());
    // Remove the following line if you don't want automatic content decompression.
    pipeline.addLast("inflater", new HttpContentDecompressor());
    // to be used since huge file transfer
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    pipeline.addLast("handler", new HttpUploadClientHandler());
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Aggregations

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