Codechange: use connection_string in favour of NetworkAddress (#9197)

We now resolve the connection_string to a NetworkAddress in a much
later state. This means there are fewer places constructing a NetworkAddress.

The main benefit of this is in later PRs that introduce different types
of NetworkAddresses. Storing this in things like NetworkGameList is
rather complex, especially as NetworkAddress has to be mutable at all
times.

Additionally, the NetworkAddress is a complex object to store simple
information: how to connect to this server.
This commit is contained in:
Patric Stout
2021-05-05 23:21:14 +02:00
committed by GitHub
parent ead30dc725
commit f94fb93779
14 changed files with 101 additions and 107 deletions

View File

@@ -272,7 +272,9 @@ protected:
static bool NGameNameSorter(NetworkGameList * const &a, NetworkGameList * const &b)
{
int r = strnatcmp(a->info.server_name, b->info.server_name, true); // Sort by name (natural sorting).
return r == 0 ? a->address.CompareTo(b->address) < 0: r < 0;
if (r == 0) r = a->connection_string.compare(b->connection_string);
return r < 0;
}
/**
@@ -646,8 +648,7 @@ public:
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
y += FONT_HEIGHT_NORMAL;
std::string address = sel->address.GetAddressAsString();
SetDParamStr(0, address.c_str());
SetDParamStr(0, sel->connection_string.c_str());
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
y += FONT_HEIGHT_NORMAL;
@@ -751,7 +752,7 @@ public:
break;
case WID_NG_REFRESH: // Refresh
if (this->server != nullptr) NetworkTCPQueryServer(this->server->address);
if (this->server != nullptr) NetworkTCPQueryServer(this->server->connection_string);
break;
case WID_NG_NEWGRF: // NewGRF Settings
@@ -1471,22 +1472,22 @@ struct NetworkLobbyWindow : public Window {
case WID_NL_JOIN: // Join company
/* Button can be clicked only when it is enabled. */
NetworkClientConnectGame(this->server->address, this->company);
NetworkClientConnectGame(this->server->connection_string, this->company);
break;
case WID_NL_NEW: // New company
NetworkClientConnectGame(this->server->address, COMPANY_NEW_COMPANY);
NetworkClientConnectGame(this->server->connection_string, COMPANY_NEW_COMPANY);
break;
case WID_NL_SPECTATE: // Spectate game
NetworkClientConnectGame(this->server->address, COMPANY_SPECTATOR);
NetworkClientConnectGame(this->server->connection_string, COMPANY_SPECTATOR);
break;
case WID_NL_REFRESH: // Refresh
/* Clear the information so removed companies don't remain */
for (auto &company : this->company_info) company = {};
NetworkTCPQueryServer(this->server->address, true);
NetworkTCPQueryServer(this->server->connection_string, true);
break;
}
}
@@ -1554,9 +1555,9 @@ static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
strecpy(_settings_client.network.last_joined, ngl->address.GetAddressAsString(false).c_str(), lastof(_settings_client.network.last_joined));
strecpy(_settings_client.network.last_joined, ngl->connection_string.c_str(), lastof(_settings_client.network.last_joined));
NetworkTCPQueryServer(ngl->address, true);
NetworkTCPQueryServer(ngl->connection_string, true);
new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
}