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
|
2023-03-14 20:54:57 +01:00
|
|
|
index c4c1fc4e082d4d35327735e0704ada4e96195db2..fe66da8bfec647fc0ef5f26476877e6aff6da546 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
|
2023-03-14 20:54:57 +01:00
|
|
|
@@ -329,6 +329,7 @@ public enum ConnectionProtocol implements BundlerInfo.Provider {
|
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
|
2022-11-22 18:50:31 +01:00
|
|
|
index 99b581052f937b0f2d6b5d73de699008c1d51774..81ec2011a93bb94200ad750f4666ba1cd8be11c5 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
|
2022-11-22 18:50:31 +01:00
|
|
|
@@ -8,9 +8,20 @@ import io.netty.handler.codec.CorruptedFrameException;
|
2021-06-14 11:46:59 +02:00
|
|
|
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-11-24 17:06:46 +01:00
|
|
|
+ @Override
|
2021-06-14 11:46:59 +02:00
|
|
|
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();
|
2022-11-22 18:50:31 +01:00
|
|
|
- byte[] bs = new byte[3];
|
2021-06-11 14:02:28 +02:00
|
|
|
+ // Paper start - reuse temporary length buffer
|
2022-11-22 18:50:31 +01:00
|
|
|
+ byte[] bs = lenBuf;
|
|
|
|
+ java.util.Arrays.fill(bs, (byte) 0);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ // Paper end
|
|
|
|
|
2021-06-14 11:46:59 +02:00
|
|
|
for(int i = 0; i < bs.length; ++i) {
|
2022-11-22 18:50:31 +01:00
|
|
|
if (!byteBuf.isReadable()) {
|
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
|
2023-03-14 20:54:57 +01:00
|
|
|
index 4a1148a76020089caf01f888f87afdbb35788dc0..46d9ad9cd8002770c26ed61f98593af71f534e71 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
|
2022-07-28 00:20:14 +02:00
|
|
|
@@ -30,11 +30,15 @@ public class PacketUtils {
|
2022-03-01 06:43:03 +01:00
|
|
|
try (co.aikar.timings.Timing ignored = timing.startTiming()) { // Paper - timings
|
|
|
|
packet.handle(listener);
|
|
|
|
} catch (Exception exception) {
|
|
|
|
- if (listener.shouldPropagateHandlingExceptions()) {
|
|
|
|
- throw exception;
|
|
|
|
+ net.minecraft.network.Connection networkmanager = listener.getConnection();
|
2021-06-11 14:02:28 +02:00
|
|
|
+ if (networkmanager.getPlayer() != null) {
|
2022-03-01 06:43:03 +01:00
|
|
|
+ LOGGER.error("Error whilst processing packet {} for {}[{}]", packet, networkmanager.getPlayer().getScoreboardName(), networkmanager.getRemoteAddress(), exception);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ } else {
|
2022-03-01 06:43:03 +01:00
|
|
|
+ LOGGER.error("Error whilst processing packet {} for connection from {}", packet, networkmanager.getRemoteAddress(), exception);
|
|
|
|
}
|
|
|
|
-
|
|
|
|
- PacketUtils.LOGGER.error("Failed to handle packet {}, suppressing error", packet, exception);
|
2022-06-08 10:45:59 +02:00
|
|
|
+ net.minecraft.network.chat.Component error = net.minecraft.network.chat.Component.literal("Packet processing error");
|
2022-07-28 00:20:14 +02:00
|
|
|
+ networkmanager.send(new net.minecraft.network.protocol.game.ClientboundDisconnectPacket(error), net.minecraft.network.PacketSendListener.thenRun(() -> networkmanager.disconnect(error)));
|
2021-06-11 14:02:28 +02:00
|
|
|
+ networkmanager.setReadOnly();
|
2022-03-01 06:43:03 +01:00
|
|
|
}
|
2021-06-11 14:02:28 +02:00
|
|
|
} else {
|
2021-06-14 11:46:59 +02:00
|
|
|
PacketUtils.LOGGER.debug("Ignoring packet due to disconnection: {}", packet);
|