OpenTTD Source 20241224-master-gee860a5c8e
|
"Helper" class for creating TCP connections in a non-blocking manner More...
#include <tcp.h>
Public Member Functions | |
TCPConnecter (const std::string &connection_string, uint16_t default_port, const NetworkAddress &bind_address={}, int family=AF_UNSPEC) | |
Create a new connecter for the given address. | |
virtual void | OnConnect (SOCKET s) |
Callback when the connection succeeded. | |
virtual void | OnFailure () |
Callback for when the connection attempt failed. | |
void | Kill () |
Kill this connecter. | |
Static Public Member Functions | |
static void | CheckCallbacks () |
Check whether we need to call the callback, i.e. | |
static void | KillAll () |
Kill all connection attempts. | |
template<class T , typename... Args> | |
static std::shared_ptr< TCPConnecter > | Create (Args &&... args) |
Create the connecter, and initiate connecting by putting it in the collection of TCP connections to make. | |
Private Types | |
enum class | Status { Init , Resolving , Failure , Connecting , Connected } |
The current status of the connecter. More... | |
Private Member Functions | |
void | Resolve () |
Start resolving the hostname. | |
void | OnResolved (addrinfo *ai) |
Callback when resolving is done. | |
bool | TryNextAddress () |
Start the connect() for the next address in the list. | |
void | Connect (addrinfo *address) |
Start a connection to the indicated address. | |
virtual bool | CheckActivity () |
Check if there was activity for this connecter. | |
Static Private Member Functions | |
static void | ResolveThunk (TCPConnecter *connecter) |
Thunk to start Resolve() on the right instance. | |
Private Attributes | |
std::thread | resolve_thread |
Thread used during resolving. | |
std::atomic< Status > | status = Status::Init |
The current status of the connecter. | |
std::atomic< bool > | killed = false |
Whether this connecter is marked as killed. | |
addrinfo * | ai = nullptr |
getaddrinfo() allocated linked-list of resolved addresses. | |
std::vector< addrinfo * > | addresses |
Addresses we can connect to. | |
std::map< SOCKET, NetworkAddress > | sock_to_address |
Mapping of a socket to the real address it is connecting to. USed for DEBUG statements. | |
size_t | current_address = 0 |
Current index in addresses we are trying. | |
std::vector< SOCKET > | sockets |
Pending connect() attempts. | |
std::chrono::steady_clock::time_point | last_attempt |
Time we last tried to connect. | |
std::string | connection_string |
Current address we are connecting to (before resolving). | |
NetworkAddress | bind_address |
Address we're binding to, if any. | |
int | family = AF_UNSPEC |
Family we are using to connect with. | |
Static Private Attributes | |
static std::vector< std::shared_ptr< TCPConnecter > > | connecters |
List of connections that are currently being created. | |
Friends | |
class | TCPServerConnecter |
"Helper" class for creating TCP connections in a non-blocking manner
|
strongprivate |
The current status of the connecter.
We track the status like this to ensure everything is executed from the game-thread, and not at another random time where we might not have the lock on the game-state.
Enumerator | |
---|---|
Init | TCPConnecter is created but resolving hasn't started. |
Resolving | The hostname is being resolved (threaded). |
Failure | Resolving failed. |
Connecting | We are currently connecting. |
Connected | The connection is established. |
TCPConnecter::TCPConnecter | ( | const std::string & | connection_string, |
uint16_t | default_port, | ||
const NetworkAddress & | bind_address = {} , |
||
int | family = AF_UNSPEC |
||
) |
Create a new connecter for the given address.
connection_string | The address to connect to. |
default_port | If not indicated in connection_string, what port to use. |
bind_address | The local bind address to use. Defaults to letting the OS find one. |
Definition at line 29 of file tcp_connect.cpp.
References connection_string, and NormalizeConnectionString().
|
virtual |
Definition at line 59 of file tcp_connect.cpp.
|
privatevirtual |
Check if there was activity for this connecter.
Reimplemented in TCPServerConnecter.
Definition at line 271 of file tcp_connect.cpp.
References NetworkError::AsString(), Connected, Connecting, connection_string, Debug, Failure, NetworkError::GetLast(), NetworkAddress::GetPeerName(), GetSocketError(), NetworkError::HasError(), Init, killed, last_attempt, OnConnect(), OnFailure(), Resolve(), resolve_thread, ResolveThunk(), Resolving, sock_to_address, sockets, StartNewThread(), status, and TryNextAddress().
Referenced by TCPServerConnecter::CheckActivity().
|
static |
Check whether we need to call the callback, i.e.
whether we have connected or aborted and call the appropriate callback for that. It's done this way to ease on the locking that would otherwise be needed everywhere.
Definition at line 463 of file tcp_connect.cpp.
References connecters.
Referenced by NetworkBackgroundLoop().
|
private |
Start a connection to the indicated address.
address | The address to connection to. |
Definition at line 88 of file tcp_connect.cpp.
References NetworkAddress::AddressFamilyAsString(), NetworkError::AsString(), bind_address, Debug, NetworkAddress::GetAddress(), NetworkAddress::GetAddressAsString(), NetworkError::GetLast(), NetworkAddress::GetPort(), SetNoDelay(), SetNonBlocking(), SetReusePort(), sock_to_address, sockets, and NetworkAddress::SocketTypeAsString().
Referenced by TryNextAddress().
|
inlinestatic |
Create the connecter, and initiate connecting by putting it in the collection of TCP connections to make.
T | The type of connecter to create. |
args | The arguments to the constructor of T. |
Definition at line 145 of file tcp.h.
References connecters.
void TCPConnecter::Kill | ( | ) |
Kill this connecter.
It will abort as soon as it can and not call any of the callbacks.
Definition at line 78 of file tcp_connect.cpp.
References killed.
|
static |
Kill all connection attempts.
Definition at line 472 of file tcp_connect.cpp.
References connecters.
|
inlinevirtual |
Callback when the connection succeeded.
s | the socket that we opened |
Reimplemented in TCPQueryConnecter, TCPClientConnecter, NetworkContentConnecter, NetworkDirectConnecter, NetworkReuseStunConnecter, NetworkCoordinatorConnecter, NetworkStunConnecter, and NetworkTurnConnecter.
Definition at line 126 of file tcp.h.
Referenced by CheckActivity(), and TCPServerConnecter::CheckActivity().
|
inlinevirtual |
Callback for when the connection attempt failed.
Reimplemented in TCPQueryConnecter, TCPClientConnecter, NetworkContentConnecter, NetworkDirectConnecter, NetworkReuseStunConnecter, NetworkCoordinatorConnecter, NetworkStunConnecter, and NetworkTurnConnecter.
Definition at line 131 of file tcp.h.
Referenced by CheckActivity(), and TCPServerConnecter::CheckActivity().
|
private |
Callback when resolving is done.
ai | A linked-list of address information. |
Definition at line 148 of file tcp_connect.cpp.
References addresses, ai, connection_string, current_address, Debug, family, and NetworkAddress::GetAddressAsString().
Referenced by Resolve().
|
private |
Start resolving the hostname.
This function must change "status" to either Status::FAILURE or Status::CONNECTING before returning.
Definition at line 220 of file tcp_connect.cpp.
References ai, Connecting, connection_string, Debug, Failure, NetworkAddress::GetHostname(), NetworkAddress::GetPort(), OnResolved(), ParseConnectionString(), and status.
Referenced by CheckActivity(), and ResolveThunk().
|
staticprivate |
Thunk to start Resolve() on the right instance.
Definition at line 262 of file tcp_connect.cpp.
References Resolve().
Referenced by CheckActivity().
|
private |
Start the connect() for the next address in the list.
Definition at line 134 of file tcp_connect.cpp.
References addresses, Connect(), current_address, and last_attempt.
Referenced by CheckActivity().
|
friend |
|
private |
Addresses we can connect to.
Definition at line 92 of file tcp.h.
Referenced by OnResolved(), and TryNextAddress().
|
private |
getaddrinfo() allocated linked-list of resolved addresses.
Definition at line 91 of file tcp.h.
Referenced by OnResolved(), and Resolve().
|
private |
|
staticprivate |
List of connections that are currently being created.
Definition at line 103 of file tcp.h.
Referenced by CheckCallbacks(), Create(), and KillAll().
|
private |
Current address we are connecting to (before resolving).
Definition at line 99 of file tcp.h.
Referenced by CheckActivity(), OnResolved(), Resolve(), and TCPConnecter().
|
private |
Current index in addresses we are trying.
Definition at line 94 of file tcp.h.
Referenced by OnResolved(), and TryNextAddress().
|
private |
Family we are using to connect with.
Definition at line 101 of file tcp.h.
Referenced by OnResolved().
|
private |
Whether this connecter is marked as killed.
Definition at line 89 of file tcp.h.
Referenced by CheckActivity(), TCPServerConnecter::CheckActivity(), and Kill().
|
private |
Time we last tried to connect.
Definition at line 97 of file tcp.h.
Referenced by CheckActivity(), and TryNextAddress().
|
private |
|
private |
Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.
Definition at line 93 of file tcp.h.
Referenced by CheckActivity(), and Connect().
|
private |
Pending connect() attempts.
Definition at line 96 of file tcp.h.
Referenced by CheckActivity(), and Connect().
|
private |
The current status of the connecter.
Definition at line 88 of file tcp.h.
Referenced by CheckActivity(), TCPServerConnecter::CheckActivity(), Resolve(), TCPServerConnecter::SetConnected(), TCPServerConnecter::SetFailure(), and TCPServerConnecter::TCPServerConnecter().