1
0
Fork 0

(svn r14709) -Codechange: make a clearer distinction between 'unique' client identification ids and the indices into the clients/client info arrays.

release/0.7
rubidium 2008-12-22 12:59:31 +00:00
parent 8431174e88
commit 782b80eff7
15 changed files with 191 additions and 187 deletions

View File

@ -807,9 +807,9 @@ CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
* Other client(s)/server: * Other client(s)/server:
* _local_company/_network_playas: what they play as * _local_company/_network_playas: what they play as
* cid = requested company/company of joining client */ * 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 (!(flags & DC_EXEC)) return CommandCost();
if (cid >= MAX_CLIENT_INFO) 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) { if (_network_server) {
NetworkClientInfo *ci = &_network_client_info[cid]; NetworkClientInfo *ci = &_network_client_info[cid];
ci->client_playas = COMPANY_SPECTATOR; ci->client_playas = COMPANY_SPECTATOR;
NetworkUpdateClientInfo(ci->client_index); NetworkUpdateClientInfo(ci->client_id);
} else if (_local_company == COMPANY_SPECTATOR) { } else if (_local_company == COMPANY_SPECTATOR) {
_network_playas = 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) */ * DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */
NetworkClientInfo *ci = &_network_client_info[cid]; NetworkClientInfo *ci = &_network_client_info[cid];
ci->client_playas = c->index; ci->client_playas = c->index;
NetworkUpdateClientInfo(ci->client_index); NetworkUpdateClientInfo(ci->client_id);
if (IsValidCompanyID(ci->client_playas)) { if (IsValidCompanyID(ci->client_playas)) {
CompanyID company_backup = _local_company; CompanyID company_backup = _local_company;

View File

@ -38,7 +38,7 @@ void IConsoleInit()
{ {
_iconsole_output_file = NULL; _iconsole_output_file = NULL;
#ifdef ENABLE_NETWORK /* Initialize network only variables */ #ifdef ENABLE_NETWORK /* Initialize network only variables */
_redirect_console_to_client = 0; _redirect_console_to_client = INVALID_CLIENT_ID;
#endif #endif
IConsoleGUIInit(); IConsoleGUIInit();
@ -90,7 +90,7 @@ void IConsolePrint(ConsoleColour color_code, const char *string)
{ {
char *str; char *str;
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK
if (_redirect_console_to_client != 0) { if (_redirect_console_to_client != INVALID_CLIENT_ID) {
/* Redirect the string to the client */ /* Redirect the string to the client */
NetworkServerSendRcon(_redirect_console_to_client, color_code, string); NetworkServerSendRcon(_redirect_console_to_client, color_code, string);
return; return;

View File

@ -372,7 +372,7 @@ DEF_CONSOLE_CMD(ConBan)
{ {
NetworkClientInfo *ci; NetworkClientInfo *ci;
const char *banip = NULL; const char *banip = NULL;
uint32 index; ClientID client_id;
if (argc == 0) { if (argc == 0) {
IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'"); 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 (argc != 2) return false;
if (strchr(argv[1], '.') == NULL) { // banning with ID if (strchr(argv[1], '.') == NULL) { // banning with ID
index = atoi(argv[1]); client_id = (ClientID)atoi(argv[1]);
ci = NetworkFindClientInfoFromIndex(index); ci = NetworkFindClientInfoFromIndex(client_id);
} else { // banning IP } else { // banning IP
ci = NetworkFindClientInfoFromIP(argv[1]); ci = NetworkFindClientInfoFromIP(argv[1]);
if (ci == NULL) { if (ci == NULL) {
banip = argv[1]; banip = argv[1];
index = (uint32)-1; client_id = (ClientID)-1;
} else { } 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!"); IConsoleError("Silly boy, you can not ban yourself!");
return true; 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"); IConsoleError("Invalid client");
return true; return true;
} }
@ -409,13 +409,13 @@ DEF_CONSOLE_CMD(ConBan)
if (ci != NULL) { if (ci != NULL) {
IConsolePrint(CC_DEFAULT, "Client banned"); IConsolePrint(CC_DEFAULT, "Client banned");
banip = GetClientIP(ci); banip = GetClientIP(ci);
NetworkServerSendError(index, NETWORK_ERROR_KICKED); NetworkServerSendError(client_id, NETWORK_ERROR_KICKED);
} else { } else {
IConsolePrint(CC_DEFAULT, "Client not online, banned IP"); IConsolePrint(CC_DEFAULT, "Client not online, banned IP");
} }
/* Add user to ban-list */ /* 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) { if (_network_ban_list[index] == NULL) {
_network_ban_list[index] = strdup(banip); _network_ban_list[index] = strdup(banip);
break; break;
@ -555,7 +555,7 @@ DEF_CONSOLE_CMD(ConServerInfo)
DEF_CONSOLE_CMD(ConKick) DEF_CONSOLE_CMD(ConKick)
{ {
NetworkClientInfo *ci; NetworkClientInfo *ci;
uint32 index; ClientID client_id;
if (argc == 0) { if (argc == 0) {
IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'"); 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 (argc != 2) return false;
if (strchr(argv[1], '.') == NULL) { if (strchr(argv[1], '.') == NULL) {
index = atoi(argv[1]); client_id = (ClientID)atoi(argv[1]);
ci = NetworkFindClientInfoFromIndex(index); ci = NetworkFindClientInfoFromIndex(client_id);
} else { } else {
ci = NetworkFindClientInfoFromIP(argv[1]); 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!"); IConsoleError("Silly boy, you can not kick yourself!");
return true; return true;
} }
if (index == 0) { if (client_id == INVALID_CLIENT_ID) {
IConsoleError("Invalid client"); IConsoleError("Invalid client");
return true; return true;
} }
if (ci != NULL) { if (ci != NULL) {
NetworkServerSendError(index, NETWORK_ERROR_KICKED); NetworkServerSendError(client_id, NETWORK_ERROR_KICKED);
} else { } else {
IConsoleError("Client not found"); IConsoleError("Client not found");
} }
@ -623,7 +623,7 @@ DEF_CONSOLE_CMD(ConResetCompany)
IConsoleError("Cannot remove company: a client is connected to that company."); IConsoleError("Cannot remove company: a client is connected to that company.");
return false; return false;
} }
const NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); const NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER);
if (ci->client_playas == index) { if (ci->client_playas == index) {
IConsoleError("Cannot remove company: the server is connected to that company."); IConsoleError("Cannot remove company: the server is connected to that company.");
return true; return true;
@ -647,7 +647,7 @@ DEF_CONSOLE_CMD(ConNetworkClients)
FOR_ALL_ACTIVE_CLIENT_INFOS(ci) { FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
IConsolePrintF(CC_INFO, "Client #%1d name: '%s' company: %1d IP: %s", 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), ci->client_playas + (IsValidCompanyID(ci->client_playas) ? 1 : 0),
GetClientIP(ci)); GetClientIP(ci));
} }
@ -1132,7 +1132,7 @@ DEF_CONSOLE_CMD(ConSay)
if (!_network_server) { if (!_network_server) {
NetworkClientSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0 /* param does not matter */, argv[1]); NetworkClientSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0 /* param does not matter */, argv[1]);
} else { } 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; return true;
@ -1185,7 +1185,7 @@ DEF_CONSOLE_CMD(ConSayCompany)
if (!_network_server) { if (!_network_server) {
NetworkClientSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2]); NetworkClientSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2]);
} else { } 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; return true;
@ -1204,7 +1204,7 @@ DEF_CONSOLE_CMD(ConSayClient)
if (!_network_server) { if (!_network_server) {
NetworkClientSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2]); NetworkClientSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2]);
} else { } 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; return true;

View File

@ -55,7 +55,7 @@ void CcGiveMoney(bool success, TileIndex tile, uint32 p1, uint32 p2)
if (!_network_server) { if (!_network_server) {
NetworkClientSendChat(NETWORK_ACTION_GIVE_MONEY, DESTTYPE_TEAM, p2, msg); NetworkClientSendChat(NETWORK_ACTION_GIVE_MONEY, DESTTYPE_TEAM, p2, msg);
} else { } 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 */ #endif /* ENABLE_NETWORK */
} }
@ -316,7 +316,7 @@ struct MainWindow : Window
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK
case WKC_RETURN: case 'T': // smart chat; send to team if any, otherwise to all case WKC_RETURN: case 'T': // smart chat; send to team if any, otherwise to all
if (_networking) { if (_networking) {
const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_index); const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_id);
bool teamchat = false; bool teamchat = false;
if (cio == NULL) break; 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 case WKC_CTRL | WKC_RETURN: case WKC_CTRL | 'T': // send text to all team mates
if (_networking) { if (_networking) {
const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_index); const NetworkClientInfo *cio = NetworkFindClientInfoFromIndex(_network_own_client_id);
if (cio == NULL) break; if (cio == NULL) break;
ShowNetworkChatQueryWindow(DESTTYPE_TEAM, cio->client_playas); ShowNetworkChatQueryWindow(DESTTYPE_TEAM, cio->client_playas);

View File

@ -22,7 +22,7 @@ void NetworkTCPSocketHandler::Initialize()
{ {
this->sock = INVALID_SOCKET; this->sock = INVALID_SOCKET;
this->index = 0; this->client_id = INVALID_CLIENT_ID;
this->last_frame = 0; this->last_frame = 0;
this->last_frame_server = 0; this->last_frame_server = 0;
this->lag_test = 0; this->lag_test = 0;

View File

@ -91,7 +91,7 @@ private:
Packet *packet_queue; ///< Packets that are awaiting delivery Packet *packet_queue; ///< Packets that are awaiting delivery
Packet *packet_recv; ///< Partially received packet Packet *packet_recv; ///< Partially received packet
public: public:
uint16 index; ///< Client index ClientID client_id; ///< Client identifier
uint32 last_frame; ///< Last frame we have executed uint32 last_frame; ///< Last frame we have executed
uint32 last_frame_server; ///< Last frame the server has executed uint32 last_frame_server; ///< Last frame the server has executed
byte lag_test; ///< Byte used for lag-testing the client byte lag_test; ///< Byte used for lag-testing the client

View File

@ -43,6 +43,8 @@
#include "table/strings.h" #include "table/strings.h"
#include "../company_base.h" #include "../company_base.h"
DECLARE_POSTFIX_INCREMENT(ClientID);
bool _network_server; ///< network-server is active bool _network_server; ///< network-server is active
bool _network_available; ///< is network mode available? bool _network_available; ///< is network mode available?
bool _network_dedicated; ///< are we a dedicated server? 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; NetworkServerGameInfo _network_game_info;
NetworkCompanyInfo _network_company_info[MAX_COMPANIES]; NetworkCompanyInfo _network_company_info[MAX_COMPANIES];
NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
uint16 _network_own_client_index; ClientID _network_own_client_id;
uint16 _redirect_console_to_client; ClientID _redirect_console_to_client;
bool _network_need_advertise; bool _network_need_advertise;
uint32 _network_last_advertise_frame; uint32 _network_last_advertise_frame;
uint8 _network_reconnect; uint8 _network_reconnect;
@ -92,19 +94,19 @@ static SOCKET _listensocket;
// The amount of clients connected // The amount of clients connected
static byte _network_clients_connected = 0; static byte _network_clients_connected = 0;
// The index counter for new clients (is never decreased) // The identifier counter for new clients (is never decreased)
static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1; static ClientID _network_client_id = CLIENT_ID_FIRST;
/* Some externs / forwards */ /* Some externs / forwards */
extern void StateGameLoop(); extern void StateGameLoop();
// Function that looks up the CI for a given client-index // Function that looks up the CI for a given client-identifier
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index) NetworkClientInfo *NetworkFindClientInfoFromIndex(ClientID client_id)
{ {
NetworkClientInfo *ci; NetworkClientInfo *ci;
for (ci = _network_client_info; ci != endof(_network_client_info); 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; return NULL;
@ -125,13 +127,13 @@ NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip)
return NULL; return NULL;
} }
// Function that looks up the CS for a given client-index // Function that looks up the CS for a given client-identifier
NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index) NetworkTCPSocketHandler *NetworkFindClientStateFromClientID(ClientID client_id)
{ {
NetworkTCPSocketHandler *cs; NetworkTCPSocketHandler *cs;
for (cs = _clients; cs != endof(_clients); 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; return NULL;
@ -144,7 +146,7 @@ void NetworkGetClientName(char *client_name, size_t size, const NetworkTCPSocket
const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
if (ci->client_name[0] == '\0') { if (ci->client_name[0] == '\0') {
snprintf(client_name, size, "Client #%4d", cs->index); snprintf(client_name, size, "Client #%4d", cs->client_id);
} else { } else {
ttd_strlcpy(client_name, ci->client_name, size); ttd_strlcpy(client_name, ci->client_name, size);
} }
@ -361,13 +363,13 @@ void CheckMinActiveClients()
_min_active_clients_paused = true; _min_active_clients_paused = true;
DoCommandP(0, 1, 0, NULL, CMD_PAUSE); 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 { } else {
if (!_min_active_clients_paused) return; if (!_min_active_clients_paused) return;
_min_active_clients_paused = false; _min_active_clients_paused = false;
DoCommandP(0, 0, 0, NULL, CMD_PAUSE); 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); NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
memset(ci, 0, sizeof(*ci)); memset(ci, 0, sizeof(*ci));
cs->index = _network_client_index++; cs->client_id = _network_client_id++;
ci->client_index = cs->index; ci->client_id = cs->client_id;
ci->client_playas = COMPANY_INACTIVE_CLIENT; ci->client_playas = COMPANY_INACTIVE_CLIENT;
ci->join_date = _date; ci->join_date = _date;
@ -441,7 +443,7 @@ void NetworkCloseClient(NetworkTCPSocketHandler *cs)
return; 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) { if (!cs->has_quit && _network_server && cs->status > STATUS_INACTIVE) {
// We did not receive a leave message from this client... // 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 ;) // Inform other clients of this... strange leaving ;)
FOR_ALL_CLIENTS(new_cs) { FOR_ALL_CLIENTS(new_cs) {
if (new_cs->status > STATUS_AUTH && cs != 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 */ /* 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) { if (cs->status == STATUS_PRE_ACTIVE && _settings_client.network.pause_on_join) {
DoCommandP(0, 0, 0, NULL, CMD_PAUSE); 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(); cs->Destroy();
@ -493,8 +495,8 @@ void NetworkCloseClient(NetworkTCPSocketHandler *cs)
// Reset the status of the last socket // Reset the status of the last socket
cs->sock = INVALID_SOCKET; cs->sock = INVALID_SOCKET;
cs->status = STATUS_INACTIVE; cs->status = STATUS_INACTIVE;
cs->index = NETWORK_EMPTY_INDEX; cs->client_id = INVALID_CLIENT_ID;
ci->client_index = NETWORK_EMPTY_INDEX; ci->client_id = INVALID_CLIENT_ID;
CheckMinActiveClients(); CheckMinActiveClients();
} }
@ -808,11 +810,11 @@ static void NetworkInitGameInfo()
_network_game_info.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1); _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 // 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]; ci = &_network_client_info[MAX_CLIENT_INFO - 1];
memset(ci, 0, sizeof(*ci)); 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; ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : _local_company;
strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name)); 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_server = 0;
_frame_counter_max = 0; _frame_counter_max = 0;
_last_sync_frame = 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 */ /* Non-dedicated server will always be company #1 */
if (!_network_dedicated) _network_playas = COMPANY_FIRST; if (!_network_dedicated) _network_playas = COMPANY_FIRST;

View File

@ -259,7 +259,7 @@ static void SendChat(const char *buf, DestType type, int dest)
if (!_network_server) { if (!_network_server) {
SEND_COMMAND(PACKET_CLIENT_CHAT)((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf); SEND_COMMAND(PACKET_CLIENT_CHAT)((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf);
} else { } 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 */ /* First, try clients */
if (*item < MAX_CLIENT_INFO) { if (*item < MAX_CLIENT_INFO) {
/* Skip inactive clients */ /* 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; if (*item < MAX_CLIENT_INFO) return _network_client_info[*item].client_name;
} }

View File

@ -244,7 +244,7 @@ DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType
// Data: // Data:
// uint8: ActionID (see network_data.h, NetworkAction) // uint8: ActionID (see network_data.h, NetworkAction)
// uint8: Destination Type (see network_data.h, DestType); // uint8: Destination Type (see network_data.h, DestType);
// uint16: Destination Company/Client // uint32: Destination CompanyID/Client-identifier
// String: Message (max NETWORK_CHAT_LENGTH) // 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 (action);
p->Send_uint8 (type); p->Send_uint8 (type);
p->Send_uint16(dest); p->Send_uint32(dest);
p->Send_string(msg); p->Send_string(msg);
MY_CLIENT->Send_Packet(p); 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) // 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. // which is always an unique number on a server.
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
{ {
NetworkClientInfo *ci; NetworkClientInfo *ci;
uint16 index = p->Recv_uint16(); ClientID client_id = (ClientID)p->Recv_uint32();
CompanyID playas = (CompanyID)p->Recv_uint8(); CompanyID playas = (CompanyID)p->Recv_uint8();
char name[NETWORK_NAME_LENGTH]; 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; if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST;
/* Do we receive a change of data? Most likely we changed playas */ /* 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 (ci != NULL) {
if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) { if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
// Client name changed, display the change // Client name changed, display the change
@ -426,10 +426,10 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
} }
// We don't have this index yet, find an empty index, and put the data there // We don't have this client_id yet, find an empty client_id, and put the data there
ci = NetworkFindClientInfoFromIndex(NETWORK_EMPTY_INDEX); ci = NetworkFindClientInfoFromIndex(INVALID_CLIENT_ID);
if (ci != NULL) { if (ci != NULL) {
ci->client_index = index; ci->client_id = client_id;
ci->client_playas = playas; ci->client_playas = playas;
strecpy(ci->client_name, name, lastof(ci->client_name)); 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) 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. */ /* Initialize the password hash salting variables, even if they were previously. */
_password_game_seed = p->Recv_uint32(); _password_game_seed = p->Recv_uint32();
@ -717,11 +717,11 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
const NetworkClientInfo *ci = NULL, *ci_to; const NetworkClientInfo *ci = NULL, *ci_to;
NetworkAction action = (NetworkAction)p->Recv_uint8(); NetworkAction action = (NetworkAction)p->Recv_uint8();
uint16 index = p->Recv_uint16(); ClientID client_id = (ClientID)p->Recv_uint32();
bool self_send = p->Recv_bool(); bool self_send = p->Recv_bool();
p->Recv_string(msg, NETWORK_CHAT_LENGTH); 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; if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
/* Did we initiate the action locally? */ /* Did we initiate the action locally? */
@ -730,7 +730,7 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
case NETWORK_ACTION_CHAT_CLIENT: case NETWORK_ACTION_CHAT_CLIENT:
/* For speaking to client we need the client-name */ /* For speaking to client we need the client-name */
snprintf(name, sizeof(name), "%s", ci_to->client_name); snprintf(name, sizeof(name), "%s", ci_to->client_name);
ci = NetworkFindClientInfoFromIndex(_network_own_client_index); ci = NetworkFindClientInfoFromIndex(_network_own_client_id);
break; break;
/* For speaking to company or giving money, we need the company-name */ /* 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); SetDParam(0, ci_to->client_playas);
GetString(name, str, lastof(name)); GetString(name, str, lastof(name));
ci = NetworkFindClientInfoFromIndex(_network_own_client_index); ci = NetworkFindClientInfoFromIndex(_network_own_client_id);
} break; } break;
default: NOT_REACHED(); break; default: NOT_REACHED(); break;
@ -761,18 +761,17 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
{ {
char str[100]; char str[100];
uint16 index;
NetworkClientInfo *ci; NetworkClientInfo *ci;
index = p->Recv_uint16(); ClientID client_id = (ClientID)p->Recv_uint32();
GetNetworkErrorMsg(str, (NetworkErrorCode)p->Recv_uint8(), lastof(str)); GetNetworkErrorMsg(str, (NetworkErrorCode)p->Recv_uint8(), lastof(str));
ci = NetworkFindClientInfoFromIndex(index); ci = NetworkFindClientInfoFromIndex(client_id);
if (ci != NULL) { if (ci != NULL) {
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str); NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str);
// The client is gone, give the NetworkClientInfo free // 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); InvalidateWindow(WC_CLIENT_LIST, 0);
@ -783,20 +782,19 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
{ {
char str[100]; char str[100];
uint16 index;
NetworkClientInfo *ci; NetworkClientInfo *ci;
index = p->Recv_uint16(); ClientID client_id = (ClientID)p->Recv_uint32();
p->Recv_string(str, lengthof(str)); p->Recv_string(str, lengthof(str));
ci = NetworkFindClientInfoFromIndex(index); ci = NetworkFindClientInfoFromIndex(client_id);
if (ci != NULL) { if (ci != NULL) {
NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str); NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str);
// The client is gone, give the NetworkClientInfo free // The client is gone, give the NetworkClientInfo free
ci->client_index = NETWORK_EMPTY_INDEX; ci->client_id = INVALID_CLIENT_ID;
} else { } 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); InvalidateWindow(WC_CLIENT_LIST, 0);
@ -807,12 +805,9 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN) DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN)
{ {
uint16 index; ClientID client_id = (ClientID)p->Recv_uint32();
NetworkClientInfo *ci;
index = p->Recv_uint16(); NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_id);
ci = NetworkFindClientInfoFromIndex(index);
if (ci != NULL) if (ci != NULL)
NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name, ""); NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name, "");
@ -940,7 +935,7 @@ void NetworkClientSendRcon(const char *password, const char *command)
void NetworkUpdateClientName() void NetworkUpdateClientName()
{ {
NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(_network_own_client_index); NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(_network_own_client_id);
if (ci == NULL) return; if (ci == NULL) return;
@ -952,7 +947,7 @@ void NetworkUpdateClientName()
if (NetworkFindName(_settings_client.network.client_name)) { if (NetworkFindName(_settings_client.network.client_name)) {
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", _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)); strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
NetworkUpdateClientInfo(NETWORK_SERVER_INDEX); NetworkUpdateClientInfo(CLIENT_ID_SERVER);
} }
} }
} }

View File

@ -14,8 +14,8 @@ extern NetworkServerGameInfo _network_game_info;
extern NetworkCompanyInfo _network_company_info[MAX_COMPANIES]; extern NetworkCompanyInfo _network_company_info[MAX_COMPANIES];
extern NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; extern NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
extern uint16 _network_own_client_index; extern ClientID _network_own_client_id;
extern uint16 _redirect_console_to_client; extern ClientID _redirect_console_to_client;
extern bool _network_need_advertise; extern bool _network_need_advertise;
extern uint32 _network_last_advertise_frame; extern uint32 _network_last_advertise_frame;
extern uint8 _network_reconnect; 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 NetworkStartDebugLog(const char *hostname, uint16 port);
void NetworkPopulateCompanyInfo(); void NetworkPopulateCompanyInfo();
void NetworkUpdateClientInfo(uint16 client_index); void NetworkUpdateClientInfo(ClientID client_id);
bool NetworkClientConnectGame(const char *host, uint16 port); bool NetworkClientConnectGame(const char *host, uint16 port);
void NetworkClientSendRcon(const char *password, const char *command); void NetworkClientSendRcon(const char *password, const char *command);
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg); 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(); void NetworkServerShowStatusToConsole();
bool NetworkServerStart(); bool NetworkServerStart();
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index); NetworkClientInfo *NetworkFindClientInfoFromIndex(ClientID client_id);
NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip); NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip);
const char* GetClientIP(const NetworkClientInfo *ci); const char* GetClientIP(const NetworkClientInfo *ci);
void NetworkServerSendRcon(uint16 client_index, ConsoleColour colour_code, const char *string); void NetworkServerSendRcon(ClientID client_id, ConsoleColour colour_code, const char *string);
void NetworkServerSendError(uint16 client_index, NetworkErrorCode error); void NetworkServerSendError(ClientID client_id, NetworkErrorCode error);
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, uint16 from_index); void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, ClientID from_id);
void NetworkInitChatMessage(); void NetworkInitChatMessage();
void CDECL NetworkAddChatMessage(uint16 color, uint8 duration, const char *message, ...); void CDECL NetworkAddChatMessage(uint16 color, uint8 duration, const char *message, ...);
void NetworkUndrawChatMessage(); void NetworkUndrawChatMessage();
void NetworkChatMessageDailyLoop(); 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 /* ENABLE_NETWORK */
#endif /* NETWORK_FUNC_H */ #endif /* NETWORK_FUNC_H */

View File

@ -1447,7 +1447,7 @@ static void ClientList_GiveMoney(byte client_no)
static void ClientList_SpeakToClient(byte client_no) static void ClientList_SpeakToClient(byte client_no)
{ {
if (NetworkFindClientInfo(client_no) != NULL) { 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); const NetworkClientInfo *ci = NetworkFindClientInfo(client_no);
int i = 0; 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])); GetString(this->action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, lastof(this->action[i]));
this->proc[i++] = &ClientList_SpeakToClient; 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])); GetString(this->action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, lastof(this->action[i]));
this->proc[i++] = &ClientList_SpeakToAll; 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 */ /* 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) { 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])); 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) */ /* 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])); GetString(this->action[i], STR_NETWORK_CLIENTLIST_KICK, lastof(this->action[i]));
this->proc[i++] = &ClientList_Kick; this->proc[i++] = &ClientList_Kick;
@ -1684,7 +1684,7 @@ struct NetworkClientListWindow : Window
colour = TC_BLACK; colour = TC_BLACK;
} }
if (ci->client_index == NETWORK_SERVER_INDEX) { if (ci->client_id == CLIENT_ID_SERVER) {
DrawString(4, y, STR_NETWORK_SERVER, colour); DrawString(4, y, STR_NETWORK_SERVER, colour);
} else { } else {
DrawString(4, y, STR_NETWORK_CLIENT, colour); DrawString(4, y, STR_NETWORK_CLIENT, colour);

View File

@ -144,7 +144,7 @@ void CDECL NetworkTextMessage(NetworkAction action, ConsoleColour color, bool se
void NetworkGetClientName(char *clientname, size_t size, const NetworkTCPSocketHandler *cs); void NetworkGetClientName(char *clientname, size_t size, const NetworkTCPSocketHandler *cs);
uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs); uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs);
byte NetworkGetCurrentLanguageIndex(); byte NetworkGetCurrentLanguageIndex();
NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index); NetworkTCPSocketHandler *NetworkFindClientStateFromClientID(ClientID client_id);
char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last); char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last);
bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]); bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]);

View File

@ -46,14 +46,14 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkTCPSocketHandler
// Packet: SERVER_CLIENT_INFO // Packet: SERVER_CLIENT_INFO
// Function: Sends info about a client // Function: Sends info about a client
// Data: // 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 // uint8: As which company the client is playing
// String: The name of the client // 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); 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_uint8 (ci->client_playas);
p->Send_string(ci->client_name); 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) if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION)
error = NETWORK_ERROR_ILLEGAL_PACKET; 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 { } 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; cs->has_quit = true;
@ -236,7 +236,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME)
// Packet: SERVER_WELCOME // Packet: SERVER_WELCOME
// Function: The client is joined and ready to receive his map // Function: The client is joined and ready to receive his map
// Data: // Data:
// uint16: Own ClientID // uint32: Own Client identifier
// //
Packet *p; Packet *p;
@ -249,7 +249,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME)
_network_game_info.clients_on++; _network_game_info.clients_on++;
p = NetworkSend_Init(PACKET_SERVER_WELCOME); 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_uint32(_settings_game.game_creation.generation_seed);
p->Send_string(_settings_client.network.network_id); p->Send_string(_settings_client.network.network_id);
cs->Send_Packet(p); 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)); SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, DEREF_CLIENT_INFO(new_cs));
} }
// Also send the info of the server // 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) 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 // 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_CLIENT_MAP_OK) Mostly what directly follows is a
// PACKET_SERVER_CLIENT_INFO // PACKET_SERVER_CLIENT_INFO
// Data: // Data:
// uint16: Client-Index // uint32: Client-identifier
// //
Packet *p = NetworkSend_Init(PACKET_SERVER_JOIN); Packet *p = NetworkSend_Init(PACKET_SERVER_JOIN);
p->Send_uint16(client_index); p->Send_uint32(client_id);
cs->Send_Packet(p); cs->Send_Packet(p);
} }
@ -499,60 +499,60 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkTCPSocketHandler *cs
cs->Send_Packet(p); 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 // Packet: SERVER_CHAT
// Function: Sends a chat-packet to the client // Function: Sends a chat-packet to the client
// Data: // Data:
// uint8: ActionID (see network_data.h, NetworkAction) // uint8: ActionID (see network_data.h, NetworkAction)
// uint16: Client-index // uint32: Client-identifier
// String: Message (max NETWORK_CHAT_LENGTH) // String: Message (max NETWORK_CHAT_LENGTH)
// //
Packet *p = NetworkSend_Init(PACKET_SERVER_CHAT); Packet *p = NetworkSend_Init(PACKET_SERVER_CHAT);
p->Send_uint8 (action); p->Send_uint8 (action);
p->Send_uint16(client_index); p->Send_uint32(client_id);
p->Send_bool (self_send); p->Send_bool (self_send);
p->Send_string(msg); p->Send_string(msg);
cs->Send_Packet(p); 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 // Packet: SERVER_ERROR_QUIT
// Function: One of the clients made an error and is quiting the game // Function: One of the clients made an error and is quiting the game
// This packet informs the other clients of that. // This packet informs the other clients of that.
// Data: // Data:
// uint16: Client-index // uint32: Client-identifier
// uint8: ErrorID (see network_data.h, NetworkErrorCode) // uint8: ErrorID (see network_data.h, NetworkErrorCode)
// //
Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR_QUIT); Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR_QUIT);
p->Send_uint16(client_index); p->Send_uint32(client_id);
p->Send_uint8 (errorno); p->Send_uint8 (errorno);
cs->Send_Packet(p); 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 // Packet: SERVER_ERROR_QUIT
// Function: A client left the game, and this packets informs the other clients // Function: A client left the game, and this packets informs the other clients
// of that. // of that.
// Data: // Data:
// uint16: Client-index // uint32: Client-identifier
// String: leave-message // String: leave-message
// //
Packet *p = NetworkSend_Init(PACKET_SERVER_QUIT); Packet *p = NetworkSend_Init(PACKET_SERVER_QUIT);
p->Send_uint16(client_index); p->Send_uint32(client_id);
p->Send_string(leavemsg); p->Send_string(leavemsg);
cs->Send_Packet(p); cs->Send_Packet(p);
@ -800,7 +800,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
FOR_ALL_CLIENTS(new_cs) { FOR_ALL_CLIENTS(new_cs) {
if (new_cs->status > STATUS_AUTH) { if (new_cs->status > STATUS_AUTH) {
SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, DEREF_CLIENT_INFO(cs)); 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 */ /* Now pause the game till the client is in sync */
DoCommandP(0, 1, 0, NULL, CMD_PAUSE); 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 { } else {
// Wrong status for this packet, give a warning to client, and close connection // 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); byte flags = GetCommandFlags(cp->cmd);
if (flags & CMD_SERVER && ci->client_index != NETWORK_SERVER_INDEX) { 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_index, GetClientIP(ci)); IConsolePrintF(CC_ERROR, "WARNING: server only command from client %d (IP: %s), kicking...", ci->client_id, GetClientIP(ci));
return false; return false;
} }
if (flags & CMD_OFFLINE) { 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; return false;
} }
if (cp->cmd != CMD_COMPANY_CTRL && !IsValidCompanyID(cp->company) && ci->client_index != NETWORK_SERVER_INDEX) { 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_index, GetClientIP(ci)); IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_id, GetClientIP(ci));
return false; return false;
} }
@ -879,7 +879,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
/* Check if cp->cmd is valid */ /* Check if cp->cmd is valid */
if (!IsValidCommand(cp->cmd)) { 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); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
free(cp); free(cp);
return; return;
@ -977,7 +977,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
FOR_ALL_CLIENTS(new_cs) { FOR_ALL_CLIENTS(new_cs) {
if (new_cs->status > STATUS_AUTH) { 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) { FOR_ALL_CLIENTS(new_cs) {
if (new_cs->status > STATUS_AUTH) { 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) { if (_settings_client.network.pause_on_join) {
DoCommandP(0, 0, 0, NULL, CMD_PAUSE); 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(); 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; NetworkTCPSocketHandler *cs;
const NetworkClientInfo *ci, *ci_own, *ci_to; const NetworkClientInfo *ci, *ci_own, *ci_to;
@ -1058,32 +1058,32 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co
switch (desttype) { switch (desttype) {
case DESTTYPE_CLIENT: case DESTTYPE_CLIENT:
/* Are we sending to the server? */ /* Are we sending to the server? */
if (dest == NETWORK_SERVER_INDEX) { if ((ClientID)dest == CLIENT_ID_SERVER) {
ci = NetworkFindClientInfoFromIndex(from_index); ci = NetworkFindClientInfoFromIndex(from_id);
/* Display the text locally, and that is it */ /* Display the text locally, and that is it */
if (ci != NULL) if (ci != NULL)
NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg); NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg);
} else { } else {
/* Else find the client to send the message to */ /* Else find the client to send the message to */
FOR_ALL_CLIENTS(cs) { FOR_ALL_CLIENTS(cs) {
if (cs->index == dest) { if (cs->client_id == (ClientID)dest) {
SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg);
break; break;
} }
} }
} }
// Display the message locally (so you know you have sent it) // Display the message locally (so you know you have sent it)
if (from_index != dest) { if (from_id != (ClientID)dest) {
if (from_index == NETWORK_SERVER_INDEX) { if (from_id == CLIENT_ID_SERVER) {
ci = NetworkFindClientInfoFromIndex(from_index); ci = NetworkFindClientInfoFromIndex(from_id);
ci_to = NetworkFindClientInfoFromIndex(dest); ci_to = NetworkFindClientInfoFromIndex((ClientID)dest);
if (ci != NULL && ci_to != NULL) if (ci != NULL && ci_to != NULL)
NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), true, ci_to->client_name, "%s", msg); NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), true, ci_to->client_name, "%s", msg);
} else { } else {
FOR_ALL_CLIENTS(cs) { FOR_ALL_CLIENTS(cs) {
if (cs->index == from_index) { if (cs->client_id == from_id) {
SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, dest, true, msg); SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, (ClientID)dest, true, msg);
break; break;
} }
} }
@ -1097,18 +1097,18 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co
ci_to = NULL; ci_to = NULL;
FOR_ALL_CLIENTS(cs) { FOR_ALL_CLIENTS(cs) {
ci = DEREF_CLIENT_INFO(cs); ci = DEREF_CLIENT_INFO(cs);
if (ci->client_playas == dest) { if (ci->client_playas == (CompanyID)dest) {
SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg); SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg);
if (cs->index == from_index) show_local = false; if (cs->client_id == from_id) show_local = false;
ci_to = ci; // Remember a client that is in the company for company-name ci_to = ci; // Remember a client that is in the company for company-name
} }
} }
ci = NetworkFindClientInfoFromIndex(from_index); ci = NetworkFindClientInfoFromIndex(from_id);
ci_own = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); ci_own = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER);
if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) { if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) {
NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg); 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; 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) // Display the message locally (so you know you have sent it)
if (ci != NULL && show_local) { if (ci != NULL && show_local) {
if (from_index == NETWORK_SERVER_INDEX) { if (from_id == CLIENT_ID_SERVER) {
char name[NETWORK_NAME_LENGTH]; char name[NETWORK_NAME_LENGTH];
StringID str = IsValidCompanyID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS; StringID str = IsValidCompanyID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
SetDParam(0, ci_to->client_playas); 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); NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci_own->client_playas), true, name, "%s", msg);
} else { } else {
FOR_ALL_CLIENTS(cs) { FOR_ALL_CLIENTS(cs) {
if (cs->index == from_index) { if (cs->client_id == from_id) {
SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, ci_to->client_index, true, msg); 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 */ /* fall-through to next case */
case DESTTYPE_BROADCAST: case DESTTYPE_BROADCAST:
FOR_ALL_CLIENTS(cs) { 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) if (ci != NULL)
NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg); NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg);
break; break;
@ -1157,7 +1157,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
NetworkAction action = (NetworkAction)p->Recv_uint8(); NetworkAction action = (NetworkAction)p->Recv_uint8();
DestType desttype = (DestType)p->Recv_uint8(); DestType desttype = (DestType)p->Recv_uint8();
int dest = p->Recv_uint16(); int dest = p->Recv_uint32();
char msg[NETWORK_CHAT_LENGTH]; char msg[NETWORK_CHAT_LENGTH];
p->Recv_string(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:
case NETWORK_ACTION_CHAT_CLIENT: case NETWORK_ACTION_CHAT_CLIENT:
case NETWORK_ACTION_CHAT_COMPANY: case NETWORK_ACTION_CHAT_COMPANY:
NetworkServerSendChat(action, desttype, dest, msg, cs->index); NetworkServerSendChat(action, desttype, dest, msg, cs->client_id);
break; break;
default: 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); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
break; break;
} }
@ -1219,7 +1219,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME)
if (NetworkFindName(client_name)) { if (NetworkFindName(client_name)) {
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", 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)); 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)); p->Recv_string(command, sizeof(command));
if (strcmp(pass, _settings_client.network.rcon_password) != 0) { 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; 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); IConsoleCmdExec(command);
_redirect_console_to_client = 0; _redirect_console_to_client = INVALID_CLIENT_ID;
return; return;
} }
@ -1369,7 +1369,7 @@ void NetworkPopulateCompanyInfo()
} }
} }
ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER);
// Register local company (if not dedicated) // Register local company (if not dedicated)
if (ci != NULL && IsValidCompanyID(ci->client_playas)) 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)); 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 // Send a packet to all clients with updated info about this client_id
void NetworkUpdateClientInfo(uint16 client_index) void NetworkUpdateClientInfo(ClientID client_id)
{ {
NetworkTCPSocketHandler *cs; NetworkTCPSocketHandler *cs;
NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_index); NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_id);
if (ci == NULL) return; if (ci == NULL) return;
@ -1436,7 +1436,7 @@ static void NetworkAutoCleanCompanies()
} }
if (!_network_dedicated) { 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; 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 // Check if it is the same as the server-name
ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER);
if (ci != NULL) { if (ci != NULL) {
if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use 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) { if (lag > 3) {
// Client did still not report in after 4 game-day, drop him // Client did still not report in after 4 game-day, drop him
// (that is, the 3 of above, + 1 before any lag is counted) // (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); NetworkCloseClient(cs);
continue; continue;
} }
// Report once per time we detect the lag // Report once per time we detect the lag
if (cs->lag_test == 0) { 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; cs->lag_test = 1;
} }
} else { } else {
@ -1584,13 +1584,13 @@ void NetworkServer_Tick(bool send_frame)
} else if (cs->status == STATUS_PRE_ACTIVE) { } else if (cs->status == STATUS_PRE_ACTIVE) {
int lag = NetworkCalculateLag(cs); int lag = NetworkCalculateLag(cs);
if (lag > _settings_client.network.max_join_time) { 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); NetworkCloseClient(cs);
} }
} else if (cs->status == STATUS_INACTIVE) { } else if (cs->status == STATUS_INACTIVE) {
int lag = NetworkCalculateLag(cs); int lag = NetworkCalculateLag(cs);
if (lag > 4 * DAY_TICKS) { 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); NetworkCloseClient(cs);
} }
} }
@ -1628,12 +1628,12 @@ void NetworkServerChangeOwner(Owner current_owner, Owner new_owner)
/* The server has to handle all administrative issues, for example /* The server has to handle all administrative issues, for example
* updating and notifying all clients of what has happened */ * updating and notifying all clients of what has happened */
NetworkTCPSocketHandler *cs; NetworkTCPSocketHandler *cs;
NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER);
/* The server has just changed from owner */ /* The server has just changed from owner */
if (current_owner == ci->client_playas) { if (current_owner == ci->client_playas) {
ci->client_playas = new_owner; 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 */ /* 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); ci = DEREF_CLIENT_INFO(cs);
if (current_owner == ci->client_playas) { if (current_owner == ci->client_playas) {
ci->client_playas = new_owner; 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"); 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'", 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), ci->client_playas + (IsValidCompanyID(ci->client_playas) ? 1 : 0),
GetClientIP(ci), ci->unique_id); 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) bool NetworkCompanyHasClients(CompanyID company)

View File

@ -8,7 +8,7 @@
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP); 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_PARAM(PACKET_SERVER_ERROR)(NetworkTCPSocketHandler *cs, NetworkErrorCode error);
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN); DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN);
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME); DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME);

View File

@ -20,18 +20,25 @@ enum {
*/ */
MAX_CLIENTS = MAX_COMPANIES + 3, 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. */ /** Maximum number of internet interfaces supported. */
MAX_INTERFACES = 9, MAX_INTERFACES = 9,
/** How many vehicle/station types we put over the network */ /** How many vehicle/station types we put over the network */
NETWORK_VEHICLE_TYPES = 5, NETWORK_VEHICLE_TYPES = 5,
NETWORK_STATION_TYPES = 5, NETWORK_STATION_TYPES = 5,
};
NETWORK_SERVER_INDEX = 1, /** 'Unique' identifier to be given to clients */
NETWORK_EMPTY_INDEX = 0, 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 { struct NetworkCompanyInfo {
@ -50,7 +57,7 @@ struct NetworkCompanyInfo {
}; };
struct NetworkClientInfo { 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 char client_name[NETWORK_CLIENT_NAME_LENGTH]; ///< Name of the client
byte client_lang; ///< The language of the client byte client_lang; ///< The language of the client
CompanyID client_playas; ///< As which company is this client playing (CompanyID) CompanyID client_playas; ///< As which company is this client playing (CompanyID)