浏览代码

[Net] ENet poll now only service the connection once.

It used to call `enet_host_service` until all events were consumed, but
that also meant constantly polling the connection leading to potentially
unbounded processing time.

It now only service the connection once, and instead consumes all the
retrieved events via `enet_host_check_events`.
Jordan Schidlowsky 4 年之前
父节点
当前提交
3b43dfee11
共有 1 个文件被更改,包括 15 次插入12 次删除
  1. 15 12
      modules/enet/networked_multiplayer_enet.cpp

+ 15 - 12
modules/enet/networked_multiplayer_enet.cpp

@@ -217,20 +217,23 @@ void NetworkedMultiplayerENet::poll() {
 
 
 	_pop_current_packet();
 	_pop_current_packet();
 
 
+	if (!host || !active) { // Might be disconnected
+		return;
+	}
+
 	ENetEvent event;
 	ENetEvent event;
-	/* Keep servicing until there are no available events left in queue. */
-	while (true) {
-		if (!host || !active) { // Might have been disconnected while emitting a notification
-			return;
-		}
+	int ret = enet_host_service(host, &event, 0);
 
 
-		int ret = enet_host_service(host, &event, 0);
+	if (ret < 0) {
+		ERR_FAIL_MSG("Enet host service error");
+	} else if (ret == 0) {
+		return; // No events
+	}
 
 
-		if (ret < 0) {
-			// Error, do something?
-			break;
-		} else if (ret == 0) {
-			break;
+	/* Keep servicing until there are no available events left in the queue. */
+	do {
+		if (!host || !active) { // Check again after every event
+			return;
 		}
 		}
 
 
 		switch (event.type) {
 		switch (event.type) {
@@ -436,7 +439,7 @@ void NetworkedMultiplayerENet::poll() {
 				// Do nothing
 				// Do nothing
 			} break;
 			} break;
 		}
 		}
-	}
+	} while (enet_host_check_events(host, &event) > 0);
 }
 }
 
 
 bool NetworkedMultiplayerENet::is_server() const {
 bool NetworkedMultiplayerENet::is_server() const {