Changeset - r25385:ad6834a4b5cc
[Not reviewed]
master
0 5 0
rubidium42 - 3 years ago 2021-05-05 17:15:37
rubidium@openttd.org
Codechange: [Network] Use a single NetworkServerGameInfo object at server side and serialize that for the clients
5 files changed with 42 insertions and 48 deletions:
0 comments (0 inline, 0 general)
src/network/core/game_info.cpp
Show inline comments
 
@@ -122,30 +122,30 @@ void CheckGameCompatibility(NetworkGameI
 
}
 

	
 
/**
 
 * 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);
 
	_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;
 

	
 
	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(_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));
 

	
 
	strecpy(ngi.server_name, _settings_client.network.server_name, lastof(ngi.server_name));
 
	strecpy(ngi.server_revision, GetNetworkRevisionString(), lastof(ngi.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 HandleIncomingNetworkGameInf
 
 * @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,
 
	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 *
 
			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;
 
	}
 
}
 

	
src/network/core/game_info.h
Show inline comments
 
@@ -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 : NetworkServerGa
 
	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 */
src/network/network_gui.cpp
Show inline comments
 
@@ -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;
 

	
src/network/network_server.cpp
Show inline comments
 
@@ -356,11 +356,8 @@ NetworkRecvStatus ServerNetworkGameSocke
 
/** 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);
 

	
src/network/network_udp.cpp
Show inline comments
 
@@ -170,11 +170,8 @@ void ServerNetworkUDPSocketHandler::Rece
 
		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);
0 comments (0 inline, 0 general)