OpenTTD
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 "core/udp.h"
23 #include "core/host.h"
24 #include "network_gui.h"
25 #include "../console_func.h"
26 #include "../3rdparty/md5/md5.h"
27 #include "../core/random_func.hpp"
28 #include "../window_func.h"
29 #include "../company_func.h"
30 #include "../company_base.h"
31 #include "../landscape_type.h"
32 #include "../rev.h"
33 #include "../core/pool_func.hpp"
34 #include "../gfx_func.h"
35 #include "../error.h"
36 
37 #include "../safeguards.h"
38 
39 #ifdef DEBUG_DUMP_COMMANDS
40 #include "../fileio_func.h"
42 bool _ddc_fastforward = true;
43 #endif /* DEBUG_DUMP_COMMANDS */
44 
47 
51 
71 uint32 _sync_seed_1;
72 #ifdef NETWORK_SEND_DOUBLE_SEED
73 uint32 _sync_seed_2;
74 #endif
75 uint32 _sync_frame;
81 
82 /* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
83 assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
85 
89 
92 
93 /* Some externs / forwards */
94 extern void StateGameLoop();
95 
101 {
102  return !NetworkClientSocket::Iterate().empty();
103 }
104 
109 {
110  /* Delete the chat window, if you were chatting with this client. */
112 }
113 
120 {
122  if (ci->client_id == client_id) return ci;
123  }
124 
125  return nullptr;
126 }
127 
134 {
135  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
136  if (cs->client_id == client_id) return cs;
137  }
138 
139  return nullptr;
140 }
141 
142 byte NetworkSpectatorCount()
143 {
144  byte count = 0;
145 
146  for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
147  if (ci->client_playas == COMPANY_SPECTATOR) count++;
148  }
149 
150  /* Don't count a dedicated server as spectator */
151  if (_network_dedicated) count--;
152 
153  return count;
154 }
155 
162 const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
163 {
164  if (strcmp(password, "*") == 0) password = "";
165 
166  if (_network_server) {
167  NetworkServerSetCompanyPassword(company_id, password, false);
168  } else {
170  }
171 
172  return password;
173 }
174 
182 const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
183 {
184  if (StrEmpty(password)) return password;
185 
186  char salted_password[NETWORK_SERVER_ID_LENGTH];
187 
188  memset(salted_password, 0, sizeof(salted_password));
189  seprintf(salted_password, lastof(salted_password), "%s", password);
190  /* Add the game seed and the server's ID as the salt. */
191  for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
192  salted_password[i] ^= password_server_id[i] ^ (password_game_seed >> (i % 32));
193  }
194 
195  Md5 checksum;
196  uint8 digest[16];
197  static char hashed_password[NETWORK_SERVER_ID_LENGTH];
198 
199  /* Generate the MD5 hash */
200  checksum.Append(salted_password, sizeof(salted_password) - 1);
201  checksum.Finish(digest);
202 
203  for (int di = 0; di < 16; di++) seprintf(hashed_password + di * 2, lastof(hashed_password), "%02x", digest[di]);
204 
205  return hashed_password;
206 }
207 
214 {
215  return HasBit(_network_company_passworded, company_id);
216 }
217 
218 /* This puts a text-message to the console, or in the future, the chat-box,
219  * (to keep it all a bit more general)
220  * If 'self_send' is true, this is the client who is sending the message */
221 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const char *name, const char *str, int64 data)
222 {
223  StringID strid;
224  switch (action) {
225  case NETWORK_ACTION_SERVER_MESSAGE:
226  /* Ignore invalid messages */
227  strid = STR_NETWORK_SERVER_MESSAGE;
228  colour = CC_DEFAULT;
229  break;
230  case NETWORK_ACTION_COMPANY_SPECTATOR:
231  colour = CC_DEFAULT;
232  strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
233  break;
234  case NETWORK_ACTION_COMPANY_JOIN:
235  colour = CC_DEFAULT;
236  strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
237  break;
238  case NETWORK_ACTION_COMPANY_NEW:
239  colour = CC_DEFAULT;
240  strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
241  break;
242  case NETWORK_ACTION_JOIN:
243  /* Show the Client ID for the server but not for the client. */
244  strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID : STR_NETWORK_MESSAGE_CLIENT_JOINED;
245  break;
246  case NETWORK_ACTION_LEAVE: strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
247  case NETWORK_ACTION_NAME_CHANGE: strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
248  case NETWORK_ACTION_GIVE_MONEY: strid = self_send ? STR_NETWORK_MESSAGE_GAVE_MONEY_AWAY : STR_NETWORK_MESSAGE_GIVE_MONEY; break;
249  case NETWORK_ACTION_CHAT_COMPANY: strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
250  case NETWORK_ACTION_CHAT_CLIENT: strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT; break;
251  default: strid = STR_NETWORK_CHAT_ALL; break;
252  }
253 
254  char message[1024];
255  SetDParamStr(0, name);
256  SetDParamStr(1, str);
257  SetDParam(2, data);
258 
259  /* All of these strings start with "***". These characters are interpreted as both left-to-right and
260  * right-to-left characters depending on the context. As the next text might be an user's name, the
261  * user name's characters will influence the direction of the "***" instead of the language setting
262  * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
263  char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
264  GetString(msg_ptr, strid, lastof(message));
265 
266  DEBUG(desync, 1, "msg: %08x; %02x; %s", _date, _date_fract, message);
267  IConsolePrintF(colour, "%s", message);
269 }
270 
271 /* Calculate the frame-lag of a client */
272 uint NetworkCalculateLag(const NetworkClientSocket *cs)
273 {
274  int lag = cs->last_frame_server - cs->last_frame;
275  /* This client has missed his ACK packet after 1 DAY_TICKS..
276  * so we increase his 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 + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
279  lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
280  }
281  return lag;
282 }
283 
284 
285 /* There was a non-recoverable error, drop back to the main menu with a nice
286  * error */
287 void NetworkError(StringID error_string)
288 {
291 }
292 
299 {
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  };
324  assert_compile(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  bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
346  bool paused = (_pause_mode != PM_UNPAUSED);
347  if (!paused && !changed) return;
348 
349  StringID str;
350  if (!changed) {
351  int i = -1;
352 
353  if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL);
354  if ((_pause_mode & PM_PAUSED_JOIN) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS);
355  if ((_pause_mode & PM_PAUSED_GAME_SCRIPT) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT);
356  if ((_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS);
357  str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
358  } else {
359  switch (changed_mode) {
360  case PM_PAUSED_NORMAL: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
361  case PM_PAUSED_JOIN: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
362  case PM_PAUSED_GAME_SCRIPT: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT); break;
363  case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
364  default: NOT_REACHED();
365  }
366  str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
367  }
368 
369  char buffer[DRAW_STRING_BUFFER];
370  GetString(buffer, str, lastof(buffer));
371  NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, nullptr, buffer);
372  break;
373  }
374 
375  default:
376  return;
377  }
378 }
379 
380 
389 static void CheckPauseHelper(bool pause, PauseMode pm)
390 {
391  if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
392 
393  DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
394 }
395 
402 {
403  uint count = 0;
404 
405  for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
406  if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
407  if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
408  count++;
409  }
410 
411  return count;
412 }
413 
418 {
420  !_network_dedicated ||
422  return;
423  }
425 }
426 
432 {
433  for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
434  if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
435  }
436 
437  return false;
438 }
439 
443 static void CheckPauseOnJoin()
444 {
447  return;
448  }
449  CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
450 }
451 
460 void ParseConnectionString(const char **company, const char **port, char *connection_string)
461 {
462  bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
463  char *p;
464  for (p = connection_string; *p != '\0'; p++) {
465  switch (*p) {
466  case '[':
467  ipv6 = true;
468  break;
469 
470  case ']':
471  ipv6 = false;
472  break;
473 
474  case '#':
475  *company = p + 1;
476  *p = '\0';
477  break;
478 
479  case ':':
480  if (ipv6) break;
481  *port = p + 1;
482  *p = '\0';
483  break;
484  }
485  }
486 }
487 
493 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
494 {
495  /* Register the login */
496  _network_clients_connected++;
497 
500  cs->client_address = address; // Save the IP of the client
501 }
502 
507 static void InitializeNetworkPools(bool close_admins = true)
508 {
509  PoolBase::Clean(PT_NCLIENT | (close_admins ? PT_NADMIN : PT_NONE));
510 }
511 
516 void NetworkClose(bool close_admins)
517 {
518  if (_network_server) {
519  if (close_admins) {
521  as->CloseConnection(true);
522  }
523  }
524 
525  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
526  cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
527  }
530  } else if (MyClient::my_client != nullptr) {
533  }
534 
536 
537  _networking = false;
538  _network_server = false;
539 
541 
543  _network_company_states = nullptr;
544 
545  InitializeNetworkPools(close_admins);
546 }
547 
548 /* Initializes the network (cleans sockets and stuff) */
549 static void NetworkInitialize(bool close_admins = true)
550 {
551  InitializeNetworkPools(close_admins);
553 
554  _sync_frame = 0;
555  _network_first_time = true;
556 
557  _network_reconnect = 0;
558 }
559 
562 public:
563  TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
564 
565  void OnFailure() override
566  {
568  }
569 
570  void OnConnect(SOCKET s) override
571  {
572  _networking = true;
575  }
576 };
577 
578 /* Query a server to fetch his game-info
579  * If game_info is true, only the gameinfo is fetched,
580  * else only the client_info is fetched */
581 void NetworkTCPQueryServer(NetworkAddress address)
582 {
583  if (!_network_available) return;
584 
586  NetworkInitialize();
587 
588  new TCPQueryConnecter(address);
589 }
590 
591 /* Validates an address entered as a string and adds the server to
592  * the list. If you use this function, the games will be marked
593  * as manually added. */
594 void NetworkAddServer(const char *b)
595 {
596  if (*b != '\0') {
597  const char *port = nullptr;
598  const char *company = nullptr;
599  char host[NETWORK_HOSTNAME_LENGTH];
600  uint16 rport;
601 
602  strecpy(host, b, lastof(host));
603 
605  rport = NETWORK_DEFAULT_PORT;
606 
607  ParseConnectionString(&company, &port, host);
608  if (port != nullptr) rport = atoi(port);
609 
610  NetworkUDPQueryServer(NetworkAddress(host, rport), true);
611  }
612 }
613 
619 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
620 {
621  for (const auto &iter : _network_bind_list) {
622  addresses->emplace_back(iter.c_str(), port);
623  }
624 
625  /* No address, so bind to everything. */
626  if (addresses->size() == 0) {
627  addresses->emplace_back("", port);
628  }
629 }
630 
631 /* Generates the list of manually added hosts from NetworkGameList and
632  * dumps them into the array _network_host_list. This array is needed
633  * by the function that generates the config file. */
634 void NetworkRebuildHostList()
635 {
636  _network_host_list.clear();
637 
638  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
639  if (item->manually) _network_host_list.emplace_back(item->address.GetAddressAsString(false));
640  }
641 }
642 
645 public:
646  TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
647 
648  void OnFailure() override
649  {
650  NetworkError(STR_NETWORK_ERROR_NOCONNECTION);
651  }
652 
653  void OnConnect(SOCKET s) override
654  {
655  _networking = true;
657  IConsoleCmdExec("exec scripts/on_client.scr 0");
659  }
660 };
661 
662 
663 /* Used by clients, to connect to a server */
664 void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
665 {
666  if (!_network_available) return;
667 
668  if (address.GetPort() == 0) return;
669 
672  _network_join_as = join_as;
673  _network_join_server_password = join_server_password;
674  _network_join_company_password = join_company_password;
675 
677  NetworkInitialize();
678 
679  _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
680  ShowJoinStatusWindow();
681 
682  new TCPClientConnecter(address);
683 }
684 
685 static void NetworkInitGameInfo()
686 {
689  }
690 
691  /* The server is a client too */
692  _network_game_info.clients_on = _network_dedicated ? 0 : 1;
693 
694  /* There should be always space for the server. */
697  ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
698 
700 }
701 
702 bool NetworkServerStart()
703 {
704  if (!_network_available) return false;
705 
706  /* Call the pre-scripts */
707  IConsoleCmdExec("exec scripts/pre_server.scr 0");
708  if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
709 
710  NetworkDisconnect(false, false);
711  NetworkInitialize(false);
712  DEBUG(net, 1, "starting listeners for clients");
714 
715  /* Only listen for admins when the password isn't empty. */
717  DEBUG(net, 1, "starting listeners for admins");
719  }
720 
721  /* Try to start UDP-server */
722  DEBUG(net, 1, "starting listeners for incoming server queries");
723  _network_udp_server = _udp_server_socket->Listen();
724 
725  _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
726  _network_server = true;
727  _networking = true;
728  _frame_counter = 0;
729  _frame_counter_server = 0;
730  _frame_counter_max = 0;
731  _last_sync_frame = 0;
732  _network_own_client_id = CLIENT_ID_SERVER;
733 
734  _network_clients_connected = 0;
735  _network_company_passworded = 0;
736 
737  NetworkInitGameInfo();
738 
739  /* execute server initialization script */
740  IConsoleCmdExec("exec scripts/on_server.scr 0");
741  /* if the server is dedicated ... add some other script */
742  if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
743 
744  /* Try to register us to the master server */
745  _network_need_advertise = true;
747 
748  /* welcome possibly still connected admins - this can only happen on a dedicated server. */
749  if (_network_dedicated) ServerNetworkAdminSocketHandler::WelcomeAll();
750 
751  return true;
752 }
753 
754 /* The server is rebooting...
755  * The only difference with NetworkDisconnect, is the packets that is sent */
756 void NetworkReboot()
757 {
758  if (_network_server) {
759  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
760  cs->SendNewGame();
761  cs->SendPackets();
762  }
763 
765  as->SendNewGame();
766  as->SendPackets();
767  }
768  }
769 
770  /* For non-dedicated servers we have to kick the admins as we are not
771  * certain that we will end up in a new network game. */
772  NetworkClose(!_network_dedicated);
773 }
774 
780 void NetworkDisconnect(bool blocking, bool close_admins)
781 {
782  if (_network_server) {
783  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
784  cs->SendShutdown();
785  cs->SendPackets();
786  }
787 
788  if (close_admins) {
790  as->SendShutdown();
791  as->SendPackets();
792  }
793  }
794  }
795 
797 
799 
800  NetworkClose(close_admins);
801 
802  /* Reinitialize the UDP stack, i.e. close all existing connections. */
804 }
805 
810 static bool NetworkReceive()
811 {
812  if (_network_server) {
815  } else {
817  }
818 }
819 
820 /* This sends all buffered commands (if possible) */
821 static void NetworkSend()
822 {
823  if (_network_server) {
826  } else {
828  }
829 }
830 
837 {
841 
843 }
844 
845 /* The main loop called from ttd.c
846  * Here we also have to do StateGameLoop if needed! */
847 void NetworkGameLoop()
848 {
849  if (!_networking) return;
850 
851  if (!NetworkReceive()) return;
852 
853  if (_network_server) {
854  /* Log the sync state to check for in-syncedness of replays. */
855  if (_date_fract == 0) {
856  /* We don't want to log multiple times if paused. */
857  static Date last_log;
858  if (last_log != _date) {
859  DEBUG(desync, 1, "sync: %08x; %02x; %08x; %08x", _date, _date_fract, _random.state[0], _random.state[1]);
860  last_log = _date;
861  }
862  }
863 
864 #ifdef DEBUG_DUMP_COMMANDS
865  /* Loading of the debug commands from -ddesync>=1 */
866  static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
867  static Date next_date = 0;
868  static uint32 next_date_fract;
869  static CommandPacket *cp = nullptr;
870  static bool check_sync_state = false;
871  static uint32 sync_state[2];
872  if (f == nullptr && next_date == 0) {
873  DEBUG(net, 0, "Cannot open commands.log");
874  next_date = 1;
875  }
876 
877  while (f != nullptr && !feof(f)) {
878  if (_date == next_date && _date_fract == next_date_fract) {
879  if (cp != nullptr) {
880  NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text, cp->company);
881  DEBUG(net, 0, "injecting: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, cp->tile, cp->p1, cp->p2, cp->cmd, cp->text, GetCommandName(cp->cmd));
882  free(cp);
883  cp = nullptr;
884  }
885  if (check_sync_state) {
886  if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
887  DEBUG(net, 0, "sync check: %08x; %02x; match", _date, _date_fract);
888  } else {
889  DEBUG(net, 0, "sync check: %08x; %02x; mismatch expected {%08x, %08x}, got {%08x, %08x}",
890  _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
891  NOT_REACHED();
892  }
893  check_sync_state = false;
894  }
895  }
896 
897  if (cp != nullptr || check_sync_state) break;
898 
899  char buff[4096];
900  if (fgets(buff, lengthof(buff), f) == nullptr) break;
901 
902  char *p = buff;
903  /* Ignore the "[date time] " part of the message */
904  if (*p == '[') {
905  p = strchr(p, ']');
906  if (p == nullptr) break;
907  p += 2;
908  }
909 
910  if (strncmp(p, "cmd: ", 5) == 0
911 #ifdef DEBUG_FAILED_DUMP_COMMANDS
912  || strncmp(p, "cmdf: ", 6) == 0
913 #endif
914  ) {
915  p += 5;
916  if (*p == ' ') p++;
917  cp = CallocT<CommandPacket>(1);
918  int company;
919  int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %x; \"%[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text);
920  /* There are 8 pieces of data to read, however the last is a
921  * string that might or might not exist. Ignore it if that
922  * string misses because in 99% of the time it's not used. */
923  assert(ret == 8 || ret == 7);
924  cp->company = (CompanyID)company;
925  } else if (strncmp(p, "join: ", 6) == 0) {
926  /* Manually insert a pause when joining; this way the client can join at the exact right time. */
927  int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
928  assert(ret == 2);
929  DEBUG(net, 0, "injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
930  cp = CallocT<CommandPacket>(1);
932  cp->cmd = CMD_PAUSE;
933  cp->p1 = PM_PAUSED_NORMAL;
934  cp->p2 = 1;
935  _ddc_fastforward = false;
936  } else if (strncmp(p, "sync: ", 6) == 0) {
937  int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
938  assert(ret == 4);
939  check_sync_state = true;
940  } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
941  strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
942  /* A message that is not very important to the log playback, but part of the log. */
943 #ifndef DEBUG_FAILED_DUMP_COMMANDS
944  } else if (strncmp(p, "cmdf: ", 6) == 0) {
945  DEBUG(net, 0, "Skipping replay of failed command: %s", p + 6);
946 #endif
947  } else {
948  /* Can't parse a line; what's wrong here? */
949  DEBUG(net, 0, "trying to parse: %s", p);
950  NOT_REACHED();
951  }
952  }
953  if (f != nullptr && feof(f)) {
954  DEBUG(net, 0, "End of commands.log");
955  fclose(f);
956  f = nullptr;
957  }
958 #endif /* DEBUG_DUMP_COMMANDS */
959  if (_frame_counter >= _frame_counter_max) {
960  /* Only check for active clients just before we're going to send out
961  * the commands so we don't send multiple pause/unpause commands when
962  * the frame_freq is more than 1 tick. Same with distributing commands. */
966  }
967 
968  bool send_frame = false;
969 
970  /* We first increase the _frame_counter */
971  _frame_counter++;
972  /* Update max-frame-counter */
973  if (_frame_counter > _frame_counter_max) {
974  _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
975  send_frame = true;
976  }
977 
979 
980  /* Then we make the frame */
981  StateGameLoop();
982 
983  _sync_seed_1 = _random.state[0];
984 #ifdef NETWORK_SEND_DOUBLE_SEED
985  _sync_seed_2 = _random.state[1];
986 #endif
987 
988  NetworkServer_Tick(send_frame);
989  } else {
990  /* Client */
991 
992  /* Make sure we are at the frame were the server is (quick-frames) */
993  if (_frame_counter_server > _frame_counter) {
994  /* Run a number of frames; when things go bad, get out. */
995  while (_frame_counter_server > _frame_counter) {
997  }
998  } else {
999  /* Else, keep on going till _frame_counter_max */
1000  if (_frame_counter_max > _frame_counter) {
1001  /* Run one frame; if things went bad, get out. */
1003  }
1004  }
1005  }
1006 
1007  NetworkSend();
1008 }
1009 
1010 static void NetworkGenerateServerId()
1011 {
1012  Md5 checksum;
1013  uint8 digest[16];
1014  char hex_output[16 * 2 + 1];
1015  char coding_string[NETWORK_NAME_LENGTH];
1016  int di;
1017 
1018  seprintf(coding_string, lastof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
1019 
1020  /* Generate the MD5 hash */
1021  checksum.Append((const uint8*)coding_string, strlen(coding_string));
1022  checksum.Finish(digest);
1023 
1024  for (di = 0; di < 16; ++di) {
1025  seprintf(hex_output + di * 2, lastof(hex_output), "%02x", digest[di]);
1026  }
1027 
1028  /* _settings_client.network.network_id is our id */
1030 }
1031 
1032 void NetworkStartDebugLog(NetworkAddress address)
1033 {
1034  extern SOCKET _debug_socket; // Comes from debug.c
1035 
1036  DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
1037 
1038  SOCKET s = address.Connect();
1039  if (s == INVALID_SOCKET) {
1040  DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
1041  return;
1042  }
1043 
1044  _debug_socket = s;
1045 
1046  DEBUG(net, 0, "DEBUG() is now redirected");
1047 }
1048 
1051 {
1052  DEBUG(net, 3, "[core] starting network...");
1053 
1054  /* Network is available */
1055  _network_available = NetworkCoreInitialize();
1056  _network_dedicated = false;
1057  _network_need_advertise = true;
1058  _network_advertise_retries = 0;
1059 
1060  /* Generate an server id when there is none yet */
1061  if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
1062 
1063  memset(&_network_game_info, 0, sizeof(_network_game_info));
1064 
1065  NetworkInitialize();
1066  DEBUG(net, 3, "[core] network online, multiplayer available");
1067  NetworkFindBroadcastIPs(&_broadcast_list);
1068 }
1069 
1072 {
1073  NetworkDisconnect(true);
1074  NetworkUDPClose();
1075 
1076  DEBUG(net, 3, "[core] shutting down network");
1077 
1078  _network_available = false;
1079 
1081 }
1082 
1087 static const uint GITHASH_SUFFIX_LEN = 12;
1088 
1094 {
1095  /* This will be allocated on heap and never free'd, but only once so not a "real" leak. */
1096  static char *network_revision = nullptr;
1097 
1098  if (!network_revision) {
1099  /* Start by taking a chance on the full revision string. */
1100  network_revision = stredup(_openttd_revision);
1101  /* Ensure it's not longer than the packet buffer length. */
1102  if (strlen(network_revision) >= NETWORK_REVISION_LENGTH) network_revision[NETWORK_REVISION_LENGTH - 1] = '\0';
1103 
1104  /* Tag names are not mangled further. */
1105  if (_openttd_revision_tagged) {
1106  DEBUG(net, 1, "Network revision name is '%s'", network_revision);
1107  return network_revision;
1108  }
1109 
1110  /* Prepare a prefix of the git hash.
1111  * Size is length + 1 for terminator, +2 for -g prefix. */
1112  assert(_openttd_revision_modified < 3);
1113  char githash_suffix[GITHASH_SUFFIX_LEN + 1] = "-";
1114  githash_suffix[1] = "gum"[_openttd_revision_modified];
1115  for (uint i = 2; i < GITHASH_SUFFIX_LEN; i++) {
1116  githash_suffix[i] = _openttd_revision_hash[i-2];
1117  }
1118 
1119  /* Where did the hash start in the original string?
1120  * Overwrite from that position, unless that would go past end of packet buffer length. */
1121  ptrdiff_t hashofs = strrchr(_openttd_revision, '-') - _openttd_revision;
1122  if (hashofs + strlen(githash_suffix) + 1 > NETWORK_REVISION_LENGTH) hashofs = strlen(network_revision) - strlen(githash_suffix);
1123  /* Replace the git hash in revision string. */
1124  strecpy(network_revision + hashofs, githash_suffix, network_revision + NETWORK_REVISION_LENGTH);
1125  assert(strlen(network_revision) < NETWORK_REVISION_LENGTH); // strlen does not include terminator, constant does, hence strictly less than
1126  DEBUG(net, 1, "Network revision name is '%s'", network_revision);
1127  }
1128 
1129  return network_revision;
1130 }
1131 
1132 static const char *ExtractNetworkRevisionHash(const char *revstr)
1133 {
1134  return strrchr(revstr, '-');
1135 }
1136 
1142 bool IsNetworkCompatibleVersion(const char *other)
1143 {
1144  if (strncmp(GetNetworkRevisionString(), other, NETWORK_REVISION_LENGTH - 1) == 0) return true;
1145 
1146  /* If this version is tagged, then the revision string must be a complete match,
1147  * since there is no git hash suffix in it.
1148  * This is needed to avoid situations like "1.9.0-beta1" comparing equal to "2.0.0-beta1". */
1149  if (_openttd_revision_tagged) return false;
1150 
1151  const char *hash1 = ExtractNetworkRevisionHash(GetNetworkRevisionString());
1152  const char *hash2 = ExtractNetworkRevisionHash(other);
1153  return hash1 && hash2 && (strncmp(hash1, hash2, GITHASH_SUFFIX_LEN) == 0);
1154 }
Owner
Enum for all companies/owners.
Definition: company_type.h:18
void NetworkServer_Tick(bool send_frame)
This is called every tick if this is a _network_server.
A game paused for &#39;min_active_clients&#39;.
Definition: openttd.h:61
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network.cpp:565
static bool NetworkReceive()
Receives something from the network.
Definition: network.cpp:810
void ParseConnectionString(const char **company, const char **port, char *connection_string)
Converts a string to ip/port/company Format: IP:port::company.
Definition: network.cpp:460
bool _networking
are we in networking mode?
Definition: network.cpp:52
static const WChar CHAR_TD_LRM
The next character acts like a left-to-right character.
Definition: string_type.h:39
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
Prepare a DoCommand to be send over the network.
const char * GetCommandName(uint32 cmd)
Definition: command.cpp:398
bool IsNetworkCompatibleVersion(const char *other)
Checks whether the given version string is compatible with our version.
Definition: network.cpp:1142
Container for all information known about a client.
Definition: network_base.h:23
uint32 _sync_seed_1
Seed to compare during sync checks.
Definition: network.cpp:71
bool server_advertise
advertise the server to the masterserver
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3215
static void WelcomeAll()
Send a Welcome packet to all connected admins.
static bool Receive()
Check whether we received/can send some data from/to the server and when that&#39;s the case handle it ap...
void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
Get the addresses to bind to.
Definition: network.cpp:619
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:92
uint16 last_port
port of the last joined server
Client list; Window numbers:
Definition: window_type.h:472
bool NetworkCoreInitialize()
Initializes the network core (as that is needed for some platforms.
Definition: core.cpp:24
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:62
char server_name[NETWORK_NAME_LENGTH]
name of the server
NetworkUDPSocketHandler * _udp_client_socket
udp client socket
Definition: network_udp.cpp:46
const char * _network_join_server_password
Login password from -p argument.
The game information that is not generated on-the-fly and has to be sent to the clients.
Definition: game.h:24
Switch to game intro menu.
Definition: openttd.h:30
const char * _network_join_company_password
Company password from -P argument.
NetworkGameList * next
Next pointer to make a linked game list.
~NetworkClientInfo()
Basically a client is leaving us right now.
Definition: network.cpp:108
GUIs related to networking.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:100
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
ClientID client_id
Client identifier (same as ClientState->client_id)
Definition: network_base.h:24
NetworkJoinStatus _network_join_status
The status of joining.
uint16 _network_udp_broadcast
Timeout for the UDP broadcasts.
Definition: network.cpp:78
uint32 p2
parameter p2.
Definition: command_type.h:478
Sending and receiving UDP messages.
Client part of the network protocol.
static void Send()
Send the packets for the server sockets.
void SendReceive()
Check whether we received/can send some data from/to the content server and when that&#39;s the case hand...
#define _ddc_fastforward
Helper variable to make the dedicated server go fast until the (first) join.
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:27
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
Definition: network.cpp:60
static void Clean(PoolType)
Clean all pools of given type.
Definition: pool_func.cpp:30
void NetworkStartUp()
This tries to launch the network for a given OS.
Definition: network.cpp:1050
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:380
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
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:83
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
Find the IPv4 broadcast addresses; IPv6 uses a completely different strategy for broadcasting.
Definition: host.cpp:194
Send message/notice to only a certain client (Private)
Definition: network_type.h:81
Base socket handler for all UDP sockets.
Definition: udp.h:46
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
static const TextColour CC_DEFAULT
Default colour of the console.
Definition: console_type.h:23
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
Definition: network.cpp:213
Base core network types and some helper functions to access them.
Structure with information shown in the game list (GUI)
void NetworkUDPQueryServer(NetworkAddress address, bool manually)
Query a specific server.
Definition: network_udp.cpp:78
Class for handling the client side of the game connection.
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:39
static void HTTPReceive()
Do the receiving for all HTTP connections.
Definition: tcp_http.cpp:295
NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo")
The pool with client information.
uint32 _sync_frame
The frame to perform the sync check.
Definition: network.cpp:75
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
char text[32 *MAX_CHAR_LENGTH]
possible text sent for name changes etc, in bytes including &#39;\0&#39;.
Definition: command_type.h:481
bool _network_udp_server
Is the UDP server started?
Definition: network.cpp:77
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network.cpp:570
Class for handling the server side of the game connection.
void NetworkUDPAdvertise()
Register us to the master server This function checks if it needs to send an advertise.
StringID GetNetworkErrorMsg(NetworkErrorCode err)
Retrieve the string id of an internal error number.
Definition: network.cpp:298
bool _network_available
is network mode available?
Definition: network.cpp:54
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:55
Network client pools.
Definition: pool_type.hpp:21
assert_compile(NetworkClientInfoPool::MAX_SIZE==NetworkClientSocketPool::MAX_SIZE)
Make sure both pools have the same size.
static const size_t MAX_SIZE
Make template parameter accessible from outside.
Definition: pool_type.hpp:86
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:18
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:24
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition: network.cpp:70
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
void StateGameLoop()
State controlling game loop.
Definition: openttd.cpp:1328
NetworkSettings network
settings related to the network
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Definition: strings_type.h:18
bool _is_network_server
Does this client wants to be a network-server?
Definition: network.cpp:56
CompanyID client_playas
As which company is this client playing (CompanyID)
Definition: network_base.h:27
mask for all command flags
Definition: command_type.h:377
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:27
NetworkAddress client_address
IP-address of the client (so he can be banned)
A normal unpaused game.
Definition: openttd.h:56
The client is spectating.
Definition: company_type.h:35
static uint NetworkCountActiveClients()
Counts the number of active clients connected.
Definition: network.cpp:401
char client_name[NETWORK_CLIENT_NAME_LENGTH]
Name of the client.
Definition: network_base.h:25
static Pool::IterateWrapper< ServerNetworkAdminSocketHandler > IterateActive(size_t from=0)
Returns an iterable ensemble of all active admin sockets.
Definition: network_admin.h:91
Chatbox; Window numbers:
Definition: window_type.h:491
Server part of the network protocol.
bool _network_need_advertise
Whether we need to advertise.
Definition: network.cpp:61
static ServerNetworkGameSocketHandler * GetByClientID(ClientID client_id)
Return the client state given it&#39;s client-identifier.
Definition: network.cpp:133
uint32 p1
parameter p1.
Definition: command_type.h:477
void IConsoleCmdExec(const char *cmdstr)
Execute a given command passed to us.
Definition: console.cpp:401
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:65
Non blocking connection create to query servers.
Definition: network.cpp:561
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:59
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:463
void CDECL IConsolePrintF(TextColour colour_code, const char *format,...)
Handle the printing of text entered into the console or redirected there by any other means...
Definition: console.cpp:124
void NetworkCoreShutdown()
Shuts down the network core (as that is needed for some platforms.
Definition: core.cpp:44
void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
Set/Reset a company password on the server end.
Resolving of hostnames/IPs.
static const uint MAX_LENGTH_COMPANY_NAME_CHARS
The maximum length of a company name in characters including &#39;\0&#39;.
Definition: company_type.h:40
static void InitializeNetworkPools(bool close_admins=true)
Resets the pools used for network clients, and the admin pool if needed.
Definition: network.cpp:507
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:245
char connect_to_ip[NETWORK_HOSTNAME_LENGTH]
default for the "Add server" query
static void AcceptConnection(SOCKET s, const NetworkAddress &address)
Handle the accepting of a connection to the server.
Definition: network.cpp:493
NetworkCompanyState * _network_company_states
Statistics about some companies.
Definition: network.cpp:58
uint16 server_admin_port
port the server listens on for the admin network
const char * GetNetworkRevisionString()
Get the network version string used by this build.
Definition: network.cpp:1093
uint8 _network_reconnect
Reconnect timeout.
Definition: network.cpp:62
static void CheckPauseHelper(bool pause, PauseMode pm)
Helper function for the pause checkers.
Definition: network.cpp:389
static ClientNetworkGameSocketHandler * my_client
This is us!
Base directory for all savegames.
Definition: fileio_type.h:110
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
Definition: network.cpp:653
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
const char * GetHostname()
Get the hostname; in case it wasn&#39;t given the IPv4 dotted representation is given.
Definition: address.cpp:22
byte clients_on
Current count of clients on server.
Definition: game.h:26
NetworkGameList * _network_game_list
Game list of this client.
A game normally paused.
Definition: openttd.h:57
static void CheckPauseOnJoin()
Check whether we should pause on join.
Definition: network.cpp:443
Part of the network protocol handling content distribution.
void NetworkShutDown()
This shuts the network down.
Definition: network.cpp:1071
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:532
CompanyID company
company that is executing the command
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
NetworkServerGameInfo _network_game_info
Information about our game.
Definition: network.cpp:57
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including &#39;\0&#39;.
Definition: config.h:42
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Text is written left-to-right by default.
Definition: strings_type.h:23
PauseMode
Modes of pausing we&#39;ve got.
Definition: openttd.h:55
bool Listen()
Start listening on the given host and port.
Definition: udp.cpp:43
void NetworkClient_Connected()
Is called after a client is connected to the server.
static bool NetworkHasJoiningClient()
Checks whether there is a joining client.
Definition: network.cpp:431
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network.cpp:648
Base class for all pools.
Definition: pool_type.hpp:82
The connection is &#39;just&#39; lost.
Definition: core.h:27
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
void NetworkUDPInitialize()
Initialize the whole UDP bit.
Network status window; Window numbers:
Definition: window_type.h:485
void NetworkFreeLocalCommandQueue()
Free the local command queues.
StringList _network_host_list
The servers we know.
Definition: network.cpp:64
uint16 server_port
port the server listens on
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1162
uint16 network_chat_timeout
timeout of chat messages in seconds
static NetworkRecvStatus SendCompanyInformationQuery()
Query the server for company information.
bool HasClients()
Return whether there is any client connected or trying to connect at all.
Definition: network.cpp:100
bool pause_on_join
pause the game when people join
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:320
Network join status.
Definition: window_type.h:32
byte _network_clients_connected
The amount of clients connected.
Definition: network.cpp:91
static const uint16 NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:29
char client_name[NETWORK_CLIENT_NAME_LENGTH]
name of the player (as client)
GUISettings gui
settings related to the GUI
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
uint32 _frame_counter
The current frame.
Definition: network.cpp:68
Non blocking connection create to actually connect to servers.
Definition: network.cpp:644
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:25
Handling of the list of games.
No pool is selected.
Definition: pool_type.hpp:19
char network_id[NETWORK_SERVER_ID_LENGTH]
network ID for servers
First company, same as owner.
Definition: company_type.h:22
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
CompanyID _network_join_as
Who would we like to join as.
CompanyMask _network_company_passworded
Bitmask of the password status of all companies.
Definition: network.cpp:80
void NetworkClientSetCompanyPassword(const char *password)
Set/Reset company password on the client side.
void NetworkBackgroundUDPLoop()
Receive the UDP packets.
uint8 frame_freq
how often do we send commands to the clients
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
static const uint NETWORK_COMPANY_NAME_LENGTH
The maximum length of the company name, in bytes including &#39;\0&#39;.
Definition: config.h:41
NetworkClientInfo(ClientID client_id=INVALID_CLIENT_ID)
Create a new client.
Definition: network_base.h:34
void NetworkClose(bool close_admins)
Close current connections.
Definition: network.cpp:516
uint32 _frame_counter_server
The frame_counter of the server, if in network-mode.
Definition: network.cpp:66
static const WChar CHAR_TD_RLM
The next character acts like a right-to-left character.
Definition: string_type.h:40
static const uint NETWORK_SERVER_ID_LENGTH
The maximum length of the network id of the servers, in bytes including &#39;\0&#39;.
Definition: config.h:43
Class for handling the server side of the game connection.
Definition: network_admin.h:25
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
Maximum number of companies.
Definition: company_type.h:23
StringList _network_ban_list
The banned clients.
Definition: network.cpp:65
void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const char *message,...)
Add a text message to the &#39;chat window&#39; to be shown.
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:46
const char * GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
Hash the given password using server ID and game seed.
Definition: network.cpp:182
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:261
char admin_password[NETWORK_PASSWORD_LENGTH]
password for the admin network
bool _network_server
network-server is active
Definition: network.cpp:53
static NetworkRecvStatus SendQuit()
Tell the server we would like to quit.
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
void NetworkUDPRemoveAdvertise(bool blocking)
Remove our advertise from the master-server.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
void NetworkBackgroundLoop()
We have to do some (simple) background stuff that runs normally, even when we are not in multiplayer...
Definition: network.cpp:836
TileIndex tile
tile command being executed on.
Definition: command_type.h:476
uint32 _last_sync_frame
Used in the server to store the last time a sync packet was sent to clients.
Definition: network.cpp:69
uint16 GetPort() const
Get the port.
Definition: address.cpp:35
void NetworkDisconnect(bool blocking, bool close_admins)
We want to disconnect from the host/clients.
Definition: network.cpp:780
Everything we need to know about a command to be able to execute it.
static void Send()
Send the packets of this socket handler.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
size_t Utf8Encode(char *buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:486
int32 Date
The type to store our dates in.
Definition: date_type.h:14
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
char last_host[NETWORK_HOSTNAME_LENGTH]
IP address of the last joined server.
static const uint NETWORK_NAME_LENGTH
The maximum length of the server name and map name, in bytes including &#39;\0&#39;.
Definition: config.h:40
uint8 _network_advertise_retries
The number of advertisement retries we did.
Definition: network.cpp:79
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Servers always have this ID.
Definition: network_type.h:41
A game paused by a game script.
Definition: openttd.h:62
Some state information of a company, especially for servers.
Definition: network_type.h:64
StringList _network_bind_list
The addresses to bind on.
Definition: network.cpp:63
static const uint NETWORK_NUM_LANDSCAPES
The number of landscapes in OpenTTD.
Definition: config.h:70
NetworkUDPSocketHandler * _udp_master_socket
udp master socket
Definition: network_udp.cpp:48
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it&#39;s client-identifier.
Definition: network.cpp:119
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:23
Network admin pool.
Definition: pool_type.hpp:22
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:417
const char * NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
Change the company password of a given company.
Definition: network.cpp:162
static const uint NETWORK_REVISION_LENGTH
The maximum length of the revision, in bytes including &#39;\0&#39;.
Definition: config.h:44
NetworkUDPSocketHandler * _udp_server_socket
udp server socket
Definition: network_udp.cpp:47
void NetworkUDPClose()
Close all UDP related stuff.
Date _date
Current date in days (day counter)
Definition: date.cpp:26
A game paused for &#39;pause_on_join&#39;.
Definition: openttd.h:59
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:67
static bool GameLoop()
Actual game loop for the client.
bool _network_first_time
Whether we have finished joining or not.
Definition: network.cpp:76
static void Send()
Send the packets for the server sockets.
uint32 cmd
command being executed.
Definition: command_type.h:479
Basic functions to receive and send UDP packets.
A game paused because a (critical) error.
Definition: openttd.h:60
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:85
static const uint GITHASH_SUFFIX_LEN
How many hex digits of the git hash to include in network revision string.
Definition: network.cpp:1087
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
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:3316
Server part of the admin network protocol.
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:199
uint8 min_active_clients
minimum amount of active clients to unpause the game
pause the game
Definition: command_type.h:254