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:com.farsunset.cim.sdk.server.filter.decoder.AppMessageDecoder.java

License:Apache License

@Override
public void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception {

    /**//from  w w  w. j a  v a2 s. co  m
     * ?3?
     */
    if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) {
        return;
    }

    buffer.markReaderIndex();

    byte conetnType = buffer.readByte();

    byte lv = buffer.readByte();// int ?
    byte hv = buffer.readByte();// int ?

    int conetnLength = getContentLength(lv, hv);

    // ?????
    if (conetnLength <= buffer.readableBytes()) {
        byte[] dataBytes = new byte[conetnLength];
        buffer.readBytes(dataBytes);

        Object message = mappingMessageObject(dataBytes, conetnType);
        if (message != null) {
            arg0.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).set(CIMSession.NATIVEAPP);
            queue.add(message);
            return;
        }
    }

    buffer.resetReaderIndex();
}

From source file:com.farsunset.cim.sdk.server.filter.decoder.WebMessageDecoder.java

License:Apache License

@Override
public void decode(ChannelHandlerContext arg0, ByteBuf iobuffer, List<Object> queue) throws Exception {

    iobuffer.markReaderIndex();//from w  ww . j a  va 2  s.c  o  m

    /**
     * ?fin??1 0 ??
     */
    byte tag = iobuffer.readByte();
    int frameFin = tag > 0 ? 0 : 1; // ?byte ?1 ?0 fin 0  1
    if (frameFin == 0) {
        iobuffer.resetReaderIndex();
        return;
    }

    /**
     * ?protobuf?? OPCODE_BINARY? OPCODE_CLOSE
     */
    int frameOqcode = tag & TAG_MASK;

    if (OPCODE_BINARY == frameOqcode) {

        byte head = iobuffer.readByte();
        byte datalength = (byte) (head & PAYLOADLEN);
        int realLength = 0;

        /**
         * Payload len7?7+16?7+64????? 0-125payload
         * data 1267????2payload data
         * 1277????8payload data
         */
        if (datalength == HAS_EXTEND_DATA) {
            realLength = iobuffer.readShort();
        } else if (datalength == HAS_EXTEND_DATA_CONTINUE) {
            realLength = (int) iobuffer.readLong();
        } else {
            realLength = datalength;
        }

        boolean masked = (head >> 7 & MASK) == 1;
        if (masked) {// ?
            // ??
            byte[] mask = new byte[4];
            iobuffer.readBytes(mask);

            byte[] data = new byte[realLength];
            iobuffer.readBytes(data);
            for (int i = 0; i < realLength; i++) {
                // ??
                data[i] = (byte) (data[i] ^ mask[i % 4]);
            }

            handleMessage(data, queue);
        }

    } else if (OPCODE_CLOSE == frameOqcode) {
        handleClose(arg0);
    } else {
        // ?
        iobuffer.readBytes(new byte[iobuffer.readableBytes()]);
    }

}

From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java

License:Apache License

/**
 * ??//from w w  w  .j  av  a2s  .  c  o  m
 */
public void channelRead2(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    try {
        while (in.isReadable()) {
            System.out.print((char) in.readByte());
            System.out.flush();
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java

License:Apache License

/**
 * ???//from  w  w w  .  j a v a 2s . co  m
 * @param ctx
 * @param msg
 * @throws Exception
 */
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    try {
        while (in.isReadable()) {
            char c = (char) in.readByte();
            content += c;
            if (c == '\n' || c == '\r') {
                if (!line.isEmpty()) {
                    System.out.println(line);
                }
                if ("quit".equals(line)) {
                    ByteBuf bf = ctx.alloc().buffer(content.getBytes().length);
                    bf.setBytes(0, content.getBytes());
                    final ChannelFuture f = ctx.writeAndFlush(bf); // (3)
                    f.addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws InterruptedException {
                            assert f == future;
                            ctx.close();
                        }
                    }); // (4)
                }
                line = "";
                continue;
            } else {
                line += c;
            }
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:com.flowpowered.engine.network.codec.ChunkDataCodec.java

License:MIT License

@Override
public ChunkDataMessage decode(ByteBuf buffer) throws IOException {
    final byte info = buffer.readByte();
    final boolean unload = (info & ISUNLOAD) == ISUNLOAD;
    final int x = buffer.readInt();
    final int y = buffer.readInt();
    final int z = buffer.readInt();
    if (unload) {
        return new ChunkDataMessage(x, y, z);
    } else {//from  w w  w .j a  v a 2 s .  c  o m
        int uncompressedSize = INTIAL_DATA_SIZE;
        final byte[] uncompressedData = new byte[uncompressedSize];
        final byte[] compressedData = new byte[buffer.readInt()];
        buffer.readBytes(compressedData);
        Inflater inflater = new Inflater();
        inflater.setInput(compressedData);
        try {
            inflater.inflate(uncompressedData);
        } catch (DataFormatException e) {
            throw new IOException("Error while reading chunk (" + x + "," + y + "," + z + ")!", e);
        }
        inflater.end();

        final int[] blocks = new int[Chunk.BLOCKS.VOLUME];

        int index = 0;
        for (int i = 0; i < blocks.length; ++i) {
            blocks[i] = uncompressedData[index++] | (uncompressedData[index++] << 8)
                    | (uncompressedData[index++] << 16) | (uncompressedData[index++] << 24);
        }

        if (index != uncompressedData.length) {
            throw new IllegalStateException(
                    "Incorrect parse size - actual:" + index + " expected: " + uncompressedData.length);
        }

        return new ChunkDataMessage(x, y, z, blocks);
    }
}

From source file:com.flowpowered.engine.network.codec.InputSnapshotCodec.java

License:MIT License

@Override
public InputSnapshotMessage decode(ByteBuf buf) throws IOException {
    List<KeyboardEvent> keyEvents = new LinkedList<>();
    int amount = buf.readInt();
    for (int i = 0; i < amount; i++) {
        keyEvents.add(new KeyboardEvent(buf.readByte(), buf.readBoolean()));
    }//from  www .jav  a 2 s. c  o m
    amount = buf.readInt();
    List<MouseEvent> mouseEvents = new LinkedList<>();
    for (int i = 0; i < amount; i++) {
        mouseEvents.add(new MouseEvent(buf.readInt(), buf.readInt(), buf.readInt(), buf.readInt(),
                buf.readInt(), buf.readInt(), buf.readBoolean()));
    }

    return new InputSnapshotMessage(buf.readFloat(), buf.readBoolean(), keyEvents, mouseEvents);
}

From source file:com.flowpowered.engine.network.codec.UpdateEntityCodec.java

License:MIT License

@Override
public UpdateEntityMessage decode(ByteBuf buffer) {
    final byte actionByte = buffer.readByte();
    if (actionByte < 0 || actionByte >= UpdateAction.values().length) {
        throw new IllegalArgumentException("Unknown response ID " + actionByte);
    }/*from   w w  w.jav  a2s  . c o m*/

    final UpdateAction action = UpdateAction.values()[actionByte];
    final int entityId = buffer.readInt();
    final Transform transform;
    switch (action) {
    case REMOVE:
        transform = null;
        break;
    case ADD:
    case TRANSFORM:
        transform = FlowByteBufUtils.readTransform(buffer);
        break;
    case POSITION:
        throw new UnsupportedOperationException("Position is unimplemented!");
    default:
        throw new IllegalArgumentException("Unknown UpdateAction!");
    }

    return new UpdateEntityMessage(entityId, transform, action, NullRepositionManager.INSTANCE);
}

From source file:com.flowpowered.engine.network.FlowProtocol.java

License:MIT License

@Override
public Codec<?> readHeader(ByteBuf buf) throws UnknownPacketException {
    byte opcode = -1;
    try {/*from  w  w w . j a va2  s.  c o  m*/
        opcode = buf.readByte();
        return getCodecLookupService().find(opcode);
    } catch (IllegalOpcodeException ex) {
        throw new UnknownPacketException("Unknown packet", opcode, -1);
    }
}

From source file:com.flowpowered.network.util.ByteBufUtils.java

License:MIT License

/**
 * Reads an integer written into the byte buffer as one of various bit sizes.
 *
 * @param buf The byte buffer to read from
 * @return The read integer/*  w w w  . j  ava  2s.c o m*/
 * @throws java.io.IOException If the reading fails
 */
public static int readVarInt(ByteBuf buf) throws IOException {
    int out = 0;
    int bytes = 0;
    byte in;
    while (true) {
        in = buf.readByte();
        out |= (in & 0x7F) << (bytes++ * 7);
        if (bytes > 5) {
            throw new IOException("Attempt to read int bigger than allowed for a varint!");
        }
        if ((in & 0x80) != 0x80) {
            break;
        }
    }
    return out;
}

From source file:com.flowpowered.network.util.ByteBufUtils.java

License:MIT License

/**
 * Reads an integer written into the byte buffer as one of various bit sizes.
 *
 * @param buf The byte buffer to read from
 * @return The read integer/*  w w  w . j  a  v  a2  s.co m*/
 * @throws java.io.IOException If the reading fails
 */
public static long readVarLong(ByteBuf buf) throws IOException {
    long out = 0;
    int bytes = 0;
    byte in;
    while (true) {
        in = buf.readByte();
        out |= (in & 0x7F) << (bytes++ * 7);
        if (bytes > 10) {
            throw new IOException("Attempt to read long bigger than allowed for a varlong!");
        }
        if ((in & 0x80) != 0x80) {
            break;
        }
    }
    return out;
}