From 22dd4bbb99f0540e5cc97afd04abc67077cebace Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Wed, 1 Aug 2018 00:20:39 -0400 Subject: [PATCH] Split natives. Add proper disposal for natives. --- api/build.gradle | 2 +- build.gradle | 1 + native/build.gradle | 8 ++++++++ .../java/com/velocitypowered/natives}/Disposable.java | 6 +++++- .../natives}/compression/JavaVelocityCompressor.java | 3 +-- .../natives}/compression/VelocityCompressor.java | 4 ++-- .../natives}/encryption/JavaVelocityCipher.java | 3 +-- .../natives}/encryption/VelocityCipher.java | 4 ++-- proxy/build.gradle | 1 + .../proxy/connection/MinecraftConnection.java | 6 +++--- .../proxy/protocol/netty/MinecraftCipherDecoder.java | 7 ++++++- .../proxy/protocol/netty/MinecraftCipherEncoder.java | 7 ++++++- .../proxy/protocol/netty/MinecraftCompressDecoder.java | 7 ++++++- .../proxy/protocol/netty/MinecraftCompressEncoder.java | 8 +++++++- settings.gradle | 9 ++++++--- 15 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 native/build.gradle rename {proxy/src/main/java/com.velocitypowered/proxy/util => native/src/main/java/com/velocitypowered/natives}/Disposable.java (52%) rename {proxy/src/main/java/com.velocitypowered/proxy/protocol => native/src/main/java/com/velocitypowered/natives}/compression/JavaVelocityCompressor.java (95%) rename {proxy/src/main/java/com.velocitypowered/proxy/protocol => native/src/main/java/com/velocitypowered/natives}/compression/VelocityCompressor.java (73%) rename {proxy/src/main/java/com.velocitypowered/proxy/protocol => native/src/main/java/com/velocitypowered/natives}/encryption/JavaVelocityCipher.java (94%) rename {proxy/src/main/java/com.velocitypowered/proxy/protocol => native/src/main/java/com/velocitypowered/natives}/encryption/VelocityCipher.java (67%) diff --git a/api/build.gradle b/api/build.gradle index 0252e96c4..8021a51dd 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -5,7 +5,7 @@ plugins { dependencies { compile 'com.google.code.gson:gson:2.8.5' - compile 'com.google.guava:guava:25.1-jre' + compile "com.google.guava:guava:${guavaVersion}" compile 'net.kyori:text:1.12-1.6.0-SNAPSHOT' compile 'com.moandjiezana.toml:toml4j:0.7.2' testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}" diff --git a/build.gradle b/build.gradle index b87c0c007..42320b400 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,7 @@ allprojects { junitVersion = '5.3.0-M1' log4jVersion = '2.11.0' nettyVersion = '4.1.28.Final' + guavaVersion = '25.1-jre' } repositories { diff --git a/native/build.gradle b/native/build.gradle new file mode 100644 index 000000000..13647ccf9 --- /dev/null +++ b/native/build.gradle @@ -0,0 +1,8 @@ +plugins { + id 'java' +} + +dependencies { + compile "com.google.guava:guava:${guavaVersion}" + compile "io.netty:netty-buffer:${nettyVersion}" +} \ No newline at end of file diff --git a/proxy/src/main/java/com.velocitypowered/proxy/util/Disposable.java b/native/src/main/java/com/velocitypowered/natives/Disposable.java similarity index 52% rename from proxy/src/main/java/com.velocitypowered/proxy/util/Disposable.java rename to native/src/main/java/com/velocitypowered/natives/Disposable.java index bca174a6d..bb2cf0350 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/util/Disposable.java +++ b/native/src/main/java/com/velocitypowered/natives/Disposable.java @@ -1,9 +1,13 @@ -package com.velocitypowered.proxy.util; +package com.velocitypowered.natives; /** * This marker interface indicates that this object should be explicitly disposed before the object can no longer be used. * Not disposing these objects will likely leak native resources and eventually lead to resource exhaustion. */ public interface Disposable { + /** + * Disposes this object. After this call returns, any use of this object becomes invalid. Multiple calls to + * this function should be safe: there should be no side-effects once an object is disposed. + */ void dispose(); } diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/compression/JavaVelocityCompressor.java b/native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java similarity index 95% rename from proxy/src/main/java/com.velocitypowered/proxy/protocol/compression/JavaVelocityCompressor.java rename to native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java index 58e59921f..6f04a4aec 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/compression/JavaVelocityCompressor.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/JavaVelocityCompressor.java @@ -1,4 +1,4 @@ -package com.velocitypowered.proxy.protocol.compression; +package com.velocitypowered.natives.compression; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; @@ -50,7 +50,6 @@ public class JavaVelocityCompressor implements VelocityCompressor { @Override public void dispose() { - ensureNotDisposed(); disposed = true; } diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/compression/VelocityCompressor.java b/native/src/main/java/com/velocitypowered/natives/compression/VelocityCompressor.java similarity index 73% rename from proxy/src/main/java/com.velocitypowered/proxy/protocol/compression/VelocityCompressor.java rename to native/src/main/java/com/velocitypowered/natives/compression/VelocityCompressor.java index 8c1eaca15..c8056a54e 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/compression/VelocityCompressor.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/VelocityCompressor.java @@ -1,6 +1,6 @@ -package com.velocitypowered.proxy.protocol.compression; +package com.velocitypowered.natives.compression; -import com.velocitypowered.proxy.util.Disposable; +import com.velocitypowered.natives.Disposable; import io.netty.buffer.ByteBuf; import java.util.zip.DataFormatException; diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/encryption/JavaVelocityCipher.java b/native/src/main/java/com/velocitypowered/natives/encryption/JavaVelocityCipher.java similarity index 94% rename from proxy/src/main/java/com.velocitypowered/proxy/protocol/encryption/JavaVelocityCipher.java rename to native/src/main/java/com/velocitypowered/natives/encryption/JavaVelocityCipher.java index 146b0fe02..dd732d036 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/encryption/JavaVelocityCipher.java +++ b/native/src/main/java/com/velocitypowered/natives/encryption/JavaVelocityCipher.java @@ -1,4 +1,4 @@ -package com.velocitypowered.proxy.protocol.encryption; +package com.velocitypowered.natives.encryption; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; @@ -33,7 +33,6 @@ public class JavaVelocityCipher implements VelocityCipher { @Override public void dispose() { - ensureNotDisposed(); disposed = true; } diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/encryption/VelocityCipher.java b/native/src/main/java/com/velocitypowered/natives/encryption/VelocityCipher.java similarity index 67% rename from proxy/src/main/java/com.velocitypowered/proxy/protocol/encryption/VelocityCipher.java rename to native/src/main/java/com/velocitypowered/natives/encryption/VelocityCipher.java index d5f51eb67..d7ba55427 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/encryption/VelocityCipher.java +++ b/native/src/main/java/com/velocitypowered/natives/encryption/VelocityCipher.java @@ -1,6 +1,6 @@ -package com.velocitypowered.proxy.protocol.encryption; +package com.velocitypowered.natives.encryption; -import com.velocitypowered.proxy.util.Disposable; +import com.velocitypowered.natives.Disposable; import io.netty.buffer.ByteBuf; import javax.crypto.ShortBufferException; diff --git a/proxy/build.gradle b/proxy/build.gradle index 39d44b65c..32632575e 100644 --- a/proxy/build.gradle +++ b/proxy/build.gradle @@ -20,6 +20,7 @@ jar { dependencies { compile project(':velocity-api') + compile project(':velocity-native') compile "io.netty:netty-codec:${nettyVersion}" compile "io.netty:netty-codec-http:${nettyVersion}" compile "io.netty:netty-handler:${nettyVersion}" diff --git a/proxy/src/main/java/com.velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com.velocitypowered/proxy/connection/MinecraftConnection.java index d5b9023fb..5496f725e 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com.velocitypowered/proxy/connection/MinecraftConnection.java @@ -3,9 +3,9 @@ package com.velocitypowered.proxy.connection; import com.google.common.base.Preconditions; import com.velocitypowered.proxy.protocol.PacketWrapper; import com.velocitypowered.proxy.protocol.StateRegistry; -import com.velocitypowered.proxy.protocol.compression.JavaVelocityCompressor; -import com.velocitypowered.proxy.protocol.encryption.JavaVelocityCipher; -import com.velocitypowered.proxy.protocol.encryption.VelocityCipher; +import com.velocitypowered.natives.compression.JavaVelocityCompressor; +import com.velocitypowered.natives.encryption.JavaVelocityCipher; +import com.velocitypowered.natives.encryption.VelocityCipher; import com.velocitypowered.proxy.protocol.netty.*; import io.netty.channel.Channel; import io.netty.channel.ChannelFutureListener; diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherDecoder.java b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherDecoder.java index ae5ea06a6..cbb64a55c 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherDecoder.java +++ b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherDecoder.java @@ -1,7 +1,7 @@ package com.velocitypowered.proxy.protocol.netty; import com.google.common.base.Preconditions; -import com.velocitypowered.proxy.protocol.encryption.VelocityCipher; +import com.velocitypowered.natives.encryption.VelocityCipher; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; @@ -26,4 +26,9 @@ public class MinecraftCipherDecoder extends ByteToMessageDecoder { throw e; } } + + @Override + protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception { + cipher.dispose(); + } } diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherEncoder.java b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherEncoder.java index afe7cf502..581f4ea82 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherEncoder.java +++ b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCipherEncoder.java @@ -1,7 +1,7 @@ package com.velocitypowered.proxy.protocol.netty; import com.google.common.base.Preconditions; -import com.velocitypowered.proxy.protocol.encryption.VelocityCipher; +import com.velocitypowered.natives.encryption.VelocityCipher; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; @@ -17,4 +17,9 @@ public class MinecraftCipherEncoder extends MessageToByteEncoder { protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { cipher.process(msg, out); } + + @Override + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { + cipher.dispose(); + } } diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java index 7cbe9a399..3e48c4030 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java +++ b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java @@ -2,7 +2,7 @@ package com.velocitypowered.proxy.protocol.netty; import com.google.common.base.Preconditions; import com.velocitypowered.proxy.protocol.ProtocolUtils; -import com.velocitypowered.proxy.protocol.compression.VelocityCompressor; +import com.velocitypowered.natives.compression.VelocityCompressor; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; @@ -42,4 +42,9 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder { throw e; } } + + @Override + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { + compressor.dispose(); + } } diff --git a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java index 7b9d94be3..226c36832 100644 --- a/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java +++ b/proxy/src/main/java/com.velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java @@ -1,9 +1,10 @@ package com.velocitypowered.proxy.protocol.netty; import com.velocitypowered.proxy.protocol.ProtocolUtils; -import com.velocitypowered.proxy.protocol.compression.VelocityCompressor; +import com.velocitypowered.natives.compression.VelocityCompressor; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; import io.netty.handler.codec.MessageToByteEncoder; public class MinecraftCompressEncoder extends MessageToByteEncoder { @@ -34,4 +35,9 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder { compressedBuffer.release(); } } + + @Override + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { + compressor.dispose(); + } } diff --git a/settings.gradle b/settings.gradle index f89347eec..e3e315775 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,9 @@ rootProject.name = 'velocity' -include 'api' -include 'proxy' +include ( + 'api', + 'proxy', + 'native' +) findProject(':api')?.name = 'velocity-api' findProject(':proxy')?.name = 'velocity-proxy' - +findProject(':native')?.name = 'velocity-native' \ No newline at end of file