1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-25 21:38:59 +02:00

Use correct address to decide whether we're connecting to a remote server or a local one

This commit is contained in:
Alexander Wilms 2023-10-06 23:27:19 +02:00
parent b58cca7770
commit c8f1512a3f
3 changed files with 23 additions and 17 deletions

View File

@ -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<long, boost::ratio<1, 1000>> 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<CConnection>(
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<boost::thread>(&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())

View File

@ -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<std::string> * names = nullptr);
void startLocalServerAndConnect();

View File

@ -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<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));