2021-07-03 11:04:32 +02:00
/*
* This file is part of OpenTTD .
* OpenTTD is free software ; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation , version 2.
* OpenTTD is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* See the GNU General Public License for more details . You should have received a copy of the GNU General Public License along with OpenTTD . If not , see < http : //www.gnu.org/licenses/>.
*/
/** @file network_coordinator.h Part of the network protocol handling Game Coordinator requests. */
# ifndef NETWORK_COORDINATOR_H
# define NETWORK_COORDINATOR_H
# include "core/tcp_coordinator.h"
2021-04-27 11:51:00 +02:00
# include "network_stun.h"
2021-05-05 10:47:01 +02:00
# include "network_turn.h"
2021-07-03 11:04:32 +02:00
/**
* Game Coordinator communication .
2021-07-15 19:43:01 +02:00
* For more detail about what the Game Coordinator does , please see
* docs / game_coordinator . md .
2021-07-03 11:04:32 +02:00
*
* 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 .
2021-07-03 11:12:28 +02:00
*
* For clients ( listing ) :
* - Client sends CLIENT_LISTING .
* - Game Coordinator returns the full list of public servers via GC_LISTING ( multiple packets ) .
2021-04-29 15:37:02 +02:00
*
* 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 .
2021-07-15 19:43:01 +02:00
* 2 ) STUN ?
2021-04-27 11:51:00 +02:00
* - 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 .
2021-05-05 10:47:01 +02:00
* 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 .
2021-04-29 15:37:02 +02:00
* - If all fails , Game Coordinator sends GC_CONNECT_FAILED to indicate no connection is possible .
2021-07-03 11:04:32 +02:00
*/
/** Class for handling the client side of the Game Coordinator connection. */
class ClientNetworkCoordinatorSocketHandler : public NetworkCoordinatorSocketHandler {
private :
std : : chrono : : steady_clock : : time_point next_update ; ///< When to send the next update (if server and public).
2021-08-18 12:06:14 +02:00
std : : map < std : : string , std : : pair < std : : string , TCPServerConnecter * > > connecter ; ///< Based on tokens, the current (invite-code, connecter) that are pending.
2021-04-29 15:37:02 +02:00
std : : map < std : : string , TCPServerConnecter * > connecter_pre ; ///< Based on invite codes, the current connecters that are pending.
2021-04-27 11:51:00 +02:00
std : : map < std : : string , std : : map < int , std : : unique_ptr < ClientNetworkStunSocketHandler > > > stun_handlers ; ///< All pending STUN handlers, stored by token:family.
2021-05-05 10:47:01 +02:00
std : : map < std : : string , std : : unique_ptr < ClientNetworkTurnSocketHandler > > turn_handlers ; ///< Pending TURN handler (if any), stored by token.
2024-01-20 21:04:49 +01:00
std : : shared_ptr < TCPConnecter > game_connecter { } ; ///< Pending connecter to the game server.
2021-07-03 11:04:32 +02:00
2023-05-08 19:01:06 +02:00
uint32_t newgrf_lookup_table_cursor = 0 ; ///< Last received cursor for the #GameInfoNewGRFLookupTable updates.
2021-07-17 23:42:43 +02:00
GameInfoNewGRFLookupTable newgrf_lookup_table ; ///< Table to look up NewGRFs in the GC_LISTING packets.
2021-07-03 11:04:32 +02:00
protected :
2024-02-03 20:28:52 +01:00
bool Receive_GC_ERROR ( Packet & p ) override ;
bool Receive_GC_REGISTER_ACK ( Packet & p ) override ;
bool Receive_GC_LISTING ( Packet & p ) override ;
bool Receive_GC_CONNECTING ( Packet & p ) override ;
bool Receive_GC_CONNECT_FAILED ( Packet & p ) override ;
bool Receive_GC_DIRECT_CONNECT ( Packet & p ) override ;
bool Receive_GC_STUN_REQUEST ( Packet & p ) override ;
bool Receive_GC_STUN_CONNECT ( Packet & p ) override ;
bool Receive_GC_NEWGRF_LOOKUP ( Packet & p ) override ;
bool Receive_GC_TURN_CONNECT ( Packet & p ) override ;
2021-07-03 11:04:32 +02:00
public :
2021-07-03 11:12:28 +02:00
/** The idle timeout; when to close the connection because it's idle. */
static constexpr std : : chrono : : seconds IDLE_TIMEOUT = std : : chrono : : seconds ( 60 ) ;
std : : chrono : : steady_clock : : time_point last_activity ; ///< The last time there was network activity.
2021-07-03 11:04:32 +02:00
bool connecting ; ///< Are we connecting to the Game Coordinator?
ClientNetworkCoordinatorSocketHandler ( ) : connecting ( false ) { }
NetworkRecvStatus CloseConnection ( bool error = true ) override ;
void SendReceive ( ) ;
2023-05-08 19:01:06 +02:00
void ConnectFailure ( const std : : string & token , uint8_t tracking_number ) ;
2021-04-27 11:51:00 +02:00
void ConnectSuccess ( const std : : string & token , SOCKET sock , NetworkAddress & address ) ;
2023-05-08 19:01:06 +02:00
void StunResult ( const std : : string & token , uint8_t family , bool result ) ;
2021-04-29 15:37:02 +02:00
2021-07-03 11:04:32 +02:00
void Connect ( ) ;
2021-04-29 15:37:02 +02:00
void CloseToken ( const std : : string & token ) ;
2021-07-19 19:32:22 +02:00
void CloseAllConnections ( ) ;
2023-05-08 19:01:06 +02:00
void CloseStunHandler ( const std : : string & token , uint8_t family = AF_UNSPEC ) ;
2021-05-05 10:47:01 +02:00
void CloseTurnHandler ( const std : : string & token ) ;
2021-07-03 11:04:32 +02:00
void Register ( ) ;
void SendServerUpdate ( ) ;
2021-07-03 11:12:28 +02:00
void GetListing ( ) ;
2021-04-29 15:37:02 +02:00
void ConnectToServer ( const std : : string & invite_code , TCPServerConnecter * connecter ) ;
2021-05-05 10:47:01 +02:00
void StartTurnConnection ( std : : string & token ) ;
2021-07-03 11:04:32 +02:00
} ;
extern ClientNetworkCoordinatorSocketHandler _network_coordinator_client ;
# endif /* NETWORK_COORDINATOR_H */