From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: William Blake Galbreath Date: Fri, 11 Oct 2019 00:17:39 -0500 Subject: [PATCH] Alternative Keepalive Handling diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index 9f86bff926434b58143522d624ea7d6e044e430d..f563cffabdb9f0dd69e50680fc3e7aae01e8123d 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -259,6 +259,7 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic private long keepAliveTime = Util.getMillis(); private boolean keepAlivePending; private long keepAliveChallenge; + private it.unimi.dsi.fastutil.longs.LongList keepAlives = new it.unimi.dsi.fastutil.longs.LongArrayList(); // Purpur // CraftBukkit start - multithreaded fields private final AtomicInteger chatSpamTickCount = new AtomicInteger(); private final java.util.concurrent.atomic.AtomicInteger tabSpamLimiter = new java.util.concurrent.atomic.AtomicInteger(); // Paper - configurable tab spam limits @@ -410,6 +411,21 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic long currentTime = Util.getMillis(); long elapsedTime = currentTime - this.keepAliveTime; + // Purpur start + if (org.purpurmc.purpur.PurpurConfig.useAlternateKeepAlive) { + if (elapsedTime >= 1000L) { // 1 second + if (!processedDisconnect && keepAlives.size() * 1000L >= KEEPALIVE_LIMIT) { + LOGGER.warn("{} was kicked due to keepalive timeout!", player.getName()); + disconnect(Component.translatable("disconnect.timeout"), org.bukkit.event.player.PlayerKickEvent.Cause.TIMEOUT); + } else { + keepAliveTime = currentTime; // hijack this field for 1 second intervals + keepAlives.add(currentTime); // currentTime is ID + send(new ClientboundKeepAlivePacket(currentTime)); + } + } + } else + // Purpur end + if (this.keepAlivePending) { if (!this.processedDisconnect && elapsedTime >= KEEPALIVE_LIMIT) { // check keepalive limit, don't fire if already disconnected ServerGamePacketListenerImpl.LOGGER.warn("{} was kicked due to keepalive timeout!", this.player.getScoreboardName()); // more info @@ -3487,6 +3503,16 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic @Override public void handleKeepAlive(ServerboundKeepAlivePacket packet) { + // Purpur start + if (org.purpurmc.purpur.PurpurConfig.useAlternateKeepAlive) { + long id = packet.getId(); + if (keepAlives.size() > 0 && keepAlives.contains(id)) { + int ping = (int) (Util.getMillis() - id); + player.latency = (player.latency * 3 + ping) / 4; + keepAlives.clear(); // we got a valid response, lets roll with it and forget the rest + } + } else + // Purpur end //PacketUtils.ensureRunningOnSameThread(packet, this, this.player.getLevel()); // CraftBukkit // Paper - This shouldn't be on the main thread if (this.keepAlivePending && packet.getId() == this.keepAliveChallenge) { int i = (int) (Util.getMillis() - this.keepAliveTime); diff --git a/src/main/java/org/purpurmc/purpur/PurpurConfig.java b/src/main/java/org/purpurmc/purpur/PurpurConfig.java index dd3ed29fdac7ae1f35ecf520f92b9f36ca6fdb98..70d128ed8d19d47056d2f3ba3f75efb12bcb0347 100644 --- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java +++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java @@ -196,6 +196,11 @@ public class PurpurConfig { laggingThreshold = getDouble("settings.lagging-threshold", laggingThreshold); } + public static boolean useAlternateKeepAlive = false; + private static void useAlternateKeepAlive() { + useAlternateKeepAlive = getBoolean("settings.use-alternate-keepalive", useAlternateKeepAlive); + } + public static int barrelRows = 3; public static boolean enderChestSixRows = false; public static boolean enderChestPermissionRows = false;