summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-05-09 18:48:21 +0200
committerGitHub <noreply@github.com>2021-05-09 18:48:21 +0200
commit583011bca0a1aa5b508ccdebe591df4eae01efb5 (patch)
treee9e354500bb319ae87a6af0ddd312c8e7428414c
parente162d0a55ccc4cf38fd09ce0fa349e4887a55448 (diff)
downloadopenttd-583011bca0a1aa5b508ccdebe591df4eae01efb5.tar.xz
Fix: lobby window doesn't close if no connection could be established (#9223)
-rw-r--r--src/network/network.cpp52
-rw-r--r--src/network/network_gui.cpp6
-rw-r--r--src/network/network_internal.h3
3 files changed, 48 insertions, 13 deletions
diff --git a/src/network/network.cpp b/src/network/network.cpp
index daf2e1d3f..4fdbdab73 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -619,35 +619,69 @@ static void NetworkInitialize(bool close_admins = true)
_network_reconnect = 0;
}
-/** Non blocking connection create to query servers */
+/** Non blocking connection to query servers for their game info. */
class TCPQueryConnecter : TCPConnecter {
private:
- bool request_company_info;
std::string connection_string;
public:
- TCPQueryConnecter(const std::string &connection_string, bool request_company_info) : TCPConnecter(connection_string, NETWORK_DEFAULT_PORT), request_company_info(request_company_info), connection_string(connection_string) {}
+ TCPQueryConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
void OnConnect(SOCKET s) override
{
_networking = true;
new ClientNetworkGameSocketHandler(s, this->connection_string);
- MyClient::SendInformationQuery(request_company_info);
+ MyClient::SendInformationQuery(false);
}
};
/**
- * Query a server to fetch his game-info.
+ * Query a server to fetch the game-info.
* @param connection_string the address to query.
- * @param request_company_info Whether to request company info too.
*/
-void NetworkTCPQueryServer(const std::string &connection_string, bool request_company_info)
+void NetworkQueryServer(const std::string &connection_string)
{
if (!_network_available) return;
NetworkInitialize();
- new TCPQueryConnecter(connection_string, request_company_info);
+ new TCPQueryConnecter(connection_string);
+}
+
+/** Non blocking connection to query servers for their game and company info. */
+class TCPLobbyQueryConnecter : TCPConnecter {
+private:
+ std::string connection_string;
+
+public:
+ TCPLobbyQueryConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
+
+ void OnFailure() override
+ {
+ DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
+
+ ShowErrorMessage(STR_NETWORK_ERROR_NOCONNECTION, INVALID_STRING_ID, WL_ERROR);
+ }
+
+ void OnConnect(SOCKET s) override
+ {
+ _networking = true;
+ new ClientNetworkGameSocketHandler(s, this->connection_string);
+ MyClient::SendInformationQuery(true);
+ }
+};
+
+/**
+ * Query a server to fetch his game-info for the lobby.
+ * @param connection_string the address to query.
+ */
+void NetworkQueryLobbyServer(const std::string &connection_string)
+{
+ if (!_network_available) return;
+
+ NetworkInitialize();
+
+ new TCPLobbyQueryConnecter(connection_string);
}
/**
@@ -670,7 +704,7 @@ NetworkGameList *NetworkAddServer(const std::string &connection_string, bool man
NetworkRebuildHostList();
UpdateNetworkGameWindow();
- NetworkTCPQueryServer(connection_string);
+ NetworkQueryServer(connection_string);
}
if (manually) item->manually = true;
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index cf16d06b1..613b562fb 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -752,7 +752,7 @@ public:
break;
case WID_NG_REFRESH: // Refresh
- if (this->server != nullptr) NetworkTCPQueryServer(this->server->connection_string);
+ if (this->server != nullptr) NetworkQueryServer(this->server->connection_string);
break;
case WID_NG_NEWGRF: // NewGRF Settings
@@ -1487,7 +1487,7 @@ struct NetworkLobbyWindow : public Window {
/* Clear the information so removed companies don't remain */
for (auto &company : this->company_info) company = {};
- NetworkTCPQueryServer(this->server->connection_string, true);
+ NetworkQueryLobbyServer(this->server->connection_string);
break;
}
}
@@ -1557,7 +1557,7 @@ static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
strecpy(_settings_client.network.last_joined, ngl->connection_string.c_str(), lastof(_settings_client.network.last_joined));
- NetworkTCPQueryServer(ngl->connection_string, true);
+ NetworkQueryLobbyServer(ngl->connection_string);
new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
}
diff --git a/src/network/network_internal.h b/src/network/network_internal.h
index ac5472fe1..3456ab251 100644
--- a/src/network/network_internal.h
+++ b/src/network/network_internal.h
@@ -87,7 +87,8 @@ extern uint8 _network_reconnect;
extern CompanyMask _network_company_passworded;
-void NetworkTCPQueryServer(const std::string &connection_string, bool request_company_info = false);
+void NetworkQueryServer(const std::string &connection_string);
+void NetworkQueryLobbyServer(const std::string &connection_string);
void GetBindAddresses(NetworkAddressList *addresses, uint16 port);
struct NetworkGameList *NetworkAddServer(const std::string &connection_string, bool manually = true);