2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file network.cpp Base functions for networking support. */
10 #include "../stdafx.h"
12 #include "../strings_func.h"
13 #include "../command_func.h"
14 #include "../timer/timer_game_tick.h"
15 #include "../timer/timer_game_economy.h"
16 #include "network_admin.h"
17 #include "network_client.h"
18 #include "network_query.h"
19 #include "network_server.h"
20 #include "network_content.h"
21 #include "network_udp.h"
22 #include "network_gamelist.h"
23 #include "network_base.h"
24 #include "network_coordinator.h"
26 #include "core/host.h"
27 #include "network_gui.h"
28 #include "../console_func.h"
29 #include "../3rdparty/md5/md5.h"
30 #include "../core/random_func.hpp"
31 #include "../window_func.h"
32 #include "../company_func.h"
33 #include "../company_base.h"
34 #include "../landscape_type.h"
36 #include "../core/pool_func.hpp"
37 #include "../gfx_func.h"
39 #include "../misc_cmd.h"
44 #include "../safeguards.h"
46 #ifdef DEBUG_DUMP_COMMANDS
47 #include "../fileio_func.h"
48 /** When running the server till the wait point, run as fast as we can! */
49 bool _ddc_fastforward
= true;
50 #endif /* DEBUG_DUMP_COMMANDS */
52 /** Make sure both pools have the same size. */
53 static_assert(NetworkClientInfoPool::MAX_SIZE
== NetworkClientSocketPool::MAX_SIZE
);
55 /** The pool with client information. */
56 NetworkClientInfoPool
_networkclientinfo_pool("NetworkClientInfo");
57 INSTANTIATE_POOL_METHODS(NetworkClientInfo
)
59 bool _networking
; ///< are we in networking mode?
60 bool _network_server
; ///< network-server is active
61 bool _network_available
; ///< is network mode available?
62 bool _network_dedicated
; ///< are we a dedicated server?
63 bool _is_network_server
; ///< Does this client wants to be a network-server?
64 NetworkCompanyState
*_network_company_states
= nullptr; ///< Statistics about some companies.
65 ClientID _network_own_client_id
; ///< Our client identifier.
66 ClientID _redirect_console_to_client
; ///< If not invalid, redirect the console output to a client.
67 uint8_t _network_reconnect
; ///< Reconnect timeout
68 StringList _network_bind_list
; ///< The addresses to bind on.
69 StringList _network_host_list
; ///< The servers we know.
70 StringList _network_ban_list
; ///< The banned clients.
71 uint32_t _frame_counter_server
; ///< The frame_counter of the server, if in network-mode
72 uint32_t _frame_counter_max
; ///< To where we may go with our clients
73 uint32_t _frame_counter
; ///< The current frame.
74 uint32_t _last_sync_frame
; ///< Used in the server to store the last time a sync packet was sent to clients.
75 NetworkAddressList _broadcast_list
; ///< List of broadcast addresses.
76 uint32_t _sync_seed_1
; ///< Seed to compare during sync checks.
77 #ifdef NETWORK_SEND_DOUBLE_SEED
78 uint32_t _sync_seed_2
; ///< Second part of the seed.
80 uint32_t _sync_frame
; ///< The frame to perform the sync check.
81 bool _network_first_time
; ///< Whether we have finished joining or not.
82 CompanyMask _network_company_passworded
; ///< Bitmask of the password status of all companies.
84 static_assert((int)NETWORK_COMPANY_NAME_LENGTH
== MAX_LENGTH_COMPANY_NAME_CHARS
* MAX_CHAR_LENGTH
);
86 /** The amount of clients connected */
87 byte _network_clients_connected
= 0;
89 extern std::string
GenerateUid(std::string_view subject
);
92 * Return whether there is any client connected or trying to connect at all.
93 * @return whether we have any client activity
97 return !NetworkClientSocket::Iterate().empty();
101 * Basically a client is leaving us right now.
103 NetworkClientInfo::~NetworkClientInfo()
105 /* Delete the chat window, if you were chatting with this client. */
106 InvalidateWindowData(WC_SEND_NETWORK_MSG
, DESTTYPE_CLIENT
, this->client_id
);
110 * Return the CI given it's client-identifier
111 * @param client_id the ClientID to search for
112 * @return return a pointer to the corresponding NetworkClientInfo struct or nullptr when not found
114 /* static */ NetworkClientInfo
*NetworkClientInfo::GetByClientID(ClientID client_id
)
116 for (NetworkClientInfo
*ci
: NetworkClientInfo::Iterate()) {
117 if (ci
->client_id
== client_id
) return ci
;
124 * Return the client state given it's client-identifier
125 * @param client_id the ClientID to search for
126 * @return return a pointer to the corresponding NetworkClientSocket struct or nullptr when not found
128 /* static */ ServerNetworkGameSocketHandler
*ServerNetworkGameSocketHandler::GetByClientID(ClientID client_id
)
130 for (NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
131 if (cs
->client_id
== client_id
) return cs
;
137 byte
NetworkSpectatorCount()
141 for (const NetworkClientInfo
*ci
: NetworkClientInfo::Iterate()) {
142 if (ci
->client_playas
== COMPANY_SPECTATOR
) count
++;
145 /* Don't count a dedicated server as spectator */
146 if (_network_dedicated
) count
--;
152 * Change the company password of a given company.
153 * @param company_id ID of the company the password should be changed for.
154 * @param password The unhashed password we like to set ('*' or '' resets the password)
155 * @return The password.
157 std::string
NetworkChangeCompanyPassword(CompanyID company_id
, std::string password
)
159 if (password
.compare("*") == 0) password
= "";
161 if (_network_server
) {
162 NetworkServerSetCompanyPassword(company_id
, password
, false);
164 NetworkClientSetCompanyPassword(password
);
171 * Hash the given password using server ID and game seed.
172 * @param password Password to hash.
173 * @param password_server_id Server ID.
174 * @param password_game_seed Game seed.
175 * @return The hashed password.
177 std::string
GenerateCompanyPasswordHash(const std::string
&password
, const std::string
&password_server_id
, uint32_t password_game_seed
)
179 if (password
.empty()) return password
;
181 size_t password_length
= password
.size();
182 size_t password_server_id_length
= password_server_id
.size();
184 std::ostringstream salted_password
;
185 /* Add the password with the server's ID and game seed as the salt. */
186 for (uint i
= 0; i
< NETWORK_SERVER_ID_LENGTH
- 1; i
++) {
187 char password_char
= (i
< password_length
? password
[i
] : 0);
188 char server_id_char
= (i
< password_server_id_length
? password_server_id
[i
] : 0);
189 char seed_char
= password_game_seed
>> (i
% 32);
190 salted_password
<< (char)(password_char
^ server_id_char
^ seed_char
); // Cast needed, otherwise interpreted as integer to format
196 /* Generate the MD5 hash */
197 std::string salted_password_string
= salted_password
.str();
198 checksum
.Append(salted_password_string
.data(), salted_password_string
.size());
199 checksum
.Finish(digest
);
201 return FormatArrayAsHex(digest
);
205 * Check if the company we want to join requires a password.
206 * @param company_id id of the company we want to check the 'passworded' flag for.
207 * @return true if the company requires a password.
209 bool NetworkCompanyIsPassworded(CompanyID company_id
)
211 return HasBit(_network_company_passworded
, company_id
);
214 /* This puts a text-message to the console, or in the future, the chat-box,
215 * (to keep it all a bit more general)
216 * If 'self_send' is true, this is the client who is sending the message */
217 void NetworkTextMessage(NetworkAction action
, TextColour colour
, bool self_send
, const std::string
&name
, const std::string
&str
, int64_t data
, const std::string
&data_str
)
221 case NETWORK_ACTION_SERVER_MESSAGE
:
222 /* Ignore invalid messages */
223 strid
= STR_NETWORK_SERVER_MESSAGE
;
226 case NETWORK_ACTION_COMPANY_SPECTATOR
:
228 strid
= STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE
;
230 case NETWORK_ACTION_COMPANY_JOIN
:
232 strid
= STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN
;
234 case NETWORK_ACTION_COMPANY_NEW
:
236 strid
= STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW
;
238 case NETWORK_ACTION_JOIN
:
239 /* Show the Client ID for the server but not for the client. */
240 strid
= _network_server
? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID
: STR_NETWORK_MESSAGE_CLIENT_JOINED
;
242 case NETWORK_ACTION_LEAVE
: strid
= STR_NETWORK_MESSAGE_CLIENT_LEFT
; break;
243 case NETWORK_ACTION_NAME_CHANGE
: strid
= STR_NETWORK_MESSAGE_NAME_CHANGE
; break;
244 case NETWORK_ACTION_GIVE_MONEY
: strid
= STR_NETWORK_MESSAGE_GIVE_MONEY
; break;
245 case NETWORK_ACTION_CHAT_COMPANY
: strid
= self_send
? STR_NETWORK_CHAT_TO_COMPANY
: STR_NETWORK_CHAT_COMPANY
; break;
246 case NETWORK_ACTION_CHAT_CLIENT
: strid
= self_send
? STR_NETWORK_CHAT_TO_CLIENT
: STR_NETWORK_CHAT_CLIENT
; break;
247 case NETWORK_ACTION_KICKED
: strid
= STR_NETWORK_MESSAGE_KICKED
; break;
248 case NETWORK_ACTION_EXTERNAL_CHAT
: strid
= STR_NETWORK_CHAT_EXTERNAL
; break;
249 default: strid
= STR_NETWORK_CHAT_ALL
; break;
252 SetDParamStr(0, name
);
253 SetDParamStr(1, str
);
255 SetDParamStr(3, data_str
);
257 /* All of these strings start with "***". These characters are interpreted as both left-to-right and
258 * right-to-left characters depending on the context. As the next text might be an user's name, the
259 * user name's characters will influence the direction of the "***" instead of the language setting
260 * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
261 std::ostringstream stream
;
262 std::ostreambuf_iterator
<char> iterator(stream
);
263 Utf8Encode(iterator
, _current_text_dir
== TD_LTR
? CHAR_TD_LRM
: CHAR_TD_RLM
);
264 std::string message
= stream
.str() + GetString(strid
);
266 Debug(desync
, 1, "msg: {:08x}; {:02x}; {}", TimerGameEconomy::date
, TimerGameEconomy::date_fract
, message
);
267 IConsolePrint(colour
, message
);
268 NetworkAddChatMessage(colour
, _settings_client
.gui
.network_chat_timeout
, message
);
271 /* Calculate the frame-lag of a client */
272 uint
NetworkCalculateLag(const NetworkClientSocket
*cs
)
274 int lag
= cs
->last_frame_server
- cs
->last_frame
;
275 /* This client has missed their ACK packet after 1 DAY_TICKS..
276 * so we increase their lag for every frame that passes!
277 * The packet can be out by a max of _net_frame_freq */
278 if (cs
->last_frame_server
+ Ticks::DAY_TICKS
+ _settings_client
.network
.frame_freq
< _frame_counter
) {
279 lag
+= _frame_counter
- (cs
->last_frame_server
+ Ticks::DAY_TICKS
+ _settings_client
.network
.frame_freq
);
285 /* There was a non-recoverable error, drop back to the main menu with a nice
287 void ShowNetworkError(StringID error_string
)
289 _switch_mode
= SM_MENU
;
290 ShowErrorMessage(error_string
, INVALID_STRING_ID
, WL_CRITICAL
);
294 * Retrieve the string id of an internal error number
295 * @param err NetworkErrorCode
296 * @return the StringID
298 StringID
GetNetworkErrorMsg(NetworkErrorCode err
)
300 /* List of possible network errors, used by
301 * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
302 static const StringID network_error_strings
[] = {
303 STR_NETWORK_ERROR_CLIENT_GENERAL
,
304 STR_NETWORK_ERROR_CLIENT_DESYNC
,
305 STR_NETWORK_ERROR_CLIENT_SAVEGAME
,
306 STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST
,
307 STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR
,
308 STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH
,
309 STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED
,
310 STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED
,
311 STR_NETWORK_ERROR_CLIENT_WRONG_REVISION
,
312 STR_NETWORK_ERROR_CLIENT_NAME_IN_USE
,
313 STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD
,
314 STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH
,
315 STR_NETWORK_ERROR_CLIENT_KICKED
,
316 STR_NETWORK_ERROR_CLIENT_CHEATER
,
317 STR_NETWORK_ERROR_CLIENT_SERVER_FULL
,
318 STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS
,
319 STR_NETWORK_ERROR_CLIENT_TIMEOUT_PASSWORD
,
320 STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER
,
321 STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP
,
322 STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN
,
323 STR_NETWORK_ERROR_CLIENT_INVALID_CLIENT_NAME
,
325 static_assert(lengthof(network_error_strings
) == NETWORK_ERROR_END
);
327 if (err
>= (ptrdiff_t)lengthof(network_error_strings
)) err
= NETWORK_ERROR_GENERAL
;
329 return network_error_strings
[err
];
333 * Handle the pause mode change so we send the right messages to the chat.
334 * @param prev_mode The previous pause mode.
335 * @param changed_mode The pause mode that got changed.
337 void NetworkHandlePauseChange(PauseMode prev_mode
, PauseMode changed_mode
)
339 if (!_networking
) return;
341 switch (changed_mode
) {
342 case PM_PAUSED_NORMAL
:
344 case PM_PAUSED_GAME_SCRIPT
:
345 case PM_PAUSED_ACTIVE_CLIENTS
:
346 case PM_PAUSED_LINK_GRAPH
: {
347 bool changed
= ((_pause_mode
== PM_UNPAUSED
) != (prev_mode
== PM_UNPAUSED
));
348 bool paused
= (_pause_mode
!= PM_UNPAUSED
);
349 if (!paused
&& !changed
) return;
355 if ((_pause_mode
& PM_PAUSED_NORMAL
) != PM_UNPAUSED
) SetDParam(++i
, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL
);
356 if ((_pause_mode
& PM_PAUSED_JOIN
) != PM_UNPAUSED
) SetDParam(++i
, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS
);
357 if ((_pause_mode
& PM_PAUSED_GAME_SCRIPT
) != PM_UNPAUSED
) SetDParam(++i
, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT
);
358 if ((_pause_mode
& PM_PAUSED_ACTIVE_CLIENTS
) != PM_UNPAUSED
) SetDParam(++i
, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS
);
359 if ((_pause_mode
& PM_PAUSED_LINK_GRAPH
) != PM_UNPAUSED
) SetDParam(++i
, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH
);
360 str
= STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1
+ i
;
362 switch (changed_mode
) {
363 case PM_PAUSED_NORMAL
: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL
); break;
364 case PM_PAUSED_JOIN
: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS
); break;
365 case PM_PAUSED_GAME_SCRIPT
: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT
); break;
366 case PM_PAUSED_ACTIVE_CLIENTS
: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS
); break;
367 case PM_PAUSED_LINK_GRAPH
: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH
); break;
368 default: NOT_REACHED();
370 str
= paused
? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED
: STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED
;
373 NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE
, CC_DEFAULT
, false, "", GetString(str
));
384 * Helper function for the pause checkers. If pause is true and the
385 * current pause mode isn't set the game will be paused, if it it false
386 * and the pause mode is set the game will be unpaused. In the other
387 * cases nothing happens to the pause state.
388 * @param pause whether we'd like to pause
389 * @param pm the mode which we would like to pause with
391 static void CheckPauseHelper(bool pause
, PauseMode pm
)
393 if (pause
== ((_pause_mode
& pm
) != PM_UNPAUSED
)) return;
395 Command
<CMD_PAUSE
>::Post(pm
, pause
);
399 * Counts the number of active clients connected.
400 * It has to be in STATUS_ACTIVE and not a spectator
401 * @return number of active clients
403 static uint
NetworkCountActiveClients()
407 for (const NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
408 if (cs
->status
!= NetworkClientSocket::STATUS_ACTIVE
) continue;
409 if (!Company::IsValidID(cs
->GetInfo()->client_playas
)) continue;
417 * Check if the minimum number of active clients has been reached and pause or unpause the game as appropriate
419 static void CheckMinActiveClients()
421 if ((_pause_mode
& PM_PAUSED_ERROR
) != PM_UNPAUSED
||
422 !_network_dedicated
||
423 (_settings_client
.network
.min_active_clients
== 0 && (_pause_mode
& PM_PAUSED_ACTIVE_CLIENTS
) == PM_UNPAUSED
)) {
426 CheckPauseHelper(NetworkCountActiveClients() < _settings_client
.network
.min_active_clients
, PM_PAUSED_ACTIVE_CLIENTS
);
430 * Checks whether there is a joining client
431 * @return true iff one client is joining (but not authorizing)
433 static bool NetworkHasJoiningClient()
435 for (const NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
436 if (cs
->status
>= NetworkClientSocket::STATUS_AUTHORIZED
&& cs
->status
< NetworkClientSocket::STATUS_ACTIVE
) return true;
443 * Check whether we should pause on join
445 static void CheckPauseOnJoin()
447 if ((_pause_mode
& PM_PAUSED_ERROR
) != PM_UNPAUSED
||
448 (!_settings_client
.network
.pause_on_join
&& (_pause_mode
& PM_PAUSED_JOIN
) == PM_UNPAUSED
)) {
451 CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN
);
455 * Parse the company part ("#company" postfix) of a connecting string.
456 * @param connection_string The string with the connection data.
457 * @param company_id The company ID to set, if available.
458 * @return A std::string_view into the connection string without the company part.
460 std::string_view
ParseCompanyFromConnectionString(const std::string
&connection_string
, CompanyID
*company_id
)
462 std::string_view ip
= connection_string
;
463 if (company_id
== nullptr) return ip
;
465 size_t offset
= ip
.find_last_of('#');
466 if (offset
!= std::string::npos
) {
467 std::string_view company_string
= ip
.substr(offset
+ 1);
468 ip
= ip
.substr(0, offset
);
470 uint8_t company_value
;
471 auto [_
, err
] = std::from_chars(company_string
.data(), company_string
.data() + company_string
.size(), company_value
);
472 if (err
== std::errc()) {
473 if (company_value
!= COMPANY_NEW_COMPANY
&& company_value
!= COMPANY_SPECTATOR
) {
474 if (company_value
> MAX_COMPANIES
|| company_value
== 0) {
475 *company_id
= COMPANY_SPECTATOR
;
477 /* "#1" means the first company, which has index 0. */
478 *company_id
= (CompanyID
)(company_value
- 1);
481 *company_id
= (CompanyID
)company_value
;
490 * Converts a string to ip/port/company
491 * Format: IP:port#company
493 * Returns the IP part as a string view into the passed string. This view is
494 * valid as long the passed connection string is valid. If there is no port
495 * present in the connection string, the port reference will not be touched.
496 * When there is no company ID present in the connection string or company_id
497 * is nullptr, then company ID will not be touched.
499 * @param connection_string The string with the connection data.
500 * @param port The port reference to set.
501 * @param company_id The company ID to set, if available.
502 * @return A std::string_view into the connection string with the (IP) address part.
504 std::string_view
ParseFullConnectionString(const std::string
&connection_string
, uint16_t &port
, CompanyID
*company_id
)
506 std::string_view ip
= ParseCompanyFromConnectionString(connection_string
, company_id
);
508 size_t port_offset
= ip
.find_last_of(':');
509 size_t ipv6_close
= ip
.find_last_of(']');
510 if (port_offset
!= std::string::npos
&& (ipv6_close
== std::string::npos
|| ipv6_close
< port_offset
)) {
511 std::string_view port_string
= ip
.substr(port_offset
+ 1);
512 ip
= ip
.substr(0, port_offset
);
513 std::from_chars(port_string
.data(), port_string
.data() + port_string
.size(), port
);
519 * Normalize a connection string. That is, ensure there is a port in the string.
520 * @param connection_string The connection string to normalize.
521 * @param default_port The port to use if none is given.
522 * @return The normalized connection string.
524 std::string
NormalizeConnectionString(const std::string
&connection_string
, uint16_t default_port
)
526 uint16_t port
= default_port
;
527 std::string_view ip
= ParseFullConnectionString(connection_string
, port
);
528 return std::string(ip
) + ":" + std::to_string(port
);
532 * Convert a string containing either "hostname" or "hostname:ip" to a
535 * @param connection_string The string to parse.
536 * @param default_port The default port to set port to if not in connection_string.
537 * @return A valid NetworkAddress of the parsed information.
539 NetworkAddress
ParseConnectionString(const std::string
&connection_string
, uint16_t default_port
)
541 uint16_t port
= default_port
;
542 std::string_view ip
= ParseFullConnectionString(connection_string
, port
);
543 return NetworkAddress(ip
, port
);
547 * Handle the accepting of a connection to the server.
548 * @param s The socket of the new connection.
549 * @param address The address of the peer.
551 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s
, const NetworkAddress
&address
)
553 /* Register the login */
554 _network_clients_connected
++;
556 ServerNetworkGameSocketHandler
*cs
= new ServerNetworkGameSocketHandler(s
);
557 cs
->client_address
= address
; // Save the IP of the client
559 InvalidateWindowData(WC_CLIENT_LIST
, 0);
563 * Resets the pools used for network clients, and the admin pool if needed.
564 * @param close_admins Whether the admin pool has to be cleared as well.
566 static void InitializeNetworkPools(bool close_admins
= true)
568 PoolBase::Clean(PT_NCLIENT
| (close_admins
? PT_NADMIN
: PT_NONE
));
572 * Close current connections.
573 * @param close_admins Whether the admin connections have to be closed as well.
575 void NetworkClose(bool close_admins
)
577 if (_network_server
) {
579 for (ServerNetworkAdminSocketHandler
*as
: ServerNetworkAdminSocketHandler::Iterate()) {
580 as
->CloseConnection(true);
584 for (NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
585 cs
->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT
);
587 ServerNetworkGameSocketHandler::CloseListeners();
588 ServerNetworkAdminSocketHandler::CloseListeners();
590 _network_coordinator_client
.CloseConnection();
592 if (MyClient::my_client
!= nullptr) {
593 MyClient::SendQuit();
594 MyClient::my_client
->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT
);
597 _network_coordinator_client
.CloseAllConnections();
599 NetworkGameSocketHandler::ProcessDeferredDeletions();
601 TCPConnecter::KillAll();
604 _network_server
= false;
606 NetworkFreeLocalCommandQueue();
608 delete[] _network_company_states
;
609 _network_company_states
= nullptr;
610 _network_company_passworded
= 0;
612 InitializeNetworkPools(close_admins
);
615 /* Initializes the network (cleans sockets and stuff) */
616 static void NetworkInitialize(bool close_admins
= true)
618 InitializeNetworkPools(close_admins
);
621 _network_first_time
= true;
623 _network_reconnect
= 0;
626 /** Non blocking connection to query servers for their game info. */
627 class TCPQueryConnecter
: public TCPServerConnecter
{
629 std::string connection_string
;
632 TCPQueryConnecter(const std::string
&connection_string
) : TCPServerConnecter(connection_string
, NETWORK_DEFAULT_PORT
), connection_string(connection_string
) {}
634 void OnFailure() override
636 Debug(net
, 9, "Query::OnFailure(): connection_string={}", this->connection_string
);
638 NetworkGameList
*item
= NetworkGameListAddItem(connection_string
);
639 item
->status
= NGLS_OFFLINE
;
640 item
->refreshing
= false;
642 UpdateNetworkGameWindow();
645 void OnConnect(SOCKET s
) override
647 Debug(net
, 9, "Query::OnConnect(): connection_string={}", this->connection_string
);
649 QueryNetworkGameSocketHandler::QueryServer(s
, this->connection_string
);
654 * Query a server to fetch the game-info.
655 * @param connection_string the address to query.
657 void NetworkQueryServer(const std::string
&connection_string
)
659 if (!_network_available
) return;
661 Debug(net
, 9, "NetworkQueryServer(): connection_string={}", connection_string
);
663 /* Mark the entry as refreshing, so the GUI can show the refresh is pending. */
664 NetworkGameList
*item
= NetworkGameListAddItem(connection_string
);
665 item
->refreshing
= true;
667 TCPConnecter::Create
<TCPQueryConnecter
>(connection_string
);
671 * Validates an address entered as a string and adds the server to
672 * the list. If you use this function, the games will be marked
674 * @param connection_string The IP:port of the server to add.
675 * @param manually Whether the enter should be marked as manual added.
676 * @param never_expire Whether the entry can expire (removed when no longer found in the public listing).
677 * @return The entry on the game list.
679 NetworkGameList
*NetworkAddServer(const std::string
&connection_string
, bool manually
, bool never_expire
)
681 if (connection_string
.empty()) return nullptr;
683 /* Ensure the item already exists in the list */
684 NetworkGameList
*item
= NetworkGameListAddItem(connection_string
);
685 if (item
->info
.server_name
.empty()) {
686 ClearGRFConfigList(&item
->info
.grfconfig
);
687 item
->info
.server_name
= connection_string
;
689 UpdateNetworkGameWindow();
691 NetworkQueryServer(connection_string
);
694 if (manually
) item
->manually
= true;
695 if (never_expire
) item
->version
= INT32_MAX
;
701 * Get the addresses to bind to.
702 * @param addresses the list to write to.
703 * @param port the port to bind to.
705 void GetBindAddresses(NetworkAddressList
*addresses
, uint16_t port
)
707 for (const auto &iter
: _network_bind_list
) {
708 addresses
->emplace_back(iter
.c_str(), port
);
711 /* No address, so bind to everything. */
712 if (addresses
->empty()) {
713 addresses
->emplace_back("", port
);
717 /* Generates the list of manually added hosts from NetworkGameList and
718 * dumps them into the array _network_host_list. This array is needed
719 * by the function that generates the config file. */
720 void NetworkRebuildHostList()
722 _network_host_list
.clear();
724 for (NetworkGameList
*item
= _network_game_list
; item
!= nullptr; item
= item
->next
) {
725 if (item
->manually
) _network_host_list
.emplace_back(item
->connection_string
);
729 /** Non blocking connection create to actually connect to servers */
730 class TCPClientConnecter
: public TCPServerConnecter
{
732 std::string connection_string
;
735 TCPClientConnecter(const std::string
&connection_string
) : TCPServerConnecter(connection_string
, NETWORK_DEFAULT_PORT
), connection_string(connection_string
) {}
737 void OnFailure() override
739 Debug(net
, 9, "Client::OnFailure(): connection_string={}", this->connection_string
);
741 ShowNetworkError(STR_NETWORK_ERROR_NOCONNECTION
);
744 void OnConnect(SOCKET s
) override
746 Debug(net
, 9, "Client::OnConnect(): connection_string={}", this->connection_string
);
749 new ClientNetworkGameSocketHandler(s
, this->connection_string
);
750 IConsoleCmdExec("exec scripts/on_client.scr 0");
751 NetworkClient_Connected();
756 * Join a client to the server at with the given connection string.
757 * The default for the passwords is \c nullptr. When the server or company needs a
758 * password and none is given, the user is asked to enter the password in the GUI.
759 * This function will return false whenever some information required to join is not
760 * correct such as the company number or the client's name, or when there is not
761 * networking avalabile at all. If the function returns false the connection with
762 * the existing server is not disconnected.
763 * It will return true when it starts the actual join process, i.e. when it
764 * actually shows the join status window.
766 * @param connection_string The IP address, port and company number to join as.
767 * @param default_company The company number to join as when none is given.
768 * @param join_server_password The password for the server.
769 * @param join_company_password The password for the company.
770 * @return Whether the join has started.
772 bool NetworkClientConnectGame(const std::string
&connection_string
, CompanyID default_company
, const std::string
&join_server_password
, const std::string
&join_company_password
)
774 Debug(net
, 9, "NetworkClientConnectGame(): connection_string={}", connection_string
);
776 CompanyID join_as
= default_company
;
777 std::string resolved_connection_string
= ServerAddress::Parse(connection_string
, NETWORK_DEFAULT_PORT
, &join_as
).connection_string
;
779 if (!_network_available
) return false;
780 if (!NetworkValidateOurClientName()) return false;
782 _network_join
.connection_string
= resolved_connection_string
;
783 _network_join
.company
= join_as
;
784 _network_join
.server_password
= join_server_password
;
785 _network_join
.company_password
= join_company_password
;
787 if (_game_mode
== GM_MENU
) {
788 /* From the menu we can immediately continue with the actual join. */
789 NetworkClientJoinGame();
791 /* When already playing a game, first go back to the main menu. This
792 * disconnects the user from the current game, meaning we can safely
793 * load in the new. After all, there is little point in continueing to
794 * play on a server if we are connecting to another one.
796 _switch_mode
= SM_JOIN_GAME
;
802 * Actually perform the joining to the server. Use #NetworkClientConnectGame
803 * when you want to connect to a specific server/company. This function
804 * assumes _network_join is already fully set up.
806 void NetworkClientJoinGame()
811 _settings_client
.network
.last_joined
= _network_join
.connection_string
;
812 Debug(net
, 9, "status = CONNECTING");
813 _network_join_status
= NETWORK_JOIN_STATUS_CONNECTING
;
814 ShowJoinStatusWindow();
816 TCPConnecter::Create
<TCPClientConnecter
>(_network_join
.connection_string
);
819 static void NetworkInitGameInfo()
821 FillStaticNetworkServerGameInfo();
822 /* The server is a client too */
823 _network_game_info
.clients_on
= _network_dedicated
? 0 : 1;
825 /* There should be always space for the server. */
826 assert(NetworkClientInfo::CanAllocateItem());
827 NetworkClientInfo
*ci
= new NetworkClientInfo(CLIENT_ID_SERVER
);
828 ci
->client_playas
= _network_dedicated
? COMPANY_SPECTATOR
: COMPANY_FIRST
;
830 ci
->client_name
= _settings_client
.network
.client_name
;
834 * Trim the given server name in place, i.e. remove leading and trailing spaces.
835 * After the trim check whether the server name is not empty.
836 * When the server name is empty a GUI error message is shown telling the
837 * user to set the servername and this function returns false.
839 * @param server_name The server name to validate. It will be trimmed of leading
840 * and trailing spaces.
841 * @return True iff the server name is valid.
843 bool NetworkValidateServerName(std::string
&server_name
)
845 StrTrimInPlace(server_name
);
846 if (!server_name
.empty()) return true;
848 ShowErrorMessage(STR_NETWORK_ERROR_BAD_SERVER_NAME
, INVALID_STRING_ID
, WL_ERROR
);
853 * Check whether the client and server name are set, for a dedicated server and if not set them to some default
854 * value and tell the user to change this as soon as possible.
855 * If the saved name is the default value, then the user is told to override this value too.
856 * This is only meant dedicated servers, as for the other servers the GUI ensures a name has been entered.
858 static void CheckClientAndServerName()
860 static const std::string fallback_client_name
= "Unnamed Client";
861 StrTrimInPlace(_settings_client
.network
.client_name
);
862 if (_settings_client
.network
.client_name
.empty() || _settings_client
.network
.client_name
.compare(fallback_client_name
) == 0) {
863 Debug(net
, 1, "No \"client_name\" has been set, using \"{}\" instead. Please set this now using the \"name <new name>\" command", fallback_client_name
);
864 _settings_client
.network
.client_name
= fallback_client_name
;
867 static const std::string fallback_server_name
= "Unnamed Server";
868 StrTrimInPlace(_settings_client
.network
.server_name
);
869 if (_settings_client
.network
.server_name
.empty() || _settings_client
.network
.server_name
.compare(fallback_server_name
) == 0) {
870 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
);
871 _settings_client
.network
.server_name
= fallback_server_name
;
875 bool NetworkServerStart()
877 if (!_network_available
) return false;
879 /* Call the pre-scripts */
880 IConsoleCmdExec("exec scripts/pre_server.scr 0");
881 if (_network_dedicated
) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
883 /* Check for the client and server names to be set, but only after the scripts had a chance to set them.*/
884 if (_network_dedicated
) CheckClientAndServerName();
886 NetworkDisconnect(false);
887 NetworkInitialize(false);
888 NetworkUDPInitialize();
889 Debug(net
, 5, "Starting listeners for clients");
890 if (!ServerNetworkGameSocketHandler::Listen(_settings_client
.network
.server_port
)) return false;
892 /* Only listen for admins when the password isn't empty. */
893 if (!_settings_client
.network
.admin_password
.empty()) {
894 Debug(net
, 5, "Starting listeners for admins");
895 if (!ServerNetworkAdminSocketHandler::Listen(_settings_client
.network
.server_admin_port
)) return false;
898 /* Try to start UDP-server */
899 Debug(net
, 5, "Starting listeners for incoming server queries");
900 NetworkUDPServerListen();
902 _network_company_states
= new NetworkCompanyState
[MAX_COMPANIES
];
903 _network_server
= true;
906 _frame_counter_server
= 0;
907 _frame_counter_max
= 0;
908 _last_sync_frame
= 0;
909 _network_own_client_id
= CLIENT_ID_SERVER
;
911 _network_clients_connected
= 0;
912 _network_company_passworded
= 0;
914 NetworkInitGameInfo();
916 if (_settings_client
.network
.server_game_type
!= SERVER_GAME_TYPE_LOCAL
) {
917 _network_coordinator_client
.Register();
920 /* execute server initialization script */
921 IConsoleCmdExec("exec scripts/on_server.scr 0");
922 /* if the server is dedicated ... add some other script */
923 if (_network_dedicated
) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
925 /* welcome possibly still connected admins - this can only happen on a dedicated server. */
926 if (_network_dedicated
) ServerNetworkAdminSocketHandler::WelcomeAll();
931 /* The server is rebooting...
932 * The only difference with NetworkDisconnect, is the packets that is sent */
935 if (_network_server
) {
936 for (NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
941 for (ServerNetworkAdminSocketHandler
*as
: ServerNetworkAdminSocketHandler::IterateActive()) {
947 /* For non-dedicated servers we have to kick the admins as we are not
948 * certain that we will end up in a new network game. */
949 NetworkClose(!_network_dedicated
);
953 * We want to disconnect from the host/clients.
954 * @param close_admins Whether the admin sockets need to be closed as well.
956 void NetworkDisconnect(bool close_admins
)
958 if (_network_server
) {
959 for (NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
965 for (ServerNetworkAdminSocketHandler
*as
: ServerNetworkAdminSocketHandler::IterateActive()) {
972 CloseWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
974 NetworkClose(close_admins
);
976 /* Reinitialize the UDP stack, i.e. close all existing connections. */
977 NetworkUDPInitialize();
981 * The setting server_game_type was updated; possibly we need to take some
984 void NetworkUpdateServerGameType()
986 if (!_networking
) return;
988 switch (_settings_client
.network
.server_game_type
) {
989 case SERVER_GAME_TYPE_LOCAL
:
990 _network_coordinator_client
.CloseConnection();
993 case SERVER_GAME_TYPE_INVITE_ONLY
:
994 case SERVER_GAME_TYPE_PUBLIC
:
995 _network_coordinator_client
.Register();
1004 * Receives something from the network.
1005 * @return true if everything went fine, false when the connection got closed.
1007 static bool NetworkReceive()
1010 if (_network_server
) {
1011 ServerNetworkAdminSocketHandler::Receive();
1012 result
= ServerNetworkGameSocketHandler::Receive();
1014 result
= ClientNetworkGameSocketHandler::Receive();
1016 NetworkGameSocketHandler::ProcessDeferredDeletions();
1020 /* This sends all buffered commands (if possible) */
1021 static void NetworkSend()
1023 if (_network_server
) {
1024 ServerNetworkAdminSocketHandler::Send();
1025 ServerNetworkGameSocketHandler::Send();
1027 ClientNetworkGameSocketHandler::Send();
1029 NetworkGameSocketHandler::ProcessDeferredDeletions();
1033 * We have to do some (simple) background stuff that runs normally,
1034 * even when we are not in multiplayer. For example stuff needed
1035 * for finding servers or downloading content.
1037 void NetworkBackgroundLoop()
1039 _network_content_client
.SendReceive();
1040 _network_coordinator_client
.SendReceive();
1041 TCPConnecter::CheckCallbacks();
1042 NetworkHTTPSocketHandler::HTTPReceive();
1043 QueryNetworkGameSocketHandler::SendReceive();
1044 NetworkGameSocketHandler::ProcessDeferredDeletions();
1046 NetworkBackgroundUDPLoop();
1049 /* The main loop called from ttd.c
1050 * Here we also have to do StateGameLoop if needed! */
1051 void NetworkGameLoop()
1053 if (!_networking
) return;
1055 if (!NetworkReceive()) return;
1057 if (_network_server
) {
1058 /* Log the sync state to check for in-syncedness of replays. */
1059 if (TimerGameEconomy::date_fract
== 0) {
1060 /* We don't want to log multiple times if paused. */
1061 static TimerGameEconomy::Date last_log
;
1062 if (last_log
!= TimerGameEconomy::date
) {
1063 Debug(desync
, 1, "sync: {:08x}; {:02x}; {:08x}; {:08x}", TimerGameEconomy::date
, TimerGameEconomy::date_fract
, _random
.state
[0], _random
.state
[1]);
1064 last_log
= TimerGameEconomy::date
;
1068 #ifdef DEBUG_DUMP_COMMANDS
1069 /* Loading of the debug commands from -ddesync>=1 */
1070 static FILE *f
= FioFOpenFile("commands.log", "rb", SAVE_DIR
);
1071 static TimerGameEconomy::Date
next_date(0);
1072 static uint32_t next_date_fract
;
1073 static CommandPacket
*cp
= nullptr;
1074 static bool check_sync_state
= false;
1075 static uint32_t sync_state
[2];
1076 if (f
== nullptr && next_date
== 0) {
1077 Debug(desync
, 0, "Cannot open commands.log");
1078 next_date
= TimerGameEconomy::Date(1);
1081 while (f
!= nullptr && !feof(f
)) {
1082 if (TimerGameEconomy::date
== next_date
&& TimerGameEconomy::date_fract
== next_date_fract
) {
1083 if (cp
!= nullptr) {
1084 NetworkSendCommand(cp
->cmd
, cp
->err_msg
, nullptr, cp
->company
, cp
->data
);
1085 Debug(desync
, 0, "Injecting: {:08x}; {:02x}; {:02x}; {:08x}; {} ({})", TimerGameEconomy::date
, TimerGameEconomy::date_fract
, (int)_current_company
, cp
->cmd
, FormatArrayAsHex(cp
->data
), GetCommandName(cp
->cmd
));
1089 if (check_sync_state
) {
1090 if (sync_state
[0] == _random
.state
[0] && sync_state
[1] == _random
.state
[1]) {
1091 Debug(desync
, 0, "Sync check: {:08x}; {:02x}; match", TimerGameEconomy::date
, TimerGameEconomy::date_fract
);
1093 Debug(desync
, 0, "Sync check: {:08x}; {:02x}; mismatch expected {{{:08x}, {:08x}}}, got {{{:08x}, {:08x}}}",
1094 TimerGameEconomy::date
, TimerGameEconomy::date_fract
, sync_state
[0], sync_state
[1], _random
.state
[0], _random
.state
[1]);
1097 check_sync_state
= false;
1101 if (cp
!= nullptr || check_sync_state
) break;
1104 if (fgets(buff
, lengthof(buff
), f
) == nullptr) break;
1107 /* Ignore the "[date time] " part of the message */
1110 if (p
== nullptr) break;
1114 if (strncmp(p
, "cmd: ", 5) == 0
1115 #ifdef DEBUG_FAILED_DUMP_COMMANDS
1116 || strncmp(p
, "cmdf: ", 6) == 0
1121 cp
= new CommandPacket();
1125 uint32_t next_date_raw
;
1126 int ret
= sscanf(p
, "%x; %x; %x; %x; %x; %255s", &next_date_raw
, &next_date_fract
, &company
, &cmd
, &cp
->err_msg
, buffer
);
1128 next_date
= TimerGameEconomy::Date((int32_t)next_date_raw
);
1129 cp
->company
= (CompanyID
)company
;
1130 cp
->cmd
= (Commands
)cmd
;
1132 /* Parse command data. */
1133 std::vector
<byte
> args
;
1134 size_t arg_len
= strlen(buffer
);
1135 for (size_t i
= 0; i
+ 1 < arg_len
; i
+= 2) {
1137 std::from_chars(buffer
+ i
, buffer
+ i
+ 2, e
, 16);
1138 args
.emplace_back(e
);
1141 } else if (strncmp(p
, "join: ", 6) == 0) {
1142 /* Manually insert a pause when joining; this way the client can join at the exact right time. */
1143 uint32_t next_date_raw
;
1144 int ret
= sscanf(p
+ 6, "%x; %x", &next_date_raw
, &next_date_fract
);
1145 next_date
= TimerGameEconomy::Date((int32_t)next_date_raw
);
1147 Debug(desync
, 0, "Injecting pause for join at {:08x}:{:02x}; please join when paused", next_date
, next_date_fract
);
1148 cp
= new CommandPacket();
1149 cp
->company
= COMPANY_SPECTATOR
;
1150 cp
->cmd
= CMD_PAUSE
;
1151 cp
->data
= EndianBufferWriter
<>::FromValue(CommandTraits
<CMD_PAUSE
>::Args
{ PM_PAUSED_NORMAL
, true });
1152 _ddc_fastforward
= false;
1153 } else if (strncmp(p
, "sync: ", 6) == 0) {
1154 uint32_t next_date_raw
;
1155 int ret
= sscanf(p
+ 6, "%x; %x; %x; %x", &next_date_raw
, &next_date_fract
, &sync_state
[0], &sync_state
[1]);
1156 next_date
= TimerGameEconomy::Date((int32_t)next_date_raw
);
1158 check_sync_state
= true;
1159 } else if (strncmp(p
, "msg: ", 5) == 0 || strncmp(p
, "client: ", 8) == 0 ||
1160 strncmp(p
, "load: ", 6) == 0 || strncmp(p
, "save: ", 6) == 0) {
1161 /* A message that is not very important to the log playback, but part of the log. */
1162 #ifndef DEBUG_FAILED_DUMP_COMMANDS
1163 } else if (strncmp(p
, "cmdf: ", 6) == 0) {
1164 Debug(desync
, 0, "Skipping replay of failed command: {}", p
+ 6);
1167 /* Can't parse a line; what's wrong here? */
1168 Debug(desync
, 0, "Trying to parse: {}", p
);
1172 if (f
!= nullptr && feof(f
)) {
1173 Debug(desync
, 0, "End of commands.log");
1177 #endif /* DEBUG_DUMP_COMMANDS */
1178 if (_frame_counter
>= _frame_counter_max
) {
1179 /* Only check for active clients just before we're going to send out
1180 * the commands so we don't send multiple pause/unpause commands when
1181 * the frame_freq is more than 1 tick. Same with distributing commands. */
1183 CheckMinActiveClients();
1184 NetworkDistributeCommands();
1187 bool send_frame
= false;
1189 /* We first increase the _frame_counter */
1191 /* Update max-frame-counter */
1192 if (_frame_counter
> _frame_counter_max
) {
1193 _frame_counter_max
= _frame_counter
+ _settings_client
.network
.frame_freq
;
1197 NetworkExecuteLocalCommandQueue();
1199 /* Then we make the frame */
1202 _sync_seed_1
= _random
.state
[0];
1203 #ifdef NETWORK_SEND_DOUBLE_SEED
1204 _sync_seed_2
= _random
.state
[1];
1207 NetworkServer_Tick(send_frame
);
1211 /* Make sure we are at the frame were the server is (quick-frames) */
1212 if (_frame_counter_server
> _frame_counter
) {
1213 /* Run a number of frames; when things go bad, get out. */
1214 while (_frame_counter_server
> _frame_counter
) {
1215 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1218 /* Else, keep on going till _frame_counter_max */
1219 if (_frame_counter_max
> _frame_counter
) {
1220 /* Run one frame; if things went bad, get out. */
1221 if (!ClientNetworkGameSocketHandler::GameLoop()) return;
1229 static void NetworkGenerateServerId()
1231 _settings_client
.network
.network_id
= GenerateUid("OpenTTD Server ID");
1234 /** This tries to launch the network for a given OS */
1235 void NetworkStartUp()
1237 Debug(net
, 3, "Starting network");
1239 /* Network is available */
1240 _network_available
= NetworkCoreInitialize();
1241 _network_dedicated
= false;
1243 /* Generate an server id when there is none yet */
1244 if (_settings_client
.network
.network_id
.empty()) NetworkGenerateServerId();
1246 _network_game_info
= {};
1248 NetworkInitialize();
1249 NetworkUDPInitialize();
1250 Debug(net
, 3, "Network online, multiplayer available");
1251 NetworkFindBroadcastIPs(&_broadcast_list
);
1252 NetworkHTTPInitialize();
1255 /** This shuts the network down */
1256 void NetworkShutDown()
1258 NetworkDisconnect();
1259 NetworkHTTPUninitialize();
1262 Debug(net
, 3, "Shutting down network");
1264 _network_available
= false;
1266 NetworkCoreShutdown();
1269 #ifdef __EMSCRIPTEN__
1272 void CDECL
em_openttd_add_server(const char *connection_string
)
1274 NetworkAddServer(connection_string
, false, true);