tron@2186: /* $Id$ */ tron@2186: Darkvater@4826: #ifdef ENABLE_NETWORK Darkvater@4826: truelight@543: #include "stdafx.h" Darkvater@4912: #include "openttd.h" // XXX StringID tron@1299: #include "debug.h" tron@1317: #include "string.h" tron@1309: #include "strings.h" truelight@543: #include "network_data.h" bjarni@2676: #include "train.h" rubidium@4261: #include "date.h" truelight@543: #include "table/strings.h" tron@2163: #include "functions.h" truelight@543: #include "network_server.h" truelight@668: #include "network_udp.h" truelight@543: #include "console.h" truelight@543: #include "command.h" tron@2162: #include "saveload.h" truelight@543: #include "vehicle.h" truelight@543: #include "station.h" tron@2159: #include "variables.h" truelight@4300: #include "genworld.h" truelight@543: truelight@543: // This file handles all the server-commands truelight@543: tron@2817: static void NetworkHandleCommandQueue(NetworkClientState* cs); truelight@716: truelight@543: // ********** truelight@543: // Sending functions truelight@716: // DEF_SERVER_SEND_COMMAND has parameter: NetworkClientState *cs truelight@543: // ********** truelight@543: truelight@716: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkClientState *cs, NetworkClientInfo *ci) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_CLIENT_INFO truelight@543: // Function: Sends info about a client truelight@543: // Data: truelight@543: // uint16: The index of the client (always unique on a server. 1 = server) truelight@543: // uint8: As which player the client is playing truelight@543: // String: The name of the client truelight@602: // String: The unique id of the client truelight@543: // truelight@543: truelight@543: if (ci->client_index != NETWORK_EMPTY_INDEX) { Darkvater@4880: Packet *p = NetworkSend_Init(PACKET_SERVER_CLIENT_INFO); truelight@543: NetworkSend_uint16(p, ci->client_index); truelight@543: NetworkSend_uint8 (p, ci->client_playas); truelight@543: NetworkSend_string(p, ci->client_name); truelight@602: NetworkSend_string(p, ci->unique_id); truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: } truelight@543: truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_COMPANY_INFO) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_COMPANY_INFO truelight@543: // Function: Sends info about the companies truelight@543: // Data: truelight@543: // truelight@543: truelight@543: int i; truelight@543: truelight@543: Player *player; truelight@543: Packet *p; truelight@543: Darkvater@2944: byte active = ActivePlayerCount(); truelight@543: truelight@543: if (active == 0) { Darkvater@4880: p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); truelight@543: truelight@543: NetworkSend_uint8 (p, NETWORK_COMPANY_INFO_VERSION); truelight@543: NetworkSend_uint8 (p, active); truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: return; truelight@543: } truelight@543: truelight@543: NetworkPopulateCompanyInfo(); truelight@543: truelight@543: FOR_ALL_PLAYERS(player) { tron@4077: if (!player->is_active) continue; truelight@543: truelight@543: p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); truelight@543: truelight@543: NetworkSend_uint8 (p, NETWORK_COMPANY_INFO_VERSION); truelight@543: NetworkSend_uint8 (p, active); truelight@734: NetworkSend_uint8 (p, player->index); truelight@543: truelight@543: NetworkSend_string(p, _network_player_info[player->index].company_name); rubidium@4326: NetworkSend_uint32(p, _network_player_info[player->index].inaugurated_year); truelight@543: NetworkSend_uint64(p, _network_player_info[player->index].company_value); truelight@543: NetworkSend_uint64(p, _network_player_info[player->index].money); truelight@543: NetworkSend_uint64(p, _network_player_info[player->index].income); truelight@543: NetworkSend_uint16(p, _network_player_info[player->index].performance); truelight@543: truelight@1011: /* Send 1 if there is a passord for the company else send 0 */ truelight@1011: if (_network_player_info[player->index].password[0] != '\0') { tron@4077: NetworkSend_uint8(p, 1); truelight@1011: } else { tron@4077: NetworkSend_uint8(p, 0); truelight@1011: } truelight@1011: tron@4077: for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) { truelight@543: NetworkSend_uint16(p, _network_player_info[player->index].num_vehicle[i]); tron@4077: } truelight@543: tron@4077: for (i = 0; i < NETWORK_STATION_TYPES; i++) { truelight@543: NetworkSend_uint16(p, _network_player_info[player->index].num_station[i]); tron@4077: } truelight@543: tron@4077: if (_network_player_info[player->index].players[0] == '\0') { truelight@543: NetworkSend_string(p, ""); tron@4077: } else { truelight@543: NetworkSend_string(p, _network_player_info[player->index].players); tron@4077: } truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@734: truelight@734: p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); truelight@734: truelight@734: NetworkSend_uint8 (p, NETWORK_COMPANY_INFO_VERSION); truelight@734: NetworkSend_uint8 (p, 0); truelight@734: truelight@734: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@716: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientState *cs, NetworkErrorCode error) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_ERROR truelight@543: // Function: The client made an error truelight@543: // Data: truelight@543: // uint8: ErrorID (see network_data.h, NetworkErrorCode) truelight@543: // truelight@543: truelight@722: char str[100]; truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR); peter1138@3417: truelight@543: NetworkSend_uint8(p, error); truelight@543: NetworkSend_Packet(p, cs); truelight@543: Darkvater@4912: GetNetworkErrorMsg(str, error, lastof(str)); Darkvater@2879: truelight@543: // Only send when the current client was in game truelight@543: if (cs->status > STATUS_AUTH) { Darkvater@4880: NetworkClientState *new_cs; Darkvater@4880: char client_name[NETWORK_CLIENT_NAME_LENGTH]; Darkvater@4880: truelight@543: NetworkGetClientName(client_name, sizeof(client_name), cs); truelight@543: Darkvater@2879: DEBUG(net, 2) ("[NET] '%s' made an error and has been disconnected. Reason: %s", client_name, str); truelight@543: tron@2373: NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str); truelight@543: truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: if (new_cs->status > STATUS_AUTH && new_cs != cs) { truelight@543: // Some errors we filter to a more general error. Clients don't have to know the real truelight@543: // reason a joining failed. truelight@543: if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) truelight@543: error = NETWORK_ERROR_ILLEGAL_PACKET; truelight@543: truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, error); truelight@543: } truelight@543: } truelight@543: } else { Darkvater@2879: DEBUG(net, 2) ("[NET] Client %d made an error and has been disconnected. Reason: %s", cs->index, str); truelight@543: } truelight@543: Darkvater@4880: cs->has_quit = true; truelight@543: truelight@543: // Make sure the data get's there before we close the connection truelight@543: NetworkSend_Packets(cs); truelight@543: truelight@543: // The client made a mistake, so drop his connection now! truelight@716: NetworkCloseClient(cs); truelight@543: } truelight@543: rubidium@5424: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHECK_NEWGRFS)(NetworkClientState *cs) rubidium@5424: { rubidium@5424: // rubidium@5424: // Packet: PACKET_SERVER_CHECK_NEWGRFS rubidium@5424: // Function: Sends info about the used GRFs to the client rubidium@5424: // Data: rubidium@5424: // uint8: Amount of GRFs rubidium@5424: // And then for each GRF: rubidium@5424: // uint32: GRF ID rubidium@5424: // 16 * uint8: MD5 checksum of the GRF rubidium@5424: // rubidium@5424: rubidium@5424: Packet *p = NetworkSend_Init(PACKET_SERVER_CHECK_NEWGRFS); rubidium@5424: const GRFConfig *c; rubidium@5424: uint grf_count = 0; rubidium@5424: rubidium@5424: for (c = _grfconfig; c != NULL; c = c->next) grf_count++; rubidium@5424: rubidium@5424: NetworkSend_uint8 (p, grf_count); rubidium@5424: for (c = _grfconfig; c != NULL; c = c->next) { rubidium@5424: NetworkSend_GRFIdentifier(p, c); rubidium@5424: } rubidium@5424: rubidium@5424: NetworkSend_Packet(p, cs); rubidium@5424: } rubidium@5424: truelight@716: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(NetworkClientState *cs, NetworkPasswordType type) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_NEED_PASSWORD truelight@543: // Function: Indication to the client that the server needs a password truelight@543: // Data: truelight@543: // uint8: Type of password truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_NEED_PASSWORD); truelight@543: NetworkSend_uint8(p, type); truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_WELCOME truelight@543: // Function: The client is joined and ready to receive his map truelight@543: // Data: truelight@543: // uint16: Own ClientID truelight@543: // truelight@543: truelight@543: Packet *p; Darkvater@2879: const NetworkClientState *new_cs; truelight@543: truelight@543: // Invalid packet when status is AUTH or higher Darkvater@2879: if (cs->status >= STATUS_AUTH) return; truelight@543: truelight@543: cs->status = STATUS_AUTH; truelight@543: _network_game_info.clients_on++; truelight@543: truelight@543: p = NetworkSend_Init(PACKET_SERVER_WELCOME); truelight@543: NetworkSend_uint16(p, cs->index); truelight@543: NetworkSend_Packet(p, cs); truelight@543: truelight@543: // Transmit info about all the active clients truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: if (new_cs != cs && new_cs->status > STATUS_AUTH) truelight@543: SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, DEREF_CLIENT_INFO(new_cs)); truelight@543: } truelight@543: // Also send the info of the server truelight@543: SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX)); truelight@543: } truelight@543: truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WAIT) truelight@543: { truelight@543: // truelight@543: // Packet: PACKET_SERVER_WAIT truelight@543: // Function: The client can not receive the map at the moment because truelight@543: // someone else is already receiving the map truelight@543: // Data: truelight@543: // uint8: Clients awaiting map truelight@543: // truelight@543: int waiting = 0; truelight@716: NetworkClientState *new_cs; truelight@543: Packet *p; truelight@543: truelight@543: // Count how many players are waiting in the queue truelight@543: FOR_ALL_CLIENTS(new_cs) { tron@4077: if (new_cs->status == STATUS_MAP_WAIT) waiting++; truelight@543: } truelight@543: truelight@543: p = NetworkSend_Init(PACKET_SERVER_WAIT); truelight@543: NetworkSend_uint8(p, waiting); truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@543: // This sends the map to the client truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_MAP truelight@543: // Function: Sends the map to the client, or a part of it (it is splitted in truelight@543: // a lot of multiple packets) truelight@543: // Data: truelight@543: // uint8: packet-type (MAP_PACKET_START, MAP_PACKET_NORMAL and MAP_PACKET_END) truelight@543: // if MAP_PACKET_START: truelight@543: // uint32: The current FrameCounter truelight@543: // if MAP_PACKET_NORMAL: truelight@543: // piece of the map (till max-size of packet) truelight@543: // if MAP_PACKET_END: truelight@543: // uint32: seed0 of player truelight@543: // uint32: seed1 of player truelight@543: // last 2 are repeated MAX_PLAYERS time truelight@543: // truelight@543: truelight@543: static FILE *file_pointer; truelight@543: static uint sent_packets; // How many packets we did send succecfully last time truelight@543: truelight@543: if (cs->status < STATUS_AUTH) { truelight@543: // Illegal call, return error and ignore the packet truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED); truelight@543: return; truelight@543: } Darkvater@4880: truelight@543: if (cs->status == STATUS_AUTH) { Darkvater@4880: char filename[256]; truelight@543: Packet *p; truelight@543: truelight@543: // Make a dump of the current game Darkvater@5296: snprintf(filename, lengthof(filename), "%s%snetwork_server.tmp", _paths.autosave_dir, PATHSEP); truelight@543: if (SaveOrLoad(filename, SL_SAVE) != SL_OK) error("network savedump failed"); truelight@543: truelight@543: file_pointer = fopen(filename, "rb"); truelight@543: fseek(file_pointer, 0, SEEK_END); truelight@543: Darkvater@5421: if (ftell(file_pointer) == 0) error("network savedump failed - zero sized savegame?"); Darkvater@5421: truelight@543: // Now send the _frame_counter and how many packets are coming truelight@543: p = NetworkSend_Init(PACKET_SERVER_MAP); truelight@543: NetworkSend_uint8(p, MAP_PACKET_START); truelight@543: NetworkSend_uint32(p, _frame_counter); truelight@543: NetworkSend_uint32(p, ftell(file_pointer)); truelight@543: NetworkSend_Packet(p, cs); truelight@543: truelight@543: fseek(file_pointer, 0, SEEK_SET); truelight@543: truelight@543: sent_packets = 4; // We start with trying 4 packets truelight@543: truelight@543: cs->status = STATUS_MAP; truelight@1602: /* Mark the start of download */ truelight@1602: cs->last_frame = _frame_counter; truelight@1602: cs->last_frame_server = _frame_counter; truelight@543: } truelight@543: truelight@543: if (cs->status == STATUS_MAP) { truelight@543: uint i; truelight@543: int res; truelight@543: for (i = 0; i < sent_packets; i++) { truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_MAP); truelight@543: NetworkSend_uint8(p, MAP_PACKET_NORMAL); truelight@4321: res = (int)fread(p->buffer + p->size, 1, SEND_MTU - p->size, file_pointer); Darkvater@4880: Darkvater@4880: if (ferror(file_pointer)) error("Error reading temporary network savegame!"); Darkvater@4880: truelight@543: p->size += res; truelight@543: NetworkSend_Packet(p, cs); truelight@543: if (feof(file_pointer)) { truelight@543: // Done reading! Darkvater@3121: Packet *p = NetworkSend_Init(PACKET_SERVER_MAP); truelight@543: NetworkSend_uint8(p, MAP_PACKET_END); truelight@543: NetworkSend_Packet(p, cs); truelight@543: truelight@543: // Set the status to DONE_MAP, no we will wait for the client truelight@543: // to send it is ready (maybe that happens like never ;)) truelight@543: cs->status = STATUS_DONE_MAP; truelight@543: fclose(file_pointer); truelight@543: truelight@543: { truelight@716: NetworkClientState *new_cs; truelight@543: bool new_map_client = false; truelight@543: // Check if there is a client waiting for receiving the map truelight@543: // and start sending him the map truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: if (new_cs->status == STATUS_MAP_WAIT) { truelight@543: // Check if we already have a new client to send the map to truelight@543: if (!new_map_client) { truelight@543: // If not, this client will get the map truelight@543: new_cs->status = STATUS_AUTH; truelight@543: new_map_client = true; truelight@543: SEND_COMMAND(PACKET_SERVER_MAP)(new_cs); truelight@543: } else { truelight@543: // Else, send the other clients how many clients are in front of them truelight@543: SEND_COMMAND(PACKET_SERVER_WAIT)(new_cs); truelight@543: } truelight@543: } truelight@543: } truelight@543: } truelight@543: truelight@543: // There is no more data, so break the for truelight@543: break; truelight@543: } truelight@543: } truelight@543: truelight@543: // Send all packets (forced) and check if we have send it all truelight@543: NetworkSend_Packets(cs); truelight@543: if (cs->packet_queue == NULL) { truelight@543: // All are sent, increase the sent_packets truelight@543: sent_packets *= 2; truelight@543: } else { truelight@543: // Not everything is sent, decrease the sent_packets truelight@543: if (sent_packets > 1) sent_packets /= 2; truelight@543: } truelight@543: } truelight@543: } truelight@543: truelight@716: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkClientState *cs, uint16 client_index) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_JOIN truelight@543: // Function: A client is joined (all active clients receive this after a truelight@543: // PACKET_CLIENT_MAP_OK) Mostly what directly follows is a truelight@543: // PACKET_SERVER_CLIENT_INFO truelight@543: // Data: truelight@543: // uint16: Client-Index truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_JOIN); truelight@543: truelight@543: NetworkSend_uint16(p, client_index); truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@543: truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_FRAME) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_FRAME truelight@543: // Function: Sends the current frame-counter to the client truelight@543: // Data: truelight@543: // uint32: Frame Counter truelight@543: // uint32: Frame Counter Max (how far may the client walk before the server?) truelight@543: // [uint32: general-seed-1] truelight@543: // [uint32: general-seed-2] truelight@543: // (last two depends on compile-settings, and are not default settings) truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_FRAME); truelight@543: NetworkSend_uint32(p, _frame_counter); truelight@543: NetworkSend_uint32(p, _frame_counter_max); truelight@543: #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME truelight@543: NetworkSend_uint32(p, _sync_seed_1); truelight@543: #ifdef NETWORK_SEND_DOUBLE_SEED truelight@543: NetworkSend_uint32(p, _sync_seed_2); truelight@543: #endif truelight@543: #endif truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SYNC) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_SYNC truelight@543: // Function: Sends a sync-check to the client truelight@543: // Data: truelight@543: // uint32: Frame Counter truelight@543: // uint32: General-seed-1 truelight@543: // [uint32: general-seed-2] truelight@543: // (last one depends on compile-settings, and are not default settings) truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_SYNC); truelight@543: NetworkSend_uint32(p, _frame_counter); truelight@543: NetworkSend_uint32(p, _sync_seed_1); truelight@543: truelight@543: #ifdef NETWORK_SEND_DOUBLE_SEED truelight@543: NetworkSend_uint32(p, _sync_seed_2); truelight@543: #endif truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@716: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkClientState *cs, CommandPacket *cp) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_COMMAND truelight@543: // Function: Sends a DoCommand to the client truelight@543: // Data: truelight@543: // uint8: PlayerID (0..MAX_PLAYERS-1) truelight@543: // uint32: CommandID (see command.h) truelight@543: // uint32: P1 (free variables used in DoCommand) truelight@543: // uint32: P2 truelight@543: // uint32: Tile tron@1820: // string: text truelight@543: // uint8: CallBackID (see callback_table.c) truelight@543: // uint32: Frame of execution truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_COMMAND); truelight@543: truelight@543: NetworkSend_uint8(p, cp->player); truelight@543: NetworkSend_uint32(p, cp->cmd); truelight@543: NetworkSend_uint32(p, cp->p1); truelight@543: NetworkSend_uint32(p, cp->p2); truelight@543: NetworkSend_uint32(p, cp->tile); tron@1820: NetworkSend_string(p, cp->text); truelight@543: NetworkSend_uint8(p, cp->callback); truelight@543: NetworkSend_uint32(p, cp->frame); truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@722: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkClientState *cs, NetworkAction action, uint16 client_index, bool self_send, const char *msg) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_CHAT truelight@543: // Function: Sends a chat-packet to the client truelight@543: // Data: truelight@543: // uint8: ActionID (see network_data.h, NetworkAction) truelight@543: // uint16: Client-index truelight@543: // String: Message (max MAX_TEXT_MSG_LEN) truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_CHAT); truelight@543: truelight@543: NetworkSend_uint8(p, action); truelight@543: NetworkSend_uint16(p, client_index); truelight@722: NetworkSend_uint8(p, self_send); truelight@543: NetworkSend_string(p, msg); truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@716: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkClientState *cs, uint16 client_index, NetworkErrorCode errorno) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_ERROR_QUIT truelight@543: // Function: One of the clients made an error and is quiting the game truelight@543: // This packet informs the other clients of that. truelight@543: // Data: truelight@543: // uint16: Client-index truelight@543: // uint8: ErrorID (see network_data.h, NetworkErrorCode) truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR_QUIT); truelight@543: truelight@543: NetworkSend_uint16(p, client_index); truelight@543: NetworkSend_uint8(p, errorno); truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@716: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkClientState *cs, uint16 client_index, const char *leavemsg) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_ERROR_QUIT truelight@543: // Function: A client left the game, and this packets informs the other clients truelight@543: // of that. truelight@543: // Data: truelight@543: // uint16: Client-index truelight@543: // String: leave-message truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_QUIT); truelight@543: truelight@543: NetworkSend_uint16(p, client_index); truelight@543: NetworkSend_string(p, leavemsg); truelight@543: truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_SHUTDOWN truelight@543: // Function: Let the clients know that the server is closing truelight@543: // Data: truelight@543: // truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_SHUTDOWN); truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@543: DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME) truelight@543: { truelight@543: // truelight@543: // Packet: PACKET_SERVER_NEWGAME truelight@543: // Function: Let the clients know that the server is loading a new map truelight@543: // Data: truelight@543: // truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_NEWGAME); truelight@543: NetworkSend_Packet(p, cs); truelight@543: } truelight@543: truelight@1026: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_RCON)(NetworkClientState *cs, uint16 color, const char *command) truelight@1026: { truelight@1026: Packet *p = NetworkSend_Init(PACKET_SERVER_RCON); truelight@1026: truelight@1026: NetworkSend_uint16(p, color); truelight@1026: NetworkSend_string(p, command); truelight@1026: NetworkSend_Packet(p, cs); truelight@1026: } truelight@1026: truelight@543: // ********** truelight@543: // Receiving functions truelight@716: // DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkClientState *cs, Packet *p truelight@543: // ********** truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO) truelight@543: { truelight@543: SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)(cs); truelight@543: } truelight@543: rubidium@5424: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED) rubidium@5424: { rubidium@5424: NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); rubidium@5424: rubidium@5424: /* We now want a password from the client else we do not allow him in! */ rubidium@5424: if (_network_game_info.use_password) { rubidium@5424: SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD); rubidium@5424: } else { rubidium@5424: if (IsValidPlayer(ci->client_playas) && _network_player_info[ci->client_playas].password[0] != '\0') { rubidium@5424: SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD); rubidium@5424: } else { rubidium@5424: SEND_COMMAND(PACKET_SERVER_WELCOME)(cs); rubidium@5424: } rubidium@5424: } rubidium@5424: } rubidium@5424: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN) truelight@543: { Darkvater@3623: char name[NETWORK_CLIENT_NAME_LENGTH]; truelight@602: char unique_id[NETWORK_NAME_LENGTH]; truelight@543: NetworkClientInfo *ci; truelight@543: byte playas; truelight@543: NetworkLanguage client_lang; truelight@543: char client_revision[NETWORK_REVISION_LENGTH]; truelight@543: truelight@903: NetworkRecv_string(cs, p, client_revision, sizeof(client_revision)); truelight@543: truelight@692: #if defined(WITH_REV) || defined(WITH_REV_HACK) darkvater@659: // Check if the client has revision control enabled tron@4077: if (strcmp(NOREV_STRING, client_revision) != 0 && tron@4077: strcmp(_network_game_info.server_revision, client_revision) != 0) { tron@4077: // Different revisions!! tron@4077: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_REVISION); tron@4077: return; truelight@543: } darkvater@663: #endif truelight@543: truelight@903: NetworkRecv_string(cs, p, name, sizeof(name)); truelight@903: playas = NetworkRecv_uint8(cs, p); truelight@903: client_lang = NetworkRecv_uint8(cs, p); truelight@903: NetworkRecv_string(cs, p, unique_id, sizeof(unique_id)); truelight@903: Darkvater@4880: if (cs->has_quit) return; truelight@543: Darkvater@2879: // join another company does not affect these values Darkvater@2879: switch (playas) { Darkvater@4861: case PLAYER_NEW_COMPANY: /* New company */ Darkvater@2944: if (ActivePlayerCount() >= _network_game_info.companies_max) { Darkvater@2879: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_FULL); Darkvater@2879: return; Darkvater@2879: } Darkvater@2879: break; Darkvater@4848: case PLAYER_SPECTATOR: /* Spectator */ Darkvater@2944: if (NetworkSpectatorCount() >= _network_game_info.spectators_max) { Darkvater@2879: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_FULL); Darkvater@2879: return; Darkvater@2879: } Darkvater@2879: break; Darkvater@4878: default: /* Join another company (companies 1-8 (index 0-7)) */ Darkvater@4878: if (!IsValidPlayer(playas)) { Darkvater@4861: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_PLAYER_MISMATCH); Darkvater@4861: return; Darkvater@4861: } Darkvater@4861: break; truelight@543: } truelight@543: Darkvater@2879: // We need a valid name.. make it Player Darkvater@3623: if (*name == '\0') ttd_strlcpy(name, "Player", sizeof(name)); Darkvater@2879: Darkvater@2879: if (!NetworkFindName(name)) { // Change name if duplicate truelight@543: // We could not create a name for this player truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NAME_IN_USE); truelight@543: return; truelight@543: } truelight@543: truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: Darkvater@3623: ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name)); Darkvater@3623: ttd_strlcpy(ci->unique_id, unique_id, sizeof(ci->unique_id)); truelight@543: ci->client_playas = playas; truelight@543: ci->client_lang = client_lang; truelight@543: Darkvater@4878: /* Make sure companies to which people try to join are not autocleaned */ Darkvater@4878: if (IsValidPlayer(playas)) _network_player_info[playas].months_empty = 0; rubidium@5424: rubidium@5424: if (_grfconfig == NULL) { rubidium@5424: RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL); rubidium@5424: } else { rubidium@5424: SEND_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)(cs); rubidium@5424: } truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD) truelight@543: { truelight@543: NetworkPasswordType type; truelight@543: char password[NETWORK_PASSWORD_LENGTH]; Darkvater@4880: const NetworkClientInfo *ci; truelight@543: truelight@903: type = NetworkRecv_uint8(cs, p); truelight@903: NetworkRecv_string(cs, p, password, sizeof(password)); truelight@543: truelight@543: if (cs->status == STATUS_INACTIVE && type == NETWORK_GAME_PASSWORD) { truelight@543: // Check game-password tron@4026: if (strcmp(password, _network_game_info.server_password) != 0) { truelight@543: // Password is invalid truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD); truelight@543: return; truelight@543: } truelight@543: truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: Darkvater@4878: if (IsValidPlayer(ci->client_playas) && _network_player_info[ci->client_playas].password[0] != '\0') { truelight@543: SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD); truelight@543: return; truelight@543: } truelight@543: truelight@543: // Valid password, allow user truelight@543: SEND_COMMAND(PACKET_SERVER_WELCOME)(cs); truelight@543: return; truelight@543: } else if (cs->status == STATUS_INACTIVE && type == NETWORK_COMPANY_PASSWORD) { truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: Darkvater@4878: if (strcmp(password, _network_player_info[ci->client_playas].password) != 0) { truelight@543: // Password is invalid truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD); truelight@543: return; truelight@543: } truelight@543: truelight@543: SEND_COMMAND(PACKET_SERVER_WELCOME)(cs); truelight@543: return; truelight@543: } truelight@543: truelight@543: truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); truelight@543: return; truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP) truelight@543: { Darkvater@4880: const NetworkClientState *new_cs; truelight@543: truelight@543: // The client was never joined.. so this is impossible, right? truelight@543: // Ignore the packet, give the client a warning, and close his connection Darkvater@4880: if (cs->status < STATUS_AUTH || cs->has_quit) { truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED); truelight@543: return; truelight@543: } truelight@543: truelight@543: // Check if someone else is receiving the map truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: if (new_cs->status == STATUS_MAP) { truelight@543: // Tell the new client to wait truelight@543: cs->status = STATUS_MAP_WAIT; truelight@543: SEND_COMMAND(PACKET_SERVER_WAIT)(cs); truelight@543: return; truelight@543: } truelight@543: } truelight@543: truelight@543: // We receive a request to upload the map.. give it to the client! truelight@543: SEND_COMMAND(PACKET_SERVER_MAP)(cs); truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK) truelight@543: { truelight@543: // Client has the map, now start syncing Darkvater@4880: if (cs->status == STATUS_DONE_MAP && !cs->has_quit) { truelight@793: char client_name[NETWORK_CLIENT_NAME_LENGTH]; truelight@716: NetworkClientState *new_cs; truelight@543: truelight@543: NetworkGetClientName(client_name, sizeof(client_name), cs); truelight@543: truelight@722: NetworkTextMessage(NETWORK_ACTION_JOIN, 1, false, client_name, ""); truelight@543: truelight@543: // Mark the client as pre-active, and wait for an ACK truelight@543: // so we know he is done loading and in sync with us truelight@543: cs->status = STATUS_PRE_ACTIVE; truelight@543: NetworkHandleCommandQueue(cs); truelight@543: SEND_COMMAND(PACKET_SERVER_FRAME)(cs); truelight@543: SEND_COMMAND(PACKET_SERVER_SYNC)(cs); truelight@543: truelight@543: // This is the frame the client receives truelight@543: // we need it later on to make sure the client is not too slow truelight@543: cs->last_frame = _frame_counter; truelight@543: cs->last_frame_server = _frame_counter; truelight@543: truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: if (new_cs->status > STATUS_AUTH) { truelight@543: SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, DEREF_CLIENT_INFO(cs)); truelight@543: SEND_COMMAND(PACKET_SERVER_JOIN)(new_cs, cs->index); truelight@543: } truelight@543: } truelight@1602: truelight@1602: if (_network_pause_on_join) { truelight@1602: /* Now pause the game till the client is in sync */ truelight@1602: DoCommandP(0, 1, 0, NULL, CMD_PAUSE); truelight@1602: Darkvater@4944: NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (incoming client)", NETWORK_SERVER_INDEX); truelight@1602: } truelight@543: } else { truelight@543: // Wrong status for this packet, give a warning to client, and close connection truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); truelight@543: } truelight@543: } truelight@543: Darkvater@1804: /** Enforce the command flags. Darkvater@1804: * Eg a server-only command can only be executed by a server, etc. Darkvater@1804: * @param *cp the commandpacket that is going to be checked Darkvater@1804: * @param *ci client information for debugging output to console Darkvater@1804: */ Darkvater@1804: static bool CheckCommandFlags(const CommandPacket *cp, const NetworkClientInfo *ci) Darkvater@1804: { Darkvater@1804: byte flags = GetCommandFlags(cp->cmd); Darkvater@1804: Darkvater@1804: if (flags & CMD_SERVER && ci->client_index != NETWORK_SERVER_INDEX) { Darkvater@4878: IConsolePrintF(_icolour_err, "WARNING: server only command from client %d (IP: %s), kicking...", ci->client_index, GetPlayerIP(ci)); Darkvater@1804: return false; Darkvater@1804: } Darkvater@1804: Darkvater@1804: if (flags & CMD_OFFLINE) { Darkvater@4878: IConsolePrintF(_icolour_err, "WARNING: offline only command from client %d (IP: %s), kicking...", ci->client_index, GetPlayerIP(ci)); Darkvater@1804: return false; Darkvater@1804: } Darkvater@4880: Darkvater@1804: return true; Darkvater@1804: } Darkvater@1804: Darkvater@1804: /** The client has done a command and wants us to handle it Darkvater@1804: * @param *cs the connected client that has sent the command Darkvater@1804: * @param *p the packet in which the command was sent Darkvater@1804: */ truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) truelight@543: { Darkvater@1804: NetworkClientState *new_cs; Darkvater@1804: const NetworkClientInfo *ci; truelight@543: byte callback; truelight@543: truelight@543: CommandPacket *cp = malloc(sizeof(CommandPacket)); truelight@543: truelight@543: // The client was never joined.. so this is impossible, right? truelight@543: // Ignore the packet, give the client a warning, and close his connection Darkvater@4880: if (cs->status < STATUS_DONE_MAP || cs->has_quit) { truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); truelight@543: return; truelight@543: } truelight@543: truelight@903: cp->player = NetworkRecv_uint8(cs, p); truelight@903: cp->cmd = NetworkRecv_uint32(cs, p); truelight@903: cp->p1 = NetworkRecv_uint32(cs, p); truelight@903: cp->p2 = NetworkRecv_uint32(cs, p); truelight@903: cp->tile = NetworkRecv_uint32(cs, p); tron@1820: NetworkRecv_string(cs, p, cp->text, lengthof(cp->text)); truelight@543: truelight@903: callback = NetworkRecv_uint8(cs, p); truelight@903: Darkvater@4880: if (cs->has_quit) return; Darkvater@1804: Darkvater@1804: ci = DEREF_CLIENT_INFO(cs); truelight@903: truelight@903: /* Check if cp->cmd is valid */ truelight@903: if (!IsValidCommand(cp->cmd)) { Darkvater@4878: IConsolePrintF(_icolour_err, "WARNING: invalid command from client %d (IP: %s).", ci->client_index, GetPlayerIP(ci)); truelight@903: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); truelight@903: return; truelight@903: } truelight@543: Darkvater@1804: if (!CheckCommandFlags(cp, ci)) { Darkvater@1804: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_KICKED); Darkvater@1804: return; Darkvater@1804: } Darkvater@1804: Darkvater@1804: /** Only CMD_PLAYER_CTRL is always allowed, for the rest, playas needs Darkvater@1804: * to match the player in the packet. If it doesn't, the client has done Darkvater@1804: * something pretty naughty (or a bug), and will be kicked Darkvater@1804: */ Darkvater@4878: if (!(cp->cmd == CMD_PLAYER_CTRL && cp->p1 == 0) && ci->client_playas != cp->player) { Darkvater@1805: IConsolePrintF(_icolour_err, "WARNING: player %d (IP: %s) tried to execute a command as player %d, kicking...", Darkvater@4878: ci->client_playas + 1, GetPlayerIP(ci), cp->player + 1); truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_PLAYER_MISMATCH); truelight@543: return; truelight@543: } truelight@748: Darkvater@1804: /** @todo CMD_PLAYER_CTRL with p1 = 0 announces a new player to the server. To give the Darkvater@1804: * player the correct ID, the server injects p2 and executes the command. Any other p1 Darkvater@1804: * is prohibited. Pretty ugly and should be redone together with its function. Darkvater@1804: * @see CmdPlayerCtrl() players.c:655 Darkvater@1804: */ Darkvater@1804: if (cp->cmd == CMD_PLAYER_CTRL) { Darkvater@1804: if (cp->p1 != 0) { Darkvater@1804: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_CHEATER); Darkvater@1804: return; Darkvater@1804: } Darkvater@1804: Darkvater@4880: /* XXX - Execute the command as a valid player. Normally this would be done by a Darkvater@4880: * spectator, but that is not allowed any commands. So do an impersonation. The drawback Darkvater@4880: * of this is that the first company's last_built_tile is also updated... */ Darkvater@4860: cp->player = 0; Darkvater@4880: cp->p2 = cs - _clients; // XXX - UGLY! p2 is mis-used to get the client-id in CmdPlayerCtrl truelight@543: } truelight@543: truelight@543: // The frame can be executed in the same frame as the next frame-packet truelight@543: // That frame just before that frame is saved in _frame_counter_max truelight@543: cp->frame = _frame_counter_max + 1; truelight@903: cp->next = NULL; truelight@543: truelight@543: // Queue the command for the clients (are send at the end of the frame truelight@543: // if they can handle it ;)) truelight@543: FOR_ALL_CLIENTS(new_cs) { Darkvater@5370: if (new_cs->status >= STATUS_MAP) { truelight@543: // Callbacks are only send back to the client who sent them in the truelight@543: // first place. This filters that out. Darkvater@1804: cp->callback = (new_cs != cs) ? 0 : callback; truelight@543: NetworkAddCommandQueue(new_cs, cp); truelight@543: } truelight@543: } truelight@543: truelight@543: cp->callback = 0; truelight@543: // Queue the command on the server truelight@543: if (_local_command_queue == NULL) { truelight@543: _local_command_queue = cp; truelight@543: } else { truelight@543: // Find last packet truelight@543: CommandPacket *c = _local_command_queue; truelight@543: while (c->next != NULL) c = c->next; truelight@543: c->next = cp; truelight@543: } truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR) truelight@543: { truelight@543: // This packets means a client noticed an error and is reporting this truelight@543: // to us. Display the error and report it to the other clients truelight@716: NetworkClientState *new_cs; Darkvater@3437: char str[100]; Darkvater@4880: char client_name[NETWORK_CLIENT_NAME_LENGTH]; peter1138@3417: NetworkErrorCode errorno = NetworkRecv_uint8(cs, p); truelight@543: truelight@543: // The client was never joined.. thank the client for the packet, but ignore it Darkvater@4880: if (cs->status < STATUS_DONE_MAP || cs->has_quit) { Darkvater@4880: cs->has_quit = true; truelight@543: return; truelight@543: } truelight@543: truelight@543: NetworkGetClientName(client_name, sizeof(client_name), cs); truelight@543: Darkvater@4912: GetNetworkErrorMsg(str, errorno, lastof(str)); truelight@543: truelight@722: DEBUG(net, 2)("[NET] %s reported an error and is closing his connection (%s)", client_name, str); truelight@543: tron@2373: NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str); truelight@543: truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: if (new_cs->status > STATUS_AUTH) { truelight@543: SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno); truelight@543: } truelight@543: } truelight@543: Darkvater@4880: cs->has_quit = true; truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT) truelight@543: { truelight@543: // The client wants to leave. Display this and report it to the other truelight@543: // clients. truelight@716: NetworkClientState *new_cs; truelight@722: char str[100]; truelight@793: char client_name[NETWORK_CLIENT_NAME_LENGTH]; truelight@543: truelight@543: // The client was never joined.. thank the client for the packet, but ignore it Darkvater@4880: if (cs->status < STATUS_DONE_MAP || cs->has_quit) { Darkvater@4880: cs->has_quit = true; truelight@543: return; truelight@543: } truelight@543: tron@2373: NetworkRecv_string(cs, p, str, lengthof(str)); truelight@543: truelight@543: NetworkGetClientName(client_name, sizeof(client_name), cs); truelight@543: tron@2373: NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str); truelight@543: truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: if (new_cs->status > STATUS_AUTH) { truelight@722: SEND_COMMAND(PACKET_SERVER_QUIT)(new_cs, cs->index, str); truelight@543: } truelight@543: } truelight@543: Darkvater@4880: cs->has_quit = true; truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK) truelight@543: { truelight@1602: uint32 frame = NetworkRecv_uint32(cs, p); truelight@1602: truelight@1602: /* The client is trying to catch up with the server */ truelight@1602: if (cs->status == STATUS_PRE_ACTIVE) { truelight@1602: /* The client is not yet catched up? */ Darkvater@4880: if (frame + DAY_TICKS < _frame_counter) return; truelight@1602: truelight@1602: /* Now he is! Unpause the game */ truelight@1602: cs->status = STATUS_ACTIVE; truelight@1602: truelight@1602: if (_network_pause_on_join) { truelight@1602: DoCommandP(0, 0, 0, NULL, CMD_PAUSE); Darkvater@4944: NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", NETWORK_SERVER_INDEX); truelight@1602: } peter1138@4713: peter1138@4717: CheckMinPlayers(); peter1138@4717: peter1138@4713: /* Execute script for, e.g. MOTD */ peter1138@4713: IConsoleCmdExec("exec scripts/on_server_connect.scr 0"); truelight@1602: } truelight@1602: truelight@543: // The client received the frame, make note of it truelight@1602: cs->last_frame = frame; truelight@543: // With those 2 values we can calculate the lag realtime truelight@543: cs->last_frame_server = _frame_counter; truelight@543: } truelight@543: truelight@543: truelight@543: truelight@839: void NetworkServer_HandleChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index) truelight@543: { truelight@716: NetworkClientState *cs; Darkvater@4880: const NetworkClientInfo *ci, *ci_own, *ci_to; truelight@543: truelight@543: switch (desttype) { truelight@543: case DESTTYPE_CLIENT: truelight@722: /* Are we sending to the server? */ truelight@722: if (dest == NETWORK_SERVER_INDEX) { truelight@543: ci = NetworkFindClientInfoFromIndex(from_index); truelight@722: /* Display the text locally, and that is it */ truelight@543: if (ci != NULL) Darkvater@4878: NetworkTextMessage(action, GetDrawStringPlayerColor(ci->client_playas), false, ci->client_name, "%s", msg); truelight@543: } else { truelight@722: /* Else find the client to send the message to */ truelight@543: FOR_ALL_CLIENTS(cs) { truelight@543: if (cs->index == dest) { truelight@722: SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); truelight@543: break; truelight@543: } truelight@543: } truelight@543: } truelight@543: truelight@543: // Display the message locally (so you know you have sent it) truelight@543: if (from_index != dest) { truelight@722: if (from_index == NETWORK_SERVER_INDEX) { truelight@543: ci = NetworkFindClientInfoFromIndex(from_index); truelight@543: ci_to = NetworkFindClientInfoFromIndex(dest); truelight@543: if (ci != NULL && ci_to != NULL) Darkvater@4878: NetworkTextMessage(action, GetDrawStringPlayerColor(ci->client_playas), true, ci_to->client_name, "%s", msg); truelight@543: } else { truelight@543: FOR_ALL_CLIENTS(cs) { truelight@543: if (cs->index == from_index) { truelight@722: SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, dest, true, msg); truelight@543: break; truelight@543: } truelight@543: } truelight@543: } truelight@543: } truelight@543: break; Darkvater@4906: case DESTTYPE_TEAM: { truelight@543: bool show_local = true; // If this is false, the message is already displayed truelight@543: // on the client who did sent it. truelight@722: /* Find all clients that belong to this player */ truelight@733: ci_to = NULL; truelight@543: FOR_ALL_CLIENTS(cs) { truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: if (ci->client_playas == dest) { truelight@722: SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); Darkvater@4880: if (cs->index == from_index) show_local = false; truelight@733: ci_to = ci; // Remember a client that is in the company for company-name truelight@543: } truelight@543: } truelight@733: truelight@543: ci = NetworkFindClientInfoFromIndex(from_index); truelight@543: ci_own = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); truelight@543: if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) { Darkvater@4878: NetworkTextMessage(action, GetDrawStringPlayerColor(ci->client_playas), false, ci->client_name, "%s", msg); Darkvater@4880: if (from_index == NETWORK_SERVER_INDEX) show_local = false; Darkvater@1834: ci_to = ci_own; truelight@543: } truelight@543: truelight@733: /* There is no such player */ Darkvater@1834: if (ci_to == NULL) break; truelight@733: truelight@543: // Display the message locally (so you know you have sent it) truelight@543: if (ci != NULL && show_local) { truelight@543: if (from_index == NETWORK_SERVER_INDEX) { truelight@543: char name[NETWORK_NAME_LENGTH]; Darkvater@4945: StringID str = IsValidPlayer(ci_to->client_playas) ? GetPlayer(ci_to->client_playas)->name_1 : STR_NETWORK_SPECTATORS; Darkvater@4945: GetString(name, str, lastof(name)); Darkvater@4878: NetworkTextMessage(action, GetDrawStringPlayerColor(ci_own->client_playas), true, name, "%s", msg); truelight@543: } else { truelight@543: FOR_ALL_CLIENTS(cs) { truelight@543: if (cs->index == from_index) { truelight@779: SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, ci_to->client_index, true, msg); truelight@543: } truelight@543: } truelight@543: } truelight@543: } truelight@543: } truelight@543: break; truelight@543: default: celestar@2013: DEBUG(net, 0)("[NET][Server] Received unknown destination type %d. Doing broadcast instead."); truelight@543: /* fall-through to next case */ truelight@543: case DESTTYPE_BROADCAST: truelight@543: FOR_ALL_CLIENTS(cs) { truelight@722: SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); truelight@543: } truelight@543: ci = NetworkFindClientInfoFromIndex(from_index); truelight@543: if (ci != NULL) Darkvater@4878: NetworkTextMessage(action, GetDrawStringPlayerColor(ci->client_playas), false, ci->client_name, "%s", msg); truelight@543: break; truelight@543: } truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT) truelight@543: { truelight@903: NetworkAction action = NetworkRecv_uint8(cs, p); truelight@903: DestType desttype = NetworkRecv_uint8(cs, p); truelight@903: int dest = NetworkRecv_uint8(cs, p); truelight@543: char msg[MAX_TEXT_MSG_LEN]; truelight@543: truelight@903: NetworkRecv_string(cs, p, msg, MAX_TEXT_MSG_LEN); truelight@543: truelight@543: NetworkServer_HandleChat(action, desttype, dest, msg, cs->index); truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD) truelight@543: { truelight@543: char password[NETWORK_PASSWORD_LENGTH]; Darkvater@4880: const NetworkClientInfo *ci; truelight@543: truelight@903: NetworkRecv_string(cs, p, password, sizeof(password)); truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: Darkvater@4878: if (IsValidPlayer(ci->client_playas)) { Darkvater@4878: ttd_strlcpy(_network_player_info[ci->client_playas].password, password, sizeof(_network_player_info[0].password)); truelight@543: } truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME) truelight@543: { truelight@793: char client_name[NETWORK_CLIENT_NAME_LENGTH]; truelight@543: NetworkClientInfo *ci; truelight@543: truelight@903: NetworkRecv_string(cs, p, client_name, sizeof(client_name)); truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: Darkvater@4880: if (cs->has_quit) return; truelight@903: truelight@543: if (ci != NULL) { truelight@543: // Display change truelight@793: if (NetworkFindName(client_name)) { tron@2373: NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, 1, false, ci->client_name, "%s", client_name); truelight@793: ttd_strlcpy(ci->client_name, client_name, sizeof(ci->client_name)); truelight@543: NetworkUpdateClientInfo(ci->client_index); truelight@543: } truelight@543: } truelight@543: } truelight@543: truelight@1026: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_RCON) truelight@1026: { truelight@1026: char pass[NETWORK_PASSWORD_LENGTH]; truelight@1026: char command[NETWORK_RCONCOMMAND_LENGTH]; truelight@1026: tron@4077: if (_network_game_info.rcon_password[0] == '\0') return; truelight@1026: truelight@1026: NetworkRecv_string(cs, p, pass, sizeof(pass)); truelight@1026: NetworkRecv_string(cs, p, command, sizeof(command)); truelight@1026: tron@4026: if (strcmp(pass, _network_game_info.rcon_password) != 0) { truelight@1026: DEBUG(net, 0)("[RCon] Wrong password from client-id %d", cs->index); truelight@1026: return; truelight@1026: } truelight@1026: truelight@1026: DEBUG(net, 0)("[RCon] Client-id %d executed: %s", cs->index, command); truelight@1026: truelight@1026: _redirect_console_to_client = cs->index; truelight@1026: IConsoleCmdExec(command); truelight@1026: _redirect_console_to_client = 0; truelight@1026: return; truelight@1026: } truelight@1026: truelight@543: // The layout for the receive-functions by the server truelight@716: typedef void NetworkServerPacket(NetworkClientState *cs, Packet *p); truelight@543: truelight@543: truelight@543: // This array matches PacketType. At an incoming truelight@543: // packet it is matches against this array truelight@543: // and that way the right function to handle that truelight@543: // packet is found. truelight@543: static NetworkServerPacket* const _network_server_packet[] = { truelight@543: NULL, /*PACKET_SERVER_FULL,*/ truelight@841: NULL, /*PACKET_SERVER_BANNED,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_JOIN), truelight@543: NULL, /*PACKET_SERVER_ERROR,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO), truelight@543: NULL, /*PACKET_SERVER_COMPANY_INFO,*/ truelight@543: NULL, /*PACKET_SERVER_CLIENT_INFO,*/ truelight@543: NULL, /*PACKET_SERVER_NEED_PASSWORD,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD), truelight@543: NULL, /*PACKET_SERVER_WELCOME,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_GETMAP), truelight@543: NULL, /*PACKET_SERVER_WAIT,*/ truelight@543: NULL, /*PACKET_SERVER_MAP,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK), truelight@543: NULL, /*PACKET_SERVER_JOIN,*/ truelight@543: NULL, /*PACKET_SERVER_FRAME,*/ truelight@543: NULL, /*PACKET_SERVER_SYNC,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_ACK), truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_COMMAND), truelight@543: NULL, /*PACKET_SERVER_COMMAND,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_CHAT), truelight@543: NULL, /*PACKET_SERVER_CHAT,*/ truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD), truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME), truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_QUIT), truelight@543: RECEIVE_COMMAND(PACKET_CLIENT_ERROR), truelight@543: NULL, /*PACKET_SERVER_QUIT,*/ truelight@543: NULL, /*PACKET_SERVER_ERROR_QUIT,*/ truelight@543: NULL, /*PACKET_SERVER_SHUTDOWN,*/ truelight@543: NULL, /*PACKET_SERVER_NEWGAME,*/ truelight@1026: NULL, /*PACKET_SERVER_RCON,*/ truelight@1026: RECEIVE_COMMAND(PACKET_CLIENT_RCON), rubidium@5424: NULL, /*PACKET_CLIENT_CHECK_NEWGRFS,*/ rubidium@5424: RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED), truelight@543: }; truelight@543: truelight@543: // If this fails, check the array above with network_data.h truelight@543: assert_compile(lengthof(_network_server_packet) == PACKET_END); truelight@543: truelight@543: // This update the company_info-stuff truelight@543: void NetworkPopulateCompanyInfo(void) truelight@543: { truelight@543: char password[NETWORK_PASSWORD_LENGTH]; Darkvater@4880: const Player *p; Darkvater@4880: const Vehicle *v; Darkvater@4880: const Station *s; Darkvater@4880: const NetworkClientState *cs; Darkvater@4880: const NetworkClientInfo *ci; Darkvater@4880: uint i; truelight@690: uint16 months_empty; truelight@543: truelight@543: FOR_ALL_PLAYERS(p) { truelight@543: if (!p->is_active) { truelight@543: memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo)); truelight@543: continue; truelight@543: } truelight@543: truelight@543: // Clean the info but not the password truelight@543: ttd_strlcpy(password, _network_player_info[p->index].password, sizeof(password)); truelight@690: months_empty = _network_player_info[p->index].months_empty; truelight@543: memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo)); truelight@690: _network_player_info[p->index].months_empty = months_empty; truelight@543: ttd_strlcpy(_network_player_info[p->index].password, password, sizeof(_network_player_info[p->index].password)); truelight@543: truelight@543: // Grap the company name darkvater@1017: SetDParam(0, p->name_1); darkvater@1017: SetDParam(1, p->name_2); Darkvater@4912: GetString(_network_player_info[p->index].company_name, STR_JUST_STRING, lastof(_network_player_info[p->index].company_name)); truelight@543: truelight@543: // Check the income rubidium@4329: if (_cur_year - 1 == p->inaugurated_year) { truelight@543: // The player is here just 1 year, so display [2], else display[1] Darkvater@4880: for (i = 0; i < lengthof(p->yearly_expenses[2]); i++) { truelight@543: _network_player_info[p->index].income -= p->yearly_expenses[2][i]; tron@4077: } tron@4077: } else { Darkvater@4880: for (i = 0; i < lengthof(p->yearly_expenses[1]); i++) { truelight@543: _network_player_info[p->index].income -= p->yearly_expenses[1][i]; tron@4077: } tron@4077: } truelight@543: truelight@543: // Set some general stuff truelight@543: _network_player_info[p->index].inaugurated_year = p->inaugurated_year; truelight@543: _network_player_info[p->index].company_value = p->old_economy[0].company_value; truelight@543: _network_player_info[p->index].money = p->money64; truelight@543: _network_player_info[p->index].performance = p->old_economy[0].performance_history; truelight@543: } truelight@543: truelight@543: // Go through all vehicles and count the type of vehicles truelight@543: FOR_ALL_VEHICLES(v) { Darkvater@4850: if (!IsValidPlayer(v->owner)) continue; Darkvater@4880: tron@4077: switch (v->type) { tron@4077: case VEH_Train: Darkvater@4880: if (IsFrontEngine(v)) _network_player_info[v->owner].num_vehicle[0]++; tron@4077: break; tron@4077: tron@4077: case VEH_Road: tron@4077: if (v->cargo_type != CT_PASSENGERS) { tron@4077: _network_player_info[v->owner].num_vehicle[1]++; tron@4077: } else { tron@4077: _network_player_info[v->owner].num_vehicle[2]++; tron@4077: } tron@4077: break; tron@4077: tron@4077: case VEH_Aircraft: Darkvater@4880: if (v->subtype <= 2) _network_player_info[v->owner].num_vehicle[3]++; tron@4077: break; tron@4077: tron@4077: case VEH_Ship: tron@4077: _network_player_info[v->owner].num_vehicle[4]++; tron@4077: break; tron@4077: tron@4077: case VEH_Special: tron@4077: case VEH_Disaster: tron@4077: break; tron@4077: } truelight@543: } truelight@543: truelight@543: // Go through all stations and count the types of stations truelight@543: FOR_ALL_STATIONS(s) { Darkvater@4850: if (IsValidPlayer(s->owner)) { Darkvater@4880: NetworkPlayerInfo *npi = &_network_player_info[s->owner]; tron@4077: tron@4077: if (s->facilities & FACIL_TRAIN) npi->num_station[0]++; tron@4077: if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[1]++; tron@4077: if (s->facilities & FACIL_BUS_STOP) npi->num_station[2]++; tron@4077: if (s->facilities & FACIL_AIRPORT) npi->num_station[3]++; tron@4077: if (s->facilities & FACIL_DOCK) npi->num_station[4]++; truelight@543: } truelight@543: } truelight@543: truelight@543: ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); truelight@543: // Register local player (if not dedicated) Darkvater@4878: if (ci != NULL && IsValidPlayer(ci->client_playas)) Darkvater@4878: ttd_strlcpy(_network_player_info[ci->client_playas].players, ci->client_name, sizeof(_network_player_info[0].players)); truelight@543: truelight@543: FOR_ALL_CLIENTS(cs) { truelight@793: char client_name[NETWORK_CLIENT_NAME_LENGTH]; truelight@543: truelight@543: NetworkGetClientName(client_name, sizeof(client_name), cs); truelight@543: truelight@543: ci = DEREF_CLIENT_INFO(cs); Darkvater@4878: if (ci != NULL && IsValidPlayer(ci->client_playas)) { Darkvater@4878: if (strlen(_network_player_info[ci->client_playas].players) != 0) Darkvater@4878: ttd_strlcat(_network_player_info[ci->client_playas].players, ", ", lengthof(_network_player_info[0].players)); truelight@668: Darkvater@4878: ttd_strlcat(_network_player_info[ci->client_playas].players, client_name, lengthof(_network_player_info[0].players)); truelight@543: } truelight@543: } truelight@543: } truelight@543: truelight@543: // Send a packet to all clients with updated info about this client_index truelight@543: void NetworkUpdateClientInfo(uint16 client_index) truelight@543: { truelight@716: NetworkClientState *cs; Darkvater@4880: NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_index); truelight@543: tron@4077: if (ci == NULL) return; truelight@656: truelight@543: FOR_ALL_CLIENTS(cs) { truelight@543: SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci); truelight@543: } truelight@543: } truelight@543: truelight@785: /* Check if we want to restart the map */ tron@1093: static void NetworkCheckRestartMap(void) truelight@785: { rubidium@4293: if (_network_restart_game_year != 0 && _cur_year >= _network_restart_game_year) { rubidium@4293: DEBUG(net, 0)("Auto-restarting map. Year %d reached.", _cur_year); truelight@785: truelight@4300: StartNewGameWithoutGUI(GENERATE_NEW_SEED); truelight@785: } truelight@785: } truelight@785: truelight@690: /* Check if the server has autoclean_companies activated truelight@690: Two things happen: truelight@690: 1) If a company is not protected, it is closed after 1 year (for example) truelight@690: 2) If a company is protected, protection is disabled after 3 years (for example) truelight@690: (and item 1. happens a year later) */ tron@1093: static void NetworkAutoCleanCompanies(void) truelight@690: { Darkvater@4880: const NetworkClientState *cs; Darkvater@4880: const NetworkClientInfo *ci; Darkvater@4880: const Player *p; truelight@690: bool clients_in_company[MAX_PLAYERS]; truelight@690: tron@4077: if (!_network_autoclean_companies) return; truelight@690: truelight@690: memset(clients_in_company, 0, sizeof(clients_in_company)); truelight@690: truelight@690: /* Detect the active companies */ truelight@690: FOR_ALL_CLIENTS(cs) { truelight@690: ci = DEREF_CLIENT_INFO(cs); Darkvater@4878: if (IsValidPlayer(ci->client_playas)) clients_in_company[ci->client_playas] = true; truelight@690: } Darkvater@4880: truelight@690: if (!_network_dedicated) { truelight@690: ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); Darkvater@4878: if (IsValidPlayer(ci->client_playas)) clients_in_company[ci->client_playas] = true; truelight@690: } truelight@690: truelight@690: /* Go through all the comapnies */ truelight@690: FOR_ALL_PLAYERS(p) { truelight@690: /* Skip the non-active once */ tron@4077: if (!p->is_active || p->is_ai) continue; truelight@690: truelight@690: if (!clients_in_company[p->index]) { truelight@690: /* The company is empty for one month more */ truelight@690: _network_player_info[p->index].months_empty++; truelight@690: truelight@690: /* Is the company empty for autoclean_unprotected-months, and is there no protection? */ truelight@690: if (_network_player_info[p->index].months_empty > _network_autoclean_unprotected && _network_player_info[p->index].password[0] == '\0') { truelight@690: /* Shut the company down */ truelight@690: DoCommandP(0, 2, p->index, NULL, CMD_PLAYER_CTRL); Darkvater@4880: IConsolePrintF(_icolour_def, "Auto-cleaned company #%d", p->index + 1); truelight@690: } truelight@690: /* Is the compnay empty for autoclean_protected-months, and there is a protection? */ truelight@690: if (_network_player_info[p->index].months_empty > _network_autoclean_protected && _network_player_info[p->index].password[0] != '\0') { truelight@690: /* Unprotect the company */ truelight@690: _network_player_info[p->index].password[0] = '\0'; Darkvater@1805: IConsolePrintF(_icolour_def, "Auto-removed protection from company #%d", p->index+1); truelight@690: _network_player_info[p->index].months_empty = 0; truelight@690: } truelight@690: } else { truelight@690: /* It is not empty, reset the date */ truelight@690: _network_player_info[p->index].months_empty = 0; truelight@690: } truelight@690: } truelight@690: } truelight@690: truelight@543: // This function changes new_name to a name that is unique (by adding #1 ...) truelight@543: // and it returns true if that succeeded. truelight@793: bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]) truelight@543: { truelight@716: NetworkClientState *new_cs; truelight@543: bool found_name = false; truelight@543: byte number = 0; truelight@793: char original_name[NETWORK_CLIENT_NAME_LENGTH]; truelight@543: Darkvater@3623: // We use NETWORK_CLIENT_NAME_LENGTH in here, because new_name is really a pointer truelight@793: ttd_strlcpy(original_name, new_name, NETWORK_CLIENT_NAME_LENGTH); truelight@543: truelight@543: while (!found_name) { Darkvater@4880: const NetworkClientInfo *ci; Darkvater@4880: truelight@543: found_name = true; truelight@543: FOR_ALL_CLIENTS(new_cs) { truelight@543: ci = DEREF_CLIENT_INFO(new_cs); tron@4026: if (strcmp(ci->client_name, new_name) == 0) { truelight@543: // Name already in use truelight@543: found_name = false; truelight@543: break; truelight@543: } truelight@543: } truelight@543: // Check if it is the same as the server-name truelight@543: ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); truelight@543: if (ci != NULL) { Darkvater@4880: if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use truelight@543: } truelight@543: truelight@543: if (!found_name) { truelight@543: // Try a new name ( #1, #2, and so on) truelight@543: miham@826: // Stop if we tried for more than 50 times.. truelight@543: if (number++ > 50) break; truelight@793: snprintf(new_name, NETWORK_CLIENT_NAME_LENGTH, "%s #%d", original_name, number); truelight@543: } truelight@543: } truelight@543: truelight@543: return found_name; truelight@543: } truelight@543: truelight@543: // Reads a packet from the stream truelight@716: bool NetworkServer_ReadPackets(NetworkClientState *cs) truelight@543: { truelight@543: Packet *p; truelight@543: NetworkRecvStatus res; tron@2952: while ((p = NetworkRecv_Packet(cs, &res)) != NULL) { truelight@903: byte type = NetworkRecv_uint8(cs, p); Darkvater@4880: if (type < PACKET_END && _network_server_packet[type] != NULL && !cs->has_quit) { truelight@543: _network_server_packet[type](cs, p); tron@4077: } else { truelight@543: DEBUG(net, 0)("[NET][Server] Received invalid packet type %d", type); tron@4077: } truelight@543: free(p); truelight@543: } truelight@543: truelight@543: return true; truelight@543: } truelight@543: truelight@543: // Handle the local command-queue tron@2817: static void NetworkHandleCommandQueue(NetworkClientState* cs) tron@2817: { ludde@2081: CommandPacket *cp; truelight@543: ludde@2081: while ( (cp = cs->command_queue) != NULL) { ludde@2081: SEND_COMMAND(PACKET_SERVER_COMMAND)(cs, cp); ludde@2081: ludde@2081: cs->command_queue = cp->next; ludde@2081: free(cp); truelight@543: } truelight@543: } truelight@543: truelight@543: // This is called every tick if this is a _network_server ludde@2235: void NetworkServer_Tick(bool send_frame) truelight@543: { truelight@716: NetworkClientState *cs; ludde@2079: #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME ludde@2079: bool send_sync = false; ludde@2079: #endif truelight@543: ludde@2079: #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME ludde@2079: if (_frame_counter >= _last_sync_frame + _network_sync_freq) { ludde@2079: _last_sync_frame = _frame_counter; ludde@2079: send_sync = true; ludde@2079: } ludde@2079: #endif ludde@2079: truelight@543: // Now we are done with the frame, inform the clients that they can truelight@543: // do their frame! truelight@543: FOR_ALL_CLIENTS(cs) { truelight@543: // Check if the speed of the client is what we can expect from a client truelight@543: if (cs->status == STATUS_ACTIVE) { truelight@543: // 1 lag-point per day truelight@543: int lag = NetworkCalculateLag(cs) / DAY_TICKS; truelight@543: if (lag > 0) { truelight@543: if (lag > 3) { truelight@543: // Client did still not report in after 4 game-day, drop him truelight@543: // (that is, the 3 of above, + 1 before any lag is counted) Darkvater@1805: IConsolePrintF(_icolour_err,"Client #%d is dropped because the client did not respond for more than 4 game-days", cs->index); truelight@716: NetworkCloseClient(cs); truelight@543: continue; truelight@543: } truelight@543: truelight@543: // Report once per time we detect the lag truelight@543: if (cs->lag_test == 0) { Darkvater@1805: IConsolePrintF(_icolour_warn,"[%d] Client #%d is slow, try increasing *net_frame_freq to a higher value!", _frame_counter, cs->index); truelight@543: cs->lag_test = 1; truelight@543: } truelight@543: } else { truelight@543: cs->lag_test = 0; truelight@543: } truelight@1602: } else if (cs->status == STATUS_PRE_ACTIVE) { truelight@1602: int lag = NetworkCalculateLag(cs); truelight@1602: if (lag > _network_max_join_time) { Darkvater@1805: IConsolePrintF(_icolour_err,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->index, _network_max_join_time); truelight@1602: NetworkCloseClient(cs); truelight@1602: } truelight@543: } truelight@543: ludde@2079: if (cs->status >= STATUS_PRE_ACTIVE) { ludde@2079: // Check if we can send command, and if we have anything in the queue truelight@543: NetworkHandleCommandQueue(cs); truelight@543: ludde@2079: // Send an updated _frame_counter_max to the client Darkvater@4880: if (send_frame) SEND_COMMAND(PACKET_SERVER_FRAME)(cs); truelight@543: truelight@543: #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME ludde@2079: // Send a sync-check packet Darkvater@4880: if (send_sync) SEND_COMMAND(PACKET_SERVER_SYNC)(cs); ludde@2079: #endif ludde@2079: } truelight@543: } truelight@668: truelight@668: /* See if we need to advertise */ truelight@668: NetworkUDPAdvertise(); truelight@543: } truelight@543: truelight@785: void NetworkServerYearlyLoop(void) truelight@785: { truelight@785: NetworkCheckRestartMap(); truelight@785: } truelight@785: truelight@716: void NetworkServerMonthlyLoop(void) truelight@690: { truelight@690: NetworkAutoCleanCompanies(); truelight@690: } truelight@690: truelight@543: #endif /* ENABLE_NETWORK */