Example usage for io.netty.util ReferenceCountUtil release

List of usage examples for io.netty.util ReferenceCountUtil release

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil release.

Prototype

public static boolean release(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

From source file:ratpack.http.client.internal.IdlingConnectionHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ReferenceCountUtil.release(msg);
    ctx.close();
}

From source file:ratpack.stream.bytebuf.internal.ByteBufComposingPublisher.java

License:Apache License

@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/*from   w w w  . j  a v a2 s. c o  m*/
        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();
            }
        }
    });
}

From source file:reactor.io.net.impl.netty.http.NettyHttpWSClientHandler.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Class<?> messageClass = msg.getClass();
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ctx.channel(), (FullHttpResponse) msg);
        NettyHttpWSClientHandler.super.channelActive(ctx);
        super.channelRead(ctx, msg);
        return;/* w  ww.j a  v a2s . co  m*/
    }

    if (TextWebSocketFrame.class.isAssignableFrom(messageClass)) {
        try {
            Buffer buffer = Buffer.wrap(((TextWebSocketFrame) msg).text());
            if (channelStream.getDecoder() == null) {
                channelSubscription.onNext((IN) buffer);
            } else {
                IN d = channelStream.getDecoder().apply(buffer);
                if (d != null) {
                    channelSubscription.onNext(d);
                }
            }
        } finally {
            ReferenceCountUtil.release(msg);
        }
    } else if (CloseWebSocketFrame.class.isAssignableFrom(messageClass)) {
        ctx.close();
    } else {
        doRead(ctx, ((WebSocketFrame) msg).content());
    }
}

From source file:reactor.io.net.impl.netty.NettyChannelHandlerBridge.java

License:Apache License

@SuppressWarnings("unchecked")
protected final void doRead(ChannelHandlerContext ctx, Object msg) {
    try {//from w  w  w . j a v  a  2s  . c om
        if (null == channelSubscription || msg == Unpooled.EMPTY_BUFFER) {
            ReferenceCountUtil.release(msg);
            return;
        }

        if (channelStream.getDecoder() == Spec.NOOP_DECODER
                || !ByteBuf.class.isAssignableFrom(msg.getClass())) {
            channelSubscription.onNext((IN) msg);
            return;
        } else if (channelStream.getDecoder() == null) {
            try {
                channelSubscription.onNext((IN) new Buffer(((ByteBuf) msg).nioBuffer()));
            } finally {
                ReferenceCountUtil.release(msg);
            }
            return;
        }

        ByteBuf data = (ByteBuf) msg;
        if (remainder == null) {
            try {
                passToConnection(data);
            } finally {
                if (data.isReadable()) {
                    remainder = data;
                } else {
                    data.release();
                }
            }
            return;
        }

        if (!bufferHasSufficientCapacity(remainder, data)) {
            ByteBuf combined = createCombinedBuffer(remainder, data, ctx);
            remainder.release();
            remainder = combined;
        } else {
            remainder.writeBytes(data);
        }
        data.release();

        try {
            passToConnection(remainder);
        } finally {
            if (remainder.isReadable()) {
                remainder.discardSomeReadBytes();
            } else {
                remainder.release();
                remainder = null;
            }
        }
    } catch (Throwable t) {
        if (channelSubscription != null) {
            channelSubscription.onError(t);
        } else if (Environment.alive()) {
            Environment.get().routeError(t);
        }
    }

}

From source file:reactor.ipc.netty.channel.ChannelOperationsHandler.java

License:Open Source License

@Override
final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg == null || msg == Unpooled.EMPTY_BUFFER || msg instanceof EmptyByteBuf) {
        return;/*from  w  w  w.  ja v a  2  s  . c o  m*/
    }
    try {
        ChannelOperations<?, ?> ops = inbound();
        if (ops != null) {
            inbound().onInboundNext(ctx, msg);
        } else if (log.isDebugEnabled()) {
            log.debug("No ChannelOperation attached. Dropping: {}", msg);
        }
    } catch (Throwable err) {
        Exceptions.throwIfFatal(err);
        exceptionCaught(ctx, err);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:reactor.ipc.netty.channel.ChannelOperationsHandler.java

License:Open Source License

void discard() {
    for (;;) {/* w ww .j a v  a2 s .  c o m*/
        if (pendingWrites == null || pendingWrites.isEmpty()) {
            return;
        }

        ChannelPromise promise;
        Object v = pendingWrites.poll();

        try {
            promise = (ChannelPromise) v;
        } catch (Throwable e) {
            ctx.fireExceptionCaught(e);
            return;
        }
        v = pendingWrites.poll();
        if (log.isDebugEnabled()) {
            log.debug("Terminated ChannelOperation. Dropping: {}", v);
        }
        ReferenceCountUtil.release(v);
        promise.tryFailure(ContextHandler.ABORTED);
    }
}

From source file:reactor.ipc.netty.channel.FluxReceive.java

License:Open Source License

@Override
public void cancel() {
    if (cancelReceiver()) {
        Queue<Object> q = receiverQueue;
        if (q != null) {
            Object o;/*from  w  w w. j  a va2  s.  c o  m*/
            while ((o = q.poll()) != null) {
                ReferenceCountUtil.release(o);
            }
        }
    }
}

From source file:reactor.ipc.netty.channel.FluxReceive.java

License:Open Source License

final boolean drainReceiver() {

    final Queue<Object> q = receiverQueue;
    final Subscriber<? super Object> a = receiver;

    if (a == null) {
        if (inboundDone) {
            cancelReceiver();//from w  ww .  ja  va 2  s  .c o  m
        }
        return false;
    }

    long r = receiverDemand;
    long e = 0L;

    while (e != r) {
        if (isCancelled()) {
            return false;
        }

        boolean d = inboundDone;
        Object v = q != null ? q.poll() : null;
        boolean empty = v == null;

        if (d && empty) {
            terminateReceiver(q, a);
            return false;
        }

        if (empty) {
            break;
        }

        try {
            a.onNext(v);
        } finally {
            ReferenceCountUtil.release(v);
        }

        e++;
    }

    if (isCancelled()) {
        return false;
    }

    if (inboundDone && (q == null || q.isEmpty())) {
        terminateReceiver(q, a);
        return false;
    }

    if (r == Long.MAX_VALUE) {
        channel.config().setAutoRead(true);
        channel.read();
        return true;
    }

    if ((receiverDemand -= e) > 0L || e > 0L) {
        channel.read();
    }

    return false;
}

From source file:reactor.ipc.netty.channel.FluxReceive.java

License:Open Source License

final void onInboundNext(Object msg) {
    if (inboundDone) {
        if (log.isDebugEnabled()) {
            log.debug("Dropping frame {}", msg);
        }/*from  www  . j a v a2 s  .  c o m*/
        return;
    }
    ReferenceCountUtil.retain(msg);
    if (receiverFastpath && receiver != null) {
        try {
            receiver.onNext(msg);
        } finally {
            ReferenceCountUtil.release(msg);
        }

    } else {
        Queue<Object> q = receiverQueue;
        if (q == null) {
            q = QueueSupplier.unbounded().get();
            receiverQueue = q;
        }
        q.offer(msg);
        if (drainReceiver()) {
            receiverFastpath = true;
        }
    }
}

From source file:reactor.ipc.netty.channel.NettyOperations.java

License:Open Source License

@Override
final public void cancel() {
    cancelReceiver();/* w  w  w.  ja  v a  2 s  .c o m*/

    if (wip++ == 0) {
        Queue<Object> q = inboundQueue;
        if (q != null) {
            Object o;
            while ((o = q.poll()) != null) {
                ReferenceCountUtil.release(o);
            }
        }
    }
}