1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-18 03:21:27 +02:00

Client: server port and testing options cleanup

Now we only pass port as ui16 instead of std::string
This commit is contained in:
Arseniy Shestakov 2017-06-04 08:49:23 +03:00
parent 4a302d4fe5
commit 6642816b1e
9 changed files with 45 additions and 57 deletions

View File

@ -254,9 +254,9 @@ int main(int argc, char** argv)
("loadplayer", po::value<int>(),"specifies which player we are in multiplayer loaded games (0=Red, etc.)")
("loadserverip",po::value<std::string>(),"IP for loaded game server")
("loadserverport",po::value<std::string>(),"port for loaded game server")
("testingport",po::value<std::string>(),"port for testing, override specified in config file")
("testingfileprefix",po::value<std::string>(),"prefix for auto save files")
("testingsavefrequency",po::value<int>(),"how often auto save should be created");
("serverport", po::value<si64>(), "override port specified in config file")
("saveprefix", po::value<std::string>(), "prefix for auto save files")
("savefrequency", po::value<si64>(), "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<std::string>();
testingSettings["prefix"].String() = vm["testingfileprefix"].as<std::string>();
testingSettings["savefrequency"].Float() = vm.count("testingsavefrequency") ? vm["testingsavefrequency"].as<int>() : 1;
}
Settings session = settings.write["session"];
session["serverport"].Integer() = vm.count("serverport") ? vm["serverport"].as<si64>() : 0;
session["saveprefix"].String() = vm.count("saveprefix") ? vm["saveprefix"].as<std::string>() : "";
session["savefrequency"].Integer() = vm.count("savefrequency") ? vm["savefrequency"].as<si64>() : 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<int>>() : std::vector<int>(),vm.count("loadnumplayers") ? vm["loadnumplayers"].as<int>() : 1,vm["loadplayer"].as<int>(),vm.count("loadserverip") ? vm["loadserverip"].as<std::string>() : "", vm.count("loadserverport") ? vm["loadserverport"].as<std::string>() : "3030");
client->loadGame(fname,vm.count("loadserver"),vm.count("loadhumanplayerindices") ? vm["loadhumanplayerindices"].as<std::vector<int>>() : std::vector<int>(),vm.count("loadnumplayers") ? vm["loadnumplayers"].as<int>() : 1,vm["loadplayer"].as<int>(),vm.count("loadserverip") ? vm["loadserverip"].as<std::string>() : "", vm.count("loadserverport") ? vm["loadserverport"].as<ui16>() : CServerHandler::getDefaultPort());
break;
}

View File

@ -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<int>(settings["testing"]["savefrequency"].Float()) == 0)
else if (cb->getDate() % static_cast<int>(settings["session"]["savefrequency"].Integer()) == 0)
{
LOCPLINT->cb->save("Saves/" + prefix + "Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
autosaveCount %= 5;

View File

@ -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<ui8, std::string> * Names /*= nullptr*/, const std::string & Address /*=""*/, const std::string & Port /*= ""*/)
CSelectionScreen::CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer /*= CMenuScreen::SINGLE_PLAYER*/, const std::map<ui8, std::string> * 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<std::string>(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<ui16>(textPort)));
}
void CSimpleJoinScreen::onChange(const std::string & newText)
{

View File

@ -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<ui8, std::string> * Names = nullptr, const std::string & Address = "", const std::string & Port = "");
CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map<ui8, std::string> * Names = nullptr, const std::string & Address = "", const ui16 Port = 0);
~CSelectionScreen();
void toggleTab(CIntObject *tab);
void changeSelection(const CMapInfo *to);

View File

@ -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<int>& 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<int>& 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<std::string>(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<CLoadFile> loader;
@ -998,7 +990,6 @@ CConnection * CServerHandler::connectToServer()
{
if(!shared->sr->ready)
waitForServer();
port = boost::lexical_cast<std::string>(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: "<<th.getDiff();
@ -1014,14 +1009,23 @@ CConnection * CServerHandler::connectToServer()
return ret;
}
ui16 CServerHandler::getDefaultPort()
{
if(settings["session"]["serverport"].Integer())
return settings["session"]["serverport"].Integer();
else
return settings["server"]["port"].Integer();
}
std::string CServerHandler::getDefaultPortStr()
{
return boost::lexical_cast<std::string>(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<std::string>(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<std::string>(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
}

View File

@ -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<int>& humanplayerindices = std::vector<int>(), 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<int>& humanplayerindices = std::vector<int>(), const int loadnumplayers = 1, int player_ = -1, const std::string & ipaddr = "", const ui16 port = 0);
void run();
void campaignMapFinished( std::shared_ptr<CCampaignState> camp );
void finishCampaign( std::shared_ptr<CCampaignState> camp );

View File

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

View File

@ -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<std::string>(port)),error);
if(error)
{
logNetwork->errorStream() << "Problem with resolving: \n" << error;

View File

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