diff --git a/native/compile-linux.sh b/native/compile-linux.sh index d3d7abab3..f0d72cc63 100755 --- a/native/compile-linux.sh +++ b/native/compile-linux.sh @@ -1,4 +1,4 @@ #!/bin/bash # Modify as you need. -gcc -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/ -shared -lz src/main/c/*.c -o src/main/resources/linux_x64/velocity-compress.so \ No newline at end of file +gcc -O3 -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/ -shared -lz src/main/c/*.c -o src/main/resources/linux_x64/velocity-compress.so \ No newline at end of file diff --git a/native/compile-osx.sh b/native/compile-osx.sh index d7a53149f..cca470c7c 100755 --- a/native/compile-osx.sh +++ b/native/compile-osx.sh @@ -2,4 +2,4 @@ # Modify as you need. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home -clang -I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin/ -shared -lz src/main/c/*.c -o src/main/resources/macosx/velocity-compress.dylib \ No newline at end of file +clang -O3 -I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin/ -shared -lz src/main/c/*.c -o src/main/resources/macosx/velocity-compress.dylib \ No newline at end of file diff --git a/native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java b/native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java index e66ca01ee..4186570c5 100644 --- a/native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java @@ -51,6 +51,8 @@ public class JavaVelocityCompressor implements VelocityCompressor { @Override public void dispose() { disposed = true; + deflater.end(); + inflater.end(); } private void ensureNotDisposed() { diff --git a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java index 7eb92c570..609f34cb4 100644 --- a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java @@ -20,5 +20,5 @@ class NativeZlibDeflate { initIDs(); } - static native void initIDs(); + private static native void initIDs(); } diff --git a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java index 310b4c3b9..d9d7a9fb9 100644 --- a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java @@ -3,7 +3,7 @@ package com.velocitypowered.natives.compression; /** * Represents a native interface for zlib's inflate functions. */ -public class NativeZlibInflate { +class NativeZlibInflate { boolean finished; int consumed; @@ -19,5 +19,5 @@ public class NativeZlibInflate { initIDs(); } - static native void initIDs(); + private static native void initIDs(); } diff --git a/native/src/main/java/com/velocitypowered/natives/compression/VelocityCompressor.java b/native/src/main/java/com/velocitypowered/natives/compression/VelocityCompressor.java index 18dce695d..de42e629e 100644 --- a/native/src/main/java/com/velocitypowered/natives/compression/VelocityCompressor.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/VelocityCompressor.java @@ -5,7 +5,13 @@ import io.netty.buffer.ByteBuf; import java.util.zip.DataFormatException; +/** + * Provides an interface to inflate and deflate {@link ByteBuf}s using zlib. + */ public interface VelocityCompressor extends Disposable { + /** + * The default preferred output buffer size for zlib. + */ int ZLIB_BUFFER_SIZE = 8192; void inflate(ByteBuf source, ByteBuf destination) throws DataFormatException; diff --git a/native/src/main/java/com/velocitypowered/natives/util/NativeCodeLoader.java b/native/src/main/java/com/velocitypowered/natives/util/NativeCodeLoader.java index 6119b9f42..10a281573 100644 --- a/native/src/main/java/com/velocitypowered/natives/util/NativeCodeLoader.java +++ b/native/src/main/java/com/velocitypowered/natives/util/NativeCodeLoader.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.function.BooleanSupplier; import java.util.function.Supplier; -public class NativeCodeLoader { +public class NativeCodeLoader implements Supplier { private final List> variants; private Variant selected; @@ -14,11 +14,12 @@ public class NativeCodeLoader { this.variants = ImmutableList.copyOf(variants); } - public Supplier supply() { + @Override + public T get() { if (selected == null) { selected = select(); } - return selected.supplier; + return selected.supplier.get(); } private Variant select() { @@ -33,14 +34,10 @@ public class NativeCodeLoader { } public String getLoadedVariant() { - for (Variant variant : variants) { - T got = variant.get(); - if (got == null) { - continue; - } - return variant.name; + if (selected == null) { + selected = select(); } - throw new IllegalArgumentException("Can't find any suitable variants"); + return selected.name; } static class Variant { @@ -57,17 +54,15 @@ public class NativeCodeLoader { this.supplier = supplier; } - public boolean setup() { + private void setup() { if (available && !hasBeenSetup) { try { setup.run(); hasBeenSetup = true; } catch (Exception e) { - //logger.error("Unable to set up {}", name, e); available = false; } } - return hasBeenSetup; } public T get() { @@ -83,10 +78,9 @@ public class NativeCodeLoader { } } - public static final BooleanSupplier MACOS = () -> System.getProperty("os.name").equalsIgnoreCase("Mac OS X") && + static final BooleanSupplier MACOS = () -> System.getProperty("os.name").equalsIgnoreCase("Mac OS X") && System.getProperty("os.arch").equals("x86_64"); - public static final BooleanSupplier LINUX = () -> System.getProperties().getProperty("os.name").equalsIgnoreCase("Linux") && + static final BooleanSupplier LINUX = () -> System.getProperties().getProperty("os.name").equalsIgnoreCase("Linux") && System.getProperty("os.arch").equals("amd64"); - public static final BooleanSupplier MAC_AND_LINUX = () -> MACOS.getAsBoolean() || LINUX.getAsBoolean(); - public static final BooleanSupplier ALWAYS = () -> true; + static final BooleanSupplier ALWAYS = () -> true; } diff --git a/native/src/main/resources/linux_x64/velocity-compress.so b/native/src/main/resources/linux_x64/velocity-compress.so index be9509931..e9655d774 100755 Binary files a/native/src/main/resources/linux_x64/velocity-compress.so and b/native/src/main/resources/linux_x64/velocity-compress.so differ diff --git a/native/src/main/resources/macosx/velocity-compress.dylib b/native/src/main/resources/macosx/velocity-compress.dylib index 1d45475d9..e5f342416 100755 Binary files a/native/src/main/resources/macosx/velocity-compress.dylib and b/native/src/main/resources/macosx/velocity-compress.dylib differ diff --git a/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java b/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java index 7273ecbd1..bd9b0f991 100644 --- a/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java +++ b/native/src/test/java/com/velocitypowered/natives/compression/VelocityCompressorTest.java @@ -4,6 +4,7 @@ import com.velocitypowered.natives.util.Natives; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; @@ -16,10 +17,15 @@ import static org.junit.jupiter.api.condition.OS.LINUX; import static org.junit.jupiter.api.condition.OS.MAC; class VelocityCompressorTest { + @BeforeAll + static void checkNatives() { + Natives.compressor.getLoadedVariant(); + } + @Test @EnabledOnOs({ MAC, LINUX }) void nativeIntegrityCheck() throws DataFormatException { - VelocityCompressor compressor = Natives.compressor.supply().get(); + VelocityCompressor compressor = Natives.compressor.get(); if (compressor instanceof JavaVelocityCompressor) { fail("Loaded regular compressor"); } @@ -38,7 +44,7 @@ class VelocityCompressorTest { ByteBuf decompressed = Unpooled.directBuffer(); Random random = new Random(1); - byte[] randomBytes = new byte[1 << 21]; + byte[] randomBytes = new byte[1 << 16]; random.nextBytes(randomBytes); source.writeBytes(randomBytes); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index 8f2430524..683f6d1a0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -191,7 +191,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { return; } - VelocityCompressor compressor = Natives.compressor.supply().get(); + VelocityCompressor compressor = Natives.compressor.get(); MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor); MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor);