Search in sources :

Example 66 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project netty by netty.

the class DeflateDecoder method decompressContent.

private ByteBuf decompressContent(ChannelHandlerContext ctx, WebSocketFrame msg) {
    if (decoder == null) {
        if (!(msg instanceof TextWebSocketFrame) && !(msg instanceof BinaryWebSocketFrame)) {
            throw new CodecException("unexpected initial frame type: " + msg.getClass().getName());
        }
        decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    }
    boolean readable = msg.content().isReadable();
    boolean emptyDeflateBlock = EMPTY_DEFLATE_BLOCK.equals(msg.content());
    decoder.writeInbound(msg.content().retain());
    if (appendFrameTail(msg)) {
        decoder.writeInbound(FRAME_TAIL.duplicate());
    }
    CompositeByteBuf compositeDecompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partUncompressedContent = decoder.readInbound();
        if (partUncompressedContent == null) {
            break;
        }
        if (!partUncompressedContent.isReadable()) {
            partUncompressedContent.release();
            continue;
        }
        compositeDecompressedContent.addComponent(true, partUncompressedContent);
    }
    // See https://github.com/netty/netty/issues/4348
    if (!emptyDeflateBlock && readable && compositeDecompressedContent.numComponents() <= 0) {
        // May contain left-over data that doesn't affect decompression
        if (!(msg instanceof ContinuationWebSocketFrame)) {
            compositeDecompressedContent.release();
            throw new CodecException("cannot read uncompressed buffer");
        }
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    return compositeDecompressedContent;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CodecException(io.netty.handler.codec.CodecException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 67 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project netty by netty.

the class SslHandler method wrap.

private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException {
    ByteBuf newDirectIn = null;
    try {
        int readerIndex = in.readerIndex();
        int readableBytes = in.readableBytes();
        // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of
        // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is called.
        final ByteBuffer[] in0;
        if (in.isDirect() || !engineType.wantsDirectBuffer) {
            // which is better then walking the composed ByteBuf in most cases.
            if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) {
                in0 = singleBuffer;
                // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object allocation
                // to a minimum.
                in0[0] = in.internalNioBuffer(readerIndex, readableBytes);
            } else {
                in0 = in.nioBuffers();
            }
        } else {
            // We could even go further here and check if its a CompositeByteBuf and if so try to decompose it and
            // only replace the ByteBuffer that are not direct. At the moment we just will replace the whole
            // CompositeByteBuf to keep the complexity to a minimum
            newDirectIn = alloc.directBuffer(readableBytes);
            newDirectIn.writeBytes(in, readerIndex, readableBytes);
            in0 = singleBuffer;
            in0[0] = newDirectIn.internalNioBuffer(newDirectIn.readerIndex(), readableBytes);
        }
        for (; ; ) {
            ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes());
            SSLEngineResult result = engine.wrap(in0, out0);
            in.skipBytes(result.bytesConsumed());
            out.writerIndex(out.writerIndex() + result.bytesProduced());
            if (result.getStatus() == Status.BUFFER_OVERFLOW) {
                out.ensureWritable(engine.getSession().getPacketBufferSize());
            } else {
                return result;
            }
        }
    } finally {
        // Null out to allow GC of ByteBuffer
        singleBuffer[0] = null;
        if (newDirectIn != null) {
            newDirectIn.release();
        }
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) SSLEngineResult(javax.net.ssl.SSLEngineResult) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 68 with CompositeByteBuf

use of 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 69 with CompositeByteBuf

use of 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 70 with CompositeByteBuf

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

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)85 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 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 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ExecutionException (java.util.concurrent.ExecutionException)3 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2