Optimize Network Manager to not need synchronization

Removes synchronization from sending packets
Makes normal packet sends no longer need to be wrapped and queued like it use to work.
Adds more packet queue immunities on top of keep alive to let the following scenarios go out
without delay:
  - Keep Alive
  - Chat
  - Kick
  - All of the packets during the Player Joined World event

Hoping that latter one helps join timeout issues more too for slow connections.

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

This may provide a decent performance improvement because thread synchronization incurs a cache reset
so by avoiding ever entering a synchronized block, we get to avoid that, and packet sending is a really
hot activity.
This commit is contained in:
Aikar
2020-05-06 05:22:03 -04:00
parent 30d007b163
commit fd5c98a9ef
141 changed files with 604 additions and 505 deletions

View File

@@ -1219,63 +1219,8 @@ index 2c1d1b1a556..2c7872bd051 100644
int j = along.length * 64 / 4096;
if (this.h == this.b) {
diff --git a/src/main/java/net/minecraft/server/NetworkManager.java b/src/main/java/net/minecraft/server/NetworkManager.java
index 02a9f3d5fa4..55441e10023 100644
--- a/src/main/java/net/minecraft/server/NetworkManager.java
+++ b/src/main/java/net/minecraft/server/NetworkManager.java
@@ -0,0 +0,0 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
public void sendPacket(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
// Paper start - handle oversized packets better
+ // Special case keepalive, allow it to go out of queue order
+ if (packet instanceof PacketPlayOutKeepAlive && this.isConnected()) {
+ 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();
@@ -0,0 +0,0 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
}
- private void sendPacketQueue() { this.o(); } // Paper - OBFHELPER
- private void o() {
+ // Paper start - Async-Anti-Xray - Stop dispatching further packets and return false if the peeked packet is a chunk packet which is not ready
+ private boolean sendPacketQueue() { return this.o(); } // OBFHELPER // void -> boolean
+ private boolean o() { // void -> boolean
if (this.channel != null && this.channel.isOpen()) {
Queue queue = this.packetQueue;
synchronized (this.packetQueue) {
- NetworkManager.QueuedPacket networkmanager_queuedpacket;
-
- while ((networkmanager_queuedpacket = (NetworkManager.QueuedPacket) this.packetQueue.poll()) != null) {
- this.b(networkmanager_queuedpacket.a, networkmanager_queuedpacket.b);
+ while (!this.packetQueue.isEmpty()) {
+ NetworkManager.QueuedPacket networkmanager_queuedpacket = (NetworkManager.QueuedPacket) queue.peek(); // poll -> peek
+
+ if (networkmanager_queuedpacket != null) { // Fix NPE (Spigot bug caused by handleDisconnection())
+ if (networkmanager_queuedpacket.getPacket() instanceof PacketPlayOutMapChunk && !((PacketPlayOutMapChunk) networkmanager_queuedpacket.getPacket()).isReady()) { // Check if the peeked packet is a chunk packet which is not ready
+ return false; // Return false if the peeked packet is a chunk packet which is not ready
+ } else {
+ queue.poll(); // poll here
+ this.dispatchPacket(networkmanager_queuedpacket.getPacket(), networkmanager_queuedpacket.getGenericFutureListener()); // dispatch the packet
+ }
+ }
}
}
}
+
+ return true; // Return true if all packets were dispatched
}
+ // Paper end
public void a() {
this.o();
diff --git a/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java b/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
index 23223f3f452..0d485064cac 100644
index 23223f3f452..9b608d73869 100644
--- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
+++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
@@ -0,0 +0,0 @@
@@ -1330,18 +1275,18 @@ index 23223f3f452..0d485064cac 100644
+ }
+ // Paper start - Async-Anti-Xray - Getter and Setter for the ready flag
+ @Override
+ public boolean isReady() {
+ return this.ready;
}
+ }
+
+ public void setReady(boolean ready) {
+ this.ready = ready;
+ }
}
+ // Paper end
+
@Override
public void a(PacketDataSerializer packetdataserializer) throws IOException {
this.a = packetdataserializer.readInt();
@@ -0,0 +0,0 @@ public class PacketPlayOutMapChunk implements Packet<PacketListenerPlayOut> {
return bytebuf;
}