Example usage for io.netty.channel.socket DatagramPacket retain

List of usage examples for io.netty.channel.socket DatagramPacket retain

Introduction

In this page you can find the example usage for io.netty.channel.socket DatagramPacket retain.

Prototype

@Override
    public DatagramPacket retain() 

Source Link

Usage

From source file:c5db.codec.UdpProtostuffEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, UdpProtostuffMessage<T> msg, List<Object> out)
        throws Exception {
    LinkBuffer buffer = new LinkBuffer(bufferAllocSize);
    if (protostuffOutput) {
        LowCopyProtostuffOutput lcpo = new LowCopyProtostuffOutput(buffer);
        schema.writeTo(lcpo, msg.message);
    } else {//from w  ww  . j a v a2 s.  c  om
        LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(buffer);
        schema.writeTo(lcpo, msg.message);
    }

    List<ByteBuffer> buffers = buffer.finish();
    ByteBuf data = Unpooled.wrappedBuffer(buffers.toArray(new ByteBuffer[buffers.size()]));
    data.retain();

    DatagramPacket dg = new DatagramPacket(data, msg.remoteAddress);
    dg.retain();
    out.add(dg);
}

From source file:com.ibasco.agql.protocols.valve.source.query.handlers.SourceQueryPacketAssembler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    log.trace("SourcePacketHandler.channelRead() : START");

    try {/*from  ww w  .ja va2 s . c  om*/
        //Make sure we are only receiving an instance of DatagramPacket
        if (!(msg instanceof DatagramPacket)) {
            return;
        }

        final DatagramPacket packet = (DatagramPacket) msg;
        final ByteBuf data = ((DatagramPacket) msg).content();

        //Verify size
        if (data.readableBytes() <= 5) {
            log.debug(
                    "Not a valid datagram for processing. Size getTotalRequests needs to be at least more than or equal to 5 bytes. Discarding. (Readable Bytes: {})",
                    data.readableBytes());
            return;
        }

        //Try to read protocol header, determine if its a single packet or a split-packet
        int protocolHeader = data.readIntLE();

        //If the packet arrived is single type, we can already forward it to the next handler
        if (protocolHeader == 0xFFFFFFFF) {
            //Pass the message to the succeeding handlers
            ctx.fireChannelRead(packet.retain());
            return;
        }
        //If the packet is a split type...we need to process each succeeding read until we have a complete packet
        else if (protocolHeader == 0xFFFFFFFE) {
            final ByteBuf reassembledPacket = processSplitPackets(data, ctx.channel().alloc(), packet.sender());
            //Check if we already have a reassembled packet
            if (reassembledPacket != null) {
                ctx.fireChannelRead(packet.replace(reassembledPacket));
                return;
            }
        }
        //Packet is not being handled by any of our processors, discard
        else {
            log.debug("Not a valid protocol header. Discarding. (Header Received: Dec = {}, Hex = {})",
                    protocolHeader, Integer.toHexString(protocolHeader));
            return;
        }
    } catch (Exception e) {
        log.error(String.format("Error while processing packet for %s", ((DatagramPacket) msg).sender()), e);
        throw e;
    } finally {
        //Release the message
        ReferenceCountUtil.release(msg);
    }
    log.trace("SourcePacketHandler.channelRead() : END");
}

From source file:com.streamsets.pipeline.lib.udp.PacketQueueUDPHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    packet.retain();
    final boolean succeeded = queue.offer(packet);
    if (succeeded) {
        gaugeMap.put(GAUGE_NUM_QUEUED_PACKETS, queuedPacketCount.incrementAndGet());
        gaugeMap.put(GAUGE_PACKET_QUEUE_SIZE, queue.size());
    } else {//from  w  w w .ja  v a2  s . c om
        gaugeMap.put(GAUGE_NUM_DROPPED_PACKETS, droppedPacketCount.incrementAndGet());
        // allow Netty to collect the buffer
        packet.release();
    }
}