Search in sources :

Example 76 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.

the class AbstractDecoderTest method readDecompressed.

protected static ByteBuf readDecompressed(final EmbeddedChannel channel) {
    CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = channel.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    return decompressed;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 77 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.

the class ParameterizedSslHandlerTest method compositeBufSizeEstimationGuaranteesSynchronousWrite.

private static void compositeBufSizeEstimationGuaranteesSynchronousWrite(SslProvider serverProvider, SslProvider clientProvider, final boolean serverDisableWrapSize, final boolean letHandlerCreateServerEngine, final boolean letHandlerCreateClientEngine) throws CertificateException, SSLException, ExecutionException, InterruptedException {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(serverProvider).build();
    final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(clientProvider).build();
    EventLoopGroup group = new NioEventLoopGroup();
    Channel sc = null;
    Channel cc = null;
    try {
        final Promise<Void> donePromise = group.next().newPromise();
        // The goal is to provide the SSLEngine with many ByteBuf components to ensure that the overhead for wrap
        // is correctly accounted for on each component.
        final int numComponents = 150;
        // This is the TLS packet size. The goal is to divide the maximum amount of application data that can fit
        // into a single TLS packet into many components to ensure the overhead is correctly taken into account.
        final int desiredBytes = 16384;
        final int singleComponentSize = desiredBytes / numComponents;
        final int expectedBytes = numComponents * singleComponentSize;
        sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                final SslHandler handler = letHandlerCreateServerEngine ? sslServerCtx.newHandler(ch.alloc()) : new SslHandler(sslServerCtx.newEngine(ch.alloc()));
                if (serverDisableWrapSize) {
                    handler.setWrapDataSize(-1);
                }
                ch.pipeline().addLast(handler);
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    private boolean sentData;

                    private Throwable writeCause;

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                        if (evt instanceof SslHandshakeCompletionEvent) {
                            SslHandshakeCompletionEvent sslEvt = (SslHandshakeCompletionEvent) evt;
                            if (sslEvt.isSuccess()) {
                                CompositeByteBuf content = ctx.alloc().compositeDirectBuffer(numComponents);
                                for (int i = 0; i < numComponents; ++i) {
                                    ByteBuf buf = ctx.alloc().directBuffer(singleComponentSize);
                                    buf.writerIndex(buf.writerIndex() + singleComponentSize);
                                    content.addComponent(true, buf);
                                }
                                ctx.writeAndFlush(content).addListener(new ChannelFutureListener() {

                                    @Override
                                    public void operationComplete(ChannelFuture future) throws Exception {
                                        writeCause = future.cause();
                                        if (writeCause == null) {
                                            sentData = true;
                                        }
                                    }
                                });
                            } else {
                                donePromise.tryFailure(sslEvt.cause());
                            }
                        }
                        ctx.fireUserEventTriggered(evt);
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        donePromise.tryFailure(new IllegalStateException("server exception sentData: " + sentData + " writeCause: " + writeCause, cause));
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        donePromise.tryFailure(new IllegalStateException("server closed sentData: " + sentData + " writeCause: " + writeCause));
                    }
                });
            }
        }).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        cc = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                if (letHandlerCreateClientEngine) {
                    ch.pipeline().addLast(sslClientCtx.newHandler(ch.alloc()));
                } else {
                    ch.pipeline().addLast(new SslHandler(sslClientCtx.newEngine(ch.alloc())));
                }
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    private int bytesSeen;

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) {
                        if (msg instanceof ByteBuf) {
                            bytesSeen += ((ByteBuf) msg).readableBytes();
                            if (bytesSeen == expectedBytes) {
                                donePromise.trySuccess(null);
                            }
                        }
                        ReferenceCountUtil.release(msg);
                    }

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                        if (evt instanceof SslHandshakeCompletionEvent) {
                            SslHandshakeCompletionEvent sslEvt = (SslHandshakeCompletionEvent) evt;
                            if (!sslEvt.isSuccess()) {
                                donePromise.tryFailure(sslEvt.cause());
                            }
                        }
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        donePromise.tryFailure(new IllegalStateException("client exception. bytesSeen: " + bytesSeen, cause));
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        donePromise.tryFailure(new IllegalStateException("client closed. bytesSeen: " + bytesSeen));
                    }
                });
            }
        }).connect(sc.localAddress()).syncUninterruptibly().channel();
        donePromise.get();
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslServerCtx);
        ReferenceCountUtil.release(sslClientCtx);
        ssc.delete();
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SSLException(javax.net.ssl.SSLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 78 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.

the class EpollDatagramUnicastTest method testSegmentedDatagramPacket.

private void testSegmentedDatagramPacket(Bootstrap sb, Bootstrap cb, boolean composite, boolean gro) throws Throwable {
    if (!(cb.group() instanceof EpollEventLoopGroup)) {
        // Only supported for the native epoll transport.
        return;
    }
    if (gro && !(sb.group() instanceof EpollEventLoopGroup)) {
        // Only supported for the native epoll transport.
        return;
    }
    assumeTrue(EpollDatagramChannel.isSegmentedDatagramPacketSupported());
    Channel sc = null;
    Channel cc = null;
    try {
        cb.handler(new SimpleChannelInboundHandler<Object>() {

            @Override
            public void channelRead0(ChannelHandlerContext ctx, Object msgs) {
            // Nothing will be sent.
            }
        });
        cc = cb.bind(newSocketAddress()).sync().channel();
        final int numBuffers = 16;
        final int segmentSize = 512;
        int bufferCapacity = numBuffers * segmentSize;
        final CountDownLatch latch = new CountDownLatch(numBuffers);
        AtomicReference<Throwable> errorRef = new AtomicReference<Throwable>();
        if (gro) {
            // Enable GRO and also ensure we can read everything with one read as otherwise
            // we will drop things on the floor.
            sb.option(EpollChannelOption.UDP_GRO, true);
            sb.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(bufferCapacity));
        }
        sc = sb.handler(new SimpleChannelInboundHandler<DatagramPacket>() {

            @Override
            public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) {
                if (packet.content().readableBytes() == segmentSize) {
                    latch.countDown();
                }
            }
        }).bind(newSocketAddress()).sync().channel();
        if (sc instanceof EpollDatagramChannel) {
            assertEquals(gro, sc.config().getOption(EpollChannelOption.UDP_GRO));
        }
        InetSocketAddress addr = sendToAddress((InetSocketAddress) sc.localAddress());
        final ByteBuf buffer;
        if (composite) {
            CompositeByteBuf compositeBuffer = Unpooled.compositeBuffer();
            for (int i = 0; i < numBuffers; i++) {
                compositeBuffer.addComponent(true, Unpooled.directBuffer(segmentSize).writeZero(segmentSize));
            }
            buffer = compositeBuffer;
        } else {
            buffer = Unpooled.directBuffer(bufferCapacity).writeZero(bufferCapacity);
        }
        cc.writeAndFlush(new io.netty.channel.unix.SegmentedDatagramPacket(buffer, segmentSize, addr)).sync();
        if (!latch.await(10, TimeUnit.SECONDS)) {
            Throwable error = errorRef.get();
            if (error != null) {
                throw error;
            }
            fail();
        }
    } finally {
        if (cc != null) {
            cc.close().sync();
        }
        if (sc != null) {
            sc.close().sync();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) DatagramPacket(io.netty.channel.socket.DatagramPacket) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator)

Example 79 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.

the class CoalescingBufferQueue method compose.

@Override
protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
    if (cumulation instanceof CompositeByteBuf) {
        CompositeByteBuf composite = (CompositeByteBuf) cumulation;
        composite.addComponent(true, next);
        return composite;
    }
    return composeIntoComposite(alloc, cumulation, next);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 80 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.

the class AbstractCoalescingBufferQueue method composeIntoComposite.

/**
 * Compose {@code cumulation} and {@code next} into a new {@link CompositeByteBuf}.
 */
protected final ByteBuf composeIntoComposite(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
    // Create a composite buffer to accumulate this pair and potentially all the buffers
    // in the queue. Using +2 as we have already dequeued current and next.
    CompositeByteBuf composite = alloc.compositeBuffer(size() + 2);
    try {
        composite.addComponent(true, cumulation);
        composite.addComponent(true, next);
    } catch (Throwable cause) {
        composite.release();
        safeRelease(next);
        throwException(cause);
    }
    return composite;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)86 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)8 ByteBuffer (java.nio.ByteBuffer)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Channel (io.netty.channel.Channel)5 ChannelFutureListener (io.netty.channel.ChannelFutureListener)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 Test (org.junit.jupiter.api.Test)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 CodecException (io.netty.handler.codec.CodecException)3 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)3 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3