OpenTTD Source  20241108-master-g80f628063a
NetworkCoordinatorSocketHandler Class Reference

Base socket handler for all Game Coordinator TCP sockets. More...

#include <tcp_coordinator.h>

Inheritance diagram for NetworkCoordinatorSocketHandler:
NetworkTCPSocketHandler NetworkSocketHandler ClientNetworkCoordinatorSocketHandler

Public Member Functions

 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...
 
virtual NetworkRecvStatus CloseConnection (bool error=true)
 This will put this socket handler in a close state. 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.
 

Protected Member Functions

bool ReceiveInvalidPacket (PacketCoordinatorType type)
 Helper for logging receiving invalid packets. More...
 
virtual bool Receive_GC_ERROR (Packet &p)
 Game Coordinator indicates there was an error. 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_GC_REGISTER_ACK (Packet &p)
 Game Coordinator acknowledges the registration. 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_GC_LISTING (Packet &p)
 Game Coordinator replies with 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_GC_CONNECTING (Packet &p)
 Game Coordinator informs the Client under what token it will start the attempt to connect the Server and Client together. More...
 
virtual bool Receive_SERCLI_CONNECT_FAILED (Packet &p)
 Client or Server failed to connect to the remote side. More...
 
virtual bool Receive_GC_CONNECT_FAILED (Packet &p)
 Game Coordinator informs the Client that it failed to find a way to connect the Client to the Server. More...
 
virtual bool Receive_CLIENT_CONNECTED (Packet &p)
 Client informs the Game Coordinator the connection with the Server is established. More...
 
virtual bool Receive_GC_DIRECT_CONNECT (Packet &p)
 Game Coordinator requests that the Client makes a direct connection to the indicated peer, which is a Server. More...
 
virtual bool Receive_GC_STUN_REQUEST (Packet &p)
 Game Coordinator requests the client/server to do a STUN request to the STUN server. More...
 
virtual bool Receive_SERCLI_STUN_RESULT (Packet &p)
 Client/server informs the Game Coordinator the result of a STUN request. More...
 
virtual bool Receive_GC_STUN_CONNECT (Packet &p)
 Game Coordinator informs the client/server of its STUN peer (the host:ip of the other side). More...
 
virtual bool Receive_GC_NEWGRF_LOOKUP (Packet &p)
 Game Coordinator informs the client of updates for the NewGRFs lookup table as used by the NewGRF deserialization in GC_LISTING. More...
 
virtual bool Receive_GC_TURN_CONNECT (Packet &p)
 Game Coordinator requests that we make a connection to the indicated peer, which is a TURN server. More...
 
bool HandlePacket (Packet &p)
 Handle the given packet, i.e. More...
 

Additional Inherited Members

- Data Fields inherited from NetworkTCPSocketHandler
SOCKET sock
 The socket currently connected to.
 
bool writable
 Can we write to this socket?
 
- 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

Base socket handler for all Game Coordinator TCP sockets.

Definition at line 71 of file tcp_coordinator.h.

Constructor & Destructor Documentation

◆ NetworkCoordinatorSocketHandler()

NetworkCoordinatorSocketHandler::NetworkCoordinatorSocketHandler ( SOCKET  s = INVALID_SOCKET)
inline

Create a new cs socket handler for a given cs.

Parameters
sThe socket we are connected with.

Definition at line 314 of file tcp_coordinator.h.

Member Function Documentation

◆ HandlePacket()

◆ Receive_CLIENT_CONNECT()

bool NetworkCoordinatorSocketHandler::Receive_CLIENT_CONNECT ( Packet p)
protectedvirtual

Client wants to connect to a Server.

uint8_t Game Coordinator protocol version. string Invite code of the Server to join.

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

Definition at line 95 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_CLIENT_CONNECT, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_CLIENT_CONNECTED()

bool NetworkCoordinatorSocketHandler::Receive_CLIENT_CONNECTED ( Packet p)
protectedvirtual

Client informs the Game Coordinator the connection with the Server is established.

The Client will disconnect from the Game Coordinator next.

uint8_t Game Coordinator protocol version. string Token to track the current connect request.

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

Definition at line 99 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_CLIENT_CONNECTED, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_CLIENT_LISTING()

bool NetworkCoordinatorSocketHandler::Receive_CLIENT_LISTING ( Packet p)
protectedvirtual

Client requests a list of all public servers.

uint8_t Game Coordinator protocol version. uint8_t Game-info version used by this client. string Revision of the client. uint32_t (Game Coordinator protocol >= 4) Cursor as received from GC_NEWGRF_LOOKUP, or zero.

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

Definition at line 93 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_CLIENT_LISTING, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_CONNECT_FAILED()

bool NetworkCoordinatorSocketHandler::Receive_GC_CONNECT_FAILED ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 98 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_CONNECT_FAILED, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_CONNECTING()

bool NetworkCoordinatorSocketHandler::Receive_GC_CONNECTING ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 96 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_CONNECTING, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_DIRECT_CONNECT()

bool NetworkCoordinatorSocketHandler::Receive_GC_DIRECT_CONNECT ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 100 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_DIRECT_CONNECT, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_ERROR()

bool NetworkCoordinatorSocketHandler::Receive_GC_ERROR ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 89 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_ERROR, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_LISTING()

bool NetworkCoordinatorSocketHandler::Receive_GC_LISTING ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 94 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_LISTING, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_NEWGRF_LOOKUP()

bool NetworkCoordinatorSocketHandler::Receive_GC_NEWGRF_LOOKUP ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 104 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_NEWGRF_LOOKUP, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_REGISTER_ACK()

bool NetworkCoordinatorSocketHandler::Receive_GC_REGISTER_ACK ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 91 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_REGISTER_ACK, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_STUN_CONNECT()

bool NetworkCoordinatorSocketHandler::Receive_GC_STUN_CONNECT ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 103 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_STUN_CONNECT, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_STUN_REQUEST()

bool NetworkCoordinatorSocketHandler::Receive_GC_STUN_REQUEST ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 101 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_STUN_REQUEST, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_GC_TURN_CONNECT()

bool NetworkCoordinatorSocketHandler::Receive_GC_TURN_CONNECT ( Packet p)
protectedvirtual

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 in ClientNetworkCoordinatorSocketHandler.

Definition at line 105 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_GC_TURN_CONNECT, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_SERCLI_CONNECT_FAILED()

bool NetworkCoordinatorSocketHandler::Receive_SERCLI_CONNECT_FAILED ( Packet p)
protectedvirtual

Client or Server failed to connect to the remote side.

uint8_t Game Coordinator protocol version. string Token to track the current connect request. uint8_t Tracking number to track current connect request.

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

Definition at line 97 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_SERCLI_CONNECT_FAILED, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_SERCLI_STUN_RESULT()

bool NetworkCoordinatorSocketHandler::Receive_SERCLI_STUN_RESULT ( Packet p)
protectedvirtual

Client/server informs the Game Coordinator the result of a STUN request.

uint8_t Game Coordinator protocol version. string Token to track the current connect request. uint8_t Interface number, as given during STUN request. bool Whether the STUN connection was successful.

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

Definition at line 102 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_SERCLI_STUN_RESULT, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_SERVER_REGISTER()

bool NetworkCoordinatorSocketHandler::Receive_SERVER_REGISTER ( Packet p)
protectedvirtual

Server is starting a multiplayer game and wants to let the Game Coordinator know.

uint8_t Game Coordinator protocol version. uint8_t Type of game (see ServerGameType). uint16_t Local port of the server. string Invite code the server wants to use (can be empty; coordinator will assign a new invite code). string Secret that belongs to the invite code (empty if invite code is empty).

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

Definition at line 90 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_SERVER_REGISTER, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ Receive_SERVER_UPDATE()

bool NetworkCoordinatorSocketHandler::Receive_SERVER_UPDATE ( Packet p)
protectedvirtual

Send an update of the current state of the server to the Game Coordinator.

uint8_t Game Coordinator protocol version. Serialized NetworkGameInfo. See game_info.hpp for details.

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

Definition at line 92 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_SERVER_UPDATE, and ReceiveInvalidPacket().

Referenced by HandlePacket().

◆ ReceiveInvalidPacket()

◆ ReceivePackets()

bool NetworkCoordinatorSocketHandler::ReceivePackets ( )

Receive a packet at TCP level.

Returns
Whether at least one packet was received.

Definition at line 58 of file tcp_coordinator.cpp.

References HandlePacket(), and NetworkTCPSocketHandler::ReceivePacket().


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