network_client.cpp

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

Generated on Fri Feb 18 21:53:41 2011 for OpenTTD by  doxygen 1.6.1