-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
211 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/nethergames/proxytransport/compression/FrameIdCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.nethergames.proxytransport.compression; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToMessageCodec; | ||
import org.cloudburstmc.protocol.bedrock.netty.BedrockBatchWrapper; | ||
|
||
import java.util.List; | ||
|
||
public class FrameIdCodec extends MessageToMessageCodec<ByteBuf, BedrockBatchWrapper> { | ||
public static final String NAME = "frame-id-codec"; | ||
|
||
@Override | ||
protected void encode(ChannelHandlerContext ctx, BedrockBatchWrapper msg, List<Object> out) throws Exception { | ||
out.add(msg.getCompressed().retain()); | ||
} | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { | ||
out.add(BedrockBatchWrapper.newInstance(msg.readRetainedSlice(msg.readableBytes()), null)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/nethergames/proxytransport/compression/ProxyTransportAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.nethergames.proxytransport.compression; | ||
|
||
import org.cloudburstmc.protocol.bedrock.data.CompressionAlgorithm; | ||
|
||
public enum ProxyTransportAlgorithm implements CompressionAlgorithm { | ||
ZSTD; | ||
|
||
private ProxyTransportAlgorithm() { | ||
|
||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/org/nethergames/proxytransport/compression/ProxyTransportCompressionCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.nethergames.proxytransport.compression; | ||
|
||
import dev.waterdog.waterdogpe.network.connection.codec.compression.ProxiedCompressionCodec; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.CompositeByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import org.cloudburstmc.protocol.bedrock.data.CompressionAlgorithm; | ||
import org.cloudburstmc.protocol.bedrock.data.PacketCompressionAlgorithm; | ||
import org.cloudburstmc.protocol.bedrock.netty.BedrockBatchWrapper; | ||
import org.cloudburstmc.protocol.bedrock.netty.codec.compression.BatchCompression; | ||
import org.cloudburstmc.protocol.bedrock.netty.codec.compression.CompressionStrategy; | ||
|
||
import java.util.List; | ||
|
||
public class ProxyTransportCompressionCodec extends ProxiedCompressionCodec { | ||
private final boolean prefixed; | ||
private final ZstdCompression zstdCompression = new ZstdCompression(); | ||
|
||
public ProxyTransportCompressionCodec(CompressionStrategy strategy, boolean prefixed) { | ||
super(strategy, prefixed); | ||
this.prefixed = prefixed; | ||
} | ||
|
||
protected void encode(ChannelHandlerContext ctx, BedrockBatchWrapper msg, List<Object> out) throws Exception { | ||
if (msg.getCompressed() == null && msg.getUncompressed() == null) { | ||
throw new IllegalStateException("Batch was not encoded before"); | ||
} else if (msg.getCompressed() != null && !msg.isModified()) { // already compressed | ||
if (!this.prefixed) { // we need to prefix the compressed data | ||
CompositeByteBuf buf = ctx.alloc().compositeDirectBuffer(2); | ||
buf.addComponent(true, ctx.alloc().ioBuffer(1).writeByte(getCompressionHeader(msg.getAlgorithm()))); | ||
buf.addComponent(true, msg.getCompressed().retainedSlice()); | ||
msg.setCompressed(buf, msg.getAlgorithm()); | ||
} | ||
|
||
this.onPassedThrough(ctx, msg); | ||
out.add(msg.retain()); | ||
} else { | ||
BatchCompression compression = this.getStrategy().getCompression(msg); | ||
if (!compression.getAlgorithm().equals(PacketCompressionAlgorithm.NONE)) { | ||
compression = this.zstdCompression; | ||
} | ||
|
||
ByteBuf compressed = compression.encode(ctx, msg.getUncompressed()); | ||
|
||
try { | ||
ByteBuf outBuf; | ||
|
||
outBuf = ctx.alloc().ioBuffer(1 + compressed.readableBytes()); | ||
outBuf.writeByte(this.getCompressionHeader(compression.getAlgorithm())); | ||
outBuf.writeBytes(compressed); | ||
|
||
msg.setCompressed(outBuf, compression.getAlgorithm()); | ||
} finally { | ||
compressed.release(); | ||
} | ||
|
||
this.onCompressed(ctx, msg); | ||
out.add(msg.retain()); | ||
} | ||
} | ||
|
||
protected byte getCompressionHeader0(CompressionAlgorithm algorithm) { | ||
if (algorithm instanceof ProxyTransportAlgorithm) { | ||
return -2; | ||
} | ||
|
||
return super.getCompressionHeader0(algorithm); | ||
} | ||
|
||
protected CompressionAlgorithm getCompressionAlgorithm0(byte header) { | ||
if (header == -2) { | ||
return ProxyTransportAlgorithm.ZSTD; | ||
} | ||
|
||
return super.getCompressionAlgorithm0(header); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/nethergames/proxytransport/compression/ZstdCompression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.nethergames.proxytransport.compression; | ||
|
||
import com.github.luben.zstd.Zstd; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.CompositeByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.util.ReferenceCountUtil; | ||
import org.cloudburstmc.protocol.bedrock.data.CompressionAlgorithm; | ||
import org.cloudburstmc.protocol.bedrock.netty.codec.compression.BatchCompression; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class ZstdCompression implements BatchCompression { | ||
private int level = -1; | ||
|
||
public ByteBuf encode(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { | ||
ByteBuf direct; | ||
if (!msg.isDirect() || msg instanceof CompositeByteBuf) { | ||
direct = ctx.alloc().ioBuffer(msg.readableBytes()); | ||
direct.writeBytes(msg); | ||
} else { | ||
direct = msg; | ||
} | ||
|
||
ByteBuf output = ctx.alloc().directBuffer(); | ||
try { | ||
int uncompressedLength = direct.readableBytes(); | ||
int maxLength = (int) Zstd.compressBound(uncompressedLength); | ||
|
||
output.ensureWritable(maxLength); | ||
|
||
int compressedLength; | ||
if (direct.hasMemoryAddress()) { | ||
compressedLength = (int) Zstd.compressUnsafe(output.memoryAddress(), maxLength, direct.memoryAddress() + direct.readerIndex(), uncompressedLength, this.level); | ||
} else { | ||
ByteBuffer sourceNio = direct.nioBuffer(direct.readerIndex(), direct.readableBytes()); | ||
ByteBuffer targetNio = output.nioBuffer(0, maxLength); | ||
|
||
compressedLength = Zstd.compress(targetNio, sourceNio, this.level); | ||
} | ||
|
||
output.writerIndex(compressedLength); | ||
return output.retain(); | ||
} finally { | ||
ReferenceCountUtil.release(output); | ||
if (direct != msg) { | ||
ReferenceCountUtil.release(direct); | ||
} | ||
} | ||
} | ||
|
||
public ByteBuf decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { | ||
throw new UnsupportedOperationException("Zstd is not supported"); | ||
} | ||
|
||
public CompressionAlgorithm getAlgorithm() { | ||
return ProxyTransportAlgorithm.ZSTD; | ||
} | ||
|
||
public void setLevel(int level) { | ||
this.level = level; | ||
} | ||
|
||
public int getLevel() { | ||
return level; | ||
} | ||
} |
136 changes: 0 additions & 136 deletions
136
src/main/java/org/nethergames/proxytransport/compression/ZstdCompressionCodec.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.