Return the magic-3 loop again... :(

This commit is contained in:
Andrew Steinborn
2020-01-01 10:14:15 -05:00
parent a15c8ecc2b
commit c14ceb315d
2 changed files with 24 additions and 9 deletions

View File

@@ -30,7 +30,6 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder<ByteBuf> {
if (claimedUncompressedSize == 0) {
// Strip the now-useless uncompressed size, this message is already uncompressed.
out.add(in.retainedSlice());
in.skipBytes(in.readableBytes());
return;
}

View File

@@ -4,22 +4,38 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.CorruptedFrameException;
import java.util.List;
public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
while (in.isReadable()) {
int ri = in.readerIndex();
read_lens: while (in.isReadable()) {
int origReaderIndex = in.readerIndex();
for (int i = 0; i < 3; i++) {
if (!in.isReadable()) {
in.readerIndex(origReaderIndex);
return;
}
byte read = in.readByte();
if (read >= 0) {
// Make sure reader index of length buffer is returned to the beginning
in.readerIndex(origReaderIndex);
int packetLength = ProtocolUtils.readVarInt(in);
if (in.readableBytes() >= packetLength) {
out.add(in.readBytes(packetLength));
continue read_lens;
} else {
in.readerIndex(ri);
break;
in.readerIndex(origReaderIndex);
return;
}
}
}
throw new CorruptedFrameException("VarInt too big");
}
}
}