12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013 |
- /*
- * CServerHandler.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "CServerHandler.h"
- #include "Client.h"
- #include "CGameInfo.h"
- #include "ServerRunner.h"
- #include "GameChatHandler.h"
- #include "CPlayerInterface.h"
- #include "gui/CGuiHandler.h"
- #include "gui/WindowHandler.h"
- #include "globalLobby/GlobalLobbyClient.h"
- #include "lobby/CSelectionBase.h"
- #include "lobby/CLobbyScreen.h"
- #include "windows/InfoWindows.h"
- #include "mainmenu/CMainMenu.h"
- #include "mainmenu/CPrologEpilogVideo.h"
- #include "mainmenu/CHighScoreScreen.h"
- #include "../lib/CConfigHandler.h"
- #include "../lib/texts/CGeneralTextHandler.h"
- #include "ConditionalWait.h"
- #include "../lib/CThreadHelper.h"
- #include "../lib/StartInfo.h"
- #include "../lib/TurnTimerInfo.h"
- #include "../lib/VCMIDirs.h"
- #include "../lib/campaign/CampaignState.h"
- #include "../lib/CPlayerState.h"
- #include "../lib/mapping/CMapInfo.h"
- #include "../lib/mapObjects/CGTownInstance.h"
- #include "../lib/mapObjects/MiscObjects.h"
- #include "../lib/modding/ModIncompatibility.h"
- #include "../lib/rmg/CMapGenOptions.h"
- #include "../lib/serializer/Connection.h"
- #include "../lib/filesystem/Filesystem.h"
- #include "../lib/registerTypes/RegisterTypesLobbyPacks.h"
- #include "../lib/serializer/CMemorySerializer.h"
- #include "../lib/UnlockGuard.h"
- #include <boost/uuid/uuid.hpp>
- #include <boost/uuid/uuid_io.hpp>
- #include <boost/uuid/uuid_generators.hpp>
- #include "../lib/serializer/Cast.h"
- #include "LobbyClientNetPackVisitors.h"
- #include <vcmi/events/EventBus.h>
- template<typename T> class CApplyOnLobby;
- class CBaseForLobbyApply
- {
- public:
- virtual bool applyOnLobbyHandler(CServerHandler * handler, CPackForLobby & pack) const = 0;
- virtual void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, CPackForLobby & pack) const = 0;
- virtual ~CBaseForLobbyApply(){};
- template<typename U> static CBaseForLobbyApply * getApplier(const U * t = nullptr)
- {
- return new CApplyOnLobby<U>();
- }
- };
- template<typename T> class CApplyOnLobby : public CBaseForLobbyApply
- {
- public:
- bool applyOnLobbyHandler(CServerHandler * handler, CPackForLobby & pack) const override
- {
- auto & ref = static_cast<T&>(pack);
- ApplyOnLobbyHandlerNetPackVisitor visitor(*handler);
- logNetwork->trace("\tImmediately apply on lobby: %s", typeid(ref).name());
- ref.visit(visitor);
- return visitor.getResult();
- }
- void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, CPackForLobby & pack) const override
- {
- auto & ref = static_cast<T &>(pack);
- ApplyOnLobbyScreenNetPackVisitor visitor(*handler, lobby);
- logNetwork->trace("\tApply on lobby from queue: %s", typeid(ref).name());
- ref.visit(visitor);
- }
- };
- template<> class CApplyOnLobby<CPack>: public CBaseForLobbyApply
- {
- public:
- bool applyOnLobbyHandler(CServerHandler * handler, CPackForLobby & pack) const override
- {
- logGlobal->error("Cannot apply plain CPack!");
- assert(0);
- return false;
- }
- void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, CPackForLobby & pack) const override
- {
- logGlobal->error("Cannot apply plain CPack!");
- assert(0);
- }
- };
- CServerHandler::~CServerHandler()
- {
- if (serverRunner)
- serverRunner->shutdown();
- networkHandler->stop();
- try
- {
- if (serverRunner)
- serverRunner->wait();
- serverRunner.reset();
- {
- auto unlockInterface = vstd::makeUnlockGuard(GH.interfaceMutex);
- threadNetwork.join();
- }
- }
- catch (const std::runtime_error & e)
- {
- logGlobal->error("Failed to shut down network thread! Reason: %s", e.what());
- assert(0);
- }
- }
- void CServerHandler::endNetwork()
- {
- if (client)
- client->endNetwork();
- networkHandler->stop();
- {
- auto unlockInterface = vstd::makeUnlockGuard(GH.interfaceMutex);
- threadNetwork.join();
- }
- }
- CServerHandler::CServerHandler()
- : networkHandler(INetworkHandler::createHandler())
- , lobbyClient(std::make_unique<GlobalLobbyClient>())
- , gameChat(std::make_unique<GameChatHandler>())
- , applier(std::make_unique<CApplier<CBaseForLobbyApply>>())
- , threadNetwork(&CServerHandler::threadRunNetwork, this)
- , state(EClientState::NONE)
- , serverPort(0)
- , campaignStateToSend(nullptr)
- , screenType(ESelectionScreen::unknown)
- , serverMode(EServerMode::NONE)
- , loadMode(ELoadMode::NONE)
- , client(nullptr)
- {
- uuid = boost::uuids::to_string(boost::uuids::random_generator()());
- registerTypesLobbyPacks(*applier);
- }
- void CServerHandler::threadRunNetwork()
- {
- logGlobal->info("Starting network thread");
- setThreadName("runNetwork");
- try {
- networkHandler->run();
- }
- catch (const TerminationRequestedException &)
- {
- logGlobal->info("Terminating network thread");
- return;
- }
- logGlobal->info("Ending network thread");
- }
- void CServerHandler::resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode newServerMode, const std::vector<std::string> & playerNames)
- {
- hostClientId = -1;
- setState(EClientState::NONE);
- serverMode = newServerMode;
- mapToStart = nullptr;
- th = std::make_unique<CStopWatch>();
- logicConnection.reset();
- si = std::make_shared<StartInfo>();
- localPlayerNames.clear();
- si->difficulty = 1;
- si->mode = mode;
- screenType = screen;
- localPlayerNames.clear();
- if(!playerNames.empty()) //if have custom set of player names - use it
- localPlayerNames = playerNames;
- else
- localPlayerNames.push_back(settings["general"]["playerName"].String());
- gameChat->resetMatchState();
- lobbyClient->resetMatchState();
- }
- GameChatHandler & CServerHandler::getGameChat()
- {
- return *gameChat;
- }
- GlobalLobbyClient & CServerHandler::getGlobalLobby()
- {
- return *lobbyClient;
- }
- INetworkHandler & CServerHandler::getNetworkHandler()
- {
- return *networkHandler;
- }
- void CServerHandler::startLocalServerAndConnect(bool connectToLobby)
- {
- logNetwork->trace("\tLocal server startup has been requested");
- #ifdef VCMI_MOBILE
- // mobile apps can't spawn separate processes - only thread mode is available
- serverRunner.reset(new ServerThreadRunner());
- #else
- if (settings["server"]["useProcess"].Bool())
- serverRunner.reset(new ServerProcessRunner());
- else
- serverRunner.reset(new ServerThreadRunner());
- #endif
- auto si = std::make_shared<StartInfo>();
- auto lastDifficulty = settings["general"]["lastDifficulty"];
- si->difficulty = lastDifficulty.Integer();
- logNetwork->trace("\tStarting local server");
- uint16_t srvport = serverRunner->start(getLocalPort(), connectToLobby, si);
- logNetwork->trace("\tConnecting to local server");
- connectToServer(getLocalHostname(), srvport);
- logNetwork->trace("\tWaiting for connection");
- }
- void CServerHandler::connectToServer(const std::string & addr, const ui16 port)
- {
- logNetwork->info("Establishing connection to %s:%d...", addr, port);
- setState(EClientState::CONNECTING);
- serverHostname = addr;
- serverPort = port;
- if (!isServerLocal())
- {
- Settings remoteAddress = settings.write["server"]["remoteHostname"];
- remoteAddress->String() = addr;
- Settings remotePort = settings.write["server"]["remotePort"];
- remotePort->Integer() = port;
- }
- networkHandler->connectToRemote(*this, addr, port);
- }
- void CServerHandler::onConnectionFailed(const std::string & errorMessage)
- {
- assert(getState() == EClientState::CONNECTING);
- boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
- if (isServerLocal())
- {
- // retry - local server might be still starting up
- logNetwork->debug("\nCannot establish connection. %s. Retrying...", errorMessage);
- networkHandler->createTimer(*this, std::chrono::milliseconds(100));
- }
- else
- {
- // remote server refused connection - show error message
- setState(EClientState::NONE);
- CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.mainMenu.serverConnectionFailed"), {});
- }
- }
- void CServerHandler::onTimer()
- {
- boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
- if(getState() == EClientState::CONNECTION_CANCELLED)
- {
- logNetwork->info("Connection aborted by player!");
- serverRunner->wait();
- serverRunner.reset();
- if (GH.windows().topWindow<CSimpleJoinScreen>() != nullptr)
- GH.windows().popWindows(1);
- return;
- }
- assert(isServerLocal());
- networkHandler->connectToRemote(*this, getLocalHostname(), getLocalPort());
- }
- void CServerHandler::onConnectionEstablished(const NetworkConnectionPtr & netConnection)
- {
- assert(getState() == EClientState::CONNECTING);
- boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
- networkConnection = netConnection;
- logNetwork->info("Connection established");
- if (serverMode == EServerMode::LOBBY_GUEST)
- {
- // say hello to lobby to switch connection to proxy mode
- getGlobalLobby().sendProxyConnectionLogin(netConnection);
- }
- logicConnection = std::make_shared<CConnection>(netConnection);
- logicConnection->uuid = uuid;
- logicConnection->enterLobbyConnectionMode();
- sendClientConnecting();
- }
- void CServerHandler::applyPackOnLobbyScreen(CPackForLobby & pack)
- {
- const CBaseForLobbyApply * apply = applier->getApplier(CTypeList::getInstance().getTypeID(&pack)); //find the applier
- apply->applyOnLobbyScreen(dynamic_cast<CLobbyScreen *>(SEL), this, pack);
- GH.windows().totalRedraw();
- }
- std::set<PlayerColor> CServerHandler::getHumanColors()
- {
- return clientHumanColors(logicConnection->connectionID);
- }
- PlayerColor CServerHandler::myFirstColor() const
- {
- return clientFirstColor(logicConnection->connectionID);
- }
- bool CServerHandler::isMyColor(PlayerColor color) const
- {
- return isClientColor(logicConnection->connectionID, color);
- }
- ui8 CServerHandler::myFirstId() const
- {
- return clientFirstId(logicConnection->connectionID);
- }
- EClientState CServerHandler::getState() const
- {
- return state;
- }
- void CServerHandler::setState(EClientState newState)
- {
- if (newState == EClientState::CONNECTION_CANCELLED && serverRunner != nullptr)
- serverRunner->shutdown();
- state = newState;
- }
- bool CServerHandler::isServerLocal() const
- {
- return serverRunner != nullptr;
- }
- bool CServerHandler::isHost() const
- {
- return logicConnection && hostClientId == logicConnection->connectionID;
- }
- bool CServerHandler::isGuest() const
- {
- return !logicConnection || hostClientId != logicConnection->connectionID;
- }
- const std::string & CServerHandler::getLocalHostname() const
- {
- return settings["server"]["localHostname"].String();
- }
- ui16 CServerHandler::getLocalPort() const
- {
- return settings["server"]["localPort"].Integer();
- }
- const std::string & CServerHandler::getRemoteHostname() const
- {
- return settings["server"]["remoteHostname"].String();
- }
- ui16 CServerHandler::getRemotePort() const
- {
- return settings["server"]["remotePort"].Integer();
- }
- const std::string & CServerHandler::getCurrentHostname() const
- {
- return serverHostname;
- }
- ui16 CServerHandler::getCurrentPort() const
- {
- return serverPort;
- }
- void CServerHandler::sendClientConnecting() const
- {
- LobbyClientConnected lcc;
- lcc.uuid = uuid;
- lcc.names = localPlayerNames;
- lcc.mode = si->mode;
- sendLobbyPack(lcc);
- }
- void CServerHandler::sendClientDisconnecting()
- {
- // FIXME: This is workaround needed to make sure client not trying to sent anything to non existed server
- if(getState() == EClientState::DISCONNECTING)
- {
- assert(0);
- return;
- }
- setState(EClientState::DISCONNECTING);
- mapToStart = nullptr;
- LobbyClientDisconnected lcd;
- lcd.clientId = logicConnection->connectionID;
- logNetwork->info("Connection has been requested to be closed.");
- if(isServerLocal())
- {
- lcd.shutdownServer = true;
- logNetwork->info("Sent closing signal to the server");
- }
- else
- {
- logNetwork->info("Sent leaving signal to the server");
- }
- sendLobbyPack(lcd);
- networkConnection->close();
- networkConnection.reset();
- logicConnection.reset();
- waitForServerShutdown();
- }
- void CServerHandler::setCampaignState(std::shared_ptr<CampaignState> newCampaign)
- {
- setState(EClientState::LOBBY_CAMPAIGN);
- LobbySetCampaign lsc;
- lsc.ourCampaign = newCampaign;
- sendLobbyPack(lsc);
- }
- void CServerHandler::setCampaignMap(CampaignScenarioID mapId) const
- {
- if(getState() == EClientState::GAMEPLAY) // FIXME: UI shouldn't sent commands in first place
- return;
- LobbySetCampaignMap lscm;
- lscm.mapId = mapId;
- sendLobbyPack(lscm);
- }
- void CServerHandler::setCampaignBonus(int bonusId) const
- {
- if(getState() == EClientState::GAMEPLAY) // FIXME: UI shouldn't sent commands in first place
- return;
- LobbySetCampaignBonus lscb;
- lscb.bonusId = bonusId;
- sendLobbyPack(lscb);
- }
- void CServerHandler::setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts) const
- {
- LobbySetMap lsm;
- lsm.mapInfo = to;
- lsm.mapGenOpts = mapGenOpts;
- sendLobbyPack(lsm);
- }
- void CServerHandler::setPlayer(PlayerColor color) const
- {
- LobbySetPlayer lsp;
- lsp.clickedColor = color;
- sendLobbyPack(lsp);
- }
- void CServerHandler::setPlayerName(PlayerColor color, const std::string & name) const
- {
- LobbySetPlayerName lspn;
- lspn.color = color;
- lspn.name = name;
- sendLobbyPack(lspn);
- }
- void CServerHandler::setPlayerOption(ui8 what, int32_t value, PlayerColor player) const
- {
- LobbyChangePlayerOption lcpo;
- lcpo.what = what;
- lcpo.value = value;
- lcpo.color = player;
- sendLobbyPack(lcpo);
- }
- void CServerHandler::setDifficulty(int to) const
- {
- LobbySetDifficulty lsd;
- lsd.difficulty = to;
- sendLobbyPack(lsd);
- }
- void CServerHandler::setSimturnsInfo(const SimturnsInfo & info) const
- {
- LobbySetSimturns pack;
- pack.simturnsInfo = info;
- sendLobbyPack(pack);
- }
- void CServerHandler::setTurnTimerInfo(const TurnTimerInfo & info) const
- {
- LobbySetTurnTime lstt;
- lstt.turnTimerInfo = info;
- sendLobbyPack(lstt);
- }
- void CServerHandler::setExtraOptionsInfo(const ExtraOptionsInfo & info) const
- {
- LobbySetExtraOptions lseo;
- lseo.extraOptionsInfo = info;
- sendLobbyPack(lseo);
- }
- void CServerHandler::sendMessage(const std::string & txt) const
- {
- std::istringstream readed;
- readed.str(txt);
- std::string command;
- readed >> command;
- if(command == "!passhost")
- {
- std::string id;
- readed >> id;
- if(id.length())
- {
- LobbyChangeHost lch;
- lch.newHostConnectionId = boost::lexical_cast<int>(id);
- sendLobbyPack(lch);
- }
- }
- else if(command == "!forcep")
- {
- std::string connectedId;
- std::string playerColorId;
- readed >> connectedId;
- readed >> playerColorId;
- if(connectedId.length() && playerColorId.length())
- {
- ui8 connected = boost::lexical_cast<int>(connectedId);
- auto color = PlayerColor(boost::lexical_cast<int>(playerColorId));
- if(color.isValidPlayer() && playerNames.find(connected) != playerNames.end())
- {
- LobbyForceSetPlayer lfsp;
- lfsp.targetConnectedPlayer = connected;
- lfsp.targetPlayerColor = color;
- sendLobbyPack(lfsp);
- }
- }
- }
- else
- {
- gameChat->sendMessageLobby(playerNames.find(myFirstId())->second.name, txt);
- }
- }
- void CServerHandler::sendGuiAction(ui8 action) const
- {
- LobbyGuiAction lga;
- lga.action = static_cast<LobbyGuiAction::EAction>(action);
- sendLobbyPack(lga);
- }
- void CServerHandler::sendRestartGame() const
- {
- GH.windows().createAndPushWindow<CLoadingScreen>();
-
- LobbyRestartGame endGame;
- sendLobbyPack(endGame);
- }
- bool CServerHandler::validateGameStart(bool allowOnlyAI) const
- {
- try
- {
- verifyStateBeforeStart(allowOnlyAI ? true : settings["session"]["onlyai"].Bool());
- }
- catch(ModIncompatibility & e)
- {
- logGlobal->warn("Incompatibility exception during start scenario: %s", e.what());
- std::string errorMsg;
- if(!e.whatMissing().empty())
- {
- errorMsg += VLC->generaltexth->translate("vcmi.server.errors.modsToEnable") + '\n';
- errorMsg += e.whatMissing();
- }
- if(!e.whatExcessive().empty())
- {
- errorMsg += VLC->generaltexth->translate("vcmi.server.errors.modsToDisable") + '\n';
- errorMsg += e.whatExcessive();
- }
- showServerError(errorMsg);
- return false;
- }
- catch(std::exception & e)
- {
- logGlobal->error("Exception during startScenario: %s", e.what());
- showServerError( std::string("Unable to start map! Reason: ") + e.what());
- return false;
- }
- return true;
- }
- void CServerHandler::sendStartGame(bool allowOnlyAI) const
- {
- verifyStateBeforeStart(allowOnlyAI ? true : settings["session"]["onlyai"].Bool());
- if(!settings["session"]["headless"].Bool())
- GH.windows().createAndPushWindow<CLoadingScreen>();
-
- LobbyPrepareStartGame lpsg;
- sendLobbyPack(lpsg);
- LobbyStartGame lsg;
- sendLobbyPack(lsg);
- }
- void CServerHandler::startMapAfterConnection(std::shared_ptr<CMapInfo> to)
- {
- mapToStart = to;
- }
- void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState)
- {
- if(CMM)
- CMM->disable();
- switch(si->mode)
- {
- case EStartMode::NEW_GAME:
- client->newGame(gameState);
- break;
- case EStartMode::CAMPAIGN:
- if(si->campState->conqueredScenarios().empty())
- si->campState->highscoreParameters.clear();
- client->newGame(gameState);
- break;
- case EStartMode::LOAD_GAME:
- client->loadGame(gameState);
- break;
- default:
- throw std::runtime_error("Invalid mode");
- }
- // After everything initialized we can accept CPackToClient netpacks
- logicConnection->enterGameplayConnectionMode(client->gameState());
- setState(EClientState::GAMEPLAY);
- }
- HighScoreParameter CServerHandler::prepareHighScores(PlayerColor player, bool victory)
- {
- const auto * gs = client->gameState();
- const auto * playerState = gs->getPlayerState(player);
- HighScoreParameter param;
- param.difficulty = gs->getStartInfo()->difficulty;
- param.day = gs->getDate();
- param.townAmount = gs->howManyTowns(player);
- param.usedCheat = gs->getPlayerState(player)->cheated;
- param.hasGrail = false;
- for(const CGHeroInstance * h : playerState->heroes)
- if(h->hasArt(ArtifactID::GRAIL))
- param.hasGrail = true;
- for(const CGTownInstance * t : playerState->towns)
- if(t->builtBuildings.count(BuildingID::GRAIL))
- param.hasGrail = true;
- param.allEnemiesDefeated = true;
- for (PlayerColor otherPlayer(0); otherPlayer < PlayerColor::PLAYER_LIMIT; ++otherPlayer)
- {
- auto ps = gs->getPlayerState(otherPlayer, false);
- if(ps && otherPlayer != player && !ps->checkVanquished())
- param.allEnemiesDefeated = false;
- }
- param.scenarioName = gs->getMapHeader()->name.toString();
- param.playerName = gs->getStartInfo()->playerInfos.find(player)->second.name;
- return param;
- }
- void CServerHandler::showHighScoresAndEndGameplay(PlayerColor player, bool victory)
- {
- HighScoreParameter param = prepareHighScores(player, victory);
- if(victory && client->gameState()->getStartInfo()->campState)
- {
- startCampaignScenario(param, client->gameState()->getStartInfo()->campState);
- }
- else
- {
- HighScoreCalculation scenarioHighScores;
- scenarioHighScores.parameters.push_back(param);
- scenarioHighScores.isCampaign = false;
- endGameplay();
- GH.defActionsDef = 63;
- CMM->menu->switchToTab("main");
- GH.windows().createAndPushWindow<CHighScoreInputScreen>(victory, scenarioHighScores);
- }
- }
- void CServerHandler::endGameplay()
- {
- // Game is ending
- // Tell the network thread to reach a stable state
- sendClientDisconnecting();
- logNetwork->info("Closed connection.");
- client->endGame();
- client.reset();
- if(CMM)
- {
- GH.curInt = CMM.get();
- CMM->enable();
- }
- else
- {
- GH.curInt = CMainMenu::create().get();
- }
- }
- void CServerHandler::restartGameplay()
- {
- client->endGame();
- client.reset();
- logicConnection->enterLobbyConnectionMode();
- }
- void CServerHandler::startCampaignScenario(HighScoreParameter param, std::shared_ptr<CampaignState> cs)
- {
- std::shared_ptr<CampaignState> ourCampaign = cs;
- if (!cs)
- ourCampaign = si->campState;
- param.campaignName = cs->getNameTranslated();
- cs->highscoreParameters.push_back(param);
- auto campaignScoreCalculator = std::make_shared<HighScoreCalculation>();
- campaignScoreCalculator->isCampaign = true;
- campaignScoreCalculator->parameters = cs->highscoreParameters;
- endGameplay();
- auto & epilogue = ourCampaign->scenario(*ourCampaign->lastScenario()).epilog;
- auto finisher = [ourCampaign, campaignScoreCalculator]()
- {
- if(ourCampaign->campaignSet != "" && ourCampaign->isCampaignFinished())
- {
- Settings entry = persistentStorage.write["completedCampaigns"][ourCampaign->getFilename()];
- entry->Bool() = true;
- }
- GH.windows().pushWindow(CMM);
- GH.windows().pushWindow(CMM->menu);
- if(!ourCampaign->isCampaignFinished())
- CMM->openCampaignLobby(ourCampaign);
- else
- {
- CMM->openCampaignScreen(ourCampaign->campaignSet);
- GH.windows().createAndPushWindow<CHighScoreInputScreen>(true, *campaignScoreCalculator);
- }
- };
- if(epilogue.hasPrologEpilog)
- {
- GH.windows().createAndPushWindow<CPrologEpilogVideo>(epilogue, finisher);
- }
- else
- {
- finisher();
- }
- }
- void CServerHandler::showServerError(const std::string & txt) const
- {
- if(auto w = GH.windows().topWindow<CLoadingScreen>())
- GH.windows().popWindow(w);
-
- CInfoWindow::showInfoDialog(txt, {});
- }
- int CServerHandler::howManyPlayerInterfaces()
- {
- int playerInts = 0;
- for(auto pint : client->playerint)
- {
- if(dynamic_cast<CPlayerInterface *>(pint.second.get()))
- playerInts++;
- }
- return playerInts;
- }
- ELoadMode CServerHandler::getLoadMode()
- {
- if(loadMode != ELoadMode::TUTORIAL && getState() == EClientState::GAMEPLAY)
- {
- if(si->campState)
- return ELoadMode::CAMPAIGN;
- for(auto pn : playerNames)
- {
- if(pn.second.connection != logicConnection->connectionID)
- return ELoadMode::MULTI;
- }
- if(howManyPlayerInterfaces() > 1) //this condition will work for hotseat mode OR multiplayer with allowed more than 1 color per player to control
- return ELoadMode::MULTI;
- return ELoadMode::SINGLE;
- }
- return loadMode;
- }
- void CServerHandler::debugStartTest(std::string filename, bool save)
- {
- logGlobal->info("Starting debug test with file: %s", filename);
- auto mapInfo = std::make_shared<CMapInfo>();
- if(save)
- {
- resetStateForLobby(EStartMode::LOAD_GAME, ESelectionScreen::loadGame, EServerMode::LOCAL, {});
- mapInfo->saveInit(ResourcePath(filename, EResType::SAVEGAME));
- }
- else
- {
- resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOCAL, {});
- mapInfo->mapInit(filename);
- }
- if(settings["session"]["donotstartserver"].Bool())
- connectToServer(getLocalHostname(), getLocalPort());
- else
- startLocalServerAndConnect(false);
- boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
- while(!settings["session"]["headless"].Bool() && !GH.windows().topWindow<CLobbyScreen>())
- boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
- while(!mi || mapInfo->fileURI != mi->fileURI)
- {
- setMapInfo(mapInfo);
- boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
- }
- // "Click" on color to remove us from it
- setPlayer(myFirstColor());
- while(myFirstColor() != PlayerColor::CANNOT_DETERMINE)
- boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
- while(true)
- {
- try
- {
- sendStartGame();
- break;
- }
- catch(...)
- {
- }
- boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
- }
- }
- class ServerHandlerCPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
- {
- private:
- CServerHandler & handler;
- public:
- ServerHandlerCPackVisitor(CServerHandler & handler)
- :handler(handler)
- {
- }
- bool callTyped() override { return false; }
- void visitForLobby(CPackForLobby & lobbyPack) override
- {
- handler.visitForLobby(lobbyPack);
- }
- void visitForClient(CPackForClient & clientPack) override
- {
- handler.visitForClient(clientPack);
- }
- };
- void CServerHandler::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message)
- {
- boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
- if(getState() == EClientState::DISCONNECTING)
- return;
- CPack * pack = logicConnection->retrievePack(message);
- ServerHandlerCPackVisitor visitor(*this);
- pack->visit(visitor);
- }
- void CServerHandler::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
- {
- boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
- if (connection != networkConnection)
- {
- // ServerHandler already closed this connection on its own
- // This is the final call from network thread that informs serverHandler that connection has died
- // ignore it since serverHandler have already shut down this connection (and possibly started a new one)
- return;
- }
- waitForServerShutdown();
- if(getState() == EClientState::DISCONNECTING)
- {
- // Note: this branch can be reached on app shutdown, when main thread holds mutex till destruction
- logNetwork->info("Successfully closed connection to server!");
- return;
- }
- logNetwork->error("Lost connection to server! Connection has been closed");
- if(client)
- {
- endGameplay();
- GH.defActionsDef = 63;
- CMM->menu->switchToTab("main");
- showServerError(CGI->generaltexth->translate("vcmi.server.errors.disconnected"));
- }
- else
- {
- LobbyClientDisconnected lcd;
- lcd.clientId = logicConnection->connectionID;
- applyPackOnLobbyScreen(lcd);
- }
- networkConnection.reset();
- }
- void CServerHandler::waitForServerShutdown()
- {
- if (!serverRunner)
- return; // may not exist for guest in MP
- serverRunner->wait();
- int exitCode = serverRunner->exitCode();
- serverRunner.reset();
- if (exitCode == 0)
- {
- logNetwork->info("Server closed correctly");
- }
- else
- {
- if (getState() == EClientState::CONNECTING)
- {
- showServerError(CGI->generaltexth->translate("vcmi.server.errors.existingProcess"));
- setState(EClientState::CONNECTION_CANCELLED); // stop attempts to reconnect
- }
- logNetwork->error("Error: server failed to close correctly or crashed!");
- logNetwork->error("Check log file for more info");
- }
- serverRunner.reset();
- }
- void CServerHandler::visitForLobby(CPackForLobby & lobbyPack)
- {
- if(applier->getApplier(CTypeList::getInstance().getTypeID(&lobbyPack))->applyOnLobbyHandler(this, lobbyPack))
- {
- if(!settings["session"]["headless"].Bool())
- applyPackOnLobbyScreen(lobbyPack);
- }
- }
- void CServerHandler::visitForClient(CPackForClient & clientPack)
- {
- client->handlePack(&clientPack);
- }
- void CServerHandler::sendLobbyPack(const CPackForLobby & pack) const
- {
- if(getState() != EClientState::STARTING)
- logicConnection->sendPack(&pack);
- }
- bool CServerHandler::inLobbyRoom() const
- {
- return serverMode == EServerMode::LOBBY_HOST || serverMode == EServerMode::LOBBY_GUEST;
- }
- bool CServerHandler::inGame() const
- {
- return logicConnection != nullptr;
- }
|