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
# 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.
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
public void dispose() {
disposed = true;
deflater.end();
inflater.end();
}
private void ensureNotDisposed() {

Datei anzeigen

@ -20,5 +20,5 @@ class NativeZlibDeflate {
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.
*/
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();
}

Datei anzeigen

@ -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;

Datei anzeigen

@ -6,7 +6,7 @@ import java.util.List;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
public class NativeCodeLoader<T> {
public class NativeCodeLoader<T> implements Supplier<T> {
private final List<Variant<T>> variants;
private Variant<T> selected;
@ -14,11 +14,12 @@ public class NativeCodeLoader<T> {
this.variants = ImmutableList.copyOf(variants);
}
public Supplier<T> supply() {
@Override
public T get() {
if (selected == null) {
selected = select();
}
return selected.supplier;
return selected.supplier.get();
}
private Variant<T> select() {
@ -33,14 +34,10 @@ public class NativeCodeLoader<T> {
}
public String getLoadedVariant() {
for (Variant<T> variant : variants) {
T got = variant.get();
if (got == null) {
continue;
if (selected == null) {
selected = select();
}
return variant.name;
}
throw new IllegalArgumentException("Can't find any suitable variants");
return selected.name;
}
static class Variant<T> {
@ -57,17 +54,15 @@ public class NativeCodeLoader<T> {
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<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");
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;
}

Datei anzeigen

@ -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);

Datei anzeigen

@ -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);