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.cpp Base functions for networking support. */
2007-01-02 18:34:03 +01:00
# include "../stdafx.h"
2004-08-09 19:04:08 +02:00
2007-12-21 20:49:27 +01:00
# include "../strings_func.h"
2007-12-21 22:50:46 +01:00
# include "../command_func.h"
2023-04-24 17:56:01 +02:00
# include "../timer/timer_game_calendar.h"
2010-10-17 19:35:42 +02:00
# include "network_admin.h"
2004-12-04 18:54:56 +01:00
# include "network_client.h"
2021-08-23 19:37:51 +02:00
# include "network_query.h"
2004-12-04 18:54:56 +01:00
# include "network_server.h"
2009-01-17 17:53:32 +01:00
# include "network_content.h"
2004-12-04 18:54:56 +01:00
# include "network_udp.h"
# include "network_gamelist.h"
2010-01-15 17:41:15 +01:00
# include "network_base.h"
2021-07-03 11:04:32 +02:00
# include "network_coordinator.h"
2007-01-02 18:34:03 +01:00
# include "core/udp.h"
2008-08-04 14:56:38 +02:00
# include "core/host.h"
2006-12-30 02:52:09 +01:00
# include "network_gui.h"
2008-05-24 12:15:06 +02:00
# include "../console_func.h"
2009-08-21 22:15:17 +02:00
# include "../3rdparty/md5/md5.h"
2007-12-25 12:26:07 +01:00
# include "../core/random_func.hpp"
# include "../window_func.h"
2008-09-30 22:51:04 +02:00
# include "../company_func.h"
2009-01-31 21:16:06 +01:00
# include "../company_base.h"
2008-05-07 11:07:19 +02:00
# include "../landscape_type.h"
2008-05-04 23:53:36 +02:00
# include "../rev.h"
2009-05-22 17:13:50 +02:00
# include "../core/pool_func.hpp"
2009-11-12 21:33:30 +01:00
# include "../gfx_func.h"
2011-12-10 16:16:58 +01:00
# include "../error.h"
2021-10-28 23:48:26 +02:00
# include "../misc_cmd.h"
2021-05-04 19:32:37 +02:00
# include <charconv>
2021-05-16 12:10:12 +02:00
# include <sstream>
# include <iomanip>
2010-04-09 19:08:44 +02:00
2014-04-23 22:13:33 +02:00
# include "../safeguards.h"
2008-01-05 22:42:34 +01:00
# ifdef DEBUG_DUMP_COMMANDS
2010-04-09 19:08:44 +02:00
# include "../fileio_func.h"
/** When running the server till the wait point, run as fast as we can! */
bool _ddc_fastforward = true ;
2008-01-11 01:30:32 +01:00
# endif /* DEBUG_DUMP_COMMANDS */
2008-01-13 02:21:35 +01:00
2011-05-04 22:24:23 +02:00
/** Make sure both pools have the same size. */
2020-12-27 11:44:22 +01:00
static_assert ( NetworkClientInfoPool : : MAX_SIZE = = NetworkClientSocketPool : : MAX_SIZE ) ;
2009-05-22 17:13:50 +02:00
2011-05-04 22:24:23 +02:00
/** The pool with client information. */
2009-05-22 17:13:50 +02:00
NetworkClientInfoPool _networkclientinfo_pool ( " NetworkClientInfo " ) ;
INSTANTIATE_POOL_METHODS ( NetworkClientInfo )
2008-12-23 21:52:27 +01:00
2009-02-25 01:48:04 +01:00
bool _networking ; ///< are we in networking mode?
2008-01-13 22:51:53 +01:00
bool _network_server ; ///< network-server is active
bool _network_available ; ///< is network mode available?
bool _network_dedicated ; ///< are we a dedicated server?
2008-05-30 20:20:26 +02:00
bool _is_network_server ; ///< Does this client wants to be a network-server?
2019-04-10 23:07:06 +02:00
NetworkCompanyState * _network_company_states = nullptr ; ///< Statistics about some companies.
2011-05-04 22:24:23 +02:00
ClientID _network_own_client_id ; ///< Our client identifier.
ClientID _redirect_console_to_client ; ///< If not invalid, redirect the console output to a client.
uint8 _network_reconnect ; ///< Reconnect timeout
StringList _network_bind_list ; ///< The addresses to bind on.
StringList _network_host_list ; ///< The servers we know.
StringList _network_ban_list ; ///< The banned clients.
uint32 _frame_counter_server ; ///< The frame_counter of the server, if in network-mode
uint32 _frame_counter_max ; ///< To where we may go with our clients
uint32 _frame_counter ; ///< The current frame.
uint32 _last_sync_frame ; ///< Used in the server to store the last time a sync packet was sent to clients.
NetworkAddressList _broadcast_list ; ///< List of broadcast addresses.
uint32 _sync_seed_1 ; ///< Seed to compare during sync checks.
2009-11-09 10:59:35 +01:00
# ifdef NETWORK_SEND_DOUBLE_SEED
2011-05-04 22:24:23 +02:00
uint32 _sync_seed_2 ; ///< Second part of the seed.
2009-11-09 10:59:35 +01:00
# endif
2011-05-04 22:24:23 +02:00
uint32 _sync_frame ; ///< The frame to perform the sync check.
bool _network_first_time ; ///< Whether we have finished joining or not.
2009-01-23 23:18:06 +01:00
CompanyMask _network_company_passworded ; ///< Bitmask of the password status of all companies.
2008-01-13 02:39:22 +01:00
2020-12-27 11:44:22 +01:00
static_assert ( ( int ) NETWORK_COMPANY_NAME_LENGTH = = MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH ) ;
2007-01-23 15:47:38 +01:00
2011-05-04 22:24:23 +02:00
/** The amount of clients connected */
2010-10-15 20:42:52 +02:00
byte _network_clients_connected = 0 ;
2004-08-27 13:01:26 +02:00
2023-04-25 19:43:45 +02:00
extern std : : string GenerateUid ( std : : string_view subject ) ;
2014-04-08 22:19:41 +02:00
/**
* Return whether there is any client connected or trying to connect at all .
* @ return whether we have any client activity
*/
bool HasClients ( )
{
2019-12-18 18:21:23 +01:00
return ! NetworkClientSocket : : Iterate ( ) . empty ( ) ;
2014-04-08 22:19:41 +02:00
}
2009-10-07 19:36:33 +02:00
/**
* Basically a client is leaving us right now .
*/
NetworkClientInfo : : ~ NetworkClientInfo ( )
{
/* Delete the chat window, if you were chatting with this client. */
InvalidateWindowData ( WC_SEND_NETWORK_MSG , DESTTYPE_CLIENT , this - > client_id ) ;
}
2008-12-22 22:38:03 +01:00
/**
* Return the CI given it ' s client - identifier
* @ param client_id the ClientID to search for
2019-04-10 23:07:06 +02:00
* @ return return a pointer to the corresponding NetworkClientInfo struct or nullptr when not found
2008-12-22 22:38:03 +01:00
*/
2011-04-22 17:54:16 +02:00
/* static */ NetworkClientInfo * NetworkClientInfo : : GetByClientID ( ClientID client_id )
2004-09-10 21:02:27 +02:00
{
2019-12-16 20:56:10 +01:00
for ( NetworkClientInfo * ci : NetworkClientInfo : : Iterate ( ) ) {
2008-12-22 13:59:31 +01:00
if ( ci - > client_id = = client_id ) return ci ;
2006-06-27 23:25:53 +02:00
}
2004-08-27 13:01:26 +02:00
2019-04-10 23:07:06 +02:00
return nullptr ;
2004-08-22 12:23:37 +02:00
}
2008-12-22 22:38:03 +01:00
/**
* Return the client state given it ' s client - identifier
* @ param client_id the ClientID to search for
2019-04-10 23:07:06 +02:00
* @ return return a pointer to the corresponding NetworkClientSocket struct or nullptr when not found
2008-12-22 22:38:03 +01:00
*/
2011-05-05 18:24:48 +02:00
/* static */ ServerNetworkGameSocketHandler * ServerNetworkGameSocketHandler : : GetByClientID ( ClientID client_id )
2004-12-04 18:54:56 +01:00
{
2019-12-16 20:56:10 +01:00
for ( NetworkClientSocket * cs : NetworkClientSocket : : Iterate ( ) ) {
2008-12-22 13:59:31 +01:00
if ( cs - > client_id = = client_id ) return cs ;
2006-06-27 23:25:53 +02:00
}
2004-09-07 00:46:02 +02:00
2019-04-10 23:07:06 +02:00
return nullptr ;
2004-09-07 00:46:02 +02:00
}
2007-03-07 12:47:46 +01:00
byte NetworkSpectatorCount ( )
2006-01-31 23:16:15 +01:00
{
byte count = 0 ;
2019-12-16 20:56:10 +01:00
for ( const NetworkClientInfo * ci : NetworkClientInfo : : Iterate ( ) ) {
2008-12-23 09:39:30 +01:00
if ( ci - > client_playas = = COMPANY_SPECTATOR ) count + + ;
2006-01-31 23:16:15 +01:00
}
2009-01-21 11:46:21 +01:00
/* Don't count a dedicated server as spectator */
if ( _network_dedicated ) count - - ;
2006-01-31 23:16:15 +01:00
return count ;
}
2011-01-19 17:30:09 +01:00
/**
2011-01-19 17:47:40 +01:00
* Change the company password of a given company .
* @ param company_id ID of the company the password should be changed for .
* @ param password The unhashed password we like to set ( ' * ' or ' ' resets the password )
* @ return The password .
2011-01-19 17:30:09 +01:00
*/
2021-05-02 09:18:56 +02:00
std : : string NetworkChangeCompanyPassword ( CompanyID company_id , std : : string password )
2011-01-19 17:30:09 +01:00
{
2021-05-02 09:18:56 +02:00
if ( password . compare ( " * " ) = = 0 ) password = " " ;
2011-01-19 17:30:09 +01:00
2011-01-19 17:47:40 +01:00
if ( _network_server ) {
2011-08-13 10:32:55 +02:00
NetworkServerSetCompanyPassword ( company_id , password , false ) ;
2011-01-19 17:30:09 +01:00
} else {
2011-01-19 17:47:40 +01:00
NetworkClientSetCompanyPassword ( password ) ;
2011-01-19 17:30:09 +01:00
}
return password ;
}
/**
2011-01-19 17:35:11 +01:00
* Hash the given password using server ID and game seed .
* @ param password Password to hash .
* @ param password_server_id Server ID .
* @ param password_game_seed Game seed .
* @ return The hashed password .
2011-01-19 17:30:09 +01:00
*/
2021-05-02 09:07:09 +02:00
std : : string GenerateCompanyPasswordHash ( const std : : string & password , const std : : string & password_server_id , uint32 password_game_seed )
2011-01-19 17:30:09 +01:00
{
2021-05-02 09:07:09 +02:00
if ( password . empty ( ) ) return password ;
2011-01-19 17:30:09 +01:00
2021-05-02 09:07:09 +02:00
size_t password_length = password . size ( ) ;
size_t password_server_id_length = password_server_id . size ( ) ;
2011-01-19 17:30:09 +01:00
2021-05-16 12:10:12 +02:00
std : : ostringstream salted_password ;
/* Add the password with the server's ID and game seed as the salt. */
2011-01-19 17:30:09 +01:00
for ( uint i = 0 ; i < NETWORK_SERVER_ID_LENGTH - 1 ; i + + ) {
2021-05-02 11:05:50 +02:00
char password_char = ( i < password_length ? password [ i ] : 0 ) ;
char server_id_char = ( i < password_server_id_length ? password_server_id [ i ] : 0 ) ;
char seed_char = password_game_seed > > ( i % 32 ) ;
2021-05-16 12:10:12 +02:00
salted_password < < ( char ) ( password_char ^ server_id_char ^ seed_char ) ; // Cast needed, otherwise interpreted as integer to format
2011-01-19 17:30:09 +01:00
}
Md5 checksum ;
2023-05-18 22:38:56 +02:00
MD5Hash digest ;
2011-01-19 17:30:09 +01:00
/* Generate the MD5 hash */
2021-05-16 12:10:12 +02:00
std : : string salted_password_string = salted_password . str ( ) ;
checksum . Append ( salted_password_string . data ( ) , salted_password_string . size ( ) ) ;
2011-01-19 17:30:09 +01:00
checksum . Finish ( digest ) ;
2023-05-18 22:38:56 +02:00
return MD5SumToString ( digest ) ;
2011-01-19 17:30:09 +01:00
}
2009-01-23 23:18:06 +01:00
/**
* Check if the company we want to join requires a password .
* @ param company_id id of the company we want to check the ' passworded ' flag for .
* @ return true if the company requires a password .
*/
bool NetworkCompanyIsPassworded ( CompanyID company_id )
{
return HasBit ( _network_company_passworded , company_id ) ;
}
2009-03-15 01:32:18 +01:00
/* This puts a text-message to the console, or in the future, the chat-box,
* ( to keep it all a bit more general )
* If ' self_send ' is true , this is the client who is sending the message */
2021-09-19 23:09:06 +02:00
void NetworkTextMessage ( NetworkAction action , TextColour colour , bool self_send , const std : : string & name , const std : : string & str , int64 data , const std : : string & data_str )
2004-09-08 21:20:46 +02:00
{
2008-12-29 11:37:53 +01:00
StringID strid ;
2004-12-04 18:54:56 +01:00
switch ( action ) {
2006-10-25 00:23:08 +02:00
case NETWORK_ACTION_SERVER_MESSAGE :
2008-12-29 11:37:53 +01:00
/* Ignore invalid messages */
strid = STR_NETWORK_SERVER_MESSAGE ;
2009-02-09 03:09:47 +01:00
colour = CC_DEFAULT ;
2004-12-04 18:54:56 +01:00
break ;
2009-01-23 23:18:06 +01:00
case NETWORK_ACTION_COMPANY_SPECTATOR :
2009-02-09 03:09:47 +01:00
colour = CC_DEFAULT ;
2009-08-05 19:59:21 +02:00
strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE ;
2009-01-23 23:18:06 +01:00
break ;
case NETWORK_ACTION_COMPANY_JOIN :
2009-02-09 03:09:47 +01:00
colour = CC_DEFAULT ;
2009-08-05 19:59:21 +02:00
strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN ;
2009-01-23 23:18:06 +01:00
break ;
case NETWORK_ACTION_COMPANY_NEW :
2009-02-09 03:09:47 +01:00
colour = CC_DEFAULT ;
2009-08-05 19:59:21 +02:00
strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW ;
2009-01-23 23:18:06 +01:00
break ;
2009-09-07 23:28:16 +02:00
case NETWORK_ACTION_JOIN :
/* Show the Client ID for the server but not for the client. */
strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID : STR_NETWORK_MESSAGE_CLIENT_JOINED ;
break ;
2009-08-05 19:59:21 +02:00
case NETWORK_ACTION_LEAVE : strid = STR_NETWORK_MESSAGE_CLIENT_LEFT ; break ;
case NETWORK_ACTION_NAME_CHANGE : strid = STR_NETWORK_MESSAGE_NAME_CHANGE ; break ;
2021-01-05 18:06:48 +01:00
case NETWORK_ACTION_GIVE_MONEY : strid = STR_NETWORK_MESSAGE_GIVE_MONEY ; break ;
2008-12-29 11:37:53 +01:00
case NETWORK_ACTION_CHAT_COMPANY : strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY ; break ;
case NETWORK_ACTION_CHAT_CLIENT : strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT ; break ;
2020-01-21 16:39:10 +01:00
case NETWORK_ACTION_KICKED : strid = STR_NETWORK_MESSAGE_KICKED ; break ;
2021-09-19 23:09:06 +02:00
case NETWORK_ACTION_EXTERNAL_CHAT : strid = STR_NETWORK_CHAT_EXTERNAL ; break ;
2009-01-09 23:48:57 +01:00
default : strid = STR_NETWORK_CHAT_ALL ; break ;
2004-12-04 18:54:56 +01:00
}
2004-12-19 16:14:55 +01:00
2008-12-29 11:37:53 +01:00
char message [ 1024 ] ;
SetDParamStr ( 0 , name ) ;
SetDParamStr ( 1 , str ) ;
SetDParam ( 2 , data ) ;
2021-09-19 23:09:06 +02:00
SetDParamStr ( 3 , data_str ) ;
2010-10-21 22:12:48 +02:00
/* All of these strings start with "***". These characters are interpreted as both left-to-right and
* right - to - left characters depending on the context . As the next text might be an user ' s name , the
* user name ' s characters will influence the direction of the " *** " instead of the language setting
* of the game . Manually set the direction of the " *** " by inserting a text - direction marker . */
2010-11-13 10:56:25 +01:00
char * msg_ptr = message + Utf8Encode ( message , _current_text_dir = = TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM ) ;
2010-10-21 22:12:48 +02:00
GetString ( msg_ptr , strid , lastof ( message ) ) ;
2008-12-29 11:37:53 +01:00
2023-04-24 17:56:01 +02:00
Debug ( desync , 1 , " msg: {:08x}; {:02x}; {} " , TimerGameCalendar : : date , TimerGameCalendar : : date_fract , message ) ;
2021-06-12 21:31:57 +02:00
IConsolePrint ( colour , message ) ;
2021-09-19 23:09:06 +02:00
NetworkAddChatMessage ( colour , _settings_client . gui . network_chat_timeout , message ) ;
2004-09-11 21:34:11 +02:00
}
2009-03-15 01:32:18 +01:00
/* Calculate the frame-lag of a client */
2008-12-23 10:47:42 +01:00
uint NetworkCalculateLag ( const NetworkClientSocket * cs )
2004-09-12 00:10:31 +02:00
{
2004-12-04 18:54:56 +01:00
int lag = cs - > last_frame_server - cs - > last_frame ;
2021-05-09 19:02:17 +02:00
/* This client has missed their ACK packet after 1 DAY_TICKS..
* so we increase their lag for every frame that passes !
2009-03-15 01:32:18 +01:00
* The packet can be out by a max of _net_frame_freq */
2010-07-24 12:14:39 +02:00
if ( cs - > last_frame_server + DAY_TICKS + _settings_client . network . frame_freq < _frame_counter ) {
2008-05-29 22:21:28 +02:00
lag + = _frame_counter - ( cs - > last_frame_server + DAY_TICKS + _settings_client . network . frame_freq ) ;
2010-07-24 12:14:39 +02:00
}
2004-12-04 18:54:56 +01:00
return lag ;
2004-09-12 00:10:31 +02:00
}
2004-12-04 18:54:56 +01:00
2009-03-15 01:32:18 +01:00
/* There was a non-recoverable error, drop back to the main menu with a nice
* error */
2021-04-30 15:39:46 +02:00
void ShowNetworkError ( StringID error_string )
2004-08-09 19:04:08 +02:00
{
2004-12-04 18:54:56 +01:00
_switch_mode = SM_MENU ;
2011-12-10 16:16:58 +01:00
ShowErrorMessage ( error_string , INVALID_STRING_ID , WL_CRITICAL ) ;
2004-12-04 18:54:56 +01:00
}
2004-08-09 19:04:08 +02:00
2008-12-29 11:37:53 +01:00
/**
* Retrieve the string id of an internal error number
2006-06-27 23:25:53 +02:00
* @ param err NetworkErrorCode
2008-12-29 11:37:53 +01:00
* @ return the StringID
*/
StringID GetNetworkErrorMsg ( NetworkErrorCode err )
2006-04-03 20:31:01 +02:00
{
/* List of possible network errors, used by
* PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
static const StringID network_error_strings [ ] = {
2009-08-05 19:59:21 +02:00
STR_NETWORK_ERROR_CLIENT_GENERAL ,
STR_NETWORK_ERROR_CLIENT_DESYNC ,
STR_NETWORK_ERROR_CLIENT_SAVEGAME ,
STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST ,
STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR ,
STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH ,
STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED ,
STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED ,
STR_NETWORK_ERROR_CLIENT_WRONG_REVISION ,
STR_NETWORK_ERROR_CLIENT_NAME_IN_USE ,
STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD ,
STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH ,
STR_NETWORK_ERROR_CLIENT_KICKED ,
STR_NETWORK_ERROR_CLIENT_CHEATER ,
2010-08-19 10:59:36 +02:00
STR_NETWORK_ERROR_CLIENT_SERVER_FULL ,
2012-01-09 22:22:48 +01:00
STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS ,
STR_NETWORK_ERROR_CLIENT_TIMEOUT_PASSWORD ,
STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER ,
STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP ,
STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN ,
2021-04-22 08:17:36 +02:00
STR_NETWORK_ERROR_CLIENT_INVALID_CLIENT_NAME ,
2006-04-03 20:31:01 +02:00
} ;
2020-12-27 11:44:22 +01:00
static_assert ( lengthof ( network_error_strings ) = = NETWORK_ERROR_END ) ;
2006-04-03 20:31:01 +02:00
2007-01-10 19:56:51 +01:00
if ( err > = ( ptrdiff_t ) lengthof ( network_error_strings ) ) err = NETWORK_ERROR_GENERAL ;
2006-04-03 20:31:01 +02:00
2008-12-29 22:41:20 +01:00
return network_error_strings [ err ] ;
2006-04-03 20:31:01 +02:00
}
2009-11-12 21:33:30 +01:00
/**
* Handle the pause mode change so we send the right messages to the chat .
* @ param prev_mode The previous pause mode .
* @ param changed_mode The pause mode that got changed .
*/
void NetworkHandlePauseChange ( PauseMode prev_mode , PauseMode changed_mode )
{
if ( ! _networking ) return ;
switch ( changed_mode ) {
case PM_PAUSED_NORMAL :
case PM_PAUSED_JOIN :
2011-12-19 21:58:59 +01:00
case PM_PAUSED_GAME_SCRIPT :
2019-01-22 19:31:08 +01:00
case PM_PAUSED_ACTIVE_CLIENTS :
case PM_PAUSED_LINK_GRAPH : {
2009-11-12 21:33:30 +01:00
bool changed = ( ( _pause_mode = = PM_UNPAUSED ) ! = ( prev_mode = = PM_UNPAUSED ) ) ;
bool paused = ( _pause_mode ! = PM_UNPAUSED ) ;
if ( ! paused & & ! changed ) return ;
StringID str ;
if ( ! changed ) {
int i = - 1 ;
2011-12-19 21:58:59 +01:00
2009-11-12 21:33:30 +01:00
if ( ( _pause_mode & PM_PAUSED_NORMAL ) ! = PM_UNPAUSED ) SetDParam ( + + i , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL ) ;
if ( ( _pause_mode & PM_PAUSED_JOIN ) ! = PM_UNPAUSED ) SetDParam ( + + i , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS ) ;
2011-12-19 21:58:59 +01:00
if ( ( _pause_mode & PM_PAUSED_GAME_SCRIPT ) ! = PM_UNPAUSED ) SetDParam ( + + i , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT ) ;
2009-11-12 21:33:30 +01:00
if ( ( _pause_mode & PM_PAUSED_ACTIVE_CLIENTS ) ! = PM_UNPAUSED ) SetDParam ( + + i , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS ) ;
2019-01-22 19:31:08 +01:00
if ( ( _pause_mode & PM_PAUSED_LINK_GRAPH ) ! = PM_UNPAUSED ) SetDParam ( + + i , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH ) ;
2009-11-12 21:33:30 +01:00
str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i ;
} else {
switch ( changed_mode ) {
case PM_PAUSED_NORMAL : SetDParam ( 0 , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL ) ; break ;
case PM_PAUSED_JOIN : SetDParam ( 0 , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS ) ; break ;
2011-12-19 21:58:59 +01:00
case PM_PAUSED_GAME_SCRIPT : SetDParam ( 0 , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT ) ; break ;
2009-11-12 21:33:30 +01:00
case PM_PAUSED_ACTIVE_CLIENTS : SetDParam ( 0 , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS ) ; break ;
2019-01-22 19:31:08 +01:00
case PM_PAUSED_LINK_GRAPH : SetDParam ( 0 , STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH ) ; break ;
2009-11-12 21:33:30 +01:00
default : NOT_REACHED ( ) ;
}
str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED ;
}
2021-05-14 18:45:54 +02:00
NetworkTextMessage ( NETWORK_ACTION_SERVER_MESSAGE , CC_DEFAULT , false , " " , GetString ( str ) ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2009-11-12 21:33:30 +01:00
default :
return ;
}
}
2009-11-12 21:52:14 +01:00
/**
* Helper function for the pause checkers . If pause is true and the
* current pause mode isn ' t set the game will be paused , if it it false
* and the pause mode is set the game will be unpaused . In the other
* cases nothing happens to the pause state .
* @ param pause whether we ' d like to pause
* @ param pm the mode which we would like to pause with
*/
static void CheckPauseHelper ( bool pause , PauseMode pm )
{
if ( pause = = ( ( _pause_mode & pm ) ! = PM_UNPAUSED ) ) return ;
2021-11-23 01:05:58 +01:00
Command < CMD_PAUSE > : : Post ( pm , pause ) ;
2009-11-12 21:52:14 +01:00
}
2009-06-02 21:56:23 +02:00
/**
* Counts the number of active clients connected .
* It has to be in STATUS_ACTIVE and not a spectator
* @ return number of active clients
*/
2008-09-30 22:39:50 +02:00
static uint NetworkCountActiveClients ( )
2006-10-03 18:15:34 +02:00
{
uint count = 0 ;
2019-12-16 20:56:10 +01:00
for ( const NetworkClientSocket * cs : NetworkClientSocket : : Iterate ( ) ) {
2010-10-24 22:07:32 +02:00
if ( cs - > status ! = NetworkClientSocket : : STATUS_ACTIVE ) continue ;
2009-06-02 21:56:23 +02:00
if ( ! Company : : IsValidID ( cs - > GetInfo ( ) - > client_playas ) ) continue ;
count + + ;
2006-10-03 18:15:34 +02:00
}
return count ;
}
2009-11-12 21:52:14 +01:00
/**
* Check if the minimum number of active clients has been reached and pause or unpause the game as appropriate
*/
2009-03-20 00:32:39 +01:00
static void CheckMinActiveClients ( )
2006-10-03 18:15:34 +02:00
{
2009-11-12 21:52:14 +01:00
if ( ( _pause_mode & PM_PAUSED_ERROR ) ! = PM_UNPAUSED | |
! _network_dedicated | |
( _settings_client . network . min_active_clients = = 0 & & ( _pause_mode & PM_PAUSED_ACTIVE_CLIENTS ) = = PM_UNPAUSED ) ) {
return ;
}
CheckPauseHelper ( NetworkCountActiveClients ( ) < _settings_client . network . min_active_clients , PM_PAUSED_ACTIVE_CLIENTS ) ;
}
2006-10-03 18:15:34 +02:00
2009-11-12 21:52:14 +01:00
/**
* Checks whether there is a joining client
* @ return true iff one client is joining ( but not authorizing )
*/
static bool NetworkHasJoiningClient ( )
{
2019-12-16 20:56:10 +01:00
for ( const NetworkClientSocket * cs : NetworkClientSocket : : Iterate ( ) ) {
2010-10-24 22:07:32 +02:00
if ( cs - > status > = NetworkClientSocket : : STATUS_AUTHORIZED & & cs - > status < NetworkClientSocket : : STATUS_ACTIVE ) return true ;
2009-11-12 21:52:14 +01:00
}
2006-10-03 18:15:34 +02:00
2009-11-12 21:52:14 +01:00
return false ;
}
2006-10-03 18:15:34 +02:00
2009-11-12 21:52:14 +01:00
/**
* Check whether we should pause on join
*/
static void CheckPauseOnJoin ( )
{
if ( ( _pause_mode & PM_PAUSED_ERROR ) ! = PM_UNPAUSED | |
( ! _settings_client . network . pause_on_join & & ( _pause_mode & PM_PAUSED_JOIN ) = = PM_UNPAUSED ) ) {
return ;
2006-10-03 18:15:34 +02:00
}
2009-11-12 21:52:14 +01:00
CheckPauseHelper ( NetworkHasJoiningClient ( ) , PM_PAUSED_JOIN ) ;
2006-10-03 18:15:34 +02:00
}
2021-07-11 12:08:03 +02:00
/**
* Parse the company part ( " #company " postfix ) of a connecting string .
* @ param connection_string The string with the connection data .
* @ param company_id The company ID to set , if available .
* @ return A std : : string_view into the connection string without the company part .
*/
std : : string_view ParseCompanyFromConnectionString ( const std : : string & connection_string , CompanyID * company_id )
{
std : : string_view ip = connection_string ;
if ( company_id = = nullptr ) return ip ;
size_t offset = ip . find_last_of ( ' # ' ) ;
if ( offset ! = std : : string : : npos ) {
std : : string_view company_string = ip . substr ( offset + 1 ) ;
ip = ip . substr ( 0 , offset ) ;
uint8 company_value ;
auto [ _ , err ] = std : : from_chars ( company_string . data ( ) , company_string . data ( ) + company_string . size ( ) , company_value ) ;
if ( err = = std : : errc ( ) ) {
if ( company_value ! = COMPANY_NEW_COMPANY & & company_value ! = COMPANY_SPECTATOR ) {
if ( company_value > MAX_COMPANIES | | company_value = = 0 ) {
* company_id = COMPANY_SPECTATOR ;
} else {
/* "#1" means the first company, which has index 0. */
* company_id = ( CompanyID ) ( company_value - 1 ) ;
}
} else {
* company_id = ( CompanyID ) company_value ;
}
}
}
return ip ;
}
2021-04-20 16:26:07 +02:00
/**
2021-04-29 12:09:03 +02:00
* Converts a string to ip / port / company
* Format : IP : port # company
2021-04-20 16:26:07 +02:00
*
2021-05-04 19:32:37 +02:00
* Returns the IP part as a string view into the passed string . This view is
* valid as long the passed connection string is valid . If there is no port
* present in the connection string , the port reference will not be touched .
* When there is no company ID present in the connection string or company_id
* is nullptr , then company ID will not be touched .
*
* @ param connection_string The string with the connection data .
* @ param port The port reference to set .
* @ param company_id The company ID to set , if available .
* @ return A std : : string_view into the connection string with the ( IP ) address part .
2021-04-20 16:26:07 +02:00
*/
2021-05-04 19:32:37 +02:00
std : : string_view ParseFullConnectionString ( const std : : string & connection_string , uint16 & port , CompanyID * company_id )
2021-04-20 16:26:07 +02:00
{
2021-07-11 12:08:03 +02:00
std : : string_view ip = ParseCompanyFromConnectionString ( connection_string , company_id ) ;
2021-05-04 19:32:37 +02:00
size_t port_offset = ip . find_last_of ( ' : ' ) ;
size_t ipv6_close = ip . find_last_of ( ' ] ' ) ;
if ( port_offset ! = std : : string : : npos & & ( ipv6_close = = std : : string : : npos | | ipv6_close < port_offset ) ) {
std : : string_view port_string = ip . substr ( port_offset + 1 ) ;
ip = ip . substr ( 0 , port_offset ) ;
std : : from_chars ( port_string . data ( ) , port_string . data ( ) + port_string . size ( ) , port ) ;
}
return ip ;
2021-04-20 16:26:07 +02:00
}
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
/**
* Normalize a connection string . That is , ensure there is a port in the string .
* @ param connection_string The connection string to normalize .
* @ param default_port The port to use if none is given .
* @ return The normalized connection string .
*/
std : : string NormalizeConnectionString ( const std : : string & connection_string , uint16 default_port )
{
uint16 port = default_port ;
std : : string_view ip = ParseFullConnectionString ( connection_string , port ) ;
return std : : string ( ip ) + " : " + std : : to_string ( port ) ;
}
2010-08-01 21:22:34 +02:00
/**
2021-04-29 12:09:03 +02:00
* Convert a string containing either " hostname " or " hostname:ip " to a
* NetworkAddress .
2007-01-17 00:01:06 +01:00
*
2021-04-29 12:09:03 +02:00
* @ param connection_string The string to parse .
* @ param default_port The default port to set port to if not in connection_string .
* @ return A valid NetworkAddress of the parsed information .
2010-08-01 21:44:49 +02:00
*/
2021-05-04 19:32:37 +02:00
NetworkAddress ParseConnectionString ( const std : : string & connection_string , uint16 default_port )
2004-09-12 16:12:33 +02:00
{
2021-05-04 19:32:37 +02:00
uint16 port = default_port ;
std : : string_view ip = ParseFullConnectionString ( connection_string , port ) ;
return NetworkAddress ( ip , port ) ;
2021-04-29 12:09:03 +02:00
}
2009-04-08 03:22:57 +02:00
2011-05-05 18:24:48 +02:00
/**
2013-01-08 23:46:42 +01:00
* Handle the accepting of a connection to the server .
2011-05-05 18:24:48 +02:00
* @ param s The socket of the new connection .
* @ param address The address of the peer .
*/
2010-10-15 23:56:06 +02:00
/* static */ void ServerNetworkGameSocketHandler : : AcceptConnection ( SOCKET s , const NetworkAddress & address )
2004-08-09 19:04:08 +02:00
{
2010-10-15 23:56:06 +02:00
/* Register the login */
_network_clients_connected + + ;
2004-12-04 18:54:56 +01:00
2010-10-15 23:56:06 +02:00
ServerNetworkGameSocketHandler * cs = new ServerNetworkGameSocketHandler ( s ) ;
2011-04-22 18:03:48 +02:00
cs - > client_address = address ; // Save the IP of the client
2021-04-18 09:54:47 +02:00
InvalidateWindowData ( WC_CLIENT_LIST , 0 ) ;
2004-08-09 19:04:08 +02:00
}
2010-10-17 19:35:42 +02:00
/**
* Resets the pools used for network clients , and the admin pool if needed .
* @ param close_admins Whether the admin pool has to be cleared as well .
*/
static void InitializeNetworkPools ( bool close_admins = true )
2009-03-07 23:40:47 +01:00
{
2011-02-20 00:05:47 +01:00
PoolBase : : Clean ( PT_NCLIENT | ( close_admins ? PT_NADMIN : PT_NONE ) ) ;
2009-03-07 23:40:47 +01:00
}
2010-10-17 19:35:42 +02:00
/**
* Close current connections .
* @ param close_admins Whether the admin connections have to be closed as well .
*/
void NetworkClose ( bool close_admins )
2004-08-09 19:04:08 +02:00
{
2004-12-04 18:54:56 +01:00
if ( _network_server ) {
2010-10-17 19:35:42 +02:00
if ( close_admins ) {
2019-12-16 20:56:10 +01:00
for ( ServerNetworkAdminSocketHandler * as : ServerNetworkAdminSocketHandler : : Iterate ( ) ) {
2010-10-17 19:52:12 +02:00
as - > CloseConnection ( true ) ;
2010-10-17 19:35:42 +02:00
}
}
2019-12-16 20:56:10 +01:00
for ( NetworkClientSocket * cs : NetworkClientSocket : : Iterate ( ) ) {
2021-05-11 12:26:16 +02:00
cs - > CloseConnection ( NETWORK_RECV_STATUS_CLIENT_QUIT ) ;
2010-10-15 21:58:56 +02:00
}
2010-10-15 23:56:06 +02:00
ServerNetworkGameSocketHandler : : CloseListeners ( ) ;
2010-10-17 19:35:42 +02:00
ServerNetworkAdminSocketHandler : : CloseListeners ( ) ;
2021-07-03 11:04:32 +02:00
_network_coordinator_client . CloseConnection ( ) ;
2021-04-29 15:37:02 +02:00
} else {
if ( MyClient : : my_client ! = nullptr ) {
MyClient : : SendQuit ( ) ;
MyClient : : my_client - > CloseConnection ( NETWORK_RECV_STATUS_CLIENT_QUIT ) ;
}
2021-07-19 19:32:22 +02:00
_network_coordinator_client . CloseAllConnections ( ) ;
2004-08-09 19:04:08 +02:00
}
2008-05-30 22:08:32 +02:00
2009-01-20 12:28:18 +01:00
TCPConnecter : : KillAll ( ) ;
2008-05-30 22:08:32 +02:00
_networking = false ;
_network_server = false ;
2008-12-22 21:42:02 +01:00
2009-02-20 01:03:29 +01:00
NetworkFreeLocalCommandQueue ( ) ;
2021-05-02 08:27:06 +02:00
delete [ ] _network_company_states ;
2019-04-10 23:07:06 +02:00
_network_company_states = nullptr ;
2023-01-15 07:24:18 +01:00
_network_company_passworded = 0 ;
2008-12-23 21:52:27 +01:00
2010-10-17 19:35:42 +02:00
InitializeNetworkPools ( close_admins ) ;
2004-08-09 19:04:08 +02:00
}
2013-01-08 23:46:42 +01:00
/* Initializes the network (cleans sockets and stuff) */
2010-10-17 19:35:42 +02:00
static void NetworkInitialize ( bool close_admins = true )
2004-08-09 19:04:08 +02:00
{
2010-10-17 19:35:42 +02:00
InitializeNetworkPools ( close_admins ) ;
2004-08-22 12:23:37 +02:00
2004-12-04 18:54:56 +01:00
_sync_frame = 0 ;
_network_first_time = true ;
2004-08-22 12:23:37 +02:00
2004-12-04 18:54:56 +01:00
_network_reconnect = 0 ;
2004-08-09 19:04:08 +02:00
}
2021-05-09 18:48:21 +02:00
/** Non blocking connection to query servers for their game info. */
2021-04-28 14:36:14 +02:00
class TCPQueryConnecter : TCPServerConnecter {
2021-04-30 11:34:47 +02:00
private :
2021-05-05 23:21:14 +02:00
std : : string connection_string ;
2021-04-30 11:34:47 +02:00
2009-01-20 12:28:18 +01:00
public :
2021-04-28 14:36:14 +02:00
TCPQueryConnecter ( const std : : string & connection_string ) : TCPServerConnecter ( connection_string , NETWORK_DEFAULT_PORT ) , connection_string ( connection_string ) { }
2009-01-20 12:28:18 +01:00
2021-05-11 19:19:37 +02:00
void OnFailure ( ) override
{
NetworkGameList * item = NetworkGameListAddItem ( connection_string ) ;
2021-08-23 20:16:22 +02:00
item - > status = NGLS_OFFLINE ;
2021-10-03 11:02:28 +02:00
item - > refreshing = false ;
2021-05-11 19:19:37 +02:00
UpdateNetworkGameWindow ( ) ;
}
2019-03-24 17:24:06 +01:00
void OnConnect ( SOCKET s ) override
2009-01-20 12:28:18 +01:00
{
2021-08-23 19:37:51 +02:00
QueryNetworkGameSocketHandler : : QueryServer ( s , this - > connection_string ) ;
2009-01-20 12:28:18 +01:00
}
} ;
2021-04-26 15:18:10 +02:00
/**
2021-05-09 18:48:21 +02:00
* Query a server to fetch the game - info .
2021-05-05 23:21:14 +02:00
* @ param connection_string the address to query .
2021-04-26 15:18:10 +02:00
*/
2021-05-09 18:48:21 +02:00
void NetworkQueryServer ( const std : : string & connection_string )
2004-08-09 19:04:08 +02:00
{
2007-02-02 22:04:50 +01:00
if ( ! _network_available ) return ;
2004-08-09 19:04:08 +02:00
2021-10-03 11:02:28 +02:00
/* Mark the entry as refreshing, so the GUI can show the refresh is pending. */
NetworkGameList * item = NetworkGameListAddItem ( connection_string ) ;
item - > refreshing = true ;
2021-05-09 18:48:21 +02:00
new TCPQueryConnecter ( connection_string ) ;
}
2021-04-29 12:09:03 +02:00
/**
* Validates an address entered as a string and adds the server to
2006-01-25 20:03:50 +01:00
* the list . If you use this function , the games will be marked
2021-04-29 12:09:03 +02:00
* as manually added .
2021-04-30 11:34:47 +02:00
* @ param connection_string The IP : port of the server to add .
2021-04-29 16:30:42 +02:00
* @ param manually Whether the enter should be marked as manual added .
* @ param never_expire Whether the entry can expire ( removed when no longer found in the public listing ) .
2021-04-30 11:34:47 +02:00
* @ return The entry on the game list .
2021-04-29 12:09:03 +02:00
*/
2021-04-29 16:30:42 +02:00
NetworkGameList * NetworkAddServer ( const std : : string & connection_string , bool manually , bool never_expire )
2004-12-18 19:58:03 +01:00
{
2021-04-30 11:34:47 +02:00
if ( connection_string . empty ( ) ) return nullptr ;
/* Ensure the item already exists in the list */
2021-05-05 23:21:14 +02:00
NetworkGameList * item = NetworkGameListAddItem ( connection_string ) ;
2021-05-06 16:19:00 +02:00
if ( item - > info . server_name . empty ( ) ) {
2021-04-30 11:34:47 +02:00
ClearGRFConfigList ( & item - > info . grfconfig ) ;
2021-05-06 16:19:00 +02:00
item - > info . server_name = connection_string ;
2021-04-30 11:34:47 +02:00
UpdateNetworkGameWindow ( ) ;
2021-05-04 20:43:35 +02:00
2021-05-09 18:48:21 +02:00
NetworkQueryServer ( connection_string ) ;
2021-04-30 11:34:47 +02:00
}
2021-05-04 20:43:35 +02:00
if ( manually ) item - > manually = true ;
2021-04-29 16:30:42 +02:00
if ( never_expire ) item - > version = INT32_MAX ;
2004-12-18 19:58:03 +01:00
2021-04-30 11:34:47 +02:00
return item ;
2004-12-20 23:14:39 +01:00
}
2009-04-10 14:56:55 +02:00
/**
* Get the addresses to bind to .
* @ param addresses the list to write to .
* @ param port the port to bind to .
*/
void GetBindAddresses ( NetworkAddressList * addresses , uint16 port )
{
2019-04-02 21:31:33 +02:00
for ( const auto & iter : _network_bind_list ) {
addresses - > emplace_back ( iter . c_str ( ) , port ) ;
2009-04-10 14:56:55 +02:00
}
/* No address, so bind to everything. */
2018-09-23 13:23:54 +02:00
if ( addresses - > size ( ) = = 0 ) {
2019-02-18 23:39:06 +01:00
addresses - > emplace_back ( " " , port ) ;
2009-04-10 14:56:55 +02:00
}
}
2004-12-21 17:56:43 +01:00
/* Generates the list of manually added hosts from NetworkGameList and
2004-12-20 23:14:39 +01:00
* dumps them into the array _network_host_list . This array is needed
* by the function that generates the config file . */
2007-03-07 12:47:46 +01:00
void NetworkRebuildHostList ( )
2004-12-20 23:14:39 +01:00
{
2019-04-02 21:31:33 +02:00
_network_host_list . clear ( ) ;
2004-12-20 23:14:39 +01:00
2019-04-10 23:07:06 +02:00
for ( NetworkGameList * item = _network_game_list ; item ! = nullptr ; item = item - > next ) {
2021-05-05 23:21:14 +02:00
if ( item - > manually ) _network_host_list . emplace_back ( item - > connection_string ) ;
2004-12-18 19:58:03 +01:00
}
}
2009-01-20 12:28:18 +01:00
/** Non blocking connection create to actually connect to servers */
2021-04-28 14:36:14 +02:00
class TCPClientConnecter : TCPServerConnecter {
2021-05-05 23:21:14 +02:00
private :
std : : string connection_string ;
2009-01-20 12:28:18 +01:00
public :
2021-04-28 14:36:14 +02:00
TCPClientConnecter ( const std : : string & connection_string ) : TCPServerConnecter ( connection_string , NETWORK_DEFAULT_PORT ) , connection_string ( connection_string ) { }
2009-01-20 12:28:18 +01:00
2019-03-24 17:24:06 +01:00
void OnFailure ( ) override
2009-01-20 12:28:18 +01:00
{
2021-04-30 15:39:46 +02:00
ShowNetworkError ( STR_NETWORK_ERROR_NOCONNECTION ) ;
2009-01-20 12:28:18 +01:00
}
2019-03-24 17:24:06 +01:00
void OnConnect ( SOCKET s ) override
2009-01-20 12:28:18 +01:00
{
_networking = true ;
2021-05-05 23:21:14 +02:00
new ClientNetworkGameSocketHandler ( s , this - > connection_string ) ;
2009-01-20 12:28:18 +01:00
IConsoleCmdExec ( " exec scripts/on_client.scr 0 " ) ;
NetworkClient_Connected ( ) ;
}
} ;
2021-05-01 13:54:30 +02:00
/**
* Join a client to the server at with the given connection string .
* The default for the passwords is \ c nullptr . When the server or company needs a
* password and none is given , the user is asked to enter the password in the GUI .
* This function will return false whenever some information required to join is not
* correct such as the company number or the client ' s name , or when there is not
* networking avalabile at all . If the function returns false the connection with
* the existing server is not disconnected .
* It will return true when it starts the actual join process , i . e . when it
* actually shows the join status window .
*
* @ param connection_string The IP address , port and company number to join as .
* @ param default_company The company number to join as when none is given .
* @ param join_server_password The password for the server .
* @ param join_company_password The password for the company .
* @ return Whether the join has started .
*/
2021-05-02 09:18:56 +02:00
bool NetworkClientConnectGame ( const std : : string & connection_string , CompanyID default_company , const std : : string & join_server_password , const std : : string & join_company_password )
2021-04-29 16:43:13 +02:00
{
CompanyID join_as = default_company ;
2021-04-28 14:36:14 +02:00
std : : string resolved_connection_string = ServerAddress : : Parse ( connection_string , NETWORK_DEFAULT_PORT , & join_as ) . connection_string ;
2009-01-20 12:28:18 +01:00
2021-05-01 13:54:30 +02:00
if ( ! _network_available ) return false ;
2021-06-21 18:29:21 +02:00
if ( ! NetworkValidateOurClientName ( ) ) return false ;
2021-04-22 08:01:52 +02:00
2021-05-05 23:21:14 +02:00
_network_join . connection_string = resolved_connection_string ;
2021-05-01 14:21:33 +02:00
_network_join . company = join_as ;
_network_join . server_password = join_server_password ;
_network_join . company_password = join_company_password ;
2004-09-10 21:02:27 +02:00
2021-05-01 14:41:25 +02:00
if ( _game_mode = = GM_MENU ) {
/* From the menu we can immediately continue with the actual join. */
NetworkClientJoinGame ( ) ;
} else {
/* When already playing a game, first go back to the main menu. This
* disconnects the user from the current game , meaning we can safely
* load in the new . After all , there is little point in continueing to
* play on a server if we are connecting to another one .
*/
_switch_mode = SM_JOIN_GAME ;
}
return true ;
}
/**
* Actually perform the joining to the server . Use # NetworkClientConnectGame
* when you want to connect to a specific server / company . This function
* assumes _network_join is already fully set up .
*/
void NetworkClientJoinGame ( )
{
2004-12-04 18:54:56 +01:00
NetworkDisconnect ( ) ;
NetworkInitialize ( ) ;
2004-08-09 19:04:08 +02:00
2021-04-27 21:10:11 +02:00
_settings_client . network . last_joined = _network_join . connection_string ;
2009-01-20 12:28:18 +01:00
_network_join_status = NETWORK_JOIN_STATUS_CONNECTING ;
ShowJoinStatusWindow ( ) ;
2004-08-22 12:23:37 +02:00
2021-05-05 23:21:14 +02:00
new TCPClientConnecter ( _network_join . connection_string ) ;
2004-08-09 19:04:08 +02:00
}
2007-03-07 12:47:46 +01:00
static void NetworkInitGameInfo ( )
2004-09-12 16:12:33 +02:00
{
2021-05-05 19:21:12 +02:00
FillStaticNetworkServerGameInfo ( ) ;
2008-06-03 10:04:35 +02:00
/* The server is a client too */
_network_game_info . clients_on = _network_dedicated ? 0 : 1 ;
2004-09-10 21:02:27 +02:00
2011-02-08 22:47:10 +01:00
/* There should be always space for the server. */
assert ( NetworkClientInfo : : CanAllocateItem ( ) ) ;
2008-12-23 21:52:27 +01:00
NetworkClientInfo * ci = new NetworkClientInfo ( CLIENT_ID_SERVER ) ;
2014-05-11 15:07:36 +02:00
ci - > client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST ;
2006-10-18 00:16:46 +02:00
2021-05-15 08:31:45 +02:00
ci - > client_name = _settings_client . network . client_name ;
2004-08-09 19:04:08 +02:00
}
2021-05-24 12:13:54 +02:00
/**
* Trim the given server name in place , i . e . remove leading and trailing spaces .
* After the trim check whether the server name is not empty .
* When the server name is empty a GUI error message is shown telling the
* user to set the servername and this function returns false .
*
* @ param server_name The server name to validate . It will be trimmed of leading
* and trailing spaces .
* @ return True iff the server name is valid .
*/
bool NetworkValidateServerName ( std : : string & server_name )
{
StrTrimInPlace ( server_name ) ;
if ( ! server_name . empty ( ) ) return true ;
ShowErrorMessage ( STR_NETWORK_ERROR_BAD_SERVER_NAME , INVALID_STRING_ID , WL_ERROR ) ;
return false ;
}
2021-05-11 18:53:23 +02:00
/**
* Check whether the client and server name are set , for a dedicated server and if not set them to some default
* value and tell the user to change this as soon as possible .
* If the saved name is the default value , then the user is told to override this value too .
* This is only meant dedicated servers , as for the other servers the GUI ensures a name has been entered .
*/
static void CheckClientAndServerName ( )
{
2021-04-27 22:02:40 +02:00
static const std : : string fallback_client_name = " Unnamed Client " ;
2021-05-24 12:13:54 +02:00
StrTrimInPlace ( _settings_client . network . client_name ) ;
2021-04-27 22:02:40 +02:00
if ( _settings_client . network . client_name . empty ( ) | | _settings_client . network . client_name . compare ( fallback_client_name ) = = 0 ) {
2021-06-12 09:10:17 +02:00
Debug ( net , 1 , " No \" client_name \" has been set, using \" {} \" instead. Please set this now using the \" name <new name> \" command " , fallback_client_name ) ;
2021-04-27 22:02:40 +02:00
_settings_client . network . client_name = fallback_client_name ;
2021-05-11 18:53:23 +02:00
}
2021-04-27 21:25:52 +02:00
static const std : : string fallback_server_name = " Unnamed Server " ;
2021-05-24 12:13:54 +02:00
StrTrimInPlace ( _settings_client . network . server_name ) ;
2021-04-27 21:25:52 +02:00
if ( _settings_client . network . server_name . empty ( ) | | _settings_client . network . server_name . compare ( fallback_server_name ) = = 0 ) {
2021-06-12 09:10:17 +02:00
Debug ( net , 1 , " No \" server_name \" has been set, using \" {} \" instead. Please set this now using the \" server_name <new name> \" command " , fallback_server_name ) ;
2021-04-27 21:25:52 +02:00
_settings_client . network . server_name = fallback_server_name ;
2021-05-11 18:53:23 +02:00
}
}
2007-03-07 12:47:46 +01:00
bool NetworkServerStart ( )
2004-09-12 16:12:33 +02:00
{
2004-12-04 18:54:56 +01:00
if ( ! _network_available ) return false ;
2004-09-10 21:02:27 +02:00
2004-12-13 18:47:21 +01:00
/* Call the pre-scripts */
IConsoleCmdExec ( " exec scripts/pre_server.scr 0 " ) ;
if ( _network_dedicated ) IConsoleCmdExec ( " exec scripts/pre_dedicated.scr 0 " ) ;
2021-05-11 18:53:23 +02:00
/* Check for the client and server names to be set, but only after the scripts had a chance to set them.*/
if ( _network_dedicated ) CheckClientAndServerName ( ) ;
2010-10-17 19:35:42 +02:00
NetworkDisconnect ( false , false ) ;
NetworkInitialize ( false ) ;
2021-07-11 22:37:14 +02:00
NetworkUDPInitialize ( ) ;
2021-06-12 09:10:17 +02:00
Debug ( net , 5 , " Starting listeners for clients " ) ;
2010-10-15 23:56:06 +02:00
if ( ! ServerNetworkGameSocketHandler : : Listen ( _settings_client . network . server_port ) ) return false ;
2004-08-09 19:04:08 +02:00
2010-10-17 19:35:42 +02:00
/* Only listen for admins when the password isn't empty. */
2021-04-27 20:26:56 +02:00
if ( ! _settings_client . network . admin_password . empty ( ) ) {
2021-06-12 09:10:17 +02:00
Debug ( net , 5 , " Starting listeners for admins " ) ;
2013-11-14 20:31:10 +01:00
if ( ! ServerNetworkAdminSocketHandler : : Listen ( _settings_client . network . server_admin_port ) ) return false ;
}
2010-10-17 19:35:42 +02:00
2009-03-15 01:32:18 +01:00
/* Try to start UDP-server */
2021-06-12 09:10:17 +02:00
Debug ( net , 5 , " Starting listeners for incoming server queries " ) ;
2021-04-11 11:22:50 +02:00
NetworkUDPServerListen ( ) ;
2004-08-22 12:23:37 +02:00
2021-05-02 08:27:06 +02:00
_network_company_states = new NetworkCompanyState [ MAX_COMPANIES ] ;
2004-12-04 18:54:56 +01:00
_network_server = true ;
_networking = true ;
_frame_counter = 0 ;
_frame_counter_server = 0 ;
_frame_counter_max = 0 ;
2005-07-16 14:59:23 +02:00
_last_sync_frame = 0 ;
2008-12-22 13:59:31 +01:00
_network_own_client_id = CLIENT_ID_SERVER ;
2004-09-10 21:02:27 +02:00
2004-12-04 18:54:56 +01:00
_network_clients_connected = 0 ;
2009-09-22 23:14:31 +02:00
_network_company_passworded = 0 ;
2004-08-22 12:23:37 +02:00
2004-12-04 18:54:56 +01:00
NetworkInitGameInfo ( ) ;
2004-09-10 21:02:27 +02:00
2021-07-11 21:57:05 +02:00
if ( _settings_client . network . server_game_type ! = SERVER_GAME_TYPE_LOCAL ) {
2021-07-03 11:04:32 +02:00
_network_coordinator_client . Register ( ) ;
}
2009-03-15 01:32:18 +01:00
/* execute server initialization script */
2004-12-04 18:54:56 +01:00
IConsoleCmdExec ( " exec scripts/on_server.scr 0 " ) ;
2009-03-15 01:32:18 +01:00
/* if the server is dedicated ... add some other script */
2004-12-04 18:54:56 +01:00
if ( _network_dedicated ) IConsoleCmdExec ( " exec scripts/on_dedicated.scr 0 " ) ;
2004-12-15 21:10:34 +01:00
2010-10-17 19:35:42 +02:00
/* welcome possibly still connected admins - this can only happen on a dedicated server. */
if ( _network_dedicated ) ServerNetworkAdminSocketHandler : : WelcomeAll ( ) ;
2004-12-04 18:54:56 +01:00
return true ;
2004-08-09 19:04:08 +02:00
}
2009-03-15 01:32:18 +01:00
/* The server is rebooting...
* The only difference with NetworkDisconnect , is the packets that is sent */
2007-03-07 12:47:46 +01:00
void NetworkReboot ( )
2004-09-12 16:12:33 +02:00
{
2004-12-04 18:54:56 +01:00
if ( _network_server ) {
2019-12-16 20:56:10 +01:00
for ( NetworkClientSocket * cs : NetworkClientSocket : : Iterate ( ) ) {
2010-10-15 22:25:07 +02:00
cs - > SendNewGame ( ) ;
2010-11-30 14:38:46 +01:00
cs - > SendPackets ( ) ;
2004-08-22 12:23:37 +02:00
}
2010-10-17 19:35:42 +02:00
2019-12-16 20:56:10 +01:00
for ( ServerNetworkAdminSocketHandler * as : ServerNetworkAdminSocketHandler : : IterateActive ( ) ) {
2010-10-17 19:35:42 +02:00
as - > SendNewGame ( ) ;
2010-11-30 14:38:46 +01:00
as - > SendPackets ( ) ;
2010-10-17 19:35:42 +02:00
}
2004-12-04 18:54:56 +01:00
}
2004-08-22 12:23:37 +02:00
2010-10-17 19:35:42 +02:00
/* For non-dedicated servers we have to kick the admins as we are not
* certain that we will end up in a new network game . */
NetworkClose ( ! _network_dedicated ) ;
2004-08-22 12:23:37 +02:00
}
2009-04-10 22:18:48 +02:00
/**
* We want to disconnect from the host / clients .
2010-10-17 19:35:42 +02:00
* @ param blocking whether to wait till everything has been closed .
* @ param close_admins Whether the admin sockets need to be closed as well .
2009-04-10 22:18:48 +02:00
*/
2010-10-17 19:35:42 +02:00
void NetworkDisconnect ( bool blocking , bool close_admins )
2004-09-12 16:12:33 +02:00
{
2004-12-04 18:54:56 +01:00
if ( _network_server ) {
2019-12-16 20:56:10 +01:00
for ( NetworkClientSocket * cs : NetworkClientSocket : : Iterate ( ) ) {
2010-10-15 22:25:07 +02:00
cs - > SendShutdown ( ) ;
2010-11-30 14:38:46 +01:00
cs - > SendPackets ( ) ;
2004-12-04 18:54:56 +01:00
}
2010-10-17 19:35:42 +02:00
if ( close_admins ) {
2019-12-16 20:56:10 +01:00
for ( ServerNetworkAdminSocketHandler * as : ServerNetworkAdminSocketHandler : : IterateActive ( ) ) {
2010-10-17 19:35:42 +02:00
as - > SendShutdown ( ) ;
2010-11-30 14:38:46 +01:00
as - > SendPackets ( ) ;
2010-10-17 19:35:42 +02:00
}
}
2004-09-12 16:12:33 +02:00
}
2004-08-27 13:01:26 +02:00
2021-05-17 15:46:38 +02:00
CloseWindowById ( WC_NETWORK_STATUS_WINDOW , WN_NETWORK_STATUS_WINDOW_JOIN ) ;
2004-12-12 14:46:10 +01:00
2010-10-17 19:35:42 +02:00
NetworkClose ( close_admins ) ;
2010-08-15 13:58:20 +02:00
/* Reinitialize the UDP stack, i.e. close all existing connections. */
NetworkUDPInitialize ( ) ;
2004-12-04 18:54:56 +01:00
}
2004-08-22 12:23:37 +02:00
2021-07-11 21:57:05 +02:00
/**
* The setting server_game_type was updated ; possibly we need to take some
* action .
*/
void NetworkUpdateServerGameType ( )
{
if ( ! _networking ) return ;
switch ( _settings_client . network . server_game_type ) {
case SERVER_GAME_TYPE_LOCAL :
_network_coordinator_client . CloseConnection ( ) ;
break ;
case SERVER_GAME_TYPE_INVITE_ONLY :
case SERVER_GAME_TYPE_PUBLIC :
_network_coordinator_client . Register ( ) ;
break ;
default :
NOT_REACHED ( ) ;
}
}
2010-05-20 20:54:14 +02:00
/**
* Receives something from the network .
2013-01-08 23:46:42 +01:00
* @ return true if everything went fine , false when the connection got closed .
2010-05-20 20:54:14 +02:00
*/
2007-03-07 12:47:46 +01:00
static bool NetworkReceive ( )
2004-12-04 18:54:56 +01:00
{
2010-10-15 23:56:06 +02:00
if ( _network_server ) {
2010-10-17 19:35:42 +02:00
ServerNetworkAdminSocketHandler : : Receive ( ) ;
2010-10-15 23:56:06 +02:00
return ServerNetworkGameSocketHandler : : Receive ( ) ;
} else {
2010-10-15 21:33:08 +02:00
return ClientNetworkGameSocketHandler : : Receive ( ) ;
}
2004-08-22 12:23:37 +02:00
}
2009-03-15 01:32:18 +01:00
/* This sends all buffered commands (if possible) */
2007-03-07 12:47:46 +01:00
static void NetworkSend ( )
2004-09-13 01:35:01 +02:00
{
2010-10-15 23:56:06 +02:00
if ( _network_server ) {
2010-10-17 19:35:42 +02:00
ServerNetworkAdminSocketHandler : : Send ( ) ;
2010-10-15 23:56:06 +02:00
ServerNetworkGameSocketHandler : : Send ( ) ;
} else {
2010-10-15 21:33:08 +02:00
ClientNetworkGameSocketHandler : : Send ( ) ;
2004-09-13 01:35:01 +02:00
}
}
2012-01-04 23:08:43 +01:00
/**
* We have to do some ( simple ) background stuff that runs normally ,
* even when we are not in multiplayer . For example stuff needed
* for finding servers or downloading content .
*/
void NetworkBackgroundLoop ( )
2004-09-12 16:12:33 +02:00
{
2009-01-20 17:51:55 +01:00
_network_content_client . SendReceive ( ) ;
2021-07-03 11:04:32 +02:00
_network_coordinator_client . SendReceive ( ) ;
2009-01-20 12:28:18 +01:00
TCPConnecter : : CheckCallbacks ( ) ;
2010-02-03 19:42:23 +01:00
NetworkHTTPSocketHandler : : HTTPReceive ( ) ;
2021-08-23 19:37:51 +02:00
QueryNetworkGameSocketHandler : : SendReceive ( ) ;
2009-01-17 17:53:32 +01:00
2012-01-04 23:08:43 +01:00
NetworkBackgroundUDPLoop ( ) ;
2004-08-22 12:23:37 +02:00
}
2009-03-15 01:32:18 +01:00
/* The main loop called from ttd.c
* Here we also have to do StateGameLoop if needed ! */
2007-03-07 12:47:46 +01:00
void NetworkGameLoop ( )
2004-09-12 16:12:33 +02:00
{
2004-12-04 18:54:56 +01:00
if ( ! _networking ) return ;
2004-08-22 12:23:37 +02:00
2004-12-04 18:54:56 +01:00
if ( ! NetworkReceive ( ) ) return ;
2004-08-22 12:23:37 +02:00
2004-12-04 18:54:56 +01:00
if ( _network_server ) {
2010-04-11 11:54:44 +02:00
/* Log the sync state to check for in-syncedness of replays. */
2023-04-24 17:56:01 +02:00
if ( TimerGameCalendar : : date_fract = = 0 ) {
2010-04-13 19:36:19 +02:00
/* We don't want to log multiple times if paused. */
2023-04-24 20:33:18 +02:00
static TimerGameCalendar : : Date last_log ;
2023-04-24 17:56:01 +02:00
if ( last_log ! = TimerGameCalendar : : date ) {
Debug ( desync , 1 , " sync: {:08x}; {:02x}; {:08x}; {:08x} " , TimerGameCalendar : : date , TimerGameCalendar : : date_fract , _random . state [ 0 ] , _random . state [ 1 ] ) ;
last_log = TimerGameCalendar : : date ;
2010-04-13 19:36:19 +02:00
}
}
2010-04-11 11:54:44 +02:00
2007-07-07 12:06:10 +02:00
# ifdef DEBUG_DUMP_COMMANDS
2010-04-09 19:08:44 +02:00
/* Loading of the debug commands from -ddesync>=1 */
2007-07-07 12:06:10 +02:00
static FILE * f = FioFOpenFile ( " commands.log " , " rb " , SAVE_DIR ) ;
static Date next_date = 0 ;
static uint32 next_date_fract ;
2019-04-10 23:07:06 +02:00
static CommandPacket * cp = nullptr ;
2010-04-13 19:36:19 +02:00
static bool check_sync_state = false ;
static uint32 sync_state [ 2 ] ;
2019-04-10 23:07:06 +02:00
if ( f = = nullptr & & next_date = = 0 ) {
2021-06-12 09:10:17 +02:00
Debug ( desync , 0 , " Cannot open commands.log " ) ;
2007-07-07 12:06:10 +02:00
next_date = 1 ;
}
2019-04-10 23:07:06 +02:00
while ( f ! = nullptr & & ! feof ( f ) ) {
2023-04-24 17:56:01 +02:00
if ( TimerGameCalendar : : date = = next_date & & TimerGameCalendar : : date_fract = = next_date_fract ) {
2019-04-10 23:07:06 +02:00
if ( cp ! = nullptr ) {
2021-10-28 23:48:26 +02:00
NetworkSendCommand ( cp - > cmd , cp - > err_msg , nullptr , cp - > company , cp - > data ) ;
2023-04-24 17:56:01 +02:00
Debug ( desync , 0 , " Injecting: {:08x}; {:02x}; {:02x}; {:08x}; {:06x}; {} ({}) " , TimerGameCalendar : : date , TimerGameCalendar : : date_fract , ( int ) _current_company , cp - > cmd , cp - > tile , FormatArrayAsHex ( cp - > data ) , GetCommandName ( cp - > cmd ) ) ;
2021-06-03 22:07:44 +02:00
delete cp ;
2019-04-10 23:07:06 +02:00
cp = nullptr ;
2010-04-13 19:36:19 +02:00
}
if ( check_sync_state ) {
if ( sync_state [ 0 ] = = _random . state [ 0 ] & & sync_state [ 1 ] = = _random . state [ 1 ] ) {
2023-04-24 17:56:01 +02:00
Debug ( desync , 0 , " Sync check: {:08x}; {:02x}; match " , TimerGameCalendar : : date , TimerGameCalendar : : date_fract ) ;
2010-04-13 19:36:19 +02:00
} else {
2021-11-06 18:59:52 +01:00
Debug ( desync , 0 , " Sync check: {:08x}; {:02x}; mismatch expected {{{:08x}, {:08x}}}, got {{{:08x}, {:08x}}} " ,
2023-04-24 17:56:01 +02:00
TimerGameCalendar : : date , TimerGameCalendar : : date_fract , sync_state [ 0 ] , sync_state [ 1 ] , _random . state [ 0 ] , _random . state [ 1 ] ) ;
2010-04-13 19:36:19 +02:00
NOT_REACHED ( ) ;
}
check_sync_state = false ;
}
2007-07-07 12:06:10 +02:00
}
2019-04-10 23:07:06 +02:00
if ( cp ! = nullptr | | check_sync_state ) break ;
2007-07-07 12:06:10 +02:00
char buff [ 4096 ] ;
2019-04-10 23:07:06 +02:00
if ( fgets ( buff , lengthof ( buff ) , f ) = = nullptr ) break ;
2010-04-09 19:08:44 +02:00
char * p = buff ;
/* Ignore the "[date time] " part of the message */
if ( * p = = ' [ ' ) {
p = strchr ( p , ' ] ' ) ;
2019-04-10 23:07:06 +02:00
if ( p = = nullptr ) break ;
2010-04-09 19:08:44 +02:00
p + = 2 ;
}
2014-09-07 16:21:16 +02:00
if ( strncmp ( p , " cmd: " , 5 ) = = 0
# ifdef DEBUG_FAILED_DUMP_COMMANDS
| | strncmp ( p , " cmdf: " , 6 ) = = 0
# endif
) {
2014-09-07 15:36:18 +02:00
p + = 5 ;
if ( * p = = ' ' ) p + + ;
2021-05-29 16:16:51 +02:00
cp = new CommandPacket ( ) ;
2010-04-09 19:08:44 +02:00
int company ;
2021-10-03 21:13:32 +02:00
uint cmd ;
2021-10-28 23:48:26 +02:00
char buffer [ 256 ] ;
int ret = sscanf ( p , " %x; %x; %x; %x; %x; %x; %255s " , & next_date , & next_date_fract , & company , & cmd , & cp - > err_msg , & cp - > tile , buffer ) ;
assert ( ret = = 6 ) ;
2010-04-09 19:08:44 +02:00
cp - > company = ( CompanyID ) company ;
2021-10-03 21:13:32 +02:00
cp - > cmd = ( Commands ) cmd ;
2021-10-28 23:48:26 +02:00
/* Parse command data. */
std : : vector < byte > args ;
size_t arg_len = strlen ( buffer ) ;
for ( size_t i = 0 ; i + 1 < arg_len ; i + = 2 ) {
byte e = 0 ;
std : : from_chars ( buffer + i , buffer + i + 1 , e , 16 ) ;
args . emplace_back ( e ) ;
}
cp - > data = args ;
2010-04-09 19:08:44 +02:00
} else if ( strncmp ( p , " join: " , 6 ) = = 0 ) {
/* Manually insert a pause when joining; this way the client can join at the exact right time. */
int ret = sscanf ( p + 6 , " %x; %x " , & next_date , & next_date_fract ) ;
assert ( ret = = 2 ) ;
2021-06-12 09:10:17 +02:00
Debug ( desync , 0 , " Injecting pause for join at {:08x}:{:02x}; please join when paused " , next_date , next_date_fract ) ;
2021-05-29 16:16:51 +02:00
cp = new CommandPacket ( ) ;
2010-04-09 19:08:44 +02:00
cp - > company = COMPANY_SPECTATOR ;
cp - > cmd = CMD_PAUSE ;
2021-11-23 01:05:58 +01:00
cp - > data = EndianBufferWriter < > : : FromValue ( CommandTraits < CMD_PAUSE > : : Args { PM_PAUSED_NORMAL , true } ) ;
2010-04-09 19:08:44 +02:00
_ddc_fastforward = false ;
2010-04-13 19:36:19 +02:00
} else if ( strncmp ( p , " sync: " , 6 ) = = 0 ) {
int ret = sscanf ( p + 6 , " %x; %x; %x; %x " , & next_date , & next_date_fract , & sync_state [ 0 ] , & sync_state [ 1 ] ) ;
assert ( ret = = 4 ) ;
check_sync_state = true ;
} else if ( strncmp ( p , " msg: " , 5 ) = = 0 | | strncmp ( p , " client: " , 8 ) = = 0 | |
strncmp ( p , " load: " , 6 ) = = 0 | | strncmp ( p , " save: " , 6 ) = = 0 ) {
/* A message that is not very important to the log playback, but part of the log. */
2014-09-07 16:21:16 +02:00
# ifndef DEBUG_FAILED_DUMP_COMMANDS
} else if ( strncmp ( p , " cmdf: " , 6 ) = = 0 ) {
2021-06-12 09:10:17 +02:00
Debug ( desync , 0 , " Skipping replay of failed command: {} " , p + 6 ) ;
2014-09-07 16:21:16 +02:00
# endif
2010-04-13 19:36:19 +02:00
} else {
/* Can't parse a line; what's wrong here? */
2021-06-12 09:10:17 +02:00
Debug ( desync , 0 , " Trying to parse: {} " , p ) ;
2010-04-13 19:36:19 +02:00
NOT_REACHED ( ) ;
2010-04-09 19:08:44 +02:00
}
}
2019-04-10 23:07:06 +02:00
if ( f ! = nullptr & & feof ( f ) ) {
2021-06-12 09:10:17 +02:00
Debug ( desync , 0 , " End of commands.log " ) ;
2010-04-09 19:08:44 +02:00
fclose ( f ) ;
2019-04-10 23:07:06 +02:00
f = nullptr ;
2007-07-07 12:06:10 +02:00
}
2008-01-11 01:30:32 +01:00
# endif /* DEBUG_DUMP_COMMANDS */
2009-09-30 15:32:39 +02:00
if ( _frame_counter > = _frame_counter_max ) {
/* Only check for active clients just before we're going to send out
* the commands so we don ' t send multiple pause / unpause commands when
2010-08-19 10:59:36 +02:00
* the frame_freq is more than 1 tick . Same with distributing commands . */
2009-11-12 21:52:14 +01:00
CheckPauseOnJoin ( ) ;
2009-09-30 15:32:39 +02:00
CheckMinActiveClients ( ) ;
2010-08-19 10:59:36 +02:00
NetworkDistributeCommands ( ) ;
2009-09-30 15:32:39 +02:00
}
2007-07-07 12:06:10 +02:00
2005-07-29 23:55:49 +02:00
bool send_frame = false ;
2009-03-15 01:32:18 +01:00
/* We first increase the _frame_counter */
2004-12-04 18:54:56 +01:00
_frame_counter + + ;
2009-03-15 01:32:18 +01:00
/* Update max-frame-counter */
2005-07-29 23:55:49 +02:00
if ( _frame_counter > _frame_counter_max ) {
2008-05-29 22:21:28 +02:00
_frame_counter_max = _frame_counter + _settings_client . network . frame_freq ;
2005-07-29 23:55:49 +02:00
send_frame = true ;
}
2004-08-22 12:23:37 +02:00
2009-01-08 14:57:50 +01:00
NetworkExecuteLocalCommandQueue ( ) ;
2004-08-22 12:23:37 +02:00
2009-03-15 01:32:18 +01:00
/* Then we make the frame */
2004-12-04 18:54:56 +01:00
StateGameLoop ( ) ;
2004-09-12 16:12:33 +02:00
2008-01-29 01:27:25 +01:00
_sync_seed_1 = _random . state [ 0 ] ;
2004-12-04 18:54:56 +01:00
# ifdef NETWORK_SEND_DOUBLE_SEED
2008-01-29 01:27:25 +01:00
_sync_seed_2 = _random . state [ 1 ] ;
2004-12-04 18:54:56 +01:00
# endif
2004-08-22 12:23:37 +02:00
2005-07-29 23:55:49 +02:00
NetworkServer_Tick ( send_frame ) ;
2004-12-04 18:54:56 +01:00
} else {
2009-03-15 01:32:18 +01:00
/* Client */
2004-08-22 12:23:37 +02:00
2009-03-15 01:32:18 +01:00
/* Make sure we are at the frame were the server is (quick-frames) */
2004-12-04 18:54:56 +01:00
if ( _frame_counter_server > _frame_counter ) {
2010-11-24 00:59:24 +01:00
/* Run a number of frames; when things go bad, get out. */
2004-12-04 18:54:56 +01:00
while ( _frame_counter_server > _frame_counter ) {
2010-11-24 00:59:24 +01:00
if ( ! ClientNetworkGameSocketHandler : : GameLoop ( ) ) return ;
2004-12-04 18:54:56 +01:00
}
} else {
2009-03-15 01:32:18 +01:00
/* Else, keep on going till _frame_counter_max */
2010-11-24 00:59:24 +01:00
if ( _frame_counter_max > _frame_counter ) {
/* Run one frame; if things went bad, get out. */
if ( ! ClientNetworkGameSocketHandler : : GameLoop ( ) ) return ;
}
2004-12-04 18:54:56 +01:00
}
2004-08-22 12:23:37 +02:00
}
2004-12-04 18:54:56 +01:00
NetworkSend ( ) ;
2004-08-22 12:23:37 +02:00
}
2010-01-21 12:17:40 +01:00
static void NetworkGenerateServerId ( )
2004-12-12 17:04:32 +01:00
{
2023-04-25 19:43:45 +02:00
_settings_client . network . network_id = GenerateUid ( " OpenTTD Server ID " ) ;
2004-12-12 17:04:32 +01:00
}
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
class TCPNetworkDebugConnecter : TCPConnecter {
2021-05-08 10:53:21 +02:00
private :
std : : string connection_string ;
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
public :
2021-05-08 10:53:21 +02:00
TCPNetworkDebugConnecter ( const std : : string & connection_string ) : TCPConnecter ( connection_string , NETWORK_DEFAULT_DEBUGLOG_PORT ) , connection_string ( connection_string ) { }
2007-02-08 13:27:53 +01:00
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
void OnFailure ( ) override
{
2021-06-12 09:10:17 +02:00
Debug ( net , 0 , " Failed to open connection to {} for redirecting Debug() " , this - > connection_string ) ;
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
}
2021-04-29 16:43:13 +02:00
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
void OnConnect ( SOCKET s ) override
{
2021-06-12 09:10:17 +02:00
Debug ( net , 3 , " Redirecting Debug() to {} " , this - > connection_string ) ;
2007-02-08 13:27:53 +01:00
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
extern SOCKET _debug_socket ;
_debug_socket = s ;
2007-02-08 13:27:53 +01:00
}
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
} ;
2007-02-08 13:27:53 +01:00
Feature: use Happy Eyeballs to make network connections (TCP-only) (#9199)
Hostnames like "content.openttd.org" resolve into multiple IPv4 and IPv6.
It is possible that either of the IPs is not working, either due to
a poorly configured OS (having IPv6 but no valid route), broken network
paths, or a service that is temporary unavailable.
Instead of trying the IPs one by one, waiting for a 3s timeout between
each, be a bit more like browsers, and stack attempts on top of each
other with slight delays. This is called Happy Eyebells.
Initially, try the first IPv6 address. If within 250ms there is no
connection yet, try the first IPv4 address. 250ms later, try the
second IPv6 address, etc, till all addresses are tried.
If any connection is created, abort all the other (pending) connections
and use the one that is created. If all fail 3s after the last connect(),
trigger a timeout for all.
2021-05-06 23:13:35 +02:00
void NetworkStartDebugLog ( const std : : string & connection_string )
{
new TCPNetworkDebugConnecter ( connection_string ) ;
2007-02-08 13:27:53 +01:00
}
2007-01-04 20:12:45 +01:00
/** This tries to launch the network for a given OS */
2007-03-07 12:47:46 +01:00
void NetworkStartUp ( )
2004-09-12 16:12:33 +02:00
{
2021-06-12 09:10:17 +02:00
Debug ( net , 3 , " Starting network " ) ;
2004-12-22 22:12:36 +01:00
2007-01-04 20:12:45 +01:00
/* Network is available */
2010-02-14 17:31:35 +01:00
_network_available = NetworkCoreInitialize ( ) ;
2004-12-04 18:54:56 +01:00
_network_dedicated = false ;
2004-08-22 12:23:37 +02:00
2010-01-21 12:17:40 +01:00
/* Generate an server id when there is none yet */
2021-04-27 21:25:52 +02:00
if ( _settings_client . network . network_id . empty ( ) ) NetworkGenerateServerId ( ) ;
2004-12-12 17:04:32 +01:00
2021-05-06 16:19:00 +02:00
_network_game_info = { } ;
2004-08-22 12:23:37 +02:00
2004-12-04 18:54:56 +01:00
NetworkInitialize ( ) ;
2021-07-11 22:37:14 +02:00
NetworkUDPInitialize ( ) ;
2021-06-12 09:10:17 +02:00
Debug ( net , 3 , " Network online, multiplayer available " ) ;
2009-04-07 21:06:36 +02:00
NetworkFindBroadcastIPs ( & _broadcast_list ) ;
2023-02-12 12:07:31 +01:00
NetworkHTTPInitialize ( ) ;
2004-08-22 12:23:37 +02:00
}
2007-01-04 20:12:45 +01:00
/** This shuts the network down */
2007-03-07 12:47:46 +01:00
void NetworkShutDown ( )
2004-09-12 16:12:33 +02:00
{
2009-04-10 22:18:48 +02:00
NetworkDisconnect ( true ) ;
2023-02-12 12:07:31 +01:00
NetworkHTTPUninitialize ( ) ;
2009-04-08 23:03:00 +02:00
NetworkUDPClose ( ) ;
2006-10-12 16:48:36 +02:00
2021-06-12 09:10:17 +02:00
Debug ( net , 3 , " Shutting down network " ) ;
2004-08-22 12:23:37 +02:00
2004-12-04 18:54:56 +01:00
_network_available = false ;
2004-08-22 12:23:37 +02:00
2007-01-04 19:50:40 +01:00
NetworkCoreShutdown ( ) ;
2004-08-22 12:23:37 +02:00
}
2020-12-05 21:57:47 +01:00
# ifdef __EMSCRIPTEN__
extern " C " {
2021-04-30 11:34:47 +02:00
void CDECL em_openttd_add_server ( const char * connection_string )
2020-12-05 21:57:47 +01:00
{
2021-04-29 16:30:42 +02:00
NetworkAddServer ( connection_string , false , true ) ;
2020-12-05 21:57:47 +01:00
}
}
# endif