network_client.cpp

Go to the documentation of this file.
00001 /* $Id: network_client.cpp 21854 2011-01-19 16:47:40Z 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 "../saveload/saveload_filter.h"
00020 #include "../command_func.h"
00021 #include "../console_func.h"
00022 #include "../fileio_func.h"
00023 #include "../3rdparty/md5/md5.h"
00024 #include "../strings_func.h"
00025 #include "../window_func.h"
00026 #include "../company_func.h"
00027 #include "../company_base.h"
00028 #include "../company_gui.h"
00029 #include "../core/random_func.hpp"
00030 #include "../date_func.h"
00031 #include "../gui.h"
00032 #include "../rev.h"
00033 #include "network.h"
00034 #include "network_base.h"
00035 #include "network_client.h"
00036 
00037 #include "table/strings.h"
00038 
00039 /* This file handles all the client-commands */
00040 
00041 
00043 struct PacketReader : LoadFilter {
00044   static const size_t CHUNK = 32 * 1024;  
00045 
00046   AutoFreeSmallVector<byte *, 16> blocks; 
00047   byte *buf;                              
00048   byte *bufe;                             
00049   byte **block;                           
00050   size_t written_bytes;                   
00051   size_t read_bytes;                      
00052 
00054   PacketReader() : LoadFilter(NULL), buf(NULL), bufe(NULL), block(NULL), written_bytes(0), read_bytes(0)
00055   {
00056   }
00057 
00062   void AddPacket(const Packet *p)
00063   {
00064     assert(this->read_bytes == 0);
00065 
00066     size_t in_packet = p->size - p->pos;
00067     size_t to_write  = min((size_t)(this->bufe - this->buf), in_packet);
00068     const byte *pbuf = p->buffer + p->pos;
00069 
00070     this->written_bytes += in_packet;
00071     if (to_write != 0) {
00072       memcpy(this->buf, pbuf, to_write);
00073       this->buf += to_write;
00074     }
00075 
00076     /* Did everything fit in the current chunk, then we're done. */
00077     if (to_write == in_packet) return;
00078 
00079     /* Allocate a new chunk and add the remaining data. */
00080     pbuf += to_write;
00081     to_write   = in_packet - to_write;
00082     this->buf  = *this->blocks.Append() = CallocT<byte>(CHUNK);
00083     this->bufe = this->buf + CHUNK;
00084 
00085     memcpy(this->buf, pbuf, to_write);
00086     this->buf += to_write;
00087   }
00088 
00089   /* virtual */ size_t Read(byte *rbuf, size_t size)
00090   {
00091     /* Limit the amount to read to whatever we still have. */
00092     size_t ret_size = size = min(this->written_bytes - this->read_bytes, size);
00093     this->read_bytes += ret_size;
00094     const byte *rbufe = rbuf + ret_size;
00095 
00096     while (rbuf != rbufe) {
00097       if (this->buf == this->bufe) {
00098         this->buf = *this->block++;
00099         this->bufe = this->buf + CHUNK;
00100       }
00101 
00102       size_t to_write = min(this->bufe - this->buf, rbufe - rbuf);
00103       memcpy(rbuf, this->buf, to_write);
00104       rbuf += to_write;
00105       this->buf += to_write;
00106     }
00107 
00108     return ret_size;
00109   }
00110 
00111   /* virtual */ void Reset()
00112   {
00113     this->read_bytes = 0;
00114 
00115     this->block = this->blocks.Begin();
00116     this->buf   = *this->block++;
00117     this->bufe  = this->buf + CHUNK;
00118   }
00119 };
00120 
00121 
00126 ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s), savegame(NULL), status(STATUS_INACTIVE)
00127 {
00128   assert(ClientNetworkGameSocketHandler::my_client == NULL);
00129   ClientNetworkGameSocketHandler::my_client = this;
00130 }
00131 
00133 ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler()
00134 {
00135   assert(ClientNetworkGameSocketHandler::my_client == this);
00136   ClientNetworkGameSocketHandler::my_client = NULL;
00137 
00138   delete this->savegame;
00139 }
00140 
00141 NetworkRecvStatus ClientNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
00142 {
00143   assert(status != NETWORK_RECV_STATUS_OKAY);
00144   /*
00145    * Sending a message just before leaving the game calls cs->SendPackets.
00146    * This might invoke this function, which means that when we close the
00147    * connection after cs->SendPackets we will close an already closed
00148    * connection. This handles that case gracefully without having to make
00149    * that code any more complex or more aware of the validity of the socket.
00150    */
00151   if (this->sock == INVALID_SOCKET) return status;
00152 
00153   DEBUG(net, 1, "Closed client connection %d", this->client_id);
00154 
00155   this->SendPackets(true);
00156 
00157   delete this->GetInfo();
00158   delete this;
00159 
00160   return status;
00161 }
00162 
00167 void ClientNetworkGameSocketHandler::ClientError(NetworkRecvStatus res)
00168 {
00169   /* First, send a CLIENT_ERROR to the server, so he knows we are
00170    *  disconnection (and why!) */
00171   NetworkErrorCode errorno;
00172 
00173   /* We just want to close the connection.. */
00174   if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
00175     this->NetworkSocketHandler::CloseConnection();
00176     this->CloseConnection(res);
00177     _networking = false;
00178 
00179     DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00180     return;
00181   }
00182 
00183   switch (res) {
00184     case NETWORK_RECV_STATUS_DESYNC:          errorno = NETWORK_ERROR_DESYNC; break;
00185     case NETWORK_RECV_STATUS_SAVEGAME:        errorno = NETWORK_ERROR_SAVEGAME_FAILED; break;
00186     case NETWORK_RECV_STATUS_NEWGRF_MISMATCH: errorno = NETWORK_ERROR_NEWGRF_MISMATCH; break;
00187     default:                                  errorno = NETWORK_ERROR_GENERAL; break;
00188   }
00189 
00190   /* This means we fucked up and the server closed the connection */
00191   if (res != NETWORK_RECV_STATUS_SERVER_ERROR && res != NETWORK_RECV_STATUS_SERVER_FULL &&
00192       res != NETWORK_RECV_STATUS_SERVER_BANNED) {
00193     SendError(errorno);
00194   }
00195 
00196   _switch_mode = SM_MENU;
00197   this->CloseConnection(res);
00198   _networking = false;
00199 }
00200 
00201 
00207 /*static */ bool ClientNetworkGameSocketHandler::Receive()
00208 {
00209   if (my_client->CanSendReceive()) {
00210     NetworkRecvStatus res = my_client->ReceivePackets();
00211     if (res != NETWORK_RECV_STATUS_OKAY) {
00212       /* The client made an error of which we can not recover
00213         *   close the client and drop back to main menu */
00214       my_client->ClientError(res);
00215       return false;
00216     }
00217   }
00218   return _networking;
00219 }
00220 
00222 /*static */ void ClientNetworkGameSocketHandler::Send()
00223 {
00224   my_client->SendPackets();
00225   my_client->CheckConnection();
00226 }
00227 
00232 /* static */ bool ClientNetworkGameSocketHandler::GameLoop()
00233 {
00234   _frame_counter++;
00235 
00236   NetworkExecuteLocalCommandQueue();
00237 
00238   extern void StateGameLoop();
00239   StateGameLoop();
00240 
00241   /* Check if we are in sync! */
00242   if (_sync_frame != 0) {
00243     if (_sync_frame == _frame_counter) {
00244 #ifdef NETWORK_SEND_DOUBLE_SEED
00245       if (_sync_seed_1 != _random.state[0] || _sync_seed_2 != _random.state[1]) {
00246 #else
00247       if (_sync_seed_1 != _random.state[0]) {
00248 #endif
00249         NetworkError(STR_NETWORK_ERROR_DESYNC);
00250         DEBUG(desync, 1, "sync_err: %08x; %02x", _date, _date_fract);
00251         DEBUG(net, 0, "Sync error detected!");
00252         my_client->ClientError(NETWORK_RECV_STATUS_DESYNC);
00253         return false;
00254       }
00255 
00256       /* If this is the first time we have a sync-frame, we
00257        *   need to let the server know that we are ready and at the same
00258        *   frame as he is.. so we can start playing! */
00259       if (_network_first_time) {
00260         _network_first_time = false;
00261         SendAck();
00262       }
00263 
00264       _sync_frame = 0;
00265     } else if (_sync_frame < _frame_counter) {
00266       DEBUG(net, 1, "Missed frame for sync-test (%d / %d)", _sync_frame, _frame_counter);
00267       _sync_frame = 0;
00268     }
00269   }
00270 
00271   return true;
00272 }
00273 
00274 
00276 ClientNetworkGameSocketHandler * ClientNetworkGameSocketHandler::my_client = NULL;
00277 
00278 static uint32 last_ack_frame;
00279 
00281 static uint32 _password_game_seed;
00283 static char _password_server_id[NETWORK_SERVER_ID_LENGTH];
00284 
00286 static uint8 _network_server_max_companies;
00288 static uint8 _network_server_max_spectators;
00289 
00291 CompanyID _network_join_as;
00292 
00294 const char *_network_join_server_password = NULL;
00296 const char *_network_join_company_password = NULL;
00297 
00299 assert_compile(NETWORK_SERVER_ID_LENGTH == 16 * 2 + 1);
00300 
00301 /***********
00302  * Sending functions
00303  *   DEF_CLIENT_SEND_COMMAND has no parameters
00304  ************/
00305 
00306 NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyInformationQuery()
00307 {
00308   my_client->status = STATUS_COMPANY_INFO;
00309   _network_join_status = NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO;
00310   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00311 
00312   Packet *p = new Packet(PACKET_CLIENT_COMPANY_INFO);
00313   my_client->SendPacket(p);
00314   return NETWORK_RECV_STATUS_OKAY;
00315 }
00316 
00317 NetworkRecvStatus ClientNetworkGameSocketHandler::SendJoin()
00318 {
00319   my_client->status = STATUS_JOIN;
00320   _network_join_status = NETWORK_JOIN_STATUS_AUTHORIZING;
00321   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00322 
00323   Packet *p = new Packet(PACKET_CLIENT_JOIN);
00324   p->Send_string(_openttd_revision);
00325   p->Send_string(_settings_client.network.client_name); // Client name
00326   p->Send_uint8 (_network_join_as);     // PlayAs
00327   p->Send_uint8 (NETLANG_ANY);          // Language
00328   my_client->SendPacket(p);
00329   return NETWORK_RECV_STATUS_OKAY;
00330 }
00331 
00332 NetworkRecvStatus ClientNetworkGameSocketHandler::SendNewGRFsOk()
00333 {
00334   Packet *p = new Packet(PACKET_CLIENT_NEWGRFS_CHECKED);
00335   my_client->SendPacket(p);
00336   return NETWORK_RECV_STATUS_OKAY;
00337 }
00338 
00339 NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const char *password)
00340 {
00341   Packet *p = new Packet(PACKET_CLIENT_GAME_PASSWORD);
00342   p->Send_string(password);
00343   my_client->SendPacket(p);
00344   return NETWORK_RECV_STATUS_OKAY;
00345 }
00346 
00347 NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyPassword(const char *password)
00348 {
00349   Packet *p = new Packet(PACKET_CLIENT_COMPANY_PASSWORD);
00350   p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
00351   my_client->SendPacket(p);
00352   return NETWORK_RECV_STATUS_OKAY;
00353 }
00354 
00355 NetworkRecvStatus ClientNetworkGameSocketHandler::SendGetMap()
00356 {
00357   my_client->status = STATUS_MAP_WAIT;
00358 
00359   Packet *p = new Packet(PACKET_CLIENT_GETMAP);
00360   /* Send the OpenTTD version to the server, let it validate it too.
00361    * But only do it for stable releases because of those we are sure
00362    * that everybody has the same NewGRF version. For trunk and the
00363    * branches we make tarballs of the OpenTTDs compiled from tarball
00364    * will have the lower bits set to 0. As such they would become
00365    * incompatible, which we would like to prevent by this. */
00366   if (HasBit(_openttd_newgrf_version, 19)) p->Send_uint32(_openttd_newgrf_version);
00367   my_client->SendPacket(p);
00368   return NETWORK_RECV_STATUS_OKAY;
00369 }
00370 
00371 NetworkRecvStatus ClientNetworkGameSocketHandler::SendMapOk()
00372 {
00373   my_client->status = STATUS_ACTIVE;
00374 
00375   Packet *p = new Packet(PACKET_CLIENT_MAP_OK);
00376   my_client->SendPacket(p);
00377   return NETWORK_RECV_STATUS_OKAY;
00378 }
00379 
00380 NetworkRecvStatus ClientNetworkGameSocketHandler::SendAck()
00381 {
00382   Packet *p = new Packet(PACKET_CLIENT_ACK);
00383 
00384   p->Send_uint32(_frame_counter);
00385   p->Send_uint8 (my_client->token);
00386   my_client->SendPacket(p);
00387   return NETWORK_RECV_STATUS_OKAY;
00388 }
00389 
00390 /* Send a command packet to the server */
00391 NetworkRecvStatus ClientNetworkGameSocketHandler::SendCommand(const CommandPacket *cp)
00392 {
00393   Packet *p = new Packet(PACKET_CLIENT_COMMAND);
00394   my_client->NetworkGameSocketHandler::SendCommand(p, cp);
00395 
00396   my_client->SendPacket(p);
00397   return NETWORK_RECV_STATUS_OKAY;
00398 }
00399 
00400 /* Send a chat-packet over the network */
00401 NetworkRecvStatus ClientNetworkGameSocketHandler::SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
00402 {
00403   Packet *p = new Packet(PACKET_CLIENT_CHAT);
00404 
00405   p->Send_uint8 (action);
00406   p->Send_uint8 (type);
00407   p->Send_uint32(dest);
00408   p->Send_string(msg);
00409   p->Send_uint64(data);
00410 
00411   my_client->SendPacket(p);
00412   return NETWORK_RECV_STATUS_OKAY;
00413 }
00414 
00415 /* Send an error-packet over the network */
00416 NetworkRecvStatus ClientNetworkGameSocketHandler::SendError(NetworkErrorCode errorno)
00417 {
00418   Packet *p = new Packet(PACKET_CLIENT_ERROR);
00419 
00420   p->Send_uint8(errorno);
00421   my_client->SendPacket(p);
00422   return NETWORK_RECV_STATUS_OKAY;
00423 }
00424 
00425 NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetPassword(const char *password)
00426 {
00427   Packet *p = new Packet(PACKET_CLIENT_SET_PASSWORD);
00428 
00429   p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
00430   my_client->SendPacket(p);
00431   return NETWORK_RECV_STATUS_OKAY;
00432 }
00433 
00434 NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetName(const char *name)
00435 {
00436   Packet *p = new Packet(PACKET_CLIENT_SET_NAME);
00437 
00438   p->Send_string(name);
00439   my_client->SendPacket(p);
00440   return NETWORK_RECV_STATUS_OKAY;
00441 }
00442 
00443 NetworkRecvStatus ClientNetworkGameSocketHandler::SendQuit()
00444 {
00445   Packet *p = new Packet(PACKET_CLIENT_QUIT);
00446 
00447   my_client->SendPacket(p);
00448   return NETWORK_RECV_STATUS_OKAY;
00449 }
00450 
00451 NetworkRecvStatus ClientNetworkGameSocketHandler::SendRCon(const char *pass, const char *command)
00452 {
00453   Packet *p = new Packet(PACKET_CLIENT_RCON);
00454   p->Send_string(pass);
00455   p->Send_string(command);
00456   my_client->SendPacket(p);
00457   return NETWORK_RECV_STATUS_OKAY;
00458 }
00459 
00460 NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const char *password)
00461 {
00462   Packet *p = new Packet(PACKET_CLIENT_MOVE);
00463   p->Send_uint8(company);
00464   p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
00465   my_client->SendPacket(p);
00466   return NETWORK_RECV_STATUS_OKAY;
00467 }
00468 
00469 
00470 /***********
00471  * Receiving functions
00472  *   DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
00473  ************/
00474 
00475 extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
00476 extern StringID _switch_mode_errorstr;
00477 
00478 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_FULL)
00479 {
00480   /* We try to join a server which is full */
00481   _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL;
00482   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00483 
00484   return NETWORK_RECV_STATUS_SERVER_FULL;
00485 }
00486 
00487 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_BANNED)
00488 {
00489   /* We try to join a server where we are banned */
00490   _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_BANNED;
00491   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00492 
00493   return NETWORK_RECV_STATUS_SERVER_BANNED;
00494 }
00495 
00496 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_COMPANY_INFO)
00497 {
00498   if (this->status != STATUS_COMPANY_INFO) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00499 
00500   byte company_info_version = p->Recv_uint8();
00501 
00502   if (!this->HasClientQuit() && company_info_version == NETWORK_COMPANY_INFO_VERSION) {
00503     /* We have received all data... (there are no more packets coming) */
00504     if (!p->Recv_bool()) return NETWORK_RECV_STATUS_CLOSE_QUERY;
00505 
00506     CompanyID current = (Owner)p->Recv_uint8();
00507     if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY;
00508 
00509     NetworkCompanyInfo *company_info = GetLobbyCompanyInfo(current);
00510     if (company_info == NULL) return NETWORK_RECV_STATUS_CLOSE_QUERY;
00511 
00512     p->Recv_string(company_info->company_name, sizeof(company_info->company_name));
00513     company_info->inaugurated_year = p->Recv_uint32();
00514     company_info->company_value    = p->Recv_uint64();
00515     company_info->money            = p->Recv_uint64();
00516     company_info->income           = p->Recv_uint64();
00517     company_info->performance      = p->Recv_uint16();
00518     company_info->use_password     = p->Recv_bool();
00519     for (uint i = 0; i < NETWORK_VEH_END; i++) {
00520       company_info->num_vehicle[i] = p->Recv_uint16();
00521     }
00522     for (uint i = 0; i < NETWORK_VEH_END; i++) {
00523       company_info->num_station[i] = p->Recv_uint16();
00524     }
00525     company_info->ai               = p->Recv_bool();
00526 
00527     p->Recv_string(company_info->clients, sizeof(company_info->clients));
00528 
00529     SetWindowDirty(WC_NETWORK_WINDOW, 0);
00530 
00531     return NETWORK_RECV_STATUS_OKAY;
00532   }
00533 
00534   return NETWORK_RECV_STATUS_CLOSE_QUERY;
00535 }
00536 
00537 /* This packet contains info about the client (playas and name)
00538  *  as client we save this in NetworkClientInfo, linked via 'client_id'
00539  *  which is always an unique number on a server. */
00540 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CLIENT_INFO)
00541 {
00542   NetworkClientInfo *ci;
00543   ClientID client_id = (ClientID)p->Recv_uint32();
00544   CompanyID playas = (CompanyID)p->Recv_uint8();
00545   char name[NETWORK_NAME_LENGTH];
00546 
00547   p->Recv_string(name, sizeof(name));
00548 
00549   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00550   if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
00551 
00552   ci = NetworkFindClientInfoFromClientID(client_id);
00553   if (ci != NULL) {
00554     if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
00555       /* Client name changed, display the change */
00556       NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
00557     } else if (playas != ci->client_playas) {
00558       /* The client changed from client-player..
00559        * Do not display that for now */
00560     }
00561 
00562     /* Make sure we're in the company the server tells us to be in,
00563      * for the rare case that we get moved while joining. */
00564     if (client_id == _network_own_client_id) SetLocalCompany(!Company::IsValidID(playas) ? COMPANY_SPECTATOR : playas);
00565 
00566     ci->client_playas = playas;
00567     strecpy(ci->client_name, name, lastof(ci->client_name));
00568 
00569     SetWindowDirty(WC_CLIENT_LIST, 0);
00570 
00571     return NETWORK_RECV_STATUS_OKAY;
00572   }
00573 
00574   /* We don't have this client_id yet, find an empty client_id, and put the data there */
00575   ci = new NetworkClientInfo(client_id);
00576   ci->client_playas = playas;
00577   if (client_id == _network_own_client_id) this->SetInfo(ci);
00578 
00579   strecpy(ci->client_name, name, lastof(ci->client_name));
00580 
00581   SetWindowDirty(WC_CLIENT_LIST, 0);
00582 
00583   return NETWORK_RECV_STATUS_OKAY;
00584 }
00585 
00586 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_ERROR)
00587 {
00588   NetworkErrorCode error = (NetworkErrorCode)p->Recv_uint8();
00589 
00590   switch (error) {
00591     /* We made an error in the protocol, and our connection is closed.... */
00592     case NETWORK_ERROR_NOT_AUTHORIZED:
00593     case NETWORK_ERROR_NOT_EXPECTED:
00594     case NETWORK_ERROR_COMPANY_MISMATCH:
00595       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_ERROR;
00596       break;
00597     case NETWORK_ERROR_FULL:
00598       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL;
00599       break;
00600     case NETWORK_ERROR_WRONG_REVISION:
00601       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_REVISION;
00602       break;
00603     case NETWORK_ERROR_WRONG_PASSWORD:
00604       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_PASSWORD;
00605       break;
00606     case NETWORK_ERROR_KICKED:
00607       _switch_mode_errorstr = STR_NETWORK_ERROR_KICKED;
00608       break;
00609     case NETWORK_ERROR_CHEATER:
00610       _switch_mode_errorstr = STR_NETWORK_ERROR_CHEATER;
00611       break;
00612     case NETWORK_ERROR_TOO_MANY_COMMANDS:
00613       _switch_mode_errorstr = STR_NETWORK_ERROR_TOO_MANY_COMMANDS;
00614       break;
00615     default:
00616       _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION;
00617   }
00618 
00619   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00620 
00621   return NETWORK_RECV_STATUS_SERVER_ERROR;
00622 }
00623 
00624 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CHECK_NEWGRFS)
00625 {
00626   if (this->status != STATUS_JOIN) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00627 
00628   uint grf_count = p->Recv_uint8();
00629   NetworkRecvStatus ret = NETWORK_RECV_STATUS_OKAY;
00630 
00631   /* Check all GRFs */
00632   for (; grf_count > 0; grf_count--) {
00633     GRFIdentifier c;
00634     this->ReceiveGRFIdentifier(p, &c);
00635 
00636     /* Check whether we know this GRF */
00637     const GRFConfig *f = FindGRFConfig(c.grfid, FGCM_EXACT, c.md5sum);
00638     if (f == NULL) {
00639       /* We do not know this GRF, bail out of initialization */
00640       char buf[sizeof(c.md5sum) * 2 + 1];
00641       md5sumToString(buf, lastof(buf), c.md5sum);
00642       DEBUG(grf, 0, "NewGRF %08X not found; checksum %s", BSWAP32(c.grfid), buf);
00643       ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH;
00644     }
00645   }
00646 
00647   if (ret == NETWORK_RECV_STATUS_OKAY) {
00648     /* Start receiving the map */
00649     return SendNewGRFsOk();
00650   }
00651 
00652   /* NewGRF mismatch, bail out */
00653   _switch_mode_errorstr = STR_NETWORK_ERROR_NEWGRF_MISMATCH;
00654   return ret;
00655 }
00656 
00657 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_NEED_GAME_PASSWORD)
00658 {
00659   if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_GAME) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00660   this->status = STATUS_AUTH_GAME;
00661 
00662   const char *password = _network_join_server_password;
00663   if (!StrEmpty(password)) {
00664     return SendGamePassword(password);
00665   }
00666 
00667   ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD);
00668 
00669   return NETWORK_RECV_STATUS_OKAY;
00670 }
00671 
00672 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_NEED_COMPANY_PASSWORD)
00673 {
00674   if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_COMPANY) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00675   this->status = STATUS_AUTH_COMPANY;
00676 
00677   _password_game_seed = p->Recv_uint32();
00678   p->Recv_string(_password_server_id, sizeof(_password_server_id));
00679   if (this->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00680 
00681   const char *password = _network_join_company_password;
00682   if (!StrEmpty(password)) {
00683     return SendCompanyPassword(password);
00684   }
00685 
00686   ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD);
00687 
00688   return NETWORK_RECV_STATUS_OKAY;
00689 }
00690 
00691 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_WELCOME)
00692 {
00693   if (this->status < STATUS_JOIN || this->status >= STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00694   this->status = STATUS_AUTHORIZED;
00695 
00696   _network_own_client_id = (ClientID)p->Recv_uint32();
00697 
00698   /* Initialize the password hash salting variables, even if they were previously. */
00699   _password_game_seed = p->Recv_uint32();
00700   p->Recv_string(_password_server_id, sizeof(_password_server_id));
00701 
00702   /* Start receiving the map */
00703   return SendGetMap();
00704 }
00705 
00706 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_WAIT)
00707 {
00708   if (this->status != STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00709   this->status = STATUS_MAP_WAIT;
00710 
00711   _network_join_status = NETWORK_JOIN_STATUS_WAITING;
00712   _network_join_waiting = p->Recv_uint8();
00713   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00714 
00715   /* We are put on hold for receiving the map.. we need GUI for this ;) */
00716   DEBUG(net, 1, "The server is currently busy sending the map to someone else, please wait..." );
00717   DEBUG(net, 1, "There are %d clients in front of you", _network_join_waiting);
00718 
00719   return NETWORK_RECV_STATUS_OKAY;
00720 }
00721 
00722 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_BEGIN)
00723 {
00724   if (this->status < STATUS_AUTHORIZED || this->status >= STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00725   this->status = STATUS_MAP;
00726 
00727   if (this->savegame != NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00728 
00729   this->savegame = new PacketReader();
00730 
00731   _frame_counter = _frame_counter_server = _frame_counter_max = p->Recv_uint32();
00732 
00733   _network_join_bytes = 0;
00734   _network_join_bytes_total = 0;
00735 
00736   _network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
00737   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00738 
00739   return NETWORK_RECV_STATUS_OKAY;
00740 }
00741 
00742 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_SIZE)
00743 {
00744   if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00745   if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00746 
00747   _network_join_bytes_total = p->Recv_uint32();
00748   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00749 
00750   return NETWORK_RECV_STATUS_OKAY;
00751 }
00752 
00753 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_DATA)
00754 {
00755   if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00756   if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00757 
00758   /* We are still receiving data, put it to the file */
00759   this->savegame->AddPacket(p);
00760 
00761   _network_join_bytes = (uint32)this->savegame->written_bytes;
00762   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00763 
00764   return NETWORK_RECV_STATUS_OKAY;
00765 }
00766 
00767 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_DONE)
00768 {
00769   if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00770   if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00771 
00772   _network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
00773   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00774 
00775   /*
00776    * Make sure everything is set for reading.
00777    *
00778    * We need the local copy and reset this->savegame because when
00779    * loading fails the network gets reset upon loading the intro
00780    * game, which would cause us to free this->savegame twice.
00781    */
00782   LoadFilter *lf = this->savegame;
00783   this->savegame = NULL;
00784   lf->Reset();
00785 
00786   /* The map is done downloading, load it */
00787   bool load_success = SafeLoad(NULL, SL_LOAD, GM_NORMAL, NO_DIRECTORY, lf);
00788 
00789   /* Long savegame loads shouldn't affect the lag calculation! */
00790   this->last_packet = _realtime_tick;
00791 
00792   if (!load_success) {
00793     DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00794     _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
00795     return NETWORK_RECV_STATUS_SAVEGAME;
00796   }
00797   /* If the savegame has successfully loaded, ALL windows have been removed,
00798    * only toolbar/statusbar and gamefield are visible */
00799 
00800   /* Say we received the map and loaded it correctly! */
00801   SendMapOk();
00802 
00803   /* New company/spectator (invalid company) or company we want to join is not active
00804    * Switch local company to spectator and await the server's judgement */
00805   if (_network_join_as == COMPANY_NEW_COMPANY || !Company::IsValidID(_network_join_as)) {
00806     SetLocalCompany(COMPANY_SPECTATOR);
00807 
00808     if (_network_join_as != COMPANY_SPECTATOR) {
00809       /* We have arrived and ready to start playing; send a command to make a new company;
00810        * the server will give us a client-id and let us in */
00811       _network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
00812       ShowJoinStatusWindow();
00813       NetworkSendCommand(0, 0, 0, CMD_COMPANY_CTRL, NULL, NULL, _local_company);
00814     }
00815   } else {
00816     /* take control over an existing company */
00817     SetLocalCompany(_network_join_as);
00818   }
00819 
00820   return NETWORK_RECV_STATUS_OKAY;
00821 }
00822 
00823 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_FRAME)
00824 {
00825   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00826 
00827   _frame_counter_server = p->Recv_uint32();
00828   _frame_counter_max = p->Recv_uint32();
00829 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
00830   /* Test if the server supports this option
00831    *  and if we are at the frame the server is */
00832   if (p->pos + 1 < p->size) {
00833     _sync_frame = _frame_counter_server;
00834     _sync_seed_1 = p->Recv_uint32();
00835 #ifdef NETWORK_SEND_DOUBLE_SEED
00836     _sync_seed_2 = p->Recv_uint32();
00837 #endif
00838   }
00839 #endif
00840   /* Receive the token. */
00841   if (p->pos != p->size) this->token = p->Recv_uint8();
00842 
00843   DEBUG(net, 5, "Received FRAME %d", _frame_counter_server);
00844 
00845   /* Let the server know that we received this frame correctly
00846    *  We do this only once per day, to save some bandwidth ;) */
00847   if (!_network_first_time && last_ack_frame < _frame_counter) {
00848     last_ack_frame = _frame_counter + DAY_TICKS;
00849     DEBUG(net, 4, "Sent ACK at %d", _frame_counter);
00850     SendAck();
00851   }
00852 
00853   return NETWORK_RECV_STATUS_OKAY;
00854 }
00855 
00856 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_SYNC)
00857 {
00858   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00859 
00860   _sync_frame = p->Recv_uint32();
00861   _sync_seed_1 = p->Recv_uint32();
00862 #ifdef NETWORK_SEND_DOUBLE_SEED
00863   _sync_seed_2 = p->Recv_uint32();
00864 #endif
00865 
00866   return NETWORK_RECV_STATUS_OKAY;
00867 }
00868 
00869 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_COMMAND)
00870 {
00871   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00872 
00873   CommandPacket cp;
00874   const char *err = this->ReceiveCommand(p, &cp);
00875   cp.frame    = p->Recv_uint32();
00876   cp.my_cmd   = p->Recv_bool();
00877 
00878   if (err != NULL) {
00879     IConsolePrintF(CC_ERROR, "WARNING: %s from server, dropping...", err);
00880     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00881   }
00882 
00883   this->incoming_queue.Append(&cp);
00884 
00885   return NETWORK_RECV_STATUS_OKAY;
00886 }
00887 
00888 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CHAT)
00889 {
00890   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00891 
00892   char name[NETWORK_NAME_LENGTH], msg[NETWORK_CHAT_LENGTH];
00893   const NetworkClientInfo *ci = NULL, *ci_to;
00894 
00895   NetworkAction action = (NetworkAction)p->Recv_uint8();
00896   ClientID client_id = (ClientID)p->Recv_uint32();
00897   bool self_send = p->Recv_bool();
00898   p->Recv_string(msg, NETWORK_CHAT_LENGTH);
00899   int64 data = p->Recv_uint64();
00900 
00901   ci_to = NetworkFindClientInfoFromClientID(client_id);
00902   if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
00903 
00904   /* Did we initiate the action locally? */
00905   if (self_send) {
00906     switch (action) {
00907       case NETWORK_ACTION_CHAT_CLIENT:
00908         /* For speaking to client we need the client-name */
00909         snprintf(name, sizeof(name), "%s", ci_to->client_name);
00910         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
00911         break;
00912 
00913       /* For speaking to company or giving money, we need the company-name */
00914       case NETWORK_ACTION_GIVE_MONEY:
00915         if (!Company::IsValidID(ci_to->client_playas)) return NETWORK_RECV_STATUS_OKAY;
00916         /* FALL THROUGH */
00917       case NETWORK_ACTION_CHAT_COMPANY: {
00918         StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
00919         SetDParam(0, ci_to->client_playas);
00920 
00921         GetString(name, str, lastof(name));
00922         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
00923         break;
00924       }
00925 
00926       default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00927     }
00928   } else {
00929     /* Display message from somebody else */
00930     snprintf(name, sizeof(name), "%s", ci_to->client_name);
00931     ci = ci_to;
00932   }
00933 
00934   if (ci != NULL) {
00935     NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), self_send, name, msg, data);
00936   }
00937   return NETWORK_RECV_STATUS_OKAY;
00938 }
00939 
00940 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_ERROR_QUIT)
00941 {
00942   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00943 
00944   ClientID client_id = (ClientID)p->Recv_uint32();
00945 
00946   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00947   if (ci != NULL) {
00948     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, GetNetworkErrorMsg((NetworkErrorCode)p->Recv_uint8()));
00949     delete ci;
00950   }
00951 
00952   SetWindowDirty(WC_CLIENT_LIST, 0);
00953 
00954   return NETWORK_RECV_STATUS_OKAY;
00955 }
00956 
00957 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_QUIT)
00958 {
00959   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00960 
00961   ClientID client_id = (ClientID)p->Recv_uint32();
00962 
00963   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00964   if (ci != NULL) {
00965     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
00966     delete ci;
00967   } else {
00968     DEBUG(net, 0, "Unknown client (%d) is leaving the game", client_id);
00969   }
00970 
00971   SetWindowDirty(WC_CLIENT_LIST, 0);
00972 
00973   /* If we come here it means we could not locate the client.. strange :s */
00974   return NETWORK_RECV_STATUS_OKAY;
00975 }
00976 
00977 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_JOIN)
00978 {
00979   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00980 
00981   ClientID client_id = (ClientID)p->Recv_uint32();
00982 
00983   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00984   if (ci != NULL) {
00985     NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name);
00986   }
00987 
00988   SetWindowDirty(WC_CLIENT_LIST, 0);
00989 
00990   return NETWORK_RECV_STATUS_OKAY;
00991 }
00992 
00993 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_SHUTDOWN)
00994 {
00995   /* Only when we're trying to join we really
00996    * care about the server shutting down. */
00997   if (this->status >= STATUS_JOIN) {
00998     _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_SHUTDOWN;
00999   }
01000 
01001   return NETWORK_RECV_STATUS_SERVER_ERROR;
01002 }
01003 
01004 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_NEWGAME)
01005 {
01006   /* Only when we're trying to join we really
01007    * care about the server shutting down. */
01008   if (this->status >= STATUS_JOIN) {
01009     /* To trottle the reconnects a bit, every clients waits its
01010      * Client ID modulo 16. This way reconnects should be spread
01011      * out a bit. */
01012     _network_reconnect = _network_own_client_id % 16;
01013     _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_REBOOT;
01014   }
01015 
01016   return NETWORK_RECV_STATUS_SERVER_ERROR;
01017 }
01018 
01019 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_RCON)
01020 {
01021   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01022 
01023   TextColour colour_code = (TextColour)p->Recv_uint16();
01024   if (!IsValidConsoleColour(colour_code)) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01025 
01026   char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
01027   p->Recv_string(rcon_out, sizeof(rcon_out));
01028 
01029   IConsolePrint(colour_code, rcon_out);
01030 
01031   return NETWORK_RECV_STATUS_OKAY;
01032 }
01033 
01034 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MOVE)
01035 {
01036   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01037 
01038   /* Nothing more in this packet... */
01039   ClientID client_id   = (ClientID)p->Recv_uint32();
01040   CompanyID company_id = (CompanyID)p->Recv_uint8();
01041 
01042   if (client_id == 0) {
01043     /* definitely an invalid client id, debug message and do nothing. */
01044     DEBUG(net, 0, "[move] received invalid client index = 0");
01045     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01046   }
01047 
01048   const NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
01049   /* Just make sure we do not try to use a client_index that does not exist */
01050   if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
01051 
01052   /* if not valid player, force spectator, else check player exists */
01053   if (!Company::IsValidID(company_id)) company_id = COMPANY_SPECTATOR;
01054 
01055   if (client_id == _network_own_client_id) {
01056     SetLocalCompany(company_id);
01057   }
01058 
01059   return NETWORK_RECV_STATUS_OKAY;
01060 }
01061 
01062 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CONFIG_UPDATE)
01063 {
01064   if (this->status < STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01065 
01066   _network_server_max_companies = p->Recv_uint8();
01067   _network_server_max_spectators = p->Recv_uint8();
01068 
01069   return NETWORK_RECV_STATUS_OKAY;
01070 }
01071 
01072 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_COMPANY_UPDATE)
01073 {
01074   if (this->status < STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01075 
01076   _network_company_passworded = p->Recv_uint16();
01077   SetWindowClassesDirty(WC_COMPANY);
01078 
01079   return NETWORK_RECV_STATUS_OKAY;
01080 }
01081 
01085 void ClientNetworkGameSocketHandler::CheckConnection()
01086 {
01087   /* Only once we're authorized we can expect a steady stream of packets. */
01088   if (this->status < STATUS_AUTHORIZED) return;
01089 
01090   /* It might... sometimes occur that the realtime ticker overflows. */
01091   if (_realtime_tick < this->last_packet) this->last_packet = _realtime_tick;
01092 
01093   /* Lag is in milliseconds; 5 seconds are roughly twice the
01094    * server's "you're slow" threshold (1 game day). */
01095   uint lag = (_realtime_tick - this->last_packet) / 1000;
01096   if (lag < 5) return;
01097 
01098   /* 20 seconds are (way) more than 4 game days after which
01099    * the server will forcefully disconnect you. */
01100   if (lag > 20) {
01101     this->NetworkGameSocketHandler::CloseConnection();
01102     _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION;
01103     return;
01104   }
01105 
01106   /* Prevent showing the lag message every tick; just update it when needed. */
01107   static uint last_lag = 0;
01108   if (last_lag == lag) return;
01109 
01110   last_lag = lag;
01111   SetDParam(0, lag);
01112   ShowErrorMessage(STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION_CAPTION, STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION, WL_INFO);
01113 }
01114 
01115 
01116 /* Is called after a client is connected to the server */
01117 void NetworkClient_Connected()
01118 {
01119   /* Set the frame-counter to 0 so nothing happens till we are ready */
01120   _frame_counter = 0;
01121   _frame_counter_server = 0;
01122   last_ack_frame = 0;
01123   /* Request the game-info */
01124   MyClient::SendJoin();
01125 }
01126 
01127 void NetworkClientSendRcon(const char *password, const char *command)
01128 {
01129   MyClient::SendRCon(password, command);
01130 }
01131 
01138 void NetworkClientRequestMove(CompanyID company_id, const char *pass)
01139 {
01140   MyClient::SendMove(company_id, pass);
01141 }
01142 
01143 void NetworkClientsToSpectators(CompanyID cid)
01144 {
01145   /* If our company is changing owner, go to spectators */
01146   if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR);
01147 
01148   NetworkClientInfo *ci;
01149   FOR_ALL_CLIENT_INFOS(ci) {
01150     if (ci->client_playas != cid) continue;
01151     NetworkTextMessage(NETWORK_ACTION_COMPANY_SPECTATOR, CC_DEFAULT, false, ci->client_name);
01152     ci->client_playas = COMPANY_SPECTATOR;
01153   }
01154 }
01155 
01156 void NetworkUpdateClientName()
01157 {
01158   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
01159 
01160   if (ci == NULL) return;
01161 
01162   /* Don't change the name if it is the same as the old name */
01163   if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
01164     if (!_network_server) {
01165       MyClient::SendSetName(_settings_client.network.client_name);
01166     } else {
01167       if (NetworkFindName(_settings_client.network.client_name)) {
01168         NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, _settings_client.network.client_name);
01169         strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
01170         NetworkUpdateClientInfo(CLIENT_ID_SERVER);
01171       }
01172     }
01173   }
01174 }
01175 
01176 void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
01177 {
01178   MyClient::SendChat(action, type, dest, msg, data);
01179 }
01180 
01185 void NetworkClientSetCompanyPassword(const char *password)
01186 {
01187   MyClient::SendSetPassword(password);
01188 }
01189 
01195 bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
01196 {
01197   /* Only companies actually playing can speak to team. Eg spectators cannot */
01198   if (!_settings_client.gui.prefer_teamchat || !Company::IsValidID(cio->client_playas)) return false;
01199 
01200   const NetworkClientInfo *ci;
01201   FOR_ALL_CLIENT_INFOS(ci) {
01202     if (ci->client_playas == cio->client_playas && ci != cio) return true;
01203   }
01204 
01205   return false;
01206 }
01207 
01212 bool NetworkMaxCompaniesReached()
01213 {
01214   return Company::GetNumItems() >= (_network_server ? _settings_client.network.max_companies : _network_server_max_companies);
01215 }
01216 
01221 bool NetworkMaxSpectatorsReached()
01222 {
01223   return NetworkSpectatorCount() >= (_network_server ? _settings_client.network.max_spectators : _network_server_max_spectators);
01224 }
01225 
01229 void NetworkPrintClients()
01230 {
01231   NetworkClientInfo *ci;
01232   FOR_ALL_CLIENT_INFOS(ci) {
01233     IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  company: %1d  IP: %s",
01234         ci->client_id,
01235         ci->client_name,
01236         ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
01237         GetClientIP(ci));
01238   }
01239 }
01240 
01241 #endif /* ENABLE_NETWORK */

Generated on Thu Jan 20 22:57:35 2011 for OpenTTD by  doxygen 1.6.1