13
0
geforkt von Mirrors/Velocity

Cleaned up native code.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-08-02 00:43:38 -04:00
Ursprung 850b2f7958
Commit 2e59138428
11 geänderte Dateien mit 33 neuen und 25 gelöschten Zeilen

Datei anzeigen

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
# Modify as you need. # 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 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

Datei anzeigen

@ -2,4 +2,4 @@
# Modify as you need. # Modify as you need.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home 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 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

Datei anzeigen

@ -51,6 +51,8 @@ public class JavaVelocityCompressor implements VelocityCompressor {
@Override @Override
public void dispose() { public void dispose() {
disposed = true; disposed = true;
deflater.end();
inflater.end();
} }
private void ensureNotDisposed() { private void ensureNotDisposed() {

Datei anzeigen

@ -20,5 +20,5 @@ class NativeZlibDeflate {
initIDs(); initIDs();
} }
static native void initIDs(); private static native void initIDs();
} }

Datei anzeigen

@ -3,7 +3,7 @@ package com.velocitypowered.natives.compression;
/** /**
* Represents a native interface for zlib's inflate functions. * Represents a native interface for zlib's inflate functions.
*/ */
public class NativeZlibInflate { class NativeZlibInflate {
boolean finished; boolean finished;
int consumed; int consumed;
@ -19,5 +19,5 @@ public class NativeZlibInflate {
initIDs(); initIDs();
} }
static native void initIDs(); private static native void initIDs();
} }

Datei anzeigen

@ -5,7 +5,13 @@ import io.netty.buffer.ByteBuf;
import java.util.zip.DataFormatException; import java.util.zip.DataFormatException;
/**
* Provides an interface to inflate and deflate {@link ByteBuf}s using zlib.
*/
public interface VelocityCompressor extends Disposable { public interface VelocityCompressor extends Disposable {
/**
* The default preferred output buffer size for zlib.
*/
int ZLIB_BUFFER_SIZE = 8192; int ZLIB_BUFFER_SIZE = 8192;
void inflate(ByteBuf source, ByteBuf destination) throws DataFormatException; void inflate(ByteBuf source, ByteBuf destination) throws DataFormatException;

Datei anzeigen

@ -6,7 +6,7 @@ import java.util.List;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Supplier; import java.util.function.Supplier;
public class NativeCodeLoader<T> { public class NativeCodeLoader<T> implements Supplier<T> {
private final List<Variant<T>> variants; private final List<Variant<T>> variants;
private Variant<T> selected; private Variant<T> selected;
@ -14,11 +14,12 @@ public class NativeCodeLoader<T> {
this.variants = ImmutableList.copyOf(variants); this.variants = ImmutableList.copyOf(variants);
} }
public Supplier<T> supply() { @Override
public T get() {
if (selected == null) { if (selected == null) {
selected = select(); selected = select();
} }
return selected.supplier; return selected.supplier.get();
} }
private Variant<T> select() { private Variant<T> select() {
@ -33,14 +34,10 @@ public class NativeCodeLoader<T> {
} }
public String getLoadedVariant() { public String getLoadedVariant() {
for (Variant<T> variant : variants) { if (selected == null) {
T got = variant.get(); selected = select();
if (got == null) {
continue;
} }
return variant.name; return selected.name;
}
throw new IllegalArgumentException("Can't find any suitable variants");
} }
static class Variant<T> { static class Variant<T> {
@ -57,17 +54,15 @@ public class NativeCodeLoader<T> {
this.supplier = supplier; this.supplier = supplier;
} }
public boolean setup() { private void setup() {
if (available && !hasBeenSetup) { if (available && !hasBeenSetup) {
try { try {
setup.run(); setup.run();
hasBeenSetup = true; hasBeenSetup = true;
} catch (Exception e) { } catch (Exception e) {
//logger.error("Unable to set up {}", name, e);
available = false; available = false;
} }
} }
return hasBeenSetup;
} }
public T get() { public T get() {
@ -83,10 +78,9 @@ public class NativeCodeLoader<T> {
} }
} }
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"); 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"); System.getProperty("os.arch").equals("amd64");
public static final BooleanSupplier MAC_AND_LINUX = () -> MACOS.getAsBoolean() || LINUX.getAsBoolean(); static final BooleanSupplier ALWAYS = () -> true;
public static final BooleanSupplier ALWAYS = () -> true;
} }

Datei anzeigen

@ -4,6 +4,7 @@ import com.velocitypowered.natives.util.Natives;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil; import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs; 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; import static org.junit.jupiter.api.condition.OS.MAC;
class VelocityCompressorTest { class VelocityCompressorTest {
@BeforeAll
static void checkNatives() {
Natives.compressor.getLoadedVariant();
}
@Test @Test
@EnabledOnOs({ MAC, LINUX }) @EnabledOnOs({ MAC, LINUX })
void nativeIntegrityCheck() throws DataFormatException { void nativeIntegrityCheck() throws DataFormatException {
VelocityCompressor compressor = Natives.compressor.supply().get(); VelocityCompressor compressor = Natives.compressor.get();
if (compressor instanceof JavaVelocityCompressor) { if (compressor instanceof JavaVelocityCompressor) {
fail("Loaded regular compressor"); fail("Loaded regular compressor");
} }
@ -38,7 +44,7 @@ class VelocityCompressorTest {
ByteBuf decompressed = Unpooled.directBuffer(); ByteBuf decompressed = Unpooled.directBuffer();
Random random = new Random(1); Random random = new Random(1);
byte[] randomBytes = new byte[1 << 21]; byte[] randomBytes = new byte[1 << 16];
random.nextBytes(randomBytes); random.nextBytes(randomBytes);
source.writeBytes(randomBytes); source.writeBytes(randomBytes);

Datei anzeigen

@ -191,7 +191,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
return; return;
} }
VelocityCompressor compressor = Natives.compressor.supply().get(); VelocityCompressor compressor = Natives.compressor.get();
MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor); MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor);
MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor); MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor);