# network_client.cpp

Go to the documentation of this file.

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