Changeset - r25813:ea6d38b6c3a1
[Not reviewed]
master
0 6 0
Patric Stout - 3 years ago 2021-04-28 12:36:14
truebrain@openttd.org
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.
6 files changed with 93 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/network/core/address.cpp
Show inline comments
 
@@ -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
 
	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));
 
}
src/network/core/address.h
Show inline comments
 
@@ -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 */
src/network/core/tcp.h
Show inline comments
 
@@ -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 */
src/network/core/tcp_connect.cpp
Show inline comments
 
@@ -33,6 +33,26 @@ TCPConnecter::TCPConnecter(const std::st
 
	_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()) {
src/network/network.cpp
Show inline comments
 
@@ -532,23 +532,6 @@ NetworkAddress ParseConnectionString(con
 
}
 

	
 
/**
 
 * Convert a string containing either "hostname" or "hostname:ip" to a
 
 * NetworkAddress, 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 NetworkAddress of the parsed information.
 
 */
 
static NetworkAddress ParseGameConnectionString(const std::string &connection_string, uint16 default_port, CompanyID *company)
 
{
 
	uint16 port = default_port;
 
	std::string_view ip = ParseFullConnectionString(connection_string, port, company);
 
	return NetworkAddress(ip, port);
 
}
 

	
 
/**
 
 * Handle the accepting of a connection to the server.
 
 * @param s The socket of the new connection.
 
 * @param address The address of the peer.
 
@@ -624,12 +607,12 @@ static void NetworkInitialize(bool close
 
}
 

	
 
/** Non blocking connection to query servers for their game info. */
 
class TCPQueryConnecter : TCPConnecter {
 
class TCPQueryConnecter : TCPServerConnecter {
 
private:
 
	std::string connection_string;
 

	
 
public:
 
	TCPQueryConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
 
	TCPQueryConnecter(const std::string &connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
 

	
 
	void OnFailure() override
 
	{
 
@@ -661,12 +644,12 @@ void NetworkQueryServer(const std::strin
 
}
 

	
 
/** Non blocking connection to query servers for their game and company info. */
 
class TCPLobbyQueryConnecter : TCPConnecter {
 
class TCPLobbyQueryConnecter : TCPServerConnecter {
 
private:
 
	std::string connection_string;
 

	
 
public:
 
	TCPLobbyQueryConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
 
	TCPLobbyQueryConnecter(const std::string &connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
 

	
 
	void OnFailure() override
 
	{
 
@@ -756,12 +739,12 @@ void NetworkRebuildHostList()
 
}
 

	
 
/** Non blocking connection create to actually connect to servers */
 
class TCPClientConnecter : TCPConnecter {
 
class TCPClientConnecter : TCPServerConnecter {
 
private:
 
	std::string connection_string;
 

	
 
public:
 
	TCPClientConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
 
	TCPClientConnecter(const std::string &connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
 

	
 
	void OnFailure() override
 
	{
 
@@ -797,7 +780,7 @@ public:
 
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password, const std::string &join_company_password)
 
{
 
	CompanyID join_as = default_company;
 
	std::string resolved_connection_string = ParseGameConnectionString(connection_string, NETWORK_DEFAULT_PORT, &join_as).GetAddressAsString(false);
 
	std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT, &join_as).connection_string;
 

	
 
	if (!_network_available) return false;
 
	if (!NetworkValidateOurClientName()) return false;
src/network/network_gamelist.cpp
Show inline comments
 
@@ -26,7 +26,7 @@ int _network_game_list_version = 0; ///<
 
/**
 
 * Add a new item to the linked gamelist. If the IP and Port match
 
 * return the existing item instead of adding it again
 
 * @param address the address of the to-be added item
 
 * @param connection_string the address of the to-be added item
 
 * @return a point to the newly added or already existing item
 
 */
 
NetworkGameList *NetworkGameListAddItem(const std::string &connection_string)
 
@@ -34,7 +34,7 @@ NetworkGameList *NetworkGameListAddItem(
 
	NetworkGameList *item, *prev_item;
 

	
 
	/* Parse the connection string to ensure the default port is there. */
 
	const std::string resolved_connection_string = ParseConnectionString(connection_string, NETWORK_DEFAULT_PORT).GetAddressAsString(false);
 
	const std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT).connection_string;
 

	
 
	prev_item = nullptr;
 
	for (item = _network_game_list; item != nullptr; item = item->next) {
0 comments (0 inline, 0 general)