diff options
author | rubidium <rubidium@openttd.org> | 2008-12-22 12:59:31 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2008-12-22 12:59:31 +0000 |
commit | 6d23f48bb6b9f5e9ae9c7568a0acdf3c206dbabc (patch) | |
tree | bb6d8bbfd056eeed669e31e87f2c987bc038a42a | |
parent | 415d5abc54acd87a16f22201a84020734c728ee5 (diff) | |
download | openttd-6d23f48bb6b9f5e9ae9c7568a0acdf3c206dbabc.tar.xz |
(svn r14709) -Codechange: make a clearer distinction between 'unique' client identification ids and the indices into the clients/client info arrays.
-rw-r--r-- | src/company_cmd.cpp | 8 | ||||
-rw-r--r-- | src/console.cpp | 4 | ||||
-rw-r--r-- | src/console_cmds.cpp | 42 | ||||
-rw-r--r-- | src/main_gui.cpp | 6 | ||||
-rw-r--r-- | src/network/core/tcp.cpp | 2 | ||||
-rw-r--r-- | src/network/core/tcp.h | 2 | ||||
-rw-r--r-- | src/network/network.cpp | 48 | ||||
-rw-r--r-- | src/network/network_chat_gui.cpp | 4 | ||||
-rw-r--r-- | src/network/network_client.cpp | 55 | ||||
-rw-r--r-- | src/network/network_func.h | 16 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 10 | ||||
-rw-r--r-- | src/network/network_internal.h | 2 | ||||
-rw-r--r-- | src/network/network_server.cpp | 158 | ||||
-rw-r--r-- | src/network/network_server.h | 2 | ||||
-rw-r--r-- | src/network/network_type.h | 19 |
15 files changed, 191 insertions, 187 deletions
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index d8a4c73b5..463f0194c 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -807,9 +807,9 @@ CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) * Other client(s)/server: * _local_company/_network_playas: what they play as * cid = requested company/company of joining client */ - uint16 cid = p2; // ClientID + ClientIndex cid = (ClientIndex)p2; - /* Has the network client a correct ClientID? */ + /* Has the network client a correct ClientIndex? */ if (!(flags & DC_EXEC)) return CommandCost(); if (cid >= MAX_CLIENT_INFO) return CommandCost(); @@ -823,7 +823,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) if (_network_server) { NetworkClientInfo *ci = &_network_client_info[cid]; ci->client_playas = COMPANY_SPECTATOR; - NetworkUpdateClientInfo(ci->client_index); + NetworkUpdateClientInfo(ci->client_id); } else if (_local_company == COMPANY_SPECTATOR) { _network_playas = COMPANY_SPECTATOR; } @@ -859,7 +859,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) * DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */ NetworkClientInfo *ci = &_network_client_info[cid]; ci->client_playas = c->index; - NetworkUpdateClientInfo(ci->client_index); + NetworkUpdateClientInfo(ci->client_id); if (IsValidCompanyID(ci->client_playas)) { CompanyID company_backup = _local_company; diff --git a/src/console.cpp b/src/console.cpp index 3cc11f31b..f034d498c 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -38,7 +38,7 @@ void IConsoleInit() { _iconsole_output_file = NULL; #ifdef ENABLE_NETWORK /* Initialize network only variables */ - _redirect_console_to_client = 0; + _redirect_console_to_client = INVALID_CLIENT_ID; #endif IConsoleGUIInit(); @@ -90,7 +90,7 @@ void IConsolePrint(ConsoleColour color_code, const char *string) { char *str; #ifdef ENABLE_NETWORK - if (_redirect_console_to_client != 0) { + if (_redirect_console_to_client != INVALID_CLIENT_ID) { /* Redirect the string to the client */ NetworkServerSendRcon(_redirect_console_to_client, color_code, string); return; diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index c216097cf..52193e226 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -372,7 +372,7 @@ DEF_CONSOLE_CMD(ConBan) { NetworkClientInfo *ci; const char *banip = NULL; - uint32 index; + ClientID client_id; if (argc == 0) { IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'"); @@ -384,24 +384,24 @@ DEF_CONSOLE_CMD(ConBan) if (argc != 2) return false; if (strchr(argv[1], '.') == NULL) { // banning with ID - index = atoi(argv[1]); - ci = NetworkFindClientInfoFromIndex(index); + client_id = (ClientID)atoi(argv[1]); + ci = NetworkFindClientInfoFromIndex(client_id); } else { // banning IP ci = NetworkFindClientInfoFromIP(argv[1]); if (ci == NULL) { banip = argv[1]; - index = (uint32)-1; + client_id = (ClientID)-1; } else { - index = ci->client_index; + client_id = ci->client_id; } } - if (index == NETWORK_SERVER_INDEX) { + if (client_id == CLIENT_ID_SERVER) { IConsoleError("Silly boy, you can not ban yourself!"); return true; } - if (index == 0 || (ci == NULL && index != (uint32)-1)) { + if (client_id == INVALID_CLIENT_ID || (ci == NULL && client_id != (ClientID)-1)) { IConsoleError("Invalid client"); return true; } @@ -409,13 +409,13 @@ DEF_CONSOLE_CMD(ConBan) if (ci != NULL) { IConsolePrint(CC_DEFAULT, "Client banned"); banip = GetClientIP(ci); - NetworkServerSendError(index, NETWORK_ERROR_KICKED); + NetworkServerSendError(client_id, NETWORK_ERROR_KICKED); } else { IConsolePrint(CC_DEFAULT, "Client not online, banned IP"); } /* Add user to ban-list */ - for (index = 0; index < lengthof(_network_ban_list); index++) { + for (uint index = 0; index < lengthof(_network_ban_list); index++) { if (_network_ban_list[index] == NULL) { _network_ban_list[index] = strdup(banip); break; @@ -555,7 +555,7 @@ DEF_CONSOLE_CMD(ConServerInfo) DEF_CONSOLE_CMD(ConKick) { NetworkClientInfo *ci; - uint32 index; + ClientID client_id; if (argc == 0) { IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'"); @@ -566,25 +566,25 @@ DEF_CONSOLE_CMD(ConKick) if (argc != 2) return false; if (strchr(argv[1], '.') == NULL) { - index = atoi(argv[1]); - ci = NetworkFindClientInfoFromIndex(index); + client_id = (ClientID)atoi(argv[1]); + ci = NetworkFindClientInfoFromIndex(client_id); } else { ci = NetworkFindClientInfoFromIP(argv[1]); - index = (ci == NULL) ? 0 : ci->client_index; + client_id = (ci == NULL) ? INVALID_CLIENT_ID : ci->client_id; } - if (index == NETWORK_SERVER_INDEX) { + if (client_id == CLIENT_ID_SERVER) { IConsoleError("Silly boy, you can not kick yourself!"); return true; } - if (index == 0) { + if (client_id == INVALID_CLIENT_ID) { IConsoleError("Invalid client"); return true; } if (ci != NULL) { - NetworkServerSendError(index, NETWORK_ERROR_KICKED); + NetworkServerSendError(client_id, NETWORK_ERROR_KICKED); } else { IConsoleError("Client not found"); } @@ -623,7 +623,7 @@ DEF_CONSOLE_CMD(ConResetCompany) IConsoleError("Cannot remove company: a client is connected to that company."); return false; } - const NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); + const NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); if (ci->client_playas == index) { IConsoleError("Cannot remove company: the server is connected to that company."); return true; @@ -647,7 +647,7 @@ DEF_CONSOLE_CMD(ConNetworkClients) FOR_ALL_ACTIVE_CLIENT_INFOS(ci) { IConsolePrintF(CC_INFO, "Client #%1d name: '%s' company: %1d IP: %s", - ci->client_index, ci->client_name, + ci->client_id, ci->client_name, ci->client_playas + (IsValidCompanyID(ci->client_playas) ? 1 : 0), GetClientIP(ci)); } @@ -1132,7 +1132,7 @@ DEF_CONSOLE_CMD(ConSay) if (!_network_server) { NetworkClientSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0 /* param does not matter */, argv[1]); } else { - NetworkServerSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, argv[1], NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, argv[1], CLIENT_ID_SERVER); } return true; @@ -1185,7 +1185,7 @@ DEF_CONSOLE_CMD(ConSayCompany) if (!_network_server) { NetworkClientSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2]); } else { - NetworkServerSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2], NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2], CLIENT_ID_SERVER); } return true; @@ -1204,7 +1204,7 @@ DEF_CONSOLE_CMD(ConSayClient) if (!_network_server) { NetworkClientSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2]); } else { - NetworkServerSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2], NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2], CLIENT_ID_SERVER); } return true; diff --git a/src/main_gui.cpp b/src/main_gui.cpp index 91c24fd65..ffa2b7feb 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -55,7 +55,7 @@ void CcGiveMoney(bool success, TileIndex tile, uint32 p1, uint32 p2) if (!_network_server) { NetworkClientSendChat(NETWORK_ACTION_GIVE_MONEY, DESTTYPE_TEAM, p2, msg); } else { - NetworkServerSendChat(NETWORK_ACTION_GIVE_MONEY, DESTTYPE_TEAM, p2, msg, NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_GIVE_MONEY, DESTTYPE_TEAM, p2, msg, CLIENT_ID_SERVER); } #endif /* ENABLE_NETWORK */ } @@ -316,7 +316,7 @@ struct MainWindow : Window #ifdef ENABLE_NETWORK case WKC_RETURN: case 'T': // smart chat; send to team if any, otherwise to all if (_networking) { - const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_index); + const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_id); bool teamchat = false; if (cio == NULL) break; @@ -342,7 +342,7 @@ struct MainWindow : Window case WKC_CTRL | WKC_RETURN: case WKC_CTRL | 'T': // send text to all team mates if (_networking) { - const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_index); + const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_id); if (cio == NULL) break; ShowNetworkChatQueryWindow(DESTTYPE_TEAM, cio->client_playas); diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 6450d4778..f6cef8728 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -22,7 +22,7 @@ void NetworkTCPSocketHandler::Initialize() { this->sock = INVALID_SOCKET; - this->index = 0; + this->client_id = INVALID_CLIENT_ID; this->last_frame = 0; this->last_frame_server = 0; this->lag_test = 0; diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index 6af0d17f8..1bafbf26e 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -91,7 +91,7 @@ private: Packet *packet_queue; ///< Packets that are awaiting delivery Packet *packet_recv; ///< Partially received packet public: - uint16 index; ///< Client index + ClientID client_id; ///< Client identifier uint32 last_frame; ///< Last frame we have executed uint32 last_frame_server; ///< Last frame the server has executed byte lag_test; ///< Byte used for lag-testing the client diff --git a/src/network/network.cpp b/src/network/network.cpp index 05839042d..44fd28b0b 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -43,6 +43,8 @@ #include "table/strings.h" #include "../company_base.h" +DECLARE_POSTFIX_INCREMENT(ClientID); + bool _network_server; ///< network-server is active bool _network_available; ///< is network mode available? bool _network_dedicated; ///< are we a dedicated server? @@ -50,8 +52,8 @@ bool _is_network_server; ///< Does this client wants to be a network-server? NetworkServerGameInfo _network_game_info; NetworkCompanyInfo _network_company_info[MAX_COMPANIES]; NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; -uint16 _network_own_client_index; -uint16 _redirect_console_to_client; +ClientID _network_own_client_id; +ClientID _redirect_console_to_client; bool _network_need_advertise; uint32 _network_last_advertise_frame; uint8 _network_reconnect; @@ -92,19 +94,19 @@ static SOCKET _listensocket; // The amount of clients connected static byte _network_clients_connected = 0; -// The index counter for new clients (is never decreased) -static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1; +// The identifier counter for new clients (is never decreased) +static ClientID _network_client_id = CLIENT_ID_FIRST; /* Some externs / forwards */ extern void StateGameLoop(); -// Function that looks up the CI for a given client-index -NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index) +// Function that looks up the CI for a given client-identifier +NetworkClientInfo *NetworkFindClientInfoFromIndex(ClientID client_id) { NetworkClientInfo *ci; for (ci = _network_client_info; ci != endof(_network_client_info); ci++) { - if (ci->client_index == client_index) return ci; + if (ci->client_id == client_id) return ci; } return NULL; @@ -125,13 +127,13 @@ NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip) return NULL; } -// Function that looks up the CS for a given client-index -NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index) +// Function that looks up the CS for a given client-identifier +NetworkTCPSocketHandler *NetworkFindClientStateFromClientID(ClientID client_id) { NetworkTCPSocketHandler *cs; for (cs = _clients; cs != endof(_clients); cs++) { - if (cs->index == client_index) return cs; + if (cs->client_id == client_id) return cs; } return NULL; @@ -144,7 +146,7 @@ void NetworkGetClientName(char *client_name, size_t size, const NetworkTCPSocket const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); if (ci->client_name[0] == '\0') { - snprintf(client_name, size, "Client #%4d", cs->index); + snprintf(client_name, size, "Client #%4d", cs->client_id); } else { ttd_strlcpy(client_name, ci->client_name, size); } @@ -361,13 +363,13 @@ void CheckMinActiveClients() _min_active_clients_paused = true; DoCommandP(0, 1, 0, NULL, CMD_PAUSE); - NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (not enough players)", NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (not enough players)", CLIENT_ID_SERVER); } else { if (!_min_active_clients_paused) return; _min_active_clients_paused = false; DoCommandP(0, 0, 0, NULL, CMD_PAUSE); - NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (enough players)", NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (enough players)", CLIENT_ID_SERVER); } } @@ -420,8 +422,8 @@ static NetworkTCPSocketHandler *NetworkAllocClient(SOCKET s) NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); memset(ci, 0, sizeof(*ci)); - cs->index = _network_client_index++; - ci->client_index = cs->index; + cs->client_id = _network_client_id++; + ci->client_id = cs->client_id; ci->client_playas = COMPANY_INACTIVE_CLIENT; ci->join_date = _date; @@ -441,7 +443,7 @@ void NetworkCloseClient(NetworkTCPSocketHandler *cs) return; } - DEBUG(net, 1, "Closed client connection %d", cs->index); + DEBUG(net, 1, "Closed client connection %d", cs->client_id); if (!cs->has_quit && _network_server && cs->status > STATUS_INACTIVE) { // We did not receive a leave message from this client... @@ -459,7 +461,7 @@ void NetworkCloseClient(NetworkTCPSocketHandler *cs) // Inform other clients of this... strange leaving ;) FOR_ALL_CLIENTS(new_cs) { if (new_cs->status > STATUS_AUTH && cs != new_cs) { - SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno); + SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->client_id, errorno); } } } @@ -467,7 +469,7 @@ void NetworkCloseClient(NetworkTCPSocketHandler *cs) /* When the client was PRE_ACTIVE, the server was in pause mode, so unpause */ if (cs->status == STATUS_PRE_ACTIVE && _settings_client.network.pause_on_join) { DoCommandP(0, 0, 0, NULL, CMD_PAUSE); - NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused", NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused", CLIENT_ID_SERVER); } cs->Destroy(); @@ -493,8 +495,8 @@ void NetworkCloseClient(NetworkTCPSocketHandler *cs) // Reset the status of the last socket cs->sock = INVALID_SOCKET; cs->status = STATUS_INACTIVE; - cs->index = NETWORK_EMPTY_INDEX; - ci->client_index = NETWORK_EMPTY_INDEX; + cs->client_id = INVALID_CLIENT_ID; + ci->client_id = INVALID_CLIENT_ID; CheckMinActiveClients(); } @@ -808,11 +810,11 @@ static void NetworkInitGameInfo() _network_game_info.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1); // We use _network_client_info[MAX_CLIENT_INFO - 1] to store the server-data in it - // The index is NETWORK_SERVER_INDEX ( = 1) + // The client identifier is CLIENT_ID_SERVER ( = 1) ci = &_network_client_info[MAX_CLIENT_INFO - 1]; memset(ci, 0, sizeof(*ci)); - ci->client_index = NETWORK_SERVER_INDEX; + ci->client_id = CLIENT_ID_SERVER; ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : _local_company; strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name)); @@ -840,7 +842,7 @@ bool NetworkServerStart() _frame_counter_server = 0; _frame_counter_max = 0; _last_sync_frame = 0; - _network_own_client_index = NETWORK_SERVER_INDEX; + _network_own_client_id = CLIENT_ID_SERVER; /* Non-dedicated server will always be company #1 */ if (!_network_dedicated) _network_playas = COMPANY_FIRST; diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 02aec095a..108209cf3 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -259,7 +259,7 @@ static void SendChat(const char *buf, DestType type, int dest) if (!_network_server) { SEND_COMMAND(PACKET_CLIENT_CHAT)((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf); } else { - NetworkServerSendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, NETWORK_SERVER_INDEX); + NetworkServerSendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, CLIENT_ID_SERVER); } } @@ -301,7 +301,7 @@ struct NetworkChatWindow : public QueryStringBaseWindow { /* First, try clients */ if (*item < MAX_CLIENT_INFO) { /* Skip inactive clients */ - while (_network_client_info[*item].client_index == NETWORK_EMPTY_INDEX && *item < MAX_CLIENT_INFO) (*item)++; + while (_network_client_info[*item].client_id == INVALID_CLIENT_ID && *item < MAX_CLIENT_INFO) (*item)++; if (*item < MAX_CLIENT_INFO) return _network_client_info[*item].client_name; } diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index c90ffcc73..3c6f4dff1 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -244,7 +244,7 @@ DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType // Data: // uint8: ActionID (see network_data.h, NetworkAction) // uint8: Destination Type (see network_data.h, DestType); - // uint16: Destination Company/Client + // uint32: Destination CompanyID/Client-identifier // String: Message (max NETWORK_CHAT_LENGTH) // @@ -252,7 +252,7 @@ DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType p->Send_uint8 (action); p->Send_uint8 (type); - p->Send_uint16(dest); + p->Send_uint32(dest); p->Send_string(msg); MY_CLIENT->Send_Packet(p); } @@ -392,12 +392,12 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO) } // This packet contains info about the client (playas and name) -// as client we save this in NetworkClientInfo, linked via 'index' +// as client we save this in NetworkClientInfo, linked via 'client_id' // which is always an unique number on a server. DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO) { NetworkClientInfo *ci; - uint16 index = p->Recv_uint16(); + ClientID client_id = (ClientID)p->Recv_uint32(); CompanyID playas = (CompanyID)p->Recv_uint8(); char name[NETWORK_NAME_LENGTH]; @@ -406,9 +406,9 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO) if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST; /* Do we receive a change of data? Most likely we changed playas */ - if (index == _network_own_client_index) _network_playas = playas; + if (client_id == _network_own_client_id) _network_playas = playas; - ci = NetworkFindClientInfoFromIndex(index); + ci = NetworkFindClientInfoFromIndex(client_id); if (ci != NULL) { if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) { // Client name changed, display the change @@ -426,10 +426,10 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO) return NETWORK_RECV_STATUS_OKAY; } - // We don't have this index yet, find an empty index, and put the data there - ci = NetworkFindClientInfoFromIndex(NETWORK_EMPTY_INDEX); + // We don't have this client_id yet, find an empty client_id, and put the data there + ci = NetworkFindClientInfoFromIndex(INVALID_CLIENT_ID); if (ci != NULL) { - ci->client_index = index; + ci->client_id = client_id; ci->client_playas = playas; strecpy(ci->client_name, name, lastof(ci->client_name)); @@ -530,7 +530,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME) { - _network_own_client_index = p->Recv_uint16(); + _network_own_client_id = (ClientID)p->Recv_uint32(); /* Initialize the password hash salting variables, even if they were previously. */ _password_game_seed = p->Recv_uint32(); @@ -717,11 +717,11 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT) const NetworkClientInfo *ci = NULL, *ci_to; NetworkAction action = (NetworkAction)p->Recv_uint8(); - uint16 index = p->Recv_uint16(); + ClientID client_id = (ClientID)p->Recv_uint32(); bool self_send = p->Recv_bool(); p->Recv_string(msg, NETWORK_CHAT_LENGTH); - ci_to = NetworkFindClientInfoFromIndex(index); + ci_to = NetworkFindClientInfoFromIndex(client_id); if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY; /* Did we initiate the action locally? */ @@ -730,7 +730,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT) case NETWORK_ACTION_CHAT_CLIENT: /* For speaking to client we need the client-name */ snprintf(name, sizeof(name), "%s", ci_to->client_name); - ci = NetworkFindClientInfoFromIndex(_network_own_client_index); + ci = NetworkFindClientInfoFromIndex(_network_own_client_id); break; /* For speaking to company or giving money, we need the company-name */ @@ -742,7 +742,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT) SetDParam(0, ci_to->client_playas); GetString(name, str, lastof(name)); - ci = NetworkFindClientInfoFromIndex(_network_own_client_index); + ci = NetworkFindClientInfoFromIndex(_network_own_client_id); } break; default: NOT_REACHED(); break; @@ -761,18 +761,17 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT) { char str[100]; - uint16 index; NetworkClientInfo *ci; - index = p->Recv_uint16(); + ClientID client_id = (ClientID)p->Recv_uint32(); GetNetworkErrorMsg(str, (NetworkErrorCode)p->Recv_uint8(), lastof(str)); - ci = NetworkFindClientInfoFromIndex(index); + ci = NetworkFindClientInfoFromIndex(client_id); if (ci != NULL) { NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str); // The client is gone, give the NetworkClientInfo free - ci->client_index = NETWORK_EMPTY_INDEX; + ci->client_id = INVALID_CLIENT_ID; } InvalidateWindow(WC_CLIENT_LIST, 0); @@ -783,20 +782,19 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT) { char str[100]; - uint16 index; NetworkClientInfo *ci; - index = p->Recv_uint16(); + ClientID client_id = (ClientID)p->Recv_uint32(); p->Recv_string(str, lengthof(str)); - ci = NetworkFindClientInfoFromIndex(index); + ci = NetworkFindClientInfoFromIndex(client_id); if (ci != NULL) { NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str); // The client is gone, give the NetworkClientInfo free - ci->client_index = NETWORK_EMPTY_INDEX; + ci->client_id = INVALID_CLIENT_ID; } else { - DEBUG(net, 0, "Unknown client (%d) is leaving the game", index); + DEBUG(net, 0, "Unknown client (%d) is leaving the game", client_id); } InvalidateWindow(WC_CLIENT_LIST, 0); @@ -807,12 +805,9 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN) { - uint16 index; - NetworkClientInfo *ci; - - index = p->Recv_uint16(); + ClientID client_id = (ClientID)p->Recv_uint32(); - ci = NetworkFindClientInfoFromIndex(index); + NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_id); if (ci != NULL) NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name, ""); @@ -940,7 +935,7 @@ void NetworkClientSendRcon(const char *password, const char *command) void NetworkUpdateClientName() { - NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(_network_own_client_index); + NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(_network_own_client_id); if (ci == NULL) return; @@ -952,7 +947,7 @@ void NetworkUpdateClientName() if (NetworkFindName(_settings_client.network.client_name)) { NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", _settings_client.network.client_name); strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name)); - NetworkUpdateClientInfo(NETWORK_SERVER_INDEX); + NetworkUpdateClientInfo(CLIENT_ID_SERVER); } } } diff --git a/src/network/network_func.h b/src/network/network_func.h index 13e45232d..cacf643f6 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -14,8 +14,8 @@ extern NetworkServerGameInfo _network_game_info; extern NetworkCompanyInfo _network_company_info[MAX_COMPANIES]; extern NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; -extern uint16 _network_own_client_index; -extern uint16 _redirect_console_to_client; +extern ClientID _network_own_client_id; +extern ClientID _redirect_console_to_client; extern bool _network_need_advertise; extern uint32 _network_last_advertise_frame; extern uint8 _network_reconnect; @@ -36,7 +36,7 @@ void ParseConnectionString(const char **company, const char **port, char *connec void NetworkStartDebugLog(const char *hostname, uint16 port); void NetworkPopulateCompanyInfo(); -void NetworkUpdateClientInfo(uint16 client_index); +void NetworkUpdateClientInfo(ClientID client_id); bool NetworkClientConnectGame(const char *host, uint16 port); void NetworkClientSendRcon(const char *password, const char *command); void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg); @@ -49,20 +49,20 @@ void NetworkServerChangeOwner(Owner current_owner, Owner new_owner); void NetworkServerShowStatusToConsole(); bool NetworkServerStart(); -NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index); +NetworkClientInfo *NetworkFindClientInfoFromIndex(ClientID client_id); NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip); const char* GetClientIP(const NetworkClientInfo *ci); -void NetworkServerSendRcon(uint16 client_index, ConsoleColour colour_code, const char *string); -void NetworkServerSendError(uint16 client_index, NetworkErrorCode error); -void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, uint16 from_index); +void NetworkServerSendRcon(ClientID client_id, ConsoleColour colour_code, const char *string); +void NetworkServerSendError(ClientID client_id, NetworkErrorCode error); +void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, ClientID from_id); void NetworkInitChatMessage(); void CDECL NetworkAddChatMessage(uint16 color, uint8 duration, const char *message, ...); void NetworkUndrawChatMessage(); void NetworkChatMessageDailyLoop(); -#define FOR_ALL_ACTIVE_CLIENT_INFOS(ci) for (ci = _network_client_info; ci != endof(_network_client_info); ci++) if (ci->client_index != NETWORK_EMPTY_INDEX) +#define FOR_ALL_ACTIVE_CLIENT_INFOS(ci) for (ci = _network_client_info; ci != endof(_network_client_info); ci++) if (ci->client_id != INVALID_CLIENT_ID) #endif /* ENABLE_NETWORK */ #endif /* NETWORK_FUNC_H */ diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index b0bf24f25..df404966e 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1447,7 +1447,7 @@ static void ClientList_GiveMoney(byte client_no) static void ClientList_SpeakToClient(byte client_no) { if (NetworkFindClientInfo(client_no) != NULL) { - ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, NetworkFindClientInfo(client_no)->client_index); + ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, NetworkFindClientInfo(client_no)->client_id); } } @@ -1488,7 +1488,7 @@ struct NetworkClientListPopupWindow : Window { const NetworkClientInfo *ci = NetworkFindClientInfo(client_no); int i = 0; - if (_network_own_client_index != ci->client_index) { + if (_network_own_client_id != ci->client_id) { GetString(this->action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, lastof(this->action[i])); this->proc[i++] = &ClientList_SpeakToClient; } @@ -1500,7 +1500,7 @@ struct NetworkClientListPopupWindow : Window { GetString(this->action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, lastof(this->action[i])); this->proc[i++] = &ClientList_SpeakToAll; - if (_network_own_client_index != ci->client_index) { + if (_network_own_client_id != ci->client_id) { /* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed */ if (IsValidCompanyID(_network_playas) && IsValidCompanyID(ci->client_playas) && _settings_game.economy.give_money) { GetString(this->action[i], STR_NETWORK_CLIENTLIST_GIVE_MONEY, lastof(this->action[i])); @@ -1509,7 +1509,7 @@ struct NetworkClientListPopupWindow : Window { } /* A server can kick clients (but not himself) */ - if (_network_server && _network_own_client_index != ci->client_index) { + if (_network_server && _network_own_client_id != ci->client_id) { GetString(this->action[i], STR_NETWORK_CLIENTLIST_KICK, lastof(this->action[i])); this->proc[i++] = &ClientList_Kick; @@ -1684,7 +1684,7 @@ struct NetworkClientListWindow : Window colour = TC_BLACK; } - if (ci->client_index == NETWORK_SERVER_INDEX) { + if (ci->client_id == CLIENT_ID_SERVER) { DrawString(4, y, STR_NETWORK_SERVER, colour); } else { DrawString(4, y, STR_NETWORK_CLIENT, colour); diff --git a/src/network/network_internal.h b/src/network/network_internal.h index 37354cb5e..0508e4eb8 100644 --- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -144,7 +144,7 @@ void CDECL NetworkTextMessage(NetworkAction action, ConsoleColour color, bool se void NetworkGetClientName(char *clientname, size_t size, const NetworkTCPSocketHandler *cs); uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs); byte NetworkGetCurrentLanguageIndex(); -NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index); +NetworkTCPSocketHandler *NetworkFindClientStateFromClientID(ClientID client_id); char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last); bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index f337fcad2..0a4f35b4a 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -46,14 +46,14 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkTCPSocketHandler // Packet: SERVER_CLIENT_INFO // Function: Sends info about a client // Data: - // uint16: The index of the client (always unique on a server. 1 = server) + // uint32: The identifier of the client (always unique on a server. 1 = server, 0 is invalid) // uint8: As which company the client is playing // String: The name of the client // - if (ci->client_index != NETWORK_EMPTY_INDEX) { + if (ci->client_id != INVALID_CLIENT_ID) { Packet *p = NetworkSend_Init(PACKET_SERVER_CLIENT_INFO); - p->Send_uint16(ci->client_index); + p->Send_uint32(ci->client_id); p->Send_uint8 (ci->client_playas); p->Send_string(ci->client_name); @@ -165,11 +165,11 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkTCPSocketHandler *cs, if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) error = NETWORK_ERROR_ILLEGAL_PACKET; - SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, error); + SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->client_id, error); } } } else { - DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", cs->index, str); + DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", cs->client_id, str); } cs->has_quit = true; @@ -236,7 +236,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME) // Packet: SERVER_WELCOME // Function: The client is joined and ready to receive his map // Data: - // uint16: Own ClientID + // uint32: Own Client identifier // Packet *p; @@ -249,7 +249,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME) _network_game_info.clients_on++; p = NetworkSend_Init(PACKET_SERVER_WELCOME); - p->Send_uint16(cs->index); + p->Send_uint32(cs->client_id); p->Send_uint32(_settings_game.game_creation.generation_seed); p->Send_string(_settings_client.network.network_id); cs->Send_Packet(p); @@ -260,7 +260,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME) SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, DEREF_CLIENT_INFO(new_cs)); } // Also send the info of the server - SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX)); + SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER)); } DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WAIT) @@ -402,7 +402,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP) } } -DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkTCPSocketHandler *cs, uint16 client_index) +DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkTCPSocketHandler *cs, ClientID client_id) { // // Packet: SERVER_JOIN @@ -410,12 +410,12 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkTCPSocketHandler *cs, u // PACKET_CLIENT_MAP_OK) Mostly what directly follows is a // PACKET_SERVER_CLIENT_INFO // Data: - // uint16: Client-Index + // uint32: Client-identifier // Packet *p = NetworkSend_Init(PACKET_SERVER_JOIN); - p->Send_uint16(client_index); + p->Send_uint32(client_id); cs->Send_Packet(p); } @@ -499,60 +499,60 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkTCPSocketHandler *cs cs->Send_Packet(p); } -DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkTCPSocketHandler *cs, NetworkAction action, uint16 client_index, bool self_send, const char *msg) +DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkTCPSocketHandler *cs, NetworkAction action, ClientID client_id, bool self_send, const char *msg) { // // Packet: SERVER_CHAT // Function: Sends a chat-packet to the client // Data: // uint8: ActionID (see network_data.h, NetworkAction) - // uint16: Client-index + // uint32: Client-identifier // String: Message (max NETWORK_CHAT_LENGTH) // Packet *p = NetworkSend_Init(PACKET_SERVER_CHAT); p->Send_uint8 (action); - p->Send_uint16(client_index); + p->Send_uint32(client_id); p->Send_bool (self_send); p->Send_string(msg); cs->Send_Packet(p); } -DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkTCPSocketHandler *cs, uint16 client_index, NetworkErrorCode errorno) +DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkTCPSocketHandler *cs, ClientID client_id, NetworkErrorCode errorno) { // // Packet: SERVER_ERROR_QUIT // Function: One of the clients made an error and is quiting the game // This packet informs the other clients of that. // Data: - // uint16: Client-index + // uint32: Client-identifier // uint8: ErrorID (see network_data.h, NetworkErrorCode) // Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR_QUIT); - p->Send_uint16(client_index); + p->Send_uint32(client_id); p->Send_uint8 (errorno); cs->Send_Packet(p); } -DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkTCPSocketHandler *cs, uint16 client_index, const char *leavemsg) +DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkTCPSocketHandler *cs, ClientID client_id, const char *leavemsg) { // // Packet: SERVER_ERROR_QUIT // Function: A client left the game, and this packets informs the other clients // of that. // Data: - // uint16: Client-index + // uint32: Client-identifier // String: leave-message // Packet *p = NetworkSend_Init(PACKET_SERVER_QUIT); - p->Send_uint16(client_index); + p->Send_uint32(client_id); p->Send_string(leavemsg); cs->Send_Packet(p); @@ -800,7 +800,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK) FOR_ALL_CLIENTS(new_cs) { if (new_cs->status > STATUS_AUTH) { SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, DEREF_CLIENT_INFO(cs)); - SEND_COMMAND(PACKET_SERVER_JOIN)(new_cs, cs->index); + SEND_COMMAND(PACKET_SERVER_JOIN)(new_cs, cs->client_id); } } @@ -808,7 +808,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK) /* Now pause the game till the client is in sync */ DoCommandP(0, 1, 0, NULL, CMD_PAUSE); - NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (incoming client)", NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (incoming client)", CLIENT_ID_SERVER); } } else { // Wrong status for this packet, give a warning to client, and close connection @@ -825,18 +825,18 @@ static bool CheckCommandFlags(const CommandPacket *cp, const NetworkClientInfo * { byte flags = GetCommandFlags(cp->cmd); - if (flags & CMD_SERVER && ci->client_index != NETWORK_SERVER_INDEX) { - IConsolePrintF(CC_ERROR, "WARNING: server only command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci)); + if (flags & CMD_SERVER && ci->client_id != CLIENT_ID_SERVER) { + IConsolePrintF(CC_ERROR, "WARNING: server only command from client %d (IP: %s), kicking...", ci->client_id, GetClientIP(ci)); return false; } if (flags & CMD_OFFLINE) { - IConsolePrintF(CC_ERROR, "WARNING: offline only command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci)); + IConsolePrintF(CC_ERROR, "WARNING: offline only command from client %d (IP: %s), kicking...", ci->client_id, GetClientIP(ci)); return false; } - if (cp->cmd != CMD_COMPANY_CTRL && !IsValidCompanyID(cp->company) && ci->client_index != NETWORK_SERVER_INDEX) { - IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci)); + if (cp->cmd != CMD_COMPANY_CTRL && !IsValidCompanyID(cp->company) && ci->client_id != CLIENT_ID_SERVER) { + IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_id, GetClientIP(ci)); return false; } @@ -879,7 +879,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) /* Check if cp->cmd is valid */ if (!IsValidCommand(cp->cmd)) { - IConsolePrintF(CC_ERROR, "WARNING: invalid command from client %d (IP: %s).", ci->client_index, GetClientIP(ci)); + IConsolePrintF(CC_ERROR, "WARNING: invalid command from client %d (IP: %s).", ci->client_id, GetClientIP(ci)); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); free(cp); return; @@ -977,7 +977,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR) FOR_ALL_CLIENTS(new_cs) { if (new_cs->status > STATUS_AUTH) { - SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno); + SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->client_id, errorno); } } @@ -1006,7 +1006,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT) FOR_ALL_CLIENTS(new_cs) { if (new_cs->status > STATUS_AUTH) { - SEND_COMMAND(PACKET_SERVER_QUIT)(new_cs, cs->index, str); + SEND_COMMAND(PACKET_SERVER_QUIT)(new_cs, cs->client_id, str); } } @@ -1033,7 +1033,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK) if (_settings_client.network.pause_on_join) { DoCommandP(0, 0, 0, NULL, CMD_PAUSE); - NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", NETWORK_SERVER_INDEX); + NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", CLIENT_ID_SERVER); } CheckMinActiveClients(); @@ -1050,7 +1050,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK) -void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index) +void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, ClientID from_id) { NetworkTCPSocketHandler *cs; const NetworkClientInfo *ci, *ci_own, *ci_to; @@ -1058,32 +1058,32 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co switch (desttype) { case DESTTYPE_CLIENT: /* Are we sending to the server? */ - if (dest == NETWORK_SERVER_INDEX) { - ci = NetworkFindClientInfoFromIndex(from_index); + if ((ClientID)dest == CLIENT_ID_SERVER) { + ci = NetworkFindClientInfoFromIndex(from_id); /* Display the text locally, and that is it */ if (ci != NULL) NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg); } else { /* Else find the client to send the message to */ FOR_ALL_CLIENTS(cs) { - if (cs->index == dest) { - SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); + if (cs->client_id == (ClientID)dest) { + SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg); break; } } } // Display the message locally (so you know you have sent it) - if (from_index != dest) { - if (from_index == NETWORK_SERVER_INDEX) { - ci = NetworkFindClientInfoFromIndex(from_index); - ci_to = NetworkFindClientInfoFromIndex(dest); + if (from_id != (ClientID)dest) { + if (from_id == CLIENT_ID_SERVER) { + ci = NetworkFindClientInfoFromIndex(from_id); + ci_to = NetworkFindClientInfoFromIndex((ClientID)dest); if (ci != NULL && ci_to != NULL) NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), true, ci_to->client_name, "%s", msg); } else { FOR_ALL_CLIENTS(cs) { - if (cs->index == from_index) { - SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, dest, true, msg); + if (cs->client_id == from_id) { + SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, (ClientID)dest, true, msg); break; } } @@ -1097,18 +1097,18 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co ci_to = NULL; FOR_ALL_CLIENTS(cs) { ci = DEREF_CLIENT_INFO(cs); - if (ci->client_playas == dest) { - SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); - if (cs->index == from_index) show_local = false; + if (ci->client_playas == (CompanyID)dest) { + SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg); + if (cs->client_id == from_id) show_local = false; ci_to = ci; // Remember a client that is in the company for company-name } } - ci = NetworkFindClientInfoFromIndex(from_index); - ci_own = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); + ci = NetworkFindClientInfoFromIndex(from_id); + ci_own = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) { NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg); - if (from_index == NETWORK_SERVER_INDEX) show_local = false; + if (from_id == CLIENT_ID_SERVER) show_local = false; ci_to = ci_own; } @@ -1117,7 +1117,7 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co // Display the message locally (so you know you have sent it) if (ci != NULL && show_local) { - if (from_index == NETWORK_SERVER_INDEX) { + if (from_id == CLIENT_ID_SERVER) { char name[NETWORK_NAME_LENGTH]; StringID str = IsValidCompanyID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS; SetDParam(0, ci_to->client_playas); @@ -1125,8 +1125,8 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci_own->client_playas), true, name, "%s", msg); } else { FOR_ALL_CLIENTS(cs) { - if (cs->index == from_index) { - SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, ci_to->client_index, true, msg); + if (cs->client_id == from_id) { + SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, ci_to->client_id, true, msg); } } } @@ -1138,9 +1138,9 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co /* fall-through to next case */ case DESTTYPE_BROADCAST: FOR_ALL_CLIENTS(cs) { - SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); + SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg); } - ci = NetworkFindClientInfoFromIndex(from_index); + ci = NetworkFindClientInfoFromIndex(from_id); if (ci != NULL) NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg); break; @@ -1157,7 +1157,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT) NetworkAction action = (NetworkAction)p->Recv_uint8(); DestType desttype = (DestType)p->Recv_uint8(); - int dest = p->Recv_uint16(); + int dest = p->Recv_uint32(); char msg[NETWORK_CHAT_LENGTH]; p->Recv_string(msg, NETWORK_CHAT_LENGTH); @@ -1170,10 +1170,10 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT) case NETWORK_ACTION_CHAT: case NETWORK_ACTION_CHAT_CLIENT: case NETWORK_ACTION_CHAT_COMPANY: - NetworkServerSendChat(action, desttype, dest, msg, cs->index); + NetworkServerSendChat(action, desttype, dest, msg, cs->client_id); break; default: - IConsolePrintF(CC_ERROR, "WARNING: invalid chat action from client %d (IP: %s).", ci->client_index, GetClientIP(ci)); + IConsolePrintF(CC_ERROR, "WARNING: invalid chat action from client %d (IP: %s).", ci->client_id, GetClientIP(ci)); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED); break; } @@ -1219,7 +1219,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME) if (NetworkFindName(client_name)) { NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", client_name); strecpy(ci->client_name, client_name, lastof(ci->client_name)); - NetworkUpdateClientInfo(ci->client_index); + NetworkUpdateClientInfo(ci->client_id); } } } @@ -1235,15 +1235,15 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_RCON) p->Recv_string(command, sizeof(command)); if (strcmp(pass, _settings_client.network.rcon_password) != 0) { - DEBUG(net, 0, "[rcon] wrong password from client-id %d", cs->index); + DEBUG(net, 0, "[rcon] wrong password from client-id %d", cs->client_id); return; } - DEBUG(net, 0, "[rcon] client-id %d executed: '%s'", cs->index, command); + DEBUG(net, 0, "[rcon] client-id %d executed: '%s'", cs->client_id, command); - _redirect_console_to_client = cs->index; + _redirect_console_to_client = cs->client_id; IConsoleCmdExec(command); - _redirect_console_to_client = 0; + _redirect_console_to_client = INVALID_CLIENT_ID; return; } @@ -1369,7 +1369,7 @@ void NetworkPopulateCompanyInfo() } } - ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); + ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); // Register local company (if not dedicated) if (ci != NULL && IsValidCompanyID(ci->client_playas)) strecpy(_network_company_info[ci->client_playas].clients, ci->client_name, lastof(_network_company_info[ci->client_playas].clients)); @@ -1390,11 +1390,11 @@ void NetworkPopulateCompanyInfo() } } -// Send a packet to all clients with updated info about this client_index -void NetworkUpdateClientInfo(uint16 client_index) +// Send a packet to all clients with updated info about this client_id +void NetworkUpdateClientInfo(ClientID client_id) { NetworkTCPSocketHandler *cs; - NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_index); + NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_id); if (ci == NULL) return; @@ -1436,7 +1436,7 @@ static void NetworkAutoCleanCompanies() } if (!_network_dedicated) { - ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); + ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); if (IsValidCompanyID(ci->client_playas)) clients_in_company[ci->client_playas] = true; } @@ -1494,7 +1494,7 @@ bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]) } } // Check if it is the same as the server-name - ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); + ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); if (ci != NULL) { if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use } @@ -1568,14 +1568,14 @@ void NetworkServer_Tick(bool send_frame) if (lag > 3) { // Client did still not report in after 4 game-day, drop him // (that is, the 3 of above, + 1 before any lag is counted) - IConsolePrintF(CC_ERROR,"Client #%d is dropped because the client did not respond for more than 4 game-days", cs->index); + IConsolePrintF(CC_ERROR,"Client #%d is dropped because the client did not respond for more than 4 game-days", cs->client_id); NetworkCloseClient(cs); continue; } // Report once per time we detect the lag if (cs->lag_test == 0) { - IConsolePrintF(CC_WARNING,"[%d] Client #%d is slow, try increasing *net_frame_freq to a higher value!", _frame_counter, cs->index); + IConsolePrintF(CC_WARNING,"[%d] Client #%d is slow, try increasing *net_frame_freq to a higher value!", _frame_counter, cs->client_id); cs->lag_test = 1; } } else { @@ -1584,13 +1584,13 @@ void NetworkServer_Tick(bool send_frame) } else if (cs->status == STATUS_PRE_ACTIVE) { int lag = NetworkCalculateLag(cs); if (lag > _settings_client.network.max_join_time) { - 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); + IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->client_id, _settings_client.network.max_join_time); NetworkCloseClient(cs); } } else if (cs->status == STATUS_INACTIVE) { int lag = NetworkCalculateLag(cs); if (lag > 4 * DAY_TICKS) { - IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->index, 4 * DAY_TICKS); + IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->client_id, 4 * DAY_TICKS); NetworkCloseClient(cs); } } @@ -1628,12 +1628,12 @@ void NetworkServerChangeOwner(Owner current_owner, Owner new_owner) /* The server has to handle all administrative issues, for example * updating and notifying all clients of what has happened */ NetworkTCPSocketHandler *cs; - NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); + NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); /* The server has just changed from owner */ if (current_owner == ci->client_playas) { ci->client_playas = new_owner; - NetworkUpdateClientInfo(NETWORK_SERVER_INDEX); + NetworkUpdateClientInfo(CLIENT_ID_SERVER); } /* Find all clients that were in control of this company, and mark them as new_owner */ @@ -1641,7 +1641,7 @@ void NetworkServerChangeOwner(Owner current_owner, Owner new_owner) ci = DEREF_CLIENT_INFO(cs); if (current_owner == ci->client_playas) { ci->client_playas = new_owner; - NetworkUpdateClientInfo(ci->client_index); + NetworkUpdateClientInfo(ci->client_id); } } } @@ -1675,20 +1675,20 @@ void NetworkServerShowStatusToConsole() status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown"); IConsolePrintF(CC_INFO, "Client #%1d name: '%s' status: '%s' frame-lag: %3d company: %1d IP: %s unique-id: '%s'", - cs->index, ci->client_name, status, lag, + cs->client_id, ci->client_name, status, lag, ci->client_playas + (IsValidCompanyID(ci->client_playas) ? 1 : 0), GetClientIP(ci), ci->unique_id); } } -void NetworkServerSendRcon(uint16 client_index, ConsoleColour colour_code, const char *string) +void NetworkServerSendRcon(ClientID client_id, ConsoleColour colour_code, const char *string) { - SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromIndex(client_index), colour_code, string); + SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromClientID(client_id), colour_code, string); } -void NetworkServerSendError(uint16 client_index, NetworkErrorCode error) +void NetworkServerSendError(ClientID client_id, NetworkErrorCode error) { - SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(client_index), error); + SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromClientID(client_id), error); } bool NetworkCompanyHasClients(CompanyID company) diff --git a/src/network/network_server.h b/src/network/network_server.h index 3b5b02fef..620fed36b 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -8,7 +8,7 @@ #ifdef ENABLE_NETWORK DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP); -DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkTCPSocketHandler *cs, uint16 client_index, NetworkErrorCode errorno); +DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkTCPSocketHandler *cs, ClientID client_id, NetworkErrorCode errorno); DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkTCPSocketHandler *cs, NetworkErrorCode error); DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN); DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME); diff --git a/src/network/network_type.h b/src/network/network_type.h index c26c84738..1f14607d8 100644 --- a/src/network/network_type.h +++ b/src/network/network_type.h @@ -20,18 +20,25 @@ enum { */ MAX_CLIENTS = MAX_COMPANIES + 3, - /** Do not change this next line. It should _ALWAYS_ be MAX_CLIENTS + 1 */ - MAX_CLIENT_INFO = MAX_CLIENTS + 1, - /** Maximum number of internet interfaces supported. */ MAX_INTERFACES = 9, /** How many vehicle/station types we put over the network */ NETWORK_VEHICLE_TYPES = 5, NETWORK_STATION_TYPES = 5, +}; - NETWORK_SERVER_INDEX = 1, - NETWORK_EMPTY_INDEX = 0, +/** 'Unique' identifier to be given to clients */ +enum ClientID { + INVALID_CLIENT_ID = 0, ///< Client is not part of anything + CLIENT_ID_SERVER = 1, ///< Servers always have this ID + CLIENT_ID_FIRST = 2, ///< The first client ID +}; + +/** Indices into the client tables */ +enum ClientIndex { + /** Do not change this next line. It should _ALWAYS_ be MAX_CLIENTS + 1 */ + MAX_CLIENT_INFO = MAX_CLIENTS + 1, }; struct NetworkCompanyInfo { @@ -50,7 +57,7 @@ struct NetworkCompanyInfo { }; struct NetworkClientInfo { - uint16 client_index; ///< Index of the client (same as ClientState->index) + ClientID client_id; ///< Client identifier (same as ClientState->client_id) char client_name[NETWORK_CLIENT_NAME_LENGTH]; ///< Name of the client byte client_lang; ///< The language of the client CompanyID client_playas; ///< As which company is this client playing (CompanyID) |