2009-08-21 20:21:05 +00: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/>.
*/
2008-05-06 15:11:33 +00:00
/** @file network_server.h Server part of the network protocol. */
2004-12-04 17:54:56 +00:00
# ifndef NETWORK_SERVER_H
# define NETWORK_SERVER_H
2010-01-15 16:41:15 +00:00
# include "network_internal.h"
2010-10-15 21:56:06 +00:00
# include "core/tcp_listen.h"
2010-01-15 16:41:15 +00:00
2010-10-15 19:58:56 +00:00
class ServerNetworkGameSocketHandler ;
2013-01-08 22:46:42 +00:00
/** Make the code look slightly nicer/simpler. */
2010-10-15 19:58:56 +00:00
typedef ServerNetworkGameSocketHandler NetworkClientSocket ;
2011-05-05 16:24:48 +00:00
/** Pool with all client sockets. */
2011-02-19 23:05:47 +00:00
typedef Pool < NetworkClientSocket , ClientIndex , 8 , MAX_CLIENT_SLOTS , PT_NCLIENT > NetworkClientSocketPool ;
2010-10-15 19:58:56 +00:00
extern NetworkClientSocketPool _networkclientsocket_pool ;
2010-10-15 13:22:00 +00:00
/** Class for handling the server side of the game connection. */
2010-10-15 21:56:06 +00:00
class ServerNetworkGameSocketHandler : public NetworkClientSocketPool : : PoolItem < & _networkclientsocket_pool > , public NetworkGameSocketHandler , public TCPListenHandler < ServerNetworkGameSocketHandler , PACKET_SERVER_FULL , PACKET_SERVER_BANNED > {
2023-06-15 20:32:15 +00:00
NetworkGameKeys intl_keys ;
2024-01-07 16:41:53 +00:00
uint64_t min_key_message_id = 0 ;
2023-06-15 20:32:15 +00:00
byte * rcon_reply_key = nullptr ;
2023-06-12 21:41:09 +00:00
2010-10-15 13:47:37 +00:00
protected :
2024-02-03 19:28:52 +00:00
NetworkRecvStatus Receive_CLIENT_JOIN ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_GAME_INFO ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD ( Packet & p ) override ;
2024-02-19 17:57:05 +00:00
NetworkRecvStatus Receive_CLIENT_SETTINGS_PASSWORD ( Packet & p ) override ;
2024-02-03 19:28:52 +00:00
NetworkRecvStatus Receive_CLIENT_GETMAP ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_MAP_OK ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_ACK ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_COMMAND ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_CHAT ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_SET_PASSWORD ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_SET_NAME ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_QUIT ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_ERROR ( Packet & p ) override ;
2024-02-19 17:57:05 +00:00
NetworkRecvStatus Receive_CLIENT_DESYNC_LOG ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_DESYNC_MSG ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_DESYNC_SYNC_DATA ( Packet & p ) override ;
2024-02-03 19:28:52 +00:00
NetworkRecvStatus Receive_CLIENT_RCON ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED ( Packet & p ) override ;
NetworkRecvStatus Receive_CLIENT_MOVE ( Packet & p ) override ;
2010-10-15 20:25:07 +00:00
2021-04-26 18:02:27 +00:00
NetworkRecvStatus SendGameInfo ( ) ;
2024-01-07 16:41:53 +00:00
NetworkRecvStatus SendGameInfoExtended ( PacketGameType reply_type , uint16_t flags , uint16_t version ) ;
2010-10-15 20:25:07 +00:00
NetworkRecvStatus SendNewGRFCheck ( ) ;
NetworkRecvStatus SendWelcome ( ) ;
NetworkRecvStatus SendNeedGamePassword ( ) ;
NetworkRecvStatus SendNeedCompanyPassword ( ) ;
2010-10-15 20:29:59 +00:00
2024-02-19 17:57:05 +00:00
bool ParseKeyPasswordPacket ( Packet & p , NetworkSharedSecrets & ss , const std : : string & password , std : : string * payload , size_t length ) ;
2023-06-15 20:32:15 +00:00
2010-10-15 13:22:00 +00:00
public :
2010-10-24 20:07:32 +00:00
/** Status of a client */
enum ClientStatus {
STATUS_INACTIVE , ///< The client is not connected nor active.
STATUS_NEWGRFS_CHECK , ///< The client is checking NewGRFs.
STATUS_AUTH_GAME , ///< The client is authorizing with game (server) password.
STATUS_AUTH_COMPANY , ///< The client is authorizing with company password.
STATUS_AUTHORIZED , ///< The client is authorized.
STATUS_MAP_WAIT , ///< The client is waiting as someone else is downloading the map.
STATUS_MAP , ///< The client is downloading the map.
STATUS_DONE_MAP , ///< The client has downloaded the map.
STATUS_PRE_ACTIVE , ///< The client is catching up the delayed frames.
STATUS_ACTIVE , ///< The client is active within in the game.
2019-08-21 18:15:02 +00:00
STATUS_CLOSE_PENDING , ///< The client connection is pending closure.
2011-12-19 17:48:04 +00:00
STATUS_END , ///< Must ALWAYS be on the end of this list!! (period).
2010-10-24 20:07:32 +00:00
} ;
2021-05-21 00:40:00 +00:00
static const char * GetClientStatusName ( ClientStatus status ) ;
2010-10-15 20:29:59 +00:00
byte lag_test ; ///< Byte used for lag-testing the client
2010-11-30 14:18:20 +00:00
byte last_token ; ///< The last random token we did send to verify the client is listening
2024-01-07 16:41:53 +00:00
uint32_t last_token_frame ; ///< The last frame we received the right token
2010-10-15 20:29:59 +00:00
ClientStatus status ; ///< Status of this client
2024-02-04 16:02:08 +00:00
CommandQueue outgoing_queue ; ///< The command-queue awaiting delivery; conceptually more a bucket to gather commands in, after which the whole bucket is sent to the client.
2021-04-18 10:29:34 +00:00
size_t receive_limit ; ///< Amount of bytes that we can receive at this moment
2019-08-20 19:42:17 +00:00
bool settings_authed = false ; ///< Authorised to control all game settings
2021-03-01 18:22:21 +00:00
bool supports_zstd = false ; ///< Client supports zstd compression
2010-10-15 20:29:59 +00:00
2024-02-02 17:13:38 +00:00
std : : shared_ptr < struct PacketWriter > savegame ; ///< Writer used to write the savegame.
2021-05-08 10:02:30 +00:00
NetworkAddress client_address ; ///< IP-address of the client (so they can be banned)
2010-12-05 14:45:52 +00:00
2019-05-21 17:58:13 +00:00
std : : string desync_log ;
2023-08-28 14:17:53 +00:00
std : : string desync_frame_info ;
2022-11-13 01:07:53 +00:00
2023-06-14 20:58:35 +00:00
uint rcon_auth_failures = 0 ;
uint settings_auth_failures = 0 ;
2010-10-15 13:22:00 +00:00
ServerNetworkGameSocketHandler ( SOCKET s ) ;
~ ServerNetworkGameSocketHandler ( ) ;
2010-10-15 18:42:52 +00:00
2023-11-20 22:26:57 +00:00
std : : unique_ptr < Packet > ReceivePacket ( ) override ;
2019-03-24 16:24:06 +00:00
NetworkRecvStatus CloseConnection ( NetworkRecvStatus status ) override ;
2014-04-23 20:44:42 +00:00
void GetClientName ( char * client_name , const char * last ) const ;
2010-10-15 13:22:00 +00:00
2021-02-27 09:50:41 +00:00
void CheckNextClientToSendMap ( NetworkClientSocket * ignore_cs = nullptr ) ;
2021-02-27 09:54:16 +00:00
NetworkRecvStatus SendWait ( ) ;
2010-10-15 20:25:07 +00:00
NetworkRecvStatus SendMap ( ) ;
NetworkRecvStatus SendErrorQuit ( ClientID client_id , NetworkErrorCode errorno ) ;
NetworkRecvStatus SendQuit ( ClientID client_id ) ;
NetworkRecvStatus SendShutdown ( ) ;
NetworkRecvStatus SendNewGame ( ) ;
2024-01-07 16:41:53 +00:00
NetworkRecvStatus SendRConResult ( uint16_t colour , const std : : string & command ) ;
2023-06-15 20:32:15 +00:00
NetworkRecvStatus SendRConDenied ( ) ;
2010-10-15 20:25:07 +00:00
NetworkRecvStatus SendMove ( ClientID client_id , CompanyID company_id ) ;
NetworkRecvStatus SendClientInfo ( NetworkClientInfo * ci ) ;
2021-05-29 17:47:58 +00:00
NetworkRecvStatus SendError ( NetworkErrorCode error , const std : : string & reason = { } ) ;
2019-08-21 18:15:02 +00:00
NetworkRecvStatus SendDesyncLog ( const std : : string & log ) ;
2021-09-27 21:47:13 +00:00
NetworkRecvStatus SendChat ( NetworkAction action , ClientID client_id , bool self_send , const std : : string & msg , NetworkTextMessageData data ) ;
2021-09-19 21:09:06 +00:00
NetworkRecvStatus SendExternalChat ( const std : : string & source , TextColour colour , const std : : string & user , const std : : string & msg ) ;
2010-10-15 20:25:07 +00:00
NetworkRecvStatus SendJoin ( ClientID client_id ) ;
NetworkRecvStatus SendFrame ( ) ;
NetworkRecvStatus SendSync ( ) ;
2024-02-04 16:20:25 +00:00
NetworkRecvStatus SendCommand ( const CommandPacket & cp ) ;
2010-10-15 20:25:07 +00:00
NetworkRecvStatus SendCompanyUpdate ( ) ;
NetworkRecvStatus SendConfigUpdate ( ) ;
2019-08-20 19:42:17 +00:00
NetworkRecvStatus SendSettingsAccessUpdate ( bool ok ) ;
2010-10-15 21:56:06 +00:00
2023-06-14 20:58:35 +00:00
NetworkRecvStatus HandleAuthFailure ( uint & failure_count ) ;
2021-05-14 18:34:15 +00:00
std : : string GetDebugInfo ( ) const override ;
2023-06-15 20:32:15 +00:00
const NetworkGameKeys & GetKeys ( )
{
if ( ! this - > intl_keys . inited ) this - > intl_keys . Initialise ( ) ;
return this - > intl_keys ;
}
2010-10-15 21:56:06 +00:00
static void Send ( ) ;
static void AcceptConnection ( SOCKET s , const NetworkAddress & address ) ;
static bool AllowConnection ( ) ;
/**
* Get the name used by the listener .
* @ return the name to show in debug logs and the like .
*/
static const char * GetName ( )
{
return " server " ;
}
2011-04-22 15:54:42 +00:00
2011-04-22 16:02:21 +00:00
const char * GetClientIP ( ) ;
2011-04-22 15:54:42 +00:00
static ServerNetworkGameSocketHandler * GetByClientID ( ClientID client_id ) ;
2010-10-15 20:25:07 +00:00
} ;
2004-12-04 17:54:56 +00:00
2005-07-29 21:55:49 +00:00
void NetworkServer_Tick ( bool send_frame ) ;
2024-01-26 15:25:25 +00:00
void ChangeNetworkRestartTime ( bool reset ) ;
2021-05-02 07:07:09 +00:00
void NetworkServerSetCompanyPassword ( CompanyID company_id , const std : : string & password , bool already_hashed = true ) ;
2014-05-11 12:52:21 +00:00
void NetworkServerUpdateCompanyPassworded ( CompanyID company_id , bool passworded ) ;
2005-10-19 19:38:35 +00:00
2005-09-18 20:56:44 +00:00
# endif /* NETWORK_SERVER_H */