#3737: Use composite buffers where possible
This commit is contained in:
@@ -1,27 +1,38 @@
|
||||
package net.md_5.bungee.protocol;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import io.netty.handler.codec.MessageToMessageEncoder;
|
||||
import java.util.List;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Prepend length of the message as a Varint21 by writing length and data to a
|
||||
* new buffer
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class Varint21LengthFieldPrepender extends MessageToByteEncoder<ByteBuf>
|
||||
public class Varint21LengthFieldPrepender extends MessageToMessageEncoder<ByteBuf>
|
||||
{
|
||||
|
||||
@Setter
|
||||
private boolean compose = true;
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> list) throws Exception
|
||||
{
|
||||
int bodyLen = msg.readableBytes();
|
||||
int headerLen = varintSize( bodyLen );
|
||||
out.ensureWritable( headerLen + bodyLen );
|
||||
|
||||
DefinedPacket.writeVarInt( bodyLen, out );
|
||||
out.writeBytes( msg );
|
||||
if ( compose )
|
||||
{
|
||||
ByteBuf buf = ctx.alloc().directBuffer( headerLen );
|
||||
DefinedPacket.writeVarInt( bodyLen, buf );
|
||||
list.add( ctx.alloc().compositeDirectBuffer( 2 ).addComponents( true, buf, msg.retain() ) );
|
||||
} else
|
||||
{
|
||||
ByteBuf buf = ctx.alloc().directBuffer( headerLen + bodyLen );
|
||||
DefinedPacket.writeVarInt( bodyLen, buf );
|
||||
buf.writeBytes( msg );
|
||||
list.add( buf );
|
||||
}
|
||||
}
|
||||
|
||||
static int varintSize(int paramInt)
|
||||
|
Reference in New Issue
Block a user