Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-06 00:00:47 +01:00
Improvements for natives.
Dieser Commit ist enthalten in:
Ursprung
12ad629d4d
Commit
68d315d1d3
@ -8,13 +8,15 @@ import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
public class JavaVelocityCompressor implements VelocityCompressor {
|
||||
public static final VelocityCompressorFactory FACTORY = JavaVelocityCompressor::new;
|
||||
|
||||
private final Deflater deflater;
|
||||
private final Inflater inflater;
|
||||
private final byte[] buf;
|
||||
private boolean disposed = false;
|
||||
|
||||
public JavaVelocityCompressor() {
|
||||
this.deflater = new Deflater();
|
||||
private JavaVelocityCompressor(int level) {
|
||||
this.deflater = new Deflater(level);
|
||||
this.inflater = new Inflater();
|
||||
this.buf = new byte[ZLIB_BUFFER_SIZE];
|
||||
}
|
||||
|
@ -4,18 +4,19 @@ import com.google.common.base.Preconditions;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
public class NativeVelocityCompressor implements VelocityCompressor {
|
||||
public static final VelocityCompressorFactory FACTORY = NativeVelocityCompressor::new;
|
||||
|
||||
private final NativeZlibInflate inflate = new NativeZlibInflate();
|
||||
private final long inflateCtx;
|
||||
private final NativeZlibDeflate deflate = new NativeZlibDeflate();
|
||||
private final long deflateCtx;
|
||||
private boolean disposed = false;
|
||||
|
||||
public NativeVelocityCompressor() {
|
||||
private NativeVelocityCompressor(int level) {
|
||||
this.inflateCtx = inflate.init();
|
||||
this.deflateCtx = deflate.init(Deflater.DEFAULT_COMPRESSION);
|
||||
this.deflateCtx = deflate.init(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,5 @@
|
||||
package com.velocitypowered.natives.compression;
|
||||
|
||||
public interface VelocityCompressorFactory {
|
||||
VelocityCompressor create(int level);
|
||||
}
|
@ -19,7 +19,7 @@ public class NativeCodeLoader<T> implements Supplier<T> {
|
||||
if (selected == null) {
|
||||
selected = select();
|
||||
}
|
||||
return selected.supplier.get();
|
||||
return selected.object;
|
||||
}
|
||||
|
||||
private Variant<T> select() {
|
||||
@ -44,14 +44,14 @@ public class NativeCodeLoader<T> implements Supplier<T> {
|
||||
private boolean available;
|
||||
private final Runnable setup;
|
||||
private final String name;
|
||||
private final Supplier<T> supplier;
|
||||
private final T object;
|
||||
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.setup = setup;
|
||||
this.name = name;
|
||||
this.supplier = supplier;
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
private void setup() {
|
||||
@ -71,7 +71,7 @@ public class NativeCodeLoader<T> implements Supplier<T> {
|
||||
}
|
||||
|
||||
if (available) {
|
||||
return supplier.get();
|
||||
return object;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
|
||||
import com.velocitypowered.natives.compression.JavaVelocityCompressor;
|
||||
import com.velocitypowered.natives.compression.NativeVelocityCompressor;
|
||||
import com.velocitypowered.natives.compression.VelocityCompressor;
|
||||
import com.velocitypowered.natives.compression.VelocityCompressorFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
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(
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.MACOS,
|
||||
copyAndLoadNative("/macosx/velocity-compress.dylib"), "native compression (macOS)",
|
||||
NativeVelocityCompressor::new),
|
||||
NativeVelocityCompressor.FACTORY),
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.LINUX,
|
||||
copyAndLoadNative("/linux_x64/velocity-compress.so"), "native compression (Linux amd64)",
|
||||
NativeVelocityCompressor::new),
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor::new)
|
||||
NativeVelocityCompressor.FACTORY),
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor.FACTORY)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import org.junit.jupiter.api.condition.EnabledOnOs;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
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.fail;
|
||||
@ -26,7 +27,7 @@ class VelocityCompressorTest {
|
||||
@Test
|
||||
@EnabledOnOs({ MAC, LINUX })
|
||||
void nativeIntegrityCheck() throws DataFormatException {
|
||||
VelocityCompressor compressor = Natives.compressor.get();
|
||||
VelocityCompressor compressor = Natives.compressor.get().create(Deflater.DEFAULT_COMPRESSION);
|
||||
if (compressor instanceof JavaVelocityCompressor) {
|
||||
fail("Loaded regular compressor");
|
||||
}
|
||||
@ -35,7 +36,7 @@ class VelocityCompressorTest {
|
||||
|
||||
@Test
|
||||
void javaIntegrityCheck() throws DataFormatException {
|
||||
JavaVelocityCompressor compressor = new JavaVelocityCompressor();
|
||||
VelocityCompressor compressor = JavaVelocityCompressor.FACTORY.create(Deflater.DEFAULT_COMPRESSION);
|
||||
check(compressor);
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import static com.velocitypowered.network.Connections.CIPHER_DECODER;
|
||||
import static com.velocitypowered.network.Connections.CIPHER_ENCODER;
|
||||
@ -192,7 +193,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
|
||||
return;
|
||||
}
|
||||
|
||||
VelocityCompressor compressor = Natives.compressor.get();
|
||||
VelocityCompressor compressor = Natives.compressor.get().create(Deflater.DEFAULT_COMPRESSION);
|
||||
MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor);
|
||||
MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor);
|
||||
|
||||
|
@ -6,6 +6,7 @@ import io.netty.handler.codec.http.HttpContent;
|
||||
import io.netty.handler.codec.http.HttpResponse;
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
import io.netty.handler.codec.http.LastHttpContent;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@ -20,6 +21,7 @@ class SimpleHttpResponseCollector extends ChannelInboundHandlerAdapter {
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
try {
|
||||
if (msg instanceof HttpResponse) {
|
||||
HttpResponseStatus status = ((HttpResponse) msg).status();
|
||||
if (status != HttpResponseStatus.OK) {
|
||||
@ -30,12 +32,14 @@ class SimpleHttpResponseCollector extends ChannelInboundHandlerAdapter {
|
||||
|
||||
if (msg instanceof HttpContent) {
|
||||
buffer.append(((HttpContent) msg).content().toString(StandardCharsets.UTF_8));
|
||||
((HttpContent) msg).release();
|
||||
|
||||
if (msg instanceof LastHttpContent) {
|
||||
ctx.close();
|
||||
reply.complete(buffer.toString());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
ReferenceCountUtil.release(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren