Reduce amount of memcpy within proxy pipeline.

This commit is contained in:
md_5
2016-01-24 11:22:39 +11:00
parent 79dbdea107
commit 052131c1fa
4 changed files with 34 additions and 37 deletions

View File

@@ -1,13 +1,14 @@
package net.md_5.bungee.compress;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import java.util.List;
import net.md_5.bungee.jni.zlib.BungeeZlib;
import net.md_5.bungee.protocol.DefinedPacket;
public class PacketDecompressor extends ByteToMessageDecoder
public class PacketDecompressor extends MessageToMessageDecoder<ByteBuf>
{
private final BungeeZlib zlib = CompressFactory.zlib.newInstance();
@@ -19,7 +20,7 @@ public class PacketDecompressor extends ByteToMessageDecoder
}
@Override
public void handlerRemoved0(ChannelHandlerContext ctx) throws Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
{
zlib.free();
}
@@ -27,22 +28,29 @@ public class PacketDecompressor extends ByteToMessageDecoder
@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.copy() );
in.readerIndex( in.writerIndex() );
out.add( in.slice().retain() );
in.skipBytes( in.readableBytes() );
} else
{
ByteBuf decompressed = ctx.alloc().directBuffer();
zlib.process( in, decompressed );
out.add( decompressed );
try
{
zlib.process( in, decompressed );
Preconditions.checkState( decompressed.readableBytes() == size, "Decompressed packet size mismatch" );
out.add( decompressed );
decompressed = null;
} finally
{
if ( decompressed != null )
{
decompressed.release();
}
}
}
}
}