mirror of https://github.com/OpenTTD/OpenTTD
Change: [Network] Encapsulate logic about the connection string to the network code (#23)
parent
be37a2cab8
commit
a61696d6c5
|
@ -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. */
|
/* 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);
|
IConsolePrintF(CC_DEFAULT, "Reconnecting to %s ...", _settings_client.network.last_joined);
|
||||||
|
|
||||||
NetworkAddress address = ParseConnectionString(_settings_client.network.last_joined, NETWORK_DEFAULT_PORT);
|
NetworkClientConnectGame(_settings_client.network.last_joined, playas);
|
||||||
NetworkClientConnectGame(address, playas);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -918,23 +917,7 @@ DEF_CONSOLE_CMD(ConNetworkConnect)
|
||||||
if (argc < 2) return false;
|
if (argc < 2) return false;
|
||||||
if (_networking) NetworkDisconnect(); // we are in network-mode, first close it!
|
if (_networking) NetworkDisconnect(); // we are in network-mode, first close it!
|
||||||
|
|
||||||
CompanyID join_as = COMPANY_NEW_COMPANY;
|
NetworkClientConnectGame(argv[1], 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);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
#include "../../stdafx.h"
|
#include "../../stdafx.h"
|
||||||
#include "../../debug.h"
|
#include "../../debug.h"
|
||||||
#include "../../rev.h"
|
#include "../../rev.h"
|
||||||
#include "../network_func.h"
|
#include "../network_internal.h"
|
||||||
#include "game_info.h"
|
#include "game_info.h"
|
||||||
|
|
||||||
#include "tcp_http.h"
|
#include "tcp_http.h"
|
||||||
|
|
|
@ -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.
|
* @param default_port The default port to set port to if not in connection_string.
|
||||||
* @return A valid NetworkAddress of the parsed information.
|
* @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];
|
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;
|
const char *port = nullptr;
|
||||||
ParseFullConnectionString(nullptr, &port, internal_connection_string);
|
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.
|
* @param default_port The default port to set port to if not in connection_string.
|
||||||
* @return A valid NetworkAddress of the parsed information.
|
* @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
|
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 *port_s = nullptr;
|
||||||
const char *company_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 */
|
/* 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)
|
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);
|
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
|
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());
|
DEBUG(net, 0, "Redirecting DEBUG() to %s", address.GetAddressAsString().c_str());
|
||||||
|
|
||||||
SOCKET s = address.Connect();
|
SOCKET s = address.Connect();
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
// #define DEBUG_FAILED_DUMP_COMMANDS
|
// #define DEBUG_FAILED_DUMP_COMMANDS
|
||||||
|
|
||||||
#include "network_type.h"
|
#include "network_type.h"
|
||||||
#include "core/address.h"
|
|
||||||
#include "../console_type.h"
|
#include "../console_type.h"
|
||||||
#include "../gfx_type.h"
|
#include "../gfx_type.h"
|
||||||
#include "../openttd.h"
|
#include "../openttd.h"
|
||||||
|
@ -47,14 +46,12 @@ void NetworkDisconnect(bool blocking = false, bool close_admins = true);
|
||||||
void NetworkGameLoop();
|
void NetworkGameLoop();
|
||||||
void NetworkBackgroundLoop();
|
void NetworkBackgroundLoop();
|
||||||
void ParseFullConnectionString(const char **company, const char **port, char *connection_string);
|
void ParseFullConnectionString(const char **company, const char **port, char *connection_string);
|
||||||
NetworkAddress ParseConnectionString(const char *connection_string, int default_port);
|
void NetworkStartDebugLog(const std::string &connection_string);
|
||||||
NetworkAddress ParseGameConnectionString(CompanyID *company, const char *connection_string, int default_port);
|
|
||||||
void NetworkStartDebugLog(NetworkAddress &address);
|
|
||||||
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
|
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
|
||||||
|
|
||||||
void NetworkUpdateClientInfo(ClientID client_id);
|
void NetworkUpdateClientInfo(ClientID client_id);
|
||||||
void NetworkClientsToSpectators(CompanyID cid);
|
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 NetworkClientRequestMove(CompanyID company, const char *pass = "");
|
||||||
void NetworkClientSendRcon(const char *password, const char *command);
|
void NetworkClientSendRcon(const char *password, const char *command);
|
||||||
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
|
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
|
||||||
|
|
|
@ -119,4 +119,8 @@ StringID GetNetworkErrorMsg(NetworkErrorCode err);
|
||||||
bool NetworkFindName(char *new_name, const char *last);
|
bool NetworkFindName(char *new_name, const char *last);
|
||||||
const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed);
|
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 */
|
#endif /* NETWORK_INTERNAL_H */
|
||||||
|
|
|
@ -473,21 +473,10 @@ struct AfterNewGRFScan : NewGRFScanCallback {
|
||||||
if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
|
if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
|
||||||
|
|
||||||
if (_network_available && network_conn != nullptr) {
|
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();
|
LoadIntroGame();
|
||||||
_switch_mode = SM_NONE;
|
_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. */
|
/* After the scan we're not used anymore. */
|
||||||
|
@ -763,8 +752,7 @@ int openttd_main(int argc, char *argv[])
|
||||||
NetworkStartUp(); // initialize network-core
|
NetworkStartUp(); // initialize network-core
|
||||||
|
|
||||||
if (debuglog_conn != nullptr && _network_available) {
|
if (debuglog_conn != nullptr && _network_available) {
|
||||||
NetworkAddress address = ParseConnectionString(debuglog_conn, NETWORK_DEFAULT_DEBUGLOG_PORT);
|
NetworkStartDebugLog(debuglog_conn);
|
||||||
NetworkStartDebugLog(address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!HandleBootstrap()) {
|
if (!HandleBootstrap()) {
|
||||||
|
@ -1476,8 +1464,7 @@ void GameLoop()
|
||||||
if (_network_reconnect > 0 && --_network_reconnect == 0) {
|
if (_network_reconnect > 0 && --_network_reconnect == 0) {
|
||||||
/* This means that we want to reconnect to the last host
|
/* This means that we want to reconnect to the last host
|
||||||
* We do this here, because it means that the network is really closed */
|
* 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(_settings_client.network.last_joined, COMPANY_SPECTATOR);
|
||||||
NetworkClientConnectGame(address, COMPANY_SPECTATOR);
|
|
||||||
}
|
}
|
||||||
/* Singleplayer */
|
/* Singleplayer */
|
||||||
StateGameLoop();
|
StateGameLoop();
|
||||||
|
|
Loading…
Reference in New Issue