# network_server.cpp

Go to the documentation of this file.

    00001 /* $Id: network_server.cpp 18875 2010-01-21 11:17:40Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #ifdef ENABLE_NETWORK
    00013
    00014 #include "../stdafx.h"
    00015 #include "../debug.h"
    00016 #include "../strings_func.h"
    00017 #include "../date_func.h"
    00018 #include "network_server.h"
    00019 #include "network_udp.h"
    00020 #include "network.h"
    00021 #include "network_base.h"
    00022 #include "../console_func.h"
    00023 #include "../company_base.h"
    00024 #include "../command_func.h"
    00025 #include "../saveload/saveload.h"
    00026 #include "../station_base.h"
    00027 #include "../genworld.h"
    00028 #include "../fileio_func.h"
    00029 #include "../company_func.h"
    00030 #include "../company_gui.h"
    00031 #include "../window_func.h"
    00032 #include "../roadveh.h"
    00033 #include "../rev.h"
    00034
    00035 #include "table/strings.h"
    00036
    00037 /* This file handles all the server-commands */
    00038
    00039 static void NetworkHandleCommandQueue(NetworkClientSocket *cs);
    00040
    00041 /***********
    00042  * Sending functions
    00043  *   DEF_SERVER_SEND_COMMAND has parameter: NetworkClientSocket *cs
    00044  ************/
    00045
    00046 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkClientSocket *cs, NetworkClientInfo *ci)
    00047 {
    00048   /*
    00049    * Packet: SERVER_CLIENT_INFO
    00050    * Function: Sends info about a client
    00051    * Data:
    00052    *    uint32:  The identifier of the client (always unique on a server. 1 = server, 0 is invalid)
    00053    *    uint8:  As which company the client is playing
    00054    *    String: The name of the client
    00055    */
    00056
    00057   if (ci->client_id != INVALID_CLIENT_ID) {
    00058     Packet *p = new Packet(PACKET_SERVER_CLIENT_INFO);
    00059     p->Send_uint32(ci->client_id);
    00060     p->Send_uint8 (ci->client_playas);
    00061     p->Send_string(ci->client_name);
    00062
    00063     cs->Send_Packet(p);
    00064   }
    00065 }
    00066
    00067 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)
    00068 {
    00069   /*
    00070    * Packet: SERVER_COMPANY_INFO
    00071    * Function: Sends info about the companies
    00072    * Data:
    00073    */
    00074
    00075   /* Fetch the latest version of the stats */
    00076   NetworkCompanyStats company_stats[MAX_COMPANIES];
    00077   NetworkPopulateCompanyStats(company_stats);
    00078
    00079   /* Make a list of all clients per company */
    00080   char clients[MAX_COMPANIES][NETWORK_CLIENTS_LENGTH];
    00081   NetworkClientSocket *csi;
    00082   memset(clients, 0, sizeof(clients));
    00083
    00084   /* Add the local player (if not dedicated) */
    00085   const NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER);
    00086   if (ci != NULL && Company::IsValidID(ci->client_playas)) {
    00087     strecpy(clients[ci->client_playas], ci->client_name, lastof(clients[ci->client_playas]));
    00088   }
    00089
    00090   FOR_ALL_CLIENT_SOCKETS(csi) {
    00091     char client_name[NETWORK_CLIENT_NAME_LENGTH];
    00092
    00093     NetworkGetClientName(client_name, sizeof(client_name), csi);
    00094
    00095     ci = csi->GetInfo();
    00096     if (ci != NULL && Company::IsValidID(ci->client_playas)) {
    00097       if (!StrEmpty(clients[ci->client_playas])) {
    00098         strecat(clients[ci->client_playas], ", ", lastof(clients[ci->client_playas]));
    00099       }
    00100
    00101       strecat(clients[ci->client_playas], client_name, lastof(clients[ci->client_playas]));
    00102     }
    00103   }
    00104
    00105   /* Now send the data */
    00106
    00107   Company *company;
    00108   Packet *p;
    00109
    00110   FOR_ALL_COMPANIES(company) {
    00111     p = new Packet(PACKET_SERVER_COMPANY_INFO);
    00112
    00113     p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
    00114     p->Send_bool  (true);
    00115     cs->Send_CompanyInformation(p, company, &company_stats[company->index]);
    00116
    00117     if (StrEmpty(clients[company->index])) {
    00118       p->Send_string("<none>");
    00119     } else {
    00120       p->Send_string(clients[company->index]);
    00121     }
    00122
    00123     cs->Send_Packet(p);
    00124   }
    00125
    00126   p = new Packet(PACKET_SERVER_COMPANY_INFO);
    00127
    00128   p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
    00129   p->Send_bool  (false);
    00130
    00131   cs->Send_Packet(p);
    00132 }
    00133
    00134 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientSocket *cs, NetworkErrorCode error)
    00135 {
    00136   /*
    00137    * Packet: SERVER_ERROR
    00138    * Function: The client made an error
    00139    * Data:
    00140    *    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
    00141    */
    00142
    00143   char str[100];
    00144   Packet *p = new Packet(PACKET_SERVER_ERROR);
    00145
    00146   p->Send_uint8(error);
    00147   cs->Send_Packet(p);
    00148
    00149   StringID strid = GetNetworkErrorMsg(error);
    00150   GetString(str, strid, lastof(str));
    00151
    00152   /* Only send when the current client was in game */
    00153   if (cs->status > STATUS_AUTH) {
    00154     NetworkClientSocket *new_cs;
    00155     char client_name[NETWORK_CLIENT_NAME_LENGTH];
    00156
    00157     NetworkGetClientName(client_name, sizeof(client_name), cs);
    00158
    00159     DEBUG(net, 1, "'%s' made an error and has been disconnected. Reason: '%s'", client_name, str);
    00160
    00161     NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, strid);
    00162
    00163     FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00164       if (new_cs->status > STATUS_AUTH && new_cs != cs) {
    00165         /* Some errors we filter to a more general error. Clients don't have to know the real
    00166          *  reason a joining failed. */
    00167         if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION)
    00168           error = NETWORK_ERROR_ILLEGAL_PACKET;
    00169
    00170         SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->client_id, error);
    00171       }
    00172     }
    00173   } else {
    00174     DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", cs->client_id, str);
    00175   }
    00176
    00177   cs->CloseConnection(false);
    00178
    00179   /* Make sure the data get's there before we close the connection */
    00180   cs->Send_Packets();
    00181
    00182   /* The client made a mistake, so drop his connection now! */
    00183   NetworkCloseClient(cs, false);
    00184 }
    00185
    00186 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHECK_NEWGRFS)(NetworkClientSocket *cs)
    00187 {
    00188   /*
    00189    * Packet: PACKET_SERVER_CHECK_NEWGRFS
    00190    * Function: Sends info about the used GRFs to the client
    00191    * Data:
    00192    *      uint8:  Amount of GRFs
    00193    *    And then for each GRF:
    00194    *      uint32: GRF ID
    00195    * 16 * uint8:  MD5 checksum of the GRF
    00196    */
    00197
    00198   Packet *p = new Packet(PACKET_SERVER_CHECK_NEWGRFS);
    00199   const GRFConfig *c;
    00200   uint grf_count = 0;
    00201
    00202   for (c = _grfconfig; c != NULL; c = c->next) {
    00203     if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
    00204   }
    00205
    00206   p->Send_uint8 (grf_count);
    00207   for (c = _grfconfig; c != NULL; c = c->next) {
    00208     if (!HasBit(c->flags, GCF_STATIC)) cs->Send_GRFIdentifier(p, c);
    00209   }
    00210
    00211   cs->Send_Packet(p);
    00212 }
    00213
    00214 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(NetworkClientSocket *cs, NetworkPasswordType type)
    00215 {
    00216   /*
    00217    * Packet: SERVER_NEED_PASSWORD
    00218    * Function: Indication to the client that the server needs a password
    00219    * Data:
    00220    *    uint8:  Type of password
    00221    */
    00222
    00223   /* Invalid packet when status is AUTH or higher */
    00224   if (cs->status >= STATUS_AUTH) return;
    00225
    00226   cs->status = STATUS_AUTHORIZING;
    00227
    00228   Packet *p = new Packet(PACKET_SERVER_NEED_PASSWORD);
    00229   p->Send_uint8(type);
    00230   p->Send_uint32(_settings_game.game_creation.generation_seed);
    00231   p->Send_string(_settings_client.network.network_id);
    00232   cs->Send_Packet(p);
    00233 }
    00234
    00235 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME)
    00236 {
    00237   /*
    00238    * Packet: SERVER_WELCOME
    00239    * Function: The client is joined and ready to receive his map
    00240    * Data:
    00241    *    uint32:  Own Client identifier
    00242    */
    00243
    00244   Packet *p;
    00245   NetworkClientSocket *new_cs;
    00246
    00247   /* Invalid packet when status is AUTH or higher */
    00248   if (cs->status >= STATUS_AUTH) return;
    00249
    00250   cs->status = STATUS_AUTH;
    00251   _network_game_info.clients_on++;
    00252
    00253   p = new Packet(PACKET_SERVER_WELCOME);
    00254   p->Send_uint32(cs->client_id);
    00255   p->Send_uint32(_settings_game.game_creation.generation_seed);
    00256   p->Send_string(_settings_client.network.network_id);
    00257   cs->Send_Packet(p);
    00258
    00259     /* Transmit info about all the active clients */
    00260   FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00261     if (new_cs != cs && new_cs->status > STATUS_AUTH)
    00262       SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, new_cs->GetInfo());
    00263   }
    00264   /* Also send the info of the server */
    00265   SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER));
    00266 }
    00267
    00268 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WAIT)
    00269 {
    00270   /*
    00271    * Packet: PACKET_SERVER_WAIT
    00272    * Function: The client can not receive the map at the moment because
    00273    *             someone else is already receiving the map
    00274    * Data:
    00275    *    uint8:  Clients awaiting map
    00276    */
    00277   int waiting = 0;
    00278   NetworkClientSocket *new_cs;
    00279   Packet *p;
    00280
    00281   /* Count how many clients are waiting in the queue */
    00282   FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00283     if (new_cs->status == STATUS_MAP_WAIT) waiting++;
    00284   }
    00285
    00286   p = new Packet(PACKET_SERVER_WAIT);
    00287   p->Send_uint8(waiting);
    00288   cs->Send_Packet(p);
    00289 }
    00290
    00291 /* This sends the map to the client */
    00292 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP)
    00293 {
    00294   /*
    00295    * Packet: SERVER_MAP
    00296    * Function: Sends the map to the client, or a part of it (it is splitted in
    00297    *   a lot of multiple packets)
    00298    * Data:
    00299    *    uint8:  packet-type (MAP_PACKET_START, MAP_PACKET_NORMAL and MAP_PACKET_END)
    00300    *  if MAP_PACKET_START:
    00301    *    uint32: The current FrameCounter
    00302    *  if MAP_PACKET_NORMAL:
    00303    *    piece of the map (till max-size of packet)
    00304    *  if MAP_PACKET_END:
    00305    *    nothing
    00306    */
    00307
    00308   static FILE *file_pointer;
    00309   static uint sent_packets; // How many packets we did send succecfully last time
    00310
    00311   if (cs->status < STATUS_AUTH) {
    00312     /* Illegal call, return error and ignore the packet */
    00313     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
    00314     return;
    00315   }
    00316
    00317   if (cs->status == STATUS_AUTH) {
    00318     const char *filename = "network_server.tmp";
    00319     Packet *p;
    00320
    00321     /* Make a dump of the current game */
    00322     if (SaveOrLoad(filename, SL_SAVE, AUTOSAVE_DIR) != SL_OK) usererror("network savedump failed");
    00323
    00324     file_pointer = FioFOpenFile(filename, "rb", AUTOSAVE_DIR);
    00325     fseek(file_pointer, 0, SEEK_END);
    00326
    00327     if (ftell(file_pointer) == 0) usererror("network savedump failed - zero sized savegame?");
    00328
    00329     /* Now send the _frame_counter and how many packets are coming */
    00330     p = new Packet(PACKET_SERVER_MAP);
    00331     p->Send_uint8 (MAP_PACKET_START);
    00332     p->Send_uint32(_frame_counter);
    00333     p->Send_uint32(ftell(file_pointer));
    00334     cs->Send_Packet(p);
    00335
    00336     fseek(file_pointer, 0, SEEK_SET);
    00337
    00338     sent_packets = 4; // We start with trying 4 packets
    00339
    00340     cs->status = STATUS_MAP;
    00341     /* Mark the start of download */
    00342     cs->last_frame = _frame_counter;
    00343     cs->last_frame_server = _frame_counter;
    00344   }
    00345
    00346   if (cs->status == STATUS_MAP) {
    00347     uint i;
    00348     int res;
    00349     for (i = 0; i < sent_packets; i++) {
    00350       Packet *p = new Packet(PACKET_SERVER_MAP);
    00351       p->Send_uint8(MAP_PACKET_NORMAL);
    00352       res = (int)fread(p->buffer + p->size, 1, SEND_MTU - p->size, file_pointer);
    00353
    00354       if (ferror(file_pointer)) usererror("Error reading temporary network savegame!");
    00355
    00356       p->size += res;
    00357       cs->Send_Packet(p);
    00358       if (feof(file_pointer)) {
    00359         /* Done reading! */
    00360         Packet *p = new Packet(PACKET_SERVER_MAP);
    00361         p->Send_uint8(MAP_PACKET_END);
    00362         cs->Send_Packet(p);
    00363
    00364         /* Set the status to DONE_MAP, no we will wait for the client
    00365          *  to send it is ready (maybe that happens like never ;)) */
    00366         cs->status = STATUS_DONE_MAP;
    00367         fclose(file_pointer);
    00368
    00369         {
    00370           NetworkClientSocket *new_cs;
    00371           bool new_map_client = false;
    00372           /* Check if there is a client waiting for receiving the map
    00373            *  and start sending him the map */
    00374           FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00375             if (new_cs->status == STATUS_MAP_WAIT) {
    00376               /* Check if we already have a new client to send the map to */
    00377               if (!new_map_client) {
    00378                 /* If not, this client will get the map */
    00379                 new_cs->status = STATUS_AUTH;
    00380                 new_map_client = true;
    00381                 SEND_COMMAND(PACKET_SERVER_MAP)(new_cs);
    00382               } else {
    00383                 /* Else, send the other clients how many clients are in front of them */
    00384                 SEND_COMMAND(PACKET_SERVER_WAIT)(new_cs);
    00385               }
    00386             }
    00387           }
    00388         }
    00389
    00390         /* There is no more data, so break the for */
    00391         break;
    00392       }
    00393     }
    00394
    00395     /* Send all packets (forced) and check if we have send it all */
    00396     cs->Send_Packets();
    00397     if (cs->IsPacketQueueEmpty()) {
    00398       /* All are sent, increase the sent_packets */
    00399       sent_packets *= 2;
    00400     } else {
    00401       /* Not everything is sent, decrease the sent_packets */
    00402       if (sent_packets > 1) sent_packets /= 2;
    00403     }
    00404   }
    00405 }
    00406
    00407 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkClientSocket *cs, ClientID client_id)
    00408 {
    00409   /*
    00410    * Packet: SERVER_JOIN
    00411    * Function: A client is joined (all active clients receive this after a
    00412    *     PACKET_CLIENT_MAP_OK) Mostly what directly follows is a
    00413    *     PACKET_SERVER_CLIENT_INFO
    00414    * Data:
    00415    *    uint32:  Client-identifier
    00416    */
    00417
    00418   Packet *p = new Packet(PACKET_SERVER_JOIN);
    00419
    00420   p->Send_uint32(client_id);
    00421
    00422   cs->Send_Packet(p);
    00423 }
    00424
    00425
    00426 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_FRAME)
    00427 {
    00428   /*
    00429    * Packet: SERVER_FRAME
    00430    * Function: Sends the current frame-counter to the client
    00431    * Data:
    00432    *    uint32: Frame Counter
    00433    *    uint32: Frame Counter Max (how far may the client walk before the server?)
    00434    *    [uint32: general-seed-1]
    00435    *    [uint32: general-seed-2]
    00436    *      (last two depends on compile-settings, and are not default settings)
    00437    */
    00438
    00439   Packet *p = new Packet(PACKET_SERVER_FRAME);
    00440   p->Send_uint32(_frame_counter);
    00441   p->Send_uint32(_frame_counter_max);
    00442 #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
    00443   p->Send_uint32(_sync_seed_1);
    00444 #ifdef NETWORK_SEND_DOUBLE_SEED
    00445   p->Send_uint32(_sync_seed_2);
    00446 #endif
    00447 #endif
    00448   cs->Send_Packet(p);
    00449 }
    00450
    00451 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SYNC)
    00452 {
    00453   /*
    00454    * Packet: SERVER_SYNC
    00455    * Function: Sends a sync-check to the client
    00456    * Data:
    00457    *    uint32: Frame Counter
    00458    *    uint32: General-seed-1
    00459    *    [uint32: general-seed-2]
    00460    *      (last one depends on compile-settings, and are not default settings)
    00461    */
    00462
    00463   Packet *p = new Packet(PACKET_SERVER_SYNC);
    00464   p->Send_uint32(_frame_counter);
    00465   p->Send_uint32(_sync_seed_1);
    00466
    00467 #ifdef NETWORK_SEND_DOUBLE_SEED
    00468   p->Send_uint32(_sync_seed_2);
    00469 #endif
    00470   cs->Send_Packet(p);
    00471 }
    00472
    00473 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkClientSocket *cs, const CommandPacket *cp)
    00474 {
    00475   /*
    00476    * Packet: SERVER_COMMAND
    00477    * Function: Sends a DoCommand to the client
    00478    * Data:
    00479    *    uint8:  CompanyID (0..MAX_COMPANIES-1)
    00480    *    uint32: CommandID (see command.h)
    00481    *    uint32: P1 (free variables used in DoCommand)
    00482    *    uint32: P2
    00483    *    uint32: Tile
    00484    *    string: text
    00485    *    uint8:  CallBackID
    00486    *    uint32: Frame of execution
    00487    */
    00488
    00489   Packet *p = new Packet(PACKET_SERVER_COMMAND);
    00490
    00491   cs->Send_Command(p, cp);
    00492   p->Send_uint32(cp->frame);
    00493   p->Send_bool  (cp->my_cmd);
    00494
    00495   cs->Send_Packet(p);
    00496 }
    00497
    00498 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkClientSocket *cs, NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data)
    00499 {
    00500   /*
    00501    * Packet: SERVER_CHAT
    00502    * Function: Sends a chat-packet to the client
    00503    * Data:
    00504    *    uint8:  ActionID (see network_data.h, NetworkAction)
    00505    *    uint32: Client-identifier
    00506    *    String: Message (max NETWORK_CHAT_LENGTH)
    00507    *    uint64: Arbitrary data
    00508    */
    00509
    00510   Packet *p = new Packet(PACKET_SERVER_CHAT);
    00511
    00512   p->Send_uint8 (action);
    00513   p->Send_uint32(client_id);
    00514   p->Send_bool  (self_send);
    00515   p->Send_string(msg);
    00516   p->Send_uint64(data);
    00517
    00518   cs->Send_Packet(p);
    00519 }
    00520
    00521 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkClientSocket *cs, ClientID client_id, NetworkErrorCode errorno)
    00522 {
    00523   /*
    00524    * Packet: SERVER_ERROR_QUIT
    00525    * Function: One of the clients made an error and is quiting the game
    00526    *      This packet informs the other clients of that.
    00527    * Data:
    00528    *    uint32:  Client-identifier
    00529    *    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
    00530    */
    00531
    00532   Packet *p = new Packet(PACKET_SERVER_ERROR_QUIT);
    00533
    00534   p->Send_uint32(client_id);
    00535   p->Send_uint8 (errorno);
    00536
    00537   cs->Send_Packet(p);
    00538 }
    00539
    00540 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkClientSocket *cs, ClientID client_id)
    00541 {
    00542   /*
    00543    * Packet: SERVER_ERROR_QUIT
    00544    * Function: A client left the game, and this packets informs the other clients
    00545    *      of that.
    00546    * Data:
    00547    *    uint32:  Client-identifier
    00548    */
    00549
    00550   Packet *p = new Packet(PACKET_SERVER_QUIT);
    00551
    00552   p->Send_uint32(client_id);
    00553
    00554   cs->Send_Packet(p);
    00555 }
    00556
    00557 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN)
    00558 {
    00559   /*
    00560    * Packet: SERVER_SHUTDOWN
    00561    * Function: Let the clients know that the server is closing
    00562    * Data:
    00563    *     <none>
    00564    */
    00565
    00566   Packet *p = new Packet(PACKET_SERVER_SHUTDOWN);
    00567   cs->Send_Packet(p);
    00568 }
    00569
    00570 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME)
    00571 {
    00572   /*
    00573    * Packet: PACKET_SERVER_NEWGAME
    00574    * Function: Let the clients know that the server is loading a new map
    00575    * Data:
    00576    *     <none>
    00577    */
    00578
    00579   Packet *p = new Packet(PACKET_SERVER_NEWGAME);
    00580   cs->Send_Packet(p);
    00581 }
    00582
    00583 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_RCON)(NetworkClientSocket *cs, uint16 colour, const char *command)
    00584 {
    00585   Packet *p = new Packet(PACKET_SERVER_RCON);
    00586
    00587   p->Send_uint16(colour);
    00588   p->Send_string(command);
    00589   cs->Send_Packet(p);
    00590 }
    00591
    00592 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_MOVE)(NetworkClientSocket *cs, ClientID client_id, CompanyID company_id)
    00593 {
    00594   Packet *p = new Packet(PACKET_SERVER_MOVE);
    00595
    00596   p->Send_uint32(client_id);
    00597   p->Send_uint8(company_id);
    00598   cs->Send_Packet(p);
    00599 }
    00600
    00601 DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMPANY_UPDATE)(NetworkClientSocket *cs)
    00602 {
    00603   Packet *p = new Packet(PACKET_SERVER_COMPANY_UPDATE);
    00604
    00605   p->Send_uint16(_network_company_passworded);
    00606   cs->Send_Packet(p);
    00607 }
    00608
    00609 DEF_SERVER_SEND_COMMAND(PACKET_SERVER_CONFIG_UPDATE)
    00610 {
    00611   Packet *p = new Packet(PACKET_SERVER_CONFIG_UPDATE);
    00612
    00613   p->Send_uint8(_settings_client.network.max_companies);
    00614   p->Send_uint8(_settings_client.network.max_spectators);
    00615   cs->Send_Packet(p);
    00616 }
    00617
    00618 /***********
    00619  * Receiving functions
    00620  *   DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkClientSocket *cs, Packet *p
    00621  ************/
    00622
    00623 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO)
    00624 {
    00625   SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)(cs);
    00626   return NETWORK_RECV_STATUS_OKAY;
    00627 }
    00628
    00629 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
    00630 {
    00631   if (cs->status != STATUS_INACTIVE) {
    00632     /* Illegal call, return error and ignore the packet */
    00633     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00634     return NETWORK_RECV_STATUS_OKAY;
    00635   }
    00636
    00637   NetworkClientInfo *ci = cs->GetInfo();
    00638
    00639   /* We now want a password from the client else we do not allow him in! */
    00640   if (!StrEmpty(_settings_client.network.server_password)) {
    00641     SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD);
    00642   } else {
    00643     if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
    00644       SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
    00645     } else {
    00646       SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
    00647     }
    00648   }
    00649   return NETWORK_RECV_STATUS_OKAY;
    00650 }
    00651
    00652 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
    00653 {
    00654   if (cs->status != STATUS_INACTIVE) {
    00655     /* Illegal call, return error and ignore the packet */
    00656     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00657     return NETWORK_RECV_STATUS_OKAY;
    00658   }
    00659
    00660   char name[NETWORK_CLIENT_NAME_LENGTH];
    00661   NetworkClientInfo *ci;
    00662   CompanyID playas;
    00663   NetworkLanguage client_lang;
    00664   char client_revision[NETWORK_REVISION_LENGTH];
    00665
    00666   p->Recv_string(client_revision, sizeof(client_revision));
    00667
    00668   /* Check if the client has revision control enabled */
    00669   if (!IsNetworkCompatibleVersion(client_revision)) {
    00670     /* Different revisions!! */
    00671     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_REVISION);
    00672     return NETWORK_RECV_STATUS_OKAY;
    00673   }
    00674
    00675   p->Recv_string(name, sizeof(name));
    00676   playas = (Owner)p->Recv_uint8();
    00677   client_lang = (NetworkLanguage)p->Recv_uint8();
    00678
    00679   if (cs->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
    00680
    00681   /* join another company does not affect these values */
    00682   switch (playas) {
    00683     case COMPANY_NEW_COMPANY: // New company
    00684       if (Company::GetNumItems() >= _settings_client.network.max_companies) {
    00685         SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_FULL);
    00686         return NETWORK_RECV_STATUS_OKAY;
    00687       }
    00688       break;
    00689     case COMPANY_SPECTATOR: // Spectator
    00690       if (NetworkSpectatorCount() >= _settings_client.network.max_spectators) {
    00691         SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_FULL);
    00692         return NETWORK_RECV_STATUS_OKAY;
    00693       }
    00694       break;
    00695     default: // Join another company (companies 1-8 (index 0-7))
    00696       if (!Company::IsValidHumanID(playas)) {
    00697         SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH);
    00698         return NETWORK_RECV_STATUS_OKAY;
    00699       }
    00700       break;
    00701   }
    00702
    00703   /* We need a valid name.. make it Player */
    00704   if (StrEmpty(name)) strecpy(name, "Player", lastof(name));
    00705
    00706   if (!NetworkFindName(name)) { // Change name if duplicate
    00707     /* We could not create a name for this client */
    00708     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NAME_IN_USE);
    00709     return NETWORK_RECV_STATUS_OKAY;
    00710   }
    00711
    00712   ci = cs->GetInfo();
    00713
    00714   strecpy(ci->client_name, name, lastof(ci->client_name));
    00715   ci->client_playas = playas;
    00716   ci->client_lang = client_lang;
    00717
    00718   /* Make sure companies to which people try to join are not autocleaned */
    00719   if (Company::IsValidID(playas)) _network_company_states[playas].months_empty = 0;
    00720
    00721   if (_grfconfig == NULL) {
    00722     RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL);
    00723   } else {
    00724     SEND_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)(cs);
    00725   }
    00726   return NETWORK_RECV_STATUS_OKAY;
    00727 }
    00728
    00729 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
    00730 {
    00731   NetworkPasswordType type;
    00732   char password[NETWORK_PASSWORD_LENGTH];
    00733   const NetworkClientInfo *ci;
    00734
    00735   type = (NetworkPasswordType)p->Recv_uint8();
    00736   p->Recv_string(password, sizeof(password));
    00737
    00738   if (cs->status == STATUS_AUTHORIZING && type == NETWORK_GAME_PASSWORD) {
    00739     /* Check game-password */
    00740     if (strcmp(password, _settings_client.network.server_password) != 0) {
    00741       /* Password is invalid */
    00742       SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
    00743       return NETWORK_RECV_STATUS_OKAY;
    00744     }
    00745
    00746     ci = cs->GetInfo();
    00747
    00748     if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
    00749       SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
    00750       return NETWORK_RECV_STATUS_OKAY;
    00751     }
    00752
    00753     /* Valid password, allow user */
    00754     SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
    00755     return NETWORK_RECV_STATUS_OKAY;
    00756   } else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) {
    00757     ci = cs->GetInfo();
    00758
    00759     if (strcmp(password, _network_company_states[ci->client_playas].password) != 0) {
    00760       /* Password is invalid */
    00761       SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
    00762       return NETWORK_RECV_STATUS_OKAY;
    00763     }
    00764
    00765     SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
    00766     return NETWORK_RECV_STATUS_OKAY;
    00767   }
    00768
    00769
    00770   SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00771   return NETWORK_RECV_STATUS_OKAY;
    00772 }
    00773
    00774 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
    00775 {
    00776   NetworkClientSocket *new_cs;
    00777
    00778   /* Do an extra version match. We told the client our version already,
    00779    * lets confirm that the client isn't lieing to us.
    00780    * But only do it for stable releases because of those we are sure
    00781    * that everybody has the same NewGRF version. For trunk and the
    00782    * branches we make tarballs of the OpenTTDs compiled from tarball
    00783    * will have the lower bits set to 0. As such they would become
    00784    * incompatible, which we would like to prevent by this. */
    00785   if (HasBit(_openttd_newgrf_version, 19)) {
    00786     if (_openttd_newgrf_version != p->Recv_uint32()) {
    00787       /* The version we get from the client differs, it must have the
    00788        * wrong version. The client must be wrong. */
    00789       SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00790       return NETWORK_RECV_STATUS_OKAY;
    00791     }
    00792   } else if (p->size != 3) {
    00793     /* We received a packet from a version that claims to be stable.
    00794      * That shouldn't happen. The client must be wrong. */
    00795     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00796     return NETWORK_RECV_STATUS_OKAY;
    00797   }
    00798
    00799   /* The client was never joined.. so this is impossible, right?
    00800    *  Ignore the packet, give the client a warning, and close his connection */
    00801   if (cs->status < STATUS_AUTH || cs->HasClientQuit()) {
    00802     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
    00803     return NETWORK_RECV_STATUS_OKAY;
    00804   }
    00805
    00806   /* Check if someone else is receiving the map */
    00807   FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00808     if (new_cs->status == STATUS_MAP) {
    00809       /* Tell the new client to wait */
    00810       cs->status = STATUS_MAP_WAIT;
    00811       SEND_COMMAND(PACKET_SERVER_WAIT)(cs);
    00812       return NETWORK_RECV_STATUS_OKAY;
    00813     }
    00814   }
    00815
    00816   /* We receive a request to upload the map.. give it to the client! */
    00817   SEND_COMMAND(PACKET_SERVER_MAP)(cs);
    00818   return NETWORK_RECV_STATUS_OKAY;
    00819 }
    00820
    00821 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
    00822 {
    00823   /* Client has the map, now start syncing */
    00824   if (cs->status == STATUS_DONE_MAP && !cs->HasClientQuit()) {
    00825     char client_name[NETWORK_CLIENT_NAME_LENGTH];
    00826     NetworkClientSocket *new_cs;
    00827
    00828     NetworkGetClientName(client_name, sizeof(client_name), cs);
    00829
    00830     NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, NULL, cs->client_id);
    00831
    00832     /* Mark the client as pre-active, and wait for an ACK
    00833      *  so we know he is done loading and in sync with us */
    00834     cs->status = STATUS_PRE_ACTIVE;
    00835     NetworkHandleCommandQueue(cs);
    00836     SEND_COMMAND(PACKET_SERVER_FRAME)(cs);
    00837     SEND_COMMAND(PACKET_SERVER_SYNC)(cs);
    00838
    00839     /* This is the frame the client receives
    00840      *  we need it later on to make sure the client is not too slow */
    00841     cs->last_frame = _frame_counter;
    00842     cs->last_frame_server = _frame_counter;
    00843
    00844     FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00845       if (new_cs->status > STATUS_AUTH) {
    00846         SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, cs->GetInfo());
    00847         SEND_COMMAND(PACKET_SERVER_JOIN)(new_cs, cs->client_id);
    00848       }
    00849     }
    00850
    00851     /* also update the new client with our max values */
    00852     SEND_COMMAND(PACKET_SERVER_CONFIG_UPDATE)(cs);
    00853
    00854     /* quickly update the syncing client with company details */
    00855     SEND_COMMAND(PACKET_SERVER_COMPANY_UPDATE)(cs);
    00856   } else {
    00857     /* Wrong status for this packet, give a warning to client, and close connection */
    00858     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00859   }
    00860   return NETWORK_RECV_STATUS_OKAY;
    00861 }
    00862
    00867 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
    00868 {
    00869   NetworkClientSocket *new_cs;
    00870
    00871   /* The client was never joined.. so this is impossible, right?
    00872    *  Ignore the packet, give the client a warning, and close his connection */
    00873   if (cs->status < STATUS_DONE_MAP || cs->HasClientQuit()) {
    00874     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00875     return NETWORK_RECV_STATUS_OKAY;
    00876   }
    00877
    00878   CommandPacket cp;
    00879   const char *err = cs->Recv_Command(p, &cp);
    00880
    00881   if (cs->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
    00882
    00883   NetworkClientInfo *ci = cs->GetInfo();
    00884
    00885   if (err != NULL) {
    00886     IConsolePrintF(CC_ERROR, "WARNING: %s from client %d (IP: %s).", err, ci->client_id, GetClientIP(ci));
    00887     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    00888     return NETWORK_RECV_STATUS_OKAY;
    00889   }
    00890
    00891
    00892   if ((GetCommandFlags(cp.cmd) & CMD_SERVER) && ci->client_id != CLIENT_ID_SERVER) {
    00893     IConsolePrintF(CC_ERROR, "WARNING: server only command from: client %d (IP: %s), kicking...", ci->client_id, GetClientIP(ci));
    00894     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_KICKED);
    00895     return NETWORK_RECV_STATUS_OKAY;
    00896   }
    00897
    00898   if ((GetCommandFlags(cp.cmd) & CMD_SPECTATOR) == 0 && !Company::IsValidID(cp.company) && ci->client_id != CLIENT_ID_SERVER) {
    00899     IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_id, GetClientIP(ci));
    00900     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_KICKED);
    00901     return NETWORK_RECV_STATUS_OKAY;
    00902   }
    00903
    00908   if (!(cp.cmd == CMD_COMPANY_CTRL && cp.p1 == 0 && ci->client_playas == COMPANY_NEW_COMPANY) && ci->client_playas != cp.company) {
    00909     IConsolePrintF(CC_ERROR, "WARNING: client %d (IP: %s) tried to execute a command as company %d, kicking...",
    00910                    ci->client_playas + 1, GetClientIP(ci), cp.company + 1);
    00911     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH);
    00912     return NETWORK_RECV_STATUS_OKAY;
    00913   }
    00914
    00920   if (cp.cmd == CMD_COMPANY_CTRL) {
    00921     if (cp.p1 != 0 || cp.company != COMPANY_SPECTATOR) {
    00922       SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_CHEATER);
    00923       return NETWORK_RECV_STATUS_OKAY;
    00924     }
    00925
    00926     /* Check if we are full - else it's possible for spectators to send a CMD_COMPANY_CTRL and the company is created regardless of max_companies! */
    00927     if (Company::GetNumItems() >= _settings_client.network.max_companies) {
    00928       NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_CLIENT, ci->client_id, "cannot create new company, server full", CLIENT_ID_SERVER);
    00929       return NETWORK_RECV_STATUS_OKAY;
    00930     }
    00931
    00932     cp.p2 = cs->client_id;
    00933   }
    00934
    00935   /* The frame can be executed in the same frame as the next frame-packet
    00936    *  That frame just before that frame is saved in _frame_counter_max */
    00937   cp.frame = _frame_counter_max + 1;
    00938   cp.next  = NULL;
    00939
    00940   CommandCallback *callback = cp.callback;
    00941
    00942   /* Queue the command for the clients (are send at the end of the frame
    00943    *   if they can handle it ;)) */
    00944   FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00945     if (new_cs->status >= STATUS_MAP) {
    00946       /* Callbacks are only send back to the client who sent them in the
    00947        *  first place. This filters that out. */
    00948       cp.callback = (new_cs != cs) ? NULL : callback;
    00949       cp.my_cmd = (new_cs == cs);
    00950       NetworkAddCommandQueue(cp, new_cs);
    00951     }
    00952   }
    00953
    00954   cp.callback = NULL;
    00955   cp.my_cmd = false;
    00956   NetworkAddCommandQueue(cp);
    00957   return NETWORK_RECV_STATUS_OKAY;
    00958 }
    00959
    00960 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
    00961 {
    00962   /* This packets means a client noticed an error and is reporting this
    00963    *  to us. Display the error and report it to the other clients */
    00964   NetworkClientSocket *new_cs;
    00965   char str[100];
    00966   char client_name[NETWORK_CLIENT_NAME_LENGTH];
    00967   NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
    00968
    00969   /* The client was never joined.. thank the client for the packet, but ignore it */
    00970   if (cs->status < STATUS_DONE_MAP || cs->HasClientQuit()) {
    00971     cs->CloseConnection();
    00972     return NETWORK_RECV_STATUS_CONN_LOST;
    00973   }
    00974
    00975   NetworkGetClientName(client_name, sizeof(client_name), cs);
    00976
    00977   StringID strid = GetNetworkErrorMsg(errorno);
    00978   GetString(str, strid, lastof(str));
    00979
    00980   DEBUG(net, 2, "'%s' reported an error and is closing its connection (%s)", client_name, str);
    00981
    00982   NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, strid);
    00983
    00984   FOR_ALL_CLIENT_SOCKETS(new_cs) {
    00985     if (new_cs->status > STATUS_AUTH) {
    00986       SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->client_id, errorno);
    00987     }
    00988   }
    00989
    00990   cs->CloseConnection(false);
    00991   return NETWORK_RECV_STATUS_CONN_LOST;
    00992 }
    00993
    00994 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
    00995 {
    00996   /* The client wants to leave. Display this and report it to the other
    00997    *  clients. */
    00998   NetworkClientSocket *new_cs;
    00999   char client_name[NETWORK_CLIENT_NAME_LENGTH];
    01000
    01001   /* The client was never joined.. thank the client for the packet, but ignore it */
    01002   if (cs->status < STATUS_DONE_MAP || cs->HasClientQuit()) {
    01003     cs->CloseConnection();
    01004     return NETWORK_RECV_STATUS_CONN_LOST;
    01005   }
    01006
    01007   NetworkGetClientName(client_name, sizeof(client_name), cs);
    01008
    01009   NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, NULL, STR_NETWORK_MESSAGE_CLIENT_LEAVING);
    01010
    01011   FOR_ALL_CLIENT_SOCKETS(new_cs) {
    01012     if (new_cs->status > STATUS_AUTH) {
    01013       SEND_COMMAND(PACKET_SERVER_QUIT)(new_cs, cs->client_id);
    01014     }
    01015   }
    01016
    01017   cs->CloseConnection(false);
    01018   return NETWORK_RECV_STATUS_CONN_LOST;
    01019 }
    01020
    01021 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK)
    01022 {
    01023   if (cs->status < STATUS_AUTH) {
    01024     /* Illegal call, return error and ignore the packet */
    01025     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
    01026     return NETWORK_RECV_STATUS_OKAY;
    01027   }
    01028
    01029   uint32 frame = p->Recv_uint32();
    01030
    01031   /* The client is trying to catch up with the server */
    01032   if (cs->status == STATUS_PRE_ACTIVE) {
    01033     /* The client is not yet catched up? */
    01034     if (frame + DAY_TICKS < _frame_counter) return NETWORK_RECV_STATUS_OKAY;
    01035
    01036     /* Now he is! Unpause the game */
    01037     cs->status = STATUS_ACTIVE;
    01038
    01039     /* Execute script for, e.g. MOTD */
    01040     IConsoleCmdExec("exec scripts/on_server_connect.scr 0");
    01041   }
    01042
    01043   /* The client received the frame, make note of it */
    01044   cs->last_frame = frame;
    01045   /* With those 2 values we can calculate the lag realtime */
    01046   cs->last_frame_server = _frame_counter;
    01047   return NETWORK_RECV_STATUS_OKAY;
    01048 }
    01049
    01050
    01051
    01052 void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, ClientID from_id, int64 data)
    01053 {
    01054   NetworkClientSocket *cs;
    01055   const NetworkClientInfo *ci, *ci_own, *ci_to;
    01056
    01057   switch (desttype) {
    01058   case DESTTYPE_CLIENT:
    01059     /* Are we sending to the server? */
    01060     if ((ClientID)dest == CLIENT_ID_SERVER) {
    01061       ci = NetworkFindClientInfoFromClientID(from_id);
    01062       /* Display the text locally, and that is it */
    01063       if (ci != NULL)
    01064         NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
    01065     } else {
    01066       /* Else find the client to send the message to */
    01067       FOR_ALL_CLIENT_SOCKETS(cs) {
    01068         if (cs->client_id == (ClientID)dest) {
    01069           SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg, data);
    01070           break;
    01071         }
    01072       }
    01073     }
    01074
    01075     /* Display the message locally (so you know you have sent it) */
    01076     if (from_id != (ClientID)dest) {
    01077       if (from_id == CLIENT_ID_SERVER) {
    01078         ci = NetworkFindClientInfoFromClientID(from_id);
    01079         ci_to = NetworkFindClientInfoFromClientID((ClientID)dest);
    01080         if (ci != NULL && ci_to != NULL)
    01081           NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColour(ci->client_playas), true, ci_to->client_name, msg, data);
    01082       } else {
    01083         FOR_ALL_CLIENT_SOCKETS(cs) {
    01084           if (cs->client_id == from_id) {
    01085             SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, (ClientID)dest, true, msg, data);
    01086             break;
    01087           }
    01088         }
    01089       }
    01090     }
    01091     break;
    01092   case DESTTYPE_TEAM: {
    01093     bool show_local = true; // If this is false, the message is already displayed
    01094                             /* on the client who did sent it.
    01095      * Find all clients that belong to this company */
    01096     ci_to = NULL;
    01097     FOR_ALL_CLIENT_SOCKETS(cs) {
    01098       ci = cs->GetInfo();
    01099       if (ci->client_playas == (CompanyID)dest) {
    01100         SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg, data);
    01101         if (cs->client_id == from_id) show_local = false;
    01102         ci_to = ci; // Remember a client that is in the company for company-name
    01103       }
    01104     }
    01105
    01106     ci = NetworkFindClientInfoFromClientID(from_id);
    01107     ci_own = NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER);
    01108     if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) {
    01109       NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
    01110       if (from_id == CLIENT_ID_SERVER) show_local = false;
    01111       ci_to = ci_own;
    01112     }
    01113
    01114     /* There is no such client */
    01115     if (ci_to == NULL) break;
    01116
    01117     /* Display the message locally (so you know you have sent it) */
    01118     if (ci != NULL && show_local) {
    01119       if (from_id == CLIENT_ID_SERVER) {
    01120         char name[NETWORK_NAME_LENGTH];
    01121         StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
    01122         SetDParam(0, ci_to->client_playas);
    01123         GetString(name, str, lastof(name));
    01124         NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data);
    01125       } else {
    01126         FOR_ALL_CLIENT_SOCKETS(cs) {
    01127           if (cs->client_id == from_id) {
    01128             SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, ci_to->client_id, true, msg, data);
    01129           }
    01130         }
    01131       }
    01132     }
    01133     }
    01134     break;
    01135   default:
    01136     DEBUG(net, 0, "[server] received unknown chat destination type %d. Doing broadcast instead", desttype);
    01137     /* fall-through to next case */
    01138   case DESTTYPE_BROADCAST:
    01139     FOR_ALL_CLIENT_SOCKETS(cs) {
    01140       SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg, data);
    01141     }
    01142     ci = NetworkFindClientInfoFromClientID(from_id);
    01143     if (ci != NULL)
    01144       NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
    01145     break;
    01146   }
    01147 }
    01148
    01149 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
    01150 {
    01151   if (cs->status < STATUS_AUTH) {
    01152     /* Illegal call, return error and ignore the packet */
    01153     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
    01154     return NETWORK_RECV_STATUS_OKAY;
    01155   }
    01156
    01157   NetworkAction action = (NetworkAction)p->Recv_uint8();
    01158   DestType desttype = (DestType)p->Recv_uint8();
    01159   int dest = p->Recv_uint32();
    01160   char msg[NETWORK_CHAT_LENGTH];
    01161
    01162   p->Recv_string(msg, NETWORK_CHAT_LENGTH);
    01163   int64 data = p->Recv_uint64();
    01164
    01165   NetworkClientInfo *ci = cs->GetInfo();
    01166   switch (action) {
    01167     case NETWORK_ACTION_GIVE_MONEY:
    01168       if (!Company::IsValidID(ci->client_playas)) break;
    01169       /* Fall-through */
    01170     case NETWORK_ACTION_CHAT:
    01171     case NETWORK_ACTION_CHAT_CLIENT:
    01172     case NETWORK_ACTION_CHAT_COMPANY:
    01173       NetworkServerSendChat(action, desttype, dest, msg, cs->client_id, data);
    01174       break;
    01175     default:
    01176       IConsolePrintF(CC_ERROR, "WARNING: invalid chat action from client %d (IP: %s).", ci->client_id, GetClientIP(ci));
    01177       SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    01178       break;
    01179   }
    01180   return NETWORK_RECV_STATUS_OKAY;
    01181 }
    01182
    01183 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
    01184 {
    01185   if (cs->status != STATUS_ACTIVE) {
    01186     /* Illegal call, return error and ignore the packet */
    01187     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    01188     return NETWORK_RECV_STATUS_OKAY;
    01189   }
    01190
    01191   char password[NETWORK_PASSWORD_LENGTH];
    01192   const NetworkClientInfo *ci;
    01193
    01194   p->Recv_string(password, sizeof(password));
    01195   ci = cs->GetInfo();
    01196
    01197   if (Company::IsValidID(ci->client_playas)) {
    01198     strecpy(_network_company_states[ci->client_playas].password, password, lastof(_network_company_states[ci->client_playas].password));
    01199     NetworkServerUpdateCompanyPassworded(ci->client_playas, !StrEmpty(_network_company_states[ci->client_playas].password));
    01200   }
    01201   return NETWORK_RECV_STATUS_OKAY;
    01202 }
    01203
    01204 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME)
    01205 {
    01206   if (cs->status != STATUS_ACTIVE) {
    01207     /* Illegal call, return error and ignore the packet */
    01208     SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
    01209     return NETWORK_RECV_STATUS_OKAY;
    01210   }
    01211
    01212   char client_name[NETWORK_CLIENT_NAME_LENGTH];
    01213   NetworkClientInfo *ci;
    01214
    01215   p->Recv_string(client_name, sizeof(client_name));
    01216   ci = cs->GetInfo();
    01217
    01218   if (cs->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
    01219
    01220   if (ci != NULL) {
    01221     /* Display change */
    01222     if (NetworkFindName(client_name)) {
    01223       NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, client_name);
    01224       strecpy(ci->client_name, client_name, lastof(ci->client_name));
    01225       NetworkUpdateClientInfo(ci->client_id);
    01226     }
    01227   }
    01228   return NETWORK_RECV_STATUS_OKAY;
    01229 }
    01230
    01231 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_RCON)
    01232 {
    01233   char pass[NETWORK_PASSWORD_LENGTH];
    01234   char command[NETWORK_RCONCOMMAND_LENGTH];
    01235
    01236   if (StrEmpty(_settings_client.network.rcon_password)) return NETWORK_RECV_STATUS_OKAY;
    01237
    01238   p->Recv_string(pass, sizeof(pass));
    01239   p->Recv_string(command, sizeof(command));
    01240
    01241   if (strcmp(pass, _settings_client.network.rcon_password) != 0) {
    01242     DEBUG(net, 0, "[rcon] wrong password from client-id %d", cs->client_id);
    01243     return NETWORK_RECV_STATUS_OKAY;
    01244   }
    01245
    01246   DEBUG(net, 0, "[rcon] client-id %d executed: '%s'", cs->client_id, command);
    01247
    01248   _redirect_console_to_client = cs->client_id;
    01249   IConsoleCmdExec(command);
    01250   _redirect_console_to_client = INVALID_CLIENT_ID;
    01251   return NETWORK_RECV_STATUS_OKAY;
    01252 }
    01253
    01254 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MOVE)
    01255 {
    01256   CompanyID company_id = (Owner)p->Recv_uint8();
    01257
    01258   /* Check if the company is valid, we don't allow moving to AI companies */
    01259   if (company_id != COMPANY_SPECTATOR && !Company::IsValidHumanID(company_id)) return NETWORK_RECV_STATUS_OKAY;
    01260
    01261   /* Check if we require a password for this company */
    01262   if (company_id != COMPANY_SPECTATOR && !StrEmpty(_network_company_states[company_id].password)) {
    01263     /* we need a password from the client - should be in this packet */
    01264     char password[NETWORK_PASSWORD_LENGTH];
    01265     p->Recv_string(password, sizeof(password));
    01266
    01267     /* Incorrect password sent, return! */
    01268     if (strcmp(password, _network_company_states[company_id].password) != 0) {
    01269       DEBUG(net, 2, "[move] wrong password from client-id #%d for company #%d", cs->client_id, company_id + 1);
    01270       return NETWORK_RECV_STATUS_OKAY;
    01271     }
    01272   }
    01273
    01274   /* if we get here we can move the client */
    01275   NetworkServerDoMove(cs->client_id, company_id);
    01276   return NETWORK_RECV_STATUS_OKAY;
    01277 }
    01278
    01279 /* The layout for the receive-functions by the server */
    01280 typedef NetworkRecvStatus NetworkServerPacket(NetworkClientSocket *cs, Packet *p);
    01281
    01282
    01283 /* This array matches PacketType. At an incoming
    01284  *  packet it is matches against this array
    01285  *  and that way the right function to handle that
    01286  *  packet is found. */
    01287 static NetworkServerPacket * const _network_server_packet[] = {
    01288   NULL, // PACKET_SERVER_FULL,
    01289   NULL, // PACKET_SERVER_BANNED,
    01290   RECEIVE_COMMAND(PACKET_CLIENT_JOIN),
    01291   NULL, // PACKET_SERVER_ERROR,
    01292   RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO),
    01293   NULL, // PACKET_SERVER_COMPANY_INFO,
    01294   NULL, // PACKET_SERVER_CLIENT_INFO,
    01295   NULL, // PACKET_SERVER_NEED_PASSWORD,
    01296   RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD),
    01297   NULL, // PACKET_SERVER_WELCOME,
    01298   RECEIVE_COMMAND(PACKET_CLIENT_GETMAP),
    01299   NULL, // PACKET_SERVER_WAIT,
    01300   NULL, // PACKET_SERVER_MAP,
    01301   RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK),
    01302   NULL, // PACKET_SERVER_JOIN,
    01303   NULL, // PACKET_SERVER_FRAME,
    01304   NULL, // PACKET_SERVER_SYNC,
    01305   RECEIVE_COMMAND(PACKET_CLIENT_ACK),
    01306   RECEIVE_COMMAND(PACKET_CLIENT_COMMAND),
    01307   NULL, // PACKET_SERVER_COMMAND,
    01308   RECEIVE_COMMAND(PACKET_CLIENT_CHAT),
    01309   NULL, // PACKET_SERVER_CHAT,
    01310   RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD),
    01311   RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME),
    01312   RECEIVE_COMMAND(PACKET_CLIENT_QUIT),
    01313   RECEIVE_COMMAND(PACKET_CLIENT_ERROR),
    01314   NULL, // PACKET_SERVER_QUIT,
    01315   NULL, // PACKET_SERVER_ERROR_QUIT,
    01316   NULL, // PACKET_SERVER_SHUTDOWN,
    01317   NULL, // PACKET_SERVER_NEWGAME,
    01318   NULL, // PACKET_SERVER_RCON,
    01319   RECEIVE_COMMAND(PACKET_CLIENT_RCON),
    01320   NULL, // PACKET_CLIENT_CHECK_NEWGRFS,
    01321   RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED),
    01322   NULL, // PACKET_SERVER_MOVE,
    01323   RECEIVE_COMMAND(PACKET_CLIENT_MOVE),
    01324   NULL, // PACKET_SERVER_COMPANY_UPDATE,
    01325   NULL, // PACKET_SERVER_CONFIG_UPDATE,
    01326 };
    01327
    01328 /* If this fails, check the array above with network_data.h */
    01329 assert_compile(lengthof(_network_server_packet) == PACKET_END);
    01330
    01331 void NetworkSocketHandler::Send_CompanyInformation(Packet *p, const Company *c, const NetworkCompanyStats *stats)
    01332 {
    01333   /* Grab the company name */
    01334   char company_name[NETWORK_COMPANY_NAME_LENGTH];
    01335   SetDParam(0, c->index);
    01336   GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
    01337
    01338   /* Get the income */
    01339   Money income = 0;
    01340   if (_cur_year - 1 == c->inaugurated_year) {
    01341     /* The company is here just 1 year, so display [2], else display[1] */
    01342     for (uint i = 0; i < lengthof(c->yearly_expenses[2]); i++) {
    01343       income -= c->yearly_expenses[2][i];
    01344     }
    01345   } else {
    01346     for (uint i = 0; i < lengthof(c->yearly_expenses[1]); i++) {
    01347       income -= c->yearly_expenses[1][i];
    01348     }
    01349   }
    01350
    01351   /* Send the information */
    01352   p->Send_uint8 (c->index);
    01353   p->Send_string(company_name);
    01354   p->Send_uint32(c->inaugurated_year);
    01355   p->Send_uint64(c->old_economy[0].company_value);
    01356   p->Send_uint64(c->money);
    01357   p->Send_uint64(income);
    01358   p->Send_uint16(c->old_economy[0].performance_history);
    01359
    01360   /* Send 1 if there is a passord for the company else send 0 */
    01361   p->Send_bool  (!StrEmpty(_network_company_states[c->index].password));
    01362
    01363   for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) {
    01364     p->Send_uint16(stats->num_vehicle[i]);
    01365   }
    01366
    01367   for (int i = 0; i < NETWORK_STATION_TYPES; i++) {
    01368     p->Send_uint16(stats->num_station[i]);
    01369   }
    01370
    01371   p->Send_bool(c->is_ai);
    01372 }
    01373
    01378 void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
    01379 {
    01380   const Vehicle *v;
    01381   const Station *s;
    01382
    01383   memset(stats, 0, sizeof(*stats) * MAX_COMPANIES);
    01384
    01385   /* Go through all vehicles and count the type of vehicles */
    01386   FOR_ALL_VEHICLES(v) {
    01387     if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
    01388     byte type = 0;
    01389     switch (v->type) {
    01390       case VEH_TRAIN: type = 0; break;
    01391       case VEH_ROAD: type = RoadVehicle::From(v)->IsBus() ? 2 : 1; break;
    01392       case VEH_AIRCRAFT: type = 3; break;
    01393       case VEH_SHIP: type = 4; break;
    01394       default: continue;
    01395     }
    01396     stats[v->owner].num_vehicle[type]++;
    01397   }
    01398
    01399   /* Go through all stations and count the types of stations */
    01400   FOR_ALL_STATIONS(s) {
    01401     if (Company::IsValidID(s->owner)) {
    01402       NetworkCompanyStats *npi = &stats[s->owner];
    01403
    01404       if (s->facilities & FACIL_TRAIN)      npi->num_station[0]++;
    01405       if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[1]++;
    01406       if (s->facilities & FACIL_BUS_STOP)   npi->num_station[2]++;
    01407       if (s->facilities & FACIL_AIRPORT)    npi->num_station[3]++;
    01408       if (s->facilities & FACIL_DOCK)       npi->num_station[4]++;
    01409     }
    01410   }
    01411 }
    01412
    01413 /* Send a packet to all clients with updated info about this client_id */
    01414 void NetworkUpdateClientInfo(ClientID client_id)
    01415 {
    01416   NetworkClientSocket *cs;
    01417   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    01418
    01419   if (ci == NULL) return;
    01420
    01421   FOR_ALL_CLIENT_SOCKETS(cs) {
    01422     SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci);
    01423   }
    01424 }
    01425
    01426 /* Check if we want to restart the map */
    01427 static void NetworkCheckRestartMap()
    01428 {
    01429   if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) {
    01430     DEBUG(net, 0, "Auto-restarting map. Year %d reached", _cur_year);
    01431
    01432     StartNewGameWithoutGUI(GENERATE_NEW_SEED);
    01433   }
    01434 }
    01435
    01436 /* Check if the server has autoclean_companies activated
    01437     Two things happen:
    01438       1) If a company is not protected, it is closed after 1 year (for example)
    01439       2) If a company is protected, protection is disabled after 3 years (for example)
    01440            (and item 1. happens a year later) */
    01441 static void NetworkAutoCleanCompanies()
    01442 {
    01443   const NetworkClientInfo *ci;
    01444   const Company *c;
    01445   bool clients_in_company[MAX_COMPANIES];
    01446   int vehicles_in_company[MAX_COMPANIES];
    01447
    01448   if (!_settings_client.network.autoclean_companies) return;
    01449
    01450   memset(clients_in_company, 0, sizeof(clients_in_company));
    01451
    01452   /* Detect the active companies */
    01453   FOR_ALL_CLIENT_INFOS(ci) {
    01454     if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
    01455   }
    01456
    01457   if (!_network_dedicated) {
    01458     ci = NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER);
    01459     if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
    01460   }
    01461
    01462   if (_settings_client.network.autoclean_novehicles != 0) {
    01463     memset(vehicles_in_company, 0, sizeof(vehicles_in_company));
    01464
    01465     const Vehicle *v;
    01466     FOR_ALL_VEHICLES(v) {
    01467       if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
    01468       vehicles_in_company[v->owner]++;
    01469     }
    01470   }
    01471
    01472   /* Go through all the comapnies */
    01473   FOR_ALL_COMPANIES(c) {
    01474     /* Skip the non-active once */
    01475     if (c->is_ai) continue;
    01476
    01477     if (!clients_in_company[c->index]) {
    01478       /* The company is empty for one month more */
    01479       _network_company_states[c->index].months_empty++;
    01480
    01481       /* Is the company empty for autoclean_unprotected-months, and is there no protection? */
    01482       if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_states[c->index].password)) {
    01483         /* Shut the company down */
    01484         DoCommandP(0, 2, c->index, CMD_COMPANY_CTRL);
    01485         IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d with no password", c->index + 1);
    01486       }
    01487       /* Is the company empty for autoclean_protected-months, and there is a protection? */
    01488       if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_states[c->index].password)) {
    01489         /* Unprotect the company */
    01490         _network_company_states[c->index].password[0] = '\0';
    01491         IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1);
    01492         _network_company_states[c->index].months_empty = 0;
    01493         NetworkServerUpdateCompanyPassworded(c->index, false);
    01494       }
    01495       /* Is the company empty for autoclean_novehicles-months, and has no vehicles? */
    01496       if (_settings_client.network.autoclean_novehicles != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_novehicles && vehicles_in_company[c->index] == 0) {
    01497         /* Shut the company down */
    01498         DoCommandP(0, 2, c->index, CMD_COMPANY_CTRL);
    01499         IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d with no vehicles", c->index + 1);
    01500       }
    01501     } else {
    01502       /* It is not empty, reset the date */
    01503       _network_company_states[c->index].months_empty = 0;
    01504     }
    01505   }
    01506 }
    01507
    01508 /* This function changes new_name to a name that is unique (by adding #1 ...)
    01509  *  and it returns true if that succeeded. */
    01510 bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
    01511 {
    01512   bool found_name = false;
    01513   uint number = 0;
    01514   char original_name[NETWORK_CLIENT_NAME_LENGTH];
    01515
    01516   /* We use NETWORK_CLIENT_NAME_LENGTH in here, because new_name is really a pointer */
    01517   ttd_strlcpy(original_name, new_name, NETWORK_CLIENT_NAME_LENGTH);
    01518
    01519   while (!found_name) {
    01520     const NetworkClientInfo *ci;
    01521
    01522     found_name = true;
    01523     FOR_ALL_CLIENT_INFOS(ci) {
    01524       if (strcmp(ci->client_name, new_name) == 0) {
    01525         /* Name already in use */
    01526         found_name = false;
    01527         break;
    01528       }
    01529     }
    01530     /* Check if it is the same as the server-name */
    01531     ci = NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER);
    01532     if (ci != NULL) {
    01533       if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use
    01534     }
    01535
    01536     if (!found_name) {
    01537       /* Try a new name (<name> #1, <name> #2, and so on) */
    01538
    01539       /* Something's really wrong when there're more names than clients */
    01540       if (number++ > MAX_CLIENTS) break;
    01541       snprintf(new_name, NETWORK_CLIENT_NAME_LENGTH, "%s #%d", original_name, number);
    01542     }
    01543   }
    01544
    01545   return found_name;
    01546 }
    01547
    01554 bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
    01555 {
    01556   NetworkClientInfo *ci;
    01557   /* Check if the name's already in use */
    01558   FOR_ALL_CLIENT_INFOS(ci) {
    01559     if (strcmp(ci->client_name, new_name) == 0) return false;
    01560   }
    01561
    01562   ci = NetworkFindClientInfoFromClientID(client_id);
    01563   if (ci == NULL) return false;
    01564
    01565   NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, true, ci->client_name, new_name);
    01566
    01567   strecpy(ci->client_name, new_name, lastof(ci->client_name));
    01568
    01569   NetworkUpdateClientInfo(client_id);
    01570   return true;
    01571 }
    01572
    01573 /* Reads a packet from the stream */
    01574 void NetworkServer_ReadPackets(NetworkClientSocket *cs)
    01575 {
    01576   Packet *p;
    01577   NetworkRecvStatus res = NETWORK_RECV_STATUS_OKAY;
    01578
    01579   while (res == NETWORK_RECV_STATUS_OKAY && (p = cs->Recv_Packet()) != NULL) {
    01580     byte type = p->Recv_uint8();
    01581     if (type < PACKET_END && _network_server_packet[type] != NULL && !cs->HasClientQuit()) {
    01582       res = _network_server_packet[type](cs, p);
    01583     } else {
    01584       cs->CloseConnection();
    01585       res = NETWORK_RECV_STATUS_MALFORMED_PACKET;
    01586       DEBUG(net, 0, "[server] received invalid packet type %d", type);
    01587     }
    01588
    01589     delete p;
    01590   }
    01591 }
    01592
    01593 /* Handle the local command-queue */
    01594 static void NetworkHandleCommandQueue(NetworkClientSocket *cs)
    01595 {
    01596   CommandPacket *cp;
    01597
    01598   while ( (cp = cs->command_queue) != NULL) {
    01599     SEND_COMMAND(PACKET_SERVER_COMMAND)(cs, cp);
    01600
    01601     cs->command_queue = cp->next;
    01602     free(cp);
    01603   }
    01604 }
    01605
    01606 /* This is called every tick if this is a _network_server */
    01607 void NetworkServer_Tick(bool send_frame)
    01608 {
    01609   NetworkClientSocket *cs;
    01610 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
    01611   bool send_sync = false;
    01612 #endif
    01613
    01614 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
    01615   if (_frame_counter >= _last_sync_frame + _settings_client.network.sync_freq) {
    01616     _last_sync_frame = _frame_counter;
    01617     send_sync = true;
    01618   }
    01619 #endif
    01620
    01621   /* Now we are done with the frame, inform the clients that they can
    01622    *  do their frame! */
    01623   FOR_ALL_CLIENT_SOCKETS(cs) {
    01624     /* Check if the speed of the client is what we can expect from a client */
    01625     if (cs->status == STATUS_ACTIVE) {
    01626       /* 1 lag-point per day */
    01627       int lag = NetworkCalculateLag(cs) / DAY_TICKS;
    01628       if (lag > 0) {
    01629         if (lag > 3) {
    01630           /* Client did still not report in after 4 game-day, drop him
    01631            *  (that is, the 3 of above, + 1 before any lag is counted) */
    01632           IConsolePrintF(CC_ERROR,"Client #%d is dropped because the client did not respond for more than 4 game-days", cs->client_id);
    01633           NetworkCloseClient(cs, true);
    01634           continue;
    01635         }
    01636
    01637         /* Report once per time we detect the lag */
    01638         if (cs->lag_test == 0) {
    01639           IConsolePrintF(CC_WARNING,"[%d] Client #%d is slow, try increasing *net_frame_freq to a higher value!", _frame_counter, cs->client_id);
    01640           cs->lag_test = 1;
    01641         }
    01642       } else {
    01643         cs->lag_test = 0;
    01644       }
    01645     } else if (cs->status == STATUS_PRE_ACTIVE) {
    01646       int lag = NetworkCalculateLag(cs);
    01647       if (lag > _settings_client.network.max_join_time) {
    01648         IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->client_id, _settings_client.network.max_join_time);
    01649         NetworkCloseClient(cs, true);
    01650       }
    01651     } else if (cs->status == STATUS_INACTIVE) {
    01652       int lag = NetworkCalculateLag(cs);
    01653       if (lag > 4 * DAY_TICKS) {
    01654         IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->client_id, 4 * DAY_TICKS);
    01655         NetworkCloseClient(cs, true);
    01656       }
    01657     }
    01658
    01659     if (cs->status >= STATUS_PRE_ACTIVE) {
    01660       /* Check if we can send command, and if we have anything in the queue */
    01661       NetworkHandleCommandQueue(cs);
    01662
    01663       /* Send an updated _frame_counter_max to the client */
    01664       if (send_frame) SEND_COMMAND(PACKET_SERVER_FRAME)(cs);
    01665
    01666 #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
    01667       /* Send a sync-check packet */
    01668       if (send_sync) SEND_COMMAND(PACKET_SERVER_SYNC)(cs);
    01669 #endif
    01670     }
    01671   }
    01672
    01673   /* See if we need to advertise */
    01674   NetworkUDPAdvertise();
    01675 }
    01676
    01677 void NetworkServerYearlyLoop()
    01678 {
    01679   NetworkCheckRestartMap();
    01680 }
    01681
    01682 void NetworkServerMonthlyLoop()
    01683 {
    01684   NetworkAutoCleanCompanies();
    01685 }
    01686
    01687 void NetworkServerChangeOwner(Owner current_owner, Owner new_owner)
    01688 {
    01689   /* The server has to handle all administrative issues, for example
    01690    * updating and notifying all clients of what has happened */
    01691   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER);
    01692
    01693   /* The server has just changed from owner */
    01694   if (current_owner == ci->client_playas) {
    01695     ci->client_playas = new_owner;
    01696     NetworkUpdateClientInfo(CLIENT_ID_SERVER);
    01697   }
    01698
    01699   /* Find all clients that were in control of this company, and mark them as new_owner */
    01700   FOR_ALL_CLIENT_INFOS(ci) {
    01701     if (current_owner == ci->client_playas) {
    01702       ci->client_playas = new_owner;
    01703       NetworkUpdateClientInfo(ci->client_id);
    01704     }
    01705   }
    01706 }
    01707
    01708 const char *GetClientIP(NetworkClientInfo *ci)
    01709 {
    01710   return ci->client_address.GetHostname();
    01711 }
    01712
    01713 void NetworkServerShowStatusToConsole()
    01714 {
    01715   static const char * const stat_str[] = {
    01716     "inactive",
    01717     "authorizing",
    01718     "authorized",
    01719     "waiting",
    01720     "loading map",
    01721     "map done",
    01722     "ready",
    01723     "active"
    01724   };
    01725
    01726   NetworkClientSocket *cs;
    01727   FOR_ALL_CLIENT_SOCKETS(cs) {
    01728     int lag = NetworkCalculateLag(cs);
    01729     NetworkClientInfo *ci = cs->GetInfo();
    01730     const char *status;
    01731
    01732     status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
    01733     IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  status: '%s'  frame-lag: %3d  company: %1d  IP: %s",
    01734       cs->client_id, ci->client_name, status, lag,
    01735       ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0),
    01736       GetClientIP(ci));
    01737   }
    01738 }
    01739
    01743 void NetworkServerSendConfigUpdate()
    01744 {
    01745   NetworkClientSocket *cs;
    01746
    01747   FOR_ALL_CLIENT_SOCKETS(cs) {
    01748     SEND_COMMAND(PACKET_SERVER_CONFIG_UPDATE)(cs);
    01749   }
    01750 }
    01751
    01752 void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded)
    01753 {
    01754   if (NetworkCompanyIsPassworded(company_id) == passworded) return;
    01755
    01756   SB(_network_company_passworded, company_id, 1, !!passworded);
    01757   SetWindowClassesDirty(WC_COMPANY);
    01758
    01759   NetworkClientSocket *cs;
    01760   FOR_ALL_CLIENT_SOCKETS(cs) {
    01761     SEND_COMMAND(PACKET_SERVER_COMPANY_UPDATE)(cs);
    01762   }
    01763 }
    01764
    01771 void NetworkServerDoMove(ClientID client_id, CompanyID company_id)
    01772 {
    01773   /* Only allow non-dedicated servers and normal clients to be moved */
    01774   if (client_id == CLIENT_ID_SERVER && _network_dedicated) return;
    01775
    01776   NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
    01777
    01778   /* No need to waste network resources if the client is in the company already! */
    01779   if (ci->client_playas == company_id) return;
    01780
    01781   ci->client_playas = company_id;
    01782
    01783   if (client_id == CLIENT_ID_SERVER) {
    01784     SetLocalCompany(company_id);
    01785   } else {
    01786     SEND_COMMAND(PACKET_SERVER_MOVE)(NetworkFindClientStateFromClientID(client_id), client_id, company_id);
    01787   }
    01788
    01789   /* announce the client's move */
    01790   NetworkUpdateClientInfo(client_id);
    01791
    01792   NetworkAction action = (company_id == COMPANY_SPECTATOR) ? NETWORK_ACTION_COMPANY_SPECTATOR : NETWORK_ACTION_COMPANY_JOIN;
    01793   NetworkServerSendChat(action, DESTTYPE_BROADCAST, 0, "", client_id, company_id + 1);
    01794 }
    01795
    01796 void NetworkServerSendRcon(ClientID client_id, ConsoleColour colour_code, const char *string)
    01797 {
    01798   SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromClientID(client_id), colour_code, string);
    01799 }
    01800
    01801 void NetworkServerSendError(ClientID client_id, NetworkErrorCode error)
    01802 {
    01803   SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromClientID(client_id), error);
    01804 }
    01805
    01806 void NetworkServerKickClient(ClientID client_id)
    01807 {
    01808   if (client_id == CLIENT_ID_SERVER) return;
    01809   NetworkServerSendError(client_id, NETWORK_ERROR_KICKED);
    01810 }
    01811
    01812 void NetworkServerBanIP(const char *banip)
    01813 {
    01814   NetworkClientInfo *ci;
    01815
    01816   /* There can be multiple clients with the same IP, kick them all */
    01817   FOR_ALL_CLIENT_INFOS(ci) {
    01818     if (ci->client_address.IsInNetmask(const_cast<char *>(banip))) {
    01819       NetworkServerKickClient(ci->client_id);
    01820     }
    01821   }
    01822
    01823   /* Add user to ban-list */
    01824   *_network_ban_list.Append() = strdup(banip);
    01825 }
    01826
    01827 bool NetworkCompanyHasClients(CompanyID company)
    01828 {
    01829   const NetworkClientInfo *ci;
    01830   FOR_ALL_CLIENT_INFOS(ci) {
    01831     if (ci->client_playas == company) return true;
    01832   }
    01833   return false;
    01834 }
    01835
    01836 #endif /* ENABLE_NETWORK */