Example usage for io.netty.buffer ByteBuf readByte

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

Introduction

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

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:dorkbox.util.bytes.OptimizeUtilsByteBuf.java

License:Apache License

/**
 * FROM KRYO/*from  www.j  a  va  2 s .  c  o m*/
 * <p>
 * look at buffer, and see if we can read the length of the long off of it (from the reader index).
 *
 * @return 0 if we could not read anything, >0 for the number of bytes for the long on the buffer
 */
public static int canReadLong(ByteBuf buffer) {
    int position = buffer.readerIndex();
    try {
        int remaining = buffer.readableBytes();
        for (int offset = 0, count = 1; offset < 64 && remaining > 0; offset += 7, remaining--, count++) {
            int b = buffer.readByte();
            if ((b & 0x80) == 0) {
                return count;
            }
        }
        return 0;
    } finally {
        buffer.readerIndex(position);
    }
}

From source file:eu.jangos.realm.network.decoder.RealmPacketDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    logger.debug("Packet received: " + in.readableBytes());

    // We should at least get the header.
    if (in.readableBytes() < HEADER_LENGTH) {
        logger.debug("Packet received but less than header size.");
        return;//from w w  w. j av a2  s  .  co m
    }

    ByteBuf msg = in.order(ByteOrder.LITTLE_ENDIAN);

    // We should decrypt the header only once per packet.
    if (opcode == 0) {
        byte[] header = new byte[HEADER_LENGTH];
        int readBytes = (ctx.channel().attr(AUTH).get() == AuthStep.STEP_AUTHED ? HEADER_LENGTH : 4);

        for (int i = 0; i < readBytes; i++) {
            header[i] = msg.readByte();
        }

        header = ctx.channel().attr(CRYPT).get().decrypt(header);

        size = (short) ((header[0] << 8 | header[1]) & 0xFF);
        opcode = (short) ((header[3] << 8 | header[2] & 0xFF));

        logger.debug("Opcode received: " + opcode + ", with size: " + size + " (readable bytes: "
                + in.readableBytes() + ") ");
    }

    if ((in.readableBytes() + 4) < size) {
        logger.debug(
                "Packet size is higher than the available bytes. (" + in.readableBytes() + ", " + size + ")");
        return;
    }

    final Opcodes code = Opcodes.convert(opcode);

    if (code == null) {
        return;
    }

    AbstractRealmClientPacket packet = null;

    switch (code) {
    case CMSG_PING:
        packet = new CMSG_PING(code, size);
        break;
    case CMSG_AUTH_SESSION:
        packet = new CMSG_AUTH_SESSION(code, (short) 0);
        break;
    case CMSG_CHAR_ENUM:
        packet = new CMSG_CHAR_ENUM(code, size);
        break;
    case CMSG_CHAR_CREATE:
        packet = new CMSG_CHAR_CREATE(code, size);
        break;
    case CMSG_CHAR_DELETE:
        packet = new CMSG_CHAR_DELETE(code, size);
        break;
    case CMSG_PLAYER_LOGIN:
        packet = new CMSG_PLAYER_LOGIN(code, size);
        break;
    default:
        logger.debug("Context: " + ctx.name() + "Packet received, opcode not supported: " + code);
        msg.clear();
        ctx.close();
        break;
    }

    if (packet != null) {
        try {
            logger.debug("Context: " + ctx.name() + "Packet received, opcode: " + code);
            logger.debug("Packet content: \n"
                    + StringUtils.toPacketString(ByteBufUtil.hexDump(msg).toUpperCase(), size, code));
            packet.decode(msg);
            opcode = 0;
            size = 0;
        } catch (Exception e) {
            return;
        }
        out.add(packet);
        msg.clear();
    }
}

From source file:eu.jangos.realm.network.packet.client.auth.CMSG_AUTH_SESSION.java

License:Apache License

@Override
public void decode(ByteBuf buf) throws Exception {
    if (buf.readableBytes() < this.size) {
        throw new Exception();
    }//ww w.j a  va  2 s  .  c  o m

    buf.readShort();
    this.build = buf.readInt();
    this.unknown = buf.readInt();

    StringBuilder b = new StringBuilder();

    byte c;

    while ((c = buf.readByte()) != 0) {
        b.append((char) c);
    }

    this.account = b.toString();
    this.seed = buf.readBytes(4).array();
    this.digest = buf.readBytes(20).array();

    int sizeAddons = buf.readInt();

    byte[] compressedAddons = buf.readBytes(buf.readableBytes()).array();

    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedAddons);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedAddons.length);

    final byte[] buffer = new byte[1024];
    while (!decompressor.finished()) {
        try {
            final int count = decompressor.inflate(buffer);
            bos.write(buffer, 0, count);
        } catch (final DataFormatException e) {
        }
    }
    try {
        bos.close();
    } catch (final IOException e) {
    }

    final byte[] decompressedAddons = bos.toByteArray();

    if (sizeAddons != decompressedAddons.length) {
        System.out.println("Something went wrong");
        return;
    }

    ByteBuf addonBuffer = Unpooled.wrappedBuffer(decompressedAddons).order(ByteOrder.LITTLE_ENDIAN);

    this.listAddon = new ArrayList<>();

    while (addonBuffer.isReadable()) {
        String name;
        byte unk6;
        int crc, unk7;

        b = new StringBuilder();
        while ((c = addonBuffer.readByte()) != 0) {
            b.append((char) c);
        }

        name = b.toString();

        crc = addonBuffer.readInt();
        unk7 = addonBuffer.readInt();
        unk6 = addonBuffer.readByte();

        AddonInfo info = new AddonInfo(name, unk6, crc, unk7);
        this.listAddon.add(info);
    }

}

From source file:eu.jangos.realm.network.packet.client.character.CMSG_CHAR_CREATE.java

License:Apache License

@Override
public void decode(ByteBuf buf) throws Exception {
    if ((buf.readableBytes() + 4) < this.size) {
        throw new Exception();
    }/*from   ww  w .  j  ava 2s  . c  o m*/

    StringBuilder b = new StringBuilder();
    byte c;
    while ((c = buf.readByte()) != 0) {
        b.append((char) c);
    }

    this.name = b.toString();
    this.race = buf.readByte();
    this.charClass = buf.readByte();
    this.gender = buf.readByte();
    this.skin = buf.readByte();
    this.face = buf.readByte();
    this.hairStyle = buf.readByte();
    this.hairColor = buf.readByte();
    this.facialHair = buf.readByte();
}

From source file:eu.jangos.realm.network.packet.client.query.CMSG_WHO.java

License:Apache License

@Override
public void decode(ByteBuf buf) throws Exception {
    if ((buf.readableBytes() + 4) < this.size) {
        throw new Exception();
    }/*from   w  w  w  . j a  va2 s.c o m*/

    this.levelMin = buf.readInt();
    this.levelMax = buf.readInt();
    this.raceMask = buf.readInt();
    this.classMask = buf.readInt();
    this.zonesCount = buf.readInt();

    for (int i = 0; i < this.zonesCount; i++) {
        this.listZones.add(buf.readInt());
    }

    this.strCount = buf.readInt();

    StringBuilder b;
    byte c;
    for (int i = 0; i < this.strCount; i++) {
        b = new StringBuilder();
        while ((c = buf.readByte()) != 0) {
            b.append((char) c);
        }

        this.listStrings.add(b.toString());
    }

}

From source file:eu.jangos.realm.network.packet.client.query.CMSG_WHOIS.java

License:Apache License

@Override
public void decode(ByteBuf buf) throws Exception {
    if ((buf.readableBytes() + 4) < this.size) {
        throw new Exception();
    }/* w w w  . j  a v  a2s .c  om*/

    StringBuilder b = new StringBuilder();
    byte c;
    while ((c = buf.readByte()) != 0) {
        b.append((char) c);
    }

    this.name = b.toString();
}

From source file:eu.matejkormuth.rpgdavid.starving.remote.netty.packets.HandshakePacket.java

License:Open Source License

@Override
public void readFrom(ByteBuf fromBuffer) {
    byte[] nickname = new byte[fromBuffer.readByte()];
    fromBuffer.readBytes(nickname);//from   w  ww  . j av a  2  s .  c o m
    byte[] accesskey = new byte[fromBuffer.readByte()];
    fromBuffer.readBytes(accesskey);
    this.nickname = new String(nickname, PROTOCOL_ENCODING);
    this.accesskey = new String(accesskey, PROTOCOL_ENCODING);
}

From source file:eu.matejkormuth.rpgdavid.starving.remote.netty.packets.WGFiltersPacket.java

License:Open Source License

@Override
public void readFrom(ByteBuf fromBuffer) {
    byte filterCount = fromBuffer.readByte();
    FilterRepresentation[] filters = new FilterRepresentation[filterCount];
    for (int i = 0; i < filterCount; i++) {
        filters[i] = new FilterRepresentation();
        byte nameLength = fromBuffer.readByte();
        byte[] nameBuffer = new byte[nameLength];
        fromBuffer.readBytes(nameBuffer);
        filters[i].name = new String(nameBuffer, PROTOCOL_ENCODING);
        byte propertiesCount = fromBuffer.readByte();
        filters[i].properties = new FilterPropertyRepresentaion[propertiesCount];
        for (int p = 0; p < propertiesCount; p++) {
            byte propertyNameLength = fromBuffer.readByte();
            byte[] propertyNameBuffer = new byte[propertyNameLength];
            fromBuffer.readBytes(propertyNameBuffer);
            filters[i].properties[p] = new FilterPropertyRepresentaion();
            filters[i].properties[p].name = new String(propertyNameBuffer, PROTOCOL_ENCODING);
            filters[i].properties[p].type = fromBuffer.readByte();
        }/*from w  ww  . j  a  v  a  2  s.  c  o  m*/
    }
}

From source file:eu.xworlds.util.raknet.protocol.OpenConnectionRequest1.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic);
    this.procotolVersion = buf.readByte();
    this.mtuPayload = buf.readBytes(buf.readableBytes()).array();
}

From source file:eu.xworlds.util.raknet.protocol.RemoteSystemRequiresPublicKey.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.error = ErrorType.values()[buf.readByte()];
}