tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file network_server.cpp Server part of the network protocol. */ rubidium@9111: Darkvater@4826: #ifdef ENABLE_NETWORK Darkvater@4826: rubidium@5469: #include "../stdafx.h" rubidium@5469: #include "../openttd.h" // XXX StringID rubidium@5469: #include "../debug.h" rubidium@8114: #include "../strings_func.h" rubidium@9428: #include "network_internal.h" rubidium@5469: #include "core/tcp.h" glx@8280: #include "../vehicle_base.h" glx@8280: #include "../vehicle_func.h" rubidium@8140: #include "../date_func.h" truelight@543: #include "network_server.h" truelight@668: #include "network_udp.h" rubidium@9336: #include "../console_func.h" rubidium@8116: #include "../command_func.h" rubidium@5469: #include "../saveload.h" rubidium@8785: #include "../station_base.h" rubidium@5469: #include "../variables.h" rubidium@5469: #include "../genworld.h" rubidium@8130: #include "../core/alloc_func.hpp" rubidium@10039: #include "../fileio_func.h" rubidium@8214: #include "../string_func.h" rubidium@10208: #include "../company_base.h" rubidium@10208: #include "../company_func.h" rubidium@10208: #include "../company_gui.h" rubidium@8270: #include "../settings_type.h" truelight@543: rubidium@8264: #include "table/strings.h" rubidium@8264: truelight@543: // This file handles all the server-commands truelight@543: rubidium@5624: static void NetworkHandleCommandQueue(NetworkTCPSocketHandler* cs); truelight@716: truelight@543: // ********** truelight@543: // Sending functions rubidium@5624: // DEF_SERVER_SEND_COMMAND has parameter: NetworkTCPSocketHandler *cs truelight@543: // ********** truelight@543: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkTCPSocketHandler *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) rubidium@10207: // uint8: As which company the client is playing truelight@543: // String: The name 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); rubidium@5900: p->Send_uint16(ci->client_index); rubidium@5900: p->Send_uint8 (ci->client_playas); rubidium@5900: p->Send_string(ci->client_name); truelight@543: rubidium@5902: cs->Send_Packet(p); 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: rubidium@10207: Company *company; truelight@543: Packet *p; truelight@543: rubidium@10207: byte active = ActiveCompanyCount(); truelight@543: truelight@543: if (active == 0) { Darkvater@4880: p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); truelight@543: rubidium@5900: p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION); rubidium@5900: p->Send_uint8 (active); truelight@543: rubidium@5902: cs->Send_Packet(p); truelight@543: return; truelight@543: } truelight@543: truelight@543: NetworkPopulateCompanyInfo(); truelight@543: rubidium@10207: FOR_ALL_COMPANIES(company) { truelight@543: p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); truelight@543: rubidium@5900: p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION); rubidium@5900: p->Send_uint8 (active); rubidium@10207: p->Send_uint8 (company->index); truelight@543: rubidium@10207: p->Send_string(_network_company_info[company->index].company_name); rubidium@10207: p->Send_uint32(_network_company_info[company->index].inaugurated_year); rubidium@10207: p->Send_uint64(_network_company_info[company->index].company_value); rubidium@10207: p->Send_uint64(_network_company_info[company->index].money); rubidium@10207: p->Send_uint64(_network_company_info[company->index].income); rubidium@10207: p->Send_uint16(_network_company_info[company->index].performance); truelight@543: truelight@1011: /* Send 1 if there is a passord for the company else send 0 */ rubidium@10207: p->Send_bool(!StrEmpty(_network_company_info[company->index].password)); truelight@1011: tron@4077: for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) { rubidium@10207: p->Send_uint16(_network_company_info[company->index].num_vehicle[i]); tron@4077: } truelight@543: tron@4077: for (i = 0; i < NETWORK_STATION_TYPES; i++) { rubidium@10207: p->Send_uint16(_network_company_info[company->index].num_station[i]); tron@4077: } truelight@543: rubidium@10207: if (StrEmpty(_network_company_info[company->index].clients)) { rubidium@5900: p->Send_string(""); tron@4077: } else { rubidium@10207: p->Send_string(_network_company_info[company->index].clients); tron@4077: } truelight@543: rubidium@5902: cs->Send_Packet(p); truelight@543: } truelight@734: truelight@734: p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); truelight@734: rubidium@5900: p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION); rubidium@5900: p->Send_uint8 (0); truelight@734: rubidium@5902: cs->Send_Packet(p); truelight@543: } truelight@543: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkTCPSocketHandler *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: rubidium@5900: p->Send_uint8(error); rubidium@5902: cs->Send_Packet(p); 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) { rubidium@5624: NetworkTCPSocketHandler *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@5380: DEBUG(net, 1, "'%s' made an error and has been disconnected. Reason: '%s'", client_name, str); truelight@543: rubidium@9337: NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, 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@5380: DEBUG(net, 1, "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 rubidium@5902: cs->Send_Packets(); truelight@543: truelight@543: // The client made a mistake, so drop his connection now! truelight@716: NetworkCloseClient(cs); truelight@543: } truelight@543: rubidium@5872: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHECK_NEWGRFS)(NetworkTCPSocketHandler *cs) rubidium@5872: { rubidium@5872: // rubidium@5872: // Packet: PACKET_SERVER_CHECK_NEWGRFS rubidium@5872: // Function: Sends info about the used GRFs to the client rubidium@5872: // Data: rubidium@5872: // uint8: Amount of GRFs rubidium@5872: // And then for each GRF: rubidium@5872: // uint32: GRF ID rubidium@5872: // 16 * uint8: MD5 checksum of the GRF rubidium@5872: // rubidium@5872: rubidium@5872: Packet *p = NetworkSend_Init(PACKET_SERVER_CHECK_NEWGRFS); rubidium@5872: const GRFConfig *c; rubidium@5872: uint grf_count = 0; rubidium@5872: rubidium@7140: for (c = _grfconfig; c != NULL; c = c->next) { skidd13@7928: if (!HasBit(c->flags, GCF_STATIC)) grf_count++; rubidium@7140: } rubidium@5872: rubidium@5900: p->Send_uint8 (grf_count); rubidium@5872: for (c = _grfconfig; c != NULL; c = c->next) { skidd13@7928: if (!HasBit(c->flags, GCF_STATIC)) cs->Send_GRFIdentifier(p, c); rubidium@5872: } rubidium@5872: rubidium@5902: cs->Send_Packet(p); rubidium@5872: } rubidium@5872: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(NetworkTCPSocketHandler *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: rubidium@6235: /* Invalid packet when status is AUTH or higher */ rubidium@6235: if (cs->status >= STATUS_AUTH) return; rubidium@6235: rubidium@6235: cs->status = STATUS_AUTHORIZING; rubidium@6235: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_NEED_PASSWORD); rubidium@5900: p->Send_uint8(type); rubidium@9413: p->Send_uint32(_settings_game.game_creation.generation_seed); rubidium@9420: p->Send_string(_settings_client.network.network_id); rubidium@5902: cs->Send_Packet(p); 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; rubidium@5624: NetworkTCPSocketHandler *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); rubidium@5900: p->Send_uint16(cs->index); rubidium@9413: p->Send_uint32(_settings_game.game_creation.generation_seed); rubidium@9420: p->Send_string(_settings_client.network.network_id); rubidium@5902: cs->Send_Packet(p); 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; rubidium@5624: NetworkTCPSocketHandler *new_cs; truelight@543: Packet *p; truelight@543: rubidium@10207: // Count how many clients 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); rubidium@5900: p->Send_uint8(waiting); rubidium@5902: cs->Send_Packet(p); 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: rubidium@10207: // nothing 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) { rubidium@6929: const char *filename = "network_server.tmp"; truelight@543: Packet *p; truelight@543: truelight@543: // Make a dump of the current game glx@9470: if (SaveOrLoad(filename, SL_SAVE, AUTOSAVE_DIR) != SL_OK) usererror("network savedump failed"); truelight@543: rubidium@6929: file_pointer = FioFOpenFile(filename, "rb", AUTOSAVE_DIR); truelight@543: fseek(file_pointer, 0, SEEK_END); truelight@543: glx@9470: if (ftell(file_pointer) == 0) usererror("network savedump failed - zero sized savegame?"); rubidium@5705: truelight@543: // Now send the _frame_counter and how many packets are coming truelight@543: p = NetworkSend_Init(PACKET_SERVER_MAP); rubidium@5900: p->Send_uint8 (MAP_PACKET_START); rubidium@5900: p->Send_uint32(_frame_counter); rubidium@5900: p->Send_uint32(ftell(file_pointer)); rubidium@5902: cs->Send_Packet(p); 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); rubidium@5900: p->Send_uint8(MAP_PACKET_NORMAL); truelight@4321: res = (int)fread(p->buffer + p->size, 1, SEND_MTU - p->size, file_pointer); Darkvater@4880: glx@9470: if (ferror(file_pointer)) usererror("Error reading temporary network savegame!"); Darkvater@4880: truelight@543: p->size += res; rubidium@5902: cs->Send_Packet(p); truelight@543: if (feof(file_pointer)) { truelight@543: // Done reading! Darkvater@3121: Packet *p = NetworkSend_Init(PACKET_SERVER_MAP); rubidium@5900: p->Send_uint8(MAP_PACKET_END); rubidium@5902: cs->Send_Packet(p); 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: { rubidium@5624: NetworkTCPSocketHandler *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 rubidium@5902: cs->Send_Packets(); rubidium@5902: if (cs->IsPacketQueueEmpty()) { 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: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkTCPSocketHandler *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: rubidium@5900: p->Send_uint16(client_index); truelight@543: rubidium@5902: cs->Send_Packet(p); 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); rubidium@5900: p->Send_uint32(_frame_counter); rubidium@5900: p->Send_uint32(_frame_counter_max); truelight@543: #ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME rubidium@5900: p->Send_uint32(_sync_seed_1); truelight@543: #ifdef NETWORK_SEND_DOUBLE_SEED rubidium@5900: p->Send_uint32(_sync_seed_2); truelight@543: #endif truelight@543: #endif rubidium@5902: cs->Send_Packet(p); 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); rubidium@5900: p->Send_uint32(_frame_counter); rubidium@5900: p->Send_uint32(_sync_seed_1); truelight@543: truelight@543: #ifdef NETWORK_SEND_DOUBLE_SEED rubidium@5900: p->Send_uint32(_sync_seed_2); truelight@543: #endif rubidium@5902: cs->Send_Packet(p); truelight@543: } truelight@543: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkTCPSocketHandler *cs, CommandPacket *cp) truelight@543: { truelight@543: // truelight@543: // Packet: SERVER_COMMAND truelight@543: // Function: Sends a DoCommand to the client truelight@543: // Data: rubidium@10207: // uint8: CompanyID (0..MAX_COMPANIES-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: rubidium@10207: p->Send_uint8 (cp->company); rubidium@5900: p->Send_uint32(cp->cmd); rubidium@5900: p->Send_uint32(cp->p1); rubidium@5900: p->Send_uint32(cp->p2); rubidium@5900: p->Send_uint32(cp->tile); rubidium@5900: p->Send_string(cp->text); rubidium@5900: p->Send_uint8 (cp->callback); rubidium@5900: p->Send_uint32(cp->frame); rubidium@7222: p->Send_bool (cp->my_cmd); truelight@543: rubidium@5902: cs->Send_Packet(p); truelight@543: } truelight@543: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkTCPSocketHandler *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 rubidium@9896: // String: Message (max NETWORK_CHAT_LENGTH) truelight@543: // truelight@543: truelight@543: Packet *p = NetworkSend_Init(PACKET_SERVER_CHAT); truelight@543: rubidium@5900: p->Send_uint8 (action); rubidium@5900: p->Send_uint16(client_index); rubidium@5918: p->Send_bool (self_send); rubidium@5900: p->Send_string(msg); truelight@543: rubidium@5902: cs->Send_Packet(p); truelight@543: } truelight@543: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkTCPSocketHandler *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: rubidium@5900: p->Send_uint16(client_index); rubidium@5900: p->Send_uint8 (errorno); truelight@543: rubidium@5902: cs->Send_Packet(p); truelight@543: } truelight@543: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkTCPSocketHandler *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: rubidium@5900: p->Send_uint16(client_index); rubidium@5900: p->Send_string(leavemsg); truelight@543: rubidium@5902: cs->Send_Packet(p); 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); rubidium@5902: cs->Send_Packet(p); 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); rubidium@5902: cs->Send_Packet(p); truelight@543: } truelight@543: rubidium@5624: DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_RCON)(NetworkTCPSocketHandler *cs, uint16 color, const char *command) truelight@1026: { truelight@1026: Packet *p = NetworkSend_Init(PACKET_SERVER_RCON); truelight@1026: rubidium@5900: p->Send_uint16(color); rubidium@5900: p->Send_string(command); rubidium@5902: cs->Send_Packet(p); truelight@1026: } truelight@1026: truelight@543: // ********** truelight@543: // Receiving functions rubidium@5624: // DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkTCPSocketHandler *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@5872: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED) rubidium@5872: { rubidium@9474: if (cs->status != STATUS_INACTIVE) { rubidium@9474: /* Illegal call, return error and ignore the packet */ rubidium@9474: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); rubidium@9474: return; rubidium@9474: } rubidium@9474: rubidium@5872: NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); rubidium@5872: rubidium@5872: /* We now want a password from the client else we do not allow him in! */ rubidium@9451: if (!StrEmpty(_settings_client.network.server_password)) { rubidium@5872: SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD); rubidium@5872: } else { rubidium@10207: if (IsValidCompanyID(ci->client_playas) && _network_company_info[ci->client_playas].password[0] != '\0') { rubidium@5872: SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD); rubidium@5872: } else { rubidium@5872: SEND_COMMAND(PACKET_SERVER_WELCOME)(cs); rubidium@5872: } rubidium@5872: } rubidium@5872: } rubidium@5872: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN) truelight@543: { rubidium@9474: if (cs->status != STATUS_INACTIVE) { rubidium@9474: /* Illegal call, return error and ignore the packet */ rubidium@9474: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); rubidium@9474: return; rubidium@9474: } rubidium@9474: Darkvater@3623: char name[NETWORK_CLIENT_NAME_LENGTH]; truelight@7807: char unique_id[NETWORK_UNIQUE_ID_LENGTH]; truelight@543: NetworkClientInfo *ci; rubidium@10207: CompanyID playas; truelight@543: NetworkLanguage client_lang; truelight@543: char client_revision[NETWORK_REVISION_LENGTH]; truelight@543: rubidium@5900: p->Recv_string(client_revision, sizeof(client_revision)); truelight@543: darkvater@659: // Check if the client has revision control enabled rubidium@6178: if (!IsNetworkCompatibleVersion(client_revision)) { tron@4077: // Different revisions!! tron@4077: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_REVISION); tron@4077: return; truelight@543: } truelight@543: rubidium@5900: p->Recv_string(name, sizeof(name)); rubidium@5900: playas = (Owner)p->Recv_uint8(); rubidium@5900: client_lang = (NetworkLanguage)p->Recv_uint8(); rubidium@5900: p->Recv_string(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) { rubidium@10207: case COMPANY_NEW_COMPANY: /* New company */ rubidium@10207: if (ActiveCompanyCount() >= _settings_client.network.max_companies) { Darkvater@2879: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_FULL); Darkvater@2879: return; Darkvater@2879: } Darkvater@2879: break; rubidium@10207: case COMPANY_SPECTATOR: /* Spectator */ rubidium@9451: if (NetworkSpectatorCount() >= _settings_client.network.max_spectators) { 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)) */ rubidium@10207: if (!IsValidCompanyID(playas)) { rubidium@10207: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH); Darkvater@4861: return; Darkvater@4861: } Darkvater@4861: break; truelight@543: } truelight@543: Darkvater@2879: // We need a valid name.. make it Player rubidium@10207: if (StrEmpty(name)) ttd_strlcpy(name, "Player", sizeof(name)); Darkvater@2879: Darkvater@2879: if (!NetworkFindName(name)) { // Change name if duplicate rubidium@10207: // We could not create a name for this client 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 */ rubidium@10207: if (IsValidCompanyID(playas)) _network_company_info[playas].months_empty = 0; rubidium@5872: rubidium@5872: if (_grfconfig == NULL) { rubidium@5872: RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL); rubidium@5872: } else { rubidium@5872: SEND_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)(cs); rubidium@5872: } 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: rubidium@5900: type = (NetworkPasswordType)p->Recv_uint8(); rubidium@5900: p->Recv_string(password, sizeof(password)); truelight@543: rubidium@6252: if (cs->status == STATUS_AUTHORIZING && type == NETWORK_GAME_PASSWORD) { truelight@543: // Check game-password rubidium@9451: if (strcmp(password, _settings_client.network.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: rubidium@10207: if (IsValidCompanyID(ci->client_playas) && _network_company_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; rubidium@6252: } else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) { truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: rubidium@10207: if (strcmp(password, _network_company_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: { rubidium@5624: NetworkTCPSocketHandler *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]; rubidium@5624: NetworkTCPSocketHandler *new_cs; truelight@543: truelight@543: NetworkGetClientName(client_name, sizeof(client_name), cs); truelight@543: rubidium@9337: NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, 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: rubidium@9420: if (_settings_client.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: rubidium@9428: NetworkServerSendChat(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) { rubidium@10207: IConsolePrintF(CC_ERROR, "WARNING: server only command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci)); Darkvater@1804: return false; Darkvater@1804: } Darkvater@1804: Darkvater@1804: if (flags & CMD_OFFLINE) { rubidium@10207: IConsolePrintF(CC_ERROR, "WARNING: offline only command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci)); Darkvater@1804: return false; Darkvater@1804: } Darkvater@4880: rubidium@10207: if (cp->cmd != CMD_COMPANY_CTRL && !IsValidCompanyID(cp->company) && ci->client_index != NETWORK_SERVER_INDEX) { rubidium@10207: IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci)); rubidium@7495: return false; rubidium@7495: } rubidium@7495: 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: { rubidium@5624: NetworkTCPSocketHandler *new_cs; Darkvater@1804: const NetworkClientInfo *ci; truelight@543: byte callback; 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: rubidium@6836: CommandPacket *cp = MallocT(1); rubidium@10207: cp->company = (CompanyID)p->Recv_uint8(); rubidium@10207: cp->cmd = p->Recv_uint32(); rubidium@10207: cp->p1 = p->Recv_uint32(); rubidium@10207: cp->p2 = p->Recv_uint32(); rubidium@10207: cp->tile = p->Recv_uint32(); rubidium@5900: p->Recv_string(cp->text, lengthof(cp->text)); truelight@543: rubidium@5900: callback = p->Recv_uint8(); truelight@903: rubidium@6836: if (cs->has_quit) { rubidium@6836: free(cp); rubidium@6836: return; rubidium@6836: } 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)) { rubidium@10207: IConsolePrintF(CC_ERROR, "WARNING: invalid command from client %d (IP: %s).", ci->client_index, GetClientIP(ci)); truelight@903: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); rubidium@6836: free(cp); truelight@903: return; truelight@903: } truelight@543: Darkvater@1804: if (!CheckCommandFlags(cp, ci)) { Darkvater@1804: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_KICKED); rubidium@6836: free(cp); Darkvater@1804: return; Darkvater@1804: } Darkvater@1804: rubidium@10207: /** Only CMD_COMPANY_CTRL is always allowed, for the rest, playas needs rubidium@10207: * to match the company 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: */ rubidium@10207: if (!(cp->cmd == CMD_COMPANY_CTRL && cp->p1 == 0 && ci->client_playas == COMPANY_NEW_COMPANY) && ci->client_playas != cp->company) { rubidium@10207: IConsolePrintF(CC_ERROR, "WARNING: client %d (IP: %s) tried to execute a command as company %d, kicking...", rubidium@10207: ci->client_playas + 1, GetClientIP(ci), cp->company + 1); rubidium@10207: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH); rubidium@6836: free(cp); truelight@543: return; truelight@543: } truelight@748: rubidium@10207: /** @todo CMD_COMPANY_CTRL with p1 = 0 announces a new company to the server. To give the rubidium@10207: * company 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. rubidium@10207: * @see CmdCompanyCtrl() Darkvater@1804: */ rubidium@10207: if (cp->cmd == CMD_COMPANY_CTRL) { Darkvater@1804: if (cp->p1 != 0) { Darkvater@1804: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_CHEATER); rubidium@6836: free(cp); Darkvater@1804: return; Darkvater@1804: } Darkvater@1804: rubidium@10207: /* XXX - Execute the command as a valid company. 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... */ rubidium@10207: cp->company = OWNER_BEGIN; rubidium@10207: cp->p2 = cs - _clients; // XXX - UGLY! p2 is mis-used to get the client-id in CmdCompanyCtrl 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) { rubidium@5381: 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; rubidium@7222: cp->my_cmd = (new_cs == cs); truelight@543: NetworkAddCommandQueue(new_cs, cp); truelight@543: } truelight@543: } truelight@543: truelight@543: cp->callback = 0; rubidium@7222: cp->my_cmd = false; 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 rubidium@5624: NetworkTCPSocketHandler *new_cs; Darkvater@3437: char str[100]; Darkvater@4880: char client_name[NETWORK_CLIENT_NAME_LENGTH]; rubidium@5900: NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8(); 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: Darkvater@5380: DEBUG(net, 2, "'%s' reported an error and is closing its connection (%s)", client_name, str); truelight@543: rubidium@9337: NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, 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. rubidium@5624: NetworkTCPSocketHandler *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: rubidium@5900: p->Recv_string(str, lengthof(str)); truelight@543: truelight@543: NetworkGetClientName(client_name, sizeof(client_name), cs); truelight@543: rubidium@9337: NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, 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: { rubidium@9474: if (cs->status < STATUS_AUTH) { rubidium@9474: /* Illegal call, return error and ignore the packet */ rubidium@9474: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED); rubidium@9474: return; rubidium@9474: } rubidium@9474: rubidium@5900: uint32 frame = p->Recv_uint32(); 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: rubidium@9420: if (_settings_client.network.pause_on_join) { truelight@1602: DoCommandP(0, 0, 0, NULL, CMD_PAUSE); rubidium@9428: NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", NETWORK_SERVER_INDEX); truelight@1602: } peter1138@4713: rubidium@10207: CheckMinActiveClients(); 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: rubidium@9428: void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index) truelight@543: { rubidium@5624: NetworkTCPSocketHandler *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) rubidium@10207: NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(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) rubidium@10207: NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(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. rubidium@10207: /* Find all clients that belong to this company */ 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) { rubidium@10207: NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(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: rubidium@10207: /* There is no such client */ 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]; rubidium@10207: StringID str = IsValidCompanyID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS; peter1138@7060: SetDParam(0, ci_to->client_playas); Darkvater@4945: GetString(name, str, lastof(name)); rubidium@10207: NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(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: Darkvater@5380: DEBUG(net, 0, "[server] received unknown chat destination type %d. Doing broadcast instead", desttype); 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) rubidium@10207: NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(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: { rubidium@9474: if (cs->status < STATUS_AUTH) { rubidium@9474: /* Illegal call, return error and ignore the packet */ rubidium@9474: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED); rubidium@9474: return; rubidium@9474: } rubidium@9474: rubidium@5900: NetworkAction action = (NetworkAction)p->Recv_uint8(); rubidium@5900: DestType desttype = (DestType)p->Recv_uint8(); rubidium@6531: int dest = p->Recv_uint16(); rubidium@9896: char msg[NETWORK_CHAT_LENGTH]; truelight@543: rubidium@9896: p->Recv_string(msg, NETWORK_CHAT_LENGTH); truelight@543: glx@9675: const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); glx@9675: switch (action) { glx@9675: case NETWORK_ACTION_GIVE_MONEY: rubidium@10207: if (!IsValidCompanyID(ci->client_playas)) break; glx@9675: /* Fall-through */ glx@9675: case NETWORK_ACTION_CHAT: glx@9675: case NETWORK_ACTION_CHAT_CLIENT: glx@9675: case NETWORK_ACTION_CHAT_COMPANY: glx@9675: NetworkServerSendChat(action, desttype, dest, msg, cs->index); glx@9675: break; glx@9675: default: rubidium@10207: IConsolePrintF(CC_ERROR, "WARNING: invalid chat action from client %d (IP: %s).", ci->client_index, GetClientIP(ci)); glx@9675: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); glx@9675: break; glx@9675: } truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD) truelight@543: { rubidium@9474: if (cs->status != STATUS_ACTIVE) { rubidium@9474: /* Illegal call, return error and ignore the packet */ rubidium@9474: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); rubidium@9474: return; rubidium@9474: } rubidium@9474: truelight@543: char password[NETWORK_PASSWORD_LENGTH]; Darkvater@4880: const NetworkClientInfo *ci; truelight@543: rubidium@5900: p->Recv_string(password, sizeof(password)); truelight@543: ci = DEREF_CLIENT_INFO(cs); truelight@543: rubidium@10207: if (IsValidCompanyID(ci->client_playas)) { rubidium@10207: ttd_strlcpy(_network_company_info[ci->client_playas].password, password, sizeof(_network_company_info[0].password)); truelight@543: } truelight@543: } truelight@543: truelight@543: DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME) truelight@543: { rubidium@9474: if (cs->status != STATUS_ACTIVE) { rubidium@9474: /* Illegal call, return error and ignore the packet */ rubidium@9474: SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); rubidium@9474: return; rubidium@9474: } rubidium@9474: truelight@793: char client_name[NETWORK_CLIENT_NAME_LENGTH]; truelight@543: NetworkClientInfo *ci; truelight@543: rubidium@5900: p->Recv_string(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)) { rubidium@9337: NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, 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: rubidium@9451: if (StrEmpty(_settings_client.network.rcon_password)) return; truelight@1026: rubidium@5900: p->Recv_string(pass, sizeof(pass)); rubidium@5900: p->Recv_string(command, sizeof(command)); truelight@1026: rubidium@9451: if (strcmp(pass, _settings_client.network.rcon_password) != 0) { Darkvater@5380: DEBUG(net, 0, "[rcon] wrong password from client-id %d", cs->index); truelight@1026: return; truelight@1026: } truelight@1026: Darkvater@5380: 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 rubidium@5624: typedef void NetworkServerPacket(NetworkTCPSocketHandler *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@5872: NULL, /*PACKET_CLIENT_CHECK_NEWGRFS,*/ rubidium@5872: 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 rubidium@6247: void NetworkPopulateCompanyInfo() truelight@543: { truelight@543: char password[NETWORK_PASSWORD_LENGTH]; rubidium@10207: const Company *c; Darkvater@4880: const Vehicle *v; Darkvater@4880: const Station *s; rubidium@5624: NetworkTCPSocketHandler *cs; Darkvater@4880: const NetworkClientInfo *ci; Darkvater@4880: uint i; truelight@690: uint16 months_empty; truelight@543: rubidium@10207: for (CompanyID cid = COMPANY_FIRST; cid < MAX_COMPANIES; cid++) { rubidium@10207: if (!IsValidCompanyID(cid)) memset(&_network_company_info[cid], 0, sizeof(NetworkCompanyInfo)); rubidium@9663: } rubidium@9659: rubidium@10207: FOR_ALL_COMPANIES(c) { truelight@543: // Clean the info but not the password rubidium@10207: ttd_strlcpy(password, _network_company_info[c->index].password, sizeof(password)); rubidium@10207: months_empty = _network_company_info[c->index].months_empty; rubidium@10207: memset(&_network_company_info[c->index], 0, sizeof(NetworkCompanyInfo)); rubidium@10207: _network_company_info[c->index].months_empty = months_empty; rubidium@10207: ttd_strlcpy(_network_company_info[c->index].password, password, sizeof(_network_company_info[c->index].password)); truelight@543: truelight@543: // Grap the company name rubidium@10207: SetDParam(0, c->index); rubidium@10207: GetString(_network_company_info[c->index].company_name, STR_COMPANY_NAME, lastof(_network_company_info[c->index].company_name)); truelight@543: truelight@543: // Check the income rubidium@10207: if (_cur_year - 1 == c->inaugurated_year) { rubidium@10207: // The company is here just 1 year, so display [2], else display[1] rubidium@10207: for (i = 0; i < lengthof(c->yearly_expenses[2]); i++) { rubidium@10207: _network_company_info[c->index].income -= c->yearly_expenses[2][i]; tron@4077: } tron@4077: } else { rubidium@10207: for (i = 0; i < lengthof(c->yearly_expenses[1]); i++) { rubidium@10207: _network_company_info[c->index].income -= c->yearly_expenses[1][i]; tron@4077: } tron@4077: } truelight@543: truelight@543: // Set some general stuff rubidium@10207: _network_company_info[c->index].inaugurated_year = c->inaugurated_year; rubidium@10207: _network_company_info[c->index].company_value = c->old_economy[0].company_value; rubidium@10207: _network_company_info[c->index].money = c->money; rubidium@10207: _network_company_info[c->index].performance = c->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) { rubidium@10207: if (!IsValidCompanyID(v->owner) || !v->IsPrimaryVehicle()) continue; glx@8280: byte type = 0; tron@4077: switch (v->type) { glx@8280: case VEH_TRAIN: type = 0; break; glx@8280: case VEH_ROAD: type = (v->cargo_type != CT_PASSENGERS) ? 1 : 2; break; glx@8280: case VEH_AIRCRAFT: type = 3; break; glx@8280: case VEH_SHIP: type = 4; break; glx@8280: default: continue; tron@4077: } rubidium@10207: _network_company_info[v->owner].num_vehicle[type]++; truelight@543: } truelight@543: truelight@543: // Go through all stations and count the types of stations truelight@543: FOR_ALL_STATIONS(s) { rubidium@10207: if (IsValidCompanyID(s->owner)) { rubidium@10207: NetworkCompanyInfo *npi = &_network_company_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); rubidium@10207: // Register local company (if not dedicated) rubidium@10207: if (ci != NULL && IsValidCompanyID(ci->client_playas)) rubidium@10207: ttd_strlcpy(_network_company_info[ci->client_playas].clients, ci->client_name, sizeof(_network_company_info[0].clients)); 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); rubidium@10207: if (ci != NULL && IsValidCompanyID(ci->client_playas)) { rubidium@10207: if (!StrEmpty(_network_company_info[ci->client_playas].clients)) { rubidium@10207: ttd_strlcat(_network_company_info[ci->client_playas].clients, ", ", lengthof(_network_company_info[0].clients)); tron@5638: } truelight@668: rubidium@10207: ttd_strlcat(_network_company_info[ci->client_playas].clients, client_name, lengthof(_network_company_info[0].clients)); 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: { rubidium@5624: NetworkTCPSocketHandler *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 */ rubidium@6247: static void NetworkCheckRestartMap() truelight@785: { rubidium@9420: if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) { Darkvater@5380: 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) */ rubidium@6247: static void NetworkAutoCleanCompanies() truelight@690: { rubidium@5624: NetworkTCPSocketHandler *cs; Darkvater@4880: const NetworkClientInfo *ci; rubidium@10207: const Company *c; rubidium@10207: bool clients_in_company[MAX_COMPANIES]; truelight@690: rubidium@9420: if (!_settings_client.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); rubidium@10207: if (IsValidCompanyID(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); rubidium@10207: if (IsValidCompanyID(ci->client_playas)) clients_in_company[ci->client_playas] = true; truelight@690: } truelight@690: truelight@690: /* Go through all the comapnies */ rubidium@10207: FOR_ALL_COMPANIES(c) { truelight@690: /* Skip the non-active once */ rubidium@10207: if (c->is_ai) continue; truelight@690: rubidium@10207: if (!clients_in_company[c->index]) { truelight@690: /* The company is empty for one month more */ rubidium@10207: _network_company_info[c->index].months_empty++; truelight@690: truelight@690: /* Is the company empty for autoclean_unprotected-months, and is there no protection? */ rubidium@10207: if (_settings_client.network.autoclean_unprotected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_info[c->index].password)) { truelight@690: /* Shut the company down */ rubidium@10207: DoCommandP(0, 2, c->index, NULL, CMD_COMPANY_CTRL); rubidium@10207: IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d", c->index + 1); truelight@690: } rubidium@10207: /* Is the company empty for autoclean_protected-months, and there is a protection? */ rubidium@10207: if (_settings_client.network.autoclean_protected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_info[c->index].password)) { truelight@690: /* Unprotect the company */ rubidium@10207: _network_company_info[c->index].password[0] = '\0'; rubidium@10207: IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1); rubidium@10207: _network_company_info[c->index].months_empty = 0; truelight@690: } truelight@690: } else { truelight@690: /* It is not empty, reset the date */ rubidium@10207: _network_company_info[c->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: { rubidium@5624: NetworkTCPSocketHandler *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 rubidium@5624: bool NetworkServer_ReadPackets(NetworkTCPSocketHandler *cs) truelight@543: { truelight@543: Packet *p; truelight@543: NetworkRecvStatus res; rubidium@5902: while ((p = cs->Recv_Packet(&res)) != NULL) { rubidium@5900: byte type = p->Recv_uint8(); 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 { Darkvater@5380: DEBUG(net, 0, "[server] received invalid packet type %d", type); tron@4077: } rubidium@5898: delete p; truelight@543: } truelight@543: truelight@543: return true; truelight@543: } truelight@543: truelight@543: // Handle the local command-queue rubidium@5624: static void NetworkHandleCommandQueue(NetworkTCPSocketHandler* 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: { rubidium@5624: NetworkTCPSocketHandler *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 rubidium@9420: if (_frame_counter >= _last_sync_frame + _settings_client.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) rubidium@9337: IConsolePrintF(CC_ERROR,"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) { rubidium@9337: IConsolePrintF(CC_WARNING,"[%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); rubidium@9420: if (lag > _settings_client.network.max_join_time) { rubidium@9420: IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->index, _settings_client.network.max_join_time); truelight@1602: NetworkCloseClient(cs); truelight@1602: } rubidium@6235: } else if (cs->status == STATUS_INACTIVE) { rubidium@6235: int lag = NetworkCalculateLag(cs); rubidium@6235: if (lag > 4 * DAY_TICKS) { rubidium@9337: IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->index, 4 * DAY_TICKS); rubidium@6235: NetworkCloseClient(cs); rubidium@6235: } 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: rubidium@6247: void NetworkServerYearlyLoop() truelight@785: { truelight@785: NetworkCheckRestartMap(); truelight@785: } truelight@785: rubidium@6247: void NetworkServerMonthlyLoop() truelight@690: { truelight@690: NetworkAutoCleanCompanies(); truelight@690: } truelight@690: rubidium@10207: void NetworkServerChangeOwner(Owner current_owner, Owner new_owner) rubidium@9428: { rubidium@9428: /* The server has to handle all administrative issues, for example rubidium@9428: * updating and notifying all clients of what has happened */ rubidium@9428: NetworkTCPSocketHandler *cs; rubidium@9428: NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); rubidium@9428: rubidium@10207: /* The server has just changed from owner */ rubidium@10207: if (current_owner == ci->client_playas) { rubidium@10207: ci->client_playas = new_owner; rubidium@9428: NetworkUpdateClientInfo(NETWORK_SERVER_INDEX); rubidium@9428: } rubidium@9428: rubidium@10207: /* Find all clients that were in control of this company, and mark them as new_owner */ rubidium@9428: FOR_ALL_CLIENTS(cs) { rubidium@9428: ci = DEREF_CLIENT_INFO(cs); rubidium@10207: if (current_owner == ci->client_playas) { rubidium@10207: ci->client_playas = new_owner; rubidium@9428: NetworkUpdateClientInfo(ci->client_index); rubidium@9428: } rubidium@9428: } rubidium@9428: } rubidium@9428: rubidium@10207: const char* GetClientIP(const NetworkClientInfo* ci) rubidium@9428: { rubidium@9428: struct in_addr addr; rubidium@9428: rubidium@9428: addr.s_addr = ci->client_ip; rubidium@9428: return inet_ntoa(addr); rubidium@9428: } rubidium@9428: rubidium@9428: void NetworkServerShowStatusToConsole() rubidium@9428: { rubidium@9428: static const char* const stat_str[] = { rubidium@9428: "inactive", rubidium@9428: "authorizing", rubidium@9428: "authorized", rubidium@9428: "waiting", rubidium@9428: "loading map", rubidium@9428: "map done", rubidium@9428: "ready", rubidium@9428: "active" rubidium@9428: }; rubidium@9428: rubidium@9428: NetworkTCPSocketHandler *cs; rubidium@9428: FOR_ALL_CLIENTS(cs) { rubidium@9428: int lag = NetworkCalculateLag(cs); rubidium@9428: const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); rubidium@9428: const char* status; rubidium@9428: rubidium@9428: status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown"); rubidium@9428: IConsolePrintF(CC_INFO, "Client #%1d name: '%s' status: '%s' frame-lag: %3d company: %1d IP: %s unique-id: '%s'", rubidium@9428: cs->index, ci->client_name, status, lag, rubidium@10207: ci->client_playas + (IsValidCompanyID(ci->client_playas) ? 1 : 0), rubidium@10207: GetClientIP(ci), ci->unique_id); rubidium@9428: } rubidium@9428: } rubidium@9428: rubidium@9428: void NetworkServerSendRcon(uint16 client_index, ConsoleColour colour_code, const char *string) rubidium@9428: { rubidium@9428: SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromIndex(client_index), colour_code, string); rubidium@9428: } rubidium@9428: rubidium@9428: void NetworkServerSendError(uint16 client_index, NetworkErrorCode error) rubidium@9428: { rubidium@9428: SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(client_index), error); rubidium@9428: } rubidium@9428: rubidium@10207: bool NetworkCompanyHasClients(CompanyID company) rubidium@9428: { rubidium@9428: const NetworkTCPSocketHandler *cs; rubidium@9428: const NetworkClientInfo *ci; rubidium@9428: FOR_ALL_CLIENTS(cs) { rubidium@9428: ci = DEREF_CLIENT_INFO(cs); rubidium@9428: if (ci->client_playas == company) return true; rubidium@9428: } rubidium@9428: return false; rubidium@9428: } truelight@543: #endif /* ENABLE_NETWORK */