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.restcomm.media.network.netty.handler.NetworkFilter.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    boolean secure = guard.isSecure(ctx.channel(), msg.sender());
    if (secure) {
        // Allow the packet to pass to next handler in pipeline for processing
        ctx.fireChannelRead(msg);/* w w  w.j  a v  a2  s  . c om*/
    } else {
        ReferenceCountUtil.release(msg);
    }
}

From source file:org.restcomm.media.rtp.netty.RtpDemultiplexer.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    // Differentiate between RTP, STUN and DTLS packets in the pipeline
    // https://tools.ietf.org/html/rfc5764#section-5.1.2
    final int offset = msg.arrayOffset();
    final byte b0 = msg.getByte(offset);
    final int b0Int = b0 & 0xff;

    if (b0Int < 2) {
        handleStunPacket(ctx, msg);//from w  ww .j  a v  a  2  s.c o  m
    } else if (b0Int > 19 && b0Int < 64) {
        handleDtlsPacket(ctx, msg);
    } else if (b0Int > 127 && b0Int < 192) {
        handleRtpPacket(ctx, msg);
    } else {
        // Unsupported packet type. Drop it.
        ReferenceCountUtil.release(msg);
        if (log.isDebugEnabled()) {
            log.debug("Channel " + ctx.channel().localAddress() + " dropped unsupported packet type " + b0Int);
        }
    }
}

From source file:org.restcomm.media.rtp.netty.RtpDemultiplexer.java

License:Open Source License

private void handleStunPacket(ChannelHandlerContext ctx, ByteBuf buffer) {
    // Retrieve data from network
    final byte[] data = buffer.array();
    final int length = buffer.readableBytes();
    final int offset = buffer.arrayOffset();

    // Wrap data into an STUN packet
    try {/*from  www  . j  av a  2  s  . com*/
        StunMessage stunPacket = StunMessage.decode(data, (char) offset, (char) length);
        ctx.fireChannelRead(stunPacket);
    } catch (StunException e) {
        // Drop packet as we could not decode it
        ReferenceCountUtil.release(buffer);
        log.warn("Channel " + ctx.channel().localAddress() + "could not decode incoming STUN packet", e);
    }
}

From source file:org.restcomm.media.rtp.netty.RtpPacketFilter.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, RtpPacket msg) throws Exception {
    // Drop message is packet does not contain any payload
    int payloadLength = msg.getPayloadLength();
    if (payloadLength <= 0) {
        ReferenceCountUtil.release(msg);
        return;/*from  w  w  w . j ava2  s. com*/
    }

    // Drop message if RTP version is unsupported
    int version = msg.getVersion();
    if (RtpPacket.VERSION != version) {
        ReferenceCountUtil.release(msg);
        return;
    }

}

From source file:org.traccar.ExtendedObjectDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NetworkMessage networkMessage = (NetworkMessage) msg;
    Object originalMessage = networkMessage.getMessage();
    try {//ww  w  .j  a  va2s  . c om
        Object decodedMessage = decode(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage);
        onMessageEvent(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage, decodedMessage);
        if (decodedMessage == null) {
            decodedMessage = handleEmptyMessage(ctx.channel(), networkMessage.getRemoteAddress(),
                    originalMessage);
        }
        if (decodedMessage != null) {
            if (decodedMessage instanceof Collection) {
                for (Object o : (Collection) decodedMessage) {
                    saveOriginal(o, originalMessage);
                    ctx.fireChannelRead(o);
                }
            } else {
                saveOriginal(decodedMessage, originalMessage);
                ctx.fireChannelRead(decodedMessage);
            }
        }
    } finally {
        ReferenceCountUtil.release(originalMessage);
    }
}

From source file:org.unidal.cat.message.storage.internals.DefaultByteBufCache.java

License:Open Source License

public void put(ByteBuffer buf) {
    byte[] array = buf.array();

    for (int i = 0; i < array.length; i++) {
        array[i] = 0;//from w w  w. j av a 2  s. c o m
    }

    buf.clear();

    boolean result = m_bufs.offer(buf);

    if (!result) {
        try {
            ReferenceCountUtil.release(buf);
        } catch (Exception e) {
            Cat.logError(e);
        }

        if (m_count.incrementAndGet() % 100 == 0) {
            m_logger.info("error when put back buf");
        }
    }
}

From source file:org.vertx.java.core.http.impl.ClientConnection.java

License:Open Source License

NetSocket createNetSocket() {
    // connection was upgraded to raw TCP socket
    upgradedConnection = true;/*w w  w  .  j a  va 2s. c  o m*/
    DefaultNetSocket socket = new DefaultNetSocket(vertx, channel, context, client.tcpHelper, true);
    Map<Channel, DefaultNetSocket> connectionMap = new HashMap<Channel, DefaultNetSocket>(1);
    connectionMap.put(channel, socket);

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler inflater = pipeline.get(HttpContentDecompressor.class);
    if (inflater != null) {
        pipeline.remove(inflater);
    }
    pipeline.remove("codec");
    pipeline.replace("handler", "handler", new VertxNetHandler(client.vertx, connectionMap) {
        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            client.connectionMap.remove(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            client.connectionMap.remove(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    });
    return socket;
}

From source file:org.vertx.java.core.http.impl.ServerConnection.java

License:Open Source License

NetSocket createNetSocket() {
    DefaultNetSocket socket = new DefaultNetSocket(vertx, channel, context, server.tcpHelper, false);
    Map<Channel, DefaultNetSocket> connectionMap = new HashMap<Channel, DefaultNetSocket>(1);
    connectionMap.put(channel, socket);/*from  w w  w  .  ja  v  a2  s  .com*/

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
        pipeline.remove(compressor);
    }

    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
        pipeline.remove("chunkedWriter");
    }

    channel.pipeline().replace("handler", "handler", new VertxNetHandler(server.vertx, connectionMap) {
        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            server.connectionMap.remove(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            server.connectionMap.remove(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    });

    // check if the encoder can be removed yet or not.
    if (lastWriteFuture == null) {
        channel.pipeline().remove("httpEncoder");
    } else {
        lastWriteFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                channel.pipeline().remove("httpEncoder");
            }
        });
    }
    return socket;
}

From source file:org.wso2.carbon.transport.http.netty.sender.TargetHandler.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w  w  w.jav a2s  . co m*/
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (targetChannel.isRequestWritten()) {
        if (msg instanceof HttpResponse) {
            targetRespMsg = setUpCarbonMessage(ctx, msg);
            // TODO: Revisit all of these after the refactor
            if (handlerExecutor != null) {
                handlerExecutor.executeAtTargetResponseReceiving(targetRespMsg);
            }
            if (this.httpResponseFuture != null) {
                try {
                    httpResponseFuture.notifyHttpListener(targetRespMsg);
                } catch (Exception e) {
                    LOG.error("Error while notifying response to listener ", e);
                }
            } else {
                LOG.error("Cannot correlate callback with request callback is null");
            }
        } else {
            if (targetRespMsg != null) {
                HttpContent httpContent = (HttpContent) msg;
                targetRespMsg.addHttpContent(httpContent);
                if (Util.isLastHttpContent(httpContent)) {
                    if (handlerExecutor != null) {
                        handlerExecutor.executeAtTargetResponseSending(targetRespMsg);
                    }
                    targetChannel.getChannel().pipeline().remove(Constants.IDLE_STATE_HANDLER);
                    connectionManager.returnChannel(targetChannel);
                }
            }
        }
    } else {
        if (msg instanceof HttpResponse) {
            LOG.warn("Received a response for an obsolete request");
        }
        ReferenceCountUtil.release(msg);
    }
}

From source file:qunar.tc.qmq.store.IndexLog.java

License:Apache License

private AppendMessageResult doAppendData(final LogSegment segment, final ByteBuffer data) {
    int currentPos = segment.getWrotePosition();
    final int freeSize = segment.getFileSize() - currentPos;
    if (data.remaining() <= freeSize) {
        if (!segment.appendData(data))
            throw new RuntimeException("append index data failed.");
        return new AppendMessageResult<>(SUCCESS, segment.getBaseOffset() + segment.getWrotePosition());
    }/*  w w  w .j  a v a2s  . c  o  m*/

    ByteBuf to = ByteBufAllocator.DEFAULT.ioBuffer(freeSize);
    try {
        partialCopy(data, to);
        if (to.isWritable(Long.BYTES)) {
            to.writeLong(-1);
        }
        fillZero(to);
        if (!segment.appendData(to.nioBuffer()))
            throw new RuntimeException("append index data failed.");
    } finally {
        ReferenceCountUtil.release(to);
    }
    return new AppendMessageResult(END_OF_FILE, segment.getBaseOffset() + segment.getFileSize());
}