Example usage for io.netty.buffer ByteBuf discardSomeReadBytes

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

Introduction

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

Prototype

public abstract ByteBuf discardSomeReadBytes();

Source Link

Document

Similar to ByteBuf#discardReadBytes() except that this method might discard some, all, or none of read bytes depending on its internal implementation to reduce overall memory bandwidth consumption at the cost of potentially additional memory consumption.

Usage

From source file:me.bigteddy98.mcproxy.protocol.NetworkManager.java

License:Open Source License

public synchronized List<Packet> handleServerBoundPackets(ByteBuf originalBuffer, ByteBuf bufferClone)
        throws InstantiationException, IllegalAccessException {
    List<Packet> list = new ArrayList<>();
    if (bufferClone.readableBytes() == 0) {
        return list;
    }//from w  w w  .  ja  v a 2 s . c  o  m
    PacketDataWrapper wrapper = new PacketDataWrapper(bufferClone);
    while (bufferClone.readableBytes() > 0) {
        bufferClone.markReaderIndex();
        int readBytes = bufferClone.readableBytes();
        int length = 0;
        {
            int bytes = 0;
            byte in;
            while (true) {
                if (readBytes < 1) {
                    bufferClone.resetReaderIndex();
                    return list;
                }
                in = bufferClone.readByte();
                length |= (in & 0x7F) << (bytes++ * 7);
                if (bytes > 5) {
                    throw new RuntimeException("VarInt too big");
                }
                if ((in & 0x80) != 0x80) {
                    break;
                }
            }
        }
        if (bufferClone.readableBytes() < length) {
            bufferClone.resetReaderIndex();
            return list;
        }
        int id = wrapper.readVarInt();
        Class<? extends Packet> clazz = PacketRegistry.getServerBoundPacket(id, this.currentState);
        if (clazz == null) {
            return list;
        }
        Packet packet = clazz.newInstance();
        packet.read(wrapper);
        packet.onReceive(this, new PacketReceiveEvent());
        list.add(packet);
        ProxyLogger.debug("Handled " + packet.toString());
        bufferClone.discardSomeReadBytes();
    }
    return list;
}

From source file:me.bigteddy98.mcproxy.protocol.NetworkManager.java

License:Open Source License

public synchronized List<Packet> handleClientBoundPackets(ByteBuf originalBuffer, ByteBuf bufferClone)
        throws InstantiationException, IllegalAccessException {
    List<Packet> list = new ArrayList<>();
    if (bufferClone.readableBytes() == 0) {
        return list;
    }/*from   ww w. j a  v a 2  s  . c  o  m*/
    PacketDataWrapper wrapper = new PacketDataWrapper(bufferClone);
    while (bufferClone.readableBytes() > 0) {
        bufferClone.markReaderIndex();
        int readBytes = bufferClone.readableBytes();
        int length = 0;
        {
            int bytes = 0;
            byte in;
            while (true) {
                if (readBytes < 1) {
                    bufferClone.resetReaderIndex();
                    return list;
                }
                in = bufferClone.readByte();
                length |= (in & 0x7F) << (bytes++ * 7);
                if (bytes > 5) {
                    throw new RuntimeException("VarInt too big");
                }
                if ((in & 0x80) != 0x80) {
                    break;
                }
            }
        }
        if (bufferClone.readableBytes() < length) {
            bufferClone.resetReaderIndex();
            return list;
        }
        int id = wrapper.readVarInt();
        Class<? extends Packet> clazz = PacketRegistry.getClientBoundPacket(id, this.currentState);
        if (clazz == null) {
            return list;
        }
        Packet packet = clazz.newInstance();
        packet.read(wrapper);
        packet.onReceive(this, new PacketReceiveEvent());
        list.add(packet);
        ProxyLogger.debug("Handled " + packet.toString());
        bufferClone.discardSomeReadBytes();
    }
    return list;
}

From source file:me.bigteddy98.slimeportal.protocol.NetworkManager.java

License:Open Source License

public synchronized List<Packet> handleServerBoundPackets(ByteBuf bufferClone)
        throws InstantiationException, IllegalAccessException {
    List<Packet> list = new ArrayList<>();
    if (bufferClone.readableBytes() == 0) {
        return list;
    }//from w ww .  j ava  2s.  com
    PacketDataWrapper wrapper = new PacketDataWrapper(bufferClone);
    while (bufferClone.readableBytes() > 0) {
        bufferClone.markReaderIndex();
        int readBytes = bufferClone.readableBytes();
        int length = 0;
        {
            int bytes = 0;
            byte in;
            while (true) {
                if (readBytes < 1) {
                    bufferClone.resetReaderIndex();
                    return list;
                }
                in = bufferClone.readByte();
                length |= (in & 0x7F) << (bytes++ * 7);
                if (bytes > 5) {
                    throw new RuntimeException("VarInt too big");
                }
                if ((in & 0x80) != 0x80) {
                    break;
                }
            }
        }
        if (bufferClone.readableBytes() < length) {
            bufferClone.resetReaderIndex();
            return list;
        }
        int id = wrapper.readVarInt();
        Class<? extends Packet> clazz = PacketRegistry.getServerBoundPacket(id, this.currentState);
        if (clazz == null) {
            return list;
        }
        Packet packet = clazz.newInstance();
        packet.read(wrapper);
        if (packet instanceof PacketInHandShake) {
            packet.onReceive(this, new PacketReceiveEvent());
        }
        list.add(packet);
        bufferClone.discardSomeReadBytes();
    }
    return list;
}

From source file:me.bigteddy98.slimeportal.protocol.NetworkManager.java

License:Open Source License

public synchronized List<Packet> handleClientBoundPackets(ByteBuf bufferClone)
        throws InstantiationException, IllegalAccessException {
    List<Packet> list = new ArrayList<>();
    if (bufferClone.readableBytes() == 0) {
        return list;
    }// ww  w.j  av  a 2  s  . c o  m
    PacketDataWrapper wrapper = new PacketDataWrapper(bufferClone);
    while (bufferClone.readableBytes() > 0) {
        bufferClone.markReaderIndex();
        int readBytes = bufferClone.readableBytes();
        int length = 0;
        {
            int bytes = 0;
            byte in;
            while (true) {
                if (readBytes < 1) {
                    bufferClone.resetReaderIndex();
                    return list;
                }
                in = bufferClone.readByte();
                length |= (in & 0x7F) << (bytes++ * 7);
                if (bytes > 5) {
                    throw new RuntimeException("VarInt too big");
                }
                if ((in & 0x80) != 0x80) {
                    break;
                }
            }
        }
        if (bufferClone.readableBytes() < length) {
            bufferClone.resetReaderIndex();
            return list;
        }
        int id = wrapper.readVarInt();
        Class<? extends Packet> clazz = PacketRegistry.getClientBoundPacket(id, this.currentState);
        if (clazz == null) {
            return list;
        }
        Packet packet = clazz.newInstance();
        packet.read(wrapper);
        if (packet instanceof PacketInHandShake) {
            packet.onReceive(this, new PacketReceiveEvent());
        }
        list.add(packet);
        bufferClone.discardSomeReadBytes();
    }
    return list;
}