diff --git a/client/CMT.cpp b/client/CMT.cpp index 11bbfa097..ece9d6ed5 100644 --- a/client/CMT.cpp +++ b/client/CMT.cpp @@ -254,9 +254,9 @@ int main(int argc, char** argv) ("loadplayer", po::value(),"specifies which player we are in multiplayer loaded games (0=Red, etc.)") ("loadserverip",po::value(),"IP for loaded game server") ("loadserverport",po::value(),"port for loaded game server") - ("testingport",po::value(),"port for testing, override specified in config file") - ("testingfileprefix",po::value(),"prefix for auto save files") - ("testingsavefrequency",po::value(),"how often auto save should be created"); + ("serverport", po::value(), "override port specified in config file") + ("saveprefix", po::value(), "prefix for auto save files") + ("savefrequency", po::value(), "limit auto save creation to each N days"); if(argc > 1) { @@ -316,14 +316,10 @@ int main(int argc, char** argv) settings.init(); // Init special testing settings - Settings testingSettings = settings.write["testing"]; - if(vm.count("testingport") && vm.count("testingfileprefix")) - { - testingSettings["enabled"].Bool() = true; - testingSettings["port"].String() = vm["testingport"].as(); - testingSettings["prefix"].String() = vm["testingfileprefix"].as(); - testingSettings["savefrequency"].Float() = vm.count("testingsavefrequency") ? vm["testingsavefrequency"].as() : 1; - } + Settings session = settings.write["session"]; + session["serverport"].Integer() = vm.count("serverport") ? vm["serverport"].as() : 0; + session["saveprefix"].String() = vm.count("saveprefix") ? vm["saveprefix"].as() : ""; + session["savefrequency"].Integer() = vm.count("savefrequency") ? vm["savefrequency"].as() : 1; // Initialize logging based on settings logConfig.configure(); @@ -1309,7 +1305,7 @@ void startGame(StartInfo * options, CConnection *serv/* = nullptr*/) if(!vm.count("loadplayer")) client->loadGame(fname); else - client->loadGame(fname,vm.count("loadserver"),vm.count("loadhumanplayerindices") ? vm["loadhumanplayerindices"].as>() : std::vector(),vm.count("loadnumplayers") ? vm["loadnumplayers"].as() : 1,vm["loadplayer"].as(),vm.count("loadserverip") ? vm["loadserverip"].as() : "", vm.count("loadserverport") ? vm["loadserverport"].as() : "3030"); + client->loadGame(fname,vm.count("loadserver"),vm.count("loadhumanplayerindices") ? vm["loadhumanplayerindices"].as>() : std::vector(),vm.count("loadnumplayers") ? vm["loadnumplayers"].as() : 1,vm["loadplayer"].as(),vm.count("loadserverip") ? vm["loadserverip"].as() : "", vm.count("loadserverport") ? vm["loadserverport"].as() : CServerHandler::getDefaultPort()); break; } diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index 10ae7da76..3ebe3eda5 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -171,12 +171,7 @@ void CPlayerInterface::yourTurn() GH.curInt = this; adventureInt->selection = nullptr; - std::string prefix = ""; - if (settings["testing"]["enabled"].Bool()) - { - prefix = settings["testing"]["prefix"].String(); - } - + std::string prefix = settings["session"]["saveprefix"].String(); if (firstCall) { if (howManyPeople == 1) @@ -192,7 +187,7 @@ void CPlayerInterface::yourTurn() } firstCall = 0; } - else if (settings["testing"].isNull() || cb->getDate() % static_cast(settings["testing"]["savefrequency"].Float()) == 0) + else if (cb->getDate() % static_cast(settings["session"]["savefrequency"].Integer()) == 0) { LOCPLINT->cb->save("Saves/" + prefix + "Autosave_" + boost::lexical_cast(autosaveCount++ + 1)); autosaveCount %= 5; diff --git a/client/CPreGame.cpp b/client/CPreGame.cpp index b20b4561b..f76b05b74 100644 --- a/client/CPreGame.cpp +++ b/client/CPreGame.cpp @@ -559,7 +559,7 @@ void CGPreGame::removeFromGui() GH.popInt(GH.topInt()); //remove background } -CSelectionScreen::CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer /*= CMenuScreen::SINGLE_PLAYER*/, const std::map * Names /*= nullptr*/, const std::string & Address /*=""*/, const std::string & Port /*= ""*/) +CSelectionScreen::CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer /*= CMenuScreen::SINGLE_PLAYER*/, const std::map * Names /*= nullptr*/, const std::string & Address /*=""*/, const ui16 Port) : ISelectionScreenInfo(Names), serverHandlingThread(nullptr), mx(new boost::recursive_mutex), serv(nullptr), ongoingClosing(false), myNameID(255) { @@ -4320,7 +4320,7 @@ CSimpleJoinScreen::CSimpleJoinScreen(CMenuScreen::EMultiMode mode) cancel = new CButton(Point(142, 142), "MUBCANC.DEF", CGI->generaltexth->zelp[561], std::bind(&CGuiHandler::popIntTotally, std::ref(GH), this), SDLK_ESCAPE); bar = new CGStatusBar(new CPicture(Rect(7, 186, 218, 18), 0)); - port->setText(boost::lexical_cast(settings["server"]["port"].Float()), true); + port->setText(CServerHandler::getDefaultPortStr(), true); address->setText(settings["server"]["server"].String(), true); address->giveFocus(); } @@ -4332,7 +4332,7 @@ void CSimpleJoinScreen::enterSelectionScreen(CMenuScreen::EMultiMode mode) GH.popIntTotally(this); - GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, mode, nullptr, textAddress, textPort)); + GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, mode, nullptr, textAddress, boost::lexical_cast(textPort))); } void CSimpleJoinScreen::onChange(const std::string & newText) { diff --git a/client/CPreGame.h b/client/CPreGame.h index 89be9799e..f4052799d 100644 --- a/client/CPreGame.h +++ b/client/CPreGame.h @@ -371,7 +371,7 @@ public: bool ongoingClosing; ui8 myNameID; //used when networking - otherwise all player are "mine" - CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map * Names = nullptr, const std::string & Address = "", const std::string & Port = ""); + CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map * Names = nullptr, const std::string & Address = "", const ui16 Port = 0); ~CSelectionScreen(); void toggleTab(CIntObject *tab); void changeSelection(const CMapInfo *to); diff --git a/client/Client.cpp b/client/Client.cpp index 99cb331e9..7b1e674a7 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -251,24 +251,16 @@ void CClient::endGame(bool closeConnection /*= true*/) } #if 1 -void CClient::loadGame(const std::string & fname, const bool server, const std::vector& humanplayerindices, const int loadNumPlayers, int player_, const std::string & ipaddr, const std::string & port) +void CClient::loadGame(const std::string & fname, const bool server, const std::vector& humanplayerindices, const int loadNumPlayers, int player_, const std::string & ipaddr, const ui16 port) { PlayerColor player(player_); //intentional shadowing logNetwork->infoStream() << "Loading procedure started!"; - std::string realPort; - if(settings["testing"]["enabled"].Bool()) - realPort = settings["testing"]["port"].String(); - else if(port.size()) - realPort = port; - else - realPort = boost::lexical_cast(settings["server"]["port"].Float()); - CServerHandler sh; if(server) sh.startServer(); else - serv = sh.justConnectToServer(ipaddr, realPort); + serv = sh.justConnectToServer(ipaddr, port); CStopWatch tmh; std::unique_ptr loader; @@ -998,7 +990,6 @@ CConnection * CServerHandler::connectToServer() { if(!shared->sr->ready) waitForServer(); - port = boost::lexical_cast(shared->sr->port); } #else waitForServer(); @@ -1006,7 +997,11 @@ CConnection * CServerHandler::connectToServer() th.update(); //put breakpoint here to attach to server before it does something stupid - CConnection *ret = justConnectToServer(settings["server"]["server"].String(), port); +#ifndef VCMI_ANDROID + CConnection *ret = justConnectToServer(settings["server"]["server"].String(), shared ? shared->sr->port : 0); +#else + CConnection *ret = justConnectToServer(settings["server"]["server"].String()); +#endif if(verbose) logNetwork->infoStream()<<"\tConnecting to the server: "<(getDefaultPort()); +} + CServerHandler::CServerHandler(bool runServer /*= false*/) { serverThread = nullptr; shared = nullptr; - if(settings["testing"]["enabled"].Bool()) - port = settings["testing"]["port"].String(); - else - port = boost::lexical_cast(settings["server"]["port"].Float()); verbose = true; #ifndef VCMI_ANDROID @@ -1055,7 +1059,7 @@ void CServerHandler::callServer() setThreadName("CServerHandler::callServer"); const std::string logName = (VCMIDirs::get().userCachePath() / "server_log.txt").string(); const std::string comm = VCMIDirs::get().serverPath().string() - + " --port=" + port + + " --port=" + getDefaultPortStr() + " --run-by-client" + (shared ? " --use-shm" : "") + " > \"" + logName + '\"'; @@ -1075,16 +1079,8 @@ void CServerHandler::callServer() #endif } -CConnection * CServerHandler::justConnectToServer(const std::string &host, const std::string &port) +CConnection * CServerHandler::justConnectToServer(const std::string &host, const ui16 port) { - std::string realPort; - if(settings["testing"]["enabled"].Bool()) - realPort = settings["testing"]["port"].String(); - else if(port.size()) - realPort = port; - else - realPort = boost::lexical_cast(settings["server"]["port"].Float()); - CConnection *ret = nullptr; while(!ret) { @@ -1092,7 +1088,7 @@ CConnection * CServerHandler::justConnectToServer(const std::string &host, const { logNetwork->infoStream() << "Establishing connection..."; ret = new CConnection( host.size() ? host : settings["server"]["server"].String(), - realPort, + port ? port : getDefaultPort(), NAME); ret->connectionID = 1; // TODO: Refactoring for the server so IDs set outside of CConnection } diff --git a/client/Client.h b/client/Client.h index ef7c55836..ecd5c201a 100644 --- a/client/Client.h +++ b/client/Client.h @@ -48,7 +48,6 @@ public: boost::thread *serverThread; //thread that called system to run server SharedMem *shared; //interprocess memory (for waiting for server) bool verbose; //whether to print log msgs - std::string port; //port number in text form //functions setting up local server void startServer(); //creates a thread with callServer @@ -56,7 +55,9 @@ public: CConnection * connectToServer(); //connects to server ////////////////////////////////////////////////////////////////////////// - static CConnection * justConnectToServer(const std::string &host = "", const std::string &port = ""); //connects to given host without taking any other actions (like setting up server) + static CConnection * justConnectToServer(const std::string &host = "", const ui16 port = 0); //connects to given host without taking any other actions (like setting up server) + static ui16 getDefaultPort(); + static std::string getDefaultPortStr(); CServerHandler(bool runServer = false); virtual ~CServerHandler(); @@ -154,7 +155,7 @@ public: void endGame(bool closeConnection = true); void stopConnection(); void save(const std::string & fname); - void loadGame(const std::string & fname, const bool server = true, const std::vector& humanplayerindices = std::vector(), const int loadnumplayers = 1, int player_ = -1, const std::string & ipaddr = "", const std::string & port = ""); + void loadGame(const std::string & fname, const bool server = true, const std::vector& humanplayerindices = std::vector(), const int loadnumplayers = 1, int player_ = -1, const std::string & ipaddr = "", const ui16 port = 0); void run(); void campaignMapFinished( std::shared_ptr camp ); void finishCampaign( std::shared_ptr camp ); diff --git a/launcher/settingsView/csettingsview_moc.cpp b/launcher/settingsView/csettingsview_moc.cpp index ab6fb4572..8d12ee54a 100644 --- a/launcher/settingsView/csettingsview_moc.cpp +++ b/launcher/settingsView/csettingsview_moc.cpp @@ -55,7 +55,7 @@ void CSettingsView::loadSettings() ui->comboBoxEnemyAI->setCurrentIndex(enemyAIIndex); ui->comboBoxPlayerAI->setCurrentIndex(playerAIIndex); - ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Float()); + ui->spinBoxNetworkPort->setValue(settings["server"]["port"].Integer()); ui->comboBoxAutoCheck->setCurrentIndex(settings["launcher"]["autoCheckRepositories"].Bool()); // all calls to plainText will trigger textChanged() signal overwriting config. Create backup before editing widget diff --git a/lib/serializer/Connection.cpp b/lib/serializer/Connection.cpp index 606687fe4..7a292d3cb 100644 --- a/lib/serializer/Connection.cpp +++ b/lib/serializer/Connection.cpp @@ -61,14 +61,14 @@ void CConnection::init() iser.fileVersion = SERIALIZATION_VERSION; } -CConnection::CConnection(std::string host, std::string port, std::string Name) +CConnection::CConnection(std::string host, ui16 port, std::string Name) :iser(this), oser(this), io_service(new asio::io_service), name(Name) { int i; boost::system::error_code error = asio::error::host_not_found; socket = new tcp::socket(*io_service); tcp::resolver resolver(*io_service); - tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port),error); + tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host, boost::lexical_cast(port)),error); if(error) { logNetwork->errorStream() << "Problem with resolving: \n" << error; diff --git a/lib/serializer/Connection.h b/lib/serializer/Connection.h index 2402d4db3..6d069c7c8 100644 --- a/lib/serializer/Connection.h +++ b/lib/serializer/Connection.h @@ -68,7 +68,7 @@ public: bool receivedStop, sendStop; - CConnection(std::string host, std::string port, std::string Name); + CConnection(std::string host, ui16 port, std::string Name); CConnection(TAcceptor * acceptor, boost::asio::io_service *Io_service, std::string Name); CConnection(TSocket * Socket, std::string Name); //use immediately after accepting connection into socket