OpenTTD Source 20260206-master-g4d4e37dbf1
TCPConnecter Class Reference

"Helper" class for creating TCP connections in a non-blocking manner More...

#include <tcp.h>

Inheritance diagram for TCPConnecter:
NetworkContentConnecter NetworkCoordinatorConnecter NetworkDirectConnecter NetworkReuseStunConnecter NetworkStunConnecter NetworkTurnConnecter TCPServerConnecter TCPClientConnecter TCPQueryConnecter

Public Member Functions

 TCPConnecter (std::string_view 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< TCPConnecterCreate (Args &&... args)
 Create the connecter, and initiate connecting by putting it in the collection of TCP connections to make.

Private Types

enum class  Status : uint8_t {
  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< Statusstatus = 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, NetworkAddresssock_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

Detailed Description

"Helper" class for creating TCP connections in a non-blocking manner

Definition at line 72 of file tcp.h.

Member Enumeration Documentation

◆ Status

enum class TCPConnecter::Status : uint8_t
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.

Definition at line 81 of file tcp.h.

Constructor & Destructor Documentation

◆ TCPConnecter() [1/2]

TCPConnecter::TCPConnecter ( )
inline

Definition at line 120 of file tcp.h.

◆ TCPConnecter() [2/2]

TCPConnecter::TCPConnecter ( std::string_view connection_string,
uint16_t default_port,
const NetworkAddress & bind_address = {},
int family = AF_UNSPEC )

Create a new connecter for the given address.

Parameters
connection_stringThe address to connect to.
default_portIf not indicated in connection_string, what port to use.
bind_addressThe local bind address to use. Defaults to letting the OS find one.
familyThe IP-family to connect with.

Definition at line 28 of file tcp_connect.cpp.

References bind_address, connection_string, family, and NormalizeConnectionString().

◆ ~TCPConnecter()

TCPConnecter::~TCPConnecter ( )
virtual

Definition at line 58 of file tcp_connect.cpp.

Member Function Documentation

◆ CheckActivity()

bool TCPConnecter::CheckActivity ( )
privatevirtual

◆ CheckCallbacks()

void TCPConnecter::CheckCallbacks ( )
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 461 of file tcp_connect.cpp.

References connecters.

Referenced by NetworkBackgroundLoop().

◆ Connect()

void TCPConnecter::Connect ( addrinfo * address)
private

Start a connection to the indicated address.

Parameters
addressThe address to connection to.

Definition at line 87 of file tcp_connect.cpp.

References NetworkAddress::AddressFamilyAsString(), bind_address, Debug, NetworkAddress::GetAddressAsString(), NetworkError::GetLast(), SetNoDelay(), SetNonBlocking(), SetReusePort(), sock_to_address, and NetworkAddress::SocketTypeAsString().

Referenced by TryNextAddress().

◆ Create()

template<class T, typename... Args>
std::shared_ptr< TCPConnecter > TCPConnecter::Create ( Args &&... args)
inlinestatic

Create the connecter, and initiate connecting by putting it in the collection of TCP connections to make.

Template Parameters
TThe type of connecter to create.
Parameters
argsThe arguments to the constructor of T.
Returns
Shared pointer to the connecter.

Definition at line 147 of file tcp.h.

References connecters.

Referenced by ClientNetworkContentSocketHandler::Connect(), ClientNetworkStunSocketHandler::Connect(), ClientNetworkTurnSocketHandler::Connect(), NetworkClientJoinGame(), NetworkQueryServer(), ClientNetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT(), and ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT().

◆ Kill()

void TCPConnecter::Kill ( )

Kill this connecter.

It will abort as soon as it can and not call any of the callbacks.

Definition at line 77 of file tcp_connect.cpp.

References killed.

◆ KillAll()

void TCPConnecter::KillAll ( )
static

Kill all connection attempts.

Definition at line 470 of file tcp_connect.cpp.

References connecters.

Referenced by ClientNetworkGameSocketHandler::NetworkClose.

◆ OnConnect()

virtual void TCPConnecter::OnConnect ( SOCKET s)
inlinevirtual

Callback when the connection succeeded.

Parameters
sthe socket that we opened

Reimplemented in NetworkContentConnecter, NetworkCoordinatorConnecter, NetworkDirectConnecter, NetworkReuseStunConnecter, NetworkStunConnecter, NetworkTurnConnecter, TCPClientConnecter, and TCPQueryConnecter.

Definition at line 128 of file tcp.h.

Referenced by CheckActivity(), and TCPServerConnecter::CheckActivity().

◆ OnFailure()

virtual void TCPConnecter::OnFailure ( )
inlinevirtual

◆ OnResolved()

void TCPConnecter::OnResolved ( addrinfo * ai)
private

Callback when resolving is done.

Parameters
aiA linked-list of address information.

Definition at line 147 of file tcp_connect.cpp.

References addresses, ai, connection_string, current_address, Debug, family, and NetworkAddress::GetAddressAsString().

Referenced by Resolve().

◆ Resolve()

void TCPConnecter::Resolve ( )
private

Start resolving the hostname.

This function must change "status" to either Status::FAILURE or Status::CONNECTING before returning.

Definition at line 219 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().

◆ ResolveThunk()

void TCPConnecter::ResolveThunk ( TCPConnecter * connecter)
staticprivate

Thunk to start Resolve() on the right instance.

Definition at line 260 of file tcp_connect.cpp.

References Resolve().

Referenced by CheckActivity().

◆ TryNextAddress()

bool TCPConnecter::TryNextAddress ( )
private

Start the connect() for the next address in the list.

Returns
True iff a new connect() is attempted.

Definition at line 133 of file tcp_connect.cpp.

References addresses, Connect(), current_address, and last_attempt.

Referenced by CheckActivity().

◆ TCPServerConnecter

friend class TCPServerConnecter
friend

Definition at line 115 of file tcp.h.

Field Documentation

◆ addresses

std::vector<addrinfo *> TCPConnecter::addresses
private

Addresses we can connect to.

Definition at line 94 of file tcp.h.

Referenced by OnResolved(), and TryNextAddress().

◆ ai

addrinfo* TCPConnecter::ai = nullptr
private

getaddrinfo() allocated linked-list of resolved addresses.

Definition at line 93 of file tcp.h.

Referenced by OnResolved(), and Resolve().

◆ bind_address

NetworkAddress TCPConnecter::bind_address
private

Address we're binding to, if any.

Definition at line 102 of file tcp.h.

Referenced by Connect(), NetworkReuseStunConnecter::NetworkReuseStunConnecter(), and TCPConnecter().

◆ connecters

std::vector< std::shared_ptr< TCPConnecter > > TCPConnecter::connecters
staticprivate

List of connections that are currently being created.

Definition at line 105 of file tcp.h.

Referenced by CheckCallbacks(), Create(), and KillAll().

◆ connection_string

◆ current_address

size_t TCPConnecter::current_address = 0
private

Current index in addresses we are trying.

Definition at line 96 of file tcp.h.

Referenced by OnResolved(), and TryNextAddress().

◆ family

int TCPConnecter::family = AF_UNSPEC
private

Family we are using to connect with.

Definition at line 103 of file tcp.h.

Referenced by OnResolved(), and TCPConnecter().

◆ killed

std::atomic<bool> TCPConnecter::killed = false
private

Whether this connecter is marked as killed.

Definition at line 91 of file tcp.h.

Referenced by CheckActivity(), TCPServerConnecter::CheckActivity(), and Kill().

◆ last_attempt

std::chrono::steady_clock::time_point TCPConnecter::last_attempt
private

Time we last tried to connect.

Definition at line 99 of file tcp.h.

Referenced by CheckActivity(), and TryNextAddress().

◆ resolve_thread

std::thread TCPConnecter::resolve_thread
private

Thread used during resolving.

Definition at line 89 of file tcp.h.

Referenced by CheckActivity().

◆ sock_to_address

std::map<SOCKET, NetworkAddress> TCPConnecter::sock_to_address
private

Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.

Definition at line 95 of file tcp.h.

Referenced by CheckActivity(), and Connect().

◆ sockets

std::vector<SOCKET> TCPConnecter::sockets
private

Pending connect() attempts.

Definition at line 98 of file tcp.h.

◆ status

std::atomic<Status> TCPConnecter::status = Status::Init
private

The current status of the connecter.

Definition at line 90 of file tcp.h.

Referenced by CheckActivity(), TCPServerConnecter::CheckActivity(), Resolve(), TCPServerConnecter::SetConnected(), and TCPServerConnecter::SetFailure().


The documentation for this class was generated from the following files: