Example usage for io.netty.buffer ByteBuf readShort

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

Introduction

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

Prototype

public abstract short readShort();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

From source file:com.zhuika.discard.DiscardClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    // /* w w w .  j a  v a2s.co m*/
    int length = in.readableBytes();
    System.out.println(":" + length);
    // ?? $$
    String head = in.readBytes(2).toString(Charset.defaultCharset());
    System.out.println(head);
    // 
    short l = in.readShort();
    System.out.println(l);
    // ?id ??
    String serialNumber = byteToArray(in.readBytes(7).array());
    System.out.println(serialNumber);
    // ???
    String agreement = byteToArray(in.readBytes(2).array());
    System.out.println(agreement);
    // ?
    String content = new String(Hex.decodeHex(byteToArray(in.readBytes(length - 17).array()).toCharArray()));//byteToArray(in.readBytes(l - 17).array());         
    System.out.println(content);
    //?
    //String flag=byteToArray(in.readBytes(1).array());
    //System.out.println(flag);
    // ? checksum
    String checksum = byteToArray(in.readBytes(2).array());
    System.out.println(checksum);
    // ?\r\n
    String end = byteToArray(in.readBytes(2).array());
    System.out.println(end);
    //Thread.sleep(70000);
    String message = "24240011" + serialNumber + "0002daa50d0a2424000d0a";
    byte[] b = HexString2Bytes(message);
    System.out.println(Hex.byteToArray(b));
    ctx.writeAndFlush(b);
}

From source file:cubicchunks.network.PacketCubeBlockChange.java

License:MIT License

@SuppressWarnings("deprecation") // Forge thinks we are trying to register a block or something :P
@Override/*from   w ww.  jav a2 s  . c  om*/
public void fromBytes(ByteBuf in) {
    this.cubePos = new CubePos(in.readInt(), in.readInt(), in.readInt());
    short numBlocks = in.readShort();
    localAddresses = new short[numBlocks];
    blockStates = new IBlockState[numBlocks];

    for (int i = 0; i < numBlocks; i++) {
        localAddresses[i] = in.readShort();
        blockStates[i] = Block.BLOCK_STATE_IDS.getByValue(readVarInt(in, 4));
    }
    int numHmapChanges = in.readUnsignedByte();
    heightValues = new int[numHmapChanges];
    for (int i = 0; i < numHmapChanges; i++) {
        heightValues[i] = in.readInt();
    }
}

From source file:cyril.server.io.AuthHandler.java

License:Open Source License

@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    if (!timeState.compareAndSet(0, DATA_MASK)) {
        // Other thread is reading (?!) or timeout has been reached in the
        // past./*  w w w.  j  a v a  2 s .c om*/
        return;
    }

    boolean goOn = true;
    while (goOn && in.readable() && ((timeState.get() & KILL_MASK) == 0)) {
        // The killmask is set when something goes wrong
        // That why we test for it with every cycle

        PaketPosition s = paketState;
        switch (s) {
        case START:
            goOn = handleIncomingStart(ctx, in);
            break;

        case CREATE_PENDING:
            // Client tried to send something while *we* were sending the
            // login data.
            // Client is trying to troll / spam us.
            readerAborts(ctx);
            goOn = false;
            break;

        case MUST_LOGIN:
            if (in.readByte() == '!') {
                paketState = PaketPosition.LOGIN;
            } else {
                readerAborts(ctx);
                goOn = false;
            }
            break;

        case LOGIN:
            // Awaiting the first two bytes
            if (in.readableBytes() < 2) {
                goOn = false;
            } else {
                int usernameLength = in.readShort();
                goOn = auth.isValidUsernameLength(wrapped, usernameLength);
                if (goOn) {
                    username = new byte[usernameLength];
                    index = 0;
                }
            }
            break;

        case LOGIN_USER:
            goOn = handleIncomingUsername(ctx, in);
            break;

        case LOGIN_TOKEN_LENGTH:
            goOn = handleIncomingTokenLength(ctx, in);
            break;

        case LOGIN_TOKEN:
            goOn = handleIncomingToken(ctx, in);
            break;

        case DECLINED_WAIT:
            goOn = handleIncomingDeclined(ctx, in);
            break;

        case AUTHENTICATED:
        default:
            throw new InternalError("Dafuq: Illegal / Unknown" + " PaketPosition " + s);
        }
    }

    if (paketState == PaketPosition.AUTHENTICATED) {
        timeState.set(0);
        return;
    }

    int oldState;
    do {
        oldState = timeState.get();
    } while (!timeState.compareAndSet(oldState, oldState & ~DATA_MASK));
    if ((oldState | KILL_MASK) != 0) {
        // Don't care about anything when KILL_MASK is set.
        readerAborts(ctx, true);
        return;
    }
}

From source file:cyril.server.io.AuthHandler.java

License:Open Source License

/**
 * Handles the length field of the login request
 * //from w  ww .  j  a v  a  2s  .c  om
 * @param ctx
 *            The current context
 * @param in
 *            The buffer to be read from (not guaranteed to have both bytes)
 * @return whether further processing may happen
 */
private boolean handleIncomingTokenLength(ChannelHandlerContext ctx, ByteBuf in) {
    // Awaiting the first two bytes
    if (in.readableBytes() < 2) {
        return false;
    }

    int incomingLength = in.readShort();
    if (incomingLength != login.token.length) {
        readerAborts(ctx);
        return false;
    }

    paketState = PaketPosition.LOGIN_TOKEN;
    index = 0;
    return true;
}

From source file:de.jackwhite20.cascade.shared.protocol.packet.Packet.java

License:Open Source License

public String readString(ByteBuf byteBuf) throws UnsupportedEncodingException {

    byte[] bytes = new byte[byteBuf.readShort()];
    byteBuf.readBytes(bytes);/*from   w  w w.  j  ava2  s .c o  m*/

    return new String(bytes, "utf-8");
}

From source file:de.sanandrew.mods.turretmod.registry.electrolytegen.ElectrolyteProcess.java

License:Creative Commons License

public ElectrolyteProcess(ByteBuf buf) {
    this.processStack = ByteBufUtils.readItemStack(buf);
    this.progress = buf.readShort();
    this.maxProgress = buf.readShort();

    ElectrolyteRegistry.Fuel fuel = ElectrolyteRegistry.getFuel(this.processStack);
    this.effectivenes = fuel.effect;
    this.trashStack = fuel.trash.copy();
    this.treasureStack = fuel.treasure.copy();
}

From source file:divconq.ctp.f.BlockCommand.java

License:Open Source License

@Override
public boolean decode(ByteBuf in) {
    while (this.state != State.DONE) {
        switch (this.state) {
        case BLOCK_TYPE: {
            if (in.readableBytes() < 1)
                return false;

            this.blocktype = in.readUnsignedByte();

            this.eof = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_EOF) != 0);
            this.skipHeaders = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_HEADER) == 0);
            this.skipPayload = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_CONTENT) == 0);

            // completely done, exit the loop and decode
            if (this.skipHeaders && this.skipPayload) {
                this.state = State.DONE;
                break;
            }//from  w  ww.j  a v a 2  s.c  om

            // to skip headers, go back to loop
            if (this.skipHeaders) {
                this.state = State.STREAM_OFFSET;
                break;
            }

            this.state = State.HEADER_ATTR;

            // deliberate fall through 
        }
        case HEADER_ATTR: {
            if (in.readableBytes() < 2)
                return false;

            this.currattr = in.readShort();

            // done with headers, go back to loop to skip down to payload
            if (this.currattr == CtpConstants.CTP_F_ATTR_END) {
                if (this.skipPayload)
                    this.state = State.DONE;
                else
                    this.state = State.STREAM_OFFSET;

                break;
            }

            this.state = State.HEADER_SIZE;

            // deliberate fall through 
        }
        case HEADER_SIZE: {
            if (in.readableBytes() < 2)
                return false;

            this.currasize = in.readShort();

            // an empty attribute is like a flag - present but no data
            // go on to next header
            if (this.currasize == 0) {
                this.headers.put(this.currattr, new byte[0]);
                this.currattr = 0;
                this.state = State.HEADER_ATTR;
                break;
            }

            this.state = State.HEADER_VALUE;

            // deliberate fall through 
        }
        case HEADER_VALUE: {
            if (in.readableBytes() < this.currasize)
                return false;

            byte[] val = new byte[this.currasize];

            in.readBytes(val);

            this.headers.put(this.currattr, val);

            this.currattr = 0;
            this.currasize = 0;

            this.state = State.HEADER_ATTR;

            break;
        }
        case STREAM_OFFSET: {
            if (in.readableBytes() < 8)
                return false;

            this.streamOffset = in.readLong();

            this.state = State.PAYLOAD_SIZE;

            // deliberate fall through 
        }
        case PAYLOAD_SIZE: {
            if (in.readableBytes() < 3)
                return false;

            this.paysize = in.readMedium();

            this.state = State.PAYLOAD;

            // deliberate fall through 
        }
        case PAYLOAD: {
            // return here, without any state reset, means we need more before we can decide what to do
            if (in.readableBytes() < this.paysize)
                return false;

            // add Data only if there are some bytes, otherwise skip buffer allocation
            if (this.paysize > 0) {
                ByteBuf bb = in.readSlice(this.paysize);
                bb.retain();
                this.data = bb;
            }

            this.state = State.DONE;

            // deliberate fall through 
        }
        case DONE: {
            break;
        }
        }
    }

    return true;
}

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();
    }// w w  w  . j a v a  2s.  co  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.matejkormuth.rpgdavid.starving.remote.netty.handlers.ByteBufToPacketHandler.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    short packetId = in.readShort();
    Packet p = packetFactory.createPacket(packetId);
    p.readFrom(in);/*from  w  ww  .j  a  va2  s  .c  om*/
    out.add(p);
}

From source file:eu.matejkormuth.rpgdavid.starving.remote.netty.handlers.ShortHeaderFrameDecoder.java

License:Open Source License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out)
        throws Exception {
    out.add(buf.readBytes(buf.readShort()));
}