Java Code Examples for io.netty.buffer.ByteBuf.getShort()

The following are Jave code examples for showing how to use getShort() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-plc4x   File: IsoOnTcpProtocol.java   Source Code and License Vote up 5 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if(logger.isTraceEnabled()) {
        logger.trace("Got Data: {}", ByteBufUtil.hexDump(in));
    }
    // If at least 4 bytes are readable, peek into them (without changing the read position)
    // and get the packet length. Only if the available amount of readable bytes is larger or
    // equal to this, continue processing the rest.
    if(in.readableBytes() >= 4) {
        logger.debug("ISO on TCP Message received");
        // The ISO on TCP protocol is really simple and in this case the buffer length
        // will take care of the higher levels not reading more than is in the packet.
        // So we just gobble up the header and continue reading in higher levels.
        if (in.getByte(0) != ISO_ON_TCP_MAGIC_NUMBER) {
            logger.warn("Expecting ISO on TCP magic number: {}", ISO_ON_TCP_MAGIC_NUMBER);
            logger.debug("Got Data: " + ByteBufUtil.hexDump(in));
            exceptionCaught(ctx, new PlcProtocolException(
                String.format("Expecting ISO on TCP magic number: %02X", ISO_ON_TCP_MAGIC_NUMBER)));
            return;
        }
        // Byte 1 is a reserved byte set to 0x00
        short packetLength = in.getShort(2);
        if(in.readableBytes() >= packetLength) {
            // Skip the 4 bytes we peeked into manually.
            in.skipBytes(4);
            // Simply place the current buffer to the output ... the next handler will continue.
            ByteBuf payload = in.readBytes(packetLength - 4);
            out.add(new IsoOnTcpMessage(payload));
        }
    }
}
 
Example 2
Project: kcp-netty   File: TcpRttDecoder.java   Source Code and License Vote up 5 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 8) {
        return;
    }

    short dataLen = in.getShort(in.readerIndex() + 6);
    if (in.readableBytes() < dataLen) {
        return;
    }

    ByteBuf msg = in.readRetainedSlice(8 + dataLen);
    out.add(msg);
}
 
Example 3
Project: kcp-netty   File: TcpRttServerHandler.java   Source Code and License Vote up 5 votes
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf buf = (ByteBuf) msg;
    short curCount = buf.getShort(buf.readerIndex());
    ctx.writeAndFlush(msg);

    if (curCount == -1) {
        ctx.close();
    }
}
 
Example 4
Project: kcp-netty   File: KcpRttServerHandler.java   Source Code and License Vote up 5 votes
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf buf = (ByteBuf) msg;
    short curCount = buf.getShort(buf.readerIndex());
    ctx.writeAndFlush(msg);

    if (curCount == -1) {
        ctx.close();
    }
}