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:org.jboss.aerogear.simplepush.server.netty.SimplePushSockJSServiceTest.java

License:Apache License

private Object readOutboundDiscardEmpty(final EmbeddedChannel ch) {
    final Object obj = ch.readOutbound();
    if (obj instanceof ByteBuf) {
        final ByteBuf buf = (ByteBuf) obj;
        if (buf.capacity() == 0) {
            ReferenceCountUtil.release(buf);
            return ch.readOutbound();
        }/*from   w  w  w  .j  a  v  a2 s . c  om*/
    }
    return obj;
}

From source file:org.jdiameter.client.impl.transport.tcp.netty.DiameterMessageHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    logger.debug("Received message TCP Transport from [{}]", ctx.channel().remoteAddress());
    IMessage m = (IMessage) msg;// w  ww. j  a v a 2  s.  c o m
    try {
        logger.debug("Passing message on to parent");
        parentConnection.onMessageReceived(m);
        logger.debug("Finished passing message on to parent");
    } catch (AvpDataException e) {
        logger.debug("Garbage was received. Discarding.");
        parentConnection.onAvpDataException(e);
    } finally {
        ReferenceCountUtil.release(m);
    }
}

From source file:org.jdiameter.client.impl.transport.tls.netty.DiameterMessageHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof IMessage) {
        IMessage m = (IMessage) msg;//from  ww  w  .  j  av  a2 s. c om
        logger.debug("Received message {} TLS Transport {}", m.getCommandCode(),
                this.parentConnection.getKey());
        try {
            logger.debug("Passing message on to parent {}", this.parentConnection.getKey());
            parentConnection.onMessageReceived(m);
            logger.debug("Finished passing message on to parent {}", this.parentConnection.getKey());
        } catch (AvpDataException e) {
            logger.debug("Garbage was received. Discarding. {}", this.parentConnection.getKey());
            parentConnection.onAvpDataException(e);
        } finally {
            if (autoRelease) {
                ReferenceCountUtil.release(msg);
            } else {
                ctx.fireChannelRead(m);
            }
        }
    }
}

From source file:org.jdiameter.client.impl.transport.tls.netty.StartTlsInitiator.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from  w  ww.  j a va 2  s. com*/
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof IMessage) {
        IMessage m = (IMessage) msg;
        logger.debug("StartTlsInitiator");
        if (m.getCommandCode() == IMessage.CAPABILITIES_EXCHANGE_ANSWER
                && this.tlsTransportClient.getTlsHandshakingState() == TlsHandshakingState.INIT) {
            AvpSet set = m.getAvps();
            Avp inbandAvp = set.getAvp(Avp.INBAND_SECURITY_ID);
            if (inbandAvp != null && inbandAvp.getUnsigned32() == 1) {
                this.tlsTransportClient.setTlsHandshakingState(TlsHandshakingState.SHAKING);

                final ChannelPipeline pipeline = ctx.pipeline();
                pipeline.remove("decoder");
                pipeline.remove("msgHandler");
                pipeline.remove(this);
                pipeline.remove("encoder");
                pipeline.remove("inbandWriter");

                pipeline.addLast("startTlsClientHandler", new StartTlsClientHandler(this.tlsTransportClient));

                logger.debug("Sending StartTlsRequest");
                ctx.writeAndFlush(Unpooled.wrappedBuffer("StartTlsRequest".getBytes()))
                        .addListener(new GenericFutureListener() {

                            @Override
                            public void operationComplete(Future f) throws Exception {
                                if (!f.isSuccess()) {
                                    logger.error(f.cause().getMessage(), f.cause());
                                }
                            }
                        });

            }
        }
    }

    ReferenceCountUtil.release(msg);
}

From source file:org.jdiameter.client.impl.transport.tls.netty.StartTlsServerHandler.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//from w  w  w  .  jav  a  2 s.  c o  m
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    logger.debug("StartTlsServerHandler");
    ByteBuf buf = (ByteBuf) msg;
    byte[] bytes = new byte[buf.readableBytes()];
    buf.getBytes(buf.readerIndex(), bytes);

    if ("StartTlsRequest".equals(new String(bytes))) {
        logger.debug("Received StartTlsRequest");
        SslContext sslContext = SslContextFactory.getSslContextForServer(this.tlsTransportClient.getConfig());
        SSLEngine sslEngine = sslContext.newEngine(ctx.alloc());
        sslEngine.setUseClientMode(false);
        SslHandler sslHandler = new SslHandler(sslEngine, false);

        final ChannelPipeline pipeline = ctx.pipeline();

        pipeline.remove("decoder");
        pipeline.remove("msgHandler");
        pipeline.remove("encoder");
        pipeline.remove("inbandWriter");
        pipeline.remove(this);

        pipeline.addLast("sslHandler", sslHandler);

        sslHandler.handshakeFuture().addListener(new GenericFutureListener() {

            @Override
            public void operationComplete(Future future) throws Exception {
                if (future.isSuccess()) {
                    logger.debug("StartTls server handshake succesfull");

                    tlsTransportClient.setTlsHandshakingState(TlsHandshakingState.SHAKEN);

                    logger.debug("restoring all handlers");

                    pipeline.addLast("decoder",
                            new DiameterMessageDecoder(
                                    StartTlsServerHandler.this.tlsTransportClient.getParent(),
                                    StartTlsServerHandler.this.tlsTransportClient.getParser()));
                    pipeline.addLast("msgHandler", new DiameterMessageHandler(
                            StartTlsServerHandler.this.tlsTransportClient.getParent(), true));

                    pipeline.addLast("encoder", new DiameterMessageEncoder(
                            StartTlsServerHandler.this.tlsTransportClient.getParser()));
                    pipeline.addLast("inbandWriter", new InbandSecurityHandler());

                }
            }
        });

        ReferenceCountUtil.release(msg);
        logger.debug("Sending StartTlsResponse");
        ctx.writeAndFlush(Unpooled.wrappedBuffer("StartTlsResponse".getBytes()))
                .addListener(new GenericFutureListener() {

                    @Override
                    public void operationComplete(Future f) throws Exception {
                        if (!f.isSuccess()) {
                            logger.error(f.cause().getMessage(), f.cause());
                        }

                    }
                });
    } else {
        ctx.fireChannelRead(msg);
    }

}

From source file:org.jupiter.transport.netty.handler.acceptor.AcceptorHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();//from   www. j a va 2 s.  c  om

    if (msg instanceof JRequestBytes) {
        JChannel jChannel = NettyChannel.attachChannel(ch);
        try {
            processor.handleRequest(jChannel, (JRequestBytes) msg);
        } catch (Throwable t) {
            processor.handleException(jChannel, (JRequestBytes) msg, Status.SERVER_ERROR, t);
        }
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("Unexpected message type received: {}, channel: {}.", msg.getClass(), ch);
        }

        ReferenceCountUtil.release(msg);
    }
}

From source file:org.jupiter.transport.netty.handler.connector.ConnectorHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();/* ww w .j  ava  2s  .co m*/

    if (msg instanceof JResponseBytes) {
        try {
            processor.handleResponse(NettyChannel.attachChannel(ch), (JResponseBytes) msg);
        } catch (Throwable t) {
            logger.error("An exception was caught: {}, on {} #channelRead().", stackTrace(t), ch);
        }
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("Unexpected message type received: {}, channel: {}.", msg.getClass(), ch);
        }

        ReferenceCountUtil.release(msg);
    }
}

From source file:org.kobeyoung81.socksproxy.RelayHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (relayChannel.isActive()) {
        //ByteBuf buffer = (ByteBuf)msg;
        //System.out.println("Content:" +" <<< " + buffer.toString(Charset.defaultCharset()));
        //System.out.println("Local: " + relayChannel.localAddress() + "; Remote: " + relayChannel.remoteAddress());
        relayChannel.writeAndFlush(msg);
    } else {/*from  w w w  .  j a  v a2 s.  c o  m*/
        ReferenceCountUtil.release(msg);
    }
}

From source file:org.kordamp.javatrove.chat04.client.impl.ClientChatHandlerImpl.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {// w w w.  ja v  a  2 s.c  om
        LOG.info("Received " + msg);
        commandDispatcher.dispatch((Command) msg);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:org.kordamp.javatrove.chat04.server.impl.ServerChatHandlerImpl.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {//from   w ww.  j ava 2  s  .  c  om
        LOG.info("Publishing " + msg);
        channels.forEach(channel -> channel.writeAndFlush(msg));
    } finally {
        ReferenceCountUtil.release(msg);
    }
}