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

This did not work :(

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-05-31 07:55:19 -04:00
Ursprung 869f2a6b32
Commit 3a8ba76f25

Datei anzeigen

@ -1,16 +1,15 @@
package com.velocitypowered.proxy.protocol.netty; package com.velocitypowered.proxy.protocol.netty;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.util.except.QuietException; import com.velocitypowered.proxy.util.except.QuietException;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.ByteProcessor;
import java.util.List; import java.util.List;
public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder { public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
private static final QuietException BAD_LENGTH_CACHED = new QuietException("Bad packet length"); private static final QuietException BAD_LENGTH_CACHED = new QuietException("Bad packet length");
private final VarintByteDecoder reader = new VarintByteDecoder();
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
@ -19,58 +18,31 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
return; return;
} }
while (in.isReadable()) { read_lens: while (in.isReadable()) {
int varintEnd = in.forEachByte(reader); int origReaderIndex = in.readerIndex();
if (varintEnd == -1) { for (int i = 0; i < 3; i++) {
if (!in.isReadable()) {
in.readerIndex(origReaderIndex);
return; return;
} }
if (reader.successfulDecode) { byte read = in.readByte();
if (reader.readVarint < 0) { if (read >= 0) {
throw BAD_LENGTH_CACHED; // Make sure reader index of length buffer is returned to the beginning
} in.readerIndex(origReaderIndex);
int packetLength = ProtocolUtils.readVarInt(in);
int minimumRead = reader.bytesRead + reader.readVarint; if (in.readableBytes() >= packetLength) {
if (in.isReadable(minimumRead)) { out.add(in.readBytes(packetLength));
out.add(in.retainedSlice(varintEnd + 1, reader.readVarint)); continue read_lens;
in.skipBytes(minimumRead);
reader.reset();
} else { } else {
reader.reset(); in.readerIndex(origReaderIndex);
return; return;
} }
} else { }
boolean tooBig = reader.bytesRead > 3; }
reader.reset();
if (tooBig) {
throw BAD_LENGTH_CACHED; throw BAD_LENGTH_CACHED;
} }
} }
} }
}
private static class VarintByteDecoder implements ByteProcessor {
private int readVarint;
private int bytesRead;
private boolean successfulDecode;
@Override
public boolean process(byte k) {
readVarint |= (k & 0x7F) << bytesRead++ * 7;
if (bytesRead > 3) {
return false;
}
if ((k & 0x80) != 128) {
successfulDecode = true;
return false;
}
return true;
}
void reset() {
readVarint = 0;
bytesRead = 0;
successfulDecode = false;
}
}
}