From 614d000376f7e12e0ec1a7e785478a0f1ebb4b60 Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Wed, 27 Sep 2023 00:31:31 +0200 Subject: [PATCH 1/8] Sleep for 10 ms instead of 1000 ms while waiting for server connection --- client/CServerHandler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index aae41f2f0..dd034d36a 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -263,8 +263,8 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po } catch(std::runtime_error & error) { - logNetwork->warn("\nCannot establish connection. %s Retrying in 1 second", error.what()); - boost::this_thread::sleep_for(boost::chrono::milliseconds(1000)); + logNetwork->warn("\nCannot establish connection. %s Retrying in 10 ms", error.what()); + boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); } } From 9f9930a9dd17e6c632c0555ef7b2465af3fbef9d Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Wed, 27 Sep 2023 15:55:15 +0200 Subject: [PATCH 2/8] CServerHandler::justConnectToServer(): Read settings only once --- client/CServerHandler.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index dd034d36a..0a88eafe8 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -251,14 +251,16 @@ void CServerHandler::startLocalServerAndConnect() void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port) { state = EClientState::CONNECTING; + std::string hostAddress = getHostAddress(); + ui16 hostPort = getHostPort(); + logNetwork->info("Establishing connection..."); while(!c && state != EClientState::CONNECTION_CANCELLED) { try { - logNetwork->info("Establishing connection..."); c = std::make_shared( - addr.size() ? addr : getHostAddress(), - port ? port : getHostPort(), + addr.size() ? addr : hostAddress, + port ? port : hostPort, NAME, uuid); } catch(std::runtime_error & error) From 39427d6658f8e2fe98eb8cb66cced3a56167354a Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Wed, 27 Sep 2023 17:11:19 +0200 Subject: [PATCH 3/8] Use different delay and maximum number of connection attempts for local and remote servers --- client/CServerHandler.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 0a88eafe8..8dc99ae6f 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -251,11 +251,39 @@ void CServerHandler::startLocalServerAndConnect() void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port) { state = EClientState::CONNECTING; + + logNetwork->info("Establishing connection..."); + std::string hostAddress = getHostAddress(); ui16 hostPort = getHostPort(); - logNetwork->info("Establishing connection..."); + + boost::chrono::duration> sleepDuration{}; + int maxConnectionAttempts; + + if(hostAddress == "127.0.0.1") + { + sleepDuration = boost::chrono::milliseconds(10); + maxConnectionAttempts = 100; + } + else + { + // remote server + sleepDuration = boost::chrono::seconds(2); + maxConnectionAttempts = 10; + } + + logNetwork->info("\nWaiting for %d ms between each of the %d attempts to connect", sleepDuration.count(), maxConnectionAttempts); + + uint connectionAttemptCount = 0; while(!c && state != EClientState::CONNECTION_CANCELLED) { + connectionAttemptCount++; + if(connectionAttemptCount > maxConnectionAttempts) + { + logNetwork->error("\nExceeded maximum of %d connection attempts", maxConnectionAttempts); + break; + } + try { c = std::make_shared( @@ -265,8 +293,7 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po } catch(std::runtime_error & error) { - logNetwork->warn("\nCannot establish connection. %s Retrying in 10 ms", error.what()); - boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); + boost::this_thread::sleep_for(sleepDuration); } } From e1401d26c53c6fb084ff7471922b36cca5b22449 Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Wed, 27 Sep 2023 17:55:39 +0200 Subject: [PATCH 4/8] hostAddress can be localhost, return from justConnectToServer() is maximum number of attempts is exceeded --- client/CServerHandler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 8dc99ae6f..2fb03be44 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -260,7 +260,7 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po boost::chrono::duration> sleepDuration{}; int maxConnectionAttempts; - if(hostAddress == "127.0.0.1") + if(hostAddress == "127.0.0.1" || hostAddress == "localhost") { sleepDuration = boost::chrono::milliseconds(10); maxConnectionAttempts = 100; @@ -281,7 +281,7 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po if(connectionAttemptCount > maxConnectionAttempts) { logNetwork->error("\nExceeded maximum of %d connection attempts", maxConnectionAttempts); - break; + return; } try From a66d8ecb8f2e8d04e1bb02989ce2e28a2641134e Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Wed, 27 Sep 2023 19:51:29 +0200 Subject: [PATCH 5/8] Use ui16 instead of uint for connectionAttemptCount to fix MinGW build --- client/CServerHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 2fb03be44..5de41a39f 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -274,7 +274,7 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po logNetwork->info("\nWaiting for %d ms between each of the %d attempts to connect", sleepDuration.count(), maxConnectionAttempts); - uint connectionAttemptCount = 0; + ui16 connectionAttemptCount = 0; while(!c && state != EClientState::CONNECTION_CANCELLED) { connectionAttemptCount++; From b58cca77703beed78a6592fafc57b47344525af9 Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Wed, 27 Sep 2023 19:59:56 +0200 Subject: [PATCH 6/8] 100 connection attempts is not always enough --- client/CServerHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 5de41a39f..a7423b9e9 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -263,7 +263,7 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po if(hostAddress == "127.0.0.1" || hostAddress == "localhost") { sleepDuration = boost::chrono::milliseconds(10); - maxConnectionAttempts = 100; + maxConnectionAttempts = 1000; } else { From c8f1512a3fcc0cace97faec4e3007bdbcfc69358 Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Fri, 6 Oct 2023 23:27:19 +0200 Subject: [PATCH 7/8] Use correct address to decide whether we're connecting to a remote server or a local one --- client/CServerHandler.cpp | 32 +++++++++++++++++++------------- client/CServerHandler.h | 4 ++-- client/mainmenu/CMainMenu.cpp | 4 ++-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index a7423b9e9..8efdbfdd3 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -252,27 +252,33 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po { state = EClientState::CONNECTING; - logNetwork->info("Establishing connection..."); + logNetwork->info("justConnectToServer(%s, %d)", addr, port); + + std::string hostAddressFromSettings = getHostAddressFromSettings(); + ui16 hostPortFromSettings = getHostPortFromSettings(); + + logNetwork->info("Host settings %s:%d", hostAddressFromSettings, hostPortFromSettings); - std::string hostAddress = getHostAddress(); - ui16 hostPort = getHostPort(); + std::string connectionAddress = addr.size() ? addr : hostAddressFromSettings; + ui16 connectionPort = port ? port : hostPortFromSettings; boost::chrono::duration> sleepDuration{}; int maxConnectionAttempts; - if(hostAddress == "127.0.0.1" || hostAddress == "localhost") + if(connectionAddress == "127.0.0.1" || connectionAddress == "localhost") { + logNetwork->info("Local server"); sleepDuration = boost::chrono::milliseconds(10); maxConnectionAttempts = 1000; } else { - // remote server + logNetwork->info("Remote server"); sleepDuration = boost::chrono::seconds(2); maxConnectionAttempts = 10; } - logNetwork->info("\nWaiting for %d ms between each of the %d attempts to connect", sleepDuration.count(), maxConnectionAttempts); + logNetwork->info("Waiting for %d ms between each of the %d attempts to connect", sleepDuration.count(), maxConnectionAttempts); ui16 connectionAttemptCount = 0; while(!c && state != EClientState::CONNECTION_CANCELLED) @@ -287,8 +293,8 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po try { c = std::make_shared( - addr.size() ? addr : hostAddress, - port ? port : hostPort, + connectionAddress, + connectionPort, NAME, uuid); } catch(std::runtime_error & error) @@ -305,12 +311,12 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po c->handler = std::make_shared(&CServerHandler::threadHandleConnection, this); - if(!addr.empty() && addr != getHostAddress()) + if(!addr.empty() && addr != getHostAddressFromSettings()) { Settings serverAddress = settings.write["server"]["server"]; serverAddress->String() = addr; } - if(port && port != getHostPort()) + if(port && port != getHostPortFromSettings()) { Settings serverPort = settings.write["server"]["port"]; serverPort->Integer() = port; @@ -394,7 +400,7 @@ std::string CServerHandler::getDefaultPortStr() return std::to_string(getDefaultPort()); } -std::string CServerHandler::getHostAddress() const +std::string CServerHandler::getHostAddressFromSettings() const { if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool()) return settings["server"]["server"].String(); @@ -405,7 +411,7 @@ std::string CServerHandler::getHostAddress() const return settings["session"]["address"].String(); } -ui16 CServerHandler::getHostPort() const +ui16 CServerHandler::getHostPortFromSettings() const { if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool()) return getDefaultPort(); @@ -995,7 +1001,7 @@ void CServerHandler::threadRunServer() setThreadName("runServer"); const std::string logName = (VCMIDirs::get().userLogsPath() / "server_log.txt").string(); std::string comm = VCMIDirs::get().serverPath().string() - + " --port=" + std::to_string(getHostPort()) + + " --port=" + std::to_string(getHostPortFromSettings()) + " --run-by-client" + " --uuid=" + uuid; if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool()) diff --git a/client/CServerHandler.h b/client/CServerHandler.h index 4572d5fb8..1607e97cf 100644 --- a/client/CServerHandler.h +++ b/client/CServerHandler.h @@ -121,8 +121,8 @@ public: CServerHandler(); - std::string getHostAddress() const; - ui16 getHostPort() const; + std::string getHostAddressFromSettings() const; + ui16 getHostPortFromSettings() const; void resetStateForLobby(const StartInfo::EMode mode, const std::vector * names = nullptr); void startLocalServerAndConnect(); diff --git a/client/mainmenu/CMainMenu.cpp b/client/mainmenu/CMainMenu.cpp index 34bda33d8..a8d6ef363 100644 --- a/client/mainmenu/CMainMenu.cpp +++ b/client/mainmenu/CMainMenu.cpp @@ -534,8 +534,8 @@ CSimpleJoinScreen::CSimpleJoinScreen(bool host) inputAddress->giveFocus(); } - inputAddress->setText(host ? CServerHandler::localhostAddress : CSH->getHostAddress(), true); - inputPort->setText(std::to_string(CSH->getHostPort()), true); + inputAddress->setText(host ? CServerHandler::localhostAddress : CSH->getHostAddressFromSettings(), true); + inputPort->setText(std::to_string(CSH->getHostPortFromSettings()), true); buttonCancel = std::make_shared(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CSimpleJoinScreen::leaveScreen, this), EShortcut::GLOBAL_CANCEL); statusBar = CGStatusBar::create(std::make_shared(background->getSurface(), Rect(7, 186, 218, 18), 7, 186)); From 732c39fcc6dc4e33971de74dc83eee8fbdbc7c38 Mon Sep 17 00:00:00 2001 From: Alexander Wilms Date: Sat, 7 Oct 2023 00:08:22 +0200 Subject: [PATCH 8/8] Fix build --- client/CServerHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 8efdbfdd3..6c65bec79 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -183,7 +183,7 @@ void CServerHandler::startLocalServerAndConnect() #if defined(SINGLE_PROCESS_APP) boost::condition_variable cond; - std::vector args{"--uuid=" + uuid, "--port=" + std::to_string(getHostPort())}; + std::vector args{"--uuid=" + uuid, "--port=" + std::to_string(getHostPortFromSettings())}; if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool()) { args.push_back("--lobby=" + settings["session"]["address"].String());