mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-09 15:41:15 +00:00
Codechange: use std::shared_ptr for vector of TCPConnecters
This commit is contained in:
parent
71b8801b61
commit
2d77f09a81
@ -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 {
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -624,7 +624,7 @@ static void NetworkInitialize(bool close_admins = true)
|
||||
}
|
||||
|
||||
/** 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::string &connection_string)
|
||||
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()
|
||||
|
@ -756,7 +756,7 @@ ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
|
||||
}
|
||||
|
||||
/** Connect to the content server. */
|
||||
class NetworkContentConnecter : TCPConnecter {
|
||||
class NetworkContentConnecter : public TCPConnecter {
|
||||
public:
|
||||
/**
|
||||
* Initiate the connecting.
|
||||
@ -791,7 +791,7 @@ void ClientNetworkContentSocketHandler::Connect()
|
||||
this->isCancelled = false;
|
||||
this->isConnecting = true;
|
||||
|
||||
new NetworkContentConnecter(NetworkContentServerConnectionString());
|
||||
TCPConnecter::Create<NetworkContentConnecter>(NetworkContentServerConnectionString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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 ClientNetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT(Packet *p)
|
||||
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 ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT(Packet *p)
|
||||
* 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 ClientNetworkCoordinatorSocketHandler::Connect()
|
||||
this->connecting = true;
|
||||
this->last_activity = std::chrono::steady_clock::now();
|
||||
|
||||
new NetworkCoordinatorConnecter(NetworkCoordinatorConnectionString());
|
||||
TCPConnecter::Create<NetworkCoordinatorConnecter>(NetworkCoordinatorConnectionString());
|
||||
}
|
||||
|
||||
NetworkRecvStatus ClientNetworkCoordinatorSocketHandler::CloseConnection(bool error)
|
||||
|
@ -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.
|
||||
|
@ -71,7 +71,7 @@ void ClientNetworkStunSocketHandler::Connect(const std::string &token, uint8_t f
|
||||
this->token = token;
|
||||
this->family = family;
|
||||
|
||||
this->connecter = new NetworkStunConnecter(this, NetworkStunConnectionString(), token, family);
|
||||
this->connecter = TCPConnecter::Create<NetworkStunConnecter>(this, NetworkStunConnectionString(), token, family);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -73,7 +73,7 @@ bool ClientNetworkTurnSocketHandler::Receive_TURN_CONNECTED(Packet *p)
|
||||
void ClientNetworkTurnSocketHandler::Connect()
|
||||
{
|
||||
this->connect_started = true;
|
||||
this->connecter = new NetworkTurnConnecter(this, this->connection_string);
|
||||
this->connecter = TCPConnecter::Create<NetworkTurnConnecter>(this, this->connection_string);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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) {}
|
||||
|
Loading…
Reference in New Issue
Block a user