PaperMC/patches/server/0283-Handle-Large-Packets-disconnecting-client.patch
MiniDigger | Martin 1ebaffacf5 some compile fixes
2023-09-22 19:31:02 +02:00

135 lines
6.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 27 Nov 2018 21:18:06 -0500
Subject: [PATCH] Handle Large Packets disconnecting client
If a players inventory is too big to send in a single packet,
split the inventory set into multiple packets instead.
diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java
index 2d7f332910328cef616609dc4c89facafcbe66cc..ff97e47cb248ade6530e52583b0b3865d77ace15 100644
--- a/src/main/java/net/minecraft/network/Connection.java
+++ b/src/main/java/net/minecraft/network/Connection.java
@@ -144,6 +144,22 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
}
public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) {
+ // Paper start
+ if (throwable instanceof io.netty.handler.codec.EncoderException && throwable.getCause() instanceof PacketEncoder.PacketTooLargeException packetTooLargeException) {
+ final Packet<?> packet = packetTooLargeException.getPacket();
+ final io.netty.util.Attribute<ConnectionProtocol.CodecData<?>> codecDataAttribute = channelhandlercontext.channel().attr(packetTooLargeException.codecKey);
+ if (packet.packetTooLarge(this)) {
+ ProtocolSwapHandler.swapProtocolIfNeeded(codecDataAttribute, packet);
+ return;
+ } else if (packet.isSkippable()) {
+ Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause());
+ ProtocolSwapHandler.swapProtocolIfNeeded(codecDataAttribute, packet);
+ return;
+ } else {
+ throwable = throwable.getCause();
+ }
+ }
+ // Paper end
if (throwable instanceof SkipPacketException) {
Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause());
} else {
diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/java/net/minecraft/network/PacketEncoder.java
index d506995a54a22efb923c9f3f4c4e065b8d2804f8..bf0419ccc0f4ceacd4636ffc21c247c5f600b86e 100644
--- a/src/main/java/net/minecraft/network/PacketEncoder.java
+++ b/src/main/java/net/minecraft/network/PacketEncoder.java
@@ -41,7 +41,7 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
int j = friendlyByteBuf.writerIndex();
packet.write(friendlyByteBuf);
int k = friendlyByteBuf.writerIndex() - j;
- if (k > 8388608) {
+ if (false && k > 8388608) { // Paper - disable
throw new IllegalArgumentException("Packet too big (is " + k + ", should be less than 8388608): " + packet);
}
@@ -54,10 +54,35 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
throw var13;
} finally {
+ // Paper start
+ int packetLength = friendlyByteBuf.readableBytes();
+ if (packetLength > MAX_PACKET_SIZE) {
+ throw new PacketTooLargeException(packet, this.codecKey, packetLength);
+ }
+ // Paper end
ProtocolSwapHandler.swapProtocolIfNeeded(attribute, packet);
}
}
}
}
+
+ // Paper start
+ private static int MAX_PACKET_SIZE = 8388608;
+
+ public static class PacketTooLargeException extends RuntimeException {
+ private final Packet<?> packet;
+ public final AttributeKey<ConnectionProtocol.CodecData<?>> codecKey;
+
+ PacketTooLargeException(Packet<?> packet, AttributeKey<ConnectionProtocol.CodecData<?>> codecKey, int packetLength) {
+ super("PacketTooLarge - " + packet.getClass().getSimpleName() + " is " + packetLength + ". Max is " + MAX_PACKET_SIZE);
+ this.packet = packet;
+ this.codecKey = codecKey;
+ }
+
+ public Packet<?> getPacket() {
+ return this.packet;
+ }
+ }
+ // Paper end
}
diff --git a/src/main/java/net/minecraft/network/protocol/Packet.java b/src/main/java/net/minecraft/network/protocol/Packet.java
index e2babff277d45d15b9a2532f7629251efa1ef92d..84e167701b0ceebbdbd2a95af47c5e3048a48050 100644
--- a/src/main/java/net/minecraft/network/protocol/Packet.java
+++ b/src/main/java/net/minecraft/network/protocol/Packet.java
@@ -10,6 +10,12 @@ public interface Packet<T extends PacketListener> {
void handle(T listener);
+ // Paper start
+ default boolean packetTooLarge(net.minecraft.network.Connection manager) {
+ return false;
+ }
+ // Paper end
+
default boolean isSkippable() {
return false;
}
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
index 48be6902fb1de7e01bc2f25fb837d2d4053d22ca..68bf2c17f9d80f9d024021f664515eb67097e3f8 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
@@ -31,6 +31,16 @@ public class ClientboundContainerSetContentPacket implements Packet<ClientGamePa
this.carriedItem = buf.readItem();
}
+ // Paper start
+ @Override
+ public boolean packetTooLarge(net.minecraft.network.Connection manager) {
+ for (int i = 0 ; i < this.items.size() ; i++) {
+ manager.send(new ClientboundContainerSetSlotPacket(this.containerId, this.stateId, i, this.items.get(i)));
+ }
+ return true;
+ }
+ // Paper end
+
@Override
public void write(FriendlyByteBuf buf) {
buf.writeByte(this.containerId);
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
index 94df9aca2b04d1b0559f70186f54ffc5421cf4e2..7ffade7c19aab8c50fcb4aeb5ffd657c6c73cd94 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
@@ -50,7 +50,7 @@ public class ClientboundLevelChunkPacketData {
throw new RuntimeException("Can't read heightmap in packet for [" + x + ", " + z + "]");
} else {
int i = buf.readVarInt();
- if (i > 2097152) {
+ if (i > 2097152) { // Paper - diff on change - if this changes, update PacketEncoder
throw new RuntimeException("Chunk Packet trying to allocate too much memory on read.");
} else {
this.buffer = new byte[i];