summaryrefslogtreecommitdiff
path: root/src/network/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/network/core')
-rw-r--r--src/network/core/address.cpp18
-rw-r--r--src/network/core/address.h34
-rw-r--r--src/network/core/tcp.h12
-rw-r--r--src/network/core/tcp_connect.cpp20
4 files changed, 84 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));
+}
diff --git a/src/network/core/address.h b/src/network/core/address.h
index af10a2756..b22bcac0e 100644
--- a/src/network/core/address.h
+++ b/src/network/core/address.h
@@ -12,6 +12,7 @@
#include "os_abstraction.h"
#include "config.h"
+#include "../../company_type.h"
#include "../../string_func.h"
#include "../../core/smallmap_type.hpp"
@@ -177,4 +178,37 @@ public:
static const std::string GetPeerName(SOCKET sock);
};
+/**
+ * Types of server addresses we know.
+ *
+ * Sorting will prefer entries at the top of this list above ones at the bottom.
+ */
+enum ServerAddressType {
+ SERVER_ADDRESS_DIRECT, ///< Server-address is based on an hostname:port.
+};
+
+/**
+ * Address to a game server.
+ *
+ * This generalises addresses which are based on different identifiers.
+ */
+class ServerAddress {
+private:
+ /**
+ * Create a new ServerAddress object.
+ *
+ * Please use ServerAddress::Parse() instead of calling this directly.
+ *
+ * @param type The type of the ServerAdress.
+ * @param connection_string The connection_string that belongs to this ServerAddress type.
+ */
+ ServerAddress(ServerAddressType type, const std::string &connection_string) : type(type), connection_string(connection_string) {}
+
+public:
+ ServerAddressType type; ///< The type of this ServerAddress.
+ std::string connection_string; ///< The connection string for this ServerAddress.
+
+ static ServerAddress Parse(const std::string &connection_string, uint16 default_port, CompanyID *company_id = nullptr);
+};
+
#endif /* NETWORK_CORE_ADDRESS_H */
diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h
index 3b217cb2e..379ef8b92 100644
--- a/src/network/core/tcp.h
+++ b/src/network/core/tcp.h
@@ -103,9 +103,14 @@ private:
void Connect(addrinfo *address);
bool CheckActivity();
+ /* We do not want any other derived classes from this class being able to
+ * access these private members, but it is okay for TCPServerConnecter. */
+ friend class TCPServerConnecter;
+
static void ResolveThunk(TCPConnecter *connecter);
public:
+ TCPConnecter() {};
TCPConnecter(const std::string &connection_string, uint16 default_port);
virtual ~TCPConnecter();
@@ -124,4 +129,11 @@ public:
static void KillAll();
};
+class TCPServerConnecter : public TCPConnecter {
+public:
+ ServerAddress server_address; ///< Address we are connecting to.
+
+ TCPServerConnecter(const std::string &connection_string, uint16 default_port);
+};
+
#endif /* NETWORK_CORE_TCP_H */
diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp
index dc7a23bb3..bb96e33f9 100644
--- a/src/network/core/tcp_connect.cpp
+++ b/src/network/core/tcp_connect.cpp
@@ -33,6 +33,26 @@ TCPConnecter::TCPConnecter(const std::string &connection_string, uint16 default_
_tcp_connecters.push_back(this);
}
+/**
+ * Create a new connecter for the server.
+ * @param connection_string The address to connect to.
+ * @param default_port If not indicated in connection_string, what port to use.
+ */
+TCPServerConnecter::TCPServerConnecter(const std::string &connection_string, uint16 default_port) :
+ server_address(ServerAddress::Parse(connection_string, default_port))
+{
+ switch (this->server_address.type) {
+ case SERVER_ADDRESS_DIRECT:
+ this->connection_string = this->server_address.connection_string;
+ break;
+
+ default:
+ NOT_REACHED();
+ }
+
+ _tcp_connecters.push_back(this);
+}
+
TCPConnecter::~TCPConnecter()
{
if (this->resolve_thread.joinable()) {