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

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

Introduction

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

Prototype

ByteBuf content();

Source Link

Document

Return the data which is held by this ByteBufHolder .

Usage

From source file:com.andrewkroh.cicso.rtp.RtpPacketHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    if (msg.content().readableBytes() > RtpPacket.FIXED_HEADER_SIZE) {
        byte[] bufferCopy = new byte[msg.content().readableBytes()];
        msg.content().readBytes(bufferCopy);
        RtpPacket packet = new RtpPacket(bufferCopy);

        listener.packetReceived(msg.sender(), msg.recipient(), packet);
    }/*from   w ww.  j  ava  2s.c o m*/
}

From source file:com.changxx.phei.netty.protocol.udp.ChineseProverbClientHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    String response = msg.content().toString(CharsetUtil.UTF_8);
    if (response.startsWith(": ")) {
        System.out.println(response);
        ctx.close();/*from  ww  w  .ja v  a  2 s  .  com*/
    }
}

From source file:com.changxx.phei.netty.protocol.udp.ChineseProverbServerHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    String req = packet.content().toString(CharsetUtil.UTF_8);
    System.out.println(req);/* w w w .j  a  v a2 s. c  o m*/
    if ("?".equals(req)) {
        ctx.writeAndFlush(new DatagramPacket(
                Unpooled.copiedBuffer(": " + nextQuote(), CharsetUtil.UTF_8),
                packet.sender()));
    }
}

From source file:com.flysoloing.learning.network.netty.qotm.QuoteOfTheMomentClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    String response = msg.content().toString(CharsetUtil.UTF_8);
    if (response.startsWith("QOTM: ")) {
        System.out.println("Quote of the Moment: " + response.substring(6));
        ctx.close();//  w w  w  .  j  a  v a  2 s.c  om
    }
}

From source file:com.flysoloing.learning.network.netty.qotm.QuoteOfTheMomentServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    System.err.println(packet);/*from   ww w . j a  va2s . co  m*/
    if ("QOTM?".equals(packet.content().toString(CharsetUtil.UTF_8))) {
        ctx.write(new DatagramPacket(Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8),
                packet.sender()));
    }
}

From source file:com.github.mrstampy.kitchensync.netty.handler.AbstractKiSyNettyHandler.java

License:Open Source License

/**
 * Content./*  w  w  w  . j a va 2  s.c  o  m*/
 *
 * @param msg
 *          the msg
 * @return the string
 */
protected String content(DatagramPacket msg) {
    return msg.content().toString(CharsetUtil.UTF_8);
}

From source file:com.github.mrstampy.kitchensync.netty.handler.AbstractKiSyNettyHandler.java

License:Open Source License

/**
 * Bytes./*from  w ww .  j  av  a2s .c  om*/
 *
 * @param msg
 *          the msg
 * @return the byte[]
 */
protected byte[] bytes(DatagramPacket msg) {
    ByteBuf content = msg.content();

    int num = content.readableBytes();
    byte[] message = new byte[num];
    content.readBytes(message);

    return message;
}

From source file:com.goodgamenow.source.serverquery.MasterQueryHandler.java

License:Open Source License

/**
 * Decodes a master server response datagram packet into a list of
 * game server addresses./*ww w  . j  av a2  s . c  o m*/
 *
 * @param ctx channel handler context
 * @param msg master server response packet
 * @exception UnsupportedEncodingException
 */
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws UnsupportedEncodingException {
    ByteBuf buf = msg.content();

    // sanity check
    int ADDR_WIDTH = 6;
    assert (buf.readableBytes() % ADDR_WIDTH) == 0 : "Master response byte count is not 6 byte aligned.";

    // decode response header
    String header = decodeIpAddress(buf);
    assert EXPECTED_HEADER_STRING.equals(header);

    while (buf.isReadable(ADDR_WIDTH)) {
        lastAddress = decodeIpAddress(buf);
        // A last address of 0.0.0.0:0 denotes the end of transmission.
        if (DEFAULT_IP.equals(lastAddress)) {
            ctx.flush();
            ctx.close();
            finishTime = System.currentTimeMillis();
            return;
        }

        if (parentContext != null) {
            InetSocketAddress address = createInetSocketAddress(lastAddress);
            ServerQuery template = query.template;
            ServerQuery squery = ServerQuery.createFromTemplate(address, template);
            parentContext.fireChannelRead(squery);
        } else {
            if (results == null) { // should never happen
                throw new IllegalStateException("Results container is null when there is no other "
                        + "ChannelHandlerContext to send results.");
            }
            // we are storing for bulk access later.
            results.add(lastAddress);
        }

    }

    assert buf.readableBytes() == 0;
    // ask for more results
    this.channelActive(ctx);
}

From source file:com.heliosapm.shorthand.caster.broadcast.BroadcastListenerRouter.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    ByteBuf data = msg.content();
    byte msgType = data.readByte();
    BroadcastType bt = BroadcastType.ORD2ENUM.get((int) msgType);
    log("Processing Broadcast [%s]", bt.name());
    BroadcastExecutable exec = bt.unmarshallPacket(data.nioBuffer(), msg.sender());
    taskThreadPool.execute(exec);// w w  w.  j a va 2s .  c  om
}

From source file:com.heliosapm.streams.onramp.DatagramToBytesDecoder.java

License:Apache License

/**
 * {@inheritDoc}//from   w  w w.  j  av  a  2 s  .  c o m
 * @see io.netty.handler.codec.MessageToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)
 */
@Override
protected void decode(final ChannelHandlerContext ctx, final DatagramPacket msg, final List<Object> out)
        throws Exception {
    out.add(msg.content().retain());
}