3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

[Experimental] Remove PacketWrapper objects

In both Velocity and BungeeCord, the most commonly created object is an
object that encapsulates a Minecraft packet and a its associated byte
data.

At first, I considered trying to recycle these objects, but then I
discovered that this object has no reason to exist, and actually somewhat
complicates the implementation. Thus, this commit removes these objects,
making Velocity more more GC-friendly by not allocating frequently-created
objects.

This is still an experimental change, but it's a fairly safe one to make.
Dieser Commit ist enthalten in:
Andrew Steinborn 2018-08-07 04:28:07 -04:00
Ursprung fdf5f27da6
Commit abbdf70d5e
4 geänderte Dateien mit 12 neuen und 49 gelöschten Zeilen

Datei anzeigen

@ -5,12 +5,12 @@ import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.encryption.VelocityCipherFactory; import com.velocitypowered.natives.encryption.VelocityCipherFactory;
import com.velocitypowered.natives.util.Natives; import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.protocol.PacketWrapper; import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolConstants;
import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.natives.encryption.JavaVelocityCipher;
import com.velocitypowered.natives.encryption.VelocityCipher; import com.velocitypowered.natives.encryption.VelocityCipher;
import com.velocitypowered.proxy.protocol.netty.*; import com.velocitypowered.proxy.protocol.netty.*;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -79,18 +79,13 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof PacketWrapper) { if (msg instanceof MinecraftPacket) {
PacketWrapper pw = (PacketWrapper) msg; sessionHandler.handle((MinecraftPacket) msg);
} else if (msg instanceof ByteBuf) {
try { try {
if (sessionHandler != null) { sessionHandler.handleUnknown((ByteBuf) msg);
if (pw.getPacket() == null) {
sessionHandler.handleUnknown(pw.getBuffer());
} else {
sessionHandler.handle(pw.getPacket());
}
}
} finally { } finally {
ReferenceCountUtil.release(pw.getBuffer()); ReferenceCountUtil.release(msg);
} }
} }
} }

Datei anzeigen

@ -1,30 +0,0 @@
package com.velocitypowered.proxy.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
public class PacketWrapper {
private final MinecraftPacket packet;
private final ByteBuf buffer;
public PacketWrapper(MinecraftPacket packet, ByteBuf buffer) {
this.packet = packet;
this.buffer = buffer;
}
public MinecraftPacket getPacket() {
return packet;
}
public ByteBuf getBuffer() {
return buffer;
}
@Override
public String toString() {
return "PacketWrapper{" +
"packet=" + packet +
", buffer=" + ByteBufUtil.hexDump(buffer) +
'}';
}
}

Datei anzeigen

@ -1,10 +1,8 @@
package com.velocitypowered.proxy.protocol.netty; package com.velocitypowered.proxy.protocol.netty;
import com.velocitypowered.proxy.protocol.PacketWrapper;
import com.velocitypowered.proxy.protocol.packet.LegacyHandshake; import com.velocitypowered.proxy.protocol.packet.LegacyHandshake;
import com.velocitypowered.proxy.protocol.packet.LegacyPing; import com.velocitypowered.proxy.protocol.packet.LegacyPing;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
@ -21,10 +19,10 @@ public class LegacyPingDecoder extends ByteToMessageDecoder {
short second = in.getUnsignedByte(in.readerIndex() + 1); short second = in.getUnsignedByte(in.readerIndex() + 1);
if (first == 0xfe && second == 0x01) { if (first == 0xfe && second == 0x01) {
in.skipBytes(in.readableBytes()); in.skipBytes(in.readableBytes());
out.add(new PacketWrapper(new LegacyPing(), Unpooled.EMPTY_BUFFER)); out.add(new LegacyPing());
} else if (first == 0x02) { } else if (first == 0x02) {
in.skipBytes(in.readableBytes()); in.skipBytes(in.readableBytes());
out.add(new PacketWrapper(new LegacyHandshake(), Unpooled.EMPTY_BUFFER)); out.add(new LegacyHandshake());
} else { } else {
ctx.pipeline().remove(this); ctx.pipeline().remove(this);
} }

Datei anzeigen

@ -26,13 +26,13 @@ public class MinecraftDecoder extends MessageToMessageDecoder<ByteBuf> {
return; return;
} }
ByteBuf slice = msg.retainedSlice(); ByteBuf slice = msg.slice();
int packetId = ProtocolUtils.readVarInt(msg); int packetId = ProtocolUtils.readVarInt(msg);
MinecraftPacket packet = this.protocolVersion.createPacket(packetId); MinecraftPacket packet = this.protocolVersion.createPacket(packetId);
if (packet == null) { if (packet == null) {
msg.skipBytes(msg.readableBytes()); msg.skipBytes(msg.readableBytes());
out.add(new PacketWrapper(null, slice)); out.add(slice.retain());
} else { } else {
try { try {
packet.decode(msg, direction, protocolVersion.id); packet.decode(msg, direction, protocolVersion.id);
@ -40,7 +40,7 @@ public class MinecraftDecoder extends MessageToMessageDecoder<ByteBuf> {
throw new CorruptedFrameException("Error decoding " + packet.getClass() + " Direction " + direction throw new CorruptedFrameException("Error decoding " + packet.getClass() + " Direction " + direction
+ " Protocol " + protocolVersion + " State " + state + " ID " + Integer.toHexString(packetId), e); + " Protocol " + protocolVersion + " State " + state + " ID " + Integer.toHexString(packetId), e);
} }
out.add(new PacketWrapper(packet, slice)); out.add(packet);
} }
} }