From 2e59138428de8d58fd5ef2c05b1f5597f7c9d647 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 2 Aug 2018 00:43:38 -0400 Subject: [PATCH] Cleaned up native code. --- native/compile-linux.sh | 2 +- native/compile-osx.sh | 2 +- .../compression/JavaVelocityCompressor.java | 2 ++ .../compression/NativeZlibDeflate.java | 2 +- .../compression/NativeZlibInflate.java | 4 +-- .../compression/VelocityCompressor.java | 6 ++++ .../natives/util/NativeCodeLoader.java | 28 +++++++----------- .../resources/linux_x64/velocity-compress.so | Bin 13968 -> 13968 bytes .../resources/macosx/velocity-compress.dylib | Bin 14428 -> 10332 bytes .../compression/VelocityCompressorTest.java | 10 +++++-- .../proxy/connection/MinecraftConnection.java | 2 +- 11 files changed, 33 insertions(+), 25 deletions(-) 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 be9509931035f4b29515b26e02af503b246003c6..e9655d774218967d8c17c250f4b01333f3f3d412 100755 GIT binary patch delta 3262 zcmai1e{35?7GAII)^X@MiR<7%A=}6#A(Wim#Qmj1WD_?`Ld2y?axJ8#Nl61OEri4c zr2b(ew~_W-BS!;Lg%A{pKM(=|inybJ>xD~ID(;RS0@SLYqeTU_&{BkGaY@qUd%J5p zNkds_{dV7c^Sw83zL_1Tccyoyd)(17!MLJjBij#^Xg>2TZ}}SAbBjNk7FwR(y}#q_ zZDapf`PKNwsQuWo(X+OC(YBH=byTfBS!8yrH;v%43qpx-jUaeU$E%jV&+@-+`Rn-; zj;eLCWHxtMsF=;{EnBQ_TK-+4>5mtij-M?5k*vSObj0~3$L;G*TQMw<5VTkicr29F z@F6Va8n?1o3>;mx7OQMU>l#&^8(gstrhdlMNtQV4V#$SidywrNkCw5dm#JSe^$1J0 z4zR>w7gN9M-lU&O({uNxiGEM{9)q=Xo+^8J^L#g_gNL7D2jt3(!9 z($3T$h&8q74HNo`Q-}$A7F0nXlRn*Fn+>3JcD1Ne9ft-G?q4(%q? zaD{Y)mzkRB-lS}t$R?moP7&JIiU8^vrha=!Mu^b3<~3QKh2J3gN;^eJP=xeuu$Ux* z_O{tyFvc7{?l9omnrW9lbVdrhMr z>X(Xom?a+!YOC?8B&MdLP58aM>sFR>9-t6=&&8N0qEXc`!ENe!8o9Cwp^u{Zj2GQJ z<<_K71Bw!%%$)6JZ|eO0yUJe`%+UqRROn(yG7?zTqFzq!2n3nxMoMjJt3RSPBETnm z#@DKc8$3@RQMb$Qej{r3DLtlRYFZN+TeK#0E|Hm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