From 009c9afe09213d03b6cc1ec007bc62aa3673a487 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 16 Sep 2019 20:01:38 -0400 Subject: [PATCH] Decode multiple VarInt-prefixed packets If the remote server does flush consolidation, Velocity will be able to frame the packets all at once instead of having to constantly decode packets. This should provide a modest performance boost for them whilst not impacting un-optimized servers. --- .../netty/MinecraftVarintFrameDecoder.java | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 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 6a114075..01010fa8 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,36 +11,35 @@ 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 (packetLength == 0) { - return; + int lastReaderIndex = in.readerIndex(); + find_packets: while (in.isReadable()) { + for (int i = 0; i < 3; i++) { + if (!in.isReadable()) { + break; } - if (in.readableBytes() < packetLength) { - 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(lastReaderIndex); + int packetLength = ProtocolUtils.readVarInt(in); + if (packetLength == 0) { + break find_packets; + } - out.add(in.readRetainedSlice(packetLength)); - return; + if (in.readableBytes() < packetLength) { + break find_packets; + } + + out.add(in.readRetainedSlice(packetLength)); + lastReaderIndex = in.readerIndex(); + continue find_packets; + } } + + throw new CorruptedFrameException("VarInt too big"); } - throw new CorruptedFrameException("VarInt too big"); + in.readerIndex(lastReaderIndex); } }