1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-29 21:56:54 +02:00

Simplified connection logic

This commit is contained in:
Ivan Savenko 2023-12-25 22:56:55 +02:00
parent 8ea69e457a
commit 5694777a96
4 changed files with 15 additions and 42 deletions

@ -181,7 +181,7 @@ void CServerHandler::resetStateForLobby(const StartInfo::EMode mode, const std::
myNames.push_back(settings["general"]["playerName"].String());
}
void CServerHandler::startLocalServerAndConnect(const std::function<void()> & onConnected)
void CServerHandler::startLocalServerAndConnect()
{
if(threadRunLocalServer)
threadRunLocalServer->join();
@ -260,12 +260,12 @@ void CServerHandler::startLocalServerAndConnect(const std::function<void()> & on
th->update(); //put breakpoint here to attach to server before it does something stupid
justConnectToServer(localhostAddress, 0, onConnected);
justConnectToServer(localhostAddress, 0);
logNetwork->trace("\tConnecting to the server: %d ms", th->getDiff());
}
void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port, const std::function<void()> & onConnected)
void CServerHandler::justConnectToServer(const std::string & addr, const ui16 port)
{
logNetwork->info("Establishing connection...");
state = EClientState::CONNECTING;
@ -281,10 +281,6 @@ void CServerHandler::justConnectToServer(const std::string & addr, const ui16 po
serverPort->Integer() = port;
}
if (onConnectedCallback)
throw std::runtime_error("Attempt to connect while there is already a pending connection!");
onConnectedCallback = onConnected;
networkClient->start(addr.size() ? addr : getHostAddress(), port ? port : getHostPort());
}
@ -311,12 +307,6 @@ void CServerHandler::onConnectionEstablished(const std::shared_ptr<NetworkConnec
c = std::make_shared<CConnection>(netConnection);
c->enterLobbyConnectionMode();
sendClientConnecting();
if (onConnectedCallback)
{
onConnectedCallback();
onConnectedCallback = {};
}
}
void CServerHandler::applyPackOnLobbyScreen(CPackForLobby & pack)
@ -820,7 +810,7 @@ void CServerHandler::restoreLastSession()
myNames.push_back(name.String());
resetStateForLobby(StartInfo::LOAD_GAME, &myNames);
screenType = ESelectionScreen::loadGame;
justConnectToServer(settings["server"]["server"].String(), settings["server"]["port"].Integer(), {});
justConnectToServer(settings["server"]["server"].String(), settings["server"]["port"].Integer());
};
auto cleanUpSession = []()
@ -850,9 +840,9 @@ void CServerHandler::debugStartTest(std::string filename, bool save)
screenType = ESelectionScreen::newGame;
}
if(settings["session"]["donotstartserver"].Bool())
justConnectToServer(localhostAddress, 3030, {});
justConnectToServer(localhostAddress, 3030);
else
startLocalServerAndConnect({});
startLocalServerAndConnect();
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));

@ -90,7 +90,6 @@ class CServerHandler : public IServerAPI, public LobbyInfo, public INetworkClien
std::shared_ptr<CMapInfo> mapToStart;
std::vector<std::string> myNames;
std::shared_ptr<HighScoreCalculation> highScoreCalc;
std::function<void()> onConnectedCallback;
void threadRunNetwork();
void threadRunServer();
@ -134,8 +133,8 @@ public:
ui16 getHostPort() const;
void resetStateForLobby(const StartInfo::EMode mode, const std::vector<std::string> * names = nullptr);
void startLocalServerAndConnect(const std::function<void()> & onConnected);
void justConnectToServer(const std::string & addr, const ui16 port, const std::function<void()> & onConnected);
void startLocalServerAndConnect();
void justConnectToServer(const std::string & addr, const ui16 port);
// Helpers for lobby state access
std::set<PlayerColor> getHumanColors();

@ -43,7 +43,12 @@ void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyClientConnected(LobbyClientCon
if(handler.mapToStart)
handler.setMapInfo(handler.mapToStart);
else if(!settings["session"]["headless"].Bool())
{
if (GH.windows().topWindow<CSimpleJoinScreen>())
GH.windows().popWindows(1);
GH.windows().createAndPushWindow<CLobbyScreen>(static_cast<ESelectionScreen>(handler.screenType));
}
handler.state = EClientState::LOBBY;
}
}

@ -611,31 +611,10 @@ void CSimpleJoinScreen::startConnection(const std::string & addr, ui16 port)
CVCMIServer::reuseClientJNIEnv(SDL_AndroidGetJNIEnv());
#endif
auto const & onConnected = [this]()
{
// async call to prevent thread race
GH.dispatchMainThread([this](){
// FIXME: this enum value is never set!!!
if(CSH->state == EClientState::CONNECTION_FAILED)
{
CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.mainMenu.serverConnectionFailed"), {});
textTitle->setText(CGI->generaltexth->translate("vcmi.mainMenu.serverAddressEnter"));
GH.startTextInput(inputAddress->pos);
buttonOk->block(false);
}
if(GH.windows().isTopWindow(this))
{
close();
}
});
};
if(addr.empty())
CSH->startLocalServerAndConnect(onConnected);
CSH->startLocalServerAndConnect();
else
CSH->justConnectToServer(addr, port, onConnected);
CSH->justConnectToServer(addr, port);
}
CLoadingScreen::CLoadingScreen()