diff options
author | rubidium42 <rubidium@openttd.org> | 2021-05-05 19:15:37 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-05-05 21:01:23 +0200 |
commit | 72bd62fd705c495d90f08adc964ff8d7f84c6cc1 (patch) | |
tree | 300f3f5066f48624e1f56dc5418d68612c690bf9 /src | |
parent | 24e7cb494738a8e0117dbd8c4a148a9ee532e129 (diff) | |
download | openttd-72bd62fd705c495d90f08adc964ff8d7f84c6cc1.tar.xz |
Codechange: [Network] Use a single NetworkServerGameInfo object at server side and serialize that for the clients
Diffstat (limited to 'src')
-rw-r--r-- | src/network/core/game_info.cpp | 52 | ||||
-rw-r--r-- | src/network/core/game_info.h | 30 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 2 | ||||
-rw-r--r-- | src/network/network_server.cpp | 5 | ||||
-rw-r--r-- | src/network/network_udp.cpp | 5 |
5 files changed, 44 insertions, 50 deletions
diff --git a/src/network/core/game_info.cpp b/src/network/core/game_info.cpp index 55b5d1749..e5e85105e 100644 --- a/src/network/core/game_info.cpp +++ b/src/network/core/game_info.cpp @@ -122,30 +122,30 @@ void CheckGameCompatibility(NetworkGameInfo &ngi) } /** - * Fill a NetworkGameInfo structure with the latest information of the server. - * @param ngi the NetworkGameInfo struct to fill with data. + * Get the NetworkServerGameInfo structure with the latest information of the server. + * @return The current NetworkServerGameInfo. */ -void FillNetworkGameInfo(NetworkGameInfo &ngi) +const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo() { - /* Update some game_info */ - ngi.clients_on = _network_game_info.clients_on; - ngi.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1); - - ngi.use_password = !StrEmpty(_settings_client.network.server_password); - ngi.clients_max = _settings_client.network.max_clients; - ngi.companies_on = (byte)Company::GetNumItems(); - ngi.companies_max = _settings_client.network.max_companies; - ngi.spectators_on = NetworkSpectatorCount(); - ngi.spectators_max = _settings_client.network.max_spectators; - ngi.game_date = _date; - ngi.map_width = MapSizeX(); - ngi.map_height = MapSizeY(); - ngi.map_set = _settings_game.game_creation.landscape; - ngi.dedicated = _network_dedicated; - ngi.grfconfig = _grfconfig; - - strecpy(ngi.server_name, _settings_client.network.server_name, lastof(ngi.server_name)); - strecpy(ngi.server_revision, GetNetworkRevisionString(), lastof(ngi.server_revision)); + _network_game_info.use_password = !StrEmpty(_settings_client.network.server_password); + _network_game_info.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1); + _network_game_info.clients_max = _settings_client.network.max_clients; + _network_game_info.companies_max = _settings_client.network.max_companies; + _network_game_info.spectators_max = _settings_client.network.max_spectators; + _network_game_info.map_width = MapSizeX(); + _network_game_info.map_height = MapSizeY(); + _network_game_info.landscape = _settings_game.game_creation.landscape; + _network_game_info.dedicated = _network_dedicated; + _network_game_info.grfconfig = _grfconfig; + + strecpy(_network_game_info.server_name, _settings_client.network.server_name, lastof(_network_game_info.server_name)); + strecpy(_network_game_info.server_revision, GetNetworkRevisionString(), lastof(_network_game_info.server_revision)); + + /* Client_on is used as global variable to keep track on the number of clients. */ + _network_game_info.companies_on = (byte)Company::GetNumItems(); + _network_game_info.spectators_on = NetworkSpectatorCount(); + _network_game_info.game_date = _date; + return &_network_game_info; } /** @@ -179,7 +179,7 @@ static void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) * @param p the packet to write the data to. * @param info the NetworkGameInfo struct to serialize from. */ -void SerializeNetworkGameInfo(Packet *p, const NetworkGameInfo *info) +void SerializeNetworkGameInfo(Packet *p, const NetworkServerGameInfo *info) { p->Send_uint8 (NETWORK_GAME_INFO_VERSION); @@ -232,7 +232,7 @@ void SerializeNetworkGameInfo(Packet *p, const NetworkGameInfo *info) p->Send_string(""); // Used to be map-name. p->Send_uint16(info->map_width); p->Send_uint16(info->map_height); - p->Send_uint8 (info->map_set); + p->Send_uint8 (info->landscape); p->Send_bool (info->dedicated); } @@ -302,10 +302,10 @@ void DeserializeNetworkGameInfo(Packet *p, NetworkGameInfo *info) while (p->Recv_uint8() != 0) {} // Used to contain the map-name. info->map_width = p->Recv_uint16(); info->map_height = p->Recv_uint16(); - info->map_set = p->Recv_uint8 (); + info->landscape = p->Recv_uint8 (); info->dedicated = p->Recv_bool (); - if (info->map_set >= NETWORK_NUM_LANDSCAPES) info->map_set = 0; + if (info->landscape >= NETWORK_NUM_LANDSCAPES) info->landscape = 0; } } diff --git a/src/network/core/game_info.h b/src/network/core/game_info.h index 324c6ea4a..10f922f31 100644 --- a/src/network/core/game_info.h +++ b/src/network/core/game_info.h @@ -57,17 +57,9 @@ */ /** - * The game information that is not generated on-the-fly and has to - * be sent to the clients. + * The game information that is sent from the server to the client. */ struct NetworkServerGameInfo { - byte clients_on; ///< Current count of clients on server -}; - -/** - * The game information that is sent from the server to the clients. - */ -struct NetworkGameInfo : NetworkServerGameInfo { GRFConfig *grfconfig; ///< List of NewGRF files used Date start_date; ///< When the game started Date game_date; ///< Current date @@ -76,16 +68,24 @@ struct NetworkGameInfo : NetworkServerGameInfo { char server_name[NETWORK_NAME_LENGTH]; ///< Server name char server_revision[NETWORK_REVISION_LENGTH]; ///< The version number the server is using (e.g.: 'r304' or 0.5.0) bool dedicated; ///< Is this a dedicated server? - bool version_compatible; ///< Can we connect to this server or not? (based on server_revision) - bool compatible; ///< Can we connect to this server or not? (based on server_revision _and_ grf_match bool use_password; ///< Is this server passworded? - byte game_info_version; ///< Version of the game info + byte clients_on; ///< Current count of clients on server byte clients_max; ///< Max clients allowed on server byte companies_on; ///< How many started companies do we have byte companies_max; ///< Max companies allowed on server byte spectators_on; ///< How many spectators do we have? byte spectators_max; ///< Max spectators allowed on server - byte map_set; ///< Graphical set + byte landscape; ///< The used landscape +}; + +/** + * The game information that is sent from the server to the clients + * with extra information only required at the client side. + */ +struct NetworkGameInfo : NetworkServerGameInfo { + bool version_compatible; ///< Can we connect to this server or not? (based on server_revision) + bool compatible; ///< Can we connect to this server or not? (based on server_revision _and_ grf_match + byte game_info_version; ///< Version of the game info }; extern NetworkServerGameInfo _network_game_info; @@ -94,12 +94,12 @@ const char *GetNetworkRevisionString(); bool IsNetworkCompatibleVersion(const char *other); void CheckGameCompatibility(NetworkGameInfo &ngi); -void FillNetworkGameInfo(NetworkGameInfo &ngi); +const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo(); void DeserializeGRFIdentifier(Packet *p, GRFIdentifier *grf); void SerializeGRFIdentifier(Packet *p, const GRFIdentifier *grf); void DeserializeNetworkGameInfo(Packet *p, NetworkGameInfo *info); -void SerializeNetworkGameInfo(Packet *p, const NetworkGameInfo *info); +void SerializeNetworkGameInfo(Packet *p, const NetworkServerGameInfo *info); #endif /* NETWORK_CORE_GAME_INFO_H */ diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index b2feacc95..ac86b868c 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -633,7 +633,7 @@ public: DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS); y += FONT_HEIGHT_NORMAL; - SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + sel->info.map_set); + SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + sel->info.landscape); DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE); // landscape y += FONT_HEIGHT_NORMAL; diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index fd69a8d37..905290bfc 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -356,11 +356,8 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendClientInfo(NetworkClientIn /** Send the client information about the server. */ NetworkRecvStatus ServerNetworkGameSocketHandler::SendGameInfo() { - NetworkGameInfo ngi; - FillNetworkGameInfo(ngi); - Packet *p = new Packet(PACKET_SERVER_GAME_INFO); - SerializeNetworkGameInfo(p, &ngi); + SerializeNetworkGameInfo(p, GetCurrentNetworkServerGameInfo()); this->SendPacket(p); diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp index bb71e5cea..de69e4a9a 100644 --- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -170,11 +170,8 @@ void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *p, Networ return; } - NetworkGameInfo ngi; - FillNetworkGameInfo(ngi); - Packet packet(PACKET_UDP_SERVER_RESPONSE); - SerializeNetworkGameInfo(&packet, &ngi); + SerializeNetworkGameInfo(&packet, GetCurrentNetworkServerGameInfo()); /* Let the client know that we are here */ this->SendPacket(&packet, client_addr); |