3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-15 19:10:09 +01:00
Paper/patches/unapplied/server/0489-Optimize-NetworkManager-Exception-Handling.patch

80 Zeilen
4.3 KiB
Diff

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Steinborn <git@steinborn.me>
Date: Sun, 5 Jul 2020 22:38:18 -0400
Subject: [PATCH] Optimize NetworkManager Exception Handling
diff --git a/src/main/java/net/minecraft/network/ConnectionProtocol.java b/src/main/java/net/minecraft/network/ConnectionProtocol.java
index 6611aebafb14b83bce3eeb87701e2edc8a0828ab..bb3b450c7e81ba47e1e8b2244d331f2ce44a7ee1 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/network/ConnectionProtocol.java
+++ b/src/main/java/net/minecraft/network/ConnectionProtocol.java
2021-06-14 11:46:59 +02:00
@@ -275,6 +275,7 @@ public enum ConnectionProtocol {
2021-06-11 14:02:28 +02:00
@Nullable
2021-06-14 11:46:59 +02:00
public Packet<?> createPacket(int id, FriendlyByteBuf buf) {
+ if (id < 0 || id >= this.idToDeserializer.size()) return null; // Paper
Function<FriendlyByteBuf, ? extends Packet<T>> function = this.idToDeserializer.get(id);
return function != null ? function.apply(buf) : null;
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/network/Varint21FrameDecoder.java b/src/main/java/net/minecraft/network/Varint21FrameDecoder.java
2021-06-14 11:46:59 +02:00
index 5356f6484751e4b4740720aecac90bdfe044283b..ed54479b14dcfc736ac90749106557f0ff537550 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/network/Varint21FrameDecoder.java
+++ b/src/main/java/net/minecraft/network/Varint21FrameDecoder.java
2021-06-14 11:46:59 +02:00
@@ -8,9 +8,20 @@ import io.netty.handler.codec.CorruptedFrameException;
import java.util.List;
2021-06-11 14:02:28 +02:00
public class Varint21FrameDecoder extends ByteToMessageDecoder {
+ private final byte[] lenBuf = new byte[3]; // Paper
2021-06-14 11:46:59 +02:00
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
2021-06-11 14:02:28 +02:00
+ // Paper start - if channel is not active just discard the packet
2021-06-14 11:46:59 +02:00
+ if (!channelHandlerContext.channel().isActive()) {
+ byteBuf.skipBytes(byteBuf.readableBytes());
2021-06-11 14:02:28 +02:00
+ return;
+ }
+ // Paper end
2021-06-14 11:46:59 +02:00
byteBuf.markReaderIndex();
2021-06-11 14:02:28 +02:00
+ // Paper start - reuse temporary length buffer
+ byte[] abyte = lenBuf;
+ java.util.Arrays.fill(abyte, (byte) 0);
+ // Paper end
2021-06-14 11:46:59 +02:00
byte[] bs = new byte[3];
2021-06-11 14:02:28 +02:00
2021-06-14 11:46:59 +02:00
for(int i = 0; i < bs.length; ++i) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/network/protocol/PacketUtils.java b/src/main/java/net/minecraft/network/protocol/PacketUtils.java
2021-06-14 16:41:34 +02:00
index 449f1b2f5dca350dc0912e14c8c2bf3eb4652b92..bcf53ec07b8eeec7a88fb67e6fb908362e6f51b0 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/network/protocol/PacketUtils.java
+++ b/src/main/java/net/minecraft/network/protocol/PacketUtils.java
@@ -1,6 +1,9 @@
package net.minecraft.network.protocol;
+import net.minecraft.network.Connection;
import net.minecraft.network.PacketListener;
+import net.minecraft.network.chat.TextComponent;
+import net.minecraft.network.protocol.game.ClientboundDisconnectPacket;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import co.aikar.timings.MinecraftTimings; // Paper
2021-06-14 11:46:59 +02:00
@@ -32,6 +35,21 @@ public class PacketUtils {
2021-06-11 14:02:28 +02:00
try (Timing ignored = timing.startTiming()) { // Paper - timings
packet.handle(listener);
} // Paper - timings
+ // Paper start
+ catch (Exception e) {
2021-06-14 16:41:34 +02:00
+ Connection networkmanager = listener.getConnection();
2021-06-11 14:02:28 +02:00
+ if (networkmanager.getPlayer() != null) {
+ LOGGER.error("Error whilst processing packet {} for {}[{}]", packet, networkmanager.getPlayer().getScoreboardName(), networkmanager.getRemoteAddress(), e);
+ } else {
+ LOGGER.error("Error whilst processing packet {} for connection from {}", packet, networkmanager.getRemoteAddress(), e);
+ }
+ TextComponent error = new TextComponent("Packet processing error");
+ networkmanager.send(new ClientboundDisconnectPacket(error), (future) -> {
+ networkmanager.disconnect(error);
+ });
+ networkmanager.setReadOnly();
+ }
+ // Paper end
} else {
2021-06-14 11:46:59 +02:00
PacketUtils.LOGGER.debug("Ignoring packet due to disconnection: {}", packet);
2021-06-11 14:02:28 +02:00
}