13
0
geforkt von Mirrors/Velocity

Improvements for natives.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-08-02 23:52:21 -04:00
Ursprung 12ad629d4d
Commit 68d315d1d3
8 geänderte Dateien mit 44 neuen und 29 gelöschten Zeilen

Datei anzeigen

@ -8,13 +8,15 @@ import java.util.zip.Deflater;
import java.util.zip.Inflater; import java.util.zip.Inflater;
public class JavaVelocityCompressor implements VelocityCompressor { public class JavaVelocityCompressor implements VelocityCompressor {
public static final VelocityCompressorFactory FACTORY = JavaVelocityCompressor::new;
private final Deflater deflater; private final Deflater deflater;
private final Inflater inflater; private final Inflater inflater;
private final byte[] buf; private final byte[] buf;
private boolean disposed = false; private boolean disposed = false;
public JavaVelocityCompressor() { private JavaVelocityCompressor(int level) {
this.deflater = new Deflater(); this.deflater = new Deflater(level);
this.inflater = new Inflater(); this.inflater = new Inflater();
this.buf = new byte[ZLIB_BUFFER_SIZE]; this.buf = new byte[ZLIB_BUFFER_SIZE];
} }

Datei anzeigen

@ -4,18 +4,19 @@ import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.util.zip.DataFormatException; import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
public class NativeVelocityCompressor implements VelocityCompressor { public class NativeVelocityCompressor implements VelocityCompressor {
public static final VelocityCompressorFactory FACTORY = NativeVelocityCompressor::new;
private final NativeZlibInflate inflate = new NativeZlibInflate(); private final NativeZlibInflate inflate = new NativeZlibInflate();
private final long inflateCtx; private final long inflateCtx;
private final NativeZlibDeflate deflate = new NativeZlibDeflate(); private final NativeZlibDeflate deflate = new NativeZlibDeflate();
private final long deflateCtx; private final long deflateCtx;
private boolean disposed = false; private boolean disposed = false;
public NativeVelocityCompressor() { private NativeVelocityCompressor(int level) {
this.inflateCtx = inflate.init(); this.inflateCtx = inflate.init();
this.deflateCtx = deflate.init(Deflater.DEFAULT_COMPRESSION); this.deflateCtx = deflate.init(level);
} }
@Override @Override

Datei anzeigen

@ -0,0 +1,5 @@
package com.velocitypowered.natives.compression;
public interface VelocityCompressorFactory {
VelocityCompressor create(int level);
}

Datei anzeigen

@ -19,7 +19,7 @@ public class NativeCodeLoader<T> implements Supplier<T> {
if (selected == null) { if (selected == null) {
selected = select(); selected = select();
} }
return selected.supplier.get(); return selected.object;
} }
private Variant<T> select() { private Variant<T> select() {
@ -44,14 +44,14 @@ public class NativeCodeLoader<T> implements Supplier<T> {
private boolean available; private boolean available;
private final Runnable setup; private final Runnable setup;
private final String name; private final String name;
private final Supplier<T> supplier; private final T object;
private boolean hasBeenSetup = false; private boolean hasBeenSetup = false;
Variant(BooleanSupplier available, Runnable setup, String name, Supplier<T> supplier) { Variant(BooleanSupplier available, Runnable setup, String name, T object) {
this.available = available.getAsBoolean(); this.available = available.getAsBoolean();
this.setup = setup; this.setup = setup;
this.name = name; this.name = name;
this.supplier = supplier; this.object = object;
} }
private void setup() { private void setup() {
@ -71,7 +71,7 @@ public class NativeCodeLoader<T> implements Supplier<T> {
} }
if (available) { if (available) {
return supplier.get(); return object;
} }
return null; return null;

Datei anzeigen

@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import com.velocitypowered.natives.compression.JavaVelocityCompressor; import com.velocitypowered.natives.compression.JavaVelocityCompressor;
import com.velocitypowered.natives.compression.NativeVelocityCompressor; import com.velocitypowered.natives.compression.NativeVelocityCompressor;
import com.velocitypowered.natives.compression.VelocityCompressor; import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.compression.VelocityCompressorFactory;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -34,15 +35,15 @@ public class Natives {
}; };
} }
public static final NativeCodeLoader<VelocityCompressor> compressor = new NativeCodeLoader<>( public static final NativeCodeLoader<VelocityCompressorFactory> compressor = new NativeCodeLoader<>(
ImmutableList.of( ImmutableList.of(
new NativeCodeLoader.Variant<>(NativeCodeLoader.MACOS, new NativeCodeLoader.Variant<>(NativeCodeLoader.MACOS,
copyAndLoadNative("/macosx/velocity-compress.dylib"), "native compression (macOS)", copyAndLoadNative("/macosx/velocity-compress.dylib"), "native compression (macOS)",
NativeVelocityCompressor::new), NativeVelocityCompressor.FACTORY),
new NativeCodeLoader.Variant<>(NativeCodeLoader.LINUX, new NativeCodeLoader.Variant<>(NativeCodeLoader.LINUX,
copyAndLoadNative("/linux_x64/velocity-compress.so"), "native compression (Linux amd64)", copyAndLoadNative("/linux_x64/velocity-compress.so"), "native compression (Linux amd64)",
NativeVelocityCompressor::new), NativeVelocityCompressor.FACTORY),
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor::new) new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor.FACTORY)
) )
); );
} }

Datei anzeigen

@ -11,6 +11,7 @@ import org.junit.jupiter.api.condition.EnabledOnOs;
import java.util.Random; import java.util.Random;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.zip.DataFormatException; import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -26,7 +27,7 @@ class VelocityCompressorTest {
@Test @Test
@EnabledOnOs({ MAC, LINUX }) @EnabledOnOs({ MAC, LINUX })
void nativeIntegrityCheck() throws DataFormatException { void nativeIntegrityCheck() throws DataFormatException {
VelocityCompressor compressor = Natives.compressor.get(); VelocityCompressor compressor = Natives.compressor.get().create(Deflater.DEFAULT_COMPRESSION);
if (compressor instanceof JavaVelocityCompressor) { if (compressor instanceof JavaVelocityCompressor) {
fail("Loaded regular compressor"); fail("Loaded regular compressor");
} }
@ -35,7 +36,7 @@ class VelocityCompressorTest {
@Test @Test
void javaIntegrityCheck() throws DataFormatException { void javaIntegrityCheck() throws DataFormatException {
JavaVelocityCompressor compressor = new JavaVelocityCompressor(); VelocityCompressor compressor = JavaVelocityCompressor.FACTORY.create(Deflater.DEFAULT_COMPRESSION);
check(compressor); check(compressor);
} }

Datei anzeigen

@ -20,6 +20,7 @@ import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.zip.Deflater;
import static com.velocitypowered.network.Connections.CIPHER_DECODER; import static com.velocitypowered.network.Connections.CIPHER_DECODER;
import static com.velocitypowered.network.Connections.CIPHER_ENCODER; import static com.velocitypowered.network.Connections.CIPHER_ENCODER;
@ -192,7 +193,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
return; return;
} }
VelocityCompressor compressor = Natives.compressor.get(); VelocityCompressor compressor = Natives.compressor.get().create(Deflater.DEFAULT_COMPRESSION);
MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor); MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor);
MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor); MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor);

Datei anzeigen

@ -6,6 +6,7 @@ import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.LastHttpContent; import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.ReferenceCountUtil;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -20,22 +21,25 @@ class SimpleHttpResponseCollector extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse) { try {
HttpResponseStatus status = ((HttpResponse) msg).status(); if (msg instanceof HttpResponse) {
if (status != HttpResponseStatus.OK) { HttpResponseStatus status = ((HttpResponse) msg).status();
ctx.close(); if (status != HttpResponseStatus.OK) {
reply.completeExceptionally(new RuntimeException("Unexpected status code " + status.code())); ctx.close();
reply.completeExceptionally(new RuntimeException("Unexpected status code " + status.code()));
}
} }
}
if (msg instanceof HttpContent) { if (msg instanceof HttpContent) {
buffer.append(((HttpContent) msg).content().toString(StandardCharsets.UTF_8)); buffer.append(((HttpContent) msg).content().toString(StandardCharsets.UTF_8));
((HttpContent) msg).release();
if (msg instanceof LastHttpContent) { if (msg instanceof LastHttpContent) {
ctx.close(); ctx.close();
reply.complete(buffer.toString()); reply.complete(buffer.toString());
}
} }
} finally {
ReferenceCountUtil.release(msg);
} }
} }
} }