# network_client.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 #ifdef ENABLE_NETWORK
    00013
    00014 #include "../stdafx.h"
    00015 #include "../debug.h"
    00016 #include "network_internal.h"
    00017 #include "network_gui.h"
    00018 #include "../saveload/saveload.h"
    00019 #include "../command_func.h"
    00020 #include "../console_func.h"
    00021 #include "../fileio_func.h"
    00022 #include "../3rdparty/md5/md5.h"
    00023 #include "../strings_func.h"
    00024 #include "../window_func.h"
    00025 #include "../company_func.h"
    00026 #include "../company_base.h"
    00027 #include "../company_gui.h"
    00028 #include "../rev.h"
    00029
    00030 #include "table/strings.h"
    00031
    00032 /* This file handles all the client-commands */
    00033
    00034
    00035 /* So we don't make too much typos ;) */
    00036 #define MY_CLIENT NetworkClientSocket::Get(0)
    00037
    00038 static uint32 last_ack_frame;
    00039
    00041 static uint32 _password_game_seed;
    00043 static char _password_server_unique_id[NETWORK_UNIQUE_ID_LENGTH];
    00044
    00046 static uint8 _network_server_max_companies;
    00048 static uint8 _network_server_max_spectators;
    00049
    00051 CompanyID _network_join_as;
    00052
    00054 const char *_network_join_server_password = NULL;
    00056 const char *_network_join_company_password = NULL;
    00057
    00059 assert_compile(NETWORK_UNIQUE_ID_LENGTH == 16 * 2 + 1);
    00060
    00066 static const char *GenerateCompanyPasswordHash(const char *password)
    00067 {
    00068   if (StrEmpty(password)) return password;
    00069
    00070   char salted_password[NETWORK_UNIQUE_ID_LENGTH];
    00071
    00072   memset(salted_password, 0, sizeof(salted_password));
    00073   snprintf(salted_password, sizeof(salted_password), "%s", password);
    00074   /* Add the game seed and the server's unique ID as the salt. */
    00075   for (uint i = 0; i < NETWORK_UNIQUE_ID_LENGTH - 1; i++) salted_password[i] ^= _password_server_unique_id[i] ^ (_password_game_seed >> i);
    00076
    00077   Md5 checksum;
    00078   uint8 digest[16];
    00079   static char hashed_password[NETWORK_UNIQUE_ID_LENGTH];
    00080
    00081   /* Generate the MD5 hash */
    00082   checksum.Append((const uint8*)salted_password, sizeof(salted_password) - 1);
    00083   checksum.Finish(digest);
    00084
    00085   for (int di = 0; di < 16; di++) sprintf(hashed_password + di * 2, "%02x", digest[di]);
    00086   hashed_password[lengthof(hashed_password) - 1] = '\0';
    00087
    00088   return hashed_password;
    00089 }
    00090
    00094 void HashCurrentCompanyPassword(const char *password)
    00095 {
    00096   _password_game_seed = _settings_game.game_creation.generation_seed;
    00097   strecpy(_password_server_unique_id, _settings_client.network.network_id, lastof(_password_server_unique_id));
    00098
    00099   const char *new_pw = GenerateCompanyPasswordHash(password);
    00100   strecpy(_network_company_states[_local_company].password, new_pw, lastof(_network_company_states[_local_company].password));
    00101
    00102   if (_network_server) {
    00103     NetworkServerUpdateCompanyPassworded(_local_company, !StrEmpty(_network_company_states[_local_company].password));
    00104   }
    00105 }
    00106
    00107
    00108 /***********
    00109  * Sending functions
    00110  *   DEF_CLIENT_SEND_COMMAND has no parameters
    00111  ************/
    00112
    00113 DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)
    00114 {
    00115   /*
    00116    * Packet: CLIENT_COMPANY_INFO
    00117    * Function: Request company-info (in detail)
    00118    * Data:
    00119    *    <none>
    00120    */
    00121   Packet *p;
    00122   _network_join_status = NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO;
    00123   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00124
    00125   p = new Packet(PACKET_CLIENT_COMPANY_INFO);
    00126   MY_CLIENT->Send_Packet(p);
    00127 }
    00128
    00129 DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_JOIN)
    00130 {
    00131   /*
    00132    * Packet: CLIENT_JOIN
    00133    * Function: Try to join the server
    00134    * Data:
    00135    *    String: OpenTTD Revision (norev000 if no revision)
    00136    *    String: Client Name (max NETWORK_NAME_LENGTH)
    00137    *    uint8:  Play as Company id (1..MAX_COMPANIES)
    00138    *    uint8:  Language ID
    00139    *    String: Unique id to find the client back in server-listing
    00140    */
    00141
    00142   Packet *p;
    00143   _network_join_status = NETWORK_JOIN_STATUS_AUTHORIZING;
    00144   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00145
    00146   p = new Packet(PACKET_CLIENT_JOIN);
    00147   p->Send_string(_openttd_revision);
    00148   p->Send_string(_settings_client.network.client_name); // Client name
    00149   p->Send_uint8 (_network_join_as);     // PlayAs
    00150   p->Send_uint8 (NETLANG_ANY);          // Language
    00151   p->Send_string(_settings_client.network.network_id);
    00152   MY_CLIENT->Send_Packet(p);
    00153 }
    00154
    00155 DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
    00156 {
    00157   /*
    00158    * Packet: CLIENT_NEWGRFS_CHECKED
    00159    * Function: Tell the server that we have the required GRFs
    00160    * Data:
    00161    */
    00162
    00163   Packet *p = new Packet(PACKET_CLIENT_NEWGRFS_CHECKED);
    00164   MY_CLIENT->Send_Packet(p);
    00165 }
    00166
    00167 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_PASSWORD)(NetworkPasswordType type, const char *password)
    00168 {
    00169   /*
    00170    * Packet: CLIENT_PASSWORD
    00171    * Function: Send a password to the server to authorize
    00172    * Data:
    00173    *    uint8:  NetworkPasswordType
    00174    *    String: Password
    00175    */
    00176   Packet *p = new Packet(PACKET_CLIENT_PASSWORD);
    00177   p->Send_uint8 (type);
    00178   p->Send_string(type == NETWORK_GAME_PASSWORD ? password : GenerateCompanyPasswordHash(password));
    00179   MY_CLIENT->Send_Packet(p);
    00180 }
    00181
    00182 DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_GETMAP)
    00183 {
    00184   /*
    00185    * Packet: CLIENT_GETMAP
    00186    * Function: Request the map from the server
    00187    * Data:
    00188    *    <none>
    00189    */
    00190
    00191   Packet *p = new Packet(PACKET_CLIENT_GETMAP);
    00192   MY_CLIENT->Send_Packet(p);
    00193 }
    00194
    00195 DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_MAP_OK)
    00196 {
    00197   /*
    00198    * Packet: CLIENT_MAP_OK
    00199    * Function: Tell the server that we are done receiving/loading the map
    00200    * Data:
    00201    *    <none>
    00202    */
    00203
    00204   Packet *p = new Packet(PACKET_CLIENT_MAP_OK);
    00205   MY_CLIENT->Send_Packet(p);
    00206 }
    00207
    00208 DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_ACK)
    00209 {
    00210   /*
    00211    * Packet: CLIENT_ACK
    00212    * Function: Tell the server we are done with this frame
    00213    * Data:
    00214    *    uint32: current FrameCounter of the client
    00215    */
    00216
    00217   Packet *p = new Packet(PACKET_CLIENT_ACK);
    00218
    00219   p->Send_uint32(_frame_counter);
    00220   MY_CLIENT->Send_Packet(p);
    00221 }
    00222
    00223 /* Send a command packet to the server */
    00224 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMMAND)(const CommandPacket *cp)
    00225 {
    00226   /*
    00227    * Packet: CLIENT_COMMAND
    00228    * Function: Send a DoCommand to the Server
    00229    * Data:
    00230    *    uint8:  CompanyID (0..MAX_COMPANIES-1)
    00231    *    uint32: CommandID (see command.h)
    00232    *    uint32: P1 (free variables used in DoCommand)
    00233    *    uint32: P2
    00234    *    uint32: Tile
    00235    *    string: text
    00236    *    uint8:  CallBackID (see callback_table.c)
    00237    */
    00238
    00239   Packet *p = new Packet(PACKET_CLIENT_COMMAND);
    00240   MY_CLIENT->Send_Command(p, cp);
    00241
    00242   MY_CLIENT->Send_Packet(p);
    00243 }
    00244
    00245 /* Send a chat-packet over the network */
    00246 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
    00247 {
    00248   /*
    00249    * Packet: CLIENT_CHAT
    00250    * Function: Send a chat-packet to the serve
    00251    * Data:
    00252    *    uint8:  ActionID (see network_data.h, NetworkAction)
    00253    *    uint8:  Destination Type (see network_data.h, DestType);
    00254    *    uint32: Destination CompanyID/Client-identifier
    00255    *    String: Message (max NETWORK_CHAT_LENGTH)
    00256    *    uint64: Some arbitrary number
    00257    */
    00258
    00259   Packet *p = new Packet(PACKET_CLIENT_CHAT);
    00260
    00261   p->Send_uint8 (action);
    00262   p->Send_uint8 (type);
    00263   p->Send_uint32(dest);
    00264   p->Send_string(msg);
    00265   p->Send_uint64(data);
    00266
    00267   MY_CLIENT->Send_Packet(p);
    00268 }
    00269
    00270 /* Send an error-packet over the network */
    00271 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_ERROR)(NetworkErrorCode errorno)
    00272 {
    00273   /*
    00274    * Packet: CLIENT_ERROR
    00275    * Function: The client made an error and is quiting the game
    00276    * Data:
    00277    *    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
    00278    */
    00279   Packet *p = new Packet(PACKET_CLIENT_ERROR);
    00280
    00281   p->Send_uint8(errorno);
    00282   MY_CLIENT->Send_Packet(p);
    00283 }
    00284
    00285 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_PASSWORD)(const char *password)
    00286 {
    00287   /*
    00288    * Packet: PACKET_CLIENT_SET_PASSWORD
    00289    * Function: Set the password for the clients current company
    00290    * Data:
    00291    *    String: Password
    00292    */
    00293   Packet *p = new Packet(PACKET_CLIENT_SET_PASSWORD);
    00294
    00295   p->Send_string(GenerateCompanyPasswordHash(password));
    00296   MY_CLIENT->Send_Packet(p);
    00297 }
    00298
    00299 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_NAME)(const char *name)
    00300 {
    00301   /*
    00302    * Packet: PACKET_CLIENT_SET_NAME
    00303    * Function: Gives the client a new name
    00304    * Data:
    00305    *    String: Name
    00306    */
    00307   Packet *p = new Packet(PACKET_CLIENT_SET_NAME);
    00308
    00309   p->Send_string(name);
    00310   MY_CLIENT->Send_Packet(p);
    00311 }
    00312
    00313 /* Send an quit-packet over the network */
    00314 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_QUIT)()
    00315 {
    00316   /*
    00317    * Packet: CLIENT_QUIT
    00318    * Function: The client is quiting the game
    00319    * Data:
    00320    */
    00321   Packet *p = new Packet(PACKET_CLIENT_QUIT);
    00322
    00323   MY_CLIENT->Send_Packet(p);
    00324 }
    00325
    00326 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_RCON)(const char *pass, const char *command)
    00327 {
    00328   Packet *p = new Packet(PACKET_CLIENT_RCON);
    00329   p->Send_string(pass);
    00330   p->Send_string(command);
    00331   MY_CLIENT->Send_Packet(p);
    00332 }
    00333
    00334 DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_MOVE)(CompanyID company, const char *pass)
    00335 {
    00336   Packet *p = new Packet(PACKET_CLIENT_MOVE);
    00337   p->Send_uint8(company);
    00338   p->Send_string(GenerateCompanyPasswordHash(pass));
    00339   MY_CLIENT->Send_Packet(p);
    00340 }
    00341
    00342
    00343 /***********
    00344  * Receiving functions
    00345  *   DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
    00346  ************/
    00347
    00348 extern bool SafeSaveOrLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir);
    00349 extern StringID _switch_mode_errorstr;
    00350
    00351 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FULL)
    00352 {
    00353   /* We try to join a server which is full */
    00354   _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL;
    00355   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00356
    00357   return NETWORK_RECV_STATUS_SERVER_FULL;
    00358 }
    00359
    00360 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_BANNED)
    00361 {
    00362   /* We try to join a server where we are banned */
    00363   _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_BANNED;
    00364   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00365
    00366   return NETWORK_RECV_STATUS_SERVER_BANNED;
    00367 }
    00368
    00369 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO)
    00370 {
    00371   byte company_info_version = p->Recv_uint8();
    00372
    00373   if (!MY_CLIENT->HasClientQuit() && company_info_version == NETWORK_COMPANY_INFO_VERSION) {
    00374     /* We have received all data... (there are no more packets coming) */
    00375     if (!p->Recv_bool()) return NETWORK_RECV_STATUS_CLOSE_QUERY;
    00376
    00377     CompanyID current = (Owner)p->Recv_uint8();
    00378     if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY;
    00379
    00380     NetworkCompanyInfo *company_info = GetLobbyCompanyInfo(current);
    00381     if (company_info == NULL) return NETWORK_RECV_STATUS_CLOSE_QUERY;
    00382
    00383     p->Recv_string(company_info->company_name, sizeof(company_info->company_name));
    00384     company_info->inaugurated_year = p->Recv_uint32();
    00385     company_info->company_value    = p->Recv_uint64();
    00386     company_info->money            = p->Recv_uint64();
    00387     company_info->income           = p->Recv_uint64();
    00388     company_info->performance      = p->Recv_uint16();
    00389     company_info->use_password     = p->Recv_bool();
    00390     for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++)
    00391       company_info->num_vehicle[i] = p->Recv_uint16();
    00392     for (int i = 0; i < NETWORK_STATION_TYPES; i++)
    00393       company_info->num_station[i] = p->Recv_uint16();
    00394     company_info->ai               = p->Recv_bool();
    00395
    00396     p->Recv_string(company_info->clients, sizeof(company_info->clients));
    00397
    00398     SetWindowDirty(WC_NETWORK_WINDOW, 0);
    00399
    00400     return NETWORK_RECV_STATUS_OKAY;
    00401   }
    00402
    00403   return NETWORK_RECV_STATUS_CLOSE_QUERY;
    00404 }
    00405
    00406 /* This packet contains info about the client (playas and name)
    00407  *  as client we save this in NetworkClientInfo, linked via 'client_id'
    00408  *  which is always an unique number on a server. */
    00409 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
    00410 {
    00411   NetworkClientInfo *ci;
    00412   ClientID client_id = (ClientID)p->Recv_uint32();
    00413   CompanyID playas = (CompanyID)p->Recv_uint8();
    00414   char name[NETWORK_NAME_LENGTH];
    00415
    00416   p->Recv_string(name, sizeof(name));
    00417
    00418   if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
    00419
    00420   ci = NetworkFindClientInfoFromClientID(client_id);
    00421   if (ci != NULL) {
    00422     if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
    00423       /* Client name changed, display the change */
    00424       NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
    00425     } else if (playas != ci->client_playas) {
    00426       /* The client changed from client-player..
    00427        * Do not display that for now */
    00428     }
    00429
    00430     ci->client_playas = playas;
    00431     strecpy(ci->client_name, name, lastof(ci->client_name));
    00432
    00433     SetWindowDirty(WC_CLIENT_LIST, 0);
    00434
    00435     return NETWORK_RECV_STATUS_OKAY;
    00436   }
    00437
    00438   /* We don't have this client_id yet, find an empty client_id, and put the data there */
    00439   ci = new NetworkClientInfo(client_id);
    00440   ci->client_playas = playas;
    00441   if (client_id == _network_own_client_id) MY_CLIENT->SetInfo(ci);
    00442
    00443   strecpy(ci->client_name, name, lastof(ci->client_name));
    00444
    00445   SetWindowDirty(WC_CLIENT_LIST, 0);
    00446
    00447   return NETWORK_RECV_STATUS_OKAY;
    00448 }
    00449
    00450 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR)
    00451 {
    00452   NetworkErrorCode error = (NetworkErrorCode)p->Recv_uint8();
    00453
    00454   switch (error) {
    00455     /* We made an error in the protocol, and our connection is closed.... */
    00456     case NETWORK_ERROR_NOT_AUTHORIZED:
    00457     case NETWORK_ERROR_NOT_EXPECTED:
    00458     case NETWORK_ERROR_COMPANY_MISMATCH:
    00459       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_ERROR;
    00460       break;
    00461     case NETWORK_ERROR_FULL:
    00462       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL;
    00463       break;
    00464     case NETWORK_ERROR_WRONG_REVISION:
    00465       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_REVISION;
    00466       break;
    00467     case NETWORK_ERROR_WRONG_PASSWORD:
    00468       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_PASSWORD;
    00469       break;
    00470     case NETWORK_ERROR_KICKED:
    00471       _switch_mode_errorstr = STR_NETWORK_ERROR_KICKED;
    00472       break;
    00473     case NETWORK_ERROR_CHEATER:
    00474       _switch_mode_errorstr = STR_NETWORK_ERROR_CHEATER;
    00475       break;
    00476     default:
    00477       _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION;
    00478   }
    00479
    00480   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00481
    00482   return NETWORK_RECV_STATUS_SERVER_ERROR;
    00483 }
    00484
    00485 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)
    00486 {
    00487   uint grf_count = p->Recv_uint8();
    00488   NetworkRecvStatus ret = NETWORK_RECV_STATUS_OKAY;
    00489
    00490   /* Check all GRFs */
    00491   for (; grf_count > 0; grf_count--) {
    00492     GRFConfig c;
    00493     MY_CLIENT->Recv_GRFIdentifier(p, &c);
    00494
    00495     /* Check whether we know this GRF */
    00496     const GRFConfig *f = FindGRFConfig(c.grfid, c.md5sum);
    00497     if (f == NULL) {
    00498       /* We do not know this GRF, bail out of initialization */
    00499       char buf[sizeof(c.md5sum) * 2 + 1];
    00500       md5sumToString(buf, lastof(buf), c.md5sum);
    00501       DEBUG(grf, 0, "NewGRF %08X not found; checksum %s", BSWAP32(c.grfid), buf);
    00502       ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH;
    00503     }
    00504   }
    00505
    00506   if (ret == NETWORK_RECV_STATUS_OKAY) {
    00507     /* Start receiving the map */
    00508     SEND_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)();
    00509   } else {
    00510     /* NewGRF mismatch, bail out */
    00511     _switch_mode_errorstr = STR_NETWORK_ERROR_NEWGRF_MISMATCH;
    00512   }
    00513   return ret;
    00514 }
    00515
    00516 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD)
    00517 {
    00518   NetworkPasswordType type = (NetworkPasswordType)p->Recv_uint8();
    00519
    00520   const char *password = _network_join_server_password;
    00521
    00522   switch (type) {
    00523     case NETWORK_COMPANY_PASSWORD:
    00524       /* Initialize the password hash salting variables. */
    00525       _password_game_seed = p->Recv_uint32();
    00526       p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id));
    00527       if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00528       password = _network_join_company_password;
    00529       /* FALL THROUGH */
    00530     case NETWORK_GAME_PASSWORD:
    00531       if (StrEmpty(password)) {
    00532         ShowNetworkNeedPassword(type);
    00533       } else {
    00534         SEND_COMMAND(PACKET_CLIENT_PASSWORD)(type, password);
    00535       }
    00536       return NETWORK_RECV_STATUS_OKAY;
    00537
    00538     default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00539   }
    00540 }
    00541
    00542 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
    00543 {
    00544   _network_own_client_id = (ClientID)p->Recv_uint32();
    00545
    00546   /* Initialize the password hash salting variables, even if they were previously. */
    00547   _password_game_seed = p->Recv_uint32();
    00548   p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id));
    00549
    00550   /* Start receiving the map */
    00551   SEND_COMMAND(PACKET_CLIENT_GETMAP)();
    00552   return NETWORK_RECV_STATUS_OKAY;
    00553 }
    00554
    00555 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WAIT)
    00556 {
    00557   _network_join_status = NETWORK_JOIN_STATUS_WAITING;
    00558   _network_join_waiting = p->Recv_uint8();
    00559   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00560
    00561   /* We are put on hold for receiving the map.. we need GUI for this ;) */
    00562   DEBUG(net, 1, "The server is currently busy sending the map to someone else, please wait..." );
    00563   DEBUG(net, 1, "There are %d clients in front of you", _network_join_waiting);
    00564
    00565   return NETWORK_RECV_STATUS_OKAY;
    00566 }
    00567
    00568 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP)
    00569 {
    00570   static FILE *file_pointer;
    00571
    00572   byte maptype;
    00573
    00574   maptype = p->Recv_uint8();
    00575
    00576   if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
    00577
    00578   /* First packet, init some stuff */
    00579   if (maptype == MAP_PACKET_START) {
    00580     file_pointer = FioFOpenFile("network_client.tmp", "wb", AUTOSAVE_DIR);;
    00581     if (file_pointer == NULL) {
    00582       _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
    00583       return NETWORK_RECV_STATUS_SAVEGAME;
    00584     }
    00585
    00586     _frame_counter = _frame_counter_server = _frame_counter_max = p->Recv_uint32();
    00587
    00588     _network_join_bytes = 0;
    00589     _network_join_bytes_total = p->Recv_uint32();
    00590
    00591     /* If the network connection has been closed due to loss of connection
    00592      * or when _network_join_kbytes_total is 0, the join status window will
    00593      * do a division by zero. When the connection is lost, we just return
    00594      * that. If kbytes_total is 0, the packet must be malformed as a
    00595      * savegame less than 1 kilobyte is practically impossible. */
    00596     if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
    00597     if (_network_join_bytes_total == 0) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00598
    00599     _network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
    00600     SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00601
    00602     /* The first packet does not contain any more data */
    00603     return NETWORK_RECV_STATUS_OKAY;
    00604   }
    00605
    00606   if (maptype == MAP_PACKET_NORMAL) {
    00607     /* We are still receiving data, put it to the file */
    00608     if (fwrite(p->buffer + p->pos, 1, p->size - p->pos, file_pointer) != (size_t)(p->size - p->pos)) {
    00609       _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
    00610       return NETWORK_RECV_STATUS_SAVEGAME;
    00611     }
    00612
    00613     _network_join_bytes = ftell(file_pointer);
    00614     SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00615   }
    00616
    00617   /* Check if this was the last packet */
    00618   if (maptype == MAP_PACKET_END) {
    00619     fclose(file_pointer);
    00620
    00621     _network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
    00622     SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00623
    00624     /* The map is done downloading, load it */
    00625     if (!SafeSaveOrLoad("network_client.tmp", SL_LOAD, GM_NORMAL, AUTOSAVE_DIR)) {
    00626       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00627       _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
    00628       return NETWORK_RECV_STATUS_SAVEGAME;
    00629     }
    00630     /* If the savegame has successfully loaded, ALL windows have been removed,
    00631      * only toolbar/statusbar and gamefield are visible */
    00632
    00633     /* Say we received the map and loaded it correctly! */
    00634     SEND_COMMAND(PACKET_CLIENT_MAP_OK)();
    00635
    00636     /* New company/spectator (invalid company) or company we want to join is not active
    00637      * Switch local company to spectator and await the server's judgement */
    00638     if (_network_join_as == COMPANY_NEW_COMPANY || !Company::IsValidID(_network_join_as)) {
    00639       SetLocalCompany(COMPANY_SPECTATOR);
    00640
    00641       if (_network_join_as != COMPANY_SPECTATOR) {
    00642         /* We have arrived and ready to start playing; send a command to make a new company;
    00643          * the server will give us a client-id and let us in */
    00644         _network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
    00645         ShowJoinStatusWindow();
    00646         NetworkSend_Command(0, 0, 0, CMD_COMPANY_CTRL, NULL, NULL);
    00647       }
    00648     } else {
    00649       /* take control over an existing company */
    00650       SetLocalCompany(_network_join_as);
    00651     }
    00652   }
    00653
    00654   return NETWORK_RECV_STATUS_OKAY;
    00655 }
    00656
    00657 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FRAME)
    00658 {
    00659   _frame_counter_server = p->Recv_uint32();
    00660   _frame_counter_max = p->Recv_uint32();
    00661 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
    00662   /* Test if the server supports this option
    00663    *  and if we are at the frame the server is */
    00664   if (p->pos < p->size) {
    00665     _sync_frame = _frame_counter_server;
    00666     _sync_seed_1 = p->Recv_uint32();
    00667 #ifdef NETWORK_SEND_DOUBLE_SEED
    00668     _sync_seed_2 = p->Recv_uint32();
    00669 #endif
    00670   }
    00671 #endif
    00672   DEBUG(net, 5, "Received FRAME %d", _frame_counter_server);
    00673
    00674   /* Let the server know that we received this frame correctly
    00675    *  We do this only once per day, to save some bandwidth ;) */
    00676   if (!_network_first_time && last_ack_frame < _frame_counter) {
    00677     last_ack_frame = _frame_counter + DAY_TICKS;
    00678     DEBUG(net, 4, "Sent ACK at %d", _frame_counter);
    00679     SEND_COMMAND(PACKET_CLIENT_ACK)();
    00680   }
    00681
    00682   return NETWORK_RECV_STATUS_OKAY;
    00683 }
    00684
    00685 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SYNC)
    00686 {
    00687   _sync_frame = p->Recv_uint32();
    00688   _sync_seed_1 = p->Recv_uint32();
    00689 #ifdef NETWORK_SEND_DOUBLE_SEED
    00690   _sync_seed_2 = p->Recv_uint32();
    00691 #endif
    00692
    00693   return NETWORK_RECV_STATUS_OKAY;
    00694 }
    00695
    00696 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMMAND)
    00697 {
    00698   CommandPacket cp;
    00699   const char *err = MY_CLIENT->Recv_Command(p, &cp);
    00700   cp.frame    = p->Recv_uint32();
    00701   cp.my_cmd   = p->Recv_bool();
    00702   cp.next     = NULL;
    00703
    00704   if (err != NULL) {
    00705     IConsolePrintF(CC_ERROR, "WARNING: %s from server, dropping...", err);
    00706     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00707   }
    00708
    00709   /* The server did send us this command..
    00710    *  queue it in our own queue, so we can handle it in the upcoming frame! */
    00711   NetworkAddCommandQueue(cp);
    00712
    00713   return NETWORK_RECV_STATUS_OKAY;
    00714 }
    00715
    00716 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
    00717 {
    00718   char name[NETWORK_NAME_LENGTH], msg[NETWORK_CHAT_LENGTH];
    00719   const NetworkClientInfo *ci = NULL, *ci_to;
    00720
    00721   NetworkAction action = (NetworkAction)p->Recv_uint8();
    00722   ClientID client_id = (ClientID)p->Recv_uint32();
    00723   bool self_send = p->Recv_bool();
    00724   p->Recv_string(msg, NETWORK_CHAT_LENGTH);
    00725   int64 data = p->Recv_uint64();
    00726
    00727   ci_to = NetworkFindClientInfoFromClientID(client_id);
    00728   if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
    00729
    00730   /* Did we initiate the action locally? */
    00731   if (self_send) {
    00732     switch (action) {
    00733       case NETWORK_ACTION_CHAT_CLIENT:
    00734         /* For speaking to client we need the client-name */
    00735         snprintf(name, sizeof(name), "%s", ci_to->client_name);
    00736         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00737         break;
    00738
    00739       /* For speaking to company or giving money, we need the company-name */
    00740       case NETWORK_ACTION_GIVE_MONEY:
    00741         if (!Company::IsValidID(ci_to->client_playas)) return NETWORK_RECV_STATUS_OKAY;
    00742         /* fallthrough */
    00743       case NETWORK_ACTION_CHAT_COMPANY: {
    00744         StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
    00745         SetDParam(0, ci_to->client_playas);
    00746
    00747         GetString(name, str, lastof(name));
    00748         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00749       } break;
    00750
    00751       default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00752     }
    00753   } else {
    00754     /* Display message from somebody else */
    00755     snprintf(name, sizeof(name), "%s", ci_to->client_name);
    00756     ci = ci_to;
    00757   }
    00758
    00759   if (ci != NULL)
    00760     NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColour(ci->client_playas), self_send, name, msg, data);
    00761   return NETWORK_RECV_STATUS_OKAY;
    00762 }
    00763
    00764 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
    00765 {
    00766   ClientID client_id = (ClientID)p->Recv_uint32();
    00767
    00768   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    00769   if (ci != NULL) {
    00770     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, GetNetworkErrorMsg((NetworkErrorCode)p->Recv_uint8()));
    00771     delete ci;
    00772   }
    00773
    00774   SetWindowDirty(WC_CLIENT_LIST, 0);
    00775
    00776   return NETWORK_RECV_STATUS_OKAY;
    00777 }
    00778
    00779 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
    00780 {
    00781   NetworkClientInfo *ci;
    00782
    00783   ClientID client_id = (ClientID)p->Recv_uint32();
    00784
    00785   ci = NetworkFindClientInfoFromClientID(client_id);
    00786   if (ci != NULL) {
    00787     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
    00788     delete ci;
    00789   } else {
    00790     DEBUG(net, 0, "Unknown client (%d) is leaving the game", client_id);
    00791   }
    00792
    00793   SetWindowDirty(WC_CLIENT_LIST, 0);
    00794
    00795   /* If we come here it means we could not locate the client.. strange :s */
    00796   return NETWORK_RECV_STATUS_OKAY;
    00797 }
    00798
    00799 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN)
    00800 {
    00801   ClientID client_id = (ClientID)p->Recv_uint32();
    00802
    00803   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    00804   if (ci != NULL)
    00805     NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name);
    00806
    00807   SetWindowDirty(WC_CLIENT_LIST, 0);
    00808
    00809   return NETWORK_RECV_STATUS_OKAY;
    00810 }
    00811
    00812 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN)
    00813 {
    00814   _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_SHUTDOWN;
    00815
    00816   return NETWORK_RECV_STATUS_SERVER_ERROR;
    00817 }
    00818
    00819 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEWGAME)
    00820 {
    00821   /* To trottle the reconnects a bit, every clients waits
    00822    *  his _local_company value before reconnecting
    00823    * COMPANY_SPECTATOR is currently 255, so to avoid long wait periods
    00824    *  set the max to 10. */
    00825   _network_reconnect = min(_local_company + 1, 10);
    00826   _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_REBOOT;
    00827
    00828   return NETWORK_RECV_STATUS_SERVER_ERROR;
    00829 }
    00830
    00831 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_RCON)
    00832 {
    00833   char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
    00834
    00835   ConsoleColour colour_code = (ConsoleColour)p->Recv_uint16();
    00836   p->Recv_string(rcon_out, sizeof(rcon_out));
    00837
    00838   IConsolePrint(colour_code, rcon_out);
    00839
    00840   return NETWORK_RECV_STATUS_OKAY;
    00841 }
    00842
    00843 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MOVE)
    00844 {
    00845   /* Nothing more in this packet... */
    00846   ClientID client_id   = (ClientID)p->Recv_uint32();
    00847   CompanyID company_id = (CompanyID)p->Recv_uint8();
    00848
    00849   if (client_id == 0) {
    00850     /* definitely an invalid client id, debug message and do nothing. */
    00851     DEBUG(net, 0, "[move] received invalid client index = 0");
    00852     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00853   }
    00854
    00855   const NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    00856   /* Just make sure we do not try to use a client_index that does not exist */
    00857   if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
    00858
    00859   /* if not valid player, force spectator, else check player exists */
    00860   if (!Company::IsValidID(company_id)) company_id = COMPANY_SPECTATOR;
    00861
    00862   if (client_id == _network_own_client_id) {
    00863     SetLocalCompany(company_id);
    00864   }
    00865
    00866   return NETWORK_RECV_STATUS_OKAY;
    00867 }
    00868
    00869 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CONFIG_UPDATE)
    00870 {
    00871   _network_server_max_companies = p->Recv_uint8();
    00872   _network_server_max_spectators = p->Recv_uint8();
    00873
    00874   return NETWORK_RECV_STATUS_OKAY;
    00875 }
    00876
    00877 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_UPDATE)
    00878 {
    00879   _network_company_passworded = p->Recv_uint16();
    00880   SetWindowClassesDirty(WC_COMPANY);
    00881
    00882   return NETWORK_RECV_STATUS_OKAY;
    00883 }
    00884
    00885
    00886 /* The layout for the receive-functions by the client */
    00887 typedef NetworkRecvStatus NetworkClientPacket(Packet *p);
    00888
    00889 /* This array matches PacketType. At an incoming
    00890  *  packet it is matches against this array
    00891  *  and that way the right function to handle that
    00892  *  packet is found. */
    00893 static NetworkClientPacket * const _network_client_packet[] = {
    00894   RECEIVE_COMMAND(PACKET_SERVER_FULL),
    00895   RECEIVE_COMMAND(PACKET_SERVER_BANNED),
    00896   NULL, // PACKET_CLIENT_JOIN,
    00897   RECEIVE_COMMAND(PACKET_SERVER_ERROR),
    00898   NULL, // PACKET_CLIENT_COMPANY_INFO,
    00899   RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO),
    00900   RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO),
    00901   RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD),
    00902   NULL, // PACKET_CLIENT_PASSWORD,
    00903   RECEIVE_COMMAND(PACKET_SERVER_WELCOME),
    00904   NULL, // PACKET_CLIENT_GETMAP,
    00905   RECEIVE_COMMAND(PACKET_SERVER_WAIT),
    00906   RECEIVE_COMMAND(PACKET_SERVER_MAP),
    00907   NULL, // PACKET_CLIENT_MAP_OK,
    00908   RECEIVE_COMMAND(PACKET_SERVER_JOIN),
    00909   RECEIVE_COMMAND(PACKET_SERVER_FRAME),
    00910   RECEIVE_COMMAND(PACKET_SERVER_SYNC),
    00911   NULL, // PACKET_CLIENT_ACK,
    00912   NULL, // PACKET_CLIENT_COMMAND,
    00913   RECEIVE_COMMAND(PACKET_SERVER_COMMAND),
    00914   NULL, // PACKET_CLIENT_CHAT,
    00915   RECEIVE_COMMAND(PACKET_SERVER_CHAT),
    00916   NULL, // PACKET_CLIENT_SET_PASSWORD,
    00917   NULL, // PACKET_CLIENT_SET_NAME,
    00918   NULL, // PACKET_CLIENT_QUIT,
    00919   NULL, // PACKET_CLIENT_ERROR,
    00920   RECEIVE_COMMAND(PACKET_SERVER_QUIT),
    00921   RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT),
    00922   RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN),
    00923   RECEIVE_COMMAND(PACKET_SERVER_NEWGAME),
    00924   RECEIVE_COMMAND(PACKET_SERVER_RCON),
    00925   NULL, // PACKET_CLIENT_RCON,
    00926   RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS),
    00927   NULL, // PACKET_CLIENT_NEWGRFS_CHECKED,
    00928   RECEIVE_COMMAND(PACKET_SERVER_MOVE),
    00929   NULL, // PACKET_CLIENT_MOVE
    00930   RECEIVE_COMMAND(PACKET_SERVER_COMPANY_UPDATE),
    00931   RECEIVE_COMMAND(PACKET_SERVER_CONFIG_UPDATE),
    00932 };
    00933
    00934 /* If this fails, check the array above with network_data.h */
    00935 assert_compile(lengthof(_network_client_packet) == PACKET_END);
    00936
    00937 /* Is called after a client is connected to the server */
    00938 void NetworkClient_Connected()
    00939 {
    00940   /* Set the frame-counter to 0 so nothing happens till we are ready */
    00941   _frame_counter = 0;
    00942   _frame_counter_server = 0;
    00943   last_ack_frame = 0;
    00944   /* Request the game-info */
    00945   SEND_COMMAND(PACKET_CLIENT_JOIN)();
    00946 }
    00947
    00948 /* Reads the packets from the socket-stream, if available */
    00949 NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientSocket *cs)
    00950 {
    00951   Packet *p;
    00952   NetworkRecvStatus res = NETWORK_RECV_STATUS_OKAY;
    00953
    00954   while (res == NETWORK_RECV_STATUS_OKAY && (p = cs->Recv_Packet()) != NULL) {
    00955     byte type = p->Recv_uint8();
    00956     if (type < PACKET_END && _network_client_packet[type] != NULL && !MY_CLIENT->HasClientQuit()) {
    00957       res = _network_client_packet[type](p);
    00958     } else {
    00959       res = NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00960       DEBUG(net, 0, "[client] received invalid packet type %d", type);
    00961     }
    00962
    00963     delete p;
    00964   }
    00965
    00966   return res;
    00967 }
    00968
    00969 void NetworkClientSendRcon(const char *password, const char *command)
    00970 {
    00971   SEND_COMMAND(PACKET_CLIENT_RCON)(password, command);
    00972 }
    00973
    00980 void NetworkClientRequestMove(CompanyID company_id, const char *pass)
    00981 {
    00982   SEND_COMMAND(PACKET_CLIENT_MOVE)(company_id, pass);
    00983 }
    00984
    00985 void NetworkUpdateClientName()
    00986 {
    00987   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00988
    00989   if (ci == NULL) return;
    00990
    00991   /* Don't change the name if it is the same as the old name */
    00992   if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
    00993     if (!_network_server) {
    00994       SEND_COMMAND(PACKET_CLIENT_SET_NAME)(_settings_client.network.client_name);
    00995     } else {
    00996       if (NetworkFindName(_settings_client.network.client_name)) {
    00997         NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, _settings_client.network.client_name);
    00998         strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
    00999         NetworkUpdateClientInfo(CLIENT_ID_SERVER);
    01000       }
    01001     }
    01002   }
    01003 }
    01004
    01005 void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
    01006 {
    01007   SEND_COMMAND(PACKET_CLIENT_CHAT)(action, type, dest, msg, data);
    01008 }
    01009
    01010 void NetworkClientSetPassword(const char *password)
    01011 {
    01012   SEND_COMMAND(PACKET_CLIENT_SET_PASSWORD)(password);
    01013 }
    01014
    01020 bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
    01021 {
    01022   /* Only companies actually playing can speak to team. Eg spectators cannot */
    01023   if (!_settings_client.gui.prefer_teamchat || !Company::IsValidID(cio->client_playas)) return false;
    01024
    01025   const NetworkClientInfo *ci;
    01026   FOR_ALL_CLIENT_INFOS(ci) {
    01027     if (ci->client_playas == cio->client_playas && ci != cio) return true;
    01028   }
    01029
    01030   return false;
    01031 }
    01032
    01037 bool NetworkMaxCompaniesReached()
    01038 {
    01039   return Company::GetNumItems() >= (_network_server ? _settings_client.network.max_companies : _network_server_max_companies);
    01040 }
    01041
    01046 bool NetworkMaxSpectatorsReached()
    01047 {
    01048   return NetworkSpectatorCount() >= (_network_server ? _settings_client.network.max_spectators : _network_server_max_spectators);
    01049 }
    01050
    01054 void NetworkPrintClients()
    01055 {
    01056   NetworkClientInfo *ci;
    01057   FOR_ALL_CLIENT_INFOS(ci) {
    01058     IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  company: %1d  IP: %s",
    01059         ci->client_id,
    01060         ci->client_name,
    01061         ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
    01062         GetClientIP(ci));
    01063   }
    01064 }
    01065
    01066 #endif /* ENABLE_NETWORK */