OpenTTD Source  20240919-master-gdf0233f4c2
ClientNetworkCoordinatorSocketHandler Class Reference

Game Coordinator communication. More...

#include <network_coordinator.h>

Inheritance diagram for ClientNetworkCoordinatorSocketHandler:
NetworkCoordinatorSocketHandler NetworkTCPSocketHandler NetworkSocketHandler

Public Member Functions

NetworkRecvStatus CloseConnection (bool error=true) override
 This will put this socket handler in a close state. More...
 
void SendReceive ()
 Check whether we received/can send some data from/to the Game Coordinator server and when that's the case handle it appropriately.
 
void ConnectFailure (const std::string &token, uint8_t tracking_number)
 Callback from a Connecter to let the Game Coordinator know the connection failed. More...
 
void ConnectSuccess (const std::string &token, SOCKET sock, NetworkAddress &address)
 Callback from a Connecter to let the Game Coordinator know the connection to the game server is established. More...
 
void StunResult (const std::string &token, uint8_t family, bool result)
 Callback from the STUN connecter to inform the Game Coordinator about the result of the STUN. More...
 
void Connect ()
 
void CloseToken (const std::string &token)
 Close everything related to this connection token. More...
 
void CloseAllConnections ()
 Close all pending connection tokens.
 
void CloseStunHandler (const std::string &token, uint8_t family=AF_UNSPEC)
 Close the STUN handler. More...
 
void CloseTurnHandler (const std::string &token)
 Close the TURN handler. More...
 
void Register ()
 Register our server to receive our invite code.
 
void SendServerUpdate ()
 Send an update of our server status to the Game Coordinator.
 
void GetListing ()
 Request a listing of all public servers.
 
void ConnectToServer (const std::string &invite_code, TCPServerConnecter *connecter)
 Join a server based on an invite code. More...
 
void StartTurnConnection (std::string &token)
 
- Public Member Functions inherited from NetworkCoordinatorSocketHandler
 NetworkCoordinatorSocketHandler (SOCKET s=INVALID_SOCKET)
 Create a new cs socket handler for a given cs. More...
 
bool ReceivePackets ()
 Receive a packet at TCP level. More...
 
- Public Member Functions inherited from NetworkTCPSocketHandler
bool IsConnected () const
 Whether this socket is currently bound to a socket. More...
 
void CloseSocket ()
 Close the actual socket of the connection. More...
 
virtual void SendPacket (std::unique_ptr< Packet > &&packet)
 This function puts the packet in the send-queue and it is send as soon as possible. More...
 
SendPacketsState SendPackets (bool closing_down=false)
 Sends all the buffered packets out for this client. More...
 
virtual std::unique_ptr< PacketReceivePacket ()
 Receives a packet for the given client. More...
 
bool CanSendReceive ()
 Check whether this socket can send or receive something. More...
 
bool HasSendQueue ()
 Whether there is something pending in the send queue. More...
 
 NetworkTCPSocketHandler (SOCKET s=INVALID_SOCKET)
 Construct a socket handler for a TCP connection. More...
 
- Public Member Functions inherited from NetworkSocketHandler
 NetworkSocketHandler ()
 Create a new unbound socket.
 
virtual ~NetworkSocketHandler ()=default
 Close the socket when destructing the socket handler.
 
void MarkClosed ()
 Mark the connection as closed. More...
 
bool HasClientQuit () const
 Whether the current client connected to the socket has quit. More...
 
void Reopen ()
 Reopen the socket so we can send/receive stuff again.
 

Data Fields

std::chrono::steady_clock::time_point last_activity
 The last time there was network activity.
 
bool connecting
 Are we connecting to the Game Coordinator?
 
- Data Fields inherited from NetworkTCPSocketHandler
SOCKET sock
 The socket currently connected to.
 
bool writable
 Can we write to this socket?
 

Static Public Attributes

static constexpr std::chrono::seconds IDLE_TIMEOUT = std::chrono::seconds(60)
 The idle timeout; when to close the connection because it's idle.
 

Protected Member Functions

bool Receive_GC_ERROR (Packet &p) override
 Game Coordinator indicates there was an error. More...
 
bool Receive_GC_REGISTER_ACK (Packet &p) override
 Game Coordinator acknowledges the registration. More...
 
bool Receive_GC_LISTING (Packet &p) override
 Game Coordinator replies with a list of all public servers. More...
 
bool Receive_GC_CONNECTING (Packet &p) override
 Game Coordinator informs the Client under what token it will start the attempt to connect the Server and Client together. More...
 
bool Receive_GC_CONNECT_FAILED (Packet &p) override
 Game Coordinator informs the Client that it failed to find a way to connect the Client to the Server. More...
 
bool Receive_GC_DIRECT_CONNECT (Packet &p) override
 Game Coordinator requests that the Client makes a direct connection to the indicated peer, which is a Server. More...
 
bool Receive_GC_STUN_REQUEST (Packet &p) override
 Game Coordinator requests the client/server to do a STUN request to the STUN server. More...
 
bool Receive_GC_STUN_CONNECT (Packet &p) override
 Game Coordinator informs the client/server of its STUN peer (the host:ip of the other side). More...
 
bool Receive_GC_NEWGRF_LOOKUP (Packet &p) override
 Game Coordinator informs the client of updates for the NewGRFs lookup table as used by the NewGRF deserialization in GC_LISTING. More...
 
bool Receive_GC_TURN_CONNECT (Packet &p) override
 Game Coordinator requests that we make a connection to the indicated peer, which is a TURN server. More...
 
- Protected Member Functions inherited from NetworkCoordinatorSocketHandler
bool ReceiveInvalidPacket (PacketCoordinatorType type)
 Helper for logging receiving invalid packets. More...
 
virtual bool Receive_SERVER_REGISTER (Packet &p)
 Server is starting a multiplayer game and wants to let the Game Coordinator know. More...
 
virtual bool Receive_SERVER_UPDATE (Packet &p)
 Send an update of the current state of the server to the Game Coordinator. More...
 
virtual bool Receive_CLIENT_LISTING (Packet &p)
 Client requests a list of all public servers. More...
 
virtual bool Receive_CLIENT_CONNECT (Packet &p)
 Client wants to connect to a Server. More...
 
virtual bool Receive_SERCLI_CONNECT_FAILED (Packet &p)
 Client or Server failed to connect to the remote side. More...
 
virtual bool Receive_CLIENT_CONNECTED (Packet &p)
 Client informs the Game Coordinator the connection with the Server is established. More...
 
virtual bool Receive_SERCLI_STUN_RESULT (Packet &p)
 Client/server informs the Game Coordinator the result of a STUN request. More...
 
bool HandlePacket (Packet &p)
 Handle the given packet, i.e. More...
 

Private Attributes

std::chrono::steady_clock::time_point next_update
 When to send the next update (if server and public).
 
std::map< std::string, std::pair< std::string, TCPServerConnecter * > > connecter
 Based on tokens, the current (invite-code, connecter) that are pending.
 
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.
 
std::shared_ptr< TCPConnectergame_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.
 

Additional Inherited Members

- Protected Attributes inherited from NetworkSocketHandler
std::unique_ptr< class NetworkEncryptionHandlerreceive_encryption_handler
 The handler for decrypting received packets.
 
std::unique_ptr< class NetworkEncryptionHandlersend_encryption_handler
 The handler for encrypting sent packets.
 

Detailed Description

Game Coordinator communication.

For more detail about what the Game Coordinator does, please see docs/game_coordinator.md.

For servers:

  • Server sends SERVER_REGISTER.
  • Game Coordinator probes server to check if it can directly connect.
  • Game Coordinator sends GC_REGISTER_ACK with type of connection.
  • Server sends every 30 seconds SERVER_UPDATE.

For clients (listing):

  • Client sends CLIENT_LISTING.
  • Game Coordinator returns the full list of public servers via GC_LISTING (multiple packets).

For clients (connecting):

  • Client sends CLIENT_CONNECT.
  • Game Coordinator checks what type of connections the servers supports: 1) Direct connect?
    • Send the client a GC_CONNECT with the peer address.
    • a) Client connects, client sends CLIENT_CONNECTED to Game Coordinator.
    • b) Client connect fails, client sends CLIENT_CONNECT_FAILED to Game Coordinator. 2) STUN?
    • Game Coordinator sends GC_STUN_REQUEST to server/client (asking for both IPv4 and IPv6 STUN requests).
    • Game Coordinator collects what combination works and sends GC_STUN_CONNECT to server/client.
    • a) Server/client connect, client sends CLIENT_CONNECTED to Game Coordinator.
    • b) Server/client connect fails, both send SERCLI_CONNECT_FAILED to Game Coordinator.
    • Game Coordinator tries other combination if available. 3) TURN?
    • Game Coordinator sends GC_TURN_CONNECT to server/client.
    • a) Server/client connect, client sends CLIENT_CONNECTED to Game Coordinator.
    • b) Server/client connect fails, both send SERCLI_CONNECT_FAILED to Game Coordinator.
  • If all fails, Game Coordinator sends GC_CONNECT_FAILED to indicate no connection is possible. Class for handling the client side of the Game Coordinator connection.

Definition at line 53 of file network_coordinator.h.

Member Function Documentation

◆ CloseConnection()

NetworkRecvStatus ClientNetworkCoordinatorSocketHandler::CloseConnection ( bool  error = true)
overridevirtual

This will put this socket handler in a close state.

It will not actually close the OS socket; use CloseSocket for this.

Parameters
errorWhether we quit under an error condition or not.
Returns
new status of the connection.

Reimplemented from NetworkTCPSocketHandler.

Definition at line 432 of file network_coordinator.cpp.

References _network_server_connection_type, CloseAllConnections(), NetworkTCPSocketHandler::CloseConnection(), NetworkTCPSocketHandler::CloseSocket(), connecting, CONNECTION_TYPE_UNKNOWN, NETWORK_RECV_STATUS_OKAY, next_update, SetWindowDirty(), and WC_CLIENT_LIST.

Referenced by NetworkClose(), NetworkCoordinatorConnecter::OnFailure(), Receive_GC_CONNECTING(), and Receive_GC_ERROR().

◆ CloseStunHandler()

void ClientNetworkCoordinatorSocketHandler::CloseStunHandler ( const std::string &  token,
uint8_t  family = AF_UNSPEC 
)

Close the STUN handler.

Parameters
tokenThe token used for the STUN handlers.
familyThe family of STUN handlers to close. AF_UNSPEC to close all STUN handlers for this token.

Definition at line 622 of file network_coordinator.cpp.

References stun_handlers.

Referenced by CloseAllConnections(), CloseToken(), and NetworkReuseStunConnecter::OnFailure().

◆ CloseToken()

void ClientNetworkCoordinatorSocketHandler::CloseToken ( const std::string &  token)

Close everything related to this connection token.

Parameters
tokenThe connection token to close.

Definition at line 668 of file network_coordinator.cpp.

References CloseStunHandler(), CloseTurnHandler(), and connecter.

Referenced by ConnectSuccess(), and Receive_GC_CONNECT_FAILED().

◆ CloseTurnHandler()

void ClientNetworkCoordinatorSocketHandler::CloseTurnHandler ( const std::string &  token)

Close the TURN handler.

Parameters
tokenThe token used for the TURN handler.

Definition at line 649 of file network_coordinator.cpp.

References CloseWindowByClass(), NRWCD_HANDLED, turn_handlers, and WC_NETWORK_ASK_RELAY.

Referenced by CloseAllConnections(), and CloseToken().

◆ ConnectFailure()

void ClientNetworkCoordinatorSocketHandler::ConnectFailure ( const std::string &  token,
uint8_t  tracking_number 
)

Callback from a Connecter to let the Game Coordinator know the connection failed.

Parameters
tokenToken of the connecter that failed.
tracking_numberTracking number of the connecter that failed.

Definition at line 545 of file network_coordinator.cpp.

References game_connecter, NETWORK_COORDINATOR_VERSION, PACKET_COORDINATOR_SERCLI_CONNECT_FAILED, and NetworkTCPSocketHandler::SendPacket().

Referenced by NetworkAskRelayWindow::Close(), CloseAllConnections(), NetworkDirectConnecter::OnFailure(), NetworkReuseStunConnecter::OnFailure(), and Receive_GC_TURN_CONNECT().

◆ ConnectSuccess()

void ClientNetworkCoordinatorSocketHandler::ConnectSuccess ( const std::string &  token,
SOCKET  sock,
NetworkAddress address 
)

Callback from a Connecter to let the Game Coordinator know the connection to the game server is established.

Parameters
tokenToken of the connecter that succeeded.
sockThe socket that the connecter can now use.

Definition at line 567 of file network_coordinator.cpp.

References _frame_counter, _network_server, ServerNetworkGameSocketHandler::AcceptConnection(), CloseToken(), connecter, Debug, game_connecter, NetworkAddress::GetHostname(), ServerNetworkGameSocketHandler::GetName(), NETWORK_COORDINATOR_VERSION, PACKET_COORDINATOR_CLIENT_CONNECTED, NetworkTCPSocketHandler::SendPacket(), and NetworkTCPSocketHandler::sock.

Referenced by ClientNetworkTurnSocketHandler::Receive_TURN_CONNECTED().

◆ ConnectToServer()

void ClientNetworkCoordinatorSocketHandler::ConnectToServer ( const std::string &  invite_code,
TCPServerConnecter connecter 
)

Join a server based on an invite code.

Parameters
invite_codeThe invite code of the server to connect to.
connecterThe connecter of the request.

Definition at line 515 of file network_coordinator.cpp.

References connecter, connecter_pre, NETWORK_COORDINATOR_VERSION, PACKET_COORDINATOR_CLIENT_CONNECT, and NetworkTCPSocketHandler::SendPacket().

◆ Receive_GC_CONNECT_FAILED()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_CONNECT_FAILED ( Packet p)
overrideprotectedvirtual

Game Coordinator informs the Client that it failed to find a way to connect the Client to the Server.

Any open connections for this token should be closed now.

string Token to track the current connect request.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 288 of file network_coordinator.cpp.

References CloseToken(), NETWORK_TOKEN_LENGTH, and Packet::Recv_string().

◆ Receive_GC_CONNECTING()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_CONNECTING ( Packet p)
overrideprotectedvirtual

Game Coordinator informs the Client under what token it will start the attempt to connect the Server and Client together.

string Token to track the current connect request. string Invite code of the Server to join.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 269 of file network_coordinator.cpp.

References CloseConnection(), connecter, connecter_pre, NETWORK_INVITE_CODE_LENGTH, NETWORK_TOKEN_LENGTH, and Packet::Recv_string().

◆ Receive_GC_DIRECT_CONNECT()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT ( Packet p)
overrideprotectedvirtual

Game Coordinator requests that the Client makes a direct connection to the indicated peer, which is a Server.

string Token to track the current connect request. uint8_t Tracking number to track current connect request. string Hostname of the peer. uint16_t Port of the peer.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 296 of file network_coordinator.cpp.

References NETWORK_HOSTNAME_LENGTH, NETWORK_TOKEN_LENGTH, Packet::Recv_string(), and Packet::Recv_uint8().

◆ Receive_GC_ERROR()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_ERROR ( Packet p)
overrideprotectedvirtual

Game Coordinator indicates there was an error.

This can either be a permanent error causing the connection to be dropped, or in response to a request that is invalid.

uint8_t Type of error (see NetworkCoordinatorErrorType). string Details of the error.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 127 of file network_coordinator.cpp.

References _settings_client, CloseConnection(), INVALID_STRING_ID, ClientSettings::network, NETWORK_COORDINATOR_ERROR_REGISTRATION_FAILED, NETWORK_COORDINATOR_ERROR_UNKNOWN, NETWORK_ERROR_DETAIL_LENGTH, Packet::Recv_string(), Packet::Recv_uint8(), NetworkSettings::server_game_type, ShowErrorMessage(), and WL_ERROR.

◆ Receive_GC_LISTING()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_LISTING ( Packet p)
overrideprotectedvirtual

Game Coordinator replies with a list of all public servers.

Multiple of these packets are received after a request till all servers are sent over. Last packet will have server count of 0.

uint16_t Amount of public servers in this packet. For each server: string Connection string for this server. Serialized NetworkGameInfo. See game_info.hpp for details.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 233 of file network_coordinator.cpp.

References _network_game_list_version, ClearGRFConfigList(), NetworkServerGameInfo::grfconfig, NetworkGameList::info, NETWORK_HOSTNAME_PORT_LENGTH, NetworkGameListAddItem(), NetworkGameListRemoveExpired(), newgrf_lookup_table, NGLS_ONLINE, Packet::Recv_string(), Packet::Recv_uint16(), NetworkGameList::status, UpdateNetworkGameWindow(), and NetworkGameList::version.

◆ Receive_GC_NEWGRF_LOOKUP()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_NEWGRF_LOOKUP ( Packet p)
overrideprotectedvirtual

Game Coordinator informs the client of updates for the NewGRFs lookup table as used by the NewGRF deserialization in GC_LISTING.

This packet is sent after a CLIENT_LISTING request, but before GC_LISTING.

uint32_t Lookup table cursor. uint16_t Number of NewGRFs in the packet, with for each of the NewGRFs: uint32_t Lookup table index for the NewGRF. uint32_t Unique NewGRF ID. uint8_t[16] MD5 checksum of the NewGRF string Name of the NewGRF.

The lookup table built using these packets are used by the deserialisation of the NewGRFs for servers in the GC_LISTING. These updates are additive, i.e. each update will add NewGRFs but never remove them. However, this lookup table is specific to the connection with the Game Coordinator, and should be considered invalid after disconnecting from the Game Coordinator.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 356 of file network_coordinator.cpp.

References newgrf_lookup_table_cursor, and Packet::Recv_uint32().

◆ Receive_GC_REGISTER_ACK()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_REGISTER_ACK ( Packet p)
overrideprotectedvirtual

Game Coordinator acknowledges the registration.

string Invite code that can be used to join this server. string Secret that belongs to the invite code (only needed if reusing the invite code on next SERVER_REGISTER). uint8_t Type of connection was detected (see ConnectionType).

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 177 of file network_coordinator.cpp.

References _network_dedicated, _network_server_connection_type, _network_server_invite_code, _settings_client, CONNECTION_TYPE_DIRECT, CONNECTION_TYPE_ISOLATED, CONNECTION_TYPE_STUN, CONNECTION_TYPE_TURN, CONNECTION_TYPE_UNKNOWN, ClientSettings::network, NETWORK_INVITE_CODE_LENGTH, NETWORK_INVITE_CODE_SECRET_LENGTH, next_update, Packet::Recv_string(), Packet::Recv_uint8(), NetworkSettings::server_game_type, NetworkSettings::server_invite_code, NetworkSettings::server_invite_code_secret, SetWindowDirty(), ShowErrorMessage(), WC_CLIENT_LIST, and WL_ERROR.

◆ Receive_GC_STUN_CONNECT()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT ( Packet p)
overrideprotectedvirtual

Game Coordinator informs the client/server of its STUN peer (the host:ip of the other side).

It should start a connect() to this peer ASAP with the local address as used with the STUN request.

string Token to track the current connect request. uint8_t Tracking number to track current connect request. uint8_t Interface number, as given during STUN request. string Host of the peer. uint16_t Port of the peer.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 322 of file network_coordinator.cpp.

References NETWORK_HOSTNAME_PORT_LENGTH, NETWORK_TOKEN_LENGTH, Packet::Recv_string(), and Packet::Recv_uint8().

◆ Receive_GC_STUN_REQUEST()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_REQUEST ( Packet p)
overrideprotectedvirtual

Game Coordinator requests the client/server to do a STUN request to the STUN server.

Important is to remember the local port these STUN requests are sent from, as this will be needed for later conenctions too. The client/server should do multiple STUN requests for every available interface that connects to the Internet (e.g., once for IPv4 and once for IPv6).

string Token to track the current connect request.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 313 of file network_coordinator.cpp.

References NETWORK_TOKEN_LENGTH, Packet::Recv_string(), ClientNetworkStunSocketHandler::Stun(), and stun_handlers.

◆ Receive_GC_TURN_CONNECT()

bool ClientNetworkCoordinatorSocketHandler::Receive_GC_TURN_CONNECT ( Packet p)
overrideprotectedvirtual

Game Coordinator requests that we make a connection to the indicated peer, which is a TURN server.

string Token to track the current connect request. uint8_t Tracking number to track current connect request. string Ticket to hand over to the TURN server. string Connection string of the TURN server.

Parameters
pThe packet that was just received.
Returns
True upon success, otherwise false.

Reimplemented from NetworkCoordinatorSocketHandler.

Definition at line 368 of file network_coordinator.cpp.

References _network_server, _settings_client, connecter, ConnectFailure(), game_connecter, ClientSettings::network, NETWORK_HOSTNAME_PORT_LENGTH, NETWORK_TOKEN_LENGTH, Packet::Recv_string(), Packet::Recv_uint8(), ClientNetworkTurnSocketHandler::Turn(), turn_handlers, and NetworkSettings::use_relay_service.

◆ StunResult()

void ClientNetworkCoordinatorSocketHandler::StunResult ( const std::string &  token,
uint8_t  family,
bool  result 
)

Callback from the STUN connecter to inform the Game Coordinator about the result of the STUN.

This helps the Game Coordinator not to wait for a timeout on its end, but rather react as soon as the client/server knows the result.

Definition at line 607 of file network_coordinator.cpp.

Referenced by NetworkStunConnecter::OnFailure(), and ClientNetworkStunSocketHandler::SendReceive().


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