Codechange: use std::shared_ptr for vector of TCPConnecters

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

View File

@@ -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::string &connection_string, uint16_t defaul
family(family)
{
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:
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();
}