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.lanternpowered.server.network.pipeline.MessageCodecHandler.java

License:MIT License

@Override
protected void encode(ChannelHandlerContext ctx, Message message, List<Object> output) throws Exception {
    final Protocol protocol = this.codecContext.getSession().getProtocol();
    final MessageRegistration<Message> registration = (MessageRegistration<Message>) protocol.outbound()
            .findByMessageType(message.getClass()).orElse(null);

    if (registration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered!");
    }/* w w  w. j a  v  a2s.  c o m*/
    CodecRegistration codecRegistration = registration.getCodecRegistration().orElse(null);
    if (codecRegistration == null) {
        throw new EncoderException(
                "Message type (" + message.getClass().getName() + ") is not registered to allow encoding!");
    }

    /*
    if (message instanceof MessagePlayOutWorldTime ||
        message instanceof MessageInOutKeepAlive) {
    } else {
    System.out.println(message.getClass().getName());
    }
    */

    final ByteBuf opcode = ctx.alloc().buffer();

    // Write the opcode of the message
    writeVarInt(opcode, codecRegistration.getOpcode());

    final Codec codec = codecRegistration.getCodec();
    final ByteBuffer content;
    try {
        content = codec.encode(this.codecContext, message);
    } finally {
        ReferenceCountUtil.release(message);
    }

    // Add the buffer to the output
    output.add(Unpooled.wrappedBuffer(opcode, ((LanternByteBuffer) content).getDelegate()));
}

From source file:org.mobicents.protocols.api.PayloadData.java

License:Open Source License

/**
 * @return the data/*from  www .j a  v a 2  s.  com*/
 */
public byte[] getData() {
    byte[] array = new byte[byteBuf.readableBytes()];
    byteBuf.getBytes(0, array);
    ReferenceCountUtil.release(byteBuf);
    return array;
}

From source file:org.mobicents.protocols.ss7.m3ua.impl.AspFactoryImpl.java

License:Open Source License

private void processPayload(IpChannelType ipChannelType, ByteBuf byteBuf) {
    M3UAMessage m3UAMessage;//from   w ww .  j  ava 2  s  . co  m
    if (ipChannelType == IpChannelType.SCTP) {
        try {
            // TODO where is streamNumber stored?
            m3UAMessage = this.messageFactory.createMessage(byteBuf);
            if (this.isHeartBeatEnabled()) {
                this.heartBeatTimer.reset();
            }
            this.read(m3UAMessage);
        } finally {
            ReferenceCountUtil.release(byteBuf);
        }
    } else {
        if (tcpIncBuffer == null) {
            tcpIncBuffer = byteBuf.alloc().compositeBuffer();
        }
        tcpIncBuffer.addComponent(byteBuf);
        tcpIncBuffer.writerIndex(tcpIncBuffer.capacity());

        while (true) {
            m3UAMessage = this.messageFactory.createMessage(tcpIncBuffer);
            if (m3UAMessage == null)
                break;

            if (this.isHeartBeatEnabled()) {
                this.heartBeatTimer.reset();
            }
            this.read(m3UAMessage);
        }
        tcpIncBuffer.discardReadBytes();
    }
}

From source file:org.nmrfx.server.ServerHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf in = (ByteBuf) msg;/*from   w  w w  .j  ava 2 s  .  c  om*/
    try {
        while (in.isReadable()) { // (1)
            char ch = (char) in.readByte();
            System.out.print(ch);
            System.out.flush();
            if (ch == '\n') {
                consumer.accept(sBuilder.toString());
                sBuilder.setLength(0);
            } else {
                sBuilder.append(ch);
            }
        }
    } finally {
        ReferenceCountUtil.release(msg); // (2)
    }
}

From source file:org.onesec.raven.rtp.RtpInboundHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!buffers.push((ByteBuf) msg))
        ReferenceCountUtil.release(msg);
}

From source file:org.onosproject.lisp.ctl.impl.LispChannelHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    try {/*w  w  w  .j a  v  a  2s.  com*/

        // process map-request message that is encapsulated in ECM
        if (msg instanceof LispEncapsulatedControl) {
            LispMessage innerMsg = extractLispMessage((LispEncapsulatedControl) msg);
            if (innerMsg instanceof LispMapRequest) {
                LispMapResolver mapResolver = LispMapResolver.getInstance();
                List<LispMessage> lispMessages = mapResolver.processMapRequest((LispEncapsulatedControl) msg);

                if (lispMessages != null) {
                    lispMessages.forEach(ctx::writeAndFlush);
                }
            }
        }

        // process map-register message
        if (msg instanceof LispMapRegister) {

            LispMapRegister register = (LispMapRegister) msg;
            IpAddress xtrAddress = valueOf(register.getSender().getAddress());
            router = routerFactory.getRouterInstance(xtrAddress);
            router.setChannel(ctx.channel());
            router.connectRouter();
            router.handleMessage(register);

            LispMapServer mapServer = LispMapServer.getInstance();
            LispMapNotify mapNotify = mapServer.processMapRegister(register);

            if (mapNotify != null) {
                ctx.writeAndFlush(mapNotify);
            }
        }

        // process info-request message
        if (msg instanceof LispInfoRequest) {
            LispMapServer mapServer = LispMapServer.getInstance();
            LispInfoReply infoReply = mapServer.processInfoRequest((LispInfoRequest) msg);

            if (infoReply != null) {
                ctx.writeAndFlush(infoReply);
            }
        }
    } finally {
        // try to remove the received message form the buffer
        ReferenceCountUtil.release(msg);
    }
}

From source file:org.onosproject.lisp.ctl.LispChannelHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    // first we need to check whether this is an ECM
    if (msg instanceof LispEncapsulatedControl) {
        msg = extractMapRequest((LispEncapsulatedControl) msg);
    }/*from w w w.  j ava  2 s  . c  o m*/

    if (msg instanceof LispMapRegister) {
        LispMapServer mapServer = new LispMapServer();
        LispMapNotify mapNotify = mapServer.processMapRegister((LispMapRegister) msg);

        // try to remove the received map-register message from buffer
        ReferenceCountUtil.release(msg);

        ctx.writeAndFlush(mapNotify);
    }

    if (msg instanceof LispMapRequest) {
        LispMapResolver mapResolver = new LispMapResolver();
        LispMapReply mapReply = (LispMapReply) mapResolver.processMapRequest((LispMapRequest) msg);

        // TODO: serialize mapReply message and write to channel
    }
}

From source file:org.onosproject.ofagent.impl.OFChannelHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {/*from w w  w .  j a v a 2s . c  o m*/
        OFMessage ofMsg = (OFMessage) msg;
        // TODO process OF message

    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:org.onosproject.openflow.controller.impl.OFChannelHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    boolean release = true;
    try {/*  w w w.  java  2  s.co m*/
        if (msg instanceof OFMessage) {
            // channelRead0 inlined
            state.processOFMessage(this, (OFMessage) msg);
        } else {
            release = false;
            ctx.fireChannelRead(msg);
        }
    } finally {
        if (release) {
            ReferenceCountUtil.release(msg);
        }
    }
}

From source file:org.restcomm.media.network.deprecated.netty.handler.DiscardServerHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {/*from   w w w .  ja  v  a2 s .  c  om*/
        // Do something
    } finally {
        // Discard the received data silently.
        ReferenceCountUtil.release(msg);
    }
}