JNI native zlib compression 🔥

This commit is contained in:
Andrew Steinborn
2018-08-01 22:22:09 -04:00
parent 22dd4bbb99
commit fb4e6fd8f1
22 changed files with 610 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package com.velocitypowered.proxy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.network.ConnectionManager;
import com.velocitypowered.proxy.config.VelocityConfiguration;
import com.velocitypowered.proxy.connection.http.NettyHttpClient;
@@ -52,6 +53,8 @@ public class VelocityServer {
}
public void start() {
logger.info("Using {}", Natives.compressor.getLoadedVariant());
// Create a key pair
logger.info("Booting up Velocity...");
try {

View File

@@ -1,6 +1,8 @@
package com.velocitypowered.proxy.connection;
import com.google.common.base.Preconditions;
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.proxy.protocol.PacketWrapper;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.natives.compression.JavaVelocityCompressor;
@@ -189,7 +191,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
return;
}
JavaVelocityCompressor compressor = new JavaVelocityCompressor();
VelocityCompressor compressor = Natives.compressor.supply().get();
MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor);
MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor);

View File

@@ -31,6 +31,7 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder<ByteBuf> {
}
Preconditions.checkState(uncompressedSize >= threshold, "Uncompressed size %s doesn't make sense with threshold %s", uncompressedSize, threshold);
// Try to use the uncompressed size, but place a cap if it might be too big (possibly malicious).
ByteBuf uncompressed = ctx.alloc().buffer(Math.min(uncompressedSize, MAXIMUM_INITIAL_BUFFER_SIZE));
try {
compressor.inflate(msg, uncompressed);

View File

@@ -25,7 +25,8 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder<ByteBuf> {
return;
}
ByteBuf compressedBuffer = ctx.alloc().buffer();
// in other words, see if a plain 8KiB buffer fits us well
ByteBuf compressedBuffer = ctx.alloc().buffer(8192);
try {
int uncompressed = msg.readableBytes();
compressor.deflate(msg, compressedBuffer);