summaryrefslogtreecommitdiff
path: root/src/network/core/address.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-04-28 14:36:14 +0200
committerPatric Stout <github@truebrain.nl>2021-07-11 20:38:42 +0200
commitcee8174d02e38542548fc74de93450cfebefaa91 (patch)
tree094ad162d578263d28470a444b3c7656d6bc09ab /src/network/core/address.cpp
parentf4dd2d88c721c085376f59908097500bc5f0c143 (diff)
downloadopenttd-cee8174d02e38542548fc74de93450cfebefaa91.tar.xz
Codechange: track servers with a ServerAddress instead of a NetworkAddress
This allows future extensions to have different ways of referencing a server, instead of forcing to use IP:port.
Diffstat (limited to 'src/network/core/address.cpp')
-rw-r--r--src/network/core/address.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp
index 0c16a2c3c..113dae686 100644
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -10,6 +10,7 @@
#include "../../stdafx.h"
#include "address.h"
+#include "../network_internal.h"
#include "../../debug.h"
#include "../../safeguards.h"
@@ -411,3 +412,20 @@ void NetworkAddress::Listen(int socktype, SocketList *sockets)
getpeername(sock, (sockaddr *)&addr, &addr_len);
return NetworkAddress(addr, addr_len).GetAddressAsString();
}
+
+/**
+ * Convert a string containing either "hostname" or "hostname:ip" to a
+ * ServerAddress, where the string can be postfixed with "#company" to
+ * indicate the requested company.
+ *
+ * @param connection_string The string to parse.
+ * @param default_port The default port to set port to if not in connection_string.
+ * @param company Pointer to the company variable to set iff indicted.
+ * @return A valid ServerAddress of the parsed information.
+ */
+/* static */ ServerAddress ServerAddress::Parse(const std::string &connection_string, uint16 default_port, CompanyID *company_id)
+{
+ uint16 port = default_port;
+ std::string_view ip = ParseFullConnectionString(connection_string, port, company_id);
+ return ServerAddress(SERVER_ADDRESS_DIRECT, std::string(ip) + ":" + std::to_string(port));
+}