Changeset - r28531:925d537e515d
[Not reviewed]
master
0 10 0
Rubidium - 11 months ago 2024-01-20 20:04:49
rubidium@openttd.org
Codechange: use std::shared_ptr for vector of TCPConnecters
10 files changed with 35 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/network/core/tcp.h
Show inline comments
 
@@ -100,6 +100,8 @@ private:
 
	NetworkAddress bind_address;                        ///< Address we're binding to, if any.
 
	int family = AF_UNSPEC;                             ///< Family we are using to connect with.
 

	
 
	static std::vector<std::shared_ptr<TCPConnecter>> connecters; ///< List of connections that are currently being created.
 

	
 
	void Resolve();
 
	void OnResolved(addrinfo *ai);
 
	bool TryNextAddress();
 
@@ -132,6 +134,18 @@ public:
 

	
 
	static void CheckCallbacks();
 
	static void KillAll();
 

	
 
	/**
 
	 * Create the connecter, and initiate connecting by putting it in the collection of TCP connections to make.
 
	 * @tparam T The type of connecter to create.
 
	 * @param args The arguments to the constructor of T.
 
	 * @return Shared pointer to the connecter.
 
	 */
 
	template <class T, typename... Args>
 
	static std::shared_ptr<TCPConnecter> Create(Args&& ... args)
 
	{
 
		return TCPConnecter::connecters.emplace_back(std::make_shared<T>(std::forward<Args>(args)...));
 
	}
 
};
 

	
 
class TCPServerConnecter : public TCPConnecter {
src/network/core/tcp_connect.cpp
Show inline comments
 
@@ -18,8 +18,7 @@
 

	
 
#include "../../safeguards.h"
 

	
 
/** List of connections that are currently being created */
 
static std::vector<TCPConnecter *> _tcp_connecters;
 
/* static */ std::vector<std::shared_ptr<TCPConnecter>> TCPConnecter::connecters;
 

	
 
/**
 
 * Create a new connecter for the given address.
 
@@ -32,8 +31,6 @@ TCPConnecter::TCPConnecter(const std::st
 
	family(family)
 
{
 
	this->connection_string = NormalizeConnectionString(connection_string, default_port);
 

	
 
	_tcp_connecters.push_back(this);
 
}
 

	
 
/**
 
@@ -57,8 +54,6 @@ TCPServerConnecter::TCPServerConnecter(c
 
		default:
 
			NOT_REACHED();
 
	}
 

	
 
	_tcp_connecters.push_back(this);
 
}
 

	
 
TCPConnecter::~TCPConnecter()
 
@@ -467,24 +462,14 @@ void TCPServerConnecter::SetFailure()
 
 */
 
/* static */ void TCPConnecter::CheckCallbacks()
 
{
 
	for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) {
 
		TCPConnecter *cur = *iter;
 

	
 
		if (cur->CheckActivity()) {
 
			iter = _tcp_connecters.erase(iter);
 
			delete cur;
 
		} else {
 
			iter++;
 
		}
 
	}
 
	TCPConnecter::connecters.erase(
 
		std::remove_if(TCPConnecter::connecters.begin(), TCPConnecter::connecters.end(),
 
				   [](auto &connecter) { return connecter->CheckActivity(); }),
 
		TCPConnecter::connecters.end());
 
}
 

	
 
/** Kill all connection attempts. */
 
/* static */ void TCPConnecter::KillAll()
 
{
 
	for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) {
 
		TCPConnecter *cur = *iter;
 
		iter = _tcp_connecters.erase(iter);
 
		delete cur;
 
	}
 
	TCPConnecter::connecters.clear();
 
}
src/network/network.cpp
Show inline comments
 
@@ -624,7 +624,7 @@ static void NetworkInitialize(bool close
 
}
 

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

	
 
@@ -658,7 +658,7 @@ void NetworkQueryServer(const std::strin
 
	NetworkGameList *item = NetworkGameListAddItem(connection_string);
 
	item->refreshing = true;
 

	
 
	new TCPQueryConnecter(connection_string);
 
	TCPConnecter::Create<TCPQueryConnecter>(connection_string);
 
}
 

	
 
/**
 
@@ -721,7 +721,7 @@ void NetworkRebuildHostList()
 
}
 

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

	
 
@@ -800,7 +800,7 @@ void NetworkClientJoinGame()
 
	_network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
 
	ShowJoinStatusWindow();
 

	
 
	new TCPClientConnecter(_network_join.connection_string);
 
	TCPConnecter::Create<TCPClientConnecter>(_network_join.connection_string);
 
}
 

	
 
static void NetworkInitGameInfo()
src/network/network_content.cpp
Show inline comments
 
@@ -756,7 +756,7 @@ ClientNetworkContentSocketHandler::~Clie
 
}
 

	
 
/** Connect to the content server. */
 
class NetworkContentConnecter : TCPConnecter {
 
class NetworkContentConnecter : public TCPConnecter {
 
public:
 
	/**
 
	 * Initiate the connecting.
 
@@ -791,7 +791,7 @@ void ClientNetworkContentSocketHandler::
 
	this->isCancelled = false;
 
	this->isConnecting = true;
 

	
 
	new NetworkContentConnecter(NetworkContentServerConnectionString());
 
	TCPConnecter::Create<NetworkContentConnecter>(NetworkContentServerConnectionString());
 
}
 

	
 
/**
src/network/network_coordinator.cpp
Show inline comments
 
@@ -100,7 +100,7 @@ public:
 
};
 

	
 
/** Connect to the Game Coordinator server. */
 
class NetworkCoordinatorConnecter : TCPConnecter {
 
class NetworkCoordinatorConnecter : public TCPConnecter {
 
public:
 
	/**
 
	 * Initiate the connecting.
 
@@ -306,7 +306,7 @@ bool ClientNetworkCoordinatorSocketHandl
 
		this->game_connecter = nullptr;
 
	}
 

	
 
	this->game_connecter = new NetworkDirectConnecter(hostname, port, token, tracking_number);
 
	this->game_connecter = TCPConnecter::Create<NetworkDirectConnecter>(hostname, port, token, tracking_number);
 
	return true;
 
}
 

	
 
@@ -349,7 +349,7 @@ bool ClientNetworkCoordinatorSocketHandl
 
	 * STUN server. This means that if there is any NAT in the local network,
 
	 * the public ip:port is still pointing to the local address, and as such
 
	 * a connection can be established. */
 
	this->game_connecter = new NetworkReuseStunConnecter(host, port, family_it->second->local_addr, token, tracking_number, family);
 
	this->game_connecter = TCPConnecter::Create<NetworkReuseStunConnecter>(host, port, family_it->second->local_addr, token, tracking_number, family);
 
	return true;
 
}
 

	
 
@@ -426,7 +426,7 @@ void ClientNetworkCoordinatorSocketHandl
 
	this->connecting = true;
 
	this->last_activity = std::chrono::steady_clock::now();
 

	
 
	new NetworkCoordinatorConnecter(NetworkCoordinatorConnectionString());
 
	TCPConnecter::Create<NetworkCoordinatorConnecter>(NetworkCoordinatorConnectionString());
 
}
 

	
 
NetworkRecvStatus ClientNetworkCoordinatorSocketHandler::CloseConnection(bool error)
src/network/network_coordinator.h
Show inline comments
 
@@ -57,7 +57,7 @@ private:
 
	std::map<std::string, TCPServerConnecter *> connecter_pre; ///< Based on invite codes, the current connecters that are pending.
 
	std::map<std::string, std::map<int, std::unique_ptr<ClientNetworkStunSocketHandler>>> stun_handlers; ///< All pending STUN handlers, stored by token:family.
 
	std::map<std::string, std::unique_ptr<ClientNetworkTurnSocketHandler>> turn_handlers; ///< Pending TURN handler (if any), stored by token.
 
	TCPConnecter *game_connecter = nullptr; ///< Pending connecter to the game server.
 
	std::shared_ptr<TCPConnecter> game_connecter{}; ///< Pending connecter to the game server.
 

	
 
	uint32_t newgrf_lookup_table_cursor = 0; ///< Last received cursor for the #GameInfoNewGRFLookupTable updates.
 
	GameInfoNewGRFLookupTable newgrf_lookup_table; ///< Table to look up NewGRFs in the GC_LISTING packets.
src/network/network_stun.cpp
Show inline comments
 
@@ -71,7 +71,7 @@ void ClientNetworkStunSocketHandler::Con
 
	this->token = token;
 
	this->family = family;
 

	
 
	this->connecter = new NetworkStunConnecter(this, NetworkStunConnectionString(), token, family);
 
	this->connecter = TCPConnecter::Create<NetworkStunConnecter>(this, NetworkStunConnectionString(), token, family);
 
}
 

	
 
/**
src/network/network_stun.h
Show inline comments
 
@@ -20,7 +20,7 @@ private:
 
	bool sent_result = false; ///< Did we sent the result of the STUN connection?
 

	
 
public:
 
	TCPConnecter *connecter = nullptr; ///< Connecter instance.
 
	std::shared_ptr<TCPConnecter> connecter{}; ///< Connecter instance.
 
	NetworkAddress local_addr;         ///< Local addresses of the socket.
 

	
 
	NetworkRecvStatus CloseConnection(bool error = true) override;
src/network/network_turn.cpp
Show inline comments
 
@@ -73,7 +73,7 @@ bool ClientNetworkTurnSocketHandler::Rec
 
void ClientNetworkTurnSocketHandler::Connect()
 
{
 
	this->connect_started = true;
 
	this->connecter = new NetworkTurnConnecter(this, this->connection_string);
 
	this->connecter = TCPConnecter::Create<NetworkTurnConnecter>(this, this->connection_string);
 
}
 

	
 
/**
src/network/network_turn.h
Show inline comments
 
@@ -24,7 +24,7 @@ protected:
 
	bool Receive_TURN_CONNECTED(Packet *p) override;
 

	
 
public:
 
	TCPConnecter *connecter = nullptr; ///< Connecter instance.
 
	std::shared_ptr<TCPConnecter> connecter{}; ///< Connecter instance.
 
	bool connect_started = false;      ///< Whether we started the connection.
 

	
 
	ClientNetworkTurnSocketHandler(const std::string &token, uint8_t tracking_number, const std::string &connection_string) : token(token), tracking_number(tracking_number), connection_string(connection_string) {}
0 comments (0 inline, 0 general)