network.cpp

Go to the documentation of this file.
00001 /* $Id: network.cpp 22032 2011-02-08 21:47:10Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 
00014 #ifdef ENABLE_NETWORK
00015 
00016 #include "../strings_func.h"
00017 #include "../command_func.h"
00018 #include "../date_func.h"
00019 #include "network_admin.h"
00020 #include "network_client.h"
00021 #include "network_server.h"
00022 #include "network_content.h"
00023 #include "network_udp.h"
00024 #include "network_gamelist.h"
00025 #include "network_base.h"
00026 #include "core/udp.h"
00027 #include "core/host.h"
00028 #include "network_gui.h"
00029 #include "../console_func.h"
00030 #include "../3rdparty/md5/md5.h"
00031 #include "../core/random_func.hpp"
00032 #include "../window_func.h"
00033 #include "../company_func.h"
00034 #include "../company_base.h"
00035 #include "../landscape_type.h"
00036 #include "../rev.h"
00037 #include "../core/pool_func.hpp"
00038 #include "../gfx_func.h"
00039 #include "table/strings.h"
00040 
00041 #ifdef DEBUG_DUMP_COMMANDS
00042 #include "../fileio_func.h"
00044 bool _ddc_fastforward = true;
00045 #endif /* DEBUG_DUMP_COMMANDS */
00046 
00047 assert_compile(NetworkClientInfoPool::MAX_SIZE == NetworkClientSocketPool::MAX_SIZE);
00048 
00049 NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo");
00050 INSTANTIATE_POOL_METHODS(NetworkClientInfo)
00051 
00052 bool _networking;         
00053 bool _network_server;     
00054 bool _network_available;  
00055 bool _network_dedicated;  
00056 bool _is_network_server;  
00057 NetworkServerGameInfo _network_game_info;
00058 NetworkCompanyState *_network_company_states = NULL;
00059 ClientID _network_own_client_id;
00060 ClientID _redirect_console_to_client;
00061 bool _network_need_advertise;
00062 uint32 _network_last_advertise_frame;
00063 uint8 _network_reconnect;
00064 StringList _network_bind_list;
00065 StringList _network_host_list;
00066 StringList _network_ban_list;
00067 uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
00068 uint32 _frame_counter_max; // To where we may go with our clients
00069 uint32 _frame_counter;
00070 uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
00071 NetworkAddressList _broadcast_list;
00072 uint32 _sync_seed_1;
00073 #ifdef NETWORK_SEND_DOUBLE_SEED
00074 uint32 _sync_seed_2;
00075 #endif
00076 uint32 _sync_frame;
00077 bool _network_first_time;
00078 bool _network_udp_server;
00079 uint16 _network_udp_broadcast;
00080 uint8 _network_advertise_retries;
00081 CompanyMask _network_company_passworded; 
00082 
00083 /* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
00084 assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
00085 assert_compile((int)NETWORK_COMPANY_NAME_LENGTH == MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH);
00086 
00087 extern NetworkUDPSocketHandler *_udp_client_socket; 
00088 extern NetworkUDPSocketHandler *_udp_server_socket; 
00089 extern NetworkUDPSocketHandler *_udp_master_socket; 
00090 
00091 /* The amount of clients connected */
00092 byte _network_clients_connected = 0;
00093 
00094 /* Some externs / forwards */
00095 extern void StateGameLoop();
00096 
00100 NetworkClientInfo::~NetworkClientInfo()
00101 {
00102   /* Delete the chat window, if you were chatting with this client. */
00103   InvalidateWindowData(WC_SEND_NETWORK_MSG, DESTTYPE_CLIENT, this->client_id);
00104 }
00105 
00111 NetworkClientInfo *NetworkFindClientInfoFromClientID(ClientID client_id)
00112 {
00113   NetworkClientInfo *ci;
00114 
00115   FOR_ALL_CLIENT_INFOS(ci) {
00116     if (ci->client_id == client_id) return ci;
00117   }
00118 
00119   return NULL;
00120 }
00121 
00127 NetworkClientSocket *NetworkFindClientStateFromClientID(ClientID client_id)
00128 {
00129   NetworkClientSocket *cs;
00130 
00131   FOR_ALL_CLIENT_SOCKETS(cs) {
00132     if (cs->client_id == client_id) return cs;
00133   }
00134 
00135   return NULL;
00136 }
00137 
00138 byte NetworkSpectatorCount()
00139 {
00140   const NetworkClientInfo *ci;
00141   byte count = 0;
00142 
00143   FOR_ALL_CLIENT_INFOS(ci) {
00144     if (ci->client_playas == COMPANY_SPECTATOR) count++;
00145   }
00146 
00147   /* Don't count a dedicated server as spectator */
00148   if (_network_dedicated) count--;
00149 
00150   return count;
00151 }
00152 
00159 const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
00160 {
00161   if (strcmp(password, "*") == 0) password = "";
00162 
00163   if (_network_server) {
00164     NetworkServerSetCompanyPassword(company_id, password, already_hashed);
00165   } else {
00166     NetworkClientSetCompanyPassword(password);
00167   }
00168 
00169   return password;
00170 }
00171 
00179 const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
00180 {
00181   if (StrEmpty(password)) return password;
00182 
00183   char salted_password[NETWORK_SERVER_ID_LENGTH];
00184 
00185   memset(salted_password, 0, sizeof(salted_password));
00186   snprintf(salted_password, sizeof(salted_password), "%s", password);
00187   /* Add the game seed and the server's ID as the salt. */
00188   for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
00189     salted_password[i] ^= password_server_id[i] ^ (password_game_seed >> (i % 32));
00190   }
00191 
00192   Md5 checksum;
00193   uint8 digest[16];
00194   static char hashed_password[NETWORK_SERVER_ID_LENGTH];
00195 
00196   /* Generate the MD5 hash */
00197   checksum.Append(salted_password, sizeof(salted_password) - 1);
00198   checksum.Finish(digest);
00199 
00200   for (int di = 0; di < 16; di++) sprintf(hashed_password + di * 2, "%02x", digest[di]);
00201   hashed_password[lengthof(hashed_password) - 1] = '\0';
00202 
00203   return hashed_password;
00204 }
00205 
00211 bool NetworkCompanyIsPassworded(CompanyID company_id)
00212 {
00213   return HasBit(_network_company_passworded, company_id);
00214 }
00215 
00216 /* This puts a text-message to the console, or in the future, the chat-box,
00217  *  (to keep it all a bit more general)
00218  * If 'self_send' is true, this is the client who is sending the message */
00219 void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const char *name, const char *str, int64 data)
00220 {
00221   StringID strid;
00222   switch (action) {
00223     case NETWORK_ACTION_SERVER_MESSAGE:
00224       /* Ignore invalid messages */
00225       strid = STR_NETWORK_SERVER_MESSAGE;
00226       colour = CC_DEFAULT;
00227       break;
00228     case NETWORK_ACTION_COMPANY_SPECTATOR:
00229       colour = CC_DEFAULT;
00230       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
00231       break;
00232     case NETWORK_ACTION_COMPANY_JOIN:
00233       colour = CC_DEFAULT;
00234       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
00235       break;
00236     case NETWORK_ACTION_COMPANY_NEW:
00237       colour = CC_DEFAULT;
00238       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
00239       break;
00240     case NETWORK_ACTION_JOIN:
00241       /* Show the Client ID for the server but not for the client. */
00242       strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID :  STR_NETWORK_MESSAGE_CLIENT_JOINED;
00243       break;
00244     case NETWORK_ACTION_LEAVE:          strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
00245     case NETWORK_ACTION_NAME_CHANGE:    strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
00246     case NETWORK_ACTION_GIVE_MONEY:     strid = self_send ? STR_NETWORK_MESSAGE_GAVE_MONEY_AWAY : STR_NETWORK_MESSAGE_GIVE_MONEY;   break;
00247     case NETWORK_ACTION_CHAT_COMPANY:   strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
00248     case NETWORK_ACTION_CHAT_CLIENT:    strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT  : STR_NETWORK_CHAT_CLIENT;  break;
00249     default:                            strid = STR_NETWORK_CHAT_ALL; break;
00250   }
00251 
00252   char message[1024];
00253   SetDParamStr(0, name);
00254   SetDParamStr(1, str);
00255   SetDParam(2, data);
00256 
00257   /* All of these strings start with "***". These characters are interpreted as both left-to-right and
00258    * right-to-left characters depending on the context. As the next text might be an user's name, the
00259    * user name's characters will influence the direction of the "***" instead of the language setting
00260    * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
00261   char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
00262   GetString(msg_ptr, strid, lastof(message));
00263 
00264   DEBUG(desync, 1, "msg: %08x; %02x; %s", _date, _date_fract, message);
00265   IConsolePrintF(colour, "%s", message);
00266   NetworkAddChatMessage((TextColour)colour, _settings_client.gui.network_chat_timeout, "%s", message);
00267 }
00268 
00269 /* Calculate the frame-lag of a client */
00270 uint NetworkCalculateLag(const NetworkClientSocket *cs)
00271 {
00272   int lag = cs->last_frame_server - cs->last_frame;
00273   /* This client has missed his ACK packet after 1 DAY_TICKS..
00274    *  so we increase his lag for every frame that passes!
00275    * The packet can be out by a max of _net_frame_freq */
00276   if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
00277     lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
00278   }
00279   return lag;
00280 }
00281 
00282 
00283 /* There was a non-recoverable error, drop back to the main menu with a nice
00284  *  error */
00285 void NetworkError(StringID error_string)
00286 {
00287   _switch_mode = SM_MENU;
00288   extern StringID _switch_mode_errorstr;
00289   _switch_mode_errorstr = error_string;
00290 }
00291 
00297 StringID GetNetworkErrorMsg(NetworkErrorCode err)
00298 {
00299   /* List of possible network errors, used by
00300    * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
00301   static const StringID network_error_strings[] = {
00302     STR_NETWORK_ERROR_CLIENT_GENERAL,
00303     STR_NETWORK_ERROR_CLIENT_DESYNC,
00304     STR_NETWORK_ERROR_CLIENT_SAVEGAME,
00305     STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST,
00306     STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR,
00307     STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH,
00308     STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED,
00309     STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED,
00310     STR_NETWORK_ERROR_CLIENT_WRONG_REVISION,
00311     STR_NETWORK_ERROR_CLIENT_NAME_IN_USE,
00312     STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD,
00313     STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH,
00314     STR_NETWORK_ERROR_CLIENT_KICKED,
00315     STR_NETWORK_ERROR_CLIENT_CHEATER,
00316     STR_NETWORK_ERROR_CLIENT_SERVER_FULL,
00317     STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS
00318   };
00319 
00320   if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
00321 
00322   return network_error_strings[err];
00323 }
00324 
00330 void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
00331 {
00332   if (!_networking) return;
00333 
00334   switch (changed_mode) {
00335     case PM_PAUSED_NORMAL:
00336     case PM_PAUSED_JOIN:
00337     case PM_PAUSED_ACTIVE_CLIENTS: {
00338       bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
00339       bool paused = (_pause_mode != PM_UNPAUSED);
00340       if (!paused && !changed) return;
00341 
00342       StringID str;
00343       if (!changed) {
00344         int i = -1;
00345         if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED)         SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL);
00346         if ((_pause_mode & PM_PAUSED_JOIN) != PM_UNPAUSED)           SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS);
00347         if ((_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS);
00348         str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
00349       } else {
00350         switch (changed_mode) {
00351           case PM_PAUSED_NORMAL:         SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
00352           case PM_PAUSED_JOIN:           SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
00353           case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
00354           default: NOT_REACHED();
00355         }
00356         str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
00357       }
00358 
00359       char buffer[DRAW_STRING_BUFFER];
00360       GetString(buffer, str, lastof(buffer));
00361       NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, NULL, buffer);
00362       break;
00363     }
00364 
00365     default:
00366       return;
00367   }
00368 }
00369 
00370 
00379 static void CheckPauseHelper(bool pause, PauseMode pm)
00380 {
00381   if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
00382 
00383   DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
00384 }
00385 
00391 static uint NetworkCountActiveClients()
00392 {
00393   const NetworkClientSocket *cs;
00394   uint count = 0;
00395 
00396   FOR_ALL_CLIENT_SOCKETS(cs) {
00397     if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
00398     if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
00399     count++;
00400   }
00401 
00402   return count;
00403 }
00404 
00408 static void CheckMinActiveClients()
00409 {
00410   if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
00411       !_network_dedicated ||
00412       (_settings_client.network.min_active_clients == 0 && (_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) == PM_UNPAUSED)) {
00413     return;
00414   }
00415   CheckPauseHelper(NetworkCountActiveClients() < _settings_client.network.min_active_clients, PM_PAUSED_ACTIVE_CLIENTS);
00416 }
00417 
00422 static bool NetworkHasJoiningClient()
00423 {
00424   const NetworkClientSocket *cs;
00425   FOR_ALL_CLIENT_SOCKETS(cs) {
00426     if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
00427   }
00428 
00429   return false;
00430 }
00431 
00435 static void CheckPauseOnJoin()
00436 {
00437   if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
00438       (!_settings_client.network.pause_on_join && (_pause_mode & PM_PAUSED_JOIN) == PM_UNPAUSED)) {
00439     return;
00440   }
00441   CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
00442 }
00443 
00452 void ParseConnectionString(const char **company, const char **port, char *connection_string)
00453 {
00454   bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
00455   char *p;
00456   for (p = connection_string; *p != '\0'; p++) {
00457     switch (*p) {
00458       case '[':
00459         ipv6 = true;
00460         break;
00461 
00462       case ']':
00463         ipv6 = false;
00464         break;
00465 
00466       case '#':
00467         *company = p + 1;
00468         *p = '\0';
00469         break;
00470 
00471       case ':':
00472         if (ipv6) break;
00473         *port = p + 1;
00474         *p = '\0';
00475         break;
00476     }
00477   }
00478 }
00479 
00480 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
00481 {
00482   /* Register the login */
00483   _network_clients_connected++;
00484 
00485   SetWindowDirty(WC_CLIENT_LIST, 0);
00486   ServerNetworkGameSocketHandler *cs = new ServerNetworkGameSocketHandler(s);
00487   cs->GetInfo()->client_address = address; // Save the IP of the client
00488 }
00489 
00494 static void InitializeNetworkPools(bool close_admins = true)
00495 {
00496   _networkclientsocket_pool.CleanPool();
00497   _networkclientinfo_pool.CleanPool();
00498   if (close_admins) _networkadminsocket_pool.CleanPool();
00499 }
00500 
00505 void NetworkClose(bool close_admins)
00506 {
00507   if (_network_server) {
00508     if (close_admins) {
00509       ServerNetworkAdminSocketHandler *as;
00510       FOR_ALL_ADMIN_SOCKETS(as) {
00511         as->CloseConnection(true);
00512       }
00513     }
00514 
00515     NetworkClientSocket *cs;
00516     FOR_ALL_CLIENT_SOCKETS(cs) {
00517       cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
00518     }
00519     ServerNetworkGameSocketHandler::CloseListeners();
00520     ServerNetworkAdminSocketHandler::CloseListeners();
00521   } else if (MyClient::my_client != NULL) {
00522     MyClient::SendQuit();
00523     MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
00524   }
00525 
00526   TCPConnecter::KillAll();
00527 
00528   _networking = false;
00529   _network_server = false;
00530 
00531   NetworkFreeLocalCommandQueue();
00532 
00533   free(_network_company_states);
00534   _network_company_states = NULL;
00535 
00536   InitializeNetworkPools(close_admins);
00537 }
00538 
00539 /* Inits the network (cleans sockets and stuff) */
00540 static void NetworkInitialize(bool close_admins = true)
00541 {
00542   InitializeNetworkPools(close_admins);
00543   NetworkUDPInitialize();
00544 
00545   _sync_frame = 0;
00546   _network_first_time = true;
00547 
00548   _network_reconnect = 0;
00549 }
00550 
00552 class TCPQueryConnecter : TCPConnecter {
00553 public:
00554   TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00555 
00556   virtual void OnFailure()
00557   {
00558     NetworkDisconnect();
00559   }
00560 
00561   virtual void OnConnect(SOCKET s)
00562   {
00563     _networking = true;
00564     new ClientNetworkGameSocketHandler(s);
00565     MyClient::SendCompanyInformationQuery();
00566   }
00567 };
00568 
00569 /* Query a server to fetch his game-info
00570  *  If game_info is true, only the gameinfo is fetched,
00571  *   else only the client_info is fetched */
00572 void NetworkTCPQueryServer(NetworkAddress address)
00573 {
00574   if (!_network_available) return;
00575 
00576   NetworkDisconnect();
00577   NetworkInitialize();
00578 
00579   new TCPQueryConnecter(address);
00580 }
00581 
00582 /* Validates an address entered as a string and adds the server to
00583  * the list. If you use this function, the games will be marked
00584  * as manually added. */
00585 void NetworkAddServer(const char *b)
00586 {
00587   if (*b != '\0') {
00588     const char *port = NULL;
00589     const char *company = NULL;
00590     char host[NETWORK_HOSTNAME_LENGTH];
00591     uint16 rport;
00592 
00593     strecpy(host, b, lastof(host));
00594 
00595     strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
00596     rport = NETWORK_DEFAULT_PORT;
00597 
00598     ParseConnectionString(&company, &port, host);
00599     if (port != NULL) rport = atoi(port);
00600 
00601     NetworkUDPQueryServer(NetworkAddress(host, rport), true);
00602   }
00603 }
00604 
00610 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
00611 {
00612   for (char **iter = _network_bind_list.Begin(); iter != _network_bind_list.End(); iter++) {
00613     *addresses->Append() = NetworkAddress(*iter, port);
00614   }
00615 
00616   /* No address, so bind to everything. */
00617   if (addresses->Length() == 0) {
00618     *addresses->Append() = NetworkAddress("", port);
00619   }
00620 }
00621 
00622 /* Generates the list of manually added hosts from NetworkGameList and
00623  * dumps them into the array _network_host_list. This array is needed
00624  * by the function that generates the config file. */
00625 void NetworkRebuildHostList()
00626 {
00627   _network_host_list.Clear();
00628 
00629   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00630     if (item->manually) *_network_host_list.Append() = strdup(item->address.GetAddressAsString(false));
00631   }
00632 }
00633 
00635 class TCPClientConnecter : TCPConnecter {
00636 public:
00637   TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00638 
00639   virtual void OnFailure()
00640   {
00641     NetworkError(STR_NETWORK_ERROR_NOCONNECTION);
00642   }
00643 
00644   virtual void OnConnect(SOCKET s)
00645   {
00646     _networking = true;
00647     new ClientNetworkGameSocketHandler(s);
00648     IConsoleCmdExec("exec scripts/on_client.scr 0");
00649     NetworkClient_Connected();
00650   }
00651 };
00652 
00653 
00654 /* Used by clients, to connect to a server */
00655 void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
00656 {
00657   if (!_network_available) return;
00658 
00659   if (address.GetPort() == 0) return;
00660 
00661   strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
00662   _settings_client.network.last_port = address.GetPort();
00663   _network_join_as = join_as;
00664   _network_join_server_password = join_server_password;
00665   _network_join_company_password = join_company_password;
00666 
00667   NetworkDisconnect();
00668   NetworkInitialize();
00669 
00670   _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
00671   ShowJoinStatusWindow();
00672 
00673   new TCPClientConnecter(address);
00674 }
00675 
00676 static void NetworkInitGameInfo()
00677 {
00678   if (StrEmpty(_settings_client.network.server_name)) {
00679     snprintf(_settings_client.network.server_name, sizeof(_settings_client.network.server_name), "Unnamed Server");
00680   }
00681 
00682   /* The server is a client too */
00683   _network_game_info.clients_on = _network_dedicated ? 0 : 1;
00684 
00685   /* There should be always space for the server. */
00686   assert(NetworkClientInfo::CanAllocateItem());
00687   NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
00688   ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : _local_company;
00689   /* Give the server a valid IP; banning it is pointless anyways */
00690   sockaddr_in sock;
00691   memset(&sock, 0, sizeof(sock));
00692   sock.sin_family = AF_INET;
00693   ci->client_address = NetworkAddress((sockaddr*)&sock, sizeof(sock));
00694 
00695   strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
00696 }
00697 
00698 bool NetworkServerStart()
00699 {
00700   if (!_network_available) return false;
00701 
00702   /* Call the pre-scripts */
00703   IConsoleCmdExec("exec scripts/pre_server.scr 0");
00704   if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
00705 
00706   NetworkDisconnect(false, false);
00707   NetworkInitialize(false);
00708   if (!ServerNetworkGameSocketHandler::Listen(_settings_client.network.server_port)) return false;
00709 
00710   /* Only listen for admins when the password isn't empty. */
00711   if (!StrEmpty(_settings_client.network.admin_password) && !ServerNetworkAdminSocketHandler::Listen(_settings_client.network.server_admin_port)) return false;
00712 
00713   /* Try to start UDP-server */
00714   _network_udp_server = _udp_server_socket->Listen();
00715 
00716   _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
00717   _network_server = true;
00718   _networking = true;
00719   _frame_counter = 0;
00720   _frame_counter_server = 0;
00721   _frame_counter_max = 0;
00722   _last_sync_frame = 0;
00723   _network_own_client_id = CLIENT_ID_SERVER;
00724 
00725   _network_clients_connected = 0;
00726   _network_company_passworded = 0;
00727 
00728   NetworkInitGameInfo();
00729 
00730   /* execute server initialization script */
00731   IConsoleCmdExec("exec scripts/on_server.scr 0");
00732   /* if the server is dedicated ... add some other script */
00733   if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
00734 
00735   /* Try to register us to the master server */
00736   _network_last_advertise_frame = 0;
00737   _network_need_advertise = true;
00738   NetworkUDPAdvertise();
00739 
00740   /* welcome possibly still connected admins - this can only happen on a dedicated server. */
00741   if (_network_dedicated) ServerNetworkAdminSocketHandler::WelcomeAll();
00742 
00743   return true;
00744 }
00745 
00746 /* The server is rebooting...
00747  * The only difference with NetworkDisconnect, is the packets that is sent */
00748 void NetworkReboot()
00749 {
00750   if (_network_server) {
00751     NetworkClientSocket *cs;
00752     FOR_ALL_CLIENT_SOCKETS(cs) {
00753       cs->SendNewGame();
00754       cs->SendPackets();
00755     }
00756 
00757     ServerNetworkAdminSocketHandler *as;
00758     FOR_ALL_ADMIN_SOCKETS(as) {
00759       as->SendNewGame();
00760       as->SendPackets();
00761     }
00762   }
00763 
00764   /* For non-dedicated servers we have to kick the admins as we are not
00765    * certain that we will end up in a new network game. */
00766   NetworkClose(!_network_dedicated);
00767 }
00768 
00774 void NetworkDisconnect(bool blocking, bool close_admins)
00775 {
00776   if (_network_server) {
00777     NetworkClientSocket *cs;
00778     FOR_ALL_CLIENT_SOCKETS(cs) {
00779       cs->SendShutdown();
00780       cs->SendPackets();
00781     }
00782 
00783     if (close_admins) {
00784       ServerNetworkAdminSocketHandler *as;
00785       FOR_ALL_ADMIN_SOCKETS(as) {
00786         as->SendShutdown();
00787         as->SendPackets();
00788       }
00789     }
00790   }
00791 
00792   if (_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(blocking);
00793 
00794   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00795 
00796   NetworkClose(close_admins);
00797 
00798   /* Reinitialize the UDP stack, i.e. close all existing connections. */
00799   NetworkUDPInitialize();
00800 }
00801 
00806 static bool NetworkReceive()
00807 {
00808   if (_network_server) {
00809     ServerNetworkAdminSocketHandler::Receive();
00810     return ServerNetworkGameSocketHandler::Receive();
00811   } else {
00812     return ClientNetworkGameSocketHandler::Receive();
00813   }
00814 }
00815 
00816 /* This sends all buffered commands (if possible) */
00817 static void NetworkSend()
00818 {
00819   if (_network_server) {
00820     ServerNetworkAdminSocketHandler::Send();
00821     ServerNetworkGameSocketHandler::Send();
00822   } else {
00823     ClientNetworkGameSocketHandler::Send();
00824   }
00825 }
00826 
00827 /* We have to do some UDP checking */
00828 void NetworkUDPGameLoop()
00829 {
00830   _network_content_client.SendReceive();
00831   TCPConnecter::CheckCallbacks();
00832   NetworkHTTPSocketHandler::HTTPReceive();
00833 
00834   if (_network_udp_server) {
00835     _udp_server_socket->ReceivePackets();
00836     _udp_master_socket->ReceivePackets();
00837   } else {
00838     _udp_client_socket->ReceivePackets();
00839     if (_network_udp_broadcast > 0) _network_udp_broadcast--;
00840     NetworkGameListRequery();
00841   }
00842 }
00843 
00844 /* The main loop called from ttd.c
00845  *  Here we also have to do StateGameLoop if needed! */
00846 void NetworkGameLoop()
00847 {
00848   if (!_networking) return;
00849 
00850   if (!NetworkReceive()) return;
00851 
00852   if (_network_server) {
00853     /* Log the sync state to check for in-syncedness of replays. */
00854     if (_date_fract == 0) {
00855       /* We don't want to log multiple times if paused. */
00856       static Date last_log;
00857       if (last_log != _date) {
00858         DEBUG(desync, 1, "sync: %08x; %02x; %08x; %08x", _date, _date_fract, _random.state[0], _random.state[1]);
00859         last_log = _date;
00860       }
00861     }
00862 
00863 #ifdef DEBUG_DUMP_COMMANDS
00864     /* Loading of the debug commands from -ddesync>=1 */
00865     static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
00866     static Date next_date = 0;
00867     static uint32 next_date_fract;
00868     static CommandPacket *cp = NULL;
00869     static bool check_sync_state = false;
00870     static uint32 sync_state[2];
00871     if (f == NULL && next_date == 0) {
00872       DEBUG(net, 0, "Cannot open commands.log");
00873       next_date = 1;
00874     }
00875 
00876     while (f != NULL && !feof(f)) {
00877       if (_date == next_date && _date_fract == next_date_fract) {
00878         if (cp != NULL) {
00879           NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, NULL, cp->text, cp->company);
00880           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));
00881           free(cp);
00882           cp = NULL;
00883         }
00884         if (check_sync_state) {
00885           if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
00886             DEBUG(net, 0, "sync check: %08x; %02x; match", _date, _date_fract);
00887           } else {
00888             DEBUG(net, 0, "sync check: %08x; %02x; mismatch expected {%08x, %08x}, got {%08x, %08x}",
00889                   _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
00890             NOT_REACHED();
00891           }
00892           check_sync_state = false;
00893         }
00894       }
00895 
00896       if (cp != NULL || check_sync_state) break;
00897 
00898       char buff[4096];
00899       if (fgets(buff, lengthof(buff), f) == NULL) break;
00900 
00901       char *p = buff;
00902       /* Ignore the "[date time] " part of the message */
00903       if (*p == '[') {
00904         p = strchr(p, ']');
00905         if (p == NULL) break;
00906         p += 2;
00907       }
00908 
00909       if (strncmp(p, "cmd: ", 5) == 0) {
00910         cp = CallocT<CommandPacket>(1);
00911         int company;
00912         int ret = sscanf(p + 5, "%x; %x; %x; %x; %x; %x; %x; \"%[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text);
00913         /* There are 8 pieces of data to read, however the last is a
00914          * string that might or might not exist. Ignore it if that
00915          * string misses because in 99% of the time it's not used. */
00916         assert(ret == 8 || ret == 7);
00917         cp->company = (CompanyID)company;
00918       } else if (strncmp(p, "join: ", 6) == 0) {
00919         /* Manually insert a pause when joining; this way the client can join at the exact right time. */
00920         int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
00921         assert(ret == 2);
00922         DEBUG(net, 0, "injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
00923         cp = CallocT<CommandPacket>(1);
00924         cp->company = COMPANY_SPECTATOR;
00925         cp->cmd = CMD_PAUSE;
00926         cp->p1 = PM_PAUSED_NORMAL;
00927         cp->p2 = 1;
00928         _ddc_fastforward = false;
00929       } else if (strncmp(p, "sync: ", 6) == 0) {
00930         int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
00931         assert(ret == 4);
00932         check_sync_state = true;
00933       } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
00934             strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
00935         /* A message that is not very important to the log playback, but part of the log. */
00936       } else {
00937         /* Can't parse a line; what's wrong here? */
00938         DEBUG(net, 0, "trying to parse: %s", p);
00939         NOT_REACHED();
00940       }
00941     }
00942     if (f != NULL && feof(f)) {
00943       DEBUG(net, 0, "End of commands.log");
00944       fclose(f);
00945       f = NULL;
00946     }
00947 #endif /* DEBUG_DUMP_COMMANDS */
00948     if (_frame_counter >= _frame_counter_max) {
00949       /* Only check for active clients just before we're going to send out
00950        * the commands so we don't send multiple pause/unpause commands when
00951        * the frame_freq is more than 1 tick. Same with distributing commands. */
00952       CheckPauseOnJoin();
00953       CheckMinActiveClients();
00954       NetworkDistributeCommands();
00955     }
00956 
00957     bool send_frame = false;
00958 
00959     /* We first increase the _frame_counter */
00960     _frame_counter++;
00961     /* Update max-frame-counter */
00962     if (_frame_counter > _frame_counter_max) {
00963       _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
00964       send_frame = true;
00965     }
00966 
00967     NetworkExecuteLocalCommandQueue();
00968 
00969     /* Then we make the frame */
00970     StateGameLoop();
00971 
00972     _sync_seed_1 = _random.state[0];
00973 #ifdef NETWORK_SEND_DOUBLE_SEED
00974     _sync_seed_2 = _random.state[1];
00975 #endif
00976 
00977     NetworkServer_Tick(send_frame);
00978   } else {
00979     /* Client */
00980 
00981     /* Make sure we are at the frame were the server is (quick-frames) */
00982     if (_frame_counter_server > _frame_counter) {
00983       /* Run a number of frames; when things go bad, get out. */
00984       while (_frame_counter_server > _frame_counter) {
00985         if (!ClientNetworkGameSocketHandler::GameLoop()) return;
00986       }
00987     } else {
00988       /* Else, keep on going till _frame_counter_max */
00989       if (_frame_counter_max > _frame_counter) {
00990         /* Run one frame; if things went bad, get out. */
00991         if (!ClientNetworkGameSocketHandler::GameLoop()) return;
00992       }
00993     }
00994   }
00995 
00996   NetworkSend();
00997 }
00998 
00999 static void NetworkGenerateServerId()
01000 {
01001   Md5 checksum;
01002   uint8 digest[16];
01003   char hex_output[16 * 2 + 1];
01004   char coding_string[NETWORK_NAME_LENGTH];
01005   int di;
01006 
01007   snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
01008 
01009   /* Generate the MD5 hash */
01010   checksum.Append((const uint8*)coding_string, strlen(coding_string));
01011   checksum.Finish(digest);
01012 
01013   for (di = 0; di < 16; ++di) {
01014     sprintf(hex_output + di * 2, "%02x", digest[di]);
01015   }
01016 
01017   /* _settings_client.network.network_id is our id */
01018   snprintf(_settings_client.network.network_id, sizeof(_settings_client.network.network_id), "%s", hex_output);
01019 }
01020 
01021 void NetworkStartDebugLog(NetworkAddress address)
01022 {
01023   extern SOCKET _debug_socket;  // Comes from debug.c
01024 
01025   DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
01026 
01027   SOCKET s = address.Connect();
01028   if (s == INVALID_SOCKET) {
01029     DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
01030     return;
01031   }
01032 
01033   _debug_socket = s;
01034 
01035   DEBUG(net, 0, "DEBUG() is now redirected");
01036 }
01037 
01039 void NetworkStartUp()
01040 {
01041   DEBUG(net, 3, "[core] starting network...");
01042 
01043   /* Network is available */
01044   _network_available = NetworkCoreInitialize();
01045   _network_dedicated = false;
01046   _network_last_advertise_frame = 0;
01047   _network_need_advertise = true;
01048   _network_advertise_retries = 0;
01049 
01050   /* Generate an server id when there is none yet */
01051   if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
01052 
01053   memset(&_network_game_info, 0, sizeof(_network_game_info));
01054 
01055   NetworkInitialize();
01056   DEBUG(net, 3, "[core] network online, multiplayer available");
01057   NetworkFindBroadcastIPs(&_broadcast_list);
01058 }
01059 
01061 void NetworkShutDown()
01062 {
01063   NetworkDisconnect(true);
01064   NetworkUDPClose();
01065 
01066   DEBUG(net, 3, "[core] shutting down network");
01067 
01068   _network_available = false;
01069 
01070   NetworkCoreShutdown();
01071 }
01072 
01077 bool IsNetworkCompatibleVersion(const char *other)
01078 {
01079   return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
01080 }
01081 
01082 #endif /* ENABLE_NETWORK */

Generated on Fri Mar 18 23:17:37 2011 for OpenTTD by  doxygen 1.6.1