Browse Source

Fixed handling of connection failure on client

Ivan Savenko 1 year ago
parent
commit
a3639e77b1
2 changed files with 19 additions and 6 deletions
  1. 18 6
      client/CServerHandler.cpp
  2. 1 0
      client/CServerHandler.h

+ 18 - 6
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);
 	}

+ 1 - 0
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<NetworkConnection> &) override;
 	void onDisconnected(const std::shared_ptr<NetworkConnection> &) override;
+	void onTimer() override;
 
 	void applyPackOnLobbyScreen(CPackForLobby & pack);
 public: