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

Generated on Thu Apr 14 00:48:15 2011 for OpenTTD by  doxygen 1.6.1