OpenTTD Source  12.0-beta2
network.cpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #include "../stdafx.h"
11 
12 #include "../strings_func.h"
13 #include "../command_func.h"
14 #include "../date_func.h"
15 #include "network_admin.h"
16 #include "network_client.h"
17 #include "network_server.h"
18 #include "network_content.h"
19 #include "network_udp.h"
20 #include "network_gamelist.h"
21 #include "network_base.h"
22 #include "network_coordinator.h"
23 #include "core/udp.h"
24 #include "core/host.h"
25 #include "network_gui.h"
26 #include "../console_func.h"
27 #include "../3rdparty/md5/md5.h"
28 #include "../core/random_func.hpp"
29 #include "../window_func.h"
30 #include "../company_func.h"
31 #include "../company_base.h"
32 #include "../landscape_type.h"
33 #include "../rev.h"
34 #include "../core/pool_func.hpp"
35 #include "../gfx_func.h"
36 #include "../error.h"
37 #include <charconv>
38 #include <sstream>
39 #include <iomanip>
40 
41 #include "../safeguards.h"
42 
43 #ifdef DEBUG_DUMP_COMMANDS
44 #include "../fileio_func.h"
46 bool _ddc_fastforward = true;
47 #endif /* DEBUG_DUMP_COMMANDS */
48 
51 
55 
73 uint32 _sync_seed_1;
74 #ifdef NETWORK_SEND_DOUBLE_SEED
75 uint32 _sync_seed_2;
76 #endif
77 uint32 _sync_frame;
80 
82 
85 
86 /* Some externs / forwards */
87 extern void StateGameLoop();
88 
93 bool HasClients()
94 {
95  return !NetworkClientSocket::Iterate().empty();
96 }
97 
102 {
103  /* Delete the chat window, if you were chatting with this client. */
105 }
106 
113 {
115  if (ci->client_id == client_id) return ci;
116  }
117 
118  return nullptr;
119 }
120 
127 {
128  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
129  if (cs->client_id == client_id) return cs;
130  }
131 
132  return nullptr;
133 }
134 
135 byte NetworkSpectatorCount()
136 {
137  byte count = 0;
138 
139  for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
140  if (ci->client_playas == COMPANY_SPECTATOR) count++;
141  }
142 
143  /* Don't count a dedicated server as spectator */
144  if (_network_dedicated) count--;
145 
146  return count;
147 }
148 
155 std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password)
156 {
157  if (password.compare("*") == 0) password = "";
158 
159  if (_network_server) {
160  NetworkServerSetCompanyPassword(company_id, password, false);
161  } else {
163  }
164 
165  return password;
166 }
167 
175 std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed)
176 {
177  if (password.empty()) return password;
178 
179  size_t password_length = password.size();
180  size_t password_server_id_length = password_server_id.size();
181 
182  std::ostringstream salted_password;
183  /* Add the password with the server's ID and game seed as the salt. */
184  for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
185  char password_char = (i < password_length ? password[i] : 0);
186  char server_id_char = (i < password_server_id_length ? password_server_id[i] : 0);
187  char seed_char = password_game_seed >> (i % 32);
188  salted_password << (char)(password_char ^ server_id_char ^ seed_char); // Cast needed, otherwise interpreted as integer to format
189  }
190 
191  Md5 checksum;
192  uint8 digest[16];
193 
194  /* Generate the MD5 hash */
195  std::string salted_password_string = salted_password.str();
196  checksum.Append(salted_password_string.data(), salted_password_string.size());
197  checksum.Finish(digest);
198 
199  std::ostringstream hashed_password;
200  hashed_password << std::hex << std::setfill('0');
201  for (int di = 0; di < 16; di++) hashed_password << std::setw(2) << (int)digest[di]; // Cast needed, otherwise interpreted as character to add
202 
203  return hashed_password.str();
204 }
205 
212 {
213  return HasBit(_network_company_passworded, company_id);
214 }
215 
216 /* This puts a text-message to the console, or in the future, the chat-box,
217  * (to keep it all a bit more general)
218  * If 'self_send' is true, this is the client who is sending the message */
219 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const std::string &name, const std::string &str, int64 data)
220 {
221  StringID strid;
222  switch (action) {
223  case NETWORK_ACTION_SERVER_MESSAGE:
224  /* Ignore invalid messages */
225  strid = STR_NETWORK_SERVER_MESSAGE;
226  colour = CC_DEFAULT;
227  break;
228  case NETWORK_ACTION_COMPANY_SPECTATOR:
229  colour = CC_DEFAULT;
230  strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
231  break;
232  case NETWORK_ACTION_COMPANY_JOIN:
233  colour = CC_DEFAULT;
234  strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
235  break;
236  case NETWORK_ACTION_COMPANY_NEW:
237  colour = CC_DEFAULT;
238  strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
239  break;
240  case NETWORK_ACTION_JOIN:
241  /* Show the Client ID for the server but not for the client. */
242  strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID : STR_NETWORK_MESSAGE_CLIENT_JOINED;
243  break;
244  case NETWORK_ACTION_LEAVE: strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
245  case NETWORK_ACTION_NAME_CHANGE: strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
246  case NETWORK_ACTION_GIVE_MONEY: strid = STR_NETWORK_MESSAGE_GIVE_MONEY; break;
247  case NETWORK_ACTION_CHAT_COMPANY: strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
248  case NETWORK_ACTION_CHAT_CLIENT: strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT; break;
249  case NETWORK_ACTION_KICKED: strid = STR_NETWORK_MESSAGE_KICKED; break;
250  default: strid = STR_NETWORK_CHAT_ALL; break;
251  }
252 
253  char message[1024];
254  SetDParamStr(0, name);
255  SetDParamStr(1, str);
256  SetDParam(2, data);
257 
258  /* All of these strings start with "***". These characters are interpreted as both left-to-right and
259  * right-to-left characters depending on the context. As the next text might be an user's name, the
260  * user name's characters will influence the direction of the "***" instead of the language setting
261  * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
262  char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
263  GetString(msg_ptr, strid, lastof(message));
264 
265  Debug(desync, 1, "msg: {:08x}; {:02x}; {}", _date, _date_fract, message);
266  IConsolePrint(colour, message);
268 }
269 
270 /* Calculate the frame-lag of a client */
271 uint NetworkCalculateLag(const NetworkClientSocket *cs)
272 {
273  int lag = cs->last_frame_server - cs->last_frame;
274  /* This client has missed their ACK packet after 1 DAY_TICKS..
275  * so we increase their lag for every frame that passes!
276  * The packet can be out by a max of _net_frame_freq */
277  if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
278  lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
279  }
280  return lag;
281 }
282 
283 
284 /* There was a non-recoverable error, drop back to the main menu with a nice
285  * error */
286 void ShowNetworkError(StringID error_string)
287 {
290 }
291 
298 {
299  /* List of possible network errors, used by
300  * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
301  static const StringID network_error_strings[] = {
302  STR_NETWORK_ERROR_CLIENT_GENERAL,
303  STR_NETWORK_ERROR_CLIENT_DESYNC,
304  STR_NETWORK_ERROR_CLIENT_SAVEGAME,
305  STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST,
306  STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR,
307  STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH,
308  STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED,
309  STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED,
310  STR_NETWORK_ERROR_CLIENT_WRONG_REVISION,
311  STR_NETWORK_ERROR_CLIENT_NAME_IN_USE,
312  STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD,
313  STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH,
314  STR_NETWORK_ERROR_CLIENT_KICKED,
315  STR_NETWORK_ERROR_CLIENT_CHEATER,
316  STR_NETWORK_ERROR_CLIENT_SERVER_FULL,
317  STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS,
318  STR_NETWORK_ERROR_CLIENT_TIMEOUT_PASSWORD,
319  STR_NETWORK_ERROR_CLIENT_TIMEOUT_COMPUTER,
320  STR_NETWORK_ERROR_CLIENT_TIMEOUT_MAP,
321  STR_NETWORK_ERROR_CLIENT_TIMEOUT_JOIN,
322  STR_NETWORK_ERROR_CLIENT_INVALID_CLIENT_NAME,
323  };
324  static_assert(lengthof(network_error_strings) == NETWORK_ERROR_END);
325 
326  if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
327 
328  return network_error_strings[err];
329 }
330 
336 void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
337 {
338  if (!_networking) return;
339 
340  switch (changed_mode) {
341  case PM_PAUSED_NORMAL:
342  case PM_PAUSED_JOIN:
345  case PM_PAUSED_LINK_GRAPH: {
346  bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
347  bool paused = (_pause_mode != PM_UNPAUSED);
348  if (!paused && !changed) return;
349 
350  StringID str;
351  if (!changed) {
352  int i = -1;
353 
354  if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL);
355  if ((_pause_mode & PM_PAUSED_JOIN) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS);
356  if ((_pause_mode & PM_PAUSED_GAME_SCRIPT) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT);
357  if ((_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS);
358  if ((_pause_mode & PM_PAUSED_LINK_GRAPH) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH);
359  str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
360  } else {
361  switch (changed_mode) {
362  case PM_PAUSED_NORMAL: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
363  case PM_PAUSED_JOIN: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
364  case PM_PAUSED_GAME_SCRIPT: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT); break;
365  case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
366  case PM_PAUSED_LINK_GRAPH: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH); break;
367  default: NOT_REACHED();
368  }
369  str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
370  }
371 
372  NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, "", GetString(str));
373  break;
374  }
375 
376  default:
377  return;
378  }
379 }
380 
381 
390 static void CheckPauseHelper(bool pause, PauseMode pm)
391 {
392  if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
393 
394  DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
395 }
396 
403 {
404  uint count = 0;
405 
406  for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
407  if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
408  if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
409  count++;
410  }
411 
412  return count;
413 }
414 
419 {
423  return;
424  }
426 }
427 
433 {
434  for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
435  if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
436  }
437 
438  return false;
439 }
440 
444 static void CheckPauseOnJoin()
445 {
448  return;
449  }
451 }
452 
459 std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id)
460 {
461  std::string_view ip = connection_string;
462  if (company_id == nullptr) return ip;
463 
464  size_t offset = ip.find_last_of('#');
465  if (offset != std::string::npos) {
466  std::string_view company_string = ip.substr(offset + 1);
467  ip = ip.substr(0, offset);
468 
469  uint8 company_value;
470  auto [_, err] = std::from_chars(company_string.data(), company_string.data() + company_string.size(), company_value);
471  if (err == std::errc()) {
472  if (company_value != COMPANY_NEW_COMPANY && company_value != COMPANY_SPECTATOR) {
473  if (company_value > MAX_COMPANIES || company_value == 0) {
474  *company_id = COMPANY_SPECTATOR;
475  } else {
476  /* "#1" means the first company, which has index 0. */
477  *company_id = (CompanyID)(company_value - 1);
478  }
479  } else {
480  *company_id = (CompanyID)company_value;
481  }
482  }
483  }
484 
485  return ip;
486 }
487 
503 std::string_view ParseFullConnectionString(const std::string &connection_string, uint16 &port, CompanyID *company_id)
504 {
505  std::string_view ip = ParseCompanyFromConnectionString(connection_string, company_id);
506 
507  size_t port_offset = ip.find_last_of(':');
508  size_t ipv6_close = ip.find_last_of(']');
509  if (port_offset != std::string::npos && (ipv6_close == std::string::npos || ipv6_close < port_offset)) {
510  std::string_view port_string = ip.substr(port_offset + 1);
511  ip = ip.substr(0, port_offset);
512  std::from_chars(port_string.data(), port_string.data() + port_string.size(), port);
513  }
514  return ip;
515 }
516 
523 std::string NormalizeConnectionString(const std::string &connection_string, uint16 default_port)
524 {
525  uint16 port = default_port;
526  std::string_view ip = ParseFullConnectionString(connection_string, port);
527  return std::string(ip) + ":" + std::to_string(port);
528 }
529 
538 NetworkAddress ParseConnectionString(const std::string &connection_string, uint16 default_port)
539 {
540  uint16 port = default_port;
541  std::string_view ip = ParseFullConnectionString(connection_string, port);
542  return NetworkAddress(ip, port);
543 }
544 
550 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
551 {
552  /* Register the login */
554 
556  cs->client_address = address; // Save the IP of the client
557 
559 }
560 
565 static void InitializeNetworkPools(bool close_admins = true)
566 {
567  PoolBase::Clean(PT_NCLIENT | (close_admins ? PT_NADMIN : PT_NONE));
568 }
569 
574 void NetworkClose(bool close_admins)
575 {
576  if (_network_server) {
577  if (close_admins) {
579  as->CloseConnection(true);
580  }
581  }
582 
583  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
584  cs->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
585  }
588 
590  } else {
591  if (MyClient::my_client != nullptr) {
594  }
595 
597  }
598 
600 
601  _networking = false;
602  _network_server = false;
603 
605 
606  delete[] _network_company_states;
607  _network_company_states = nullptr;
608 
609  InitializeNetworkPools(close_admins);
610 }
611 
612 /* Initializes the network (cleans sockets and stuff) */
613 static void NetworkInitialize(bool close_admins = true)
614 {
615  InitializeNetworkPools(close_admins);
616 
617  _sync_frame = 0;
618  _network_first_time = true;
619 
620  _network_reconnect = 0;
621 }
622 
625 private:
626  std::string connection_string;
627 
628 public:
629  TCPQueryConnecter(const std::string &connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
630 
631  void OnFailure() override
632  {
633  NetworkGameList *item = NetworkGameListAddItem(connection_string);
634  item->online = false;
635 
637  }
638 
639  void OnConnect(SOCKET s) override
640  {
641  _networking = true;
642  new ClientNetworkGameSocketHandler(s, this->connection_string);
644  }
645 };
646 
651 void NetworkQueryServer(const std::string &connection_string)
652 {
653  if (!_network_available) return;
654 
655  NetworkInitialize();
656 
657  new TCPQueryConnecter(connection_string);
658 }
659 
669 NetworkGameList *NetworkAddServer(const std::string &connection_string, bool manually, bool never_expire)
670 {
671  if (connection_string.empty()) return nullptr;
672 
673  /* Ensure the item already exists in the list */
674  NetworkGameList *item = NetworkGameListAddItem(connection_string);
675  if (item->info.server_name.empty()) {
677  item->info.server_name = connection_string;
678 
680 
681  NetworkQueryServer(connection_string);
682  }
683 
684  if (manually) item->manually = true;
685  if (never_expire) item->version = INT32_MAX;
686 
687  return item;
688 }
689 
695 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
696 {
697  for (const auto &iter : _network_bind_list) {
698  addresses->emplace_back(iter.c_str(), port);
699  }
700 
701  /* No address, so bind to everything. */
702  if (addresses->size() == 0) {
703  addresses->emplace_back("", port);
704  }
705 }
706 
707 /* Generates the list of manually added hosts from NetworkGameList and
708  * dumps them into the array _network_host_list. This array is needed
709  * by the function that generates the config file. */
710 void NetworkRebuildHostList()
711 {
712  _network_host_list.clear();
713 
714  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
715  if (item->manually) _network_host_list.emplace_back(item->connection_string);
716  }
717 }
718 
721 private:
722  std::string connection_string;
723 
724 public:
725  TCPClientConnecter(const std::string &connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
726 
727  void OnFailure() override
728  {
729  ShowNetworkError(STR_NETWORK_ERROR_NOCONNECTION);
730  }
731 
732  void OnConnect(SOCKET s) override
733  {
734  _networking = true;
735  new ClientNetworkGameSocketHandler(s, this->connection_string);
736  IConsoleCmdExec("exec scripts/on_client.scr 0");
738  }
739 };
740 
758 bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password, const std::string &join_company_password)
759 {
760  CompanyID join_as = default_company;
761  std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT, &join_as).connection_string;
762 
763  if (!_network_available) return false;
764  if (!NetworkValidateOurClientName()) return false;
765 
766  _network_join.connection_string = resolved_connection_string;
767  _network_join.company = join_as;
768  _network_join.server_password = join_server_password;
769  _network_join.company_password = join_company_password;
770 
771  if (_game_mode == GM_MENU) {
772  /* From the menu we can immediately continue with the actual join. */
774  } else {
775  /* When already playing a game, first go back to the main menu. This
776  * disconnects the user from the current game, meaning we can safely
777  * load in the new. After all, there is little point in continueing to
778  * play on a server if we are connecting to another one.
779  */
781  }
782  return true;
783 }
784 
791 {
793  NetworkInitialize();
794 
796  _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
797  ShowJoinStatusWindow();
798 
800 }
801 
802 static void NetworkInitGameInfo()
803 {
805  /* The server is a client too */
807 
808  /* There should be always space for the server. */
812 
814 }
815 
826 bool NetworkValidateServerName(std::string &server_name)
827 {
828  StrTrimInPlace(server_name);
829  if (!server_name.empty()) return true;
830 
831  ShowErrorMessage(STR_NETWORK_ERROR_BAD_SERVER_NAME, INVALID_STRING_ID, WL_ERROR);
832  return false;
833 }
834 
842 {
843  static const std::string fallback_client_name = "Unnamed Client";
845  if (_settings_client.network.client_name.empty() || _settings_client.network.client_name.compare(fallback_client_name) == 0) {
846  Debug(net, 1, "No \"client_name\" has been set, using \"{}\" instead. Please set this now using the \"name <new name>\" command", fallback_client_name);
847  _settings_client.network.client_name = fallback_client_name;
848  }
849 
850  static const std::string fallback_server_name = "Unnamed Server";
852  if (_settings_client.network.server_name.empty() || _settings_client.network.server_name.compare(fallback_server_name) == 0) {
853  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);
854  _settings_client.network.server_name = fallback_server_name;
855  }
856 }
857 
858 bool NetworkServerStart()
859 {
860  if (!_network_available) return false;
861 
862  /* Call the pre-scripts */
863  IConsoleCmdExec("exec scripts/pre_server.scr 0");
864  if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
865 
866  /* Check for the client and server names to be set, but only after the scripts had a chance to set them.*/
868 
869  NetworkDisconnect(false, false);
870  NetworkInitialize(false);
872  Debug(net, 5, "Starting listeners for clients");
874 
875  /* Only listen for admins when the password isn't empty. */
876  if (!_settings_client.network.admin_password.empty()) {
877  Debug(net, 5, "Starting listeners for admins");
879  }
880 
881  /* Try to start UDP-server */
882  Debug(net, 5, "Starting listeners for incoming server queries");
884 
886  _network_server = true;
887  _networking = true;
888  _frame_counter = 0;
890  _frame_counter_max = 0;
891  _last_sync_frame = 0;
893 
896 
897  NetworkInitGameInfo();
898 
899  if (_settings_client.network.server_game_type != SERVER_GAME_TYPE_LOCAL) {
901  }
902 
903  /* execute server initialization script */
904  IConsoleCmdExec("exec scripts/on_server.scr 0");
905  /* if the server is dedicated ... add some other script */
906  if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
907 
908  /* welcome possibly still connected admins - this can only happen on a dedicated server. */
910 
911  return true;
912 }
913 
914 /* The server is rebooting...
915  * The only difference with NetworkDisconnect, is the packets that is sent */
916 void NetworkReboot()
917 {
918  if (_network_server) {
919  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
920  cs->SendNewGame();
921  cs->SendPackets();
922  }
923 
925  as->SendNewGame();
926  as->SendPackets();
927  }
928  }
929 
930  /* For non-dedicated servers we have to kick the admins as we are not
931  * certain that we will end up in a new network game. */
933 }
934 
940 void NetworkDisconnect(bool blocking, bool close_admins)
941 {
942  if (_network_server) {
943  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
944  cs->SendShutdown();
945  cs->SendPackets();
946  }
947 
948  if (close_admins) {
950  as->SendShutdown();
951  as->SendPackets();
952  }
953  }
954  }
955 
957 
958  NetworkClose(close_admins);
959 
960  /* Reinitialize the UDP stack, i.e. close all existing connections. */
962 }
963 
969 {
970  if (!_networking) return;
971 
973  case SERVER_GAME_TYPE_LOCAL:
975  break;
976 
977  case SERVER_GAME_TYPE_INVITE_ONLY:
978  case SERVER_GAME_TYPE_PUBLIC:
980  break;
981 
982  default:
983  NOT_REACHED();
984  }
985 }
986 
991 static bool NetworkReceive()
992 {
993  if (_network_server) {
996  } else {
998  }
999 }
1000 
1001 /* This sends all buffered commands (if possible) */
1002 static void NetworkSend()
1003 {
1004  if (_network_server) {
1007  } else {
1009  }
1010 }
1011 
1018 {
1023 
1025 }
1026 
1027 /* The main loop called from ttd.c
1028  * Here we also have to do StateGameLoop if needed! */
1029 void NetworkGameLoop()
1030 {
1031  if (!_networking) return;
1032 
1033  if (!NetworkReceive()) return;
1034 
1035  if (_network_server) {
1036  /* Log the sync state to check for in-syncedness of replays. */
1037  if (_date_fract == 0) {
1038  /* We don't want to log multiple times if paused. */
1039  static Date last_log;
1040  if (last_log != _date) {
1041  Debug(desync, 1, "sync: {:08x}; {:02x}; {:08x}; {:08x}", _date, _date_fract, _random.state[0], _random.state[1]);
1042  last_log = _date;
1043  }
1044  }
1045 
1046 #ifdef DEBUG_DUMP_COMMANDS
1047  /* Loading of the debug commands from -ddesync>=1 */
1048  static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
1049  static Date next_date = 0;
1050  static uint32 next_date_fract;
1051  static CommandPacket *cp = nullptr;
1052  static bool check_sync_state = false;
1053  static uint32 sync_state[2];
1054  if (f == nullptr && next_date == 0) {
1055  Debug(desync, 0, "Cannot open commands.log");
1056  next_date = 1;
1057  }
1058 
1059  while (f != nullptr && !feof(f)) {
1060  if (_date == next_date && _date_fract == next_date_fract) {
1061  if (cp != nullptr) {
1062  NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text, cp->company);
1063  Debug(desync, 0, "Injecting: {:08x}; {:02x}; {:02x}; {:06x}; {:08x}; {:08x}; {:08x}; \"{}\" ({})", _date, _date_fract, (int)_current_company, cp->tile, cp->p1, cp->p2, cp->cmd, cp->text, GetCommandName(cp->cmd));
1064  delete cp;
1065  cp = nullptr;
1066  }
1067  if (check_sync_state) {
1068  if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
1069  Debug(desync, 0, "Sync check: {:08x}; {:02x}; match", _date, _date_fract);
1070  } else {
1071  Debug(desync, 0, "Sync check: {:08x}; {:02x}; mismatch expected {{:08x}, {:08x}}, got {{:08x}, {:08x}}",
1072  _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
1073  NOT_REACHED();
1074  }
1075  check_sync_state = false;
1076  }
1077  }
1078 
1079  if (cp != nullptr || check_sync_state) break;
1080 
1081  char buff[4096];
1082  if (fgets(buff, lengthof(buff), f) == nullptr) break;
1083 
1084  char *p = buff;
1085  /* Ignore the "[date time] " part of the message */
1086  if (*p == '[') {
1087  p = strchr(p, ']');
1088  if (p == nullptr) break;
1089  p += 2;
1090  }
1091 
1092  if (strncmp(p, "cmd: ", 5) == 0
1093 #ifdef DEBUG_FAILED_DUMP_COMMANDS
1094  || strncmp(p, "cmdf: ", 6) == 0
1095 #endif
1096  ) {
1097  p += 5;
1098  if (*p == ' ') p++;
1099  cp = new CommandPacket();
1100  int company;
1101  char buffer[128];
1102  int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %x; \"%127[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, buffer);
1103  cp->text = buffer;
1104  /* There are 8 pieces of data to read, however the last is a
1105  * string that might or might not exist. Ignore it if that
1106  * string misses because in 99% of the time it's not used. */
1107  assert(ret == 8 || ret == 7);
1108  cp->company = (CompanyID)company;
1109  } else if (strncmp(p, "join: ", 6) == 0) {
1110  /* Manually insert a pause when joining; this way the client can join at the exact right time. */
1111  int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
1112  assert(ret == 2);
1113  Debug(desync, 0, "Injecting pause for join at {:08x}:{:02x}; please join when paused", next_date, next_date_fract);
1114  cp = new CommandPacket();
1115  cp->company = COMPANY_SPECTATOR;
1116  cp->cmd = CMD_PAUSE;
1117  cp->p1 = PM_PAUSED_NORMAL;
1118  cp->p2 = 1;
1119  _ddc_fastforward = false;
1120  } else if (strncmp(p, "sync: ", 6) == 0) {
1121  int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
1122  assert(ret == 4);
1123  check_sync_state = true;
1124  } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
1125  strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
1126  /* A message that is not very important to the log playback, but part of the log. */
1127 #ifndef DEBUG_FAILED_DUMP_COMMANDS
1128  } else if (strncmp(p, "cmdf: ", 6) == 0) {
1129  Debug(desync, 0, "Skipping replay of failed command: {}", p + 6);
1130 #endif
1131  } else {
1132  /* Can't parse a line; what's wrong here? */
1133  Debug(desync, 0, "Trying to parse: {}", p);
1134  NOT_REACHED();
1135  }
1136  }
1137  if (f != nullptr && feof(f)) {
1138  Debug(desync, 0, "End of commands.log");
1139  fclose(f);
1140  f = nullptr;
1141  }
1142 #endif /* DEBUG_DUMP_COMMANDS */
1144  /* Only check for active clients just before we're going to send out
1145  * the commands so we don't send multiple pause/unpause commands when
1146  * the frame_freq is more than 1 tick. Same with distributing commands. */
1147  CheckPauseOnJoin();
1150  }
1151 
1152  bool send_frame = false;
1153 
1154  /* We first increase the _frame_counter */
1155  _frame_counter++;
1156  /* Update max-frame-counter */
1159  send_frame = true;
1160  }
1161 
1163 
1164  /* Then we make the frame */
1165  StateGameLoop();
1166 
1167  _sync_seed_1 = _random.state[0];
1168 #ifdef NETWORK_SEND_DOUBLE_SEED
1169  _sync_seed_2 = _random.state[1];
1170 #endif
1171 
1172  NetworkServer_Tick(send_frame);
1173  } else {
1174  /* Client */
1175 
1176  /* Make sure we are at the frame were the server is (quick-frames) */
1178  /* Run a number of frames; when things go bad, get out. */
1181  }
1182  } else {
1183  /* Else, keep on going till _frame_counter_max */
1185  /* Run one frame; if things went bad, get out. */
1187  }
1188  }
1189  }
1190 
1191  NetworkSend();
1192 }
1193 
1194 static void NetworkGenerateServerId()
1195 {
1196  Md5 checksum;
1197  uint8 digest[16];
1198  char hex_output[16 * 2 + 1];
1199  char coding_string[NETWORK_NAME_LENGTH];
1200  int di;
1201 
1202  seprintf(coding_string, lastof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
1203 
1204  /* Generate the MD5 hash */
1205  checksum.Append((const uint8*)coding_string, strlen(coding_string));
1206  checksum.Finish(digest);
1207 
1208  for (di = 0; di < 16; ++di) {
1209  seprintf(hex_output + di * 2, lastof(hex_output), "%02x", digest[di]);
1210  }
1211 
1212  /* _settings_client.network.network_id is our id */
1213  _settings_client.network.network_id = hex_output;
1214 }
1215 
1217 private:
1218  std::string connection_string;
1219 
1220 public:
1221  TCPNetworkDebugConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_DEFAULT_DEBUGLOG_PORT), connection_string(connection_string) {}
1222 
1223  void OnFailure() override
1224  {
1225  Debug(net, 0, "Failed to open connection to {} for redirecting Debug()", this->connection_string);
1226  }
1227 
1228  void OnConnect(SOCKET s) override
1229  {
1230  Debug(net, 3, "Redirecting Debug() to {}", this->connection_string);
1231 
1232  extern SOCKET _debug_socket;
1233  _debug_socket = s;
1234  }
1235 };
1236 
1237 void NetworkStartDebugLog(const std::string &connection_string)
1238 {
1239  new TCPNetworkDebugConnecter(connection_string);
1240 }
1241 
1244 {
1245  Debug(net, 3, "Starting network");
1246 
1247  /* Network is available */
1249  _network_dedicated = false;
1250 
1251  /* Generate an server id when there is none yet */
1252  if (_settings_client.network.network_id.empty()) NetworkGenerateServerId();
1253 
1254  _network_game_info = {};
1255 
1256  NetworkInitialize();
1258  Debug(net, 3, "Network online, multiplayer available");
1260 }
1261 
1264 {
1265  NetworkDisconnect(true);
1266  NetworkUDPClose();
1267 
1268  Debug(net, 3, "Shutting down network");
1269 
1270  _network_available = false;
1271 
1273 }
1274 
1275 #ifdef __EMSCRIPTEN__
1276 extern "C" {
1277 
1278 void CDECL em_openttd_add_server(const char *connection_string)
1279 {
1280  NetworkAddServer(connection_string, false, true);
1281 }
1282 
1283 }
1284 #endif
network_content.h
TCPConnecter::CheckCallbacks
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:468
NetworkValidateServerName
bool NetworkValidateServerName(std::string &server_name)
Trim the given server name in place, i.e.
Definition: network.cpp:826
host.h
CommandContainer::cmd
uint32 cmd
command being executed.
Definition: command_type.h:483
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3218
NETWORK_RECV_STATUS_CLIENT_QUIT
@ NETWORK_RECV_STATUS_CLIENT_QUIT
The connection is lost gracefully. Other clients are already informed of this leaving client.
Definition: core.h:27
_frame_counter
uint32 _frame_counter
The current frame.
Definition: network.cpp:70
NETWORK_DEFAULT_PORT
static const uint16 NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:28
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:110
ClientNetworkContentSocketHandler::SendReceive
void SendReceive()
Check whether we received/can send some data from/to the content server and when that's the case hand...
Definition: network_content.cpp:799
NetworkValidateOurClientName
bool NetworkValidateOurClientName()
Convenience method for NetworkValidateClientName on _settings_client.network.client_name.
Definition: network_client.cpp:1277
NetworkAddServer
NetworkGameList * NetworkAddServer(const std::string &connection_string, bool manually, bool never_expire)
Validates an address entered as a string and adds the server to the list.
Definition: network.cpp:669
NetworkClientInfo::client_name
std::string client_name
Name of the client.
Definition: network_base.h:26
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:400
NetworkUDPServerListen
void NetworkUDPServerListen()
Start the listening of the UDP server component.
Definition: network_udp.cpp:144
GetBindAddresses
void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
Get the addresses to bind to.
Definition: network.cpp:695
NetworkJoinInfo::company_password
std::string company_password
The password of the company to join.
Definition: network_client.h:119
NetworkSettings::frame_freq
uint8 frame_freq
how often do we send commands to the clients
Definition: settings_type.h:262
NetworkSettings::server_port
uint16 server_port
port the server listens on
Definition: settings_type.h:273
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:71
NETWORK_NAME_LENGTH
static const uint NETWORK_NAME_LENGTH
The maximum length of the server name and map name, in bytes including '\0'.
Definition: config.h:55
NetworkClientInfo::client_playas
CompanyID client_playas
As which company is this client playing (CompanyID)
Definition: network_base.h:27
TD_LTR
@ TD_LTR
Text is written left-to-right by default.
Definition: strings_type.h:23
_date_fract
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:29
NetworkCompanyState
Some state information of a company, especially for servers.
Definition: network_type.h:72
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
_network_company_passworded
CompanyMask _network_company_passworded
Bitmask of the password status of all companies.
Definition: network.cpp:79
GetCommandName
const char * GetCommandName(uint32 cmd)
Definition: command.cpp:407
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:99
WC_CLIENT_LIST
@ WC_CLIENT_LIST
Client list; Window numbers:
Definition: window_type.h:470
TCPNetworkDebugConnecter
Definition: network.cpp:1216
ServerNetworkAdminSocketHandler::WelcomeAll
static void WelcomeAll()
Send a Welcome packet to all connected admins.
Definition: network_admin.cpp:968
NetworkJoinInfo::server_password
std::string server_password
The password of the server to join.
Definition: network_client.h:118
ParseConnectionString
NetworkAddress ParseConnectionString(const std::string &connection_string, uint16 default_port)
Convert a string containing either "hostname" or "hostname:ip" to a NetworkAddress.
Definition: network.cpp:538
ClientNetworkGameSocketHandler::GameLoop
static bool GameLoop()
Actual game loop for the client.
Definition: network_client.cpp:261
TCPQueryConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network.cpp:631
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
NetworkHasJoiningClient
static bool NetworkHasJoiningClient()
Checks whether there is a joining client.
Definition: network.cpp:432
Utf8Encode
size_t Utf8Encode(T buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:616
NetworkJoinInfo::connection_string
std::string connection_string
The address of the server to join.
Definition: network_client.h:116
_networkclientinfo_pool
NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo")
Make sure both pools have the same size.
NetworkSettings::pause_on_join
bool pause_on_join
pause the game when people join
Definition: settings_type.h:272
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
CommandContainer::p2
uint32 p2
parameter p2.
Definition: command_type.h:482
NetworkHandlePauseChange
void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
Handle the pause mode change so we send the right messages to the chat.
Definition: network.cpp:336
ServerNetworkAdminSocketHandler::IterateActive
static Pool::IterateWrapperFiltered< ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter > IterateActive(size_t from=0)
Returns an iterable ensemble of all active admin sockets.
Definition: network_admin.h:95
_random
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:25
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
_network_bind_list
StringList _network_bind_list
The addresses to bind on.
Definition: network.cpp:65
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:66
NetworkGameListAddItem
NetworkGameList * NetworkGameListAddItem(const std::string &connection_string)
Add a new item to the linked gamelist.
Definition: network_gamelist.cpp:32
NetworkBackgroundLoop
void NetworkBackgroundLoop()
We have to do some (simple) background stuff that runs normally, even when we are not in multiplayer.
Definition: network.cpp:1017
network_gui.h
_network_join_status
NetworkJoinStatus _network_join_status
The status of joining.
Definition: network_gui.cpp:2114
GetNetworkErrorMsg
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
Definition: network.cpp:297
ClientNetworkGameSocketHandler::my_client
static ClientNetworkGameSocketHandler * my_client
This is us!
Definition: network_client.h:42
_network_game_info
NetworkServerGameInfo _network_game_info
Information about our game.
Definition: game_info.cpp:37
_redirect_console_to_client
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
Definition: network.cpp:63
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ClientNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
Definition: network_client.cpp:160
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
NetworkSendCommand
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company)
Prepare a DoCommand to be send over the network.
Definition: network_command.cpp:136
ServerAddress::Parse
static ServerAddress Parse(const std::string &connection_string, uint16 default_port, CompanyID *company_id=nullptr)
Convert a string containing either "hostname", "hostname:port" or invite code to a ServerAddress,...
Definition: address.cpp:454
CC_DEFAULT
static const TextColour CC_DEFAULT
Default colour of the console.
Definition: console_type.h:23
_frame_counter_max
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:69
_network_first_time
bool _network_first_time
Whether we have finished joining or not.
Definition: network.cpp:78
NetworkServer_Tick
void NetworkServer_Tick(bool send_frame)
This is called every tick if this is a _network_server.
Definition: network_server.cpp:1634
ServerNetworkAdminSocketHandler::Send
static void Send()
Send the packets for the server sockets.
Definition: network_admin.cpp:96
network_base.h
FillStaticNetworkServerGameInfo
void FillStaticNetworkServerGameInfo()
Fill a NetworkServerGameInfo structure with the static content, or things that are so static they can...
Definition: game_info.cpp:123
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:383
TCPNetworkDebugConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network.cpp:1223
MAX_CHAR_LENGTH
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Definition: strings_type.h:18
_network_game_list
NetworkGameList * _network_game_list
Game list of this client.
Definition: network_gamelist.cpp:23
MAX_LENGTH_COMPANY_NAME_CHARS
static const uint MAX_LENGTH_COMPANY_NAME_CHARS
The maximum length of a company name in characters including '\0'.
Definition: company_type.h:40
NetworkClientInfo::~NetworkClientInfo
~NetworkClientInfo()
Basically a client is leaving us right now.
Definition: network.cpp:101
ClientNetworkCoordinatorSocketHandler::CloseAllConnections
void CloseAllConnections()
Close all pending connection tokens.
Definition: network_coordinator.cpp:684
NetworkChangeCompanyPassword
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password)
Change the company password of a given company.
Definition: network.cpp:155
NetworkQueryServer
void NetworkQueryServer(const std::string &connection_string)
Query a server to fetch the game-info.
Definition: network.cpp:651
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:61
HasClients
bool HasClients()
Return whether there is any client connected or trying to connect at all.
Definition: network.cpp:93
network_coordinator.h
CommandContainer::text
std::string text
possible text sent for name changes etc.
Definition: command_type.h:485
Pool::MAX_SIZE
static constexpr size_t MAX_SIZE
Make template parameter accessible from outside.
Definition: pool_type.hpp:85
COMPANY_FIRST
@ COMPANY_FIRST
First company, same as owner.
Definition: company_type.h:22
NetworkClientInfo::GetByClientID
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
Definition: network.cpp:112
TCPQueryConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network.cpp:639
_frame_counter_server
uint32 _frame_counter_server
The frame_counter of the server, if in network-mode.
Definition: network.cpp:68
NetworkClientJoinGame
void NetworkClientJoinGame()
Actually perform the joining to the server.
Definition: network.cpp:790
NetworkJoinInfo::company
CompanyID company
The company to join.
Definition: network_client.h:117
CommandPacket::company
CompanyID company
company that is executing the command
Definition: network_internal.h:111
CommandContainer::p1
uint32 p1
parameter p1.
Definition: command_type.h:481
COMPANY_NEW_COMPANY
@ COMPANY_NEW_COMPANY
The client wants a new company.
Definition: company_type.h:34
ServerNetworkGameSocketHandler::Send
static void Send()
Send the packets for the server sockets.
Definition: network_server.cpp:318
ClientNetworkGameSocketHandler::SendInformationQuery
static NetworkRecvStatus SendInformationQuery()
Make sure the server ID length is the same as a md5 hash.
Definition: network_client.cpp:334
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:107
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
NetworkSettings::last_joined
std::string last_joined
Last joined server.
Definition: settings_type.h:297
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:21
CMD_PAUSE
@ CMD_PAUSE
pause the game
Definition: command_type.h:256
NormalizeConnectionString
std::string NormalizeConnectionString(const std::string &connection_string, uint16 default_port)
Normalize a connection string.
Definition: network.cpp:523
TCPListenHandler< ServerNetworkAdminSocketHandler, ADMIN_PACKET_SERVER_FULL, ADMIN_PACKET_SERVER_BANNED >::Receive
static bool Receive()
Handle the receiving of packets.
Definition: tcp_listen.h:101
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:245
StrTrimInPlace
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
Definition: string.cpp:355
GenerateCompanyPasswordHash
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed)
Hash the given password using server ID and game seed.
Definition: network.cpp:175
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:47
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
NetworkSettings::min_active_clients
uint8 min_active_clients
minimum amount of active clients to unpause the game
Definition: settings_type.h:295
NetworkGameList::manually
bool manually
True if the server was added manually.
Definition: network_gamelist.h:24
NetworkSettings::admin_password
std::string admin_password
password for the admin network
Definition: settings_type.h:282
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
NetworkClient_Connected
void NetworkClient_Connected()
Is called after a client is connected to the server.
Definition: network_client.cpp:1182
TCPClientConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network.cpp:727
_sync_seed_1
uint32 _sync_seed_1
Seed to compare during sync checks.
Definition: network.cpp:73
ClientNetworkCoordinatorSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
This will put this socket handler in a close state.
Definition: network_coordinator.cpp:433
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
TCPClientConnecter
Non blocking connection create to actually connect to servers.
Definition: network.cpp:720
NetworkDisconnect
void NetworkDisconnect(bool blocking, bool close_admins)
We want to disconnect from the host/clients.
Definition: network.cpp:940
NetworkSettings::client_name
std::string client_name
name of the player (as client)
Definition: settings_type.h:284
CHAR_TD_RLM
static const WChar CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition: string_type.h:40
InitializeNetworkPools
static void InitializeNetworkPools(bool close_admins=true)
Resets the pools used for network clients, and the admin pool if needed.
Definition: network.cpp:565
PoolBase::Clean
static void Clean(PoolType)
Clean all pools of given type.
Definition: pool_func.cpp:30
NetworkShutDown
void NetworkShutDown()
This shuts the network down.
Definition: network.cpp:1263
CLIENT_ID_SERVER
@ CLIENT_ID_SERVER
Servers always have this ID.
Definition: network_type.h:49
PT_NADMIN
@ PT_NADMIN
Network admin pool.
Definition: pool_type.hpp:21
_network_company_states
NetworkCompanyState * _network_company_states
Statistics about some companies.
Definition: network.cpp:61
NETWORK_SERVER_ID_LENGTH
static const uint NETWORK_SERVER_ID_LENGTH
The maximum length of the network id of the servers, in bytes including '\0'.
Definition: config.h:59
NetworkGameList::version
int version
Used to see which servers are no longer available on the Game Coordinator and can be removed.
Definition: network_gamelist.h:26
ParseFullConnectionString
std::string_view ParseFullConnectionString(const std::string &connection_string, uint16 &port, CompanyID *company_id)
Converts a string to ip/port/company Format: IP:port::company.
Definition: network.cpp:503
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:56
CheckPauseHelper
static void CheckPauseHelper(bool pause, PauseMode pm)
Helper function for the pause checkers.
Definition: network.cpp:390
PT_NONE
@ PT_NONE
No pool is selected.
Definition: pool_type.hpp:18
CheckClientAndServerName
static void CheckClientAndServerName()
Check whether the client and server name are set, for a dedicated server and if not set them to some ...
Definition: network.cpp:841
ServerNetworkGameSocketHandler::GetByClientID
static ServerNetworkGameSocketHandler * GetByClientID(ClientID client_id)
Return the client state given it's client-identifier.
Definition: network.cpp:126
network_client.h
CMD_FLAGS_MASK
@ CMD_FLAGS_MASK
mask for all command flags
Definition: command_type.h:381
PauseMode
PauseMode
Modes of pausing we've got.
Definition: openttd.h:60
_network_reconnect
uint8 _network_reconnect
Reconnect timeout.
Definition: network.cpp:64
network_server.h
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:59
ClientNetworkCoordinatorSocketHandler::Register
void Register()
Register our server to receive our invite code.
Definition: network_coordinator.cpp:453
TCPConnecter::KillAll
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:483
PM_PAUSED_GAME_SCRIPT
@ PM_PAUSED_GAME_SCRIPT
A game paused by a game script.
Definition: openttd.h:67
Randomizer::state
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:23
ServerNetworkGameSocketHandler::AcceptConnection
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the accepting of a connection to the server.
Definition: network.cpp:550
PT_NCLIENT
@ PT_NCLIENT
Network client pools.
Definition: pool_type.hpp:20
ParseCompanyFromConnectionString
std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id)
Parse the company part ("#company" postfix) of a connecting string.
Definition: network.cpp:459
NetworkCompanyIsPassworded
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
Definition: network.cpp:211
NetworkClientInfo::client_id
ClientID client_id
Client identifier (same as ClientState->client_id)
Definition: network_base.h:25
StateGameLoop
void StateGameLoop()
State controlling game loop.
Definition: openttd.cpp:1316
_network_own_client_id
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:62
NetworkBackgroundUDPLoop
void NetworkBackgroundUDPLoop()
Receive the UDP packets.
Definition: network_udp.cpp:161
TCPNetworkDebugConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network.cpp:1228
NetworkSettings::server_name
std::string server_name
name of the server
Definition: settings_type.h:279
NetworkCoreShutdown
void NetworkCoreShutdown()
Shuts down the network core (as that is needed for some platforms.
Definition: core.cpp:44
ClientNetworkGameSocketHandler::Receive
static bool Receive()
Check whether we received/can send some data from/to the server and when that's the case handle it ap...
Definition: network_client.cpp:236
_network_content_client
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Definition: network_content.cpp:35
ServerNetworkGameSocketHandler
Class for handling the server side of the game connection.
Definition: network_server.h:24
PM_PAUSED_ACTIVE_CLIENTS
@ PM_PAUSED_ACTIVE_CLIENTS
A game paused for 'min_active_clients'.
Definition: openttd.h:66
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:46
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
NetworkSettings::server_game_type
ServerGameType server_game_type
Server type: local / public / invite-only.
Definition: settings_type.h:276
_ddc_fastforward
#define _ddc_fastforward
Helper variable to make the dedicated server go fast until the (first) join.
Definition: network_internal.h:48
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
Pool::PoolItem<&_networkclientinfo_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
Pool
Base class for all pools.
Definition: pool_type.hpp:81
ClientNetworkCoordinatorSocketHandler::SendReceive
void SendReceive()
Check whether we received/can send some data from/to the Game Coordinator server and when that's the ...
Definition: network_coordinator.cpp:716
network_udp.h
NetworkAddChatMessage
void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const std::string &message)
Add a text message to the 'chat window' to be shown.
Definition: network_chat_gui.cpp:90
NetworkClientSetCompanyPassword
void NetworkClientSetCompanyPassword(const std::string &password)
Set/Reset company password on the client side.
Definition: network_client.cpp:1324
COMPANY_SPECTATOR
@ COMPANY_SPECTATOR
The client is spectating.
Definition: company_type.h:35
ServerNetworkAdminSocketHandler
Class for handling the server side of the game connection.
Definition: network_admin.h:25
CheckPauseOnJoin
static void CheckPauseOnJoin()
Check whether we should pause on join.
Definition: network.cpp:444
_network_clients_connected
byte _network_clients_connected
The amount of clients connected.
Definition: network.cpp:84
WC_NETWORK_STATUS_WINDOW
@ WC_NETWORK_STATUS_WINDOW
Network status window; Window numbers:
Definition: window_type.h:477
NetworkUDPInitialize
void NetworkUDPInitialize()
Initialize the whole UDP bit.
Definition: network_udp.cpp:125
NETWORK_DEFAULT_DEBUGLOG_PORT
static const uint16 NETWORK_DEFAULT_DEBUGLOG_PORT
The default port debug-log is sent to (TCP)
Definition: config.h:30
_network_coordinator_client
ClientNetworkCoordinatorSocketHandler _network_coordinator_client
The connection to the Game Coordinator.
Definition: network_coordinator.cpp:30
PM_PAUSED_NORMAL
@ PM_PAUSED_NORMAL
A game normally paused.
Definition: openttd.h:62
NetworkHTTPSocketHandler::HTTPReceive
static void HTTPReceive()
Do the receiving for all HTTP connections.
Definition: tcp_http.cpp:288
Pool::PoolItem<&_networkclientinfo_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
NetworkCountActiveClients
static uint NetworkCountActiveClients()
Counts the number of active clients connected.
Definition: network.cpp:402
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
udp.h
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:24
TCPServerConnecter
Definition: tcp.h:138
SM_MENU
@ SM_MENU
Switch to game intro menu.
Definition: openttd.h:32
ClientNetworkGameSocketHandler
Class for handling the client side of the game connection.
Definition: network_client.h:16
PM_PAUSED_LINK_GRAPH
@ PM_PAUSED_LINK_GRAPH
A game paused due to the link graph schedule lagging.
Definition: openttd.h:68
NetworkFindBroadcastIPs
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
Find the IPv4 broadcast addresses; IPv6 uses a completely different strategy for broadcasting.
Definition: host.cpp:129
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
PM_PAUSED_JOIN
@ PM_PAUSED_JOIN
A game paused for 'pause_on_join'.
Definition: openttd.h:64
_sync_frame
uint32 _sync_frame
The frame to perform the sync check.
Definition: network.cpp:77
NetworkGameList
Structure with information shown in the game list (GUI)
Definition: network_gamelist.h:18
CheckMinActiveClients
static void CheckMinActiveClients()
Check if the minimum number of active clients has been reached and pause or unpause the game as appro...
Definition: network.cpp:418
NetworkGameSocketHandler::client_id
ClientID client_id
Client identifier.
Definition: tcp_game.h:501
WC_SEND_NETWORK_MSG
@ WC_SEND_NETWORK_MSG
Chatbox; Window numbers:
Definition: window_type.h:489
CHAR_TD_LRM
static const WChar CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition: string_type.h:39
NetworkClientConnectGame
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password, const std::string &join_company_password)
Join a client to the server at with the given connection string.
Definition: network.cpp:758
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
_network_ban_list
StringList _network_ban_list
The banned clients.
Definition: network.cpp:67
NetworkUDPClose
void NetworkUDPClose()
Close all UDP related stuff.
Definition: network_udp.cpp:150
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:594
NetworkClose
void NetworkClose(bool close_admins)
Close current connections.
Definition: network.cpp:574
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1176
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler
ServerNetworkGameSocketHandler(SOCKET s)
Create a new socket for the server side of the game connection.
Definition: network_server.cpp:208
ServerNetworkGameSocketHandler::client_address
NetworkAddress client_address
IP-address of the client (so they can be banned)
Definition: network_server.h:73
NetworkCoreInitialize
bool NetworkCoreInitialize()
Initializes the network core (as that is needed for some platforms.
Definition: core.cpp:24
IConsoleCmdExec
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
Definition: console.cpp:302
NetworkUpdateServerGameType
void NetworkUpdateServerGameType()
The setting server_game_type was updated; possibly we need to take some action.
Definition: network.cpp:968
NetworkGameList::next
NetworkGameList * next
Next pointer to make a linked game list.
Definition: network_gamelist.h:27
network_gamelist.h
DESTTYPE_CLIENT
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
Definition: network_type.h:92
NetworkExecuteLocalCommandQueue
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:190
ClientNetworkGameSocketHandler::SendQuit
static NetworkRecvStatus SendQuit()
Tell the server we would like to quit.
Definition: network_client.cpp:489
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::CloseListeners
static void CloseListeners()
Close the sockets we're listening on.
Definition: tcp_listen.h:164
NetworkServerGameInfo::server_name
std::string server_name
Server name.
Definition: game_info.h:99
NetworkStartUp
void NetworkStartUp()
This tries to launch the network for a given OS.
Definition: network.cpp:1243
_network_host_list
StringList _network_host_list
The servers we know.
Definition: network.cpp:66
network_admin.h
CommandContainer::tile
TileIndex tile
tile command being executed on.
Definition: command_type.h:480
NetworkGameList::info
NetworkGameInfo info
The game information of this server.
Definition: network_gamelist.h:21
NetworkServerGameInfo::clients_on
byte clients_on
Current count of clients on server.
Definition: game_info.h:103
_network_available
bool _network_available
is network mode available?
Definition: network.cpp:58
WN_NETWORK_STATUS_WINDOW_JOIN
@ WN_NETWORK_STATUS_WINDOW_JOIN
Network join status.
Definition: window_type.h:31
NetworkReceive
static bool NetworkReceive()
Receives something from the network.
Definition: network.cpp:991
NetworkServerSetCompanyPassword
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed)
Set/Reset a company password on the server end.
Definition: network_server.cpp:1604
PM_PAUSED_ERROR
@ PM_PAUSED_ERROR
A game paused because a (critical) error.
Definition: openttd.h:65
NetworkFreeLocalCommandQueue
void NetworkFreeLocalCommandQueue()
Free the local command queues.
Definition: network_command.cpp:224
_network_join
NetworkJoinInfo _network_join
Information about the game to join to.
Definition: network_client.cpp:321
SM_JOIN_GAME
@ SM_JOIN_GAME
Join a network game.
Definition: openttd.h:40
GUISettings::network_chat_timeout
uint16 network_chat_timeout
timeout of chat messages in seconds
Definition: settings_type.h:182
ServerAddress::connection_string
std::string connection_string
The connection string for this ServerAddress.
Definition: address.h:212
NetworkGameList::online
bool online
False if the server did not respond (default status)
Definition: network_gamelist.h:23
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
NetworkServerGameInfo::grfconfig
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game_info.h:94
_broadcast_list
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition: network.cpp:72
ClientNetworkGameSocketHandler::Send
static void Send()
Send the packets of this socket handler.
Definition: network_client.cpp:251
NetworkSettings::server_admin_port
uint16 server_admin_port
port the server listens on for the admin network
Definition: settings_type.h:274
TCPQueryConnecter
Non blocking connection to query servers for their game info.
Definition: network.cpp:624
DAY_TICKS
static const int DAY_TICKS
1 day is 74 ticks; _date_fract used to be uint16 and incremented by 885.
Definition: date_type.h:28
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
_last_sync_frame
uint32 _last_sync_frame
Used in the server to store the last time a sync packet was sent to clients.
Definition: network.cpp:71
_is_network_server
bool _is_network_server
Does this client wants to be a network-server?
Definition: network.cpp:60
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:118
NetworkClientInfo
Container for all information known about a client.
Definition: network_base.h:24
TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::Listen
static bool Listen(uint16 port)
Listen on a particular port.
Definition: tcp_listen.h:143
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:296
TCPClientConnecter::OnConnect
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network.cpp:732
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
WL_CRITICAL
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:25
NetworkDistributeCommands
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
Definition: network_command.cpp:278
NetworkSettings::network_id
std::string network_id
network ID for servers
Definition: settings_type.h:287
NETWORK_COMPANY_NAME_LENGTH
static const uint NETWORK_COMPANY_NAME_LENGTH
The maximum length of the company name, in bytes including '\0'.
Definition: config.h:56
IConsolePrint
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console.cpp:94