Skip to content

Commit 0cbda4e

Browse files
authored
Merge pull request #58 from PaperMC/dev/3.0.0
[pull] main from PaperMC:dev/3.0.0
2 parents fcd274f + f712997 commit 0cbda4e

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket;
6969
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
7070
import com.velocitypowered.proxy.protocol.packet.config.StartUpdatePacket;
71+
import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder;
7172
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
7273
import io.netty.buffer.ByteBuf;
7374
import io.netty.buffer.ByteBufUtil;
@@ -91,6 +92,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
9192
Boolean.getBoolean("velocity.log-server-backpressure");
9293
private static final int MAXIMUM_PACKETS_TO_FLUSH =
9394
Integer.getInteger("velocity.max-packets-per-flush", 8192);
95+
private static final int LARGE_PACKET_THRESHOLD = 1024 * 128;
9496

9597
private final VelocityServer server;
9698
private final VelocityServerConnection serverConn;
@@ -455,17 +457,19 @@ public void handleGeneric(MinecraftPacket packet) {
455457
if (packet instanceof PluginMessagePacket pluginMessage) {
456458
pluginMessage.retain();
457459
}
460+
boolean huge = packet instanceof DeferredByteBufHolder def && def.content().readableBytes() > LARGE_PACKET_THRESHOLD;
458461
playerConnection.delayedWrite(packet);
459-
if (++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) {
462+
if (huge || ++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) {
460463
playerConnection.flush();
461464
packetsFlushed = 0;
462465
}
463466
}
464467

465468
@Override
466469
public void handleUnknown(ByteBuf buf) {
470+
boolean huge = buf.readableBytes() > LARGE_PACKET_THRESHOLD;
467471
playerConnection.delayedWrite(buf.retain());
468-
if (++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) {
472+
if (huge || ++packetsFlushed >= MAXIMUM_PACKETS_TO_FLUSH) {
469473
playerConnection.flush();
470474
packetsFlushed = 0;
471475
}

proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/PluginMessagePacket.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
public class PluginMessagePacket extends DeferredByteBufHolder implements MinecraftPacket {
3333

34-
private static final int MAX_PAYLOAD_SIZE = Integer.getInteger("velocity.max-plugin-message-payload-size", 32767);
34+
private static final int MAX_PAYLOAD_SIZE_CLIENTBOUND = getPayloadLimit(Direction.CLIENTBOUND);
35+
private static final int MAX_PAYLOAD_SIZE_SERVERBOUND = getPayloadLimit(Direction.SERVERBOUND);
3536

3637
private @Nullable String channel;
3738

@@ -52,6 +53,19 @@ public String getChannel() {
5253
return channel;
5354
}
5455

56+
private static int getPayloadLimit(Direction direction) {
57+
if (System.getProperty("velocity.max-plugin-message-payload-size") != null) {
58+
return Integer.getInteger("velocity.max-plugin-message-payload-size");
59+
}
60+
if (direction == Direction.SERVERBOUND) {
61+
return Integer.getInteger("velocity.max-plugin-message-payload-size.serverbound", 32767);
62+
} else {
63+
// This is the vanilla expected limit, a payload this large feels like a nightmare given the trust
64+
// we give to servers...
65+
return Integer.getInteger("velocity.max-plugin-message-payload-size.clientbound", 1048576);
66+
}
67+
}
68+
5569
public void setChannel(String channel) {
5670
this.channel = channel;
5771
}
@@ -104,7 +118,8 @@ public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersi
104118

105119
@Override
106120
public int decodeExpectedMaxLength(ByteBuf buf, Direction direction, ProtocolVersion version) {
107-
return ProtocolUtils.DEFAULT_MAX_STRING_BYTES + MAX_PAYLOAD_SIZE;
121+
return ProtocolUtils.DEFAULT_MAX_STRING_BYTES +
122+
(direction == Direction.CLIENTBOUND ? MAX_PAYLOAD_SIZE_CLIENTBOUND : MAX_PAYLOAD_SIZE_SERVERBOUND);
108123
}
109124

110125
@Override

0 commit comments

Comments
 (0)