13
0
geforkt von Mirrors/Velocity

Have one MinecraftVarintLengthEncoder, not two

Applies more to the margins (such as login phase and server list ping), but every bit does help.
Dieser Commit ist enthalten in:
Andrew Steinborn 2024-09-03 00:33:57 -04:00
Ursprung 52ae735ea3
Commit f034c0277d
2 geänderte Dateien mit 19 neuen und 18 gelöschten Zeilen

Datei anzeigen

@ -30,7 +30,7 @@ import com.velocitypowered.proxy.protocol.netty.AutoReadHolderHandler;
import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder; import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintFrameDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftVarintFrameDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthCompositeEncoder; import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
@ -49,13 +49,13 @@ public class BackendChannelInitializer extends ChannelInitializer<Channel> {
} }
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) {
ch.pipeline() ch.pipeline()
.addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder())
.addLast(READ_TIMEOUT, .addLast(READ_TIMEOUT,
new ReadTimeoutHandler(server.getConfiguration().getReadTimeout(), new ReadTimeoutHandler(server.getConfiguration().getReadTimeout(),
TimeUnit.MILLISECONDS)) TimeUnit.MILLISECONDS))
.addLast(FRAME_ENCODER, MinecraftVarintLengthCompositeEncoder.INSTANCE) .addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE)
.addLast(MINECRAFT_DECODER, .addLast(MINECRAFT_DECODER,
new MinecraftDecoder(ProtocolUtils.Direction.CLIENTBOUND)) new MinecraftDecoder(ProtocolUtils.Direction.CLIENTBOUND))
.addLast(FLOW_HANDLER, new AutoReadHolderHandler()) .addLast(FLOW_HANDLER, new AutoReadHolderHandler())

Datei anzeigen

@ -23,33 +23,34 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToMessageEncoder;
import java.util.List;
/** /**
* Handler for appending a length for Minecraft packets. * Handler for appending a length for Minecraft packets.
*/ */
@ChannelHandler.Sharable @ChannelHandler.Sharable
public class MinecraftVarintLengthEncoder extends MessageToByteEncoder<ByteBuf> { public class MinecraftVarintLengthEncoder extends MessageToMessageEncoder<ByteBuf> {
public static final MinecraftVarintLengthEncoder INSTANCE = new MinecraftVarintLengthEncoder(); public static final MinecraftVarintLengthEncoder INSTANCE = new MinecraftVarintLengthEncoder();
public static final boolean IS_JAVA_CIPHER = Natives.cipher.get() == JavaVelocityCipher.FACTORY;
static final boolean IS_JAVA_CIPHER = Natives.cipher.get() == JavaVelocityCipher.FACTORY;
private MinecraftVarintLengthEncoder() { private MinecraftVarintLengthEncoder() {
} }
@Override @Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, ByteBuf buf,
ProtocolUtils.writeVarInt(out, msg.readableBytes()); List<Object> list) throws Exception {
out.writeBytes(msg); final int length = buf.readableBytes();
} final int varintLength = ProtocolUtils.varIntBytes(length);
@Override final ByteBuf lenBuf = IS_JAVA_CIPHER
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) ? ctx.alloc().heapBuffer(varintLength)
throws Exception { : ctx.alloc().directBuffer(varintLength);
int anticipatedRequiredCapacity = ProtocolUtils.varIntBytes(msg.readableBytes())
+ msg.readableBytes(); ProtocolUtils.writeVarInt(lenBuf, length);
return IS_JAVA_CIPHER list.add(lenBuf);
? ctx.alloc().heapBuffer(anticipatedRequiredCapacity) list.add(buf.retain());
: ctx.alloc().directBuffer(anticipatedRequiredCapacity);
} }
} }