diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 00a438064..7fef5cd1e 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -251,7 +251,7 @@ void CServerHandler::startLocalServerAndConnect() while(!androidTestServerReadyFlag.load()) { logNetwork->info("still waiting..."); - boost::this_thread::sleep_for(boost::chrono::milliseconds(1000)); + boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } logNetwork->info("waiting for server finished..."); androidTestServerReadyFlag = false; @@ -285,6 +285,22 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po } void CServerHandler::onConnectionFailed(const std::string & errorMessage) +{ + if (isServerLocal()) + { + // retry - local server might be still starting up + logNetwork->debug("\nCannot establish connection. %s Retrying...", errorMessage); + networkClient->setTimer(std::chrono::milliseconds(100)); + } + else + { + // remote server refused connection - show error message + state = EClientState::CONNECTION_FAILED; + CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.mainMenu.serverConnectionFailed"), {}); + } +} + +void CServerHandler::onTimer() { if(state == EClientState::CONNECTION_CANCELLED) { @@ -292,11 +308,6 @@ void CServerHandler::onConnectionFailed(const std::string & errorMessage) return; } - logNetwork->warn("\nCannot establish connection. %s Retrying in 1 second", errorMessage); - - //FIXME: replace with asio timer - boost::this_thread::sleep_for(boost::chrono::milliseconds(1000)); - //FIXME: pass parameters from initial attempt networkClient->start(getHostAddress(), getHostPort()); } @@ -1001,6 +1012,7 @@ void CServerHandler::threadRunServer() } else { + state = EClientState::CONNECTION_CANCELLED; // stop attempts to reconnect logNetwork->error("Error: server failed to close correctly or crashed!"); logNetwork->error("Check %s for more info", logName); } diff --git a/client/CServerHandler.h b/client/CServerHandler.h index e62e9701d..790a26adb 100644 --- a/client/CServerHandler.h +++ b/client/CServerHandler.h @@ -100,6 +100,7 @@ class CServerHandler : public IServerAPI, public LobbyInfo, public INetworkClien void onConnectionFailed(const std::string & errorMessage) override; void onConnectionEstablished(const std::shared_ptr &) override; void onDisconnected(const std::shared_ptr &) override; + void onTimer() override; void applyPackOnLobbyScreen(CPackForLobby & pack); public: