changeset 11551:939f22f810ef draft

(svn r15916) -Codechange: let the network game list use NetworkAddress
author rubidium <rubidium@openttd.org>
date Thu, 02 Apr 2009 20:17:46 +0000
parents 1449b6fb6ce6
children c9c477fd6d49
files src/network/core/address.cpp src/network/core/address.h src/network/network.cpp src/network/network_gamelist.cpp src/network/network_gamelist.h src/network/network_gui.cpp src/network/network_udp.cpp
diffstat 7 files changed, 61 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -14,7 +14,9 @@
 const char *NetworkAddress::GetHostname()
 {
 	if (this->hostname == NULL) {
-		this->hostname = strdup(inet_ntoa(((struct sockaddr_in *)&this->address)->sin_addr));
+		char buf[NETWORK_HOSTNAME_LENGTH] = { '\0' };
+		getnameinfo((struct sockaddr *)&this->address, sizeof(this->address), buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
+		this->hostname = strdup(buf);
 	}
 	return this->hostname;
 }
@@ -41,6 +43,18 @@
 	}
 }
 
+void NetworkAddress::SetPort(uint16 port)
+{
+	switch (this->address.ss_family) {
+		case AF_INET:
+			((struct sockaddr_in*)&this->address)->sin_port = htons(port);
+			break;
+
+		default:
+			NOT_REACHED();
+	}
+}
+
 const char *NetworkAddress::GetAddressAsString()
 {
 	/* 6 = for the : and 5 for the decimal port number */
--- a/src/network/core/address.h
+++ b/src/network/core/address.h
@@ -33,7 +33,7 @@
 		memset(&this->address, 0, sizeof(this->address));
 		this->address.ss_family = AF_INET;
 		((struct sockaddr_in*)&this->address)->sin_addr.s_addr = ip;
-		((struct sockaddr_in*)&this->address)->sin_port = htons(port);
+		this->SetPort(port);
 	}
 
 	/**
@@ -58,7 +58,7 @@
 	{
 		memset(&this->address, 0, sizeof(this->address));
 		this->address.ss_family = AF_INET;
-		((struct sockaddr_in*)&this->address)->sin_port = htons(port);
+		this->SetPort(port);
 	}
 
 	/**
@@ -111,6 +111,12 @@
 	uint16 GetPort() const;
 
 	/**
+	 * Set the port
+	 * @param port set the port number
+	 */
+	void SetPort(uint16 port);
+
+	/**
 	 * Check whether the IP address has been resolved already
 	 * @return true iff the port has been resolved
 	 */
@@ -118,6 +124,19 @@
 	{
 		return this->resolved;
 	}
+
+	/**
+	 * Compare the address of this class with the address of another.
+	 * @param address the other address.
+	 */
+	bool operator == (NetworkAddress &address)
+	{
+		if (this->IsResolved() != address.IsResolved()) return false;
+
+		if (this->IsResolved()) return memcmp(&this->address, &address.address, sizeof(this->address)) == 0;
+
+		return this->GetPort() == address.GetPort() && strcmp(this->GetHostname(), address.GetHostname()) == 0;
+	}
 };
 
 #endif /* ENABLE_NETWORK */
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -714,7 +714,7 @@
 	while (item != NULL && i != lengthof(_network_host_list)) {
 		if (item->manually) {
 			free(_network_host_list[i]);
-			_network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->port);
+			_network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->address.GetPort());
 		}
 		item = item->next;
 	}
--- a/src/network/network_gamelist.cpp
+++ b/src/network/network_gamelist.cpp
@@ -41,7 +41,7 @@
 		NetworkGameList *ins_item = _network_game_delayed_insertion_list;
 		_network_game_delayed_insertion_list = ins_item->next;
 
-		NetworkGameList *item = NetworkGameListAddItem(ins_item->ip, ins_item->port);
+		NetworkGameList *item = NetworkGameListAddItem(ins_item->address);
 
 		if (item != NULL) {
 			if (StrEmpty(item->info.server_name)) {
@@ -63,22 +63,21 @@
  * @param ip the IP-address (inet_addr) of the to-be added item
  * @param port the port the server is running on
  * @return a point to the newly added or already existing item */
-NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
+NetworkGameList *NetworkGameListAddItem(NetworkAddress address)
 {
-	if (ip == 0) return NULL;
+	if (!address.IsResolved()) return NULL;
 
 	NetworkGameList *item, *prev_item;
 
 	prev_item = NULL;
 	for (item = _network_game_list; item != NULL; item = item->next) {
-		if (item->ip == ip && item->port == port) return item;
+		if (item->address == address) return item;
 		prev_item = item;
 	}
 
 	item = CallocT<NetworkGameList>(1);
 	item->next = NULL;
-	item->ip = ip;
-	item->port = port;
+	item->address = address;
 
 	if (prev_item == NULL) {
 		_network_game_list = item;
@@ -142,7 +141,7 @@
 
 		/* item gets mostly zeroed by NetworkUDPQueryServer */
 		uint8 retries = item->retries;
-		NetworkUDPQueryServer(NetworkAddress(item->ip, item->port));
+		NetworkUDPQueryServer(NetworkAddress(item->address));
 		item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
 	}
 }
--- a/src/network/network_gamelist.h
+++ b/src/network/network_gamelist.h
@@ -5,24 +5,24 @@
 #ifndef NETWORK_GAMELIST_H
 #define NETWORK_GAMELIST_H
 
+#include "core/address.h"
 #include "network_type.h"
 
 /** Structure with information shown in the game list (GUI) */
 struct NetworkGameList {
-	NetworkGameInfo info;  ///< The game information of this server
-	uint32 ip;             ///< The IP of the game server
-	uint16 port;           ///< The port of the game server
-	bool online;           ///< False if the server did not respond (default status)
-	bool manually;         ///< True if the server was added manually
-	uint8 retries;         ///< Number of retries (to stop requerying)
-	NetworkGameList *next; ///< Next pointer to make a linked game list
+	NetworkGameInfo info;   ///< The game information of this server
+	NetworkAddress address; ///< The connection info of the game server
+	bool online;            ///< False if the server did not respond (default status)
+	bool manually;          ///< True if the server was added manually
+	uint8 retries;          ///< Number of retries (to stop requerying)
+	NetworkGameList *next;  ///< Next pointer to make a linked game list
 };
 
 /** Game list of this client */
 extern NetworkGameList *_network_game_list;
 
 void NetworkGameListAddItemDelayed(NetworkGameList *item);
-NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port);
+NetworkGameList *NetworkGameListAddItem(NetworkAddress address);
 void NetworkGameListRemoveItem(NetworkGameList *remove);
 void NetworkGameListRequery();
 
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -403,7 +403,7 @@
 			y += NET_PRC__SIZE_OF_ROW;
 		}
 
-		const NetworkGameList *last_joined = NetworkGameListAddItem(inet_addr(_settings_client.network.last_host), _settings_client.network.last_port);
+		const NetworkGameList *last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
 		/* Draw the last joined server, if any */
 		if (last_joined != NULL) this->DrawServerLine(last_joined, y = this->widget[NGWW_LASTJOINED].top + 3, last_joined == sel);
 
@@ -454,7 +454,7 @@
 			y += 10;
 
 			SetDParamStr(0, sel->info.hostname);
-			SetDParam(1, sel->port);
+			SetDParam(1, sel->address.GetPort());
 			DrawString(x, this->widget[NGWW_DETAILS].right, y, STR_NETWORK_SERVER_ADDRESS, TC_GOLD); // server address
 			y += 10;
 
@@ -523,7 +523,7 @@
 			} break;
 
 			case NGWW_LASTJOINED: {
-				NetworkGameList *last_joined = NetworkGameListAddItem(inet_addr(_settings_client.network.last_host), _settings_client.network.last_port);
+				NetworkGameList *last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
 				if (last_joined != NULL) {
 					this->server = last_joined;
 
@@ -562,14 +562,14 @@
 
 			case NGWW_JOIN: // Join Game
 				if (this->server != NULL) {
-					snprintf(_settings_client.network.last_host, sizeof(_settings_client.network.last_host), "%s", inet_ntoa(*(struct in_addr *)&this->server->ip));
-					_settings_client.network.last_port = this->server->port;
+					snprintf(_settings_client.network.last_host, sizeof(_settings_client.network.last_host), "%s", this->server->address.GetHostname());
+					_settings_client.network.last_port = this->server->address.GetPort();
 					ShowNetworkLobbyWindow(this->server);
 				}
 				break;
 
 			case NGWW_REFRESH: // Refresh
-				if (this->server != NULL) NetworkUDPQueryServer(NetworkAddress(this->server->info.hostname, this->server->port));
+				if (this->server != NULL) NetworkUDPQueryServer(this->server->address);
 				break;
 
 			case NGWW_NEWGRF: // NewGRF Settings
--- a/src/network/network_udp.cpp
+++ b/src/network/network_udp.cpp
@@ -220,7 +220,7 @@
 	DEBUG(net, 4, "[udp] server response from %s", client_addr->GetAddressAsString());
 
 	/* Find next item */
-	item = NetworkGameListAddItem(client_addr->GetIP(), client_addr->GetPort());
+	item = NetworkGameListAddItem(*client_addr);
 
 	this->Recv_NetworkGameInfo(p, &item->info);
 
@@ -254,8 +254,7 @@
 				this->Send_GRFIdentifier(&packet, in_request[i]);
 			}
 
-			NetworkAddress out_addr(item->ip, item->port);
-			this->SendPacket(&packet, &out_addr);
+			this->SendPacket(&packet, &item->address);
 		}
 	}
 
@@ -428,12 +427,9 @@
 {
 	NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr;
 
-	NetworkAddress out_addr(info->GetIP(), info->GetPort());
-
 	/* Clear item in gamelist */
 	NetworkGameList *item = CallocT<NetworkGameList>(1);
-	item->ip = info->GetIP();
-	item->port = info->GetPort();
+	item->address = NetworkAddress(*info);
 	strecpy(item->info.server_name, info->GetHostname(), lastof(item->info.server_name));
 	strecpy(item->info.hostname, info->GetHostname(), lastof(item->info.hostname));
 	item->manually = info->manually;
@@ -442,7 +438,7 @@
 	_network_udp_mutex->BeginCritical();
 	/* Init the packet */
 	Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
-	if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, &out_addr);
+	if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, info);
 	_network_udp_mutex->EndCritical();
 
 	delete info;