Example usage for io.netty.buffer ByteBuf readerIndex

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

Introduction

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

Prototype

public abstract ByteBuf readerIndex(int readerIndex);

Source Link

Document

Sets the readerIndex of this buffer.

Usage

From source file:com.talent.mysql.packet.request.AuthPacket.java

License:Open Source License

/**
 * @param args/*from  w ww .  j a va2 s.  c o  m*/
 */
public static void main(String[] args) {
    AuthPacket authPacket1 = new AuthPacket();
    authPacket1.decodeBody(null);

    byte[] bs = new byte[] { 82, 0, 0, 0, 10, 49, 48, 46, 48, 46, 49, 45, 77, 97, 114, 105, 97, 68, 66, 0, -98,
            1, 0, 0, 110, 104, 61, 56, 64, 122, 101, 107, 0, -1, -9, 8, 2, 0, 15, -96, 21, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 105, 78, 41, 35, 111, 43, 39, 124, 98, 82, 87, 60, 0, 109, 121, 115, 113, 108, 95, 110, 97,
            116, 105, 118, 101, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0 };
    ByteBuf byteBuf = Unpooled.buffer(bs.length);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);

    byteBuf.setBytes(0, bs);

    HandshakePacket handshakePacket = new HandshakePacket();
    try {
        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);
    } catch (DecodeException e) {
        e.printStackTrace();
    }

    BackendConf backendConf = BackendConf.getInstance();
    BackendServerConf backendServerConf = backendConf.getServers()[0];

    AuthPacket authPacket = new AuthPacket();
    authPacket.charsetIndex = (byte) (handshakePacket.charset & 0xff);
    authPacket.user = backendServerConf.getProps().get("user").getBytes();
    try {
        authPacket.password = getPass(backendServerConf.getProps().get("pwd"), handshakePacket);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    authPacket.passwordLen = (byte) authPacket.password.length;
    authPacket.database = backendServerConf.getProps().get("db").getBytes();
    ByteBuf byteBuf1 = authPacket.encode();
    System.out.println(Arrays.toString(byteBuf1.array()));

}

From source file:com.talent.mysql.packet.response.HandshakePacket.java

License:Open Source License

/**
 * @param args/*from   w w w. ja  v a 2 s.c om*/
 * @throws DecodeException
 */
public static void main(String[] args) {
    byte[] bs = new byte[] { 82, 0, 0, 0, 10, 49, 48, 46, 48, 46, 49, 45, 77, 97, 114, 105, 97, 68, 66, 0, -98,
            1, 0, 0, 110, 104, 61, 56, 64, 122, 101, 107, 0, -1, -9, 8, 2, 0, 15, -96, 21, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 105, 78, 41, 35, 111, 43, 39, 124, 98, 82, 87, 60, 0, 109, 121, 115, 113, 108, 95, 110, 97,
            116, 105, 118, 101, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0 };
    ByteBuf byteBuf = Unpooled.buffer(bs.length);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);

    byteBuf.setBytes(0, bs);

    HandshakePacket handshakePacket = new HandshakePacket();
    try {
        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);

        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);

        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);

        handshakePacket.decode(byteBuf);
        byteBuf.readerIndex(0);
    } catch (DecodeException e) {
        e.printStackTrace();
    }

}

From source file:com.vethrfolnir.encdec.ReadDataFiles.java

License:Open Source License

public static long[] readFile(URL url) {

    ByteBuf buff = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);

    try (DataInputStream is = new DataInputStream(url.openStream())) {
        buff.writeBytes(is, is.available());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }//from ww  w. j  a  v  a2s  . c o m

    int bits2 = buff.readUnsignedShort();

    System.out.println("First two bits: " + bits2 + " hex: 0x" + PrintData.fillHex(bits2, 2));

    long[] out_dat = new long[12];

    buff.readerIndex(6);
    int pointer = 0;
    for (int i = 0; i < 3; i++) {
        long[] buf = new long[4];

        for (int j = 0; j < 4; j++) {
            buf[j] = buff.readUnsignedInt();
        }

        out_dat[pointer++] = buf[0] ^ (xor_tab_datfile[0]);
        out_dat[pointer++] = buf[1] ^ (xor_tab_datfile[1] & 0xFFFFFFFFL);
        out_dat[pointer++] = buf[2] ^ (xor_tab_datfile[2] & 0xFFFFFFFFL);
        out_dat[pointer++] = buf[3] ^ (xor_tab_datfile[3]);
    }

    for (int i = 0; i < out_dat.length; i++) {
        System.out.print(" " + (out_dat[i]));
    }

    System.out.println();
    return null;
}

From source file:com.vethrfolnir.game.network.mu.crypt.MuEncoder.java

License:Open Source License

public static ByteBuf EncodePacket(ByteBuf buff, int serial) {
    int header = GetHeaderSize(buff);
    int packetSize = GetPacketSize(buff);
    int contentSize = packetSize - header;

    int encodedSize = (((contentSize / 8) + (((contentSize % 8) > 0) ? 1 : 0)) * 11) + header;

    int size = header;
    int originalHead = buff.getUnsignedByte(0);

    ByteBuf out = alloc.heapBuffer(encodedSize, encodedSize);

    //buff.writerIndex(buff.writerIndex() + 1);
    short[] Contents = new short[contentSize + 1];
    Contents[0] = (short) serial; // XXX:  Check this

    buff.readerIndex(header - 1);

    buff.setByte(header - 1, serial);// w  ww.  j  ava 2s. com

    MuCryptUtils.readAsUByteArray(buff, Contents);

    //System.out.println("Encoding: "+PrintData.printData(Contents));

    size += EncodeBuffer(Contents, out, header, (contentSize + 1));

    out.writerIndex(0);

    // Header
    out.writeByte(originalHead);

    // Size write
    switch (originalHead) {
    case 0xc3:
        out.writeByte(size);
        break;
    case 0xC4:
        out.writeByte(size >> 8);
        out.writeByte(size & 0xFF);
        break;
    }

    out.writerIndex(size);

    return out;
}

From source file:com.vethrfolnir.game.network.mu.crypt.MuKeyFactory.java

License:Open Source License

private static void readFile(String path, long[] out_dat) {

    AssetManager assetManager = Corax.fetch(AssetManager.class);

    ByteBuf buff = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);

    try (InputStream is = assetManager.loadAsset(FileKey.class, path)) {
        buff.writeBytes(is, is.available());
    } catch (IOException e) {
        e.printStackTrace();/*w  w  w  .ja  v a2 s . com*/
    }

    buff.readerIndex(6);
    int pointer = 0;
    for (int i = 0; i < 3; i++) {
        long[] buf = new long[4];

        for (int j = 0; j < 4; j++) {
            buf[j] = buff.readUnsignedInt();
        }

        out_dat[pointer++] = buf[0] ^ (xor_tab_datfile[0]);
        out_dat[pointer++] = buf[1] ^ (xor_tab_datfile[1] & 0xFFFFFFFFL);
        out_dat[pointer++] = buf[2] ^ (xor_tab_datfile[2] & 0xFFFFFFFFL);
        out_dat[pointer++] = buf[3] ^ (xor_tab_datfile[3]);
    }
}

From source file:com.vethrfolnir.game.network.mu.MuChannelHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buff = (msg instanceof ByteBuffer) ? ctx.alloc().buffer().writeBytes((ByteBuffer) msg)
            : (ByteBuf) msg;//from  w ww.j av a2 s  .  c o  m

    buff.readerIndex(2);

    int opcode = buff.readUnsignedByte();
    switch (opcode) { // double opcode
    case 0xf1:
    case 0xf3:
    case 0x0e:
    case 0x03:
        buff.readerIndex(buff.readerIndex() - 1);
        opcode = buff.readUnsignedShort(); // ex 0xF1_03
        break;
    default:
        break;
    }

    if (opcode == 0xe00) { // Time packet?
        buff.clear();
        buff.release();
        return;
    }

    ReadPacket packet = clientpackets.get(opcode);

    if (packet != null) {
        MuClient client = ctx.channel().attr(MuClient.ClientKey).get();
        //System.out.println("Got opcode: 0x"+PrintData.fillHex(opcode, 2)+ " packet = \n"+packet.getClass().getSimpleName());
        packet.read(client, buff);
    } else {
        log.warn("Unknown packet[opcode = 0x" + PrintData.fillHex(opcode, 2) + "]. Dump: ");
        log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex())));
    }

    //log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex())));

    if (buff.refCnt() > 0) {
        //System.out.println("Handler Release when packet[opcode = 0x"+PrintData.fillHex(opcode, 2)+"]");
        buff.release();
    }
}

From source file:com.vethrfolnir.game.network.mu.received.ExRequestAuth.java

License:Open Source License

@Override
public void read(MuClient client, ByteBuf buff, Object... params) {

    if (MuNetworkServer.onlineClients() >= capacity) {
        invalidate(buff);// w  ww  .ja v a 2  s .c  o m

        client.sendPacket(MuPackets.ExAuthAnswer, AuthResult.ServerIsFull);
        return;
    }

    byte[] data1 = new byte[10];
    byte[] data2 = new byte[10];

    buff.getBytes(4, data1);
    buff.getBytes(14, data2);

    MuCryptUtils.Dec3bit(data1, 0, 10);
    MuCryptUtils.Dec3bit(data2, 0, 10);

    buff.setBytes(4, data1);
    buff.setBytes(14, data2);

    buff.readerIndex(4);

    String user = readS(buff, 10);
    String password = readS(buff, 10);

    buff.readerIndex(38);

    String version = readS(buff, 5);
    String mainSerial = readS(buff, 16);
    System.out.println("user: [" + user + "] pass[" + password + "] - " + " Version:[" + version + "] "
            + " Serial: [" + mainSerial + "]");

    enqueue(client, user, password, version, mainSerial);
}

From source file:com.vethrfolnir.login.network.mu.ClientChannelHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buff = (ByteBuf) msg;

    switch (buff.getUnsignedByte(0)) {
    case 0xC1:/*from  w  w  w  . j  a va  2  s.c o  m*/
    case 0xC2:
    case 0xC3:
    case 0xC4:
        break;
    default:
        ctx.close();
        buff.release(); //TODO: maybe add a flood protector?
        log.warn("Client[" + ctx.channel() + "] is not a mu online client! Disconnecting!");
        return;
    }

    // we are not interested in the header and size;
    buff.readerIndex(2);

    int opcode = buff.readUnsignedShort();

    switch (opcode) {

    case 0xf4_03: { // Request Server information
        int serverId = buff.readUnsignedByte();
        ByteBuf buf = ctx.alloc().heapBuffer().order(ByteOrder.LITTLE_ENDIAN);

        WritePacket packet = SendServerInfo.StaticPacket;
        packet.write(null, buf, nameService, serverId);
        packet.markLength(buf);
        ;

        ctx.writeAndFlush(buf);
        break;
    }
    case 0xf4_06: { // Request Server list
        ByteBuf buf = ctx.alloc().heapBuffer().order(ByteOrder.LITTLE_ENDIAN);

        WritePacket packet = SendServerLists.StaticPacket;
        packet.write(null, buf, nameService);
        packet.markLength(buf);

        ctx.writeAndFlush(buf);
        break;
    }
    default:
        log.warn("Unkown packet[OPCODE = " + Integer.toHexString(opcode) + "] Dump: "
                + PrintData.printData(buff.nioBuffer(0, buff.writerIndex())));
        ctx.close();
        break;
    }
    buff.release();
}

From source file:com.vethrfolnir.network.ReadPacket.java

License:Open Source License

protected void invalidate(ByteBuf buff) {
    buff.readerIndex(buff.writerIndex());
}

From source file:com.xx_dev.apn.proxy.utils.ByteBufLogUtil.java

License:Apache License

public static String toLogString(ByteBuf byteBuf) {
    StringBuilder sb = new StringBuilder();
    byteBuf.readerIndex(0);
    while (byteBuf.isReadable()) {
        sb.append(String.valueOf((int) byteBuf.readByte())).append(",");
    }/*from w ww.  j  a va2 s. c om*/

    byteBuf.readerIndex(0);
    return sb.toString();
}