Example usage for io.netty.buffer ByteBuf getInt

List of usage examples for io.netty.buffer ByteBuf getInt

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf getInt.

Prototype

public abstract int getInt(int index);

Source Link

Document

Gets a 32-bit integer at the specified absolute index in this buffer.

Usage

From source file:org.jdiameter.client.impl.transport.tls.netty.DiameterMessageDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    logger.debug("Decode message size: {}", in.readableBytes());

    if (in.readableBytes() >= 4) {
        int first = in.getInt(in.readerIndex());
        byte version = (byte) (first >> 24);
        if (version != 1) {
            return;
        }/*from w w  w  .j av  a 2  s .com*/

        int messageLength = (first & 0xFFFFFF);
        if (in.readableBytes() < messageLength) {
            return;
        }

        logger.debug("Decoding message version: {}, length: {}", version, messageLength);

        byte[] bytes = new byte[messageLength];
        in.readBytes(bytes);
        try {
            out.add(this.parser.createMessage(bytes));
        } catch (AvpDataException e) {
            logger.error(e.getMessage(), e);

            this.parentConnection.onAvpDataException(e);
        }
    }
}

From source file:org.neo4j.bolt.transport.TransportSelectionHandler.java

License:Open Source License

private boolean isBoltPreamble(ByteBuf in) {
    return in.getInt(0) == BOLT_MAGIC_PREAMBLE;
}

From source file:org.traccar.protocol.TeltonikaFrameDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    // Check minimum length
    if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) {
        return null;
    }//  ww w .j ava2s.c  om

    // Read packet
    int length = buf.getUnsignedShort(buf.readerIndex());
    if (length > 0) {
        if (buf.readableBytes() >= (length + 2)) {
            return buf.readRetainedSlice(length + 2);
        }
    } else {
        int dataLength = buf.getInt(buf.readerIndex() + 4);
        if (buf.readableBytes() >= (dataLength + 12)) {
            return buf.readRetainedSlice(dataLength + 12);
        }
    }

    return null;
}

From source file:qunar.tc.qmq.netty.DecodeHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list)
        throws Exception {
    if (in.readableBytes() < RemotingHeader.MIN_HEADER_SIZE + RemotingHeader.LENGTH_FIELD)
        return;/*from  w ww. j  a  va 2 s  . c om*/

    int magicCode = in.getInt(in.readerIndex() + RemotingHeader.LENGTH_FIELD);
    if (DEFAULT_MAGIC_CODE != magicCode) {
        throw new IOException("Illegal Data, MagicCode=" + Integer.toHexString(magicCode));
    }

    in.markReaderIndex();
    int total = in.readInt();
    if (in.readableBytes() < total) {
        in.resetReaderIndex();
        return;
    }

    short headerSize = in.readShort();
    RemotingHeader remotingHeader = decodeHeader(in);

    int bodyLength = total - headerSize - RemotingHeader.HEADER_SIZE_LEN;

    RemotingCommand remotingCommand = new RemotingCommand();
    //because netty(lower version) has memory leak when ByteBuf cross thread
    //We can ensure server use high version netty, bu we can't ensure client
    if (isServer) {
        ByteBuf bodyData = in.readSlice(bodyLength);
        bodyData.retain();
        remotingCommand.setBody(bodyData);
    } else {
        ByteBuf bodyData = Unpooled.buffer(bodyLength, bodyLength);
        in.readBytes(bodyData, bodyLength);
        remotingCommand.setBody(bodyData);
    }
    remotingCommand.setHeader(remotingHeader);
    list.add(remotingCommand);
}

From source file:ru.jts_dev.authserver.util.Encoder.java

License:Open Source License

public ByteBuf validateChecksum(ByteBuf buf) {
    if (buf.readableBytes() % 4 != 0 || buf.readableBytes() <= 4) {
        throw new IndexOutOfBoundsException("ByteBuf size must be multiply of 4 and more, that 4");
    }//w ww  .jav  a2  s .co  m

    long checksum = 0;
    long check;
    int i;

    for (i = 0; i < buf.readableBytes() - 4; i += 4) {
        check = buf.getInt(i);

        checksum ^= check;
    }

    check = buf.getInt(i);

    if (check != checksum) {
        throw new InvalidParameterException("Wrong checksum");
    }

    return buf.copy(0, buf.readableBytes() - 4);
}

From source file:ru.jts_dev.authserver.util.Encoder.java

License:Open Source License

public ByteBuf appendChecksum(ByteBuf buf) {
    if (buf.readableBytes() % 4 != 0) {
        throw new IndexOutOfBoundsException("ByteBuf size must be multiply of 4");
    }//from w w w.jav a 2  s . c om
    int checksum = 0;
    for (int i = 0; i < buf.readableBytes(); i += 4) {
        checksum ^= buf.getInt(i);
    }

    buf.writeInt(checksum);

    buf.writeZero(4); // for blowfish block

    return buf;
}

From source file:ru.jts_dev.authserver.util.Encoder.java

License:Open Source License

public ByteBuf encWithXor(ByteBuf buf) {
    if (buf.readableBytes() % 4 != 0) {
        throw new IndexOutOfBoundsException("ByteBuf size must be multiply of 4");
    }/*from   w  w w . j  a  v a2 s.c om*/
    int edx;
    int ecx = 0; // Initial xor key

    buf.writeLong(random.nextLong()); // 8 bytes padding

    for (int pos = 4; pos < buf.readableBytes(); pos += 4) {
        edx = buf.getInt(pos);

        ecx += edx;
        edx ^= ecx;

        buf.setInt(pos, edx);
    }
    buf.writeInt(ecx);

    buf.writeInt(random.nextInt()); // 4 bytes for blowfish block

    return buf;
}

From source file:ru.jts_dev.gameserver.util.Encoder.java

License:Open Source License

@Transformer
public ByteBuf decrypt(ByteBuf data, @Header(CONNECTION_ID) String connectionId) {
    GameSession gameSession = sessionService.getSessionBy(connectionId);

    assert gameSession != null : "GameSession for " + connectionId + " does not exist";

    ByteBuf key = gameSession.getDecryptKey();

    int temp = 0;
    for (int i = 0; i < data.readableBytes(); i++) {
        final int temp2 = data.getUnsignedByte(i);
        data.setByte(i, (byte) (temp2 ^ key.getByte(i & 15) ^ temp));
        temp = temp2;// ww  w  .ja v a  2s .  co m
    }

    int old = key.getInt(8);
    old += data.readableBytes();

    key.setInt(8, old);

    return data;
}

From source file:ru.jts_dev.gameserver.util.Encoder.java

License:Open Source License

@Transformer
public ByteBuf encrypt(ByteBuf data, @Header(CONNECTION_ID) String connectionId) {
    GameSession gameSession = sessionService.getSessionBy(connectionId);

    assert gameSession != null : "GameSession for " + connectionId + " does not exist";

    ByteBuf key = gameSession.getEncryptKey();

    int temp = 0;
    for (int i = 0; i < data.readableBytes(); i++) {
        int temp2 = data.getUnsignedByte(data.readerIndex() + i);
        temp = temp2 ^ key.getByte(i & 15) ^ temp;
        data.setByte(data.readerIndex() + i, (byte) temp);
    }/*from   ww w .  j a  v a 2 s  .  co m*/

    int old = key.getInt(8);
    old += data.readableBytes();

    key.setInt(8, old);

    return data;
}