mirror of https://github.com/OpenTTD/OpenTTD
Codechange: split ParseConnectionString into two functions
One also looks for a company, the other doesn't. There were more uses of the latter than the first, leaving very weird code all over the place.pull/9053/head
parent
05612d60ae
commit
31897eaa7d
|
@ -916,7 +916,7 @@ DEF_CONSOLE_CMD(ConNetworkConnect)
|
||||||
uint16 rport = NETWORK_DEFAULT_PORT;
|
uint16 rport = NETWORK_DEFAULT_PORT;
|
||||||
CompanyID join_as = COMPANY_NEW_COMPANY;
|
CompanyID join_as = COMPANY_NEW_COMPANY;
|
||||||
|
|
||||||
ParseConnectionString(&company, &port, ip);
|
ParseGameConnectionString(&company, &port, ip);
|
||||||
|
|
||||||
IConsolePrintF(CC_DEFAULT, "Connecting to %s...", ip);
|
IConsolePrintF(CC_DEFAULT, "Connecting to %s...", ip);
|
||||||
if (company != nullptr) {
|
if (company != nullptr) {
|
||||||
|
|
|
@ -203,10 +203,8 @@ int NetworkHTTPSocketHandler::HandleHeader()
|
||||||
*url = '\0';
|
*url = '\0';
|
||||||
|
|
||||||
/* Fetch the hostname, and possible port number. */
|
/* Fetch the hostname, and possible port number. */
|
||||||
const char *company = nullptr;
|
|
||||||
const char *port = nullptr;
|
const char *port = nullptr;
|
||||||
ParseConnectionString(&company, &port, hname);
|
ParseConnectionString(&port, hname);
|
||||||
if (company != nullptr) return_error("[tcp/http] invalid hostname");
|
|
||||||
|
|
||||||
NetworkAddress address(hname, port == nullptr ? 80 : atoi(port));
|
NetworkAddress address(hname, port == nullptr ? 80 : atoi(port));
|
||||||
|
|
||||||
|
|
|
@ -446,6 +446,36 @@ static void CheckPauseOnJoin()
|
||||||
CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
|
CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a string to ip/port
|
||||||
|
* Format: IP:port
|
||||||
|
*
|
||||||
|
* connection_string will be re-terminated to separate out the hostname, port will
|
||||||
|
* be set to the port strings given by the user, inside the memory area originally
|
||||||
|
* occupied by connection_string.
|
||||||
|
*/
|
||||||
|
void ParseConnectionString(const char **port, char *connection_string)
|
||||||
|
{
|
||||||
|
bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
|
||||||
|
for (char *p = connection_string; *p != '\0'; p++) {
|
||||||
|
switch (*p) {
|
||||||
|
case '[':
|
||||||
|
ipv6 = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ']':
|
||||||
|
ipv6 = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ':':
|
||||||
|
if (ipv6) break;
|
||||||
|
*port = p + 1;
|
||||||
|
*p = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a string to ip/port/company
|
* Converts a string to ip/port/company
|
||||||
* Format: IP:port#company
|
* Format: IP:port#company
|
||||||
|
@ -454,11 +484,10 @@ static void CheckPauseOnJoin()
|
||||||
* be set to the company and port strings given by the user, inside the memory area originally
|
* be set to the company and port strings given by the user, inside the memory area originally
|
||||||
* occupied by connection_string.
|
* occupied by connection_string.
|
||||||
*/
|
*/
|
||||||
void ParseConnectionString(const char **company, const char **port, char *connection_string)
|
void ParseGameConnectionString(const char **company, const char **port, char *connection_string)
|
||||||
{
|
{
|
||||||
bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
|
bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
|
||||||
char *p;
|
for (char *p = connection_string; *p != '\0'; p++) {
|
||||||
for (p = connection_string; *p != '\0'; p++) {
|
|
||||||
switch (*p) {
|
switch (*p) {
|
||||||
case '[':
|
case '[':
|
||||||
ipv6 = true;
|
ipv6 = true;
|
||||||
|
@ -592,7 +621,6 @@ void NetworkAddServer(const char *b)
|
||||||
{
|
{
|
||||||
if (*b != '\0') {
|
if (*b != '\0') {
|
||||||
const char *port = nullptr;
|
const char *port = nullptr;
|
||||||
const char *company = nullptr;
|
|
||||||
char host[NETWORK_HOSTNAME_LENGTH];
|
char host[NETWORK_HOSTNAME_LENGTH];
|
||||||
uint16 rport;
|
uint16 rport;
|
||||||
|
|
||||||
|
@ -601,7 +629,7 @@ void NetworkAddServer(const char *b)
|
||||||
strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
|
strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
|
||||||
rport = NETWORK_DEFAULT_PORT;
|
rport = NETWORK_DEFAULT_PORT;
|
||||||
|
|
||||||
ParseConnectionString(&company, &port, host);
|
ParseConnectionString(&port, host);
|
||||||
if (port != nullptr) rport = atoi(port);
|
if (port != nullptr) rport = atoi(port);
|
||||||
|
|
||||||
NetworkUDPQueryServer(NetworkAddress(host, rport), true);
|
NetworkUDPQueryServer(NetworkAddress(host, rport), true);
|
||||||
|
|
|
@ -43,7 +43,8 @@ void NetworkReboot();
|
||||||
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
|
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
|
||||||
void NetworkGameLoop();
|
void NetworkGameLoop();
|
||||||
void NetworkBackgroundLoop();
|
void NetworkBackgroundLoop();
|
||||||
void ParseConnectionString(const char **company, const char **port, char *connection_string);
|
void ParseConnectionString(const char **port, char *connection_string);
|
||||||
|
void ParseGameConnectionString(const char **company, const char **port, char *connection_string);
|
||||||
void NetworkStartDebugLog(NetworkAddress address);
|
void NetworkStartDebugLog(NetworkAddress address);
|
||||||
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
|
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
|
||||||
|
|
||||||
|
|
|
@ -478,7 +478,7 @@ struct AfterNewGRFScan : NewGRFScanCallback {
|
||||||
uint16 rport = NETWORK_DEFAULT_PORT;
|
uint16 rport = NETWORK_DEFAULT_PORT;
|
||||||
CompanyID join_as = COMPANY_NEW_COMPANY;
|
CompanyID join_as = COMPANY_NEW_COMPANY;
|
||||||
|
|
||||||
ParseConnectionString(&company, &port, network_conn);
|
ParseGameConnectionString(&company, &port, network_conn);
|
||||||
|
|
||||||
if (company != nullptr) {
|
if (company != nullptr) {
|
||||||
join_as = (CompanyID)atoi(company);
|
join_as = (CompanyID)atoi(company);
|
||||||
|
@ -584,11 +584,8 @@ int openttd_main(int argc, char *argv[])
|
||||||
dedicated = true;
|
dedicated = true;
|
||||||
SetDebugString("net=6");
|
SetDebugString("net=6");
|
||||||
if (mgo.opt != nullptr) {
|
if (mgo.opt != nullptr) {
|
||||||
/* Use the existing method for parsing (openttd -n).
|
|
||||||
* However, we do ignore the #company part. */
|
|
||||||
const char *temp = nullptr;
|
|
||||||
const char *port = nullptr;
|
const char *port = nullptr;
|
||||||
ParseConnectionString(&temp, &port, mgo.opt);
|
ParseConnectionString(&port, mgo.opt);
|
||||||
if (!StrEmpty(mgo.opt)) scanner->dedicated_host = mgo.opt;
|
if (!StrEmpty(mgo.opt)) scanner->dedicated_host = mgo.opt;
|
||||||
if (port != nullptr) scanner->dedicated_port = atoi(port);
|
if (port != nullptr) scanner->dedicated_port = atoi(port);
|
||||||
}
|
}
|
||||||
|
@ -774,13 +771,12 @@ 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) {
|
||||||
const char *not_used = nullptr;
|
|
||||||
const char *port = nullptr;
|
const char *port = nullptr;
|
||||||
uint16 rport;
|
uint16 rport;
|
||||||
|
|
||||||
rport = NETWORK_DEFAULT_DEBUGLOG_PORT;
|
rport = NETWORK_DEFAULT_DEBUGLOG_PORT;
|
||||||
|
|
||||||
ParseConnectionString(¬_used, &port, debuglog_conn);
|
ParseConnectionString(&port, debuglog_conn);
|
||||||
if (port != nullptr) rport = atoi(port);
|
if (port != nullptr) rport = atoi(port);
|
||||||
|
|
||||||
NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport));
|
NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport));
|
||||||
|
|
Loading…
Reference in New Issue