Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-12-25 15:50:19 +01:00
Modify MinecraftCompressorAndLengthEncoder
to handle 8MiB outgoing packets like vanilla supports now
Dieser Commit ist enthalten in:
Ursprung
0b07456c89
Commit
7e932eaad4
@ -163,16 +163,17 @@ public enum ProtocolUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the specified {@code value} as a 21-bit Minecraft VarInt to the specified {@code buf}.
|
* Writes the specified {@code value} as a 28-bit Minecraft VarInt to the specified {@code buf}.
|
||||||
* The upper 11 bits will be discarded.
|
* The upper 4 bits will be discarded.
|
||||||
*
|
*
|
||||||
* @param buf the buffer to read from
|
* @param buf the buffer to read from
|
||||||
* @param value the integer to write
|
* @param value the integer to write
|
||||||
*/
|
*/
|
||||||
public static void write21BitVarInt(ByteBuf buf, int value) {
|
public static void write28BitVarInt(ByteBuf buf, int value) {
|
||||||
// See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
|
// See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
|
||||||
int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
|
int w = (value & 0x7F | 0x80) << 24 | (((value >>> 7) & 0x7F | 0x80) << 16)
|
||||||
buf.writeMedium(w);
|
| ((value >>> 14) & 0x7F | 0x80) << 8 | (value >>> 21);
|
||||||
|
buf.writeInt(w);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String readString(ByteBuf buf) {
|
public static String readString(ByteBuf buf) {
|
||||||
|
@ -57,7 +57,7 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder<By
|
|||||||
throws DataFormatException {
|
throws DataFormatException {
|
||||||
int uncompressed = msg.readableBytes();
|
int uncompressed = msg.readableBytes();
|
||||||
|
|
||||||
ProtocolUtils.write21BitVarInt(out, 0); // Dummy packet length
|
ProtocolUtils.write28BitVarInt(out, 0); // Dummy packet length
|
||||||
ProtocolUtils.writeVarInt(out, uncompressed);
|
ProtocolUtils.writeVarInt(out, uncompressed);
|
||||||
ByteBuf compatibleIn = MoreByteBufUtils.ensureCompatible(ctx.alloc(), compressor, msg);
|
ByteBuf compatibleIn = MoreByteBufUtils.ensureCompatible(ctx.alloc(), compressor, msg);
|
||||||
|
|
||||||
@ -68,14 +68,14 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder<By
|
|||||||
compatibleIn.release();
|
compatibleIn.release();
|
||||||
}
|
}
|
||||||
int compressedLength = out.writerIndex() - startCompressed;
|
int compressedLength = out.writerIndex() - startCompressed;
|
||||||
if (compressedLength >= 1 << 21) {
|
if (compressedLength >= 1 << 23) {
|
||||||
throw new DataFormatException("The server sent a very large (over 2MiB compressed) packet.");
|
throw new DataFormatException("The server sent a very large (over 8MiB compressed) packet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
int writerIndex = out.writerIndex();
|
int writerIndex = out.writerIndex();
|
||||||
int packetLength = out.readableBytes() - 3;
|
int packetLength = out.readableBytes() - 3;
|
||||||
out.writerIndex(0);
|
out.writerIndex(0);
|
||||||
ProtocolUtils.write21BitVarInt(out, packetLength); // Rewrite packet length
|
ProtocolUtils.write28BitVarInt(out, packetLength); // Rewrite packet length
|
||||||
out.writerIndex(writerIndex);
|
out.writerIndex(writerIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +92,7 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder<By
|
|||||||
}
|
}
|
||||||
|
|
||||||
// (maximum data length after compression) + packet length varint + uncompressed data varint
|
// (maximum data length after compression) + packet length varint + uncompressed data varint
|
||||||
int initialBufferSize = (uncompressed - 1) + 3 + ProtocolUtils.varIntBytes(uncompressed);
|
int initialBufferSize = (uncompressed - 1) + 4 + ProtocolUtils.varIntBytes(uncompressed);
|
||||||
return MoreByteBufUtils.preferredBuffer(ctx.alloc(), compressor, initialBufferSize);
|
return MoreByteBufUtils.preferredBuffer(ctx.alloc(), compressor, initialBufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,16 +74,16 @@ public class ProtocolUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test3Bytes() {
|
void test4Bytes() {
|
||||||
ByteBuf buf = Unpooled.buffer(5);
|
ByteBuf buf = Unpooled.buffer(5);
|
||||||
for (int i = 0; i < 2097152; i += 31) {
|
for (int i = 0; i < (1 << 28) - 1; i += 31) {
|
||||||
writeReadTest3Bytes(buf, i);
|
writeReadTest4Bytes(buf, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeReadTest3Bytes(ByteBuf buf, int test) {
|
private void writeReadTest4Bytes(ByteBuf buf, int test) {
|
||||||
buf.clear();
|
buf.clear();
|
||||||
ProtocolUtils.write21BitVarInt(buf, test);
|
ProtocolUtils.write28BitVarInt(buf, test);
|
||||||
assertEquals(test, ProtocolUtils.readVarInt(buf));
|
assertEquals(test, ProtocolUtils.readVarInt(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren