2009-08-21 22:21:05 +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/>.
*/
2008-05-06 17:11:33 +02:00
/** @file network_server.h Server part of the network protocol. */
2004-12-04 18:54:56 +01:00
# ifndef NETWORK_SERVER_H
# define NETWORK_SERVER_H
2010-01-15 17:41:15 +01:00
# include "network_internal.h"
2010-10-15 23:56:06 +02:00
# include "core/tcp_listen.h"
2010-01-15 17:41:15 +01:00
2010-10-15 21:58:56 +02:00
class ServerNetworkGameSocketHandler ;
2013-01-08 23:46:42 +01:00
/** Make the code look slightly nicer/simpler. */
2010-10-15 21:58:56 +02:00
typedef ServerNetworkGameSocketHandler NetworkClientSocket ;
2011-05-05 18:24:48 +02:00
/** Pool with all client sockets. */
2011-02-20 00:05:47 +01:00
typedef Pool < NetworkClientSocket , ClientIndex , 8 , MAX_CLIENT_SLOTS , PT_NCLIENT > NetworkClientSocketPool ;
2010-10-15 21:58:56 +02:00
extern NetworkClientSocketPool _networkclientsocket_pool ;
2010-10-15 15:22:00 +02:00
/** Class for handling the server side of the game connection. */
2010-10-15 23:56:06 +02:00
class ServerNetworkGameSocketHandler : public NetworkClientSocketPool : : PoolItem < & _networkclientsocket_pool > , public NetworkGameSocketHandler , public TCPListenHandler < ServerNetworkGameSocketHandler , PACKET_SERVER_FULL , PACKET_SERVER_BANNED > {
2010-10-15 15:47:37 +02:00
protected :
2024-03-14 21:08:21 +01:00
std : : unique_ptr < class NetworkAuthenticationServerHandler > authentication_handler ; ///< The handler for the authentication.
2024-03-17 11:18:37 +01:00
std : : string peer_public_key ; ///< The public key of our client.
2024-03-14 21:08:21 +01:00
2024-02-03 20:28:52 +01:00
NetworkRecvStatus Receive_CLIENT_JOIN ( Packet & p ) override ;
2024-03-13 21:33:28 +01:00
NetworkRecvStatus Receive_CLIENT_IDENTIFY ( Packet & p ) override ;
2024-02-03 20:28:52 +01:00
NetworkRecvStatus Receive_CLIENT_GAME_INFO ( Packet & p ) override ;
2024-03-14 21:08:21 +01:00
NetworkRecvStatus Receive_CLIENT_AUTH_RESPONSE ( Packet & p ) override ;
2024-02-03 20:28:52 +01:00
NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD ( Packet & p ) override ;
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 ;
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 22:25:07 +02:00
2021-04-26 20:02:27 +02:00
NetworkRecvStatus SendGameInfo ( ) ;
2010-10-15 22:25:07 +02:00
NetworkRecvStatus SendNewGRFCheck ( ) ;
NetworkRecvStatus SendWelcome ( ) ;
2024-03-14 21:08:21 +01:00
NetworkRecvStatus SendAuthRequest ( ) ;
2024-03-14 21:36:28 +01:00
NetworkRecvStatus SendEnableEncryption ( ) ;
2010-10-15 22:25:07 +02:00
NetworkRecvStatus SendNeedCompanyPassword ( ) ;
2010-10-15 22:29:59 +02:00
2010-10-15 15:22:00 +02:00
public :
2010-10-24 22:07:32 +02:00
/** Status of a client */
enum ClientStatus {
STATUS_INACTIVE , ///< The client is not connected nor active.
2024-03-14 21:08:21 +01:00
STATUS_AUTH_GAME , ///< The client is authorizing with game (server) password.
2024-03-13 21:33:28 +01:00
STATUS_IDENTIFY , ///< The client is identifying itself.
2010-10-24 22:07:32 +02:00
STATUS_NEWGRFS_CHECK , ///< The client is checking NewGRFs.
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.
2011-12-19 18:48:04 +01:00
STATUS_END , ///< Must ALWAYS be on the end of this list!! (period).
2010-10-24 22:07:32 +02:00
} ;
2024-03-16 23:59:32 +01:00
uint8_t lag_test ; ///< Byte used for lag-testing the client
uint8_t last_token ; ///< The last random token we did send to verify the client is listening
2023-05-08 19:01:06 +02:00
uint32_t last_token_frame ; ///< The last frame we received the right token
2010-10-15 22:29:59 +02:00
ClientStatus status ; ///< Status of this client
2024-02-04 17:02:08 +01: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 12:29:34 +02:00
size_t receive_limit ; ///< Amount of bytes that we can receive at this moment
2010-10-15 22:29:59 +02:00
2024-02-02 18:13:38 +01:00
std : : shared_ptr < struct PacketWriter > savegame ; ///< Writer used to write the savegame.
2021-05-08 12:02:30 +02:00
NetworkAddress client_address ; ///< IP-address of the client (so they can be banned)
2010-12-05 15:45:52 +01:00
2010-10-15 15:22:00 +02:00
ServerNetworkGameSocketHandler ( SOCKET s ) ;
~ ServerNetworkGameSocketHandler ( ) ;
2010-10-15 20:42:52 +02:00
2024-02-03 19:03:47 +01:00
std : : unique_ptr < Packet > ReceivePacket ( ) override ;
2019-03-24 17:24:06 +01:00
NetworkRecvStatus CloseConnection ( NetworkRecvStatus status ) override ;
2021-06-14 16:09:50 +02:00
std : : string GetClientName ( ) const ;
2010-10-15 15:22:00 +02:00
2021-02-27 10:50:41 +01:00
void CheckNextClientToSendMap ( NetworkClientSocket * ignore_cs = nullptr ) ;
2021-02-27 10:54:16 +01:00
NetworkRecvStatus SendWait ( ) ;
2010-10-15 22:25:07 +02:00
NetworkRecvStatus SendMap ( ) ;
NetworkRecvStatus SendErrorQuit ( ClientID client_id , NetworkErrorCode errorno ) ;
NetworkRecvStatus SendQuit ( ClientID client_id ) ;
NetworkRecvStatus SendShutdown ( ) ;
NetworkRecvStatus SendNewGame ( ) ;
2023-05-08 19:01:06 +02:00
NetworkRecvStatus SendRConResult ( uint16_t colour , const std : : string & command ) ;
2010-10-15 22:25:07 +02:00
NetworkRecvStatus SendMove ( ClientID client_id , CompanyID company_id ) ;
NetworkRecvStatus SendClientInfo ( NetworkClientInfo * ci ) ;
2021-05-29 19:47:58 +02:00
NetworkRecvStatus SendError ( NetworkErrorCode error , const std : : string & reason = { } ) ;
2023-05-08 19:01:06 +02:00
NetworkRecvStatus SendChat ( NetworkAction action , ClientID client_id , bool self_send , const std : : string & msg , int64_t data ) ;
2021-09-19 23:09:06 +02:00
NetworkRecvStatus SendExternalChat ( const std : : string & source , TextColour colour , const std : : string & user , const std : : string & msg ) ;
2010-10-15 22:25:07 +02:00
NetworkRecvStatus SendJoin ( ClientID client_id ) ;
NetworkRecvStatus SendFrame ( ) ;
NetworkRecvStatus SendSync ( ) ;
2024-02-04 17:20:25 +01:00
NetworkRecvStatus SendCommand ( const CommandPacket & cp ) ;
2010-10-15 22:25:07 +02:00
NetworkRecvStatus SendCompanyUpdate ( ) ;
NetworkRecvStatus SendConfigUpdate ( ) ;
2010-10-15 23:56:06 +02: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 17:54:42 +02:00
2021-06-13 21:05:15 +02:00
const std : : string & GetClientIP ( ) ;
2024-03-17 19:11:55 +01:00
std : : string_view GetPeerPublicKey ( ) const { return this - > peer_public_key ; }
2011-04-22 18:02:21 +02:00
2011-04-22 17:54:42 +02:00
static ServerNetworkGameSocketHandler * GetByClientID ( ClientID client_id ) ;
2010-10-15 22:25:07 +02:00
} ;
2004-12-04 18:54:56 +01:00
2005-07-29 23:55:49 +02:00
void NetworkServer_Tick ( bool send_frame ) ;
2024-01-26 16:25:25 +01:00
void ChangeNetworkRestartTime ( bool reset ) ;
2021-05-02 09:07:09 +02:00
void NetworkServerSetCompanyPassword ( CompanyID company_id , const std : : string & password , bool already_hashed = true ) ;
2014-05-11 14:52:21 +02:00
void NetworkServerUpdateCompanyPassworded ( CompanyID company_id , bool passworded ) ;
2005-10-19 21:38:35 +02:00
2005-09-18 22:56:44 +02:00
# endif /* NETWORK_SERVER_H */