Example usage for io.netty.util ReferenceCountUtil retain

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

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public static <T> T retain(T msg) 

Source Link

Document

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

Usage

From source file:io.liveoak.stomp.common.StompMessageDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, StompFrame msg, List<Object> out) throws Exception {
    if (msg instanceof StompContentFrame) {
        if (msg.command() == Stomp.Command.MESSAGE || msg.command() == Stomp.Command.SEND
                || msg.command() == Stomp.Command.ERROR) {
            StompMessage stompMessage = new DefaultStompMessage(msg.headers(),
                    ((StompContentFrame) msg).content().retain(), msg.command() == Stomp.Command.ERROR);
            out.add(stompMessage);/*from  www.j a  v  a 2  s .com*/
        } else {
            ReferenceCountUtil.retain(msg);
            out.add(msg);
        }
    } else {
        ReferenceCountUtil.retain(msg);
        out.add(msg);
    }

}

From source file:io.liveoak.stomp.server.protocol.ReceiptHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, StompFrame msg) throws Exception {
    String receiptId = msg.headers().get(Headers.RECEIPT);
    if (receiptId != null) {
        ctx.writeAndFlush(StompFrame.newReceiptFrame(receiptId));
    }//from w ww.  j a  va2 s. c o  m

    // retain and keep it moving upstream
    ReferenceCountUtil.retain(msg);
    ctx.fireChannelRead(msg);
}

From source file:io.liveoak.stomp.server.protocol.SubscribeHandler.java

License:Open Source License

@Override
public void handleControlFrame(ChannelHandlerContext ctx, StompControlFrame msg) throws StompServerException {
    String subscriptionId = msg.headers().get(Headers.ID);
    String destination = msg.headers().get(Headers.DESTINATION);
    StompConnection stompConnection = ctx.channel().attr(ConnectHandler.CONNECTION).get();
    this.serverContext.handleSubscribe(stompConnection, destination, subscriptionId, msg.headers());

    // retain and send upstream for RECEIPT
    ReferenceCountUtil.retain(msg);
    ctx.fireChannelRead(msg);/*  w  w w  .  ja  va  2  s .co m*/
}

From source file:io.liveoak.stomp.server.protocol.UnsubscribeHandler.java

License:Open Source License

@Override
public void handleControlFrame(ChannelHandlerContext ctx, StompControlFrame msg) throws StompServerException {
    String subscriptionId = msg.headers().get(Headers.ID);
    StompConnection stompConnection = ctx.channel().attr(ConnectHandler.CONNECTION).get();
    this.serverContext.handleUnsubscribe(stompConnection, subscriptionId);

    // retain and send upstream for RECEIPT
    ReferenceCountUtil.retain(msg);
    ctx.fireChannelRead(msg);/*from   ww  w.  j  a  v  a 2 s  .  c  om*/
}

From source file:io.netty.example.http2.helloworld.frame.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//*from  w  ww  .j  a  v  a2s .  co m*/
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:io.netty.example.http2.helloworld.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///from   w  w  w . jav a 2s  . c  o m
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec,
            upgradeCodecFactory);
    final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(
            sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build());

    p.addLast(cleartextHttp2ServerUpgradeHandler);
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:io.nodyn.netty.DataEventHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    emit("data", ReferenceCountUtil.retain(msg));
    super.channelRead(ctx, msg);
}

From source file:io.nodyn.netty.pipe.ipc.IPCDataEventHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, final Object msg) throws Exception {
    this.process.getEventLoop().submitUserTask(new Runnable() {
        @Override/* w  ww .jav  a  2  s.c  o m*/
        public void run() {
            emit("dataWithHandle", ReferenceCountUtil.retain(msg));
        }
    }, "ipc-data-with-handle");
    super.channelRead(ctx, msg);
}

From source file:me.netty.http.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///from www .j a va2s.  co m
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new Http1Handler());
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:openbns.commons.net.codec.sts.HttpObjectAggregator.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, StsObject msg, List<Object> out) throws Exception {
    FullStsMessage currentMessage = this.currentMessage;

    if (msg instanceof StsMessage) {
        tooLongFrameFound = false;//from   ww  w.  ja v  a 2 s  . co m
        assert currentMessage == null;

        StsMessage m = (StsMessage) msg;

        if (!m.getDecoderResult().isSuccess()) {
            this.currentMessage = null;
            out.add(ReferenceCountUtil.retain(m));
            return;
        }
        if (msg instanceof StsRequest) {
            StsRequest header = (StsRequest) msg;
            this.currentMessage = currentMessage = new DefaultFullStsRequest(header.getProtocolVersion(),
                    header.getMethod(), header.getUri(),
                    Unpooled.compositeBuffer(maxCumulationBufferComponents));
        } else if (msg instanceof StsResponse) {
            StsResponse header = (StsResponse) msg;
            this.currentMessage = currentMessage = new DefaultFullStsResponse(header.getStatus(),
                    Unpooled.compositeBuffer(maxCumulationBufferComponents));
        } else {
            throw new Error();
        }

        currentMessage.headers().set(m.headers());
    } else if (msg instanceof StsContent) {
        if (tooLongFrameFound) {
            if (msg instanceof LastStsContent) {
                this.currentMessage = null;
            }
            // already detect the too long frame so just discard the content
            return;
        }
        assert currentMessage != null;

        // Merge the received chunk into the content of the current message.
        StsContent chunk = (StsContent) msg;
        CompositeByteBuf content = (CompositeByteBuf) currentMessage.content();

        if (content.readableBytes() > maxContentLength - chunk.content().readableBytes()) {
            tooLongFrameFound = true;

            // release current message to prevent leaks
            currentMessage.release();
            this.currentMessage = null;

            throw new TooLongFrameException("HTTP content length exceeded " + maxContentLength + " bytes.");
        }

        // Append the content of the chunk
        if (chunk.content().isReadable()) {
            chunk.retain();
            content.addComponent(chunk.content());
            content.writerIndex(content.writerIndex() + chunk.content().readableBytes());
        }

        final boolean last;
        if (!chunk.getDecoderResult().isSuccess()) {
            currentMessage.setDecoderResult(DecoderResult.failure(chunk.getDecoderResult().cause()));
            last = true;
        } else {
            last = chunk instanceof LastStsContent;
        }

        if (last) {
            this.currentMessage = null;

            // Merge trailing headers into the message.
            if (chunk instanceof LastStsContent) {
                LastStsContent trailer = (LastStsContent) chunk;
                currentMessage.headers().add(trailer.trailingHeaders());
            }

            // Set the 'Content-Length' header.
            currentMessage.headers().set(StsHeaders.Names.CONTENT_LENGTH,
                    String.valueOf(content.readableBytes()));

            // All done
            out.add(currentMessage);
        }
    } else {
        throw new Error();
    }
}