# network_client.cpp

Go to the documentation of this file.

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