OpenTTD Source: NetworkCoordinatorSocketHandler Class Reference

OpenTTD Source 13.1

Public Member Functions | Protected Member Functions
NetworkCoordinatorSocketHandler Class Reference

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

`#include <tcp_coordinator.h>`

Inheritance diagram for NetworkCoordinatorSocketHandler:

## 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 (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 Packet * | ReceivePacket ()
| 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 ()
| 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?

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

  s | The socket we are connected with.

Definition at line 314 of file tcp_coordinator.h.

## Member Function Documentation

## ◆ HandlePacket()

bool NetworkCoordinatorSocketHandler::HandlePacket | ( | Packet * | p | ) |
| protected

Handle the given packet, i.e.

pass it to the right. parser receive command.

Parameters

  p | The packet to handle.

Returns
  True iff we should immediately handle further packets.

Definition at line 25 of file tcp_coordinator.cpp.

References Debug, PACKET_COORDINATOR_CLIENT_CONNECT, PACKET_COORDINATOR_CLIENT_CONNECTED, PACKET_COORDINATOR_CLIENT_LISTING, PACKET_COORDINATOR_GC_CONNECT_FAILED, PACKET_COORDINATOR_GC_CONNECTING, PACKET_COORDINATOR_GC_DIRECT_CONNECT, PACKET_COORDINATOR_GC_ERROR, PACKET_COORDINATOR_GC_LISTING, PACKET_COORDINATOR_GC_NEWGRF_LOOKUP, PACKET_COORDINATOR_GC_REGISTER_ACK, PACKET_COORDINATOR_GC_STUN_CONNECT, PACKET_COORDINATOR_GC_STUN_REQUEST, PACKET_COORDINATOR_GC_TURN_CONNECT, PACKET_COORDINATOR_SERCLI_CONNECT_FAILED, PACKET_COORDINATOR_SERCLI_STUN_RESULT, PACKET_COORDINATOR_SERVER_REGISTER, PACKET_COORDINATOR_SERVER_UPDATE, Receive_CLIENT_CONNECT(), Receive_CLIENT_CONNECTED(), Receive_CLIENT_LISTING(), Receive_GC_CONNECT_FAILED(), Receive_GC_CONNECTING(), Receive_GC_DIRECT_CONNECT(), Receive_GC_ERROR(), Receive_GC_LISTING(), Receive_GC_NEWGRF_LOOKUP(), Receive_GC_REGISTER_ACK(), Receive_GC_STUN_CONNECT(), Receive_GC_STUN_REQUEST(), Receive_GC_TURN_CONNECT(), Receive_SERCLI_CONNECT_FAILED(), Receive_SERCLI_STUN_RESULT(), Receive_SERVER_REGISTER(), Receive_SERVER_UPDATE(), and Packet::Recv_uint8().

Referenced by ReceivePackets().

## ◆ Receive_CLIENT_CONNECT()

bool NetworkCoordinatorSocketHandler::Receive_CLIENT_CONNECT | ( | Packet * | p | ) |
| protectedvirtual

Client wants to connect to a Server.

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

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Definition at line 96 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 Game Coordinator protocol version. string Token to track the current connect request.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Definition at line 100 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 Game Coordinator protocol version. uint8 Game-info version used by this client. string Revision of the client. uint32 (Game Coordinator protocol >= 4) Cursor as received from GC_NEWGRF_LOOKUP, or zero.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Definition at line 94 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

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented in ClientNetworkCoordinatorSocketHandler.

Definition at line 99 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

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented in ClientNetworkCoordinatorSocketHandler.

Definition at line 97 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 Tracking number to track current connect request. string Hostname of the peer. uint16 Port of the peer.

Parameters

  p | The 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_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 Type of error (see NetworkCoordinatorErrorType). string Details of the error.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented in ClientNetworkCoordinatorSocketHandler.

Definition at line 90 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 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

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented in ClientNetworkCoordinatorSocketHandler.

Definition at line 95 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 Lookup table cursor. uint16 Number of NewGRFs in the packet, with for each of the NewGRFs: uint32 Lookup table index for the NewGRF. uint32 Unique NewGRF ID. byte[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

  p | The 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_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 Type of connection was detected (see ConnectionType).

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented in ClientNetworkCoordinatorSocketHandler.

Definition at line 92 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 Tracking number to track current connect request. uint8 Interface number, as given during STUN request. string Host of the peer. uint16 Port of the peer.

Parameters

  p | The 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_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

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented in ClientNetworkCoordinatorSocketHandler.

Definition at line 102 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 Tracking number to track current connect request. string Ticket to hand over to the TURN server. string Connection string of the TURN server.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Reimplemented in ClientNetworkCoordinatorSocketHandler.

Definition at line 106 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 Game Coordinator protocol version. string Token to track the current connect request. uint8 Tracking number to track current connect request.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Definition at line 98 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 Game Coordinator protocol version. string Token to track the current connect request. uint8 Interface number, as given during STUN request. bool Whether the STUN connection was successful.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Definition at line 103 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 Game Coordinator protocol version. uint8 Type of game (see ServerGameType). uint16 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

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Definition at line 91 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 Game Coordinator protocol version. Serialized NetworkGameInfo. See game_info.hpp for details.

Parameters

  p | The packet that was just received.

Returns
  True upon success, otherwise false.

Definition at line 93 of file tcp_coordinator.cpp.

References PACKET_COORDINATOR_SERVER_UPDATE, and ReceiveInvalidPacket().

Referenced by HandlePacket().

## ◆ ReceiveInvalidPacket()

bool NetworkCoordinatorSocketHandler::ReceiveInvalidPacket | ( | PacketCoordinatorType | type | ) |
| protected

Helper for logging receiving invalid packets.

Parameters

  type | The received packet type.

Returns
  Always false, as it's an error.

Definition at line 84 of file tcp_coordinator.cpp.

References Debug.

Referenced by Receive_CLIENT_CONNECT(), Receive_CLIENT_CONNECTED(), Receive_CLIENT_LISTING(), Receive_GC_CONNECT_FAILED(), Receive_GC_CONNECTING(), Receive_GC_DIRECT_CONNECT(), Receive_GC_ERROR(), Receive_GC_LISTING(), Receive_GC_NEWGRF_LOOKUP(), Receive_GC_REGISTER_ACK(), Receive_GC_STUN_CONNECT(), Receive_GC_STUN_REQUEST(), Receive_GC_TURN_CONNECT(), Receive_SERCLI_CONNECT_FAILED(), Receive_SERCLI_STUN_RESULT(), Receive_SERVER_REGISTER(), and Receive_SERVER_UPDATE().

## ◆ 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:

- src/network/core/tcp_coordinator.h
- src/network/core/tcp_coordinator.cpp