papermc/patches/server/0296-Handle-Large-Packets-disconnecting-client.patch

116 lines
5.6 KiB
Diff
Raw Normal View History

2021-06-11 12:02:28 +00:00
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
2021-06-13 08:26:58 +00:00
index 0c5c62be83223e20f216df84413b8c2438db81ff..8bbf13a7aea1142b3154a1c76837a98aa5ed431d 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/Connection.java
+++ b/src/main/java/net/minecraft/network/Connection.java
2021-06-13 08:26:58 +00:00
@@ -120,6 +120,15 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
2021-06-11 12:02:28 +00:00
}
public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) {
+ // Paper start
2021-06-13 08:26:58 +00:00
+ if (throwable instanceof io.netty.handler.codec.EncoderException && throwable.getCause() instanceof PacketEncoder.PacketTooLargeException) {
2021-06-11 12:02:28 +00:00
+ if (((PacketEncoder.PacketTooLargeException) throwable.getCause()).getPacket().packetTooLarge(this)) {
+ 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
2021-06-13 08:26:58 +00:00
index b8a0c0411fd2caab21672de7f3e721645b61a8ba..c0df136c408d7f001395291d8c80da5b4b54d462 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/PacketEncoder.java
+++ b/src/main/java/net/minecraft/network/PacketEncoder.java
2021-06-13 08:26:58 +00:00
@@ -54,7 +54,31 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
throw var9;
2021-06-11 12:02:28 +00:00
}
}
+
+ // Paper start
2021-06-13 08:26:58 +00:00
+ int packetLength = friendlyByteBuf.readableBytes();
2021-06-11 12:02:28 +00:00
+ if (packetLength > MAX_PACKET_SIZE) {
+ throw new PacketTooLargeException(packet, packetLength);
+ }
+ // Paper end
}
}
}
+
+ // Paper start
+ private static int MAX_PACKET_SIZE = 2097152;
+
+ public static class PacketTooLargeException extends RuntimeException {
+ private final Packet<?> packet;
+
+ PacketTooLargeException(Packet<?> packet, int packetLength) {
+ super("PacketTooLarge - " + packet.getClass().getSimpleName() + " is " + packetLength + ". Max is " + MAX_PACKET_SIZE);
+ this.packet = packet;
+ }
+
+ public Packet<?> getPacket() {
+ return 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
2021-06-13 08:26:58 +00:00
index 10c1f2d8a92f848c3f2be9d1d06fd254978e6dcc..74bfe0d3942259c45702b099efdc4e101a4e3022 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/protocol/Packet.java
+++ b/src/main/java/net/minecraft/network/protocol/Packet.java
2021-06-13 08:26:58 +00:00
@@ -8,6 +8,12 @@ public interface Packet<T extends PacketListener> {
2021-06-11 12:02:28 +00:00
void handle(T listener);
+ // Paper start
2021-06-13 08:26:58 +00:00
+ default boolean packetTooLarge(net.minecraft.network.Connection manager) {
2021-06-11 12:02:28 +00:00
+ 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
2021-06-13 08:26:58 +00:00
index a27d46e90ad04c624d41f6cbededcd3121dfeee8..d5ca796408f975f4f002a5b385574d3516915507 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
2021-06-13 08:26:58 +00:00
@@ -10,6 +10,16 @@ public class ClientboundContainerSetContentPacket implements Packet<ClientGamePa
private final int containerId;
private final List<ItemStack> items;
2021-06-11 12:02:28 +00:00
+ //Paper start
+ @Override
2021-06-13 08:26:58 +00:00
+ public boolean packetTooLarge(net.minecraft.network.Connection manager) {
2021-06-11 12:02:28 +00:00
+ for (int i = 0 ; i < this.items.size() ; i++) {
+ manager.send(new ClientboundContainerSetSlotPacket(this.containerId, i, this.items.get(i)));
+ }
+ return true;
+ }
+ // Paper end
2021-06-13 08:26:58 +00:00
+
2021-06-11 12:02:28 +00:00
public ClientboundContainerSetContentPacket(int syncId, NonNullList<ItemStack> contents) {
2021-06-13 08:26:58 +00:00
this.containerId = syncId;
this.items = NonNullList.withSize(contents.size(), ItemStack.EMPTY);
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
2021-06-13 08:26:58 +00:00
index 1451a98d69b185dd15a2d1d7681bcecb6a4f99c1..96626835fee3c0fdb452acacdc9f737ad90c08de 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
2021-06-13 08:26:58 +00:00
@@ -64,7 +64,7 @@ public class ClientboundLevelChunkPacket implements Packet<ClientGamePacketListe
2021-06-11 12:02:28 +00:00
} else {
2021-06-13 08:26:58 +00:00
this.biomes = buf.readVarIntArray(ChunkBiomeContainer.MAX_SIZE);
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];