1
0
Fork 0

Codechange: use std::shared_ptr for vector of TCPConnecters

pull/11845/head
Rubidium 2024-01-20 21:04:49 +01:00 committed by rubidium42
parent 71b8801b61
commit 2d77f09a81
10 changed files with 35 additions and 36 deletions

View File

@ -100,6 +100,8 @@ private:
NetworkAddress bind_address; ///< Address we're binding to, if any. NetworkAddress bind_address; ///< Address we're binding to, if any.
int family = AF_UNSPEC; ///< Family we are using to connect with. 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 Resolve();
void OnResolved(addrinfo *ai); void OnResolved(addrinfo *ai);
bool TryNextAddress(); bool TryNextAddress();
@ -132,6 +134,18 @@ public:
static void CheckCallbacks(); static void CheckCallbacks();
static void KillAll(); 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 { class TCPServerConnecter : public TCPConnecter {

View File

@ -18,8 +18,7 @@
#include "../../safeguards.h" #include "../../safeguards.h"
/** List of connections that are currently being created */ /* static */ std::vector<std::shared_ptr<TCPConnecter>> TCPConnecter::connecters;
static std::vector<TCPConnecter *> _tcp_connecters;
/** /**
* Create a new connecter for the given address. * Create a new connecter for the given address.
@ -32,8 +31,6 @@ TCPConnecter::TCPConnecter(const std::string &connection_string, uint16_t defaul
family(family) family(family)
{ {
this->connection_string = NormalizeConnectionString(connection_string, default_port); this->connection_string = NormalizeConnectionString(connection_string, default_port);
_tcp_connecters.push_back(this);
} }
/** /**
@ -57,8 +54,6 @@ TCPServerConnecter::TCPServerConnecter(const std::string &connection_string, uin
default: default:
NOT_REACHED(); NOT_REACHED();
} }
_tcp_connecters.push_back(this);
} }
TCPConnecter::~TCPConnecter() TCPConnecter::~TCPConnecter()
@ -467,24 +462,14 @@ void TCPServerConnecter::SetFailure()
*/ */
/* static */ void TCPConnecter::CheckCallbacks() /* static */ void TCPConnecter::CheckCallbacks()
{ {
for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) { TCPConnecter::connecters.erase(
TCPConnecter *cur = *iter; std::remove_if(TCPConnecter::connecters.begin(), TCPConnecter::connecters.end(),
[](auto &connecter) { return connecter->CheckActivity(); }),
if (cur->CheckActivity()) { TCPConnecter::connecters.end());
iter = _tcp_connecters.erase(iter);
delete cur;
} else {
iter++;
}
}
} }
/** Kill all connection attempts. */ /** Kill all connection attempts. */
/* static */ void TCPConnecter::KillAll() /* static */ void TCPConnecter::KillAll()
{ {
for (auto iter = _tcp_connecters.begin(); iter < _tcp_connecters.end(); /* nothing */) { TCPConnecter::connecters.clear();
TCPConnecter *cur = *iter;
iter = _tcp_connecters.erase(iter);
delete cur;
}
} }

View File

@ -624,7 +624,7 @@ static void NetworkInitialize(bool close_admins = true)
} }
/** Non blocking connection to query servers for their game info. */ /** Non blocking connection to query servers for their game info. */
class TCPQueryConnecter : TCPServerConnecter { class TCPQueryConnecter : public TCPServerConnecter {
private: private:
std::string connection_string; std::string connection_string;
@ -658,7 +658,7 @@ void NetworkQueryServer(const std::string &connection_string)
NetworkGameList *item = NetworkGameListAddItem(connection_string); NetworkGameList *item = NetworkGameListAddItem(connection_string);
item->refreshing = true; 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 */ /** Non blocking connection create to actually connect to servers */
class TCPClientConnecter : TCPServerConnecter { class TCPClientConnecter : public TCPServerConnecter {
private: private:
std::string connection_string; std::string connection_string;
@ -800,7 +800,7 @@ void NetworkClientJoinGame()
_network_join_status = NETWORK_JOIN_STATUS_CONNECTING; _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
ShowJoinStatusWindow(); ShowJoinStatusWindow();
new TCPClientConnecter(_network_join.connection_string); TCPConnecter::Create<TCPClientConnecter>(_network_join.connection_string);
} }
static void NetworkInitGameInfo() static void NetworkInitGameInfo()

View File

@ -756,7 +756,7 @@ ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
} }
/** Connect to the content server. */ /** Connect to the content server. */
class NetworkContentConnecter : TCPConnecter { class NetworkContentConnecter : public TCPConnecter {
public: public:
/** /**
* Initiate the connecting. * Initiate the connecting.
@ -791,7 +791,7 @@ void ClientNetworkContentSocketHandler::Connect()
this->isCancelled = false; this->isCancelled = false;
this->isConnecting = true; this->isConnecting = true;
new NetworkContentConnecter(NetworkContentServerConnectionString()); TCPConnecter::Create<NetworkContentConnecter>(NetworkContentServerConnectionString());
} }
/** /**

View File

@ -100,7 +100,7 @@ public:
}; };
/** Connect to the Game Coordinator server. */ /** Connect to the Game Coordinator server. */
class NetworkCoordinatorConnecter : TCPConnecter { class NetworkCoordinatorConnecter : public TCPConnecter {
public: public:
/** /**
* Initiate the connecting. * Initiate the connecting.
@ -306,7 +306,7 @@ bool ClientNetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT(Packet *p)
this->game_connecter = nullptr; 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; return true;
} }
@ -349,7 +349,7 @@ bool ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT(Packet *p)
* STUN server. This means that if there is any NAT in the local network, * 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 * the public ip:port is still pointing to the local address, and as such
* a connection can be established. */ * 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; return true;
} }
@ -426,7 +426,7 @@ void ClientNetworkCoordinatorSocketHandler::Connect()
this->connecting = true; this->connecting = true;
this->last_activity = std::chrono::steady_clock::now(); this->last_activity = std::chrono::steady_clock::now();
new NetworkCoordinatorConnecter(NetworkCoordinatorConnectionString()); TCPConnecter::Create<NetworkCoordinatorConnecter>(NetworkCoordinatorConnectionString());
} }
NetworkRecvStatus ClientNetworkCoordinatorSocketHandler::CloseConnection(bool error) NetworkRecvStatus ClientNetworkCoordinatorSocketHandler::CloseConnection(bool error)

View File

@ -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, 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::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. 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. 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. GameInfoNewGRFLookupTable newgrf_lookup_table; ///< Table to look up NewGRFs in the GC_LISTING packets.

View File

@ -71,7 +71,7 @@ void ClientNetworkStunSocketHandler::Connect(const std::string &token, uint8_t f
this->token = token; this->token = token;
this->family = family; this->family = family;
this->connecter = new NetworkStunConnecter(this, NetworkStunConnectionString(), token, family); this->connecter = TCPConnecter::Create<NetworkStunConnecter>(this, NetworkStunConnectionString(), token, family);
} }
/** /**

View File

@ -20,7 +20,7 @@ private:
bool sent_result = false; ///< Did we sent the result of the STUN connection? bool sent_result = false; ///< Did we sent the result of the STUN connection?
public: public:
TCPConnecter *connecter = nullptr; ///< Connecter instance. std::shared_ptr<TCPConnecter> connecter{}; ///< Connecter instance.
NetworkAddress local_addr; ///< Local addresses of the socket. NetworkAddress local_addr; ///< Local addresses of the socket.
NetworkRecvStatus CloseConnection(bool error = true) override; NetworkRecvStatus CloseConnection(bool error = true) override;

View File

@ -73,7 +73,7 @@ bool ClientNetworkTurnSocketHandler::Receive_TURN_CONNECTED(Packet *p)
void ClientNetworkTurnSocketHandler::Connect() void ClientNetworkTurnSocketHandler::Connect()
{ {
this->connect_started = true; this->connect_started = true;
this->connecter = new NetworkTurnConnecter(this, this->connection_string); this->connecter = TCPConnecter::Create<NetworkTurnConnecter>(this, this->connection_string);
} }
/** /**

View File

@ -24,7 +24,7 @@ protected:
bool Receive_TURN_CONNECTED(Packet *p) override; bool Receive_TURN_CONNECTED(Packet *p) override;
public: public:
TCPConnecter *connecter = nullptr; ///< Connecter instance. std::shared_ptr<TCPConnecter> connecter{}; ///< Connecter instance.
bool connect_started = false; ///< Whether we started the connection. 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) {} ClientNetworkTurnSocketHandler(const std::string &token, uint8_t tracking_number, const std::string &connection_string) : token(token), tracking_number(tracking_number), connection_string(connection_string) {}