Example usage for io.netty.buffer ByteBuf discardReadBytes

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

Introduction

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

Prototype

public abstract ByteBuf discardReadBytes();

Source Link

Document

Discards the bytes between the 0th index and readerIndex .

Usage

From source file:org.virtue.network.protocol.event.GameEventDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    if (buf.isReadable()) {
        int opcode = buf.readByte() - cipher.nextInt() & 0xFF;
        if (opcode < 0) {
            buf.discardReadBytes();
            return;
        }//from www  . ja v a  2 s  .c  om
        int length = Constants.PACKET_SIZES[opcode];
        if (length < 0) {
            switch (length) {
            case -1:
                if (buf.isReadable()) {
                    length = buf.readByte() & 0xff;
                }
                break;
            case -2:
                if (buf.readableBytes() >= 2) {
                    length = buf.readShort() & 0xffff;
                }
                break;
            default:
                length = buf.readableBytes();
                break;
            }
        }
        if (buf.readableBytes() >= length) {
            if (length < 0) {
                return;
            }
            byte[] payload = new byte[length];
            buf.readBytes(payload, 0, length);
            out.add(new GameEventMessage(opcode, new InboundBuffer(payload)));
        }
    }
}