geforkt von Mirrors/Velocity
Try two, with improved state maintenance and some bugs fixed
Dieser Commit ist enthalten in:
Ursprung
3a8ba76f25
Commit
c856635ac5
@ -1,15 +1,17 @@
|
|||||||
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 static final QuietException VARINT_BIG_CACHED = new QuietException("VarInt too big");
|
||||||
|
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) {
|
||||||
@ -18,31 +20,65 @@ public class MinecraftVarintFrameDecoder extends ByteToMessageDecoder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
read_lens: while (in.isReadable()) {
|
while (in.isReadable()) {
|
||||||
int origReaderIndex = in.readerIndex();
|
reader.reset();
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
if (!in.isReadable()) {
|
|
||||||
in.readerIndex(origReaderIndex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
byte read = in.readByte();
|
int varintEnd = in.forEachByte(reader);
|
||||||
if (read >= 0) {
|
if (varintEnd == -1) {
|
||||||
// Make sure reader index of length buffer is returned to the beginning
|
// We tried to go beyond the end of the buffer. This is probably a good sign that the
|
||||||
in.readerIndex(origReaderIndex);
|
// buffer was too short to hold a proper varint.
|
||||||
int packetLength = ProtocolUtils.readVarInt(in);
|
return;
|
||||||
|
|
||||||
if (in.readableBytes() >= packetLength) {
|
|
||||||
out.add(in.readBytes(packetLength));
|
|
||||||
continue read_lens;
|
|
||||||
} else {
|
|
||||||
in.readerIndex(origReaderIndex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw BAD_LENGTH_CACHED;
|
if (reader.result == DecodeResult.SUCCESS) {
|
||||||
|
if (reader.readVarint < 0) {
|
||||||
|
throw BAD_LENGTH_CACHED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int minimumRead = reader.bytesRead + reader.readVarint;
|
||||||
|
if (in.isReadable(minimumRead)) {
|
||||||
|
out.add(in.retainedSlice(varintEnd + 1, reader.readVarint));
|
||||||
|
in.skipBytes(minimumRead);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (reader.result == DecodeResult.TOO_BIG) {
|
||||||
|
throw VARINT_BIG_CACHED;
|
||||||
|
} else if (reader.result == DecodeResult.TOO_SHORT) {
|
||||||
|
// No-op: we couldn't get a useful result.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class VarintByteDecoder implements ByteProcessor {
|
||||||
|
private int readVarint;
|
||||||
|
private int bytesRead;
|
||||||
|
private DecodeResult result = DecodeResult.TOO_SHORT;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean process(byte k) {
|
||||||
|
readVarint |= (k & 0x7F) << bytesRead++ * 7;
|
||||||
|
if (bytesRead > 3) {
|
||||||
|
result = DecodeResult.TOO_BIG;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((k & 0x80) != 128) {
|
||||||
|
result = DecodeResult.SUCCESS;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
readVarint = 0;
|
||||||
|
bytesRead = 0;
|
||||||
|
result = DecodeResult.TOO_SHORT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum DecodeResult {
|
||||||
|
SUCCESS,
|
||||||
|
TOO_SHORT,
|
||||||
|
TOO_BIG
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren