# network_client.cpp

Go to the documentation of this file.

    00001 /* $Id: network_client.cpp 19690 2010-04-21 19:20:28Z 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   ci = NetworkFindClientInfoFromClientID(client_id);
    00457   if (ci != NULL) {
    00458     if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
    00459       /* Client name changed, display the change */
    00460       NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
    00461     } else if (playas != ci->client_playas) {
    00462       /* The client changed from client-player..
    00463        * Do not display that for now */
    00464     }
    00465
    00466     /* Make sure we're in the company the server tells us to be in,
    00467      * for the rare case that we get moved while joining. */
    00468     if (client_id == _network_own_client_id) SetLocalCompany(!Company::IsValidID(playas) ? COMPANY_SPECTATOR : playas);
    00469
    00470     ci->client_playas = playas;
    00471     strecpy(ci->client_name, name, lastof(ci->client_name));
    00472
    00473     SetWindowDirty(WC_CLIENT_LIST, 0);
    00474
    00475     return NETWORK_RECV_STATUS_OKAY;
    00476   }
    00477
    00478   /* We don't have this client_id yet, find an empty client_id, and put the data there */
    00479   ci = new NetworkClientInfo(client_id);
    00480   ci->client_playas = playas;
    00481   if (client_id == _network_own_client_id) MY_CLIENT->SetInfo(ci);
    00482
    00483   strecpy(ci->client_name, name, lastof(ci->client_name));
    00484
    00485   SetWindowDirty(WC_CLIENT_LIST, 0);
    00486
    00487   return NETWORK_RECV_STATUS_OKAY;
    00488 }
    00489
    00490 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR)
    00491 {
    00492   NetworkErrorCode error = (NetworkErrorCode)p->Recv_uint8();
    00493
    00494   switch (error) {
    00495     /* We made an error in the protocol, and our connection is closed.... */
    00496     case NETWORK_ERROR_NOT_AUTHORIZED:
    00497     case NETWORK_ERROR_NOT_EXPECTED:
    00498     case NETWORK_ERROR_COMPANY_MISMATCH:
    00499       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_ERROR;
    00500       break;
    00501     case NETWORK_ERROR_FULL:
    00502       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL;
    00503       break;
    00504     case NETWORK_ERROR_WRONG_REVISION:
    00505       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_REVISION;
    00506       break;
    00507     case NETWORK_ERROR_WRONG_PASSWORD:
    00508       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_PASSWORD;
    00509       break;
    00510     case NETWORK_ERROR_KICKED:
    00511       _switch_mode_errorstr = STR_NETWORK_ERROR_KICKED;
    00512       break;
    00513     case NETWORK_ERROR_CHEATER:
    00514       _switch_mode_errorstr = STR_NETWORK_ERROR_CHEATER;
    00515       break;
    00516     default:
    00517       _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION;
    00518   }
    00519
    00520   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00521
    00522   return NETWORK_RECV_STATUS_SERVER_ERROR;
    00523 }
    00524
    00525 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)
    00526 {
    00527   uint grf_count = p->Recv_uint8();
    00528   NetworkRecvStatus ret = NETWORK_RECV_STATUS_OKAY;
    00529
    00530   /* Check all GRFs */
    00531   for (; grf_count > 0; grf_count--) {
    00532     GRFConfig c;
    00533     MY_CLIENT->Recv_GRFIdentifier(p, &c);
    00534
    00535     /* Check whether we know this GRF */
    00536     const GRFConfig *f = FindGRFConfig(c.grfid, c.md5sum);
    00537     if (f == NULL) {
    00538       /* We do not know this GRF, bail out of initialization */
    00539       char buf[sizeof(c.md5sum) * 2 + 1];
    00540       md5sumToString(buf, lastof(buf), c.md5sum);
    00541       DEBUG(grf, 0, "NewGRF %08X not found; checksum %s", BSWAP32(c.grfid), buf);
    00542       ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH;
    00543     }
    00544   }
    00545
    00546   if (ret == NETWORK_RECV_STATUS_OKAY) {
    00547     /* Start receiving the map */
    00548     return SEND_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)();
    00549   }
    00550
    00551   /* NewGRF mismatch, bail out */
    00552   _switch_mode_errorstr = STR_NETWORK_ERROR_NEWGRF_MISMATCH;
    00553   return ret;
    00554 }
    00555
    00556 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD)
    00557 {
    00558   const char *password = _network_join_server_password;
    00559   if (!StrEmpty(password)) {
    00560     return SEND_COMMAND(PACKET_CLIENT_GAME_PASSWORD)(password);
    00561   }
    00562
    00563   ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD);
    00564
    00565   return NETWORK_RECV_STATUS_OKAY;
    00566 }
    00567
    00568 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD)
    00569 {
    00570   _password_game_seed = p->Recv_uint32();
    00571   p->Recv_string(_password_server_id, sizeof(_password_server_id));
    00572   if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00573
    00574   const char *password = _network_join_company_password;
    00575   if (!StrEmpty(password)) {
    00576     return SEND_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD)(password);
    00577   }
    00578
    00579   ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD);
    00580
    00581   return NETWORK_RECV_STATUS_OKAY;
    00582 }
    00583
    00584 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
    00585 {
    00586   _network_own_client_id = (ClientID)p->Recv_uint32();
    00587
    00588   /* Initialize the password hash salting variables, even if they were previously. */
    00589   _password_game_seed = p->Recv_uint32();
    00590   p->Recv_string(_password_server_id, sizeof(_password_server_id));
    00591
    00592   /* Start receiving the map */
    00593   return SEND_COMMAND(PACKET_CLIENT_GETMAP)();
    00594 }
    00595
    00596 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WAIT)
    00597 {
    00598   _network_join_status = NETWORK_JOIN_STATUS_WAITING;
    00599   _network_join_waiting = p->Recv_uint8();
    00600   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00601
    00602   /* We are put on hold for receiving the map.. we need GUI for this ;) */
    00603   DEBUG(net, 1, "The server is currently busy sending the map to someone else, please wait..." );
    00604   DEBUG(net, 1, "There are %d clients in front of you", _network_join_waiting);
    00605
    00606   return NETWORK_RECV_STATUS_OKAY;
    00607 }
    00608
    00609 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP)
    00610 {
    00611   static FILE *file_pointer;
    00612
    00613   byte maptype;
    00614
    00615   maptype = p->Recv_uint8();
    00616
    00617   if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
    00618
    00619   /* First packet, init some stuff */
    00620   if (maptype == MAP_PACKET_START) {
    00621     file_pointer = FioFOpenFile("network_client.tmp", "wb", AUTOSAVE_DIR);
    00622     if (file_pointer == 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 (maptype == MAP_PACKET_NORMAL) {
    00648     /* We are still receiving data, put it to the file */
    00649     if (fwrite(p->buffer + p->pos, 1, p->size - p->pos, file_pointer) != (size_t)(p->size - p->pos)) {
    00650       _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
    00651       return NETWORK_RECV_STATUS_SAVEGAME;
    00652     }
    00653
    00654     _network_join_bytes = ftell(file_pointer);
    00655     SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00656   }
    00657
    00658   /* Check if this was the last packet */
    00659   if (maptype == MAP_PACKET_END) {
    00660     fclose(file_pointer);
    00661
    00662     _network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
    00663     SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
    00664
    00665     /* The map is done downloading, load it */
    00666     if (!SafeSaveOrLoad("network_client.tmp", SL_LOAD, GM_NORMAL, AUTOSAVE_DIR)) {
    00667       DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
    00668       _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
    00669       return NETWORK_RECV_STATUS_SAVEGAME;
    00670     }
    00671     /* If the savegame has successfully loaded, ALL windows have been removed,
    00672      * only toolbar/statusbar and gamefield are visible */
    00673
    00674     /* Say we received the map and loaded it correctly! */
    00675     SEND_COMMAND(PACKET_CLIENT_MAP_OK)();
    00676
    00677     /* New company/spectator (invalid company) or company we want to join is not active
    00678      * Switch local company to spectator and await the server's judgement */
    00679     if (_network_join_as == COMPANY_NEW_COMPANY || !Company::IsValidID(_network_join_as)) {
    00680       SetLocalCompany(COMPANY_SPECTATOR);
    00681
    00682       if (_network_join_as != COMPANY_SPECTATOR) {
    00683         /* We have arrived and ready to start playing; send a command to make a new company;
    00684          * the server will give us a client-id and let us in */
    00685         _network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
    00686         ShowJoinStatusWindow();
    00687         NetworkSend_Command(0, 0, 0, CMD_COMPANY_CTRL, NULL, NULL, _local_company);
    00688       }
    00689     } else {
    00690       /* take control over an existing company */
    00691       SetLocalCompany(_network_join_as);
    00692     }
    00693   }
    00694
    00695   return NETWORK_RECV_STATUS_OKAY;
    00696 }
    00697
    00698 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FRAME)
    00699 {
    00700   _frame_counter_server = p->Recv_uint32();
    00701   _frame_counter_max = p->Recv_uint32();
    00702 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
    00703   /* Test if the server supports this option
    00704    *  and if we are at the frame the server is */
    00705   if (p->pos < p->size) {
    00706     _sync_frame = _frame_counter_server;
    00707     _sync_seed_1 = p->Recv_uint32();
    00708 #ifdef NETWORK_SEND_DOUBLE_SEED
    00709     _sync_seed_2 = p->Recv_uint32();
    00710 #endif
    00711   }
    00712 #endif
    00713   DEBUG(net, 5, "Received FRAME %d", _frame_counter_server);
    00714
    00715   /* Let the server know that we received this frame correctly
    00716    *  We do this only once per day, to save some bandwidth ;) */
    00717   if (!_network_first_time && last_ack_frame < _frame_counter) {
    00718     last_ack_frame = _frame_counter + DAY_TICKS;
    00719     DEBUG(net, 4, "Sent ACK at %d", _frame_counter);
    00720     SEND_COMMAND(PACKET_CLIENT_ACK)();
    00721   }
    00722
    00723   return NETWORK_RECV_STATUS_OKAY;
    00724 }
    00725
    00726 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SYNC)
    00727 {
    00728   _sync_frame = p->Recv_uint32();
    00729   _sync_seed_1 = p->Recv_uint32();
    00730 #ifdef NETWORK_SEND_DOUBLE_SEED
    00731   _sync_seed_2 = p->Recv_uint32();
    00732 #endif
    00733
    00734   return NETWORK_RECV_STATUS_OKAY;
    00735 }
    00736
    00737 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMMAND)
    00738 {
    00739   CommandPacket cp;
    00740   const char *err = MY_CLIENT->Recv_Command(p, &cp);
    00741   cp.frame    = p->Recv_uint32();
    00742   cp.my_cmd   = p->Recv_bool();
    00743   cp.next     = NULL;
    00744
    00745   if (err != NULL) {
    00746     IConsolePrintF(CC_ERROR, "WARNING: %s from server, dropping...", err);
    00747     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00748   }
    00749
    00750   /* The server did send us this command..
    00751    *  queue it in our own queue, so we can handle it in the upcoming frame! */
    00752   NetworkAddCommandQueue(cp);
    00753
    00754   return NETWORK_RECV_STATUS_OKAY;
    00755 }
    00756
    00757 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
    00758 {
    00759   char name[NETWORK_NAME_LENGTH], msg[NETWORK_CHAT_LENGTH];
    00760   const NetworkClientInfo *ci = NULL, *ci_to;
    00761
    00762   NetworkAction action = (NetworkAction)p->Recv_uint8();
    00763   ClientID client_id = (ClientID)p->Recv_uint32();
    00764   bool self_send = p->Recv_bool();
    00765   p->Recv_string(msg, NETWORK_CHAT_LENGTH);
    00766   int64 data = p->Recv_uint64();
    00767
    00768   ci_to = NetworkFindClientInfoFromClientID(client_id);
    00769   if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
    00770
    00771   /* Did we initiate the action locally? */
    00772   if (self_send) {
    00773     switch (action) {
    00774       case NETWORK_ACTION_CHAT_CLIENT:
    00775         /* For speaking to client we need the client-name */
    00776         snprintf(name, sizeof(name), "%s", ci_to->client_name);
    00777         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00778         break;
    00779
    00780       /* For speaking to company or giving money, we need the company-name */
    00781       case NETWORK_ACTION_GIVE_MONEY:
    00782         if (!Company::IsValidID(ci_to->client_playas)) return NETWORK_RECV_STATUS_OKAY;
    00783         /* fallthrough */
    00784       case NETWORK_ACTION_CHAT_COMPANY: {
    00785         StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
    00786         SetDParam(0, ci_to->client_playas);
    00787
    00788         GetString(name, str, lastof(name));
    00789         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
    00790       } break;
    00791
    00792       default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00793     }
    00794   } else {
    00795     /* Display message from somebody else */
    00796     snprintf(name, sizeof(name), "%s", ci_to->client_name);
    00797     ci = ci_to;
    00798   }
    00799
    00800   if (ci != NULL) {
    00801     NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColour(ci->client_playas), self_send, name, msg, data);
    00802   }
    00803   return NETWORK_RECV_STATUS_OKAY;
    00804 }
    00805
    00806 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
    00807 {
    00808   ClientID client_id = (ClientID)p->Recv_uint32();
    00809
    00810   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    00811   if (ci != NULL) {
    00812     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, GetNetworkErrorMsg((NetworkErrorCode)p->Recv_uint8()));
    00813     delete ci;
    00814   }
    00815
    00816   SetWindowDirty(WC_CLIENT_LIST, 0);
    00817
    00818   return NETWORK_RECV_STATUS_OKAY;
    00819 }
    00820
    00821 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
    00822 {
    00823   NetworkClientInfo *ci;
    00824
    00825   ClientID client_id = (ClientID)p->Recv_uint32();
    00826
    00827   ci = NetworkFindClientInfoFromClientID(client_id);
    00828   if (ci != NULL) {
    00829     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
    00830     delete ci;
    00831   } else {
    00832     DEBUG(net, 0, "Unknown client (%d) is leaving the game", client_id);
    00833   }
    00834
    00835   SetWindowDirty(WC_CLIENT_LIST, 0);
    00836
    00837   /* If we come here it means we could not locate the client.. strange :s */
    00838   return NETWORK_RECV_STATUS_OKAY;
    00839 }
    00840
    00841 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN)
    00842 {
    00843   ClientID client_id = (ClientID)p->Recv_uint32();
    00844
    00845   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    00846   if (ci != NULL) {
    00847     NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name);
    00848   }
    00849
    00850   SetWindowDirty(WC_CLIENT_LIST, 0);
    00851
    00852   return NETWORK_RECV_STATUS_OKAY;
    00853 }
    00854
    00855 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN)
    00856 {
    00857   _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_SHUTDOWN;
    00858
    00859   return NETWORK_RECV_STATUS_SERVER_ERROR;
    00860 }
    00861
    00862 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEWGAME)
    00863 {
    00864   /* To trottle the reconnects a bit, every clients waits
    00865    *  his _local_company value before reconnecting
    00866    * COMPANY_SPECTATOR is currently 255, so to avoid long wait periods
    00867    *  set the max to 10. */
    00868   _network_reconnect = min(_local_company + 1, 10);
    00869   _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_REBOOT;
    00870
    00871   return NETWORK_RECV_STATUS_SERVER_ERROR;
    00872 }
    00873
    00874 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_RCON)
    00875 {
    00876   char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
    00877
    00878   ConsoleColour colour_code = (ConsoleColour)p->Recv_uint16();
    00879   p->Recv_string(rcon_out, sizeof(rcon_out));
    00880
    00881   IConsolePrint(colour_code, rcon_out);
    00882
    00883   return NETWORK_RECV_STATUS_OKAY;
    00884 }
    00885
    00886 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MOVE)
    00887 {
    00888   /* Nothing more in this packet... */
    00889   ClientID client_id   = (ClientID)p->Recv_uint32();
    00890   CompanyID company_id = (CompanyID)p->Recv_uint8();
    00891
    00892   if (client_id == 0) {
    00893     /* definitely an invalid client id, debug message and do nothing. */
    00894     DEBUG(net, 0, "[move] received invalid client index = 0");
    00895     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
    00896   }
    00897
    00898   const NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    00899   /* Just make sure we do not try to use a client_index that does not exist */
    00900   if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
    00901
    00902   /* if not valid player, force spectator, else check player exists */
    00903   if (!Company::IsValidID(company_id)) company_id = COMPANY_SPECTATOR;
    00904
    00905   if (client_id == _network_own_client_id) {
    00906     SetLocalCompany(company_id);
    00907   }
    00908
    00909   return NETWORK_RECV_STATUS_OKAY;
    00910 }
    00911
    00912 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CONFIG_UPDATE)
    00913 {
    00914   _network_server_max_companies = p->Recv_uint8();
    00915   _network_server_max_spectators = p->Recv_uint8();
    00916
    00917   return NETWORK_RECV_STATUS_OKAY;
    00918 }
    00919
    00920 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_UPDATE)
    00921 {
    00922   _network_company_passworded = p->Recv_uint16();
    00923   SetWindowClassesDirty(WC_COMPANY);
    00924
    00925   return NETWORK_RECV_STATUS_OKAY;
    00926 }
    00927
    00928
    00929 /* The layout for the receive-functions by the client */
    00930 typedef NetworkRecvStatus NetworkClientPacket(Packet *p);
    00931
    00932 /* This array matches PacketType. At an incoming
    00933  *  packet it is matches against this array
    00934  *  and that way the right function to handle that
    00935  *  packet is found. */
    00936 static NetworkClientPacket * const _network_client_packet[] = {
    00937   RECEIVE_COMMAND(PACKET_SERVER_FULL),
    00938   RECEIVE_COMMAND(PACKET_SERVER_BANNED),
    00939   NULL, // PACKET_CLIENT_JOIN,
    00940   RECEIVE_COMMAND(PACKET_SERVER_ERROR),
    00941   NULL, // PACKET_CLIENT_COMPANY_INFO,
    00942   RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO),
    00943   RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO),
    00944   RECEIVE_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD),
    00945   RECEIVE_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD),
    00946   NULL, // PACKET_CLIENT_GAME_PASSWORD,
    00947   NULL, // PACKET_CLIENT_COMPANY_PASSWORD,
    00948   RECEIVE_COMMAND(PACKET_SERVER_WELCOME),
    00949   NULL, // PACKET_CLIENT_GETMAP,
    00950   RECEIVE_COMMAND(PACKET_SERVER_WAIT),
    00951   RECEIVE_COMMAND(PACKET_SERVER_MAP),
    00952   NULL, // PACKET_CLIENT_MAP_OK,
    00953   RECEIVE_COMMAND(PACKET_SERVER_JOIN),
    00954   RECEIVE_COMMAND(PACKET_SERVER_FRAME),
    00955   RECEIVE_COMMAND(PACKET_SERVER_SYNC),
    00956   NULL, // PACKET_CLIENT_ACK,
    00957   NULL, // PACKET_CLIENT_COMMAND,
    00958   RECEIVE_COMMAND(PACKET_SERVER_COMMAND),
    00959   NULL, // PACKET_CLIENT_CHAT,
    00960   RECEIVE_COMMAND(PACKET_SERVER_CHAT),
    00961   NULL, // PACKET_CLIENT_SET_PASSWORD,
    00962   NULL, // PACKET_CLIENT_SET_NAME,
    00963   NULL, // PACKET_CLIENT_QUIT,
    00964   NULL, // PACKET_CLIENT_ERROR,
    00965   RECEIVE_COMMAND(PACKET_SERVER_QUIT),
    00966   RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT),
    00967   RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN),
    00968   RECEIVE_COMMAND(PACKET_SERVER_NEWGAME),
    00969   RECEIVE_COMMAND(PACKET_SERVER_RCON),
    00970   NULL, // PACKET_CLIENT_RCON,
    00971   RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS),
    00972   NULL, // PACKET_CLIENT_NEWGRFS_CHECKED,
    00973   RECEIVE_COMMAND(PACKET_SERVER_MOVE),
    00974   NULL, // PACKET_CLIENT_MOVE
    00975   RECEIVE_COMMAND(PACKET_SERVER_COMPANY_UPDATE),
    00976   RECEIVE_COMMAND(PACKET_SERVER_CONFIG_UPDATE),
    00977 };
    00978
    00979 /* If this fails, check the array above with network_data.h */
    00980 assert_compile(lengthof(_network_client_packet) == PACKET_END);
    00981
    00982 /* Is called after a client is connected to the server */
    00983 void NetworkClient_Connected()
    00984 {
    00985   /* Set the frame-counter to 0 so nothing happens till we are ready */
    00986   _frame_counter = 0;
    00987   _frame_counter_server = 0;
    00988   last_ack_frame = 0;
    00989   /* Request the game-info */
    00990   SEND_COMMAND(PACKET_CLIENT_JOIN)();
    00991 }
    00992
    00993 /* Reads the packets from the socket-stream, if available */
    00994 NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientSocket *cs)
    00995 {
    00996   Packet *p;
    00997   NetworkRecvStatus res = NETWORK_RECV_STATUS_OKAY;
    00998
    00999   while (res == NETWORK_RECV_STATUS_OKAY && (p = cs->Recv_Packet()) != NULL) {
    01000     byte type = p->Recv_uint8();
    01001     if (type < PACKET_END && _network_client_packet[type] != NULL && !MY_CLIENT->HasClientQuit()) {
    01002       res = _network_client_packet[type](p);
    01003     } else {
    01004       res = NETWORK_RECV_STATUS_MALFORMED_PACKET;
    01005       DEBUG(net, 0, "[client] received invalid packet type %d", type);
    01006     }
    01007
    01008     delete p;
    01009   }
    01010
    01011   return res;
    01012 }
    01013
    01014 void NetworkClientSendRcon(const char *password, const char *command)
    01015 {
    01016   SEND_COMMAND(PACKET_CLIENT_RCON)(password, command);
    01017 }
    01018
    01025 void NetworkClientRequestMove(CompanyID company_id, const char *pass)
    01026 {
    01027   SEND_COMMAND(PACKET_CLIENT_MOVE)(company_id, pass);
    01028 }
    01029
    01030 void NetworkClientsToSpectators(CompanyID cid)
    01031 {
    01032   /* If our company is changing owner, go to spectators */
    01033   if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR);
    01034
    01035   NetworkClientInfo *ci;
    01036   FOR_ALL_CLIENT_INFOS(ci) {
    01037     if (ci->client_playas != cid) continue;
    01038     NetworkTextMessage(NETWORK_ACTION_COMPANY_SPECTATOR, CC_DEFAULT, false, ci->client_name);
    01039     ci->client_playas = COMPANY_SPECTATOR;
    01040   }
    01041 }
    01042
    01043 void NetworkUpdateClientName()
    01044 {
    01045   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
    01046
    01047   if (ci == NULL) return;
    01048
    01049   /* Don't change the name if it is the same as the old name */
    01050   if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
    01051     if (!_network_server) {
    01052       SEND_COMMAND(PACKET_CLIENT_SET_NAME)(_settings_client.network.client_name);
    01053     } else {
    01054       if (NetworkFindName(_settings_client.network.client_name)) {
    01055         NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, _settings_client.network.client_name);
    01056         strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
    01057         NetworkUpdateClientInfo(CLIENT_ID_SERVER);
    01058       }
    01059     }
    01060   }
    01061 }
    01062
    01063 void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
    01064 {
    01065   SEND_COMMAND(PACKET_CLIENT_CHAT)(action, type, dest, msg, data);
    01066 }
    01067
    01068 static void NetworkClientSetPassword(const char *password)
    01069 {
    01070   SEND_COMMAND(PACKET_CLIENT_SET_PASSWORD)(password);
    01071 }
    01072
    01078 bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
    01079 {
    01080   /* Only companies actually playing can speak to team. Eg spectators cannot */
    01081   if (!_settings_client.gui.prefer_teamchat || !Company::IsValidID(cio->client_playas)) return false;
    01082
    01083   const NetworkClientInfo *ci;
    01084   FOR_ALL_CLIENT_INFOS(ci) {
    01085     if (ci->client_playas == cio->client_playas && ci != cio) return true;
    01086   }
    01087
    01088   return false;
    01089 }
    01090
    01096 const char *NetworkChangeCompanyPassword(const char *password)
    01097 {
    01098   if (strcmp(password, "*") == 0) password = "";
    01099
    01100   if (!_network_server) {
    01101     NetworkClientSetPassword(password);
    01102   } else {
    01103     HashCurrentCompanyPassword(password);
    01104   }
    01105
    01106   return password;
    01107 }
    01108
    01113 bool NetworkMaxCompaniesReached()
    01114 {
    01115   return Company::GetNumItems() >= (_network_server ? _settings_client.network.max_companies : _network_server_max_companies);
    01116 }
    01117
    01122 bool NetworkMaxSpectatorsReached()
    01123 {
    01124   return NetworkSpectatorCount() >= (_network_server ? _settings_client.network.max_spectators : _network_server_max_spectators);
    01125 }
    01126
    01130 void NetworkPrintClients()
    01131 {
    01132   NetworkClientInfo *ci;
    01133   FOR_ALL_CLIENT_INFOS(ci) {
    01134     IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  company: %1d  IP: %s",
    01135         ci->client_id,
    01136         ci->client_name,
    01137         ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
    01138         GetClientIP(ci));
    01139   }
    01140 }
    01141
    01142 #endif /* ENABLE_NETWORK */