OpenTTD Source: TCPConnecter Class Reference

OpenTTD Source 13.1

Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends
TCPConnecter Class Reference

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

`#include <tcp.h>`

Inheritance diagram for TCPConnecter:

## Public Member Functions
| TCPConnecter (const std::string &connection_string, uint16 default_port, NetworkAddress bind_address={}, int family=AF_UNSPEC)
| Create a new connecter for the given address. More...
virtual void | OnConnect (SOCKET s)
| Callback when the connection succeeded. More...
virtual void | OnFailure ()
| Callback for when the connection attempt failed.
void | Kill ()
| Kill this connecter. More...

## Static Public Member Functions
static void | CheckCallbacks ()
| Check whether we need to call the callback, i.e. More...
static void | KillAll ()
| Kill all connection attempts.

## Private Types
enum | Status { Status::Init, Status::Resolving, Status::Failure, Status::Connecting, Status::Connected }
| The current status of the connecter. More...

## Private Member Functions
void | Resolve ()
| Start resolving the hostname. More...
void | OnResolved (addrinfo *ai)
| Callback when resolving is done. More...
bool | TryNextAddress ()
| Start the connect() for the next address in the list. More...
void | Connect (addrinfo *address)
| Start a connection to the indicated address. More...
virtual bool | CheckActivity ()
| Check if there was activity for this connecter. More...

## 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.

## Friends
class | TCPServerConnecter

## Detailed Description

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

Definition at line 71 of file tcp.h.

## Member Enumeration Documentation

## ◆ Status

enum TCPConnecter::Status
| 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 80 of file tcp.h.

## Constructor & Destructor Documentation

## ◆ TCPConnecter()

TCPConnecter::TCPConnecter | ( | const std::string & | connection_string,
|  | uint16 | default_port,
|  | NetworkAddress | bind_address = `{}`,
|  | int | family = `AF_UNSPEC`
| ) |  |

Create a new connecter for the given address.

Parameters

  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 32 of file tcp_connect.cpp.

References _tcp_connecters, connection_string, and NormalizeConnectionString().

## Member Function Documentation

## ◆ CheckActivity()

bool TCPConnecter::CheckActivity | ( |  | ) |
| privatevirtual

Check if there was activity for this connecter.

Returns
  True iff the TCPConnecter is done and can be cleaned up.

Reimplemented in TCPServerConnecter.

Definition at line 279 of file tcp_connect.cpp.

References NetworkError::AsString(), Connected, Connecting, connection_string, Debug, Failure, NetworkError::GetLast(), GetSocketError(), NetworkError::HasError(), Init, killed, last_attempt, OnFailure(), Resolve(), resolve_thread, ResolveThunk(), Resolving, sock_to_address, StartNewThread(), status, and TryNextAddress().

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

## ◆ 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 471 of file tcp_connect.cpp.

References _tcp_connecters, and CheckActivity().

Referenced by NetworkBackgroundLoop().

## ◆ Connect()

void TCPConnecter::Connect | ( | addrinfo * | address | ) |
| private

Start a connection to the indicated address.

Parameters

  address | The address to connection to.

Definition at line 95 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, and NetworkAddress::SocketTypeAsString().

Referenced by TryNextAddress().

## ◆ 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 85 of file tcp_connect.cpp.

References killed.

Referenced by ClientNetworkCoordinatorSocketHandler::CloseAllConnections(), ClientNetworkStunSocketHandler::CloseConnection(), ClientNetworkTurnSocketHandler::CloseConnection(), ClientNetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT(), ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT(), and ClientNetworkCoordinatorSocketHandler::Receive_GC_TURN_CONNECT().

## ◆ OnConnect()

virtual void TCPConnecter::OnConnect | ( | SOCKET | s | ) |
| inlinevirtual

Callback when the connection succeeded.

Parameters

  s | the socket that we opened

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

Definition at line 125 of file tcp.h.

Referenced by TCPServerConnecter::CheckActivity().

## ◆ OnResolved()

void TCPConnecter::OnResolved | ( | addrinfo * | ai | ) |
| private

Callback when resolving is done.

Parameters

  ai | A linked-list of address information.

Definition at line 155 of file tcp_connect.cpp.

References addresses, ai, and family.

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 227 of file tcp_connect.cpp.

References ai, Connecting, connection_string, Debug, error(), Failure, NetworkAddress::GetHostname(), NetworkAddress::GetPort(), lastof, OnResolved(), ParseConnectionString(), seprintf(), and status.

Referenced by CheckActivity(), and ResolveThunk().

## ◆ 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 141 of file tcp_connect.cpp.

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

Referenced by CheckActivity().

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

- src/network/core/tcp.h
- src/network/core/tcp_connect.cpp