network.cpp

Go to the documentation of this file.
00001 /* $Id: network.cpp 21513 2010-12-14 15:00:43Z 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 
00158 bool NetworkCompanyIsPassworded(CompanyID company_id)
00159 {
00160   return HasBit(_network_company_passworded, company_id);
00161 }
00162 
00163 /* This puts a text-message to the console, or in the future, the chat-box,
00164  *  (to keep it all a bit more general)
00165  * If 'self_send' is true, this is the client who is sending the message */
00166 void NetworkTextMessage(NetworkAction action, ConsoleColour colour, bool self_send, const char *name, const char *str, int64 data)
00167 {
00168   StringID strid;
00169   switch (action) {
00170     case NETWORK_ACTION_SERVER_MESSAGE:
00171       /* Ignore invalid messages */
00172       strid = STR_NETWORK_SERVER_MESSAGE;
00173       colour = CC_DEFAULT;
00174       break;
00175     case NETWORK_ACTION_COMPANY_SPECTATOR:
00176       colour = CC_DEFAULT;
00177       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE;
00178       break;
00179     case NETWORK_ACTION_COMPANY_JOIN:
00180       colour = CC_DEFAULT;
00181       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN;
00182       break;
00183     case NETWORK_ACTION_COMPANY_NEW:
00184       colour = CC_DEFAULT;
00185       strid = STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW;
00186       break;
00187     case NETWORK_ACTION_JOIN:
00188       /* Show the Client ID for the server but not for the client. */
00189       strid = _network_server ? STR_NETWORK_MESSAGE_CLIENT_JOINED_ID :  STR_NETWORK_MESSAGE_CLIENT_JOINED;
00190       break;
00191     case NETWORK_ACTION_LEAVE:          strid = STR_NETWORK_MESSAGE_CLIENT_LEFT; break;
00192     case NETWORK_ACTION_NAME_CHANGE:    strid = STR_NETWORK_MESSAGE_NAME_CHANGE; break;
00193     case NETWORK_ACTION_GIVE_MONEY:     strid = self_send ? STR_NETWORK_MESSAGE_GAVE_MONEY_AWAY : STR_NETWORK_MESSAGE_GIVE_MONEY;   break;
00194     case NETWORK_ACTION_CHAT_COMPANY:   strid = self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY; break;
00195     case NETWORK_ACTION_CHAT_CLIENT:    strid = self_send ? STR_NETWORK_CHAT_TO_CLIENT  : STR_NETWORK_CHAT_CLIENT;  break;
00196     default:                            strid = STR_NETWORK_CHAT_ALL; break;
00197   }
00198 
00199   char message[1024];
00200   SetDParamStr(0, name);
00201   SetDParamStr(1, str);
00202   SetDParam(2, data);
00203 
00204   /* All of these strings start with "***". These characters are interpreted as both left-to-right and
00205    * right-to-left characters depending on the context. As the next text might be an user's name, the
00206    * user name's characters will influence the direction of the "***" instead of the language setting
00207    * of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
00208   char *msg_ptr = message + Utf8Encode(message, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
00209   GetString(msg_ptr, strid, lastof(message));
00210 
00211   DEBUG(desync, 1, "msg: %08x; %02x; %s", _date, _date_fract, message);
00212   IConsolePrintF(colour, "%s", message);
00213   NetworkAddChatMessage((TextColour)colour, _settings_client.gui.network_chat_timeout, "%s", message);
00214 }
00215 
00216 /* Calculate the frame-lag of a client */
00217 uint NetworkCalculateLag(const NetworkClientSocket *cs)
00218 {
00219   int lag = cs->last_frame_server - cs->last_frame;
00220   /* This client has missed his ACK packet after 1 DAY_TICKS..
00221    *  so we increase his lag for every frame that passes!
00222    * The packet can be out by a max of _net_frame_freq */
00223   if (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq < _frame_counter) {
00224     lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _settings_client.network.frame_freq);
00225   }
00226   return lag;
00227 }
00228 
00229 
00230 /* There was a non-recoverable error, drop back to the main menu with a nice
00231  *  error */
00232 void NetworkError(StringID error_string)
00233 {
00234   _switch_mode = SM_MENU;
00235   extern StringID _switch_mode_errorstr;
00236   _switch_mode_errorstr = error_string;
00237 }
00238 
00244 StringID GetNetworkErrorMsg(NetworkErrorCode err)
00245 {
00246   /* List of possible network errors, used by
00247    * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
00248   static const StringID network_error_strings[] = {
00249     STR_NETWORK_ERROR_CLIENT_GENERAL,
00250     STR_NETWORK_ERROR_CLIENT_DESYNC,
00251     STR_NETWORK_ERROR_CLIENT_SAVEGAME,
00252     STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST,
00253     STR_NETWORK_ERROR_CLIENT_PROTOCOL_ERROR,
00254     STR_NETWORK_ERROR_CLIENT_NEWGRF_MISMATCH,
00255     STR_NETWORK_ERROR_CLIENT_NOT_AUTHORIZED,
00256     STR_NETWORK_ERROR_CLIENT_NOT_EXPECTED,
00257     STR_NETWORK_ERROR_CLIENT_WRONG_REVISION,
00258     STR_NETWORK_ERROR_CLIENT_NAME_IN_USE,
00259     STR_NETWORK_ERROR_CLIENT_WRONG_PASSWORD,
00260     STR_NETWORK_ERROR_CLIENT_COMPANY_MISMATCH,
00261     STR_NETWORK_ERROR_CLIENT_KICKED,
00262     STR_NETWORK_ERROR_CLIENT_CHEATER,
00263     STR_NETWORK_ERROR_CLIENT_SERVER_FULL,
00264     STR_NETWORK_ERROR_CLIENT_TOO_MANY_COMMANDS
00265   };
00266 
00267   if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
00268 
00269   return network_error_strings[err];
00270 }
00271 
00277 void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
00278 {
00279   if (!_networking) return;
00280 
00281   switch (changed_mode) {
00282     case PM_PAUSED_NORMAL:
00283     case PM_PAUSED_JOIN:
00284     case PM_PAUSED_ACTIVE_CLIENTS: {
00285       bool changed = ((_pause_mode == PM_UNPAUSED) != (prev_mode == PM_UNPAUSED));
00286       bool paused = (_pause_mode != PM_UNPAUSED);
00287       if (!paused && !changed) return;
00288 
00289       StringID str;
00290       if (!changed) {
00291         int i = -1;
00292         if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED)         SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL);
00293         if ((_pause_mode & PM_PAUSED_JOIN) != PM_UNPAUSED)           SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS);
00294         if ((_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) != PM_UNPAUSED) SetDParam(++i, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS);
00295         str = STR_NETWORK_SERVER_MESSAGE_GAME_STILL_PAUSED_1 + i;
00296       } else {
00297         switch (changed_mode) {
00298           case PM_PAUSED_NORMAL:         SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
00299           case PM_PAUSED_JOIN:           SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
00300           case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
00301           default: NOT_REACHED();
00302         }
00303         str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
00304       }
00305 
00306       char buffer[DRAW_STRING_BUFFER];
00307       GetString(buffer, str, lastof(buffer));
00308       NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, NULL, buffer);
00309       break;
00310     }
00311 
00312     default:
00313       return;
00314   }
00315 }
00316 
00317 
00326 static void CheckPauseHelper(bool pause, PauseMode pm)
00327 {
00328   if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
00329 
00330   DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
00331 }
00332 
00338 static uint NetworkCountActiveClients()
00339 {
00340   const NetworkClientSocket *cs;
00341   uint count = 0;
00342 
00343   FOR_ALL_CLIENT_SOCKETS(cs) {
00344     if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue;
00345     if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue;
00346     count++;
00347   }
00348 
00349   return count;
00350 }
00351 
00355 static void CheckMinActiveClients()
00356 {
00357   if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
00358       !_network_dedicated ||
00359       (_settings_client.network.min_active_clients == 0 && (_pause_mode & PM_PAUSED_ACTIVE_CLIENTS) == PM_UNPAUSED)) {
00360     return;
00361   }
00362   CheckPauseHelper(NetworkCountActiveClients() < _settings_client.network.min_active_clients, PM_PAUSED_ACTIVE_CLIENTS);
00363 }
00364 
00369 static bool NetworkHasJoiningClient()
00370 {
00371   const NetworkClientSocket *cs;
00372   FOR_ALL_CLIENT_SOCKETS(cs) {
00373     if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true;
00374   }
00375 
00376   return false;
00377 }
00378 
00382 static void CheckPauseOnJoin()
00383 {
00384   if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED ||
00385       (!_settings_client.network.pause_on_join && (_pause_mode & PM_PAUSED_JOIN) == PM_UNPAUSED)) {
00386     return;
00387   }
00388   CheckPauseHelper(NetworkHasJoiningClient(), PM_PAUSED_JOIN);
00389 }
00390 
00399 void ParseConnectionString(const char **company, const char **port, char *connection_string)
00400 {
00401   bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
00402   char *p;
00403   for (p = connection_string; *p != '\0'; p++) {
00404     switch (*p) {
00405       case '[':
00406         ipv6 = true;
00407         break;
00408 
00409       case ']':
00410         ipv6 = false;
00411         break;
00412 
00413       case '#':
00414         *company = p + 1;
00415         *p = '\0';
00416         break;
00417 
00418       case ':':
00419         if (ipv6) break;
00420         *port = p + 1;
00421         *p = '\0';
00422         break;
00423     }
00424   }
00425 }
00426 
00427 /* static */ void ServerNetworkGameSocketHandler::AcceptConnection(SOCKET s, const NetworkAddress &address)
00428 {
00429   /* Register the login */
00430   _network_clients_connected++;
00431 
00432   SetWindowDirty(WC_CLIENT_LIST, 0);
00433   ServerNetworkGameSocketHandler *cs = new ServerNetworkGameSocketHandler(s);
00434   cs->GetInfo()->client_address = address; // Save the IP of the client
00435 }
00436 
00441 static void InitializeNetworkPools(bool close_admins = true)
00442 {
00443   _networkclientsocket_pool.CleanPool();
00444   _networkclientinfo_pool.CleanPool();
00445   if (close_admins) _networkadminsocket_pool.CleanPool();
00446 }
00447 
00452 void NetworkClose(bool close_admins)
00453 {
00454   if (_network_server) {
00455     if (close_admins) {
00456       ServerNetworkAdminSocketHandler *as;
00457       FOR_ALL_ADMIN_SOCKETS(as) {
00458         as->CloseConnection(true);
00459       }
00460     }
00461 
00462     NetworkClientSocket *cs;
00463     FOR_ALL_CLIENT_SOCKETS(cs) {
00464       cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
00465     }
00466     ServerNetworkGameSocketHandler::CloseListeners();
00467     ServerNetworkAdminSocketHandler::CloseListeners();
00468   } else if (MyClient::my_client != NULL) {
00469     MyClient::SendQuit();
00470     MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
00471   }
00472 
00473   TCPConnecter::KillAll();
00474 
00475   _networking = false;
00476   _network_server = false;
00477 
00478   NetworkFreeLocalCommandQueue();
00479 
00480   free(_network_company_states);
00481   _network_company_states = NULL;
00482 
00483   InitializeNetworkPools(close_admins);
00484 }
00485 
00486 /* Inits the network (cleans sockets and stuff) */
00487 static void NetworkInitialize(bool close_admins = true)
00488 {
00489   InitializeNetworkPools(close_admins);
00490   NetworkUDPInitialize();
00491 
00492   _sync_frame = 0;
00493   _network_first_time = true;
00494 
00495   _network_reconnect = 0;
00496 }
00497 
00499 class TCPQueryConnecter : TCPConnecter {
00500 public:
00501   TCPQueryConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00502 
00503   virtual void OnFailure()
00504   {
00505     NetworkDisconnect();
00506   }
00507 
00508   virtual void OnConnect(SOCKET s)
00509   {
00510     _networking = true;
00511     new ClientNetworkGameSocketHandler(s);
00512     MyClient::SendCompanyInformationQuery();
00513   }
00514 };
00515 
00516 /* Query a server to fetch his game-info
00517  *  If game_info is true, only the gameinfo is fetched,
00518  *   else only the client_info is fetched */
00519 void NetworkTCPQueryServer(NetworkAddress address)
00520 {
00521   if (!_network_available) return;
00522 
00523   NetworkDisconnect();
00524   NetworkInitialize();
00525 
00526   new TCPQueryConnecter(address);
00527 }
00528 
00529 /* Validates an address entered as a string and adds the server to
00530  * the list. If you use this function, the games will be marked
00531  * as manually added. */
00532 void NetworkAddServer(const char *b)
00533 {
00534   if (*b != '\0') {
00535     const char *port = NULL;
00536     const char *company = NULL;
00537     char host[NETWORK_HOSTNAME_LENGTH];
00538     uint16 rport;
00539 
00540     strecpy(host, b, lastof(host));
00541 
00542     strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
00543     rport = NETWORK_DEFAULT_PORT;
00544 
00545     ParseConnectionString(&company, &port, host);
00546     if (port != NULL) rport = atoi(port);
00547 
00548     NetworkUDPQueryServer(NetworkAddress(host, rport), true);
00549   }
00550 }
00551 
00557 void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
00558 {
00559   for (char **iter = _network_bind_list.Begin(); iter != _network_bind_list.End(); iter++) {
00560     *addresses->Append() = NetworkAddress(*iter, port);
00561   }
00562 
00563   /* No address, so bind to everything. */
00564   if (addresses->Length() == 0) {
00565     *addresses->Append() = NetworkAddress("", port);
00566   }
00567 }
00568 
00569 /* Generates the list of manually added hosts from NetworkGameList and
00570  * dumps them into the array _network_host_list. This array is needed
00571  * by the function that generates the config file. */
00572 void NetworkRebuildHostList()
00573 {
00574   _network_host_list.Clear();
00575 
00576   for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
00577     if (item->manually) *_network_host_list.Append() = strdup(item->address.GetAddressAsString(false));
00578   }
00579 }
00580 
00582 class TCPClientConnecter : TCPConnecter {
00583 public:
00584   TCPClientConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
00585 
00586   virtual void OnFailure()
00587   {
00588     NetworkError(STR_NETWORK_ERROR_NOCONNECTION);
00589   }
00590 
00591   virtual void OnConnect(SOCKET s)
00592   {
00593     _networking = true;
00594     new ClientNetworkGameSocketHandler(s);
00595     IConsoleCmdExec("exec scripts/on_client.scr 0");
00596     NetworkClient_Connected();
00597   }
00598 };
00599 
00600 
00601 /* Used by clients, to connect to a server */
00602 void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
00603 {
00604   if (!_network_available) return;
00605 
00606   if (address.GetPort() == 0) return;
00607 
00608   strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
00609   _settings_client.network.last_port = address.GetPort();
00610   _network_join_as = join_as;
00611   _network_join_server_password = join_server_password;
00612   _network_join_company_password = join_company_password;
00613 
00614   NetworkDisconnect();
00615   NetworkInitialize();
00616 
00617   _network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
00618   ShowJoinStatusWindow();
00619 
00620   new TCPClientConnecter(address);
00621 }
00622 
00623 static void NetworkInitGameInfo()
00624 {
00625   if (StrEmpty(_settings_client.network.server_name)) {
00626     snprintf(_settings_client.network.server_name, sizeof(_settings_client.network.server_name), "Unnamed Server");
00627   }
00628 
00629   /* The server is a client too */
00630   _network_game_info.clients_on = _network_dedicated ? 0 : 1;
00631 
00632   NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
00633   ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : _local_company;
00634   /* Give the server a valid IP; banning it is pointless anyways */
00635   sockaddr_in sock;
00636   memset(&sock, 0, sizeof(sock));
00637   sock.sin_family = AF_INET;
00638   ci->client_address = NetworkAddress((sockaddr*)&sock, sizeof(sock));
00639 
00640   strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
00641 }
00642 
00643 bool NetworkServerStart()
00644 {
00645   if (!_network_available) return false;
00646 
00647   /* Call the pre-scripts */
00648   IConsoleCmdExec("exec scripts/pre_server.scr 0");
00649   if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
00650 
00651   NetworkDisconnect(false, false);
00652   NetworkInitialize(false);
00653   if (!ServerNetworkGameSocketHandler::Listen(_settings_client.network.server_port)) return false;
00654 
00655   /* Only listen for admins when the password isn't empty. */
00656   if (!StrEmpty(_settings_client.network.admin_password) && !ServerNetworkAdminSocketHandler::Listen(_settings_client.network.server_admin_port)) return false;
00657 
00658   /* Try to start UDP-server */
00659   _network_udp_server = _udp_server_socket->Listen();
00660 
00661   _network_company_states = CallocT<NetworkCompanyState>(MAX_COMPANIES);
00662   _network_server = true;
00663   _networking = true;
00664   _frame_counter = 0;
00665   _frame_counter_server = 0;
00666   _frame_counter_max = 0;
00667   _last_sync_frame = 0;
00668   _network_own_client_id = CLIENT_ID_SERVER;
00669 
00670   _network_clients_connected = 0;
00671   _network_company_passworded = 0;
00672 
00673   NetworkInitGameInfo();
00674 
00675   /* execute server initialization script */
00676   IConsoleCmdExec("exec scripts/on_server.scr 0");
00677   /* if the server is dedicated ... add some other script */
00678   if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
00679 
00680   /* Try to register us to the master server */
00681   _network_last_advertise_frame = 0;
00682   _network_need_advertise = true;
00683   NetworkUDPAdvertise();
00684 
00685   /* welcome possibly still connected admins - this can only happen on a dedicated server. */
00686   if (_network_dedicated) ServerNetworkAdminSocketHandler::WelcomeAll();
00687 
00688   return true;
00689 }
00690 
00691 /* The server is rebooting...
00692  * The only difference with NetworkDisconnect, is the packets that is sent */
00693 void NetworkReboot()
00694 {
00695   if (_network_server) {
00696     NetworkClientSocket *cs;
00697     FOR_ALL_CLIENT_SOCKETS(cs) {
00698       cs->SendNewGame();
00699       cs->SendPackets();
00700     }
00701 
00702     ServerNetworkAdminSocketHandler *as;
00703     FOR_ALL_ADMIN_SOCKETS(as) {
00704       as->SendNewGame();
00705       as->SendPackets();
00706     }
00707   }
00708 
00709   /* For non-dedicated servers we have to kick the admins as we are not
00710    * certain that we will end up in a new network game. */
00711   NetworkClose(!_network_dedicated);
00712 }
00713 
00719 void NetworkDisconnect(bool blocking, bool close_admins)
00720 {
00721   if (_network_server) {
00722     NetworkClientSocket *cs;
00723     FOR_ALL_CLIENT_SOCKETS(cs) {
00724       cs->SendShutdown();
00725       cs->SendPackets();
00726     }
00727 
00728     if (close_admins) {
00729       ServerNetworkAdminSocketHandler *as;
00730       FOR_ALL_ADMIN_SOCKETS(as) {
00731         as->SendShutdown();
00732         as->SendPackets();
00733       }
00734     }
00735   }
00736 
00737   if (_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(blocking);
00738 
00739   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00740 
00741   NetworkClose(close_admins);
00742 
00743   /* Reinitialize the UDP stack, i.e. close all existing connections. */
00744   NetworkUDPInitialize();
00745 }
00746 
00751 static bool NetworkReceive()
00752 {
00753   if (_network_server) {
00754     ServerNetworkAdminSocketHandler::Receive();
00755     return ServerNetworkGameSocketHandler::Receive();
00756   } else {
00757     return ClientNetworkGameSocketHandler::Receive();
00758   }
00759 }
00760 
00761 /* This sends all buffered commands (if possible) */
00762 static void NetworkSend()
00763 {
00764   if (_network_server) {
00765     ServerNetworkAdminSocketHandler::Send();
00766     ServerNetworkGameSocketHandler::Send();
00767   } else {
00768     ClientNetworkGameSocketHandler::Send();
00769   }
00770 }
00771 
00772 /* We have to do some UDP checking */
00773 void NetworkUDPGameLoop()
00774 {
00775   _network_content_client.SendReceive();
00776   TCPConnecter::CheckCallbacks();
00777   NetworkHTTPSocketHandler::HTTPReceive();
00778 
00779   if (_network_udp_server) {
00780     _udp_server_socket->ReceivePackets();
00781     _udp_master_socket->ReceivePackets();
00782   } else {
00783     _udp_client_socket->ReceivePackets();
00784     if (_network_udp_broadcast > 0) _network_udp_broadcast--;
00785     NetworkGameListRequery();
00786   }
00787 }
00788 
00789 /* The main loop called from ttd.c
00790  *  Here we also have to do StateGameLoop if needed! */
00791 void NetworkGameLoop()
00792 {
00793   if (!_networking) return;
00794 
00795   if (!NetworkReceive()) return;
00796 
00797   if (_network_server) {
00798     /* Log the sync state to check for in-syncedness of replays. */
00799     if (_date_fract == 0) {
00800       /* We don't want to log multiple times if paused. */
00801       static Date last_log;
00802       if (last_log != _date) {
00803         DEBUG(desync, 1, "sync: %08x; %02x; %08x; %08x", _date, _date_fract, _random.state[0], _random.state[1]);
00804         last_log = _date;
00805       }
00806     }
00807 
00808 #ifdef DEBUG_DUMP_COMMANDS
00809     /* Loading of the debug commands from -ddesync>=1 */
00810     static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
00811     static Date next_date = 0;
00812     static uint32 next_date_fract;
00813     static CommandPacket *cp = NULL;
00814     static bool check_sync_state = false;
00815     static uint32 sync_state[2];
00816     if (f == NULL && next_date == 0) {
00817       DEBUG(net, 0, "Cannot open commands.log");
00818       next_date = 1;
00819     }
00820 
00821     while (f != NULL && !feof(f)) {
00822       if (_date == next_date && _date_fract == next_date_fract) {
00823         if (cp != NULL) {
00824           NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, NULL, cp->text, cp->company);
00825           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));
00826           free(cp);
00827           cp = NULL;
00828         }
00829         if (check_sync_state) {
00830           if (sync_state[0] == _random.state[0] && sync_state[1] == _random.state[1]) {
00831             DEBUG(net, 0, "sync check: %08x; %02x; match", _date, _date_fract);
00832           } else {
00833             DEBUG(net, 0, "sync check: %08x; %02x; mismatch expected {%08x, %08x}, got {%08x, %08x}",
00834                   _date, _date_fract, sync_state[0], sync_state[1], _random.state[0], _random.state[1]);
00835             NOT_REACHED();
00836           }
00837           check_sync_state = false;
00838         }
00839       }
00840 
00841       if (cp != NULL || check_sync_state) break;
00842 
00843       char buff[4096];
00844       if (fgets(buff, lengthof(buff), f) == NULL) break;
00845 
00846       char *p = buff;
00847       /* Ignore the "[date time] " part of the message */
00848       if (*p == '[') {
00849         p = strchr(p, ']');
00850         if (p == NULL) break;
00851         p += 2;
00852       }
00853 
00854       if (strncmp(p, "cmd: ", 5) == 0) {
00855         cp = CallocT<CommandPacket>(1);
00856         int company;
00857         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);
00858         /* There are 8 pieces of data to read, however the last is a
00859          * string that might or might not exist. Ignore it if that
00860          * string misses because in 99% of the time it's not used. */
00861         assert(ret == 8 || ret == 7);
00862         cp->company = (CompanyID)company;
00863       } else if (strncmp(p, "join: ", 6) == 0) {
00864         /* Manually insert a pause when joining; this way the client can join at the exact right time. */
00865         int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
00866         assert(ret == 2);
00867         DEBUG(net, 0, "injecting pause for join at %08x:%02x; please join when paused", next_date, next_date_fract);
00868         cp = CallocT<CommandPacket>(1);
00869         cp->company = COMPANY_SPECTATOR;
00870         cp->cmd = CMD_PAUSE;
00871         cp->p1 = PM_PAUSED_NORMAL;
00872         cp->p2 = 1;
00873         _ddc_fastforward = false;
00874       } else if (strncmp(p, "sync: ", 6) == 0) {
00875         int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
00876         assert(ret == 4);
00877         check_sync_state = true;
00878       } else if (strncmp(p, "msg: ", 5) == 0 || strncmp(p, "client: ", 8) == 0 ||
00879             strncmp(p, "load: ", 6) == 0 || strncmp(p, "save: ", 6) == 0) {
00880         /* A message that is not very important to the log playback, but part of the log. */
00881       } else {
00882         /* Can't parse a line; what's wrong here? */
00883         DEBUG(net, 0, "trying to parse: %s", p);
00884         NOT_REACHED();
00885       }
00886     }
00887     if (f != NULL && feof(f)) {
00888       DEBUG(net, 0, "End of commands.log");
00889       fclose(f);
00890       f = NULL;
00891     }
00892 #endif /* DEBUG_DUMP_COMMANDS */
00893     if (_frame_counter >= _frame_counter_max) {
00894       /* Only check for active clients just before we're going to send out
00895        * the commands so we don't send multiple pause/unpause commands when
00896        * the frame_freq is more than 1 tick. Same with distributing commands. */
00897       CheckPauseOnJoin();
00898       CheckMinActiveClients();
00899       NetworkDistributeCommands();
00900     }
00901 
00902     bool send_frame = false;
00903 
00904     /* We first increase the _frame_counter */
00905     _frame_counter++;
00906     /* Update max-frame-counter */
00907     if (_frame_counter > _frame_counter_max) {
00908       _frame_counter_max = _frame_counter + _settings_client.network.frame_freq;
00909       send_frame = true;
00910     }
00911 
00912     NetworkExecuteLocalCommandQueue();
00913 
00914     /* Then we make the frame */
00915     StateGameLoop();
00916 
00917     _sync_seed_1 = _random.state[0];
00918 #ifdef NETWORK_SEND_DOUBLE_SEED
00919     _sync_seed_2 = _random.state[1];
00920 #endif
00921 
00922     NetworkServer_Tick(send_frame);
00923   } else {
00924     /* Client */
00925 
00926     /* Make sure we are at the frame were the server is (quick-frames) */
00927     if (_frame_counter_server > _frame_counter) {
00928       /* Run a number of frames; when things go bad, get out. */
00929       while (_frame_counter_server > _frame_counter) {
00930         if (!ClientNetworkGameSocketHandler::GameLoop()) return;
00931       }
00932     } else {
00933       /* Else, keep on going till _frame_counter_max */
00934       if (_frame_counter_max > _frame_counter) {
00935         /* Run one frame; if things went bad, get out. */
00936         if (!ClientNetworkGameSocketHandler::GameLoop()) return;
00937       }
00938     }
00939   }
00940 
00941   NetworkSend();
00942 }
00943 
00944 static void NetworkGenerateServerId()
00945 {
00946   Md5 checksum;
00947   uint8 digest[16];
00948   char hex_output[16 * 2 + 1];
00949   char coding_string[NETWORK_NAME_LENGTH];
00950   int di;
00951 
00952   snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Server ID");
00953 
00954   /* Generate the MD5 hash */
00955   checksum.Append((const uint8*)coding_string, strlen(coding_string));
00956   checksum.Finish(digest);
00957 
00958   for (di = 0; di < 16; ++di) {
00959     sprintf(hex_output + di * 2, "%02x", digest[di]);
00960   }
00961 
00962   /* _settings_client.network.network_id is our id */
00963   snprintf(_settings_client.network.network_id, sizeof(_settings_client.network.network_id), "%s", hex_output);
00964 }
00965 
00966 void NetworkStartDebugLog(NetworkAddress address)
00967 {
00968   extern SOCKET _debug_socket;  // Comes from debug.c
00969 
00970   DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", address.GetHostname(), address.GetPort());
00971 
00972   SOCKET s = address.Connect();
00973   if (s == INVALID_SOCKET) {
00974     DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
00975     return;
00976   }
00977 
00978   _debug_socket = s;
00979 
00980   DEBUG(net, 0, "DEBUG() is now redirected");
00981 }
00982 
00984 void NetworkStartUp()
00985 {
00986   DEBUG(net, 3, "[core] starting network...");
00987 
00988   /* Network is available */
00989   _network_available = NetworkCoreInitialize();
00990   _network_dedicated = false;
00991   _network_last_advertise_frame = 0;
00992   _network_need_advertise = true;
00993   _network_advertise_retries = 0;
00994 
00995   /* Generate an server id when there is none yet */
00996   if (StrEmpty(_settings_client.network.network_id)) NetworkGenerateServerId();
00997 
00998   memset(&_network_game_info, 0, sizeof(_network_game_info));
00999 
01000   NetworkInitialize();
01001   DEBUG(net, 3, "[core] network online, multiplayer available");
01002   NetworkFindBroadcastIPs(&_broadcast_list);
01003 }
01004 
01006 void NetworkShutDown()
01007 {
01008   NetworkDisconnect(true);
01009   NetworkUDPClose();
01010 
01011   DEBUG(net, 3, "[core] shutting down network");
01012 
01013   _network_available = false;
01014 
01015   NetworkCoreShutdown();
01016 }
01017 
01022 bool IsNetworkCompatibleVersion(const char *other)
01023 {
01024   return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
01025 }
01026 
01027 #endif /* ENABLE_NETWORK */

Generated on Fri Dec 31 17:15:33 2010 for OpenTTD by  doxygen 1.6.1