diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/network/core/tcp_listen.h | 5 | ||||
-rw-r--r-- | src/network/network.cpp | 38 | ||||
-rw-r--r-- | src/network/network_admin.cpp | 47 | ||||
-rw-r--r-- | src/network/network_admin.h | 32 | ||||
-rw-r--r-- | src/network/network_base.h | 13 | ||||
-rw-r--r-- | src/network/network_chat_gui.cpp | 3 | ||||
-rw-r--r-- | src/network/network_client.cpp | 6 | ||||
-rw-r--r-- | src/network/network_command.cpp | 10 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 20 | ||||
-rw-r--r-- | src/network/network_server.cpp | 87 | ||||
-rw-r--r-- | src/network/network_server.h | 13 | ||||
-rw-r--r-- | src/script/api/script_clientlist.cpp | 6 |
12 files changed, 89 insertions, 191 deletions
diff --git a/src/network/core/tcp_listen.h b/src/network/core/tcp_listen.h index 6c1d3e4ff..d2982363e 100644 --- a/src/network/core/tcp_listen.h +++ b/src/network/core/tcp_listen.h @@ -102,8 +102,7 @@ public: FD_ZERO(&write_fd); - Tsocket *cs; - FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) { + for (Tsocket *cs : Tsocket::Iterate()) { FD_SET(cs->sock, &read_fd); FD_SET(cs->sock, &write_fd); } @@ -122,7 +121,7 @@ public: } /* read stuff from clients */ - FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) { + for (Tsocket *cs : Tsocket::Iterate()) { cs->writable = !!FD_ISSET(cs->sock, &write_fd); if (FD_ISSET(cs->sock, &read_fd)) { cs->ReceivePackets(); diff --git a/src/network/network.cpp b/src/network/network.cpp index e7ce387cc..a27aa224a 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -99,8 +99,7 @@ extern void StateGameLoop(); */ bool HasClients() { - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) return true; + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) return true; return false; } @@ -121,9 +120,7 @@ NetworkClientInfo::~NetworkClientInfo() */ /* static */ NetworkClientInfo *NetworkClientInfo::GetByClientID(ClientID client_id) { - NetworkClientInfo *ci; - - FOR_ALL_CLIENT_INFOS(ci) { + for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (ci->client_id == client_id) return ci; } @@ -137,9 +134,7 @@ NetworkClientInfo::~NetworkClientInfo() */ /* static */ ServerNetworkGameSocketHandler *ServerNetworkGameSocketHandler::GetByClientID(ClientID client_id) { - NetworkClientSocket *cs; - - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->client_id == client_id) return cs; } @@ -148,10 +143,9 @@ NetworkClientInfo::~NetworkClientInfo() byte NetworkSpectatorCount() { - const NetworkClientInfo *ci; byte count = 0; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (ci->client_playas == COMPANY_SPECTATOR) count++; } @@ -408,10 +402,9 @@ static void CheckPauseHelper(bool pause, PauseMode pm) */ static uint NetworkCountActiveClients() { - const NetworkClientSocket *cs; uint count = 0; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue; if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue; count++; @@ -439,8 +432,7 @@ static void CheckMinActiveClients() */ static bool NetworkHasJoiningClient() { - const NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true; } @@ -527,14 +519,12 @@ void NetworkClose(bool close_admins) { if (_network_server) { if (close_admins) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::Iterate()) { as->CloseConnection(true); } } - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST); } ServerNetworkGameSocketHandler::CloseListeners(); @@ -768,14 +758,12 @@ bool NetworkServerStart() void NetworkReboot() { if (_network_server) { - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { cs->SendNewGame(); cs->SendPackets(); } - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { as->SendNewGame(); as->SendPackets(); } @@ -794,15 +782,13 @@ void NetworkReboot() void NetworkDisconnect(bool blocking, bool close_admins) { if (_network_server) { - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { cs->SendShutdown(); cs->SendPackets(); } if (close_admins) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { as->SendShutdown(); as->SendPackets(); } diff --git a/src/network/network_admin.cpp b/src/network/network_admin.cpp index 037307627..f304740a6 100644 --- a/src/network/network_admin.cpp +++ b/src/network/network_admin.cpp @@ -94,8 +94,7 @@ ServerNetworkAdminSocketHandler::~ServerNetworkAdminSocketHandler() /** Send the packets for the server sockets. */ /* static */ void ServerNetworkAdminSocketHandler::Send() { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::Iterate()) { if (as->status == ADMIN_STATUS_INACTIVE && as->realtime_connect + ADMIN_AUTHORISATION_TIMEOUT < _realtime_tick) { DEBUG(net, 1, "[admin] Admin did not send its authorisation within %d seconds", ADMIN_AUTHORISATION_TIMEOUT / 1000); as->CloseConnection(true); @@ -727,17 +726,16 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_POLL(Packet *p) case ADMIN_UPDATE_CLIENT_INFO: /* The admin is requesting client info. */ - const NetworkClientSocket *cs; if (d1 == UINT32_MAX) { this->SendClientInfo(nullptr, NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER)); - FOR_ALL_CLIENT_SOCKETS(cs) { + for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { this->SendClientInfo(cs, cs->GetInfo()); } } else { if (d1 == CLIENT_ID_SERVER) { this->SendClientInfo(nullptr, NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER)); } else { - cs = NetworkClientSocket::GetByClientID((ClientID)d1); + const NetworkClientSocket *cs = NetworkClientSocket::GetByClientID((ClientID)d1); if (cs != nullptr) this->SendClientInfo(cs, cs->GetInfo()); } } @@ -817,8 +815,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_CHAT(Packet *p) */ void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendClientInfo(cs, cs->GetInfo()); if (new_client) { @@ -834,8 +831,7 @@ void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client) */ void NetworkAdminClientUpdate(const NetworkClientInfo *ci) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendClientUpdate(ci); } @@ -848,8 +844,7 @@ void NetworkAdminClientUpdate(const NetworkClientInfo *ci) */ void NetworkAdminClientQuit(ClientID client_id) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendClientQuit(client_id); } @@ -863,8 +858,7 @@ void NetworkAdminClientQuit(ClientID client_id) */ void NetworkAdminClientError(ClientID client_id, NetworkErrorCode error_code) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_CLIENT_INFO] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendClientError(client_id, error_code); } @@ -883,8 +877,7 @@ void NetworkAdminCompanyInfo(const Company *company, bool new_company) return; } - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_COMPANY_INFO] != ADMIN_FREQUENCY_AUTOMATIC) continue; as->SendCompanyInfo(company); @@ -902,8 +895,7 @@ void NetworkAdminCompanyUpdate(const Company *company) { if (company == nullptr) return; - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_COMPANY_INFO] != ADMIN_FREQUENCY_AUTOMATIC) continue; as->SendCompanyUpdate(company); @@ -917,8 +909,7 @@ void NetworkAdminCompanyUpdate(const Company *company) */ void NetworkAdminCompanyRemove(CompanyID company_id, AdminCompanyRemoveReason bcrr) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { as->SendCompanyRemove(company_id, bcrr); } } @@ -931,8 +922,7 @@ void NetworkAdminChat(NetworkAction action, DestType desttype, ClientID client_i { if (from_admin) return; - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_CHAT] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendChat(action, desttype, client_id, msg, data); } @@ -957,8 +947,7 @@ void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, */ void NetworkAdminConsole(const char *origin, const char *string) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_CONSOLE] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendConsole(origin, string); } @@ -971,8 +960,7 @@ void NetworkAdminConsole(const char *origin, const char *string) */ void NetworkAdminGameScript(const char *json) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_GAMESCRIPT] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendGameScript(json); } @@ -988,8 +976,7 @@ void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacke { ClientID client_id = owner == nullptr ? _network_own_client_id : owner->client_id; - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { if (as->update_frequency[ADMIN_UPDATE_CMD_LOGGING] & ADMIN_FREQUENCY_AUTOMATIC) { as->SendCmdLogging(client_id, cp); } @@ -1001,8 +988,7 @@ void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacke */ void ServerNetworkAdminSocketHandler::WelcomeAll() { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { as->SendWelcome(); } } @@ -1013,8 +999,7 @@ void ServerNetworkAdminSocketHandler::WelcomeAll() */ void NetworkAdminUpdate(AdminUpdateFrequency freq) { - ServerNetworkAdminSocketHandler *as; - FOR_ALL_ACTIVE_ADMIN_SOCKETS(as) { + for (ServerNetworkAdminSocketHandler *as : ServerNetworkAdminSocketHandler::IterateActive()) { for (int i = 0; i < ADMIN_UPDATE_END; i++) { if (as->update_frequency[i] & freq) { /* Update the admin for the required details */ diff --git a/src/network/network_admin.h b/src/network/network_admin.h index bf3dc2242..98dbab481 100644 --- a/src/network/network_admin.h +++ b/src/network/network_admin.h @@ -82,28 +82,18 @@ public: { return "admin"; } -}; - -/** - * Iterate over all the sockets from a given starting point. - * @param var The variable to iterate with. - * @param start The start of the iteration. - */ -#define FOR_ALL_ADMIN_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(ServerNetworkAdminSocketHandler, adminsocket_index, var, start) -/** - * Iterate over all the sockets. - * @param var The variable to iterate with. - */ -#define FOR_ALL_ADMIN_SOCKETS(var) FOR_ALL_ADMIN_SOCKETS_FROM(var, 0) - -/** - * Iterate over all the active sockets. - * @param var The variable to iterate with. - */ -#define FOR_ALL_ACTIVE_ADMIN_SOCKETS(var) \ - FOR_ALL_ADMIN_SOCKETS(var) \ - if (var->GetAdminStatus() == ADMIN_STATUS_ACTIVE) + /** + * Returns an iterable ensemble of all active admin sockets + * @param from index of the first socket to consider + * @return an iterable ensemble of all active admin sockets + */ + static Pool::IterateWrapper<ServerNetworkAdminSocketHandler> IterateActive(size_t from = 0) + { + return Pool::IterateWrapper<ServerNetworkAdminSocketHandler>(from, + [](size_t index) { return ServerNetworkAdminSocketHandler::Get(index)->GetAdminStatus() == ADMIN_STATUS_ACTIVE; }); + } +}; void NetworkAdminClientInfo(const NetworkClientSocket *cs, bool new_client = false); void NetworkAdminClientUpdate(const NetworkClientInfo *ci); diff --git a/src/network/network_base.h b/src/network/network_base.h index 629f28734..cbb7e88f2 100644 --- a/src/network/network_base.h +++ b/src/network/network_base.h @@ -37,17 +37,4 @@ struct NetworkClientInfo : NetworkClientInfoPool::PoolItem<&_networkclientinfo_p static NetworkClientInfo *GetByClientID(ClientID client_id); }; -/** - * Iterate over all the clients from a given index. - * @param var The variable to iterate with. - * @param start The location to start the iteration from. - */ -#define FOR_ALL_CLIENT_INFOS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientInfo, clientinfo_index, var, start) - -/** - * Iterate over all the clients. - * @param var The variable to iterate with. - */ -#define FOR_ALL_CLIENT_INFOS(var) FOR_ALL_CLIENT_INFOS_FROM(var, 0) - #endif /* NETWORK_BASE_H */ diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 80bf01411..adaf6978c 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -336,8 +336,7 @@ struct NetworkChatWindow : public Window { /* First, try clients */ if (*item < MAX_CLIENT_SLOTS) { /* Skip inactive clients */ - NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS_FROM(ci, *item) { + for (NetworkClientInfo *ci : NetworkClientInfo::Iterate(*item)) { *item = ci->index; return ci->client_name; } diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 5cbcb4c3b..08ec7823e 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -1234,8 +1234,7 @@ void NetworkClientsToSpectators(CompanyID cid) /* If our company is changing owner, go to spectators */ if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR); - NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (ci->client_playas != cid) continue; NetworkTextMessage(NETWORK_ACTION_COMPANY_SPECTATOR, CC_DEFAULT, false, ci->client_name); ci->client_playas = COMPANY_SPECTATOR; @@ -1299,8 +1298,7 @@ bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio) /* Only companies actually playing can speak to team. Eg spectators cannot */ if (!_settings_client.gui.prefer_teamchat || !Company::IsValidID(cio->client_playas)) return false; - const NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (ci->client_playas == cio->client_playas && ci != cio) return true; } diff --git a/src/network/network_command.cpp b/src/network/network_command.cpp index 0994c50a8..5d36a39b9 100644 --- a/src/network/network_command.cpp +++ b/src/network/network_command.cpp @@ -238,8 +238,7 @@ static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket CommandCallback *callback = cp.callback; cp.frame = _frame_counter_max + 1; - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->status >= NetworkClientSocket::STATUS_MAP) { /* Callbacks are only send back to the client who sent them in the * first place. This filters that out. */ @@ -249,8 +248,8 @@ static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket } } - cp.callback = (cs != owner) ? nullptr : callback; - cp.my_cmd = (cs == owner); + cp.callback = (nullptr != owner) ? nullptr : callback; + cp.my_cmd = (nullptr == owner); _local_execution_queue.Append(&cp); } @@ -283,8 +282,7 @@ void NetworkDistributeCommands() DistributeQueue(&_local_wait_queue, nullptr); /* Then send the queues of the others. */ - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { DistributeQueue(&cs->incoming_queue, cs); } } diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index fde95be1a..2faf5a395 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1883,10 +1883,9 @@ struct NetworkClientListWindow : Window { bool CheckClientListHeight() { int num = 0; - const NetworkClientInfo *ci; /* Should be replaced with a loop through all clients */ - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++; } @@ -1910,8 +1909,7 @@ struct NetworkClientListWindow : Window { this->line_height = max(this->icon_size.height + 2U, (uint)FONT_HEIGHT_NORMAL); uint width = 100; // Default width - const NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { width = max(width, GetStringBoundingBox(ci->client_name).width); } @@ -1947,8 +1945,7 @@ struct NetworkClientListWindow : Window { uint name_right = rtl ? right - type_icon_width : right; int i = 0; - const NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { TextColour colour; if (this->selected_item == i++) { // Selected item, highlight it GfxFillRect(r.left + 1, y, r.right - 1, y + this->line_height - 1, PC_BLACK); @@ -1976,15 +1973,14 @@ struct NetworkClientListWindow : Window { { /* Show the popup with option */ if (this->selected_item != -1) { - NetworkClientInfo *ci; - int client_no = this->selected_item; - FOR_ALL_CLIENT_INFOS(ci) { - if (client_no == 0) break; + for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { + if (client_no == 0) { + PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top); + break; + } client_no--; } - - if (ci != nullptr) PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top); } } diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 7eabf24f5..42a5ab0fc 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -258,14 +258,13 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta if (status != NETWORK_RECV_STATUS_CONN_LOST && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) { /* We did not receive a leave message from this client... */ char client_name[NETWORK_CLIENT_NAME_LENGTH]; - NetworkClientSocket *new_cs; this->GetClientName(client_name, lastof(client_name)); NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST); /* Inform other clients of this... strange leaving ;) */ - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status > STATUS_AUTHORIZED && this != new_cs) { new_cs->SendErrorQuit(this->client_id, NETWORK_ERROR_CONNECTION_LOST); } @@ -310,8 +309,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta /** Send the packets for the server sockets. */ /* static */ void ServerNetworkGameSocketHandler::Send() { - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->writable) { if (cs->SendPackets() != SPS_CLOSED && cs->status == STATUS_MAP) { /* This client is in the middle of a map-send, call the function for that */ @@ -354,7 +352,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyInfo() /* Make a list of all clients per company */ char clients[MAX_COMPANIES][NETWORK_CLIENTS_LENGTH]; - NetworkClientSocket *csi; memset(clients, 0, sizeof(clients)); /* Add the local player (if not dedicated) */ @@ -363,7 +360,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyInfo() strecpy(clients[ci->client_playas], ci->client_name, lastof(clients[ci->client_playas])); } - FOR_ALL_CLIENT_SOCKETS(csi) { + for (NetworkClientSocket *csi : NetworkClientSocket::Iterate()) { char client_name[NETWORK_CLIENT_NAME_LENGTH]; ((ServerNetworkGameSocketHandler*)csi)->GetClientName(client_name, lastof(client_name)); @@ -424,7 +421,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode err /* Only send when the current client was in game */ if (this->status > STATUS_AUTHORIZED) { - NetworkClientSocket *new_cs; char client_name[NETWORK_CLIENT_NAME_LENGTH]; this->GetClientName(client_name, lastof(client_name)); @@ -433,7 +429,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode err NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, strid); - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status > STATUS_AUTHORIZED && new_cs != this) { /* Some errors we filter to a more general error. Clients don't have to know the real * reason a joining failed. */ @@ -509,7 +505,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedCompanyPassword() NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome() { Packet *p; - NetworkClientSocket *new_cs; /* Invalid packet when status is AUTH or higher */ if (this->status >= STATUS_AUTHORIZED) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); @@ -527,7 +522,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome() this->SendPacket(p); /* Transmit info about all the active clients */ - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs != this && new_cs->status > STATUS_AUTHORIZED) { this->SendClientInfo(new_cs->GetInfo()); } @@ -540,11 +535,10 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome() NetworkRecvStatus ServerNetworkGameSocketHandler::SendWait() { int waiting = 0; - NetworkClientSocket *new_cs; Packet *p; /* Count how many clients are waiting in the queue, in front of you! */ - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status != STATUS_MAP_WAIT) continue; if (new_cs->GetInfo()->join_date < this->GetInfo()->join_date || (new_cs->GetInfo()->join_date == this->GetInfo()->join_date && new_cs->client_id < this->client_id)) waiting++; } @@ -611,9 +605,8 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() this->status = STATUS_DONE_MAP; /* Find the best candidate for joining, i.e. the first joiner. */ - NetworkClientSocket *new_cs; NetworkClientSocket *best = nullptr; - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status == STATUS_MAP_WAIT) { if (best == nullptr || best->GetInfo()->join_date > new_cs->GetInfo()->join_date || (best->GetInfo()->join_date == new_cs->GetInfo()->join_date && best->client_id > new_cs->client_id)) { best = new_cs; @@ -628,7 +621,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() best->SendMap(); /* And update the rest. */ - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status == STATUS_MAP_WAIT) new_cs->SendWait(); } } @@ -1000,7 +993,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWOR NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP(Packet *p) { - NetworkClientSocket *new_cs; /* The client was never joined.. so this is impossible, right? * Ignore the packet, give the client a warning, and close his connection */ if (this->status < STATUS_AUTHORIZED || this->HasClientQuit()) { @@ -1008,7 +1000,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP(Packet * } /* Check if someone else is receiving the map */ - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status == STATUS_MAP) { /* Tell the new client to wait */ this->status = STATUS_MAP_WAIT; @@ -1025,7 +1017,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet * /* Client has the map, now start syncing */ if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) { char client_name[NETWORK_CLIENT_NAME_LENGTH]; - NetworkClientSocket *new_cs; this->GetClientName(client_name, lastof(client_name)); @@ -1043,7 +1034,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet * this->last_frame = _frame_counter; this->last_frame_server = _frame_counter; - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status > STATUS_AUTHORIZED) { new_cs->SendClientInfo(this->GetInfo()); new_cs->SendJoin(this->client_id); @@ -1135,7 +1126,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p { /* This packets means a client noticed an error and is reporting this * to us. Display the error and report it to the other clients */ - NetworkClientSocket *new_cs; char str[100]; char client_name[NETWORK_CLIENT_NAME_LENGTH]; NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8(); @@ -1154,7 +1144,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, strid); - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status > STATUS_AUTHORIZED) { new_cs->SendErrorQuit(this->client_id, errorno); } @@ -1169,7 +1159,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet *p) { /* The client wants to leave. Display this and report it to the other * clients. */ - NetworkClientSocket *new_cs; char client_name[NETWORK_CLIENT_NAME_LENGTH]; /* The client was never joined.. thank the client for the packet, but ignore it */ @@ -1181,7 +1170,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet *p) NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, nullptr, STR_NETWORK_MESSAGE_CLIENT_LEAVING); - FOR_ALL_CLIENT_SOCKETS(new_cs) { + for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (new_cs->status > STATUS_AUTHORIZED && new_cs != this) { new_cs->SendQuit(this->client_id); } @@ -1250,7 +1239,6 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ACK(Packet *p) */ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, ClientID from_id, int64 data, bool from_admin) { - NetworkClientSocket *cs; const NetworkClientInfo *ci, *ci_own, *ci_to; switch (desttype) { @@ -1268,7 +1256,7 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co } } else { /* Else find the client to send the message to */ - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->client_id == (ClientID)dest) { cs->SendChat(action, from_id, false, msg, data); break; @@ -1285,7 +1273,7 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), true, ci_to->client_name, msg, data); } } else { - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->client_id == from_id) { cs->SendChat(action, (ClientID)dest, true, msg, data); break; @@ -1299,7 +1287,7 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co bool show_local = true; /* Find all clients that belong to this company */ ci_to = nullptr; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { ci = cs->GetInfo(); if (ci != nullptr && ci->client_playas == (CompanyID)dest) { cs->SendChat(action, from_id, false, msg, data); @@ -1333,7 +1321,7 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co GetString(name, str, lastof(name)); NetworkTextMessage(action, GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data); } else { - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->client_id == from_id) { cs->SendChat(action, ci_to->client_id, true, msg, data); } @@ -1347,7 +1335,7 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co FALLTHROUGH; case DESTTYPE_BROADCAST: - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { cs->SendChat(action, from_id, false, msg, data); } @@ -1584,14 +1572,13 @@ void NetworkPopulateCompanyStats(NetworkCompanyStats *stats) */ void NetworkUpdateClientInfo(ClientID client_id) { - NetworkClientSocket *cs; NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id); if (ci == nullptr) return; DEBUG(desync, 1, "client: %08x; %02x; %02x; %04x", _date, _date_fract, (int)ci->client_playas, client_id); - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { cs->SendClientInfo(ci); } @@ -1616,7 +1603,6 @@ static void NetworkCheckRestartMap() */ static void NetworkAutoCleanCompanies() { - const NetworkClientInfo *ci; bool clients_in_company[MAX_COMPANIES]; int vehicles_in_company[MAX_COMPANIES]; @@ -1625,12 +1611,12 @@ static void NetworkAutoCleanCompanies() memset(clients_in_company, 0, sizeof(clients_in_company)); /* Detect the active companies */ - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true; } if (!_network_dedicated) { - ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); + const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true; } @@ -1695,10 +1681,8 @@ bool NetworkFindName(char *new_name, const char *last) strecpy(original_name, new_name, lastof(original_name)); while (!found_name) { - const NetworkClientInfo *ci; - found_name = true; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (strcmp(ci->client_name, new_name) == 0) { /* Name already in use */ found_name = false; @@ -1706,7 +1690,7 @@ bool NetworkFindName(char *new_name, const char *last) } } /* Check if it is the same as the server-name */ - ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); + const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); if (ci != nullptr) { if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use } @@ -1731,13 +1715,12 @@ bool NetworkFindName(char *new_name, const char *last) */ bool NetworkServerChangeClientName(ClientID client_id, const char *new_name) { - NetworkClientInfo *ci; /* Check if the name's already in use */ - FOR_ALL_CLIENT_INFOS(ci) { + for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (strcmp(ci->client_name, new_name) == 0) return false; } - ci = NetworkClientInfo::GetByClientID(client_id); + NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id); if (ci == nullptr) return false; NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, true, ci->client_name, new_name); @@ -1785,7 +1768,6 @@ static void NetworkHandleCommandQueue(NetworkClientSocket *cs) */ void NetworkServer_Tick(bool send_frame) { - NetworkClientSocket *cs; #ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME bool send_sync = false; #endif @@ -1799,7 +1781,7 @@ void NetworkServer_Tick(bool send_frame) /* Now we are done with the frame, inform the clients that they can * do their frame! */ - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { /* We allow a number of bytes per frame, but only to the burst amount * to be available for packet receiving at any particular time. */ cs->receive_limit = min(cs->receive_limit + _settings_client.network.bytes_per_frame, @@ -1956,8 +1938,7 @@ void NetworkServerShowStatusToConsole() }; assert_compile(lengthof(stat_str) == NetworkClientSocket::STATUS_END); - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { NetworkClientInfo *ci = cs->GetInfo(); if (ci == nullptr) continue; uint lag = NetworkCalculateLag(cs); @@ -1976,9 +1957,7 @@ void NetworkServerShowStatusToConsole() */ void NetworkServerSendConfigUpdate() { - NetworkClientSocket *cs; - - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendConfigUpdate(); } } @@ -1995,8 +1974,7 @@ void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded) SB(_network_company_passworded, company_id, 1, !!passworded); SetWindowClassesDirty(WC_COMPANY); - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendCompanyUpdate(); } @@ -2090,8 +2068,7 @@ uint NetworkServerKickOrBanIP(const char *ip, bool ban) uint n = 0; /* There can be multiple clients with the same IP, kick them all */ - NetworkClientSocket *cs; - FOR_ALL_CLIENT_SOCKETS(cs) { + for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->client_id == CLIENT_ID_SERVER) continue; if (cs->client_address.IsInNetmask(ip)) { NetworkServerKickClient(cs->client_id); @@ -2109,8 +2086,7 @@ uint NetworkServerKickOrBanIP(const char *ip, bool ban) */ bool NetworkCompanyHasClients(CompanyID company) { - const NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (ci->client_playas == company) return true; } return false; @@ -2138,8 +2114,7 @@ void ServerNetworkGameSocketHandler::GetClientName(char *client_name, const char */ void NetworkPrintClients() { - NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (_network_server) { IConsolePrintF(CC_INFO, "Client #%1d name: '%s' company: %1d IP: %s", ci->client_id, diff --git a/src/network/network_server.h b/src/network/network_server.h index d44598cc3..9a1873520 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -120,17 +120,4 @@ void NetworkServer_Tick(bool send_frame); void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true); void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded); -/** - * Iterate over all the sockets from a given starting point. - * @param var The variable to iterate with. - * @param start The start of the iteration. - */ -#define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start) - -/** - * Iterate over all the sockets. - * @param var The variable to iterate with. - */ -#define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0) - #endif /* NETWORK_SERVER_H */ diff --git a/src/script/api/script_clientlist.cpp b/src/script/api/script_clientlist.cpp index 38d9bae49..eb57d8e44 100644 --- a/src/script/api/script_clientlist.cpp +++ b/src/script/api/script_clientlist.cpp @@ -18,8 +18,7 @@ ScriptClientList::ScriptClientList() { if (!_networking) return; - NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { this->AddItem(ci->client_id); } } @@ -36,8 +35,7 @@ ScriptClientList_Company::ScriptClientList_Company(ScriptCompany::CompanyID comp c = (CompanyID)company; } - NetworkClientInfo *ci; - FOR_ALL_CLIENT_INFOS(ci) { + for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { if (ci->client_playas == c) this->AddItem(ci->client_id); } } |