From e2027c9479355480406bdd0909d678380f88e2d1 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 25 Jul 2019 07:57:09 +0300
Subject: [PATCH] Add net phasing

---
 components/net/cpp/include/ftl/net/universe.hpp | 1 +
 components/net/cpp/src/universe.cpp             | 9 ++++++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/components/net/cpp/include/ftl/net/universe.hpp b/components/net/cpp/include/ftl/net/universe.hpp
index 0fec5b023..8cf1c462f 100644
--- a/components/net/cpp/include/ftl/net/universe.hpp
+++ b/components/net/cpp/include/ftl/net/universe.hpp
@@ -216,6 +216,7 @@ class Universe : public ftl::Configurable {
 	ftl::net::Dispatcher disp_;
 	std::thread thread_;
 	std::list<ReconnectInfo> reconnects_;
+	size_t phase_;
 
 	struct ConnHandler {
 		callback_t id;
diff --git a/components/net/cpp/src/universe.cpp b/components/net/cpp/src/universe.cpp
index bc4f24fca..541131085 100644
--- a/components/net/cpp/src/universe.cpp
+++ b/components/net/cpp/src/universe.cpp
@@ -24,12 +24,12 @@ using ftl::net::callback_t;
 
 callback_t ftl::net::Universe::cbid__ = 0;
 
-Universe::Universe() : Configurable(), active_(true), this_peer(ftl::net::this_peer), thread_(Universe::__start, this) {
+Universe::Universe() : Configurable(), active_(true), this_peer(ftl::net::this_peer), thread_(Universe::__start, this), phase_(0) {
 	_installBindings();
 }
 
 Universe::Universe(nlohmann::json &config) :
-		Configurable(config), active_(true), this_peer(ftl::net::this_peer), thread_(Universe::__start, this) {
+		Configurable(config), active_(true), this_peer(ftl::net::this_peer), thread_(Universe::__start, this), phase_(0) {
 
 	_installBindings();
 }
@@ -305,7 +305,9 @@ void Universe::_run() {
 			SHARED_LOCK(net_mutex_, lk);
 
 			// Also check each clients socket to see if any messages or errors are waiting
-			for (auto s : peers_) {
+			for (size_t p=0; p<peers_.size(); ++p) {
+				auto s = peers_[(p+phase_)%peers_.size()];
+
 				if (s != NULL && s->isValid()) {
 					// Note: It is possible that the socket becomes invalid after check but before
 					// looking at the FD sets, therefore cache the original socket
@@ -323,6 +325,7 @@ void Universe::_run() {
 					}
 				}
 			}
+			++phase_;
 		}
 	}
 }
-- 
GitLab