Example usage for io.netty.buffer ByteBuf writeByte

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

Introduction

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

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

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

License:Open Source License

public static final long readRing(ByteBuf converter, short[] shift) {
    for (int i = 0; i < shift.length; i++) {
        converter.writeByte(shift[i]);
    }//w ww.jav  a  2s . c  om

    long uInt = converter.readInt();
    converter.clear();
    return uInt;
}

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

License:Open Source License

public static final short[] writeByteArray(ByteBuf buff, short[] uByteArray) {
    for (int i = 0; i < uByteArray.length; i++) {
        buff.writeByte(uByteArray[i]);
    }//  w w  w .  j  a va2 s.c om
    return uByteArray;
}

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

License:Open Source License

/**
 * C3 C4/*from  w  w  w  .  j  av a 2s  .c  om*/
 * @param buff
 * @return 
 */
public static ByteBuf DecodePacket(ByteBuf buff) {
    if (buff.writerIndex() <= 2) {
        log.fatal("Ambiguous buffer! " + ByteBufUtil.hexDump(buff));
        return null;
    }
    if (buff.readerIndex() != 0) {
        log.warn("Buffer must be at index 0!");
        buff.readerIndex(0);
    }

    int header = GetHeaderSize(buff);
    int packetSize = GetPacketSize(buff);
    int decodedSize = GetDecodedSize(buff);

    int contentSize = packetSize - header;

    //System.out.println("Header[0x"+PrintData.fillHex(buff.getUnsignedByte(0), 2)+"] size: "+GetHeaderSize(buff)+" Packet Size: "+GetPacketSize(buff) +" Content Size: "+contentSize +" Decoded: "+ GetDecodedSize(buff));

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

    int originalHead = buff.getUnsignedByte(0);

    buff.readerIndex(header);
    int size = DecodeBlock(buff, out, header, contentSize);
    //buff.clear();

    size += header - 1;

    out.writerIndex(0);
    out.writeByte(originalHead);

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

    out.writerIndex(size);
    out.readerIndex(header);

    DecXor32(out, header, size);

    out.readerIndex(0);
    return out;
}

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);//from   w  w  w .  jav a2  s .com

    buff.setByte(header - 1, serial);

    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.MuEncoder.java

License:Open Source License

/**
 * @param contents//from  w w w  .j  a v a2s.co  m
 * @param out
 * @param header
 * @param Size
 * @param toServer 
 * @return 
 */
private static int EncodeBuffer(short[] contents, ByteBuf out, int header, int Size) {
    int i = 0;
    int EncSize = 0;

    while (i < Size) {
        short[] Encrypted = new short[11];

        if (i + 8 < Size) {
            short[] Decrypted = new short[8];
            System.arraycopy(contents, i, Decrypted, 0, 8);
            BlockEncode(Encrypted, Decrypted, 8, MuKeyFactory.getServerToClientPacketEncKeys());
        } else {
            short[] Decrypted = new short[Size - i];
            System.arraycopy(contents, i, Decrypted, 0, Decrypted.length);

            BlockEncode(Encrypted, Decrypted, (Size - i), MuKeyFactory.getServerToClientPacketEncKeys());
        }

        out.writerIndex(header + EncSize);
        for (int j = 0; j < Encrypted.length; j++) {
            out.writeByte(Encrypted[j]);
        }

        i += 8;
        EncSize += 11;
    }

    return EncSize;
}

From source file:com.vethrfolnir.game.network.mu.packets.MuWritePacket.java

License:Open Source License

public void writeD(ByteBuf buff, int value, ByteOrder order) {
    switch (order.toString()) {
    case "BIG_ENDIAN": {
        buff.writeByte(value >> 8);
        buff.writeByte(value >> 16);
        buff.writeByte(value >> 32);
        buff.writeByte(value & 0xFF);
        break;//from ww  w.  j ava 2  s . c  om
    }
    case "LITTLE_ENDIAN": {
        buff.writeByte(value & 0xFF);
        buff.writeByte(value >> 32);
        buff.writeByte(value >> 16);
        buff.writeByte(value >> 8);
    }
    }
}

From source file:com.vethrfolnir.game.network.mu.packets.MuWritePacket.java

License:Open Source License

public void writeSh(ByteBuf buff, int value, ByteOrder order) {
    switch (order.toString()) {
    case "BIG_ENDIAN": {
        buff.writeByte(value >> 8);
        buff.writeByte(value & 0xFF);
        break;// ww  w .  j av  a  2  s .  c o m
    }
    case "LITTLE_ENDIAN": {
        buff.writeByte(value & 0xFF);
        buff.writeByte(value >> 8);
    }
    }
}

From source file:com.vethrfolnir.game.network.mu.packets.MuWritePacket.java

License:Open Source License

public void writeS(ByteBuf buff, String value, int max) {
    if (value == null)
        throw new RuntimeException("Value is null!");

    try {/*from w w w.  ja va 2 s .c o  m*/
        int l = value.length();
        buff.writeBytes(value.getBytes("ISO-8859-1"));
        for (int i = l; i < max; i++)
            buff.writeByte(0x00);
    } catch (UnsupportedEncodingException e) {
        log.warn("Failed writing string!", e);
    }
}

From source file:com.vethrfolnir.login.network.mu.send.SendServerLists.java

License:Open Source License

/**
 * @param context/*from www .ja  va 2 s.  c o  m*/
 * @param buff
 * @param params - GameNameService[0] 
 */
@Override
public void write(NetworkClient context, ByteBuf buff, Object... params) {
    GameNameService nameService = as(params[0]);

    int actualSize = nameService.liveServerSize();

    int size = (7 + (actualSize * 4));
    buff.writeByte(0xC2); // opcode

    //buff.writeShort(size);
    buff.writeByte(0x00); // size or empty ?
    buff.writeByte(size); // size?

    buff.writeByte(0xF4); // code
    buff.writeByte(0x06); // code

    buff.writeByte(0x00); // size or empty ?
    buff.writeByte(actualSize); // actual size

    // Formula
    // writeC((1 * 20 + 1));
    // 1 = server position on list, if changed to another nr it will subserver to that server id, else disapire
    // 20 = server to mult

    for (HashMap<Integer, GameServer> map : nameService.getLiveServers().values()) {
        for (GameServer server : map.values()) {
            int onlinePlayers = server.getOnlinePlayers() / server.getCap() * 100;

            buff.writeShort(server.getServerId());
            buff.writeShort(onlinePlayers);
        }
    }
}

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

License:Open Source License

public void writeC(ByteBuf buff, int value) {
    buff.writeByte(value);
}