3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Cleaner and more correct to use ByteBufs directly.

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-01-11 18:32:03 -05:00
Ursprung 1e041963f0
Commit 1a2b162353

Datei anzeigen

@ -11,27 +11,25 @@ import java.util.List;
public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
private final byte[] lenBuf = new byte[3];
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (!in.isReadable()) { if (!in.isReadable()) {
return; return;
} }
Arrays.fill(lenBuf, (byte) 0); ByteBuf lenBuf = ctx.alloc().buffer(3);
int origReaderIndex = in.readerIndex(); int origReaderIndex = in.readerIndex();
try {
ByteBuf wrappedBuf = Unpooled.wrappedBuffer(lenBuf); for (int i = 0; i < 3; i++) {
for (int i = 0; i < lenBuf.length; i++) {
if (!in.isReadable()) { if (!in.isReadable()) {
in.readerIndex(origReaderIndex); in.readerIndex(origReaderIndex);
return; return;
} }
lenBuf[i] = in.readByte(); byte read = in.readByte();
if (lenBuf[i] > 0) { lenBuf.writeByte(read);
int packetLength = ProtocolUtils.readVarInt(wrappedBuf); if (read > 0) {
int packetLength = ProtocolUtils.readVarInt(lenBuf.slice());
if (packetLength == 0) { if (packetLength == 0) {
return; return;
} }
@ -47,5 +45,8 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
} }
throw new CorruptedFrameException("VarInt too big"); throw new CorruptedFrameException("VarInt too big");
} finally {
lenBuf.release();
}
} }
} }