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.
This commit is contained in:

committed by
Patric Stout

parent
f4dd2d88c7
commit
cee8174d02
@@ -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));
|
||||
}
|
||||
|
@@ -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 */
|
||||
|
@@ -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 */
|
||||
|
@@ -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()) {
|
||||
|
Reference in New Issue
Block a user