diff options
-rw-r--r-- | src/console_cmds.cpp | 21 | ||||
-rw-r--r-- | src/network/core/tcp_http.cpp | 2 | ||||
-rw-r--r-- | src/network/network.cpp | 26 | ||||
-rw-r--r-- | src/network/network_func.h | 7 | ||||
-rw-r--r-- | src/network/network_internal.h | 4 | ||||
-rw-r--r-- | src/openttd.cpp | 19 |
6 files changed, 33 insertions, 46 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 7ab6d3179..9dce39909 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -901,8 +901,7 @@ DEF_CONSOLE_CMD(ConNetworkReconnect) /* Don't resolve the address first, just print it directly as it comes from the config file. */ IConsolePrintF(CC_DEFAULT, "Reconnecting to %s ...", _settings_client.network.last_joined); - NetworkAddress address = ParseConnectionString(_settings_client.network.last_joined, NETWORK_DEFAULT_PORT); - NetworkClientConnectGame(address, playas); + NetworkClientConnectGame(_settings_client.network.last_joined, playas); return true; } @@ -918,23 +917,7 @@ DEF_CONSOLE_CMD(ConNetworkConnect) if (argc < 2) return false; if (_networking) NetworkDisconnect(); // we are in network-mode, first close it! - CompanyID join_as = COMPANY_NEW_COMPANY; - NetworkAddress address = ParseGameConnectionString(&join_as, argv[1], NETWORK_DEFAULT_PORT); - - IConsolePrintF(CC_DEFAULT, "Connecting to %s...", address.GetAddressAsString().c_str()); - if (join_as != COMPANY_NEW_COMPANY) { - IConsolePrintF(CC_DEFAULT, " company-no: %d", join_as); - - /* From a user pov 0 is a new company, internally it's different and all - * companies are offset by one to ease up on users (eg companies 1-8 not 0-7) */ - if (join_as != COMPANY_SPECTATOR) { - if (join_as > MAX_COMPANIES) return false; - join_as--; - } - } - - NetworkClientConnectGame(address, join_as); - + NetworkClientConnectGame(argv[1], COMPANY_NEW_COMPANY); return true; } diff --git a/src/network/core/tcp_http.cpp b/src/network/core/tcp_http.cpp index 99eca1cb7..e0c269faf 100644 --- a/src/network/core/tcp_http.cpp +++ b/src/network/core/tcp_http.cpp @@ -12,7 +12,7 @@ #include "../../stdafx.h" #include "../../debug.h" #include "../../rev.h" -#include "../network_func.h" +#include "../network_internal.h" #include "game_info.h" #include "tcp_http.h" diff --git a/src/network/network.cpp b/src/network/network.cpp index 0be46bbf3..d7cf9367a 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -490,10 +490,10 @@ void ParseFullConnectionString(const char **company, const char **port, char *co * @param default_port The default port to set port to if not in connection_string. * @return A valid NetworkAddress of the parsed information. */ -NetworkAddress ParseConnectionString(const char *connection_string, int default_port) +NetworkAddress ParseConnectionString(const std::string &connection_string, int default_port) { char internal_connection_string[NETWORK_HOSTNAME_PORT_LENGTH]; - strecpy(internal_connection_string, connection_string, lastof(internal_connection_string)); + strecpy(internal_connection_string, connection_string.c_str(), lastof(internal_connection_string)); const char *port = nullptr; ParseFullConnectionString(nullptr, &port, internal_connection_string); @@ -512,10 +512,10 @@ NetworkAddress ParseConnectionString(const char *connection_string, int default_ * @param default_port The default port to set port to if not in connection_string. * @return A valid NetworkAddress of the parsed information. */ -NetworkAddress ParseGameConnectionString(CompanyID *company, const char *connection_string, int default_port) +NetworkAddress ParseGameConnectionString(CompanyID *company, const std::string &connection_string, int default_port) { char internal_connection_string[NETWORK_HOSTNAME_PORT_LENGTH + 4]; // 4 extra for the "#" and company - strecpy(internal_connection_string, connection_string, lastof(internal_connection_string)); + strecpy(internal_connection_string, connection_string.c_str(), lastof(internal_connection_string)); const char *port_s = nullptr; const char *company_s = nullptr; @@ -693,6 +693,20 @@ public: } }; +void NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password, const char *join_company_password) +{ + CompanyID join_as = default_company; + NetworkAddress address = ParseGameConnectionString(&join_as, connection_string, NETWORK_DEFAULT_PORT); + + if (join_as != COMPANY_NEW_COMPANY && join_as != COMPANY_SPECTATOR) { + join_as--; + if (join_as >= MAX_COMPANIES) { + return; + } + } + + NetworkClientConnectGame(address, join_as, join_server_password, join_company_password); +} /* Used by clients, to connect to a server */ void NetworkClientConnectGame(NetworkAddress &address, CompanyID join_as, const char *join_server_password, const char *join_company_password) @@ -1063,10 +1077,12 @@ static void NetworkGenerateServerId() seprintf(_settings_client.network.network_id, lastof(_settings_client.network.network_id), "%s", hex_output); } -void NetworkStartDebugLog(NetworkAddress &address) +void NetworkStartDebugLog(const std::string &connection_string) { extern SOCKET _debug_socket; // Comes from debug.c + NetworkAddress address = ParseConnectionString(connection_string, NETWORK_DEFAULT_DEBUGLOG_PORT); + DEBUG(net, 0, "Redirecting DEBUG() to %s", address.GetAddressAsString().c_str()); SOCKET s = address.Connect(); diff --git a/src/network/network_func.h b/src/network/network_func.h index 967bb6260..cd2b291bc 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -18,7 +18,6 @@ // #define DEBUG_FAILED_DUMP_COMMANDS #include "network_type.h" -#include "core/address.h" #include "../console_type.h" #include "../gfx_type.h" #include "../openttd.h" @@ -47,14 +46,12 @@ void NetworkDisconnect(bool blocking = false, bool close_admins = true); void NetworkGameLoop(); void NetworkBackgroundLoop(); void ParseFullConnectionString(const char **company, const char **port, char *connection_string); -NetworkAddress ParseConnectionString(const char *connection_string, int default_port); -NetworkAddress ParseGameConnectionString(CompanyID *company, const char *connection_string, int default_port); -void NetworkStartDebugLog(NetworkAddress &address); +void NetworkStartDebugLog(const std::string &connection_string); void NetworkPopulateCompanyStats(NetworkCompanyStats *stats); void NetworkUpdateClientInfo(ClientID client_id); void NetworkClientsToSpectators(CompanyID cid); -void NetworkClientConnectGame(NetworkAddress &address, CompanyID join_as, const char *join_server_password = nullptr, const char *join_company_password = nullptr); +void NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password = nullptr, const char *join_company_password = nullptr); void NetworkClientRequestMove(CompanyID company, const char *pass = ""); void NetworkClientSendRcon(const char *password, const char *command); void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0); diff --git a/src/network/network_internal.h b/src/network/network_internal.h index c64eac795..ff3c8c22d 100644 --- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -119,4 +119,8 @@ StringID GetNetworkErrorMsg(NetworkErrorCode err); bool NetworkFindName(char *new_name, const char *last); const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed); +void NetworkClientConnectGame(NetworkAddress &address, CompanyID join_as, const char *join_server_password = nullptr, const char *join_company_password = nullptr); +NetworkAddress ParseConnectionString(const std::string &connection_string, int default_port); +NetworkAddress ParseGameConnectionString(CompanyID *company, const std::string &connection_string, int default_port); + #endif /* NETWORK_INTERNAL_H */ diff --git a/src/openttd.cpp b/src/openttd.cpp index b12824385..fbeeba793 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -473,21 +473,10 @@ struct AfterNewGRFScan : NewGRFScanCallback { if (_switch_mode != SM_NONE) MakeNewgameSettingsLive(); if (_network_available && network_conn != nullptr) { - CompanyID join_as = COMPANY_NEW_COMPANY; - NetworkAddress address = ParseGameConnectionString(&join_as, network_conn, NETWORK_DEFAULT_PORT); - - if (join_as != COMPANY_NEW_COMPANY && join_as != COMPANY_SPECTATOR) { - join_as--; - if (join_as >= MAX_COMPANIES) { - delete this; - return; - } - } - LoadIntroGame(); _switch_mode = SM_NONE; - NetworkClientConnectGame(address, join_as, join_server_password, join_company_password); + NetworkClientConnectGame(network_conn, COMPANY_NEW_COMPANY, join_server_password, join_company_password); } /* After the scan we're not used anymore. */ @@ -763,8 +752,7 @@ int openttd_main(int argc, char *argv[]) NetworkStartUp(); // initialize network-core if (debuglog_conn != nullptr && _network_available) { - NetworkAddress address = ParseConnectionString(debuglog_conn, NETWORK_DEFAULT_DEBUGLOG_PORT); - NetworkStartDebugLog(address); + NetworkStartDebugLog(debuglog_conn); } if (!HandleBootstrap()) { @@ -1476,8 +1464,7 @@ void GameLoop() if (_network_reconnect > 0 && --_network_reconnect == 0) { /* This means that we want to reconnect to the last host * We do this here, because it means that the network is really closed */ - NetworkAddress address = ParseConnectionString(_settings_client.network.last_joined, NETWORK_DEFAULT_PORT); - NetworkClientConnectGame(address, COMPANY_SPECTATOR); + NetworkClientConnectGame(_settings_client.network.last_joined, COMPANY_SPECTATOR); } /* Singleplayer */ StateGameLoop(); |