mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r15916) -Codechange: let the network game list use NetworkAddress
This commit is contained in:
parent
c0c6e07081
commit
1e205e01b8
|
@ -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 @@ uint16 NetworkAddress::GetPort() const
|
|||
}
|
||||
}
|
||||
|
||||
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 */
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
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 @@ public:
|
|||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,6 +110,12 @@ public:
|
|||
*/
|
||||
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 @@ public:
|
|||
{
|
||||
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 */
|
||||
|
|
|
@ -714,7 +714,7 @@ void NetworkRebuildHostList()
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ static void NetworkGameListHandleDelayedInsert()
|
|||
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 @@ static void NetworkGameListHandleDelayedInsert()
|
|||
* @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 @@ void NetworkGameListRequery()
|
|||
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -403,7 +403,7 @@ public:
|
|||
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 @@ public:
|
|||
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 @@ public:
|
|||
} 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 @@ public:
|
|||
|
||||
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
|
||||
|
|
|
@ -220,7 +220,7 @@ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
|
|||
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 @@ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
|
|||
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 @@ void NetworkUDPQueryServerThread(void *pntr)
|
|||
{
|
||||
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 @@ void NetworkUDPQueryServerThread(void *pntr)
|
|||
_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;
|
||||
|
|
Loading…
Reference in New Issue