2020-05-06 09:48:49 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2020-05-06 09:22:03 +00:00
From: Aikar <aikar@aikar.co>
Date: Wed, 6 May 2020 04:53:35 -0400
Subject: [PATCH] Optimize Network Manager and add advanced packet support
Adds ability for 1 packet to bundle other packets to follow it
2020-05-07 05:32:02 +00:00
Adds ability for a packet to delay sending more packets until a state is ready.
2020-05-06 09:22:03 +00:00
Removes synchronization from sending packets
Removes processing packet queue off of main thread
- for the few cases where it is allowed, order is not necessary nor
should it even be happening concurrently in first place (handshaking/login/status)
Ensures packets sent asynchronously are dispatched on main thread
This helps ensure safety for ProtocolLib as packet listeners
are commonly accessing world state. This will allow you to schedule
a packet to be sent async, but itll be dispatched sync for packet
listeners to process.
This should solve some deadlock risks
2020-06-20 08:37:17 +00:00
Also adds Netty Channel Flush Consolidation to reduce the amount of flushing
Also avoids spamming closed channel exception by rechecking closed state in dispatch
and then catch exceptions and close if they fire.
2020-05-06 09:22:03 +00:00
Part of this commit was authored by: Spottedleaf
2021-03-16 07:19:45 +00:00
diff --git a/src/main/java/net/minecraft/network/NetworkManager.java b/src/main/java/net/minecraft/network/NetworkManager.java
2021-03-16 13:04:28 +00:00
index 6d40ade5a52383ed86d28d272c3dc83dbdcbd218..8e591f75b2c80fdb7e012574e9d7c348f1ffc90d 100644
2021-03-16 07:19:45 +00:00
--- a/src/main/java/net/minecraft/network/NetworkManager.java
+++ b/src/main/java/net/minecraft/network/NetworkManager.java
2021-03-16 13:04:28 +00:00
@@ -25,8 +25,15 @@ import net.minecraft.network.chat.ChatMessage;
import net.minecraft.network.chat.IChatBaseComponent;
import net.minecraft.network.protocol.EnumProtocolDirection;
import net.minecraft.network.protocol.Packet;
+import net.minecraft.network.protocol.game.PacketPlayOutBoss;
+import net.minecraft.network.protocol.game.PacketPlayOutChat;
+import net.minecraft.network.protocol.game.PacketPlayOutKeepAlive;
import net.minecraft.network.protocol.game.PacketPlayOutKickDisconnect;
+import net.minecraft.network.protocol.game.PacketPlayOutTabComplete;
+import net.minecraft.network.protocol.game.PacketPlayOutTitle;
import net.minecraft.server.CancelledPacketHandleException;
+import net.minecraft.server.MCUtil;
+import net.minecraft.server.level.EntityPlayer;
import net.minecraft.server.network.LoginListener;
import net.minecraft.server.network.PlayerConnection;
import net.minecraft.util.LazyInitVar;
@@ -75,6 +82,10 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
2020-05-06 09:22:03 +00:00
public int protocolVersion;
public java.net.InetSocketAddress virtualHost;
private static boolean enableExplicitFlush = Boolean.getBoolean("paper.explicit-flush");
+ // Optimize network
+ boolean isPending = true;
+ boolean queueImmunity = false;
+ EnumProtocol protocol;
// Paper end
public NetworkManager(EnumProtocolDirection enumprotocoldirection) {
2021-03-16 13:04:28 +00:00
@@ -98,6 +109,7 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
2020-05-06 09:22:03 +00:00
}
public void setProtocol(EnumProtocol enumprotocol) {
+ protocol = enumprotocol; // Paper
this.channel.attr(NetworkManager.c).set(enumprotocol);
this.channel.config().setAutoRead(true);
NetworkManager.LOGGER.debug("Enabled auto read");
2021-03-16 13:04:28 +00:00
@@ -168,19 +180,84 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
2020-06-25 14:09:55 +00:00
Validate.notNull(packetlistener, "packetListener", new Object[0]);
2020-05-06 09:22:03 +00:00
this.packetListener = packetlistener;
}
+ // Paper start
2020-07-06 04:27:37 +00:00
+ EntityPlayer getPlayer() {
2020-05-17 01:38:19 +00:00
+ if (packetListener instanceof PlayerConnection) {
+ return ((PlayerConnection) packetListener).player;
+ } else {
+ return null;
+ }
+ }
2020-05-06 09:22:03 +00:00
+ private static class InnerUtil { // Attempt to hide these methods from ProtocolLib so it doesn't accidently pick them up.
+ private static java.util.List<Packet> buildExtraPackets(Packet packet) {
+ java.util.List<Packet> extra = packet.getExtraPackets();
+ if (extra == null || extra.isEmpty()) {
+ return null;
+ }
+ java.util.List<Packet> ret = new java.util.ArrayList<>(1 + extra.size());
+ buildExtraPackets0(extra, ret);
+ return ret;
+ }
+
+ private static void buildExtraPackets0(java.util.List<Packet> extraPackets, java.util.List<Packet> into) {
+ for (Packet extra : extraPackets) {
+ into.add(extra);
+ java.util.List<Packet> extraExtra = extra.getExtraPackets();
+ if (extraExtra != null && !extraExtra.isEmpty()) {
+ buildExtraPackets0(extraExtra, into);
+ }
+ }
+ }
2020-05-06 15:07:51 +00:00
+ // Paper start
+ private static boolean canSendImmediate(NetworkManager networkManager, Packet<?> packet) {
2020-06-28 22:44:34 +00:00
+ return networkManager.isPending || networkManager.protocol != EnumProtocol.PLAY ||
2020-05-06 15:07:51 +00:00
+ packet instanceof PacketPlayOutKeepAlive ||
+ packet instanceof PacketPlayOutChat ||
2020-12-16 22:16:03 +00:00
+ packet instanceof PacketPlayOutTabComplete ||
+ packet instanceof PacketPlayOutTitle ||
+ packet instanceof PacketPlayOutBoss;
2020-05-06 15:07:51 +00:00
+ }
+ // Paper end
2020-05-06 09:22:03 +00:00
+ }
+ // Paper end
public void sendPacket(Packet<?> packet) {
this.sendPacket(packet, (GenericFutureListener) null);
}
public void sendPacket(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
- if (this.isConnected()) {
2020-08-25 02:22:08 +00:00
- this.p();
2020-05-06 09:22:03 +00:00
- this.b(packet, genericfuturelistener);
- } else {
- this.packetQueue.add(new NetworkManager.QueuedPacket(packet, genericfuturelistener));
+ // Paper start - handle oversized packets better
+ boolean connected = this.isConnected();
2020-05-07 05:32:02 +00:00
+ if (!connected && !preparing) {
+ return; // Do nothing
+ }
2020-05-17 01:38:19 +00:00
+ packet.onPacketDispatch(getPlayer());
2020-05-06 15:07:51 +00:00
+ if (connected && (InnerUtil.canSendImmediate(this, packet) || (
2020-05-06 09:22:03 +00:00
+ MCUtil.isMainThread() && packet.isReady() && this.packetQueue.isEmpty() &&
+ (packet.getExtraPackets() == null || packet.getExtraPackets().isEmpty())
+ ))) {
+ this.dispatchPacket(packet, genericfuturelistener);
+ return;
}
+ // write the packets to the queue, then flush - antixray hooks there already
+ java.util.List<Packet> extraPackets = InnerUtil.buildExtraPackets(packet);
+ boolean hasExtraPackets = extraPackets != null && !extraPackets.isEmpty();
+ if (!hasExtraPackets) {
+ this.packetQueue.add(new NetworkManager.QueuedPacket(packet, genericfuturelistener));
+ } else {
+ java.util.List<NetworkManager.QueuedPacket> packets = new java.util.ArrayList<>(1 + extraPackets.size());
+ packets.add(new NetworkManager.QueuedPacket(packet, null)); // delay the future listener until the end of the extra packets
2020-06-20 08:37:17 +00:00
+
2020-05-06 09:22:03 +00:00
+ for (int i = 0, len = extraPackets.size(); i < len;) {
+ Packet extra = extraPackets.get(i);
+ boolean end = ++i == len;
+ packets.add(new NetworkManager.QueuedPacket(extra, end ? genericfuturelistener : null)); // append listener to the end
+ }
2020-06-20 08:37:17 +00:00
2020-05-06 09:22:03 +00:00
+ this.packetQueue.addAll(packets); // atomic
+ }
+ this.sendPacketQueue();
+ // Paper end
}
private void dispatchPacket(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> genericFutureListener) { this.b(packet, genericFutureListener); } // Paper - OBFHELPER
2021-03-16 13:04:28 +00:00
@@ -194,51 +271,116 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
2020-06-20 08:37:17 +00:00
this.channel.config().setAutoRead(false);
}
+ EntityPlayer player = getPlayer(); // Paper
if (this.channel.eventLoop().inEventLoop()) {
if (enumprotocol != enumprotocol1) {
this.setProtocol(enumprotocol);
}
+ // Paper start
+ if (!isConnected()) {
+ packet.onPacketDispatchFinish(player, null);
+ return;
+ }
+ try {
+ // Paper end
ChannelFuture channelfuture = this.channel.writeAndFlush(packet);
2020-05-17 01:38:19 +00:00
if (genericfuturelistener != null) {
channelfuture.addListener(genericfuturelistener);
}
+ // Paper start
+ if (packet.hasFinishListener()) {
2020-06-20 08:37:17 +00:00
+ channelfuture.addListener((ChannelFutureListener) channelFuture -> packet.onPacketDispatchFinish(player, channelFuture));
2020-05-17 01:38:19 +00:00
+ }
+ // Paper end
channelfuture.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
2020-06-20 08:37:17 +00:00
+ // Paper start
+ } catch (Exception e) {
+ LOGGER.error("NetworkException: " + player, e);
+ close(new ChatMessage("disconnect.genericReason", "Internal Exception: " + e.getMessage()));;
+ packet.onPacketDispatchFinish(player, null);
+ }
+ // Paper end
2020-05-17 01:38:19 +00:00
} else {
2020-06-20 08:37:17 +00:00
this.channel.eventLoop().execute(() -> {
if (enumprotocol != enumprotocol1) {
this.setProtocol(enumprotocol);
}
+ // Paper start
+ if (!isConnected()) {
+ packet.onPacketDispatchFinish(player, null);
+ return;
+ }
+ try {
+ // Paper end
ChannelFuture channelfuture1 = this.channel.writeAndFlush(packet);
+
2020-05-17 01:38:19 +00:00
if (genericfuturelistener != null) {
channelfuture1.addListener(genericfuturelistener);
}
+ // Paper start
+ if (packet.hasFinishListener()) {
2020-06-20 08:37:17 +00:00
+ channelfuture1.addListener((ChannelFutureListener) channelFuture -> packet.onPacketDispatchFinish(player, channelFuture));
2020-05-17 01:38:19 +00:00
+ }
+ // Paper end
channelfuture1.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
2020-06-20 08:37:17 +00:00
+ // Paper start
+ } catch (Exception e) {
+ LOGGER.error("NetworkException: " + player, e);
+ close(new ChatMessage("disconnect.genericReason", "Internal Exception: " + e.getMessage()));;
+ packet.onPacketDispatchFinish(player, null);
+ }
+ // Paper end
2020-05-17 01:38:19 +00:00
});
2020-06-20 08:37:17 +00:00
}
2020-05-06 09:22:03 +00:00
}
2020-08-25 02:22:08 +00:00
- private void sendPacketQueue() { this.p(); } // Paper - OBFHELPER
- private void p() {
2020-05-06 09:22:03 +00:00
- if (this.channel != null && this.channel.isOpen()) {
- Queue queue = this.packetQueue;
-
2020-08-25 02:22:08 +00:00
+ // Paper start - rewrite this to be safer if ran off main thread
+ private boolean sendPacketQueue() { return this.p(); } // OBFHELPER // void -> boolean
+ private boolean p() { // void -> boolean
2020-05-06 09:22:03 +00:00
+ if (!isConnected()) {
+ return true;
+ }
+ if (MCUtil.isMainThread()) {
+ return processQueue();
+ } else if (isPending) {
+ // Should only happen during login/status stages
synchronized (this.packetQueue) {
- NetworkManager.QueuedPacket networkmanager_queuedpacket;
-
- while ((networkmanager_queuedpacket = (NetworkManager.QueuedPacket) this.packetQueue.poll()) != null) {
- this.b(networkmanager_queuedpacket.a, networkmanager_queuedpacket.b);
- }
+ return this.processQueue();
+ }
+ }
+ return false;
+ }
+ private boolean processQueue() {
+ if (this.packetQueue.isEmpty()) return true;
+ // If we are on main, we are safe here in that nothing else should be processing queue off main anymore
+ // But if we are not on main due to login/status, the parent is synchronized on packetQueue
+ java.util.Iterator<QueuedPacket> iterator = this.packetQueue.iterator();
+ while (iterator.hasNext()) {
+ NetworkManager.QueuedPacket queued = iterator.next(); // poll -> peek
+
+ // Fix NPE (Spigot bug caused by handleDisconnection())
+ if (queued == null) {
+ return true;
+ }
+ Packet<?> packet = queued.getPacket();
+ if (!packet.isReady()) {
+ return false;
+ } else {
+ iterator.remove();
+ this.dispatchPacket(packet, queued.getGenericFutureListener());
}
}
+ return true;
}
+ // Paper end
public void a() {
2020-08-25 02:22:08 +00:00
this.p();
2021-03-16 13:04:28 +00:00
@@ -271,9 +413,21 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
2020-05-07 05:32:02 +00:00
return this.socketAddress;
}
2020-05-17 01:38:19 +00:00
+ // Paper start
+ public void clearPacketQueue() {
+ EntityPlayer player = getPlayer();
+ packetQueue.forEach(queuedPacket -> {
+ Packet<?> packet = queuedPacket.getPacket();
+ if (packet.hasFinishListener()) {
+ packet.onPacketDispatchFinish(player, null);
+ }
+ });
+ packetQueue.clear();
+ } // Paper end
2020-05-06 09:22:03 +00:00
public void close(IChatBaseComponent ichatbasecomponent) {
// Spigot Start
this.preparing = false;
2020-05-07 05:32:02 +00:00
+ clearPacketQueue(); // Paper
2020-05-06 09:22:03 +00:00
// Spigot End
if (this.channel.isOpen()) {
this.channel.close(); // We can't wait as this may be called from an event loop.
2021-03-16 13:04:28 +00:00
@@ -341,7 +495,7 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
2020-06-12 17:21:29 +00:00
public void handleDisconnection() {
if (this.channel != null && !this.channel.isOpen()) {
if (this.o) {
- NetworkManager.LOGGER.warn("handleDisconnection() called twice");
+ //NetworkManager.LOGGER.warn("handleDisconnection() called twice"); // Paper - Do not log useless message
} else {
this.o = true;
2020-08-25 02:22:08 +00:00
if (this.k() != null) {
2021-03-16 13:04:28 +00:00
@@ -349,7 +503,7 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
2020-08-25 02:22:08 +00:00
} else if (this.j() != null) {
this.j().a(new ChatMessage("multiplayer.disconnect.generic"));
2020-05-07 05:32:02 +00:00
}
- this.packetQueue.clear(); // Free up packet queue.
+ clearPacketQueue(); // Paper
// Paper start - Add PlayerConnectionCloseEvent
2020-08-25 02:22:08 +00:00
final PacketListener packetListener = this.j();
2020-05-07 05:32:02 +00:00
if (packetListener instanceof PlayerConnection) {
2021-03-16 07:19:45 +00:00
diff --git a/src/main/java/net/minecraft/network/protocol/Packet.java b/src/main/java/net/minecraft/network/protocol/Packet.java
index 0783b0777c8d7788bbf6780b464b709bf6dc2191..b644c91cecd8a347319dfe8c8923fd05919a9795 100644
--- a/src/main/java/net/minecraft/network/protocol/Packet.java
+++ b/src/main/java/net/minecraft/network/protocol/Packet.java
2020-06-26 06:29:44 +00:00
@@ -1,5 +1,6 @@
2021-03-16 07:19:45 +00:00
package net.minecraft.network.protocol;
2020-06-26 06:29:44 +00:00
+import io.netty.channel.ChannelFuture; // Paper
import java.io.IOException;
2021-03-16 07:19:45 +00:00
import net.minecraft.network.PacketDataSerializer;
import net.minecraft.network.PacketListener;
@@ -13,6 +14,20 @@ public interface Packet<T extends PacketListener> {
2020-05-06 09:22:03 +00:00
void a(T t0);
// Paper start
2020-05-17 01:38:19 +00:00
+
+ /**
+ * @param player Null if not at PLAY stage yet
+ */
+ default void onPacketDispatch(@javax.annotation.Nullable EntityPlayer player) {}
+
+ /**
+ * @param player Null if not at PLAY stage yet
+ * @param future Can be null if packet was cancelled
+ */
2020-06-26 06:29:44 +00:00
+ default void onPacketDispatchFinish(@javax.annotation.Nullable EntityPlayer player, @javax.annotation.Nullable ChannelFuture future) {}
2020-05-17 01:38:19 +00:00
+ default boolean hasFinishListener() { return false; }
2020-05-06 09:22:03 +00:00
+ default boolean isReady() { return true; }
+ default java.util.List<Packet> getExtraPackets() { return null; }
default boolean packetTooLarge(NetworkManager manager) {
return false;
}
2021-03-16 07:19:45 +00:00
diff --git a/src/main/java/net/minecraft/server/network/ServerConnection.java b/src/main/java/net/minecraft/server/network/ServerConnection.java
index d992cb5cd827e0fe655809e1088939cdad9c2301..dc362724ea0cc1b2f9d9ceffff483217b4356c40 100644
--- a/src/main/java/net/minecraft/server/network/ServerConnection.java
+++ b/src/main/java/net/minecraft/server/network/ServerConnection.java
2020-08-25 02:22:08 +00:00
@@ -16,6 +16,7 @@ import io.netty.channel.epoll.EpollServerSocketChannel;
2020-06-26 12:04:38 +00:00
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.flush.FlushConsolidationHandler; // Paper
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.io.IOException;
import java.net.InetAddress;
2021-03-16 07:19:45 +00:00
@@ -54,10 +55,12 @@ public class ServerConnection {
2020-06-20 08:37:17 +00:00
private final List<NetworkManager> connectedChannels = Collections.synchronizedList(Lists.newArrayList());
// Paper start - prevent blocking on adding a new network manager while the server is ticking
private final java.util.Queue<NetworkManager> pending = new java.util.concurrent.ConcurrentLinkedQueue<>();
+ private static final boolean disableFlushConsolidation = Boolean.getBoolean("Paper.disableFlushConsolidate"); // Paper
private void addPending() {
2020-05-06 09:22:03 +00:00
NetworkManager manager = null;
while ((manager = pending.poll()) != null) {
connectedChannels.add(manager);
+ manager.isPending = false;
}
}
// Paper end
2021-03-16 07:19:45 +00:00
@@ -92,6 +95,7 @@ public class ServerConnection {
2020-06-20 08:37:17 +00:00
;
}
2020-06-26 12:04:38 +00:00
+ if (!disableFlushConsolidation) channel.pipeline().addFirst(new FlushConsolidationHandler()); // Paper
2020-06-20 08:37:17 +00:00
channel.pipeline().addLast("timeout", new ReadTimeoutHandler(30)).addLast("legacy_query", new LegacyPingHandler(ServerConnection.this)).addLast("splitter", new PacketSplitter()).addLast("decoder", new PacketDecoder(EnumProtocolDirection.SERVERBOUND)).addLast("prepender", new PacketPrepender()).addLast("encoder", new PacketEncoder(EnumProtocolDirection.CLIENTBOUND));
2020-08-25 02:22:08 +00:00
int j = ServerConnection.this.e.k();
Object object = j > 0 ? new NetworkManagerServer(j) : new NetworkManager(EnumProtocolDirection.SERVERBOUND);