From ba8629ca0eb80b2dbd06f9a7e3ec3f65db43e98a Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 15 Nov 2019 00:39:11 -0500 Subject: [PATCH] Try to decode multiple packets at once --- .../netty/MinecraftVarintFrameDecoder.java | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java index dd1fa5aa1..b003fc48c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintFrameDecoder.java @@ -11,33 +11,31 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - if (!in.isReadable()) { - return; - } - - 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.readRetainedSlice(packetLength)); - } else { + read_lens: while (in.isReadable()) { + int origReaderIndex = in.readerIndex(); + for (int i = 0; i < 3; i++) { + if (!in.isReadable()) { in.readerIndex(origReaderIndex); + return; } - 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); - throw new CorruptedFrameException("VarInt too big"); + if (in.readableBytes() >= packetLength) { + out.add(in.readRetainedSlice(packetLength)); + continue read_lens; + } else { + in.readerIndex(origReaderIndex); + return; + } + } + } + + throw new CorruptedFrameException("VarInt too big"); + } } }