1
0
Fork 0

Codechange: [Network] Use a single NetworkServerGameInfo object at server side and serialize that for the clients

pull/9197/head
rubidium42 2021-05-05 19:15:37 +02:00 committed by rubidium42
parent 24e7cb4947
commit 72bd62fd70
5 changed files with 42 additions and 48 deletions

View File

@ -122,30 +122,30 @@ void CheckGameCompatibility(NetworkGameInfo &ngi)
} }
/** /**
* Fill a NetworkGameInfo structure with the latest information of the server. * Get the NetworkServerGameInfo structure with the latest information of the server.
* @param ngi the NetworkGameInfo struct to fill with data. * @return The current NetworkServerGameInfo.
*/ */
void FillNetworkGameInfo(NetworkGameInfo &ngi) const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo()
{ {
/* Update some game_info */ _network_game_info.use_password = !StrEmpty(_settings_client.network.server_password);
ngi.clients_on = _network_game_info.clients_on; _network_game_info.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1);
ngi.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;
ngi.use_password = !StrEmpty(_settings_client.network.server_password); strecpy(_network_game_info.server_name, _settings_client.network.server_name, lastof(_network_game_info.server_name));
ngi.clients_max = _settings_client.network.max_clients; strecpy(_network_game_info.server_revision, GetNetworkRevisionString(), lastof(_network_game_info.server_revision));
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)); /* Client_on is used as global variable to keep track on the number of clients. */
strecpy(ngi.server_revision, GetNetworkRevisionString(), lastof(ngi.server_revision)); _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 p the packet to write the data to.
* @param info the NetworkGameInfo struct to serialize from. * @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); 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_string(""); // Used to be map-name.
p->Send_uint16(info->map_width); p->Send_uint16(info->map_width);
p->Send_uint16(info->map_height); p->Send_uint16(info->map_height);
p->Send_uint8 (info->map_set); p->Send_uint8 (info->landscape);
p->Send_bool (info->dedicated); 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. while (p->Recv_uint8() != 0) {} // Used to contain the map-name.
info->map_width = p->Recv_uint16(); info->map_width = p->Recv_uint16();
info->map_height = 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 (); 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;
} }
} }

View File

@ -57,17 +57,9 @@
*/ */
/** /**
* The game information that is not generated on-the-fly and has to * The game information that is sent from the server to the client.
* be sent to the clients.
*/ */
struct NetworkServerGameInfo { 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 GRFConfig *grfconfig; ///< List of NewGRF files used
Date start_date; ///< When the game started Date start_date; ///< When the game started
Date game_date; ///< Current date Date game_date; ///< Current date
@ -76,16 +68,24 @@ struct NetworkGameInfo : NetworkServerGameInfo {
char server_name[NETWORK_NAME_LENGTH]; ///< Server name 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) 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 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? 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 clients_max; ///< Max clients allowed on server
byte companies_on; ///< How many started companies do we have byte companies_on; ///< How many started companies do we have
byte companies_max; ///< Max companies allowed on server byte companies_max; ///< Max companies allowed on server
byte spectators_on; ///< How many spectators do we have? byte spectators_on; ///< How many spectators do we have?
byte spectators_max; ///< Max spectators allowed on server 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; extern NetworkServerGameInfo _network_game_info;
@ -94,12 +94,12 @@ const char *GetNetworkRevisionString();
bool IsNetworkCompatibleVersion(const char *other); bool IsNetworkCompatibleVersion(const char *other);
void CheckGameCompatibility(NetworkGameInfo &ngi); void CheckGameCompatibility(NetworkGameInfo &ngi);
void FillNetworkGameInfo(NetworkGameInfo &ngi); const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo();
void DeserializeGRFIdentifier(Packet *p, GRFIdentifier *grf); void DeserializeGRFIdentifier(Packet *p, GRFIdentifier *grf);
void SerializeGRFIdentifier(Packet *p, const GRFIdentifier *grf); void SerializeGRFIdentifier(Packet *p, const GRFIdentifier *grf);
void DeserializeNetworkGameInfo(Packet *p, NetworkGameInfo *info); 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 */ #endif /* NETWORK_CORE_GAME_INFO_H */

View File

@ -633,7 +633,7 @@ public:
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS); DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
y += FONT_HEIGHT_NORMAL; 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 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE); // landscape
y += FONT_HEIGHT_NORMAL; y += FONT_HEIGHT_NORMAL;

View File

@ -356,11 +356,8 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendClientInfo(NetworkClientIn
/** Send the client information about the server. */ /** Send the client information about the server. */
NetworkRecvStatus ServerNetworkGameSocketHandler::SendGameInfo() NetworkRecvStatus ServerNetworkGameSocketHandler::SendGameInfo()
{ {
NetworkGameInfo ngi;
FillNetworkGameInfo(ngi);
Packet *p = new Packet(PACKET_SERVER_GAME_INFO); Packet *p = new Packet(PACKET_SERVER_GAME_INFO);
SerializeNetworkGameInfo(p, &ngi); SerializeNetworkGameInfo(p, GetCurrentNetworkServerGameInfo());
this->SendPacket(p); this->SendPacket(p);

View File

@ -170,11 +170,8 @@ void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *p, Networ
return; return;
} }
NetworkGameInfo ngi;
FillNetworkGameInfo(ngi);
Packet packet(PACKET_UDP_SERVER_RESPONSE); Packet packet(PACKET_UDP_SERVER_RESPONSE);
SerializeNetworkGameInfo(&packet, &ngi); SerializeNetworkGameInfo(&packet, GetCurrentNetworkServerGameInfo());
/* Let the client know that we are here */ /* Let the client know that we are here */
this->SendPacket(&packet, client_addr); this->SendPacket(&packet, client_addr);