1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-17 11:56:46 +02:00

Merge pull request #2959 from Alexander-Wilms/faster-server-connection

Sleep for 10 ms instead of 1000 ms while waiting for server connection
This commit is contained in:
Nordsoft91 2023-10-08 17:21:57 +02:00 committed by GitHub
commit 43636af2e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 15 deletions

View File

@ -183,7 +183,7 @@ void CServerHandler::startLocalServerAndConnect()
#if defined(SINGLE_PROCESS_APP) #if defined(SINGLE_PROCESS_APP)
boost::condition_variable cond; boost::condition_variable cond;
std::vector<std::string> args{"--uuid=" + uuid, "--port=" + std::to_string(getHostPort())}; std::vector<std::string> args{"--uuid=" + uuid, "--port=" + std::to_string(getHostPortFromSettings())};
if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool()) if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool())
{ {
args.push_back("--lobby=" + settings["session"]["address"].String()); args.push_back("--lobby=" + settings["session"]["address"].String());
@ -251,20 +251,55 @@ void CServerHandler::startLocalServerAndConnect()
void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port) void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port)
{ {
state = EClientState::CONNECTING; state = EClientState::CONNECTING;
logNetwork->info("justConnectToServer(%s, %d)", addr, port);
std::string hostAddressFromSettings = getHostAddressFromSettings();
ui16 hostPortFromSettings = getHostPortFromSettings();
logNetwork->info("Host settings %s:%d", hostAddressFromSettings, hostPortFromSettings);
std::string connectionAddress = addr.size() ? addr : hostAddressFromSettings;
ui16 connectionPort = port ? port : hostPortFromSettings;
boost::chrono::duration<long, boost::ratio<1, 1000>> sleepDuration{};
int maxConnectionAttempts;
if(connectionAddress == "127.0.0.1" || connectionAddress == "localhost")
{
logNetwork->info("Local server");
sleepDuration = boost::chrono::milliseconds(10);
maxConnectionAttempts = 1000;
}
else
{
logNetwork->info("Remote server");
sleepDuration = boost::chrono::seconds(2);
maxConnectionAttempts = 10;
}
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) while(!c && state != EClientState::CONNECTION_CANCELLED)
{ {
connectionAttemptCount++;
if(connectionAttemptCount > maxConnectionAttempts)
{
logNetwork->error("\nExceeded maximum of %d connection attempts", maxConnectionAttempts);
return;
}
try try
{ {
logNetwork->info("Establishing connection...");
c = std::make_shared<CConnection>( c = std::make_shared<CConnection>(
addr.size() ? addr : getHostAddress(), connectionAddress,
port ? port : getHostPort(), connectionPort,
NAME, uuid); NAME, uuid);
} }
catch(std::runtime_error & error) catch(std::runtime_error & error)
{ {
logNetwork->warn("\nCannot establish connection. %s Retrying in 1 second", error.what()); boost::this_thread::sleep_for(sleepDuration);
boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
} }
} }
@ -276,12 +311,12 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po
c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this); c->handler = std::make_shared<boost::thread>(&CServerHandler::threadHandleConnection, this);
if(!addr.empty() && addr != getHostAddress()) if(!addr.empty() && addr != getHostAddressFromSettings())
{ {
Settings serverAddress = settings.write["server"]["server"]; Settings serverAddress = settings.write["server"]["server"];
serverAddress->String() = addr; serverAddress->String() = addr;
} }
if(port && port != getHostPort()) if(port && port != getHostPortFromSettings())
{ {
Settings serverPort = settings.write["server"]["port"]; Settings serverPort = settings.write["server"]["port"];
serverPort->Integer() = port; serverPort->Integer() = port;
@ -365,7 +400,7 @@ std::string CServerHandler::getDefaultPortStr()
return std::to_string(getDefaultPort()); return std::to_string(getDefaultPort());
} }
std::string CServerHandler::getHostAddress() const std::string CServerHandler::getHostAddressFromSettings() const
{ {
if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool()) if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool())
return settings["server"]["server"].String(); return settings["server"]["server"].String();
@ -376,7 +411,7 @@ std::string CServerHandler::getHostAddress() const
return settings["session"]["address"].String(); return settings["session"]["address"].String();
} }
ui16 CServerHandler::getHostPort() const ui16 CServerHandler::getHostPortFromSettings() const
{ {
if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool()) if(settings["session"]["lobby"].isNull() || !settings["session"]["lobby"].Bool())
return getDefaultPort(); return getDefaultPort();
@ -967,7 +1002,7 @@ void CServerHandler::threadRunServer()
setThreadName("runServer"); setThreadName("runServer");
const std::string logName = (VCMIDirs::get().userLogsPath() / "server_log.txt").string(); const std::string logName = (VCMIDirs::get().userLogsPath() / "server_log.txt").string();
std::string comm = VCMIDirs::get().serverPath().string() std::string comm = VCMIDirs::get().serverPath().string()
+ " --port=" + std::to_string(getHostPort()) + " --port=" + std::to_string(getHostPortFromSettings())
+ " --run-by-client" + " --run-by-client"
+ " --uuid=" + uuid; + " --uuid=" + uuid;
if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool()) if(settings["session"]["lobby"].Bool() && settings["session"]["host"].Bool())

View File

@ -121,8 +121,8 @@ public:
CServerHandler(); CServerHandler();
std::string getHostAddress() const; std::string getHostAddressFromSettings() const;
ui16 getHostPort() const; ui16 getHostPortFromSettings() const;
void resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names = nullptr); void resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names = nullptr);
void startLocalServerAndConnect(); void startLocalServerAndConnect();

View File

@ -532,8 +532,8 @@ CSimpleJoinScreen::CSimpleJoinScreen(bool host)
inputAddress->giveFocus(); inputAddress->giveFocus();
} }
inputAddress->setText(host ? CServerHandler::localhostAddress : CSH->getHostAddress(), true); inputAddress->setText(host ? CServerHandler::localhostAddress : CSH->getHostAddressFromSettings(), true);
inputPort->setText(std::to_string(CSH->getHostPort()), true); inputPort->setText(std::to_string(CSH->getHostPortFromSettings()), true);
buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CSimpleJoinScreen::leaveScreen, this), EShortcut::GLOBAL_CANCEL); buttonCancel = std::make_shared<CButton>(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<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186)); statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));