network_client.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00466 
00467 /***********
00468  * Receiving functions
00469  *   DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
00470  ************/
00471 
00472 extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
00473 extern StringID _switch_mode_errorstr;
00474 
00475 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_FULL)
00476 {
00477   /* We try to join a server which is full */
00478   _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL;
00479   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00480 
00481   return NETWORK_RECV_STATUS_SERVER_FULL;
00482 }
00483 
00484 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_BANNED)
00485 {
00486   /* We try to join a server where we are banned */
00487   _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_BANNED;
00488   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00489 
00490   return NETWORK_RECV_STATUS_SERVER_BANNED;
00491 }
00492 
00493 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_COMPANY_INFO)
00494 {
00495   if (this->status != STATUS_COMPANY_INFO) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00496 
00497   byte company_info_version = p->Recv_uint8();
00498 
00499   if (!this->HasClientQuit() && company_info_version == NETWORK_COMPANY_INFO_VERSION) {
00500     /* We have received all data... (there are no more packets coming) */
00501     if (!p->Recv_bool()) return NETWORK_RECV_STATUS_CLOSE_QUERY;
00502 
00503     CompanyID current = (Owner)p->Recv_uint8();
00504     if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY;
00505 
00506     NetworkCompanyInfo *company_info = GetLobbyCompanyInfo(current);
00507     if (company_info == NULL) return NETWORK_RECV_STATUS_CLOSE_QUERY;
00508 
00509     p->Recv_string(company_info->company_name, sizeof(company_info->company_name));
00510     company_info->inaugurated_year = p->Recv_uint32();
00511     company_info->company_value    = p->Recv_uint64();
00512     company_info->money            = p->Recv_uint64();
00513     company_info->income           = p->Recv_uint64();
00514     company_info->performance      = p->Recv_uint16();
00515     company_info->use_password     = p->Recv_bool();
00516     for (uint i = 0; i < NETWORK_VEH_END; i++) {
00517       company_info->num_vehicle[i] = p->Recv_uint16();
00518     }
00519     for (uint i = 0; i < NETWORK_VEH_END; i++) {
00520       company_info->num_station[i] = p->Recv_uint16();
00521     }
00522     company_info->ai               = p->Recv_bool();
00523 
00524     p->Recv_string(company_info->clients, sizeof(company_info->clients));
00525 
00526     SetWindowDirty(WC_NETWORK_WINDOW, 0);
00527 
00528     return NETWORK_RECV_STATUS_OKAY;
00529   }
00530 
00531   return NETWORK_RECV_STATUS_CLOSE_QUERY;
00532 }
00533 
00534 /* This packet contains info about the client (playas and name)
00535  *  as client we save this in NetworkClientInfo, linked via 'client_id'
00536  *  which is always an unique number on a server. */
00537 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CLIENT_INFO)
00538 {
00539   NetworkClientInfo *ci;
00540   ClientID client_id = (ClientID)p->Recv_uint32();
00541   CompanyID playas = (CompanyID)p->Recv_uint8();
00542   char name[NETWORK_NAME_LENGTH];
00543 
00544   p->Recv_string(name, sizeof(name));
00545 
00546   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00547   if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
00548 
00549   ci = NetworkFindClientInfoFromClientID(client_id);
00550   if (ci != NULL) {
00551     if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
00552       /* Client name changed, display the change */
00553       NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
00554     } else if (playas != ci->client_playas) {
00555       /* The client changed from client-player..
00556        * Do not display that for now */
00557     }
00558 
00559     /* Make sure we're in the company the server tells us to be in,
00560      * for the rare case that we get moved while joining. */
00561     if (client_id == _network_own_client_id) SetLocalCompany(!Company::IsValidID(playas) ? COMPANY_SPECTATOR : playas);
00562 
00563     ci->client_playas = playas;
00564     strecpy(ci->client_name, name, lastof(ci->client_name));
00565 
00566     SetWindowDirty(WC_CLIENT_LIST, 0);
00567 
00568     return NETWORK_RECV_STATUS_OKAY;
00569   }
00570 
00571   /* We don't have this client_id yet, find an empty client_id, and put the data there */
00572   ci = new NetworkClientInfo(client_id);
00573   ci->client_playas = playas;
00574   if (client_id == _network_own_client_id) this->SetInfo(ci);
00575 
00576   strecpy(ci->client_name, name, lastof(ci->client_name));
00577 
00578   SetWindowDirty(WC_CLIENT_LIST, 0);
00579 
00580   return NETWORK_RECV_STATUS_OKAY;
00581 }
00582 
00583 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_ERROR)
00584 {
00585   NetworkErrorCode error = (NetworkErrorCode)p->Recv_uint8();
00586 
00587   switch (error) {
00588     /* We made an error in the protocol, and our connection is closed.... */
00589     case NETWORK_ERROR_NOT_AUTHORIZED:
00590     case NETWORK_ERROR_NOT_EXPECTED:
00591     case NETWORK_ERROR_COMPANY_MISMATCH:
00592       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_ERROR;
00593       break;
00594     case NETWORK_ERROR_FULL:
00595       _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL;
00596       break;
00597     case NETWORK_ERROR_WRONG_REVISION:
00598       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_REVISION;
00599       break;
00600     case NETWORK_ERROR_WRONG_PASSWORD:
00601       _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_PASSWORD;
00602       break;
00603     case NETWORK_ERROR_KICKED:
00604       _switch_mode_errorstr = STR_NETWORK_ERROR_KICKED;
00605       break;
00606     case NETWORK_ERROR_CHEATER:
00607       _switch_mode_errorstr = STR_NETWORK_ERROR_CHEATER;
00608       break;
00609     case NETWORK_ERROR_TOO_MANY_COMMANDS:
00610       _switch_mode_errorstr = STR_NETWORK_ERROR_TOO_MANY_COMMANDS;
00611       break;
00612     default:
00613       _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION;
00614   }
00615 
00616   DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00617 
00618   return NETWORK_RECV_STATUS_SERVER_ERROR;
00619 }
00620 
00621 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CHECK_NEWGRFS)
00622 {
00623   if (this->status != STATUS_JOIN) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00624 
00625   uint grf_count = p->Recv_uint8();
00626   NetworkRecvStatus ret = NETWORK_RECV_STATUS_OKAY;
00627 
00628   /* Check all GRFs */
00629   for (; grf_count > 0; grf_count--) {
00630     GRFIdentifier c;
00631     this->ReceiveGRFIdentifier(p, &c);
00632 
00633     /* Check whether we know this GRF */
00634     const GRFConfig *f = FindGRFConfig(c.grfid, FGCM_EXACT, c.md5sum);
00635     if (f == NULL) {
00636       /* We do not know this GRF, bail out of initialization */
00637       char buf[sizeof(c.md5sum) * 2 + 1];
00638       md5sumToString(buf, lastof(buf), c.md5sum);
00639       DEBUG(grf, 0, "NewGRF %08X not found; checksum %s", BSWAP32(c.grfid), buf);
00640       ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH;
00641     }
00642   }
00643 
00644   if (ret == NETWORK_RECV_STATUS_OKAY) {
00645     /* Start receiving the map */
00646     return SendNewGRFsOk();
00647   }
00648 
00649   /* NewGRF mismatch, bail out */
00650   _switch_mode_errorstr = STR_NETWORK_ERROR_NEWGRF_MISMATCH;
00651   return ret;
00652 }
00653 
00654 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_NEED_GAME_PASSWORD)
00655 {
00656   if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_GAME) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00657   this->status = STATUS_AUTH_GAME;
00658 
00659   const char *password = _network_join_server_password;
00660   if (!StrEmpty(password)) {
00661     return SendGamePassword(password);
00662   }
00663 
00664   ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD);
00665 
00666   return NETWORK_RECV_STATUS_OKAY;
00667 }
00668 
00669 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_NEED_COMPANY_PASSWORD)
00670 {
00671   if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_COMPANY) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00672   this->status = STATUS_AUTH_COMPANY;
00673 
00674   _password_game_seed = p->Recv_uint32();
00675   p->Recv_string(_password_server_id, sizeof(_password_server_id));
00676   if (this->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00677 
00678   const char *password = _network_join_company_password;
00679   if (!StrEmpty(password)) {
00680     return SendCompanyPassword(password);
00681   }
00682 
00683   ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD);
00684 
00685   return NETWORK_RECV_STATUS_OKAY;
00686 }
00687 
00688 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_WELCOME)
00689 {
00690   if (this->status < STATUS_JOIN || this->status >= STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00691   this->status = STATUS_AUTHORIZED;
00692 
00693   _network_own_client_id = (ClientID)p->Recv_uint32();
00694 
00695   /* Initialize the password hash salting variables, even if they were previously. */
00696   _password_game_seed = p->Recv_uint32();
00697   p->Recv_string(_password_server_id, sizeof(_password_server_id));
00698 
00699   /* Start receiving the map */
00700   return SendGetMap();
00701 }
00702 
00703 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_WAIT)
00704 {
00705   if (this->status != STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00706   this->status = STATUS_MAP_WAIT;
00707 
00708   _network_join_status = NETWORK_JOIN_STATUS_WAITING;
00709   _network_join_waiting = p->Recv_uint8();
00710   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00711 
00712   /* We are put on hold for receiving the map.. we need GUI for this ;) */
00713   DEBUG(net, 1, "The server is currently busy sending the map to someone else, please wait..." );
00714   DEBUG(net, 1, "There are %d clients in front of you", _network_join_waiting);
00715 
00716   return NETWORK_RECV_STATUS_OKAY;
00717 }
00718 
00719 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_BEGIN)
00720 {
00721   if (this->status < STATUS_AUTHORIZED || this->status >= STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00722   this->status = STATUS_MAP;
00723 
00724   if (this->savegame != NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00725 
00726   this->savegame = new PacketReader();
00727 
00728   _frame_counter = _frame_counter_server = _frame_counter_max = p->Recv_uint32();
00729 
00730   _network_join_bytes = 0;
00731   _network_join_bytes_total = 0;
00732 
00733   _network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
00734   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00735 
00736   return NETWORK_RECV_STATUS_OKAY;
00737 }
00738 
00739 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_SIZE)
00740 {
00741   if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00742   if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00743 
00744   _network_join_bytes_total = p->Recv_uint32();
00745   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00746 
00747   return NETWORK_RECV_STATUS_OKAY;
00748 }
00749 
00750 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_DATA)
00751 {
00752   if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00753   if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00754 
00755   /* We are still receiving data, put it to the file */
00756   this->savegame->AddPacket(p);
00757 
00758   _network_join_bytes = (uint32)this->savegame->written_bytes;
00759   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00760 
00761   return NETWORK_RECV_STATUS_OKAY;
00762 }
00763 
00764 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MAP_DONE)
00765 {
00766   if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00767   if (this->savegame == NULL) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00768 
00769   _network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
00770   SetWindowDirty(WC_NETWORK_STATUS_WINDOW, 0);
00771 
00772   /*
00773    * Make sure everything is set for reading.
00774    *
00775    * We need the local copy and reset this->savegame because when
00776    * loading fails the network gets reset upon loading the intro
00777    * game, which would cause us to free this->savegame twice.
00778    */
00779   LoadFilter *lf = this->savegame;
00780   this->savegame = NULL;
00781   lf->Reset();
00782 
00783   /* The map is done downloading, load it */
00784   bool load_success = SafeLoad(NULL, SL_LOAD, GM_NORMAL, NO_DIRECTORY, lf);
00785 
00786   /* Long savegame loads shouldn't affect the lag calculation! */
00787   this->last_packet = _realtime_tick;
00788 
00789   if (!load_success) {
00790     DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
00791     _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR;
00792     return NETWORK_RECV_STATUS_SAVEGAME;
00793   }
00794   /* If the savegame has successfully loaded, ALL windows have been removed,
00795    * only toolbar/statusbar and gamefield are visible */
00796 
00797   /* Say we received the map and loaded it correctly! */
00798   SendMapOk();
00799 
00800   /* New company/spectator (invalid company) or company we want to join is not active
00801    * Switch local company to spectator and await the server's judgement */
00802   if (_network_join_as == COMPANY_NEW_COMPANY || !Company::IsValidID(_network_join_as)) {
00803     SetLocalCompany(COMPANY_SPECTATOR);
00804 
00805     if (_network_join_as != COMPANY_SPECTATOR) {
00806       /* We have arrived and ready to start playing; send a command to make a new company;
00807        * the server will give us a client-id and let us in */
00808       _network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
00809       ShowJoinStatusWindow();
00810       NetworkSendCommand(0, 0, 0, CMD_COMPANY_CTRL, NULL, NULL, _local_company);
00811     }
00812   } else {
00813     /* take control over an existing company */
00814     SetLocalCompany(_network_join_as);
00815   }
00816 
00817   return NETWORK_RECV_STATUS_OKAY;
00818 }
00819 
00820 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_FRAME)
00821 {
00822   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00823 
00824   _frame_counter_server = p->Recv_uint32();
00825   _frame_counter_max = p->Recv_uint32();
00826 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
00827   /* Test if the server supports this option
00828    *  and if we are at the frame the server is */
00829   if (p->pos + 1 < p->size) {
00830     _sync_frame = _frame_counter_server;
00831     _sync_seed_1 = p->Recv_uint32();
00832 #ifdef NETWORK_SEND_DOUBLE_SEED
00833     _sync_seed_2 = p->Recv_uint32();
00834 #endif
00835   }
00836 #endif
00837   /* Receive the token. */
00838   if (p->pos != p->size) this->token = p->Recv_uint8();
00839 
00840   DEBUG(net, 5, "Received FRAME %d", _frame_counter_server);
00841 
00842   /* Let the server know that we received this frame correctly
00843    *  We do this only once per day, to save some bandwidth ;) */
00844   if (!_network_first_time && last_ack_frame < _frame_counter) {
00845     last_ack_frame = _frame_counter + DAY_TICKS;
00846     DEBUG(net, 4, "Sent ACK at %d", _frame_counter);
00847     SendAck();
00848   }
00849 
00850   return NETWORK_RECV_STATUS_OKAY;
00851 }
00852 
00853 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_SYNC)
00854 {
00855   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00856 
00857   _sync_frame = p->Recv_uint32();
00858   _sync_seed_1 = p->Recv_uint32();
00859 #ifdef NETWORK_SEND_DOUBLE_SEED
00860   _sync_seed_2 = p->Recv_uint32();
00861 #endif
00862 
00863   return NETWORK_RECV_STATUS_OKAY;
00864 }
00865 
00866 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_COMMAND)
00867 {
00868   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00869 
00870   CommandPacket cp;
00871   const char *err = this->ReceiveCommand(p, &cp);
00872   cp.frame    = p->Recv_uint32();
00873   cp.my_cmd   = p->Recv_bool();
00874 
00875   if (err != NULL) {
00876     IConsolePrintF(CC_ERROR, "WARNING: %s from server, dropping...", err);
00877     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00878   }
00879 
00880   this->incoming_queue.Append(&cp);
00881 
00882   return NETWORK_RECV_STATUS_OKAY;
00883 }
00884 
00885 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CHAT)
00886 {
00887   if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00888 
00889   char name[NETWORK_NAME_LENGTH], msg[NETWORK_CHAT_LENGTH];
00890   const NetworkClientInfo *ci = NULL, *ci_to;
00891 
00892   NetworkAction action = (NetworkAction)p->Recv_uint8();
00893   ClientID client_id = (ClientID)p->Recv_uint32();
00894   bool self_send = p->Recv_bool();
00895   p->Recv_string(msg, NETWORK_CHAT_LENGTH);
00896   int64 data = p->Recv_uint64();
00897 
00898   ci_to = NetworkFindClientInfoFromClientID(client_id);
00899   if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
00900 
00901   /* Did we initiate the action locally? */
00902   if (self_send) {
00903     switch (action) {
00904       case NETWORK_ACTION_CHAT_CLIENT:
00905         /* For speaking to client we need the client-name */
00906         snprintf(name, sizeof(name), "%s", ci_to->client_name);
00907         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
00908         break;
00909 
00910       /* For speaking to company or giving money, we need the company-name */
00911       case NETWORK_ACTION_GIVE_MONEY:
00912         if (!Company::IsValidID(ci_to->client_playas)) return NETWORK_RECV_STATUS_OKAY;
00913         /* FALL THROUGH */
00914       case NETWORK_ACTION_CHAT_COMPANY: {
00915         StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
00916         SetDParam(0, ci_to->client_playas);
00917 
00918         GetString(name, str, lastof(name));
00919         ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
00920         break;
00921       }
00922 
00923       default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00924     }
00925   } else {
00926     /* Display message from somebody else */
00927     snprintf(name, sizeof(name), "%s", ci_to->client_name);
00928     ci = ci_to;
00929   }
00930 
00931   if (ci != NULL) {
00932     NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), self_send, name, msg, data);
00933   }
00934   return NETWORK_RECV_STATUS_OKAY;
00935 }
00936 
00937 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_ERROR_QUIT)
00938 {
00939   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00940 
00941   ClientID client_id = (ClientID)p->Recv_uint32();
00942 
00943   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00944   if (ci != NULL) {
00945     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, GetNetworkErrorMsg((NetworkErrorCode)p->Recv_uint8()));
00946     delete ci;
00947   }
00948 
00949   SetWindowDirty(WC_CLIENT_LIST, 0);
00950 
00951   return NETWORK_RECV_STATUS_OKAY;
00952 }
00953 
00954 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_QUIT)
00955 {
00956   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00957 
00958   ClientID client_id = (ClientID)p->Recv_uint32();
00959 
00960   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00961   if (ci != NULL) {
00962     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
00963     delete ci;
00964   } else {
00965     DEBUG(net, 0, "Unknown client (%d) is leaving the game", client_id);
00966   }
00967 
00968   SetWindowDirty(WC_CLIENT_LIST, 0);
00969 
00970   /* If we come here it means we could not locate the client.. strange :s */
00971   return NETWORK_RECV_STATUS_OKAY;
00972 }
00973 
00974 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_JOIN)
00975 {
00976   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
00977 
00978   ClientID client_id = (ClientID)p->Recv_uint32();
00979 
00980   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00981   if (ci != NULL) {
00982     NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name);
00983   }
00984 
00985   SetWindowDirty(WC_CLIENT_LIST, 0);
00986 
00987   return NETWORK_RECV_STATUS_OKAY;
00988 }
00989 
00990 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_SHUTDOWN)
00991 {
00992   /* Only when we're trying to join we really
00993    * care about the server shutting down. */
00994   if (this->status >= STATUS_JOIN) {
00995     _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_SHUTDOWN;
00996   }
00997 
00998   return NETWORK_RECV_STATUS_SERVER_ERROR;
00999 }
01000 
01001 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_NEWGAME)
01002 {
01003   /* Only when we're trying to join we really
01004    * care about the server shutting down. */
01005   if (this->status >= STATUS_JOIN) {
01006     /* To trottle the reconnects a bit, every clients waits its
01007      * Client ID modulo 16. This way reconnects should be spread
01008      * out a bit. */
01009     _network_reconnect = _network_own_client_id % 16;
01010     _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_REBOOT;
01011   }
01012 
01013   return NETWORK_RECV_STATUS_SERVER_ERROR;
01014 }
01015 
01016 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_RCON)
01017 {
01018   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01019 
01020   TextColour colour_code = (TextColour)p->Recv_uint16();
01021   if (!IsValidConsoleColour(colour_code)) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01022 
01023   char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
01024   p->Recv_string(rcon_out, sizeof(rcon_out));
01025 
01026   IConsolePrint(colour_code, rcon_out);
01027 
01028   return NETWORK_RECV_STATUS_OKAY;
01029 }
01030 
01031 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_MOVE)
01032 {
01033   if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01034 
01035   /* Nothing more in this packet... */
01036   ClientID client_id   = (ClientID)p->Recv_uint32();
01037   CompanyID company_id = (CompanyID)p->Recv_uint8();
01038 
01039   if (client_id == 0) {
01040     /* definitely an invalid client id, debug message and do nothing. */
01041     DEBUG(net, 0, "[move] received invalid client index = 0");
01042     return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01043   }
01044 
01045   const NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
01046   /* Just make sure we do not try to use a client_index that does not exist */
01047   if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
01048 
01049   /* if not valid player, force spectator, else check player exists */
01050   if (!Company::IsValidID(company_id)) company_id = COMPANY_SPECTATOR;
01051 
01052   if (client_id == _network_own_client_id) {
01053     SetLocalCompany(company_id);
01054   }
01055 
01056   return NETWORK_RECV_STATUS_OKAY;
01057 }
01058 
01059 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_CONFIG_UPDATE)
01060 {
01061   if (this->status < STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01062 
01063   _network_server_max_companies = p->Recv_uint8();
01064   _network_server_max_spectators = p->Recv_uint8();
01065 
01066   return NETWORK_RECV_STATUS_OKAY;
01067 }
01068 
01069 DEF_GAME_RECEIVE_COMMAND(Client, PACKET_SERVER_COMPANY_UPDATE)
01070 {
01071   if (this->status < STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
01072 
01073   _network_company_passworded = p->Recv_uint16();
01074   SetWindowClassesDirty(WC_COMPANY);
01075 
01076   return NETWORK_RECV_STATUS_OKAY;
01077 }
01078 
01082 void ClientNetworkGameSocketHandler::CheckConnection()
01083 {
01084   /* Only once we're authorized we can expect a steady stream of packets. */
01085   if (this->status < STATUS_AUTHORIZED) return;
01086 
01087   /* It might... sometimes occur that the realtime ticker overflows. */
01088   if (_realtime_tick < this->last_packet) this->last_packet = _realtime_tick;
01089 
01090   /* Lag is in milliseconds; 5 seconds are roughly twice the
01091    * server's "you're slow" threshold (1 game day). */
01092   uint lag = (_realtime_tick - this->last_packet) / 1000;
01093   if (lag < 5) return;
01094 
01095   /* 20 seconds are (way) more than 4 game days after which
01096    * the server will forcefully disconnect you. */
01097   if (lag > 20) {
01098     this->NetworkGameSocketHandler::CloseConnection();
01099     _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION;
01100     return;
01101   }
01102 
01103   /* Prevent showing the lag message every tick; just update it when needed. */
01104   static uint last_lag = 0;
01105   if (last_lag == lag) return;
01106 
01107   last_lag = lag;
01108   SetDParam(0, lag);
01109   ShowErrorMessage(STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION_CAPTION, STR_NETWORK_ERROR_CLIENT_GUI_LOST_CONNECTION, WL_INFO);
01110 }
01111 
01112 
01113 /* Is called after a client is connected to the server */
01114 void NetworkClient_Connected()
01115 {
01116   /* Set the frame-counter to 0 so nothing happens till we are ready */
01117   _frame_counter = 0;
01118   _frame_counter_server = 0;
01119   last_ack_frame = 0;
01120   /* Request the game-info */
01121   MyClient::SendJoin();
01122 }
01123 
01124 void NetworkClientSendRcon(const char *password, const char *command)
01125 {
01126   MyClient::SendRCon(password, command);
01127 }
01128 
01135 void NetworkClientRequestMove(CompanyID company_id, const char *pass)
01136 {
01137   MyClient::SendMove(company_id, pass);
01138 }
01139 
01140 void NetworkClientsToSpectators(CompanyID cid)
01141 {
01142   /* If our company is changing owner, go to spectators */
01143   if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR);
01144 
01145   NetworkClientInfo *ci;
01146   FOR_ALL_CLIENT_INFOS(ci) {
01147     if (ci->client_playas != cid) continue;
01148     NetworkTextMessage(NETWORK_ACTION_COMPANY_SPECTATOR, CC_DEFAULT, false, ci->client_name);
01149     ci->client_playas = COMPANY_SPECTATOR;
01150   }
01151 }
01152 
01153 void NetworkUpdateClientName()
01154 {
01155   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(_network_own_client_id);
01156 
01157   if (ci == NULL) return;
01158 
01159   /* Don't change the name if it is the same as the old name */
01160   if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
01161     if (!_network_server) {
01162       MyClient::SendSetName(_settings_client.network.client_name);
01163     } else {
01164       if (NetworkFindName(_settings_client.network.client_name)) {
01165         NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, _settings_client.network.client_name);
01166         strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
01167         NetworkUpdateClientInfo(CLIENT_ID_SERVER);
01168       }
01169     }
01170   }
01171 }
01172 
01173 void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
01174 {
01175   MyClient::SendChat(action, type, dest, msg, data);
01176 }
01177 
01182 void NetworkClientSetCompanyPassword(const char *password)
01183 {
01184   MyClient::SendSetPassword(password);
01185 }
01186 
01192 bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
01193 {
01194   /* Only companies actually playing can speak to team. Eg spectators cannot */
01195   if (!_settings_client.gui.prefer_teamchat || !Company::IsValidID(cio->client_playas)) return false;
01196 
01197   const NetworkClientInfo *ci;
01198   FOR_ALL_CLIENT_INFOS(ci) {
01199     if (ci->client_playas == cio->client_playas && ci != cio) return true;
01200   }
01201 
01202   return false;
01203 }
01204 
01209 bool NetworkMaxCompaniesReached()
01210 {
01211   return Company::GetNumItems() >= (_network_server ? _settings_client.network.max_companies : _network_server_max_companies);
01212 }
01213 
01218 bool NetworkMaxSpectatorsReached()
01219 {
01220   return NetworkSpectatorCount() >= (_network_server ? _settings_client.network.max_spectators : _network_server_max_spectators);
01221 }
01222 
01226 void NetworkPrintClients()
01227 {
01228   NetworkClientInfo *ci;
01229   FOR_ALL_CLIENT_INFOS(ci) {
01230     IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  company: %1d  IP: %s",
01231         ci->client_id,
01232         ci->client_name,
01233         ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
01234         GetClientIP(ci));
01235   }
01236 }
01237 
01238 #endif /* ENABLE_NETWORK */

Generated on Sun Jan 23 01:49:04 2011 for OpenTTD by  doxygen 1.6.1