Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

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

Usage

From source file:com.uber.tchannel.codecs.CodecUtils.java

License:Open Source License

public static ByteBuf readArg(ByteBuf buffer) {
    if (buffer.readableBytes() < TFrame.FRAME_SIZE_LENGTH) {
        return null;
    }/*  w  ww  . jav a 2 s .  c  o  m*/

    int len = buffer.readUnsignedShort();
    if (len > buffer.readableBytes()) {
        throw new UnsupportedOperationException("wrong read index for args");
    } else if (len == 0) {
        return Unpooled.EMPTY_BUFFER;
    }

    /* Read a slice, retain a copy */
    ByteBuf arg = buffer.readSlice(len);
    arg.retain();

    return arg;
}

From source file:com.uber.tchannel.codecs.TFrameCodec.java

License:Open Source License

public static TFrame decode(ByteBuf buffer) {
    // size:2/* w w w .  j  av  a  2 s. c o  m*/
    int size = buffer.readUnsignedShort() - TFrame.FRAME_HEADER_LENGTH;

    // type:1
    byte type = buffer.readByte();

    // reserved:1
    buffer.skipBytes(1);

    // id:4
    long id = buffer.readUnsignedInt();

    // reserved:8
    buffer.skipBytes(8);

    // payload:16+
    ByteBuf payload = buffer.readSlice(size);
    //        payload.retain();

    return new TFrame(size, type, id, payload);
}

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   w w  w.j a  v a2  s  . 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.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  w w  .  ja va  2  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.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 .  jav  a2  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 int readSh(ByteBuf buff) {
    return buff.readUnsignedShort();
}

From source file:de.gandev.modjn.entity.func.request.WriteMultipleRegistersRequest.java

License:Apache License

@Override
public void decode(ByteBuf data) {
    super.decode(data);

    byteCount = data.readUnsignedByte();

    registers = new int[byteCount / 2];
    for (int i = 0; i < registers.length; i++) {
        registers[i] = data.readUnsignedShort();
    }/*from   w  w w .ja v a  2  s .  c o m*/
}

From source file:de.gandev.modjn.entity.func.response.ReadHoldingRegistersResponse.java

License:Apache License

@Override
public void decode(ByteBuf data) {
    byteCount = data.readUnsignedByte();

    registers = new int[byteCount / 2];
    for (int i = 0; i < registers.length; i++) {
        registers[i] = data.readUnsignedShort();
    }/*from  ww  w .  j  a  v a  2  s  . co  m*/
}

From source file:de.gandev.modjn.entity.func.response.ReadInputRegistersResponse.java

License:Apache License

@Override
public void decode(ByteBuf data) {
    byteCount = data.readUnsignedByte();

    inputRegisters = new int[byteCount / 2];
    for (int i = 0; i < inputRegisters.length; i++) {
        inputRegisters[i] = data.readUnsignedShort();
    }/* ww  w  . java  2 s .  c o m*/
}

From source file:de.jackwhite20.comix.handler.HandshakeHandler.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out)
        throws Exception {
    ByteBuf copy = byteBuf.copy();//from ww w  . j  a  v  a  2 s .c o  m

    Protocol.readVarInt(byteBuf);

    int packetId = Protocol.readVarInt(byteBuf);

    if (packetId == 0) {
        if (protocolMode == ProtocolState.HANDSHAKE) {
            Protocol.readVarInt(byteBuf);
            Protocol.readString(byteBuf);
            byteBuf.readUnsignedShort();
            protocolMode = ProtocolState.valueOf((byte) Protocol.readVarInt(byteBuf));
        }

        if (protocolMode == ProtocolState.STATUS) {
            ByteBuf responseBuffer = Unpooled.buffer();
            String response = Comix.getInstance().getStatusResponseString();
            response = response.replace("%online%", "" + Comix.getInstance().getClientsOnline()); // Replace online count
            Protocol.writeVarInt(3 + response.length(), responseBuffer); // Size
            Protocol.writeVarInt(0, responseBuffer); // Packet id
            Protocol.writeString(response, responseBuffer); // Data as json string
            channelHandlerContext.writeAndFlush(responseBuffer);

            // Sending Pong instant because otherwise the pong will not receive properly!
            ByteBuf pongBuffer = Unpooled.buffer();
            Protocol.writeVarInt(9, pongBuffer);
            Protocol.writeVarInt(1, pongBuffer);
            pongBuffer.writeLong(0);
            channelHandlerContext.writeAndFlush(pongBuffer);

            channelHandlerContext.close();
        }

        if (protocolMode == ProtocolState.LOGIN) {
            if (byteBuf.readableBytes() == 0) {
                upstreamHandler.connectDownstream(copy);

                downstreamInitialized = true;

                out.add(copy.retain());

                return;
            }

            String name = Protocol.readString(byteBuf);

            //TODO: Improve
            if (Comix.getInstance().getComixConfig().isMaintenance()) {
                if (Comix.getInstance().isWhitelistEnabled()) {
                    if (!Comix.getInstance().isWhitelisted(name)) {
                        kick(channelHandlerContext, Comix.getInstance().getWhitelistKickMessage());
                    }
                } else {
                    kick(channelHandlerContext,
                            Comix.getInstance().getComixConfig().getMaintenanceKickMessage());
                }

                channelHandlerContext.close();
                return;
            } else if (Comix.getInstance().isWhitelistEnabled() && !Comix.getInstance().isWhitelisted(name)) {
                kick(channelHandlerContext, Comix.getInstance().getWhitelistKickMessage());

                channelHandlerContext.close();
                return;
            }

            if (!downstreamInitialized)
                upstreamHandler.connectDownstream(copy);
            else
                upstreamHandler.addInitialPacket(copy);

            ComixClient comixClient = new ComixClient(name, upstreamHandler.getDownstreamHandler(),
                    upstreamHandler);
            Comix.getInstance().addClient(comixClient);
            upstreamHandler.setClient(comixClient);

            channelHandlerContext.channel().pipeline().remove(this);

            Comix.getLogger().log(Level.INFO, "Handshake", "Player logged in: " + name);

            out.add(copy.retain());
        }
    }
}