Clean up lan and wan server code

This commit is contained in:
Benau 2018-03-02 15:21:27 +08:00
parent 80a9cc5c48
commit efa294d4f4
10 changed files with 114 additions and 74 deletions

View File

@ -584,7 +584,9 @@ void cmdLineHelp()
" --public-server Allow direct connection to the server (without stk server)\n"
" --lan-server=name Start a LAN server (not a playing client).\n"
" --server-password= Sets a password for a server (both client&server).\n"
" --connect-now=ip Connect to a server with IP known now (in format x.x.x.x:xxx(port)).\n"
" --connect-now=ip Connect to a server with IP known now\n"
" (in format x.x.x.x:xxx(port)), the port should be its\n"
" server discovery port.\n"
" --login=s Automatically log in (set the login).\n"
" --password=s Automatically log in (set the password).\n"
" --port=n Port number to use.\n"
@ -1087,8 +1089,11 @@ int handleCmdLine()
Log::info("main", "Try to connect to server '%s'.",
ip.toString().c_str() );
irr::core::stringw name = StringUtils::utf8ToWide(ip.toString());
ServersManager::get()->addServer(new Server(name, /*lan*/true,
16, 0, ip));
ServersManager::get()->addServer(new Server(0, name,
NetworkConfig::get()->getMaxPlayers(), 0,
race_manager->getDifficulty(),
NetworkConfig::get()->getServerGameMode(race_manager->getMinorMode(),
race_manager->getMajorMode()), ip));
ServersManager::get()->setJoinedServer(0);
STKHost::create();
}

View File

@ -61,52 +61,69 @@ void NetworkConfig::setIsServer(bool b)
} // setIsServer
// ----------------------------------------------------------------------------
unsigned NetworkConfig::getServerGameMode(RaceManager::MinorRaceModeType mode,
bool gp)
unsigned NetworkConfig::getServerGameMode(RaceManager::MinorRaceModeType minor,
RaceManager::MajorRaceModeType major)
{
if (gp)
if (major == RaceManager::MAJOR_MODE_GRAND_PRIX)
{
if (mode == RaceManager::MINOR_MODE_NORMAL_RACE)
if (minor == RaceManager::MINOR_MODE_NORMAL_RACE)
return 0;
else if (mode == RaceManager::MINOR_MODE_TIME_TRIAL)
else if (minor == RaceManager::MINOR_MODE_TIME_TRIAL)
return 1;
else if (minor == RaceManager::MINOR_MODE_FOLLOW_LEADER)
return 2;
}
else
{
if (mode == RaceManager::MINOR_MODE_NORMAL_RACE)
return 2;
else if (mode == RaceManager::MINOR_MODE_TIME_TRIAL)
if (minor == RaceManager::MINOR_MODE_NORMAL_RACE)
return 3;
else if (mode == RaceManager::MINOR_MODE_3_STRIKES)
else if (minor == RaceManager::MINOR_MODE_TIME_TRIAL)
return 4;
else if (mode == RaceManager::MINOR_MODE_SOCCER)
else if (minor == RaceManager::MINOR_MODE_FOLLOW_LEADER)
return 5;
else if (minor == RaceManager::MINOR_MODE_3_STRIKES)
return 6;
else if (minor == RaceManager::MINOR_MODE_SOCCER)
return 7;
}
return 0;
} // getServerGameMode
// ----------------------------------------------------------------------------
std::pair<RaceManager::MinorRaceModeType, bool>
std::pair<RaceManager::MinorRaceModeType, RaceManager::MajorRaceModeType>
NetworkConfig::getLocalGameMode(unsigned id)
{
switch(id)
{
case 0:
return { RaceManager::MINOR_MODE_NORMAL_RACE, true };
return { RaceManager::MINOR_MODE_NORMAL_RACE,
RaceManager::MAJOR_MODE_GRAND_PRIX };
case 1:
return { RaceManager::MINOR_MODE_TIME_TRIAL, true };
return { RaceManager::MINOR_MODE_TIME_TRIAL,
RaceManager::MAJOR_MODE_GRAND_PRIX };
case 2:
return { RaceManager::MINOR_MODE_NORMAL_RACE, false };
return { RaceManager::MINOR_MODE_FOLLOW_LEADER,
RaceManager::MAJOR_MODE_GRAND_PRIX };
case 3:
return { RaceManager::MINOR_MODE_TIME_TRIAL, false };
return { RaceManager::MINOR_MODE_NORMAL_RACE,
RaceManager::MAJOR_MODE_SINGLE };
case 4:
return { RaceManager::MINOR_MODE_3_STRIKES, false };
return { RaceManager::MINOR_MODE_TIME_TRIAL,
RaceManager::MAJOR_MODE_SINGLE };
case 5:
return { RaceManager::MINOR_MODE_SOCCER, false };
return { RaceManager::MINOR_MODE_FOLLOW_LEADER,
RaceManager::MAJOR_MODE_SINGLE };
case 6:
return { RaceManager::MINOR_MODE_3_STRIKES,
RaceManager::MAJOR_MODE_SINGLE };
case 7:
return { RaceManager::MINOR_MODE_SOCCER,
RaceManager::MAJOR_MODE_SINGLE };
default:
break;
}
return { RaceManager::MINOR_MODE_NORMAL_RACE, false };
return { RaceManager::MINOR_MODE_NORMAL_RACE,
RaceManager::MAJOR_MODE_SINGLE };
} // getLocalGameMode

View File

@ -200,10 +200,12 @@ public:
bool isAutoConnect() const { return m_auto_connect; }
// ------------------------------------------------------------------------
/** Returns the game mode id for server database. */
unsigned getServerGameMode(RaceManager::MinorRaceModeType mode, bool gp);
unsigned getServerGameMode(RaceManager::MinorRaceModeType mode,
RaceManager::MajorRaceModeType);
// ------------------------------------------------------------------------
/** Returns the game mode id and if grandprix from server database id. */
std::pair<RaceManager::MinorRaceModeType, bool> getLocalGameMode(unsigned);
/** Returns the minor and majar game mode from server database id. */
std::pair<RaceManager::MinorRaceModeType, RaceManager::MajorRaceModeType>
getLocalGameMode(unsigned);
// ------------------------------------------------------------------------
void setDirectConnect(bool val) { m_direct_connect = val; }
// ------------------------------------------------------------------------

View File

@ -381,7 +381,7 @@ void ServerLobby::registerServer()
request->addParameter("difficulty", race_manager->getDifficulty());
request->addParameter("game_mode",
NetworkConfig::get()->getServerGameMode(race_manager->getMinorMode(),
race_manager->getMajorMode() == RaceManager::MAJOR_MODE_GRAND_PRIX));
race_manager->getMajorMode()));
Log::info("ServerLobby", "Public server addr %s", addr.toString().c_str());
request->executeNow();

View File

@ -26,9 +26,8 @@ Server::SortOrder Server::m_sort_order = Server::SO_NAME;
/** Constructor based on XML data received from the stk server.
* \param xml The data for one server as received as part of the
* get-all stk-server request.
* \param is_lan If this is a lan only server.
*/
Server::Server(const XMLNode & xml, bool is_lan)
Server::Server(const XMLNode& xml)
{
assert(xml.getName() == "server");
@ -37,10 +36,10 @@ Server::Server(const XMLNode & xml, bool is_lan)
m_server_id = 0;
m_current_players = 0;
m_max_players = 0;
m_is_lan = is_lan;
unsigned server_data = 0;
xml.get("game_mode", &server_data);
m_minor_mode = NetworkConfig::get()->getLocalGameMode(server_data).first;
m_major_mode = NetworkConfig::get()->getLocalGameMode(server_data).second;
xml.get("difficulty", &server_data);
m_difficulty = (RaceManager::Difficulty)server_data;
@ -63,29 +62,32 @@ Server::Server(const XMLNode & xml, bool is_lan)
// ----------------------------------------------------------------------------
/** Manual server creation, based on data received from a LAN server discovery
* (see ServersManager::getLANRefresh). This constructor is only used for
* LAN servers.
* (see ServersManager::getLANRefresh) or local graphics server creation
* where the server info is known already.
* \param server_id ID of server.
* \param name Name of the server.
* \param is_lan If this is a lan-only server.
* \param max_players Maximum number of players allowed on this server.
* \param current_players The currently connected number of players.
* \param difficulty The difficulty of server.
* \param server_mode The game modes of server (including minor and major).
* \param address IP and port of the server.
*/
Server::Server(const core::stringw &name, bool is_lan, int max_players,
int current_players, const TransportAddress &address)
Server::Server(unsigned server_id, const core::stringw &name, int max_players,
int current_players, unsigned difficulty, unsigned server_mode,
const TransportAddress &address)
{
m_name = name;
m_satisfaction_score = 0;
m_server_id = 0;
m_server_id = server_id;
m_current_players = current_players;
m_max_players = max_players;
m_is_lan = is_lan;
m_address.copy(address);
// In case of LAN server, public and private port are the same.
m_private_port = m_address.getPort();
m_minor_mode = RaceManager::MINOR_MODE_NORMAL_RACE;
m_difficulty = RaceManager::DIFFICULTY_HARD;
} // server(name, ...)
m_difficulty = (RaceManager::Difficulty)difficulty;
m_minor_mode = NetworkConfig::get()->getLocalGameMode(server_mode).first;
m_major_mode = NetworkConfig::get()->getLocalGameMode(server_mode).second;
} // server(server_id, ...)
// ----------------------------------------------------------------------------
/** \brief Filter the add-on with a list of words.

View File

@ -84,6 +84,8 @@ protected:
RaceManager::MinorRaceModeType m_minor_mode;
RaceManager::MajorRaceModeType m_major_mode;
RaceManager::Difficulty m_difficulty;
/** The sort order to be used in the comparison. */
@ -92,9 +94,10 @@ protected:
public:
/** Initialises the object from an XML node. */
Server(const XMLNode &xml, bool is_lan);
Server(const irr::core::stringw &name, bool is_lan, int max_players,
int current_players, const TransportAddress &address);
Server(const XMLNode &xml);
Server(unsigned server_id, const irr::core::stringw &name,
int max_players, int current_players, unsigned difficulty,
unsigned server_mode, const TransportAddress &address);
bool filterByWords(const irr::core::stringw words) const;
// ------------------------------------------------------------------------
/** Returns ip address and port of this server. */
@ -120,13 +123,13 @@ public:
/** Returns the number of currently connected players. */
const int getCurrentPlayers() const { return m_current_players; }
// ------------------------------------------------------------------------
RaceManager::MinorRaceModeType getRaceMinorMode() const { return m_minor_mode; }
RaceManager::MinorRaceModeType getRaceMinorMode() const
{ return m_minor_mode; }
// ------------------------------------------------------------------------
void setRaceMinorMode(RaceManager::MinorRaceModeType m) { m_minor_mode = m; }
RaceManager::MajorRaceModeType getRaceMajorMode() const
{ return m_major_mode; }
// ------------------------------------------------------------------------
RaceManager::Difficulty getDifficulty() const { return m_difficulty; }
// ------------------------------------------------------------------------
void setDifficulty(RaceManager::Difficulty d) { m_difficulty = d; }
RaceManager::Difficulty getDifficulty() const { return m_difficulty; }
// ------------------------------------------------------------------------
/** Compares two servers according to the sort order currently defined.
* \param a The addon to compare this addon to.

View File

@ -169,6 +169,8 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
// any local servers.
double start_time = StkTime::getRealTime();
const double DURATION = 1.0;
int cur_server_id = ServersManager::get()->getNumServers();
assert(cur_server_id == 0);
while(StkTime::getRealTime() - start_time < DURATION)
{
TransportAddress sender;
@ -183,12 +185,10 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const
uint8_t players = s.getUInt8();
uint32_t my_ip = s.getUInt32();
uint16_t my_port = s.getUInt16();
uint16_t mode = s.getUInt16();
uint8_t difficulty = s.getUInt8();
Server* server = new Server(name, /*lan*/true,
max_players, players, sender);
server->setDifficulty((RaceManager::Difficulty)difficulty);
server->setRaceMinorMode((RaceManager::MinorRaceModeType)mode);
uint8_t mode = s.getUInt8();
Server* server = new Server(cur_server_id++, name,
max_players,players, difficulty, mode, sender);
ServersManager::get()->addServer(server);
m_success = true;
} // if received_data
@ -259,7 +259,7 @@ void ServersManager::refresh(bool success, const XMLNode *input)
const XMLNode *servers_xml = input->getNode("servers");
for (unsigned int i = 0; i < servers_xml->getNumNodes(); i++)
{
addServer(new Server(*servers_xml->getNode(i), /*is_lan*/false));
addServer(new Server(*servers_xml->getNode(i)));
}
m_last_load_time.setAtomic((float)StkTime::getRealTime());
} // refresh

View File

@ -833,8 +833,10 @@ void STKHost::handleDirectSocketRequest(Network* lan_network)
s.addUInt8(0); // FIXME: current number of connected players
s.addUInt32(sender.getIP());
s.addUInt16(sender.getPort());
s.addUInt16((uint16_t)race_manager->getMinorMode());
s.addUInt8((uint8_t)race_manager->getDifficulty());
s.addUInt8((uint8_t)
NetworkConfig::get()->getServerGameMode(race_manager->getMinorMode(),
race_manager->getMajorMode()));
lan_network->sendRawPacket(s, sender);
} // if message is server-requested
else if (command == "connection-request")

View File

@ -175,19 +175,24 @@ void CreateServerScreen::createServer()
if (!password.empty())
password = std::string(" --server-password=") + password;
ServersManager::get()->cleanUpServers();
TransportAddress server_address(0x7f000001,
NetworkConfig::get()->getServerDiscoveryPort());
TransportAddress address(0x7f000001,
NetworkConfig::get()->getServerDiscoveryPort());
Server *server = new Server(0/*server_id*/, name, max_players,
/*current_player*/0, (RaceManager::Difficulty)
difficulty_widget->getSelection(PLAYER_ID_GAME_MASTER),
NetworkConfig::get()->getServerGameMode(race_manager->getMinorMode(),
race_manager->getMajorMode()), server_address);
ServersManager::get()->addServer(server);
ServersManager::get()->setJoinedServer(0);
#undef USE_GRAPHICS_SERVER
#ifdef USE_GRAPHICS_SERVER
NetworkConfig::get()->setIsServer(true);
// In case of a LAN game, we can create the new server object now
if (NetworkConfig::get()->isLAN())
{
// FIXME Is this actually necessary?? Only in case of WAN, or LAN and WAN?
TransportAddress address(0x7f000001,0); // 127.0.0.1
Server *server = new Server(name, /*lan*/true, max_players,
/*current_player*/1, address);
ServersManager::get()->addServer(server);
}
// In case of a WAN game, we register this server with the
// stk server, and will get the server's id when this
// request is finished.
@ -246,14 +251,6 @@ void CreateServerScreen::createServer()
SeparateProcess* sp =
new SeparateProcess(SeparateProcess::getCurrentExecutableLocation(),
server_cfg.str() + password);
ServersManager::get()->cleanUpServers();
TransportAddress address(0x7f000001,
NetworkConfig::get()->getServerDiscoveryPort());
Server *server = new Server(name, /*lan*/true, max_players,
/*current_player*/0, address);
ServersManager::get()->addServer(server);
ServersManager::get()->setJoinedServer(0);
STKHost::create(sp);
#endif

View File

@ -108,16 +108,28 @@ void NetworkingLobby::init()
{
m_server_name = server->getName();
core::stringw each_line;
//I18N: In the networking lobby
each_line = _("Server name: %s", m_server_name);
m_server_info.push_back(each_line);
const core::stringw& difficulty_name =
race_manager->getDifficultyName(race_manager->getDifficulty());
race_manager->getDifficultyName(server->getDifficulty());
//I18N: In the networking lobby
each_line = _("Difficulty: %s", difficulty_name);
m_server_info.push_back(each_line);
//I18N: In the networking lobby
each_line = _("Max players: %d", server->getMaxPlayers());
m_server_info.push_back(each_line);
//I18N: In the networking lobby
core::stringw mode = RaceManager::getNameOf(server->getRaceMinorMode());
each_line = _("Game mode: %s", mode);
race_manager->setMinorMode(server->getRaceMinorMode());
race_manager->setMajorMode(server->getRaceMajorMode());
race_manager->setDifficulty(server->getDifficulty());
m_server_info.push_back(each_line);
}