Search in sources :

Example 41 with CompositeByteBuf

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

the class ByteBufComposingPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super CompositeByteBuf> subscriber) {
    subscriber.onSubscribe(new ManagedSubscription<CompositeByteBuf>(subscriber, ByteBuf::release) {

        private Subscription subscription;

        private CompositeByteBuf composite;

        private volatile State state;

        @Override
        protected void onRequest(long n) {
            if (subscription == null) {
                upstream.subscribe(new Subscriber<ByteBuf>() {

                    @Override
                    public void onSubscribe(Subscription s) {
                        subscription = s;
                        state = State.Fetching;
                        s.request(1);
                    }

                    @Override
                    public void onNext(ByteBuf t) {
                        if (state == State.Closed) {
                            t.release();
                            return;
                        }
                        if (composite == null) {
                            composite = alloc.compositeBuffer(maxNum);
                        }
                        composite.addComponent(true, t);
                        if (composite.numComponents() == maxNum || composite.readableBytes() >= watermark) {
                            state = State.Writing;
                            emitNext(composite);
                            composite = null;
                            maybeFetch();
                        } else {
                            subscription.request(1);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        state = State.Closed;
                        ReferenceCountUtil.release(composite);
                        emitError(t);
                    }

                    @Override
                    public void onComplete() {
                        state = State.Closed;
                        if (composite != null) {
                            emitNext(composite);
                        }
                        emitComplete();
                    }
                });
            } else {
                maybeFetch();
            }
        }

        private void maybeFetch() {
            if (getDemand() > 0 && state != State.Fetching) {
                state = State.Fetching;
                subscription.request(1);
            }
        }

        @Override
        protected void onCancel() {
            state = State.Closed;
            ReferenceCountUtil.release(composite);
            if (subscription != null) {
                subscription.cancel();
            }
        }
    });
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 42 with CompositeByteBuf

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

the class ServerSentEventDecoder method str.

private String str(List<ByteBuf> bufs) {
    if (bufs.isEmpty()) {
        return null;
    } else {
        String str;
        if (bufs.size() == 1) {
            str = bufs.get(0).toString(StandardCharsets.UTF_8);
        } else {
            CompositeByteBuf composite = allocator.compositeBuffer(bufs.size() * 2 - 1);
            Iterator<ByteBuf> iterator = bufs.iterator();
            composite.addComponent(true, iterator.next());
            while (iterator.hasNext()) {
                composite.addComponent(true, NEWLINE_BYTEBUF.retainedDuplicate());
                composite.addComponent(true, iterator.next());
            }
            str = composite.toString(StandardCharsets.UTF_8);
        }
        bufs.forEach(ByteBuf::release);
        bufs.clear();
        return str;
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 43 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project mongo-java-driver by mongodb.

the class NettyStream method readAsync.

@Override
public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
    scheduleReadTimeout();
    ByteBuf buffer = null;
    Throwable exceptionResult = null;
    synchronized (this) {
        exceptionResult = pendingException;
        if (exceptionResult == null) {
            if (!hasBytesAvailable(numBytes)) {
                pendingReader = new PendingReader(numBytes, handler);
            } else {
                CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
                int bytesNeeded = numBytes;
                for (Iterator<io.netty.buffer.ByteBuf> iter = pendingInboundBuffers.iterator(); iter.hasNext(); ) {
                    io.netty.buffer.ByteBuf next = iter.next();
                    int bytesNeededFromCurrentBuffer = Math.min(next.readableBytes(), bytesNeeded);
                    if (bytesNeededFromCurrentBuffer == next.readableBytes()) {
                        composite.addComponent(next);
                        iter.remove();
                    } else {
                        next.retain();
                        composite.addComponent(next.readSlice(bytesNeededFromCurrentBuffer));
                    }
                    composite.writerIndex(composite.writerIndex() + bytesNeededFromCurrentBuffer);
                    bytesNeeded -= bytesNeededFromCurrentBuffer;
                    if (bytesNeeded == 0) {
                        break;
                    }
                }
                buffer = new NettyByteBuf(composite).flip();
            }
        }
    }
    if (exceptionResult != null) {
        disableReadTimeout();
        handler.failed(exceptionResult);
    }
    if (buffer != null) {
        disableReadTimeout();
        handler.completed(buffer);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(org.bson.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 44 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project spring-framework by spring-projects.

the class NettyDataBuffer method write.

/**
	 * Writes one or more Netty {@link ByteBuf}s to this buffer, starting at the current
	 * writing position.
	 * @param byteBufs the buffers to write into this buffer
	 * @return this buffer
	 */
public NettyDataBuffer write(ByteBuf... byteBufs) {
    Assert.notNull(byteBufs, "'byteBufs' must not be null");
    CompositeByteBuf composite = new CompositeByteBuf(this.byteBuf.alloc(), this.byteBuf.isDirect(), byteBufs.length + 1);
    composite.addComponent(this.byteBuf);
    composite.addComponents(byteBufs);
    int writerIndex = this.byteBuf.readableBytes() + Arrays.stream(byteBufs).mapToInt(ByteBuf::readableBytes).sum();
    composite.writerIndex(writerIndex);
    this.byteBuf = composite;
    return this;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 45 with CompositeByteBuf

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

the class SslHandlerTest method compositeBufSizeEstimationGuaranteesSynchronousWrite.

private void compositeBufSizeEstimationGuaranteesSynchronousWrite(SslProvider serverProvider, SslProvider clientProvider) 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();
        final int expectedBytes = 469 + 1024 + 1024;
        sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                        if (evt instanceof SslHandshakeCompletionEvent) {
                            SslHandshakeCompletionEvent sslEvt = (SslHandshakeCompletionEvent) evt;
                            if (sslEvt.isSuccess()) {
                                final ByteBuf input = ctx.alloc().buffer();
                                input.writeBytes(new byte[expectedBytes]);
                                CompositeByteBuf content = ctx.alloc().compositeBuffer();
                                content.addComponent(true, input.readRetainedSlice(469));
                                content.addComponent(true, input.readRetainedSlice(1024));
                                content.addComponent(true, input.readRetainedSlice(1024));
                                ctx.writeAndFlush(content).addListener(new ChannelFutureListener() {

                                    @Override
                                    public void operationComplete(ChannelFuture future) {
                                        input.release();
                                    }
                                });
                            } else {
                                donePromise.tryFailure(sslEvt.cause());
                            }
                        }
                        ctx.fireUserEventTriggered(evt);
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        donePromise.tryFailure(cause);
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        donePromise.tryFailure(new IllegalStateException("server closed"));
                    }
                });
            }
        }).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 {
                ch.pipeline().addLast(sslClientCtx.newHandler(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 exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        donePromise.tryFailure(cause);
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        donePromise.tryFailure(new IllegalStateException("client closed"));
                    }
                });
            }
        }).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) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) CodecException(io.netty.handler.codec.CodecException) SSLProtocolException(javax.net.ssl.SSLProtocolException) DecoderException(io.netty.handler.codec.DecoderException) SSLException(javax.net.ssl.SSLException) ClosedChannelException(java.nio.channels.ClosedChannelException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

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