OpenTTD Source
20241108-master-g80f628063a
|
Game Coordinator communication. More...
#include <network_coordinator.h>
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< 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 ()=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< TCPConnecter > | game_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 NetworkEncryptionHandler > | receive_encryption_handler |
The handler for decrypting received packets. | |
std::unique_ptr< class NetworkEncryptionHandler > | send_encryption_handler |
The handler for encrypting sent packets. | |
Game Coordinator communication.
For more detail about what the Game Coordinator does, please see docs/game_coordinator.md.
For servers:
For clients (listing):
For clients (connecting):
Definition at line 53 of file network_coordinator.h.
|
overridevirtual |
This will put this socket handler in a close state.
It will not actually close the OS socket; use CloseSocket for this.
error | Whether we quit under an error condition or not. |
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 NetworkCoordinatorConnecter::OnFailure(), Receive_GC_CONNECTING(), and Receive_GC_ERROR().
void ClientNetworkCoordinatorSocketHandler::CloseStunHandler | ( | const std::string & | token, |
uint8_t | family = AF_UNSPEC |
||
) |
Close the STUN handler.
token | The token used for the STUN handlers. |
family | The 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().
void ClientNetworkCoordinatorSocketHandler::CloseToken | ( | const std::string & | token | ) |
Close everything related to this connection token.
token | The 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().
void ClientNetworkCoordinatorSocketHandler::CloseTurnHandler | ( | const std::string & | token | ) |
Close the TURN handler.
token | The 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().
void ClientNetworkCoordinatorSocketHandler::ConnectFailure | ( | const std::string & | token, |
uint8_t | tracking_number | ||
) |
Callback from a Connecter to let the Game Coordinator know the connection failed.
token | Token of the connecter that failed. |
tracking_number | Tracking 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().
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.
token | Token of the connecter that succeeded. |
sock | The 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().
void ClientNetworkCoordinatorSocketHandler::ConnectToServer | ( | const std::string & | invite_code, |
TCPServerConnecter * | connecter | ||
) |
Join a server based on an invite code.
invite_code | The invite code of the server to connect to. |
connecter | The 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().
|
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.
p | The packet that was just received. |
Reimplemented from NetworkCoordinatorSocketHandler.
Definition at line 288 of file network_coordinator.cpp.
References CloseToken(), NETWORK_TOKEN_LENGTH, and Packet::Recv_string().
|
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.
p | The packet that was just received. |
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().
|
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.
p | The packet that was just received. |
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().
|
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.
p | The packet that was just received. |
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.
|
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.
p | The packet that was just received. |
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.
|
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.
p | The packet that was just received. |
Reimplemented from NetworkCoordinatorSocketHandler.
Definition at line 356 of file network_coordinator.cpp.
References newgrf_lookup_table_cursor, and Packet::Recv_uint32().
|
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).
p | The packet that was just received. |
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.
|
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.
p | The packet that was just received. |
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().
|
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.
p | The packet that was just received. |
Reimplemented from NetworkCoordinatorSocketHandler.
Definition at line 313 of file network_coordinator.cpp.
References NETWORK_TOKEN_LENGTH, Packet::Recv_string(), ClientNetworkStunSocketHandler::Stun(), and stun_handlers.
|
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.
p | The packet that was just received. |
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.
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().