Use server id file to determine the status of no-graphics server

This commit is contained in:
Benau 2018-02-28 16:09:53 +08:00
parent e760642842
commit 13d4cc50cf
6 changed files with 57 additions and 7 deletions

View File

@ -1049,6 +1049,12 @@ int handleCmdLine()
NetworkConfig::get()->setPassword(StringUtils::wideToUtf8(pw));
}
if (CommandLine::has("--server-id-file", &s))
{
NetworkConfig::get()->setServerIdFile(
file_manager->getUserConfigFile(s));
}
if(CommandLine::has("--max-players", &n))
UserConfigParams::m_server_max_players=n;
NetworkConfig::get()->
@ -1100,7 +1106,7 @@ int handleCmdLine()
}
can_wan = true;
}
else
else if (!can_wan)
{
Log::warn("main", "No saved online player session to create a wan server");
}

View File

@ -24,7 +24,6 @@
#include "network/transport_address.hpp"
#include "race/race_manager.hpp"
#include "utils/synchronised.hpp"
#include "irrString.h"
@ -88,6 +87,9 @@ private:
uint32_t m_cur_user_id;
std::string m_cur_user_token;
/** Used by client server to determine if the child server is created. */
std::string m_server_id_file;
NetworkConfig();
public:
@ -217,6 +219,9 @@ public:
// ------------------------------------------------------------------------
void setUserDetails(Online::XMLRequest* r, const std::string& name);
// ------------------------------------------------------------------------
void setServerIdFile(const std::string& id) { m_server_id_file = id; }
// ------------------------------------------------------------------------
const std::string& getServerIdFile() const { return m_server_id_file; }
}; // class NetworkConfig

View File

@ -81,9 +81,22 @@ void RequestConnection::asynchronousUpdate()
{
if (STKHost::get()->isClientServer())
{
// Allow 10 seconds for the separate process to fully
// start-up
StkTime::sleep(10000);
// Allow up to 10 seconds for the separate process to
// fully start-up
double timeout = StkTime::getRealTime() + 10.;
while (StkTime::getRealTime() < timeout)
{
const std::string& sid = NetworkConfig::get()
->getServerIdFile();
assert(!sid.empty());
if (file_manager->fileExists(sid))
{
file_manager->removeFile(sid);
break;
}
StkTime::sleep(10);
}
NetworkConfig::get()->setServerIdFile("");
}
const Server *server =
ServersManager::get()->getServerByID(m_server_id);

View File

@ -41,6 +41,8 @@
#include "utils/random_generator.hpp"
#include "utils/time.hpp"
#include <fstream>
/** This is the central game setup protocol running in the server. It is
* mostly a finite state machine. Note that all nodes in ellipses and light
* grey background are actual states; nodes in boxes and white background
@ -190,6 +192,20 @@ bool ServerLobby::notifyEventAsynchronous(Event* event)
return true;
} // notifyEventAsynchronous
//-----------------------------------------------------------------------------
/** Create the server id file to let the graphics server client connect. */
void ServerLobby::createServerIdFile()
{
const std::string& sid = NetworkConfig::get()->getServerIdFile();
if (!sid.empty())
{
std::fstream fs;
fs.open(sid, std::ios::out);
fs.close();
NetworkConfig::get()->setServerIdFile("");
}
} // createServerIdFile
//-----------------------------------------------------------------------------
/** Find out the public IP server or poll STK server asynchronously. */
void ServerLobby::asynchronousUpdate()
@ -204,6 +220,7 @@ void ServerLobby::asynchronousUpdate()
{
m_state = ACCEPTING_CLIENTS;
STKHost::get()->startListening();
createServerIdFile();
return;
}
STKHost::get()->setPublicAddress();
@ -225,7 +242,10 @@ void ServerLobby::asynchronousUpdate()
// to react to any requests before the server is registered.
registerServer();
if (m_server_registered)
{
m_state = ACCEPTING_CLIENTS;
createServerIdFile();
}
break;
}
case ACCEPTING_CLIENTS:

View File

@ -83,6 +83,7 @@ private:
void finishedLoadingWorldClient(Event *event);
void startedRaceOnClient(Event *event);
void unregisterServer();
void createServerIdFile();
public:
ServerLobby();
virtual ~ServerLobby();

View File

@ -224,13 +224,18 @@ void CreateServerScreen::createServer()
server_string += token;
}
std::string server_id_file = "server_id_file_";
server_id_file += StringUtils::toString(StkTime::getTimeSinceEpoch());
NetworkConfig::get()->setServerIdFile(
file_manager->getUserConfigFile(server_id_file));
char option[1024];
sprintf(option, " --no-graphics --type=%d --difficulty=%d "
"--max-players=%d --network-console --no-console-log "
"--stdout=server.log",
"--stdout=server.log --server-id-file=%s",
gamemode_widget->getSelection(PLAYER_ID_GAME_MASTER),
difficulty_widget->getSelection(PLAYER_ID_GAME_MASTER),
max_players);
max_players, server_id_file.c_str());
SeparateProcess* sp =
new SeparateProcess(SeparateProcess::getCurrentExecutableLocation(),
server_string + option + password, "quit");