# network_client.cpp

Go to the documentation of this file.

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