Refactor native code and implement our own JNI wrapper around zlib.
The previous native cipher code has been refactored so that it may be loaded and used slightly more generically, allowing more native components to be easily added as time goes on. I have also written a new native code compression module, which wraps around zlib in the same manner that Inflater / Deflater does, however it operates directly on the memory addresses of it's input / output buffers which means that we can save one, or maybe even two copies. To support this, the VarInt decoder has been adjusted to always use a native buffer.
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
package net.md_5.bungee.protocol;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
public class PacketCompressor extends MessageToByteEncoder<ByteBuf>
|
||||
{
|
||||
|
||||
private final byte[] buffer = new byte[ 8192 ];
|
||||
private final Deflater deflater = new Deflater();
|
||||
@Setter
|
||||
private int threshold = 256;
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception
|
||||
{
|
||||
int origSize = msg.readableBytes();
|
||||
if ( origSize < threshold )
|
||||
{
|
||||
DefinedPacket.writeVarInt( 0, out );
|
||||
out.writeBytes( msg );
|
||||
} else
|
||||
{
|
||||
byte[] data = new byte[ origSize ];
|
||||
msg.readBytes( data );
|
||||
|
||||
DefinedPacket.writeVarInt( data.length, out );
|
||||
|
||||
deflater.setInput( data );
|
||||
deflater.finish();
|
||||
while ( !deflater.finished() )
|
||||
{
|
||||
int count = deflater.deflate( buffer );
|
||||
out.writeBytes( buffer, 0, count );
|
||||
}
|
||||
deflater.reset();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
package net.md_5.bungee.protocol;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
public class PacketDecompressor extends ByteToMessageDecoder
|
||||
{
|
||||
|
||||
private final Inflater inflater = new Inflater();
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
|
||||
{
|
||||
if ( in.readableBytes() == 0 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int size = DefinedPacket.readVarInt( in );
|
||||
if ( size == 0 )
|
||||
{
|
||||
out.add( in.readBytes( in.readableBytes() ) );
|
||||
} else
|
||||
{
|
||||
byte[] compressedData = new byte[ in.readableBytes() ];
|
||||
in.readBytes( compressedData );
|
||||
inflater.setInput( compressedData );
|
||||
|
||||
byte[] data = new byte[ size ];
|
||||
inflater.inflate( data );
|
||||
out.add( Unpooled.wrappedBuffer( data ) );
|
||||
inflater.reset();
|
||||
}
|
||||
}
|
||||
}
|
@@ -36,7 +36,11 @@ public class Varint21FrameDecoder extends ByteToMessageDecoder
|
||||
return;
|
||||
} else
|
||||
{
|
||||
out.add( in.readBytes( length ) );
|
||||
// TODO: Really should be a slice!
|
||||
ByteBuf dst = ctx.alloc().directBuffer( length );
|
||||
in.readBytes( dst );
|
||||
|
||||
out.add( dst );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user