From dbc0aa4cdb5595801903762e284b071fa4cfbd10 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 21 May 2026 21:32:42 +0100 Subject: [PATCH] Codechange: make ClientStatus and ServerStatus scoped enums (#15578) --- src/core/random_func.cpp | 2 +- src/network/network.cpp | 6 +- src/network/network_client.cpp | 70 +++++++-------- src/network/network_client.h | 24 ++--- src/network/network_command.cpp | 2 +- src/network/network_server.cpp | 153 ++++++++++++++++---------------- src/network/network_server.h | 26 +++--- 7 files changed, 141 insertions(+), 142 deletions(-) diff --git a/src/core/random_func.cpp b/src/core/random_func.cpp index cbf8cc93ec..aae3ae2660 100644 --- a/src/core/random_func.cpp +++ b/src/core/random_func.cpp @@ -71,7 +71,7 @@ void SetRandomSeed(uint32_t seed) #ifdef RANDOM_DEBUG uint32_t Random(const std::source_location location) { - if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) { + if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::ClientStatus::Inactive))) { Debug(random, 0, "{:08x}; {:02x}; {:04x}; {:02x}; {}:{}", TimerGameEconomy::date, TimerGameEconomy::date_fract, _frame_counter, _current_company, location.file_name(), location.line()); } diff --git a/src/network/network.cpp b/src/network/network.cpp index 338f5b68c7..1678571cb8 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -424,7 +424,7 @@ static void CheckPauseHelper(bool pause, PauseMode pm) /** * Counts the number of active clients connected. - * It has to be in STATUS_ACTIVE and not a spectator + * It has to be in \c ClientStatus::Active and not a spectator * @return number of active clients */ static uint NetworkCountActiveClients() @@ -432,7 +432,7 @@ static uint NetworkCountActiveClients() uint count = 0; for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->status != NetworkClientSocket::STATUS_ACTIVE) continue; + if (cs->status != NetworkClientSocket::ClientStatus::Active) continue; if (!Company::IsValidID(cs->GetInfo()->client_playas)) continue; count++; } @@ -460,7 +460,7 @@ static void CheckMinActiveClients() static bool NetworkHasJoiningClient() { for (const NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->status >= NetworkClientSocket::STATUS_AUTHORIZED && cs->status < NetworkClientSocket::STATUS_ACTIVE) return true; + if (cs->status >= NetworkClientSocket::ClientStatus::Authorized && cs->status < NetworkClientSocket::ClientStatus::Active) return true; } return false; diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 92bc4d3579..eecfcd1517 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -289,7 +289,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendJoin() Debug(net, 9, "Client::SendJoin()"); Debug(net, 9, "Client::status = JOIN"); - my_client->status = STATUS_JOIN; + my_client->status = ServerStatus::Join; Debug(net, 9, "Client::join_status = Authorizing"); _network_join_status = NetworkJoinStatus::Authorizing; SetWindowDirty(WindowClass::NetworkStatus, NetworkStatusWindowNumber::Join); @@ -354,7 +354,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendGetMap() Debug(net, 9, "Client::SendGetMap()"); Debug(net, 9, "Client::status = MAP_WAIT"); - my_client->status = STATUS_MAP_WAIT; + my_client->status = ServerStatus::MapWait; auto p = std::make_unique(my_client, PacketGameType::ClientGetMap); my_client->SendPacket(std::move(p)); @@ -370,7 +370,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendMapOk() Debug(net, 9, "Client::SendMapOk()"); Debug(net, 9, "Client::status = ACTIVE"); - my_client->status = STATUS_ACTIVE; + my_client->status = ServerStatus::Active; auto p = std::make_unique(my_client, PacketGameType::ClientMapOk); my_client->SendPacket(std::move(p)); @@ -518,7 +518,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company) */ bool ClientNetworkGameSocketHandler::IsConnected() { - return my_client != nullptr && my_client->status == STATUS_ACTIVE; + return my_client != nullptr && my_client->status == ServerStatus::Active; } @@ -563,7 +563,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerClientInfo(Packet std::string name = p.Recv_string(NETWORK_NAME_LENGTH); std::string public_key = p.Recv_string(NETWORK_PUBLIC_KEY_LENGTH); - if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Authorized) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT; /* The server validates the name when receiving it from clients, so when it is wrong * here something went really wrong. In the best case the packet got malformed on its @@ -669,14 +669,14 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerError(Packet &p) } /* Perform an emergency save if we had already entered the game */ - if (this->status == STATUS_ACTIVE) ClientNetworkEmergencySave(); + if (this->status == ServerStatus::Active) ClientNetworkEmergencySave(); return NETWORK_RECV_STATUS_SERVER_ERROR; } NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerCheckNewGRFs(Packet &p) { - if (this->status != STATUS_ENCRYPTED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Encrypted) return NETWORK_RECV_STATUS_MALFORMED_PACKET; uint grf_count = p.Recv_uint8(); NetworkRecvStatus ret = NETWORK_RECV_STATUS_OKAY; @@ -722,9 +722,9 @@ class ClientGamePasswordRequestHandler : public NetworkAuthenticationPasswordReq NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerAuthenticationRequest(Packet &p) { - if (this->status != STATUS_JOIN && this->status != STATUS_AUTH_GAME) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Join && this->status != ServerStatus::AuthGame) return NETWORK_RECV_STATUS_MALFORMED_PACKET; Debug(net, 9, "Client::status = AUTH_GAME"); - this->status = STATUS_AUTH_GAME; + this->status = ServerStatus::AuthGame; Debug(net, 9, "Client::ReceiveServerAuthenticationRequest()"); @@ -747,7 +747,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerAuthenticationReq NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerEnableEncryption(Packet &p) { - if (this->status != STATUS_AUTH_GAME || this->authentication_handler == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::AuthGame || this->authentication_handler == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET; Debug(net, 9, "Client::ReceiveServerEnableEncryption()"); @@ -758,16 +758,16 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerEnableEncryption( this->authentication_handler = nullptr; Debug(net, 9, "Client::status = ENCRYPTED"); - this->status = STATUS_ENCRYPTED; + this->status = ServerStatus::Encrypted; return this->SendIdentify(); } NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerWelcome(Packet &p) { - if (this->status < STATUS_ENCRYPTED || this->status >= STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Encrypted || this->status >= ServerStatus::Authorized) return NETWORK_RECV_STATUS_MALFORMED_PACKET; Debug(net, 9, "Client::status = AUTHORIZED"); - this->status = STATUS_AUTHORIZED; + this->status = ServerStatus::Authorized; _network_own_client_id = (ClientID)p.Recv_uint32(); @@ -780,7 +780,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerWelcome(Packet &p NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerWaitForMap(Packet &p) { /* We set the internal wait state when requesting the map. */ - if (this->status != STATUS_MAP_WAIT) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::MapWait) return NETWORK_RECV_STATUS_MALFORMED_PACKET; Debug(net, 9, "Client::ReceiveServerWaitForMap()"); @@ -795,9 +795,9 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerWaitForMap(Packet NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapBegin(Packet &p) { - if (this->status < STATUS_AUTHORIZED || this->status >= STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Authorized || this->status >= ServerStatus::Map) return NETWORK_RECV_STATUS_MALFORMED_PACKET; Debug(net, 9, "Client::status = MAP"); - this->status = STATUS_MAP; + this->status = ServerStatus::Map; if (this->savegame != nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET; @@ -819,7 +819,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapBegin(Packet & NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapSize(Packet &p) { - if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Map) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (this->savegame == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET; _network_join_bytes_total = p.Recv_uint32(); @@ -832,7 +832,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapSize(Packet &p NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapData(Packet &p) { - if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Map) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (this->savegame == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET; /* We are still receiving data, put it to the file */ @@ -846,7 +846,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapData(Packet &p NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapDone(Packet &) { - if (this->status != STATUS_MAP) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Map) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (this->savegame == nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET; Debug(net, 9, "Client::ReceiveServerMapDone()"); @@ -910,7 +910,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMapDone(Packet &) NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerFrame(Packet &p) { - if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Active) return NETWORK_RECV_STATUS_MALFORMED_PACKET; _frame_counter_server = p.Recv_uint32(); _frame_counter_max = p.Recv_uint32(); @@ -945,7 +945,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerFrame(Packet &p) NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerSync(Packet &p) { - if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Active) return NETWORK_RECV_STATUS_MALFORMED_PACKET; _sync_frame = p.Recv_uint32(); _sync_seed_1 = p.Recv_uint32(); @@ -960,7 +960,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerSync(Packet &p) NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerCommand(Packet &p) { - if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Active) return NETWORK_RECV_STATUS_MALFORMED_PACKET; CommandPacket cp; auto err = this->ReceiveCommand(p, cp); @@ -981,7 +981,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerCommand(Packet &p NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerChat(Packet &p) { - if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Active) return NETWORK_RECV_STATUS_MALFORMED_PACKET; std::string name; const NetworkClientInfo *ci = nullptr, *ci_to; @@ -1031,7 +1031,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerChat(Packet &p) NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerExternalChat(Packet &p) { - if (this->status != STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status != ServerStatus::Active) return NETWORK_RECV_STATUS_MALFORMED_PACKET; std::string source = p.Recv_string(NETWORK_CHAT_LENGTH); ExtendedTextColour colour = ExtendedTextColour::FromNetwork(p.Recv_uint16()); @@ -1049,7 +1049,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerExternalChat(Pack NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerErrorQuit(Packet &p) { - if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Authorized) return NETWORK_RECV_STATUS_MALFORMED_PACKET; ClientID client_id = (ClientID)p.Recv_uint32(); @@ -1068,7 +1068,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerErrorQuit(Packet NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerQuit(Packet &p) { - if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Authorized) return NETWORK_RECV_STATUS_MALFORMED_PACKET; ClientID client_id = (ClientID)p.Recv_uint32(); @@ -1090,7 +1090,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerQuit(Packet &p) NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerClientJoined(Packet &p) { - if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Authorized) return NETWORK_RECV_STATUS_MALFORMED_PACKET; ClientID client_id = (ClientID)p.Recv_uint32(); @@ -1112,11 +1112,11 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerShutdown(Packet & /* Only when we're trying to join we really * care about the server shutting down. */ - if (this->status >= STATUS_JOIN) { + if (this->status >= ServerStatus::Join) { ShowErrorMessage(GetEncodedString(STR_NETWORK_MESSAGE_SERVER_SHUTDOWN), {}, WL_CRITICAL); } - if (this->status == STATUS_ACTIVE) ClientNetworkEmergencySave(); + if (this->status == ServerStatus::Active) ClientNetworkEmergencySave(); return NETWORK_RECV_STATUS_SERVER_ERROR; } @@ -1127,7 +1127,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerNewGame(Packet &) /* Only when we're trying to join we really * care about the server shutting down. */ - if (this->status >= STATUS_JOIN) { + if (this->status >= ServerStatus::Join) { /* To throttle the reconnects a bit, every clients waits its * Client ID modulo 16 + 1 (value 0 means no reconnect). * This way reconnects should be spread out a bit. */ @@ -1135,14 +1135,14 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerNewGame(Packet &) ShowErrorMessage(GetEncodedString(STR_NETWORK_MESSAGE_SERVER_REBOOT), {}, WL_CRITICAL); } - if (this->status == STATUS_ACTIVE) ClientNetworkEmergencySave(); + if (this->status == ServerStatus::Active) ClientNetworkEmergencySave(); return NETWORK_RECV_STATUS_SERVER_ERROR; } NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerRemoteConsoleCommand(Packet &p) { - if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Authorized) return NETWORK_RECV_STATUS_MALFORMED_PACKET; Debug(net, 9, "Client::ReceiveServerRemoteConsoleCommand()"); @@ -1158,7 +1158,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerRemoteConsoleComm NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMove(Packet &p) { - if (this->status < STATUS_AUTHORIZED) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Authorized) return NETWORK_RECV_STATUS_MALFORMED_PACKET; /* Nothing more in this packet... */ ClientID client_id = (ClientID)p.Recv_uint32(); @@ -1188,7 +1188,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerMove(Packet &p) NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerConfigurationUpdate(Packet &p) { - if (this->status < STATUS_ACTIVE) return NETWORK_RECV_STATUS_MALFORMED_PACKET; + if (this->status < ServerStatus::Active) return NETWORK_RECV_STATUS_MALFORMED_PACKET; _network_server_max_companies = p.Recv_uint8(); _network_server_name = p.Recv_string(NETWORK_NAME_LENGTH); @@ -1206,7 +1206,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::ReceiveServerConfigurationUpda void ClientNetworkGameSocketHandler::CheckConnection() { /* Only once we're authorized we can expect a steady stream of packets. */ - if (this->status < STATUS_AUTHORIZED) return; + if (this->status < ServerStatus::Authorized) return; /* 5 seconds are roughly twice the server's "you're slow" threshold (1 game day). */ std::chrono::steady_clock::duration lag = std::chrono::steady_clock::now() - this->last_packet; diff --git a/src/network/network_client.h b/src/network/network_client.h index 1f504b9568..eb907b9506 100644 --- a/src/network/network_client.h +++ b/src/network/network_client.h @@ -21,20 +21,20 @@ private: uint8_t token = 0; ///< The token we need to send back to the server to prove we're the right client. /** Status of the connection with the server. */ - enum ServerStatus : uint8_t { - STATUS_INACTIVE, ///< The client is not connected nor active. - STATUS_JOIN, ///< We are trying to join a server. - STATUS_AUTH_GAME, ///< Last action was requesting game (server) password. - STATUS_ENCRYPTED, ///< The game authentication has completed and from here on the connection to the server is encrypted. - STATUS_NEWGRFS_CHECK, ///< Last action was checking NewGRFs. - STATUS_AUTHORIZED, ///< The client is authorized at the server. - STATUS_MAP_WAIT, ///< The client is waiting as someone else is downloading the map. - STATUS_MAP, ///< The client is downloading the map. - STATUS_ACTIVE, ///< The client is active within in the game. - STATUS_END, ///< Must ALWAYS be on the end of this list!! (period) + enum class ServerStatus : uint8_t { + Inactive, ///< The client is not connected nor active. + Join, ///< We are trying to join a server. + AuthGame, ///< Last action was requesting game (server) password. + Encrypted, ///< The game authentication has completed and from here on the connection to the server is encrypted. + NewGRFsCheck, ///< Last action was checking NewGRFs. + Authorized, ///< The client is authorized at the server. + MapWait, ///< The client is waiting as someone else is downloading the map. + Map, ///< The client is downloading the map. + Active, ///< The client is active within in the game. + End, ///< Must ALWAYS be on the end of this list!! (period) }; - ServerStatus status = STATUS_INACTIVE; ///< Status of the connection with the server. + ServerStatus status = ServerStatus::Inactive; ///< Status of the connection with the server. protected: friend void NetworkExecuteLocalCommandQueue(); diff --git a/src/network/network_command.cpp b/src/network/network_command.cpp index 27bb05f4dc..0b1c4a611b 100644 --- a/src/network/network_command.cpp +++ b/src/network/network_command.cpp @@ -304,7 +304,7 @@ static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket cp.frame = _frame_counter_max + 1; for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->status >= NetworkClientSocket::STATUS_MAP) { + if (cs->status >= NetworkClientSocket::ClientStatus::Map) { /* Callbacks are only send back to the client who sent them in the * first place. This filters that out. */ cp.callback = (cs != owner) ? nullptr : callback; diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 73d2c4f04a..b3bb574f44 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -246,7 +246,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta */ if (this->IsPendingDeletion() || this->sock == INVALID_SOCKET) return status; - if (status != NETWORK_RECV_STATUS_CLIENT_QUIT && status != NETWORK_RECV_STATUS_SERVER_ERROR && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) { + if (status != NETWORK_RECV_STATUS_CLIENT_QUIT && status != NETWORK_RECV_STATUS_SERVER_ERROR && !this->HasClientQuit() && this->status >= ClientStatus::Authorized) { /* We did not receive a leave message from this client... */ std::string client_name = this->GetClientName(); @@ -254,7 +254,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta /* Inform other clients of this... strange leaving ;) */ for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status >= STATUS_AUTHORIZED && this != new_cs) { + if (new_cs->status >= ClientStatus::Authorized && this != new_cs) { new_cs->SendErrorQuit(this->client_id, NetworkErrorCode::ConnectionLost); } } @@ -262,7 +262,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta /* If we were transferring a map to this client, stop the savegame creation * process and queue the next client to receive the map. */ - if (this->status == STATUS_MAP) { + if (this->status == ClientStatus::Map) { /* Ensure the saving of the game is stopped too. */ this->savegame->Destroy(); this->savegame = nullptr; @@ -274,7 +274,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta Debug(net, 3, "[{}] Client #{} closed connection", ServerNetworkGameSocketHandler::GetName(), this->client_id); /* We just lost one client :( */ - if (this->status >= STATUS_AUTHORIZED) _network_game_info.clients_on--; + if (this->status >= ClientStatus::Authorized) _network_game_info.clients_on--; extern uint8_t _network_clients_connected; _network_clients_connected--; @@ -306,7 +306,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvSta { for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { if (cs->writable) { - if (cs->SendPackets() != SPS_CLOSED && cs->status == STATUS_MAP) { + if (cs->SendPackets() != SPS_CLOSED && cs->status == ClientStatus::Map) { /* This client is in the middle of a map-send, call the function for that */ cs->SendMap(); } @@ -376,7 +376,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode err StringID strid = GetNetworkErrorMsg(error); /* Only send when the current client was in game */ - if (this->status >= STATUS_AUTHORIZED) { + if (this->status >= ClientStatus::Authorized) { std::string client_name = this->GetClientName(); Debug(net, 1, "'{}' made an error and has been disconnected: {}", client_name, GetString(strid)); @@ -388,7 +388,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode err } for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) { + if (new_cs->status >= ClientStatus::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. */ if (error == NetworkErrorCode::NotAuthorized || error == NetworkErrorCode::NotExpected || error == NetworkErrorCode::WrongRevision) { @@ -415,11 +415,11 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck() { Debug(net, 9, "client[{}] SendNewGRFCheck()", this->client_id); - /* Invalid packet when status is anything but STATUS_IDENTIFY. */ - if (this->status != STATUS_IDENTIFY) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); + /* Invalid packet when status is anything but ClientStatus::Identify. */ + if (this->status != ClientStatus::Identify) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); Debug(net, 9, "client[{}] status = NEWGRFS_CHECK", this->client_id); - this->status = STATUS_NEWGRFS_CHECK; + this->status = ClientStatus::NewGRFsCheck; if (_grfconfig.empty()) { /* There are no NewGRFs, so they're welcome. */ @@ -447,11 +447,11 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendAuthRequest() { Debug(net, 9, "client[{}] SendAuthRequest()", this->client_id); - /* Invalid packet when status is anything but STATUS_INACTIVE or STATUS_AUTH_GAME. */ - if (this->status != STATUS_INACTIVE && status != STATUS_AUTH_GAME) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); + /* Invalid packet when status is anything but ClientStatus::Inactive or ClientStatus::AuthGame. */ + if (this->status != ClientStatus::Inactive && status != ClientStatus::AuthGame) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); Debug(net, 9, "client[{}] status = AUTH_GAME", this->client_id); - this->status = STATUS_AUTH_GAME; + this->status = ClientStatus::AuthGame; /* Reset 'lag' counters */ this->last_frame = this->last_frame_server = _frame_counter; @@ -475,8 +475,8 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendEnableEncryption() { Debug(net, 9, "client[{}] SendEnableEncryption()", this->client_id); - /* Invalid packet when status is anything but STATUS_AUTH_GAME. */ - if (this->status != STATUS_AUTH_GAME) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); + /* Invalid packet when status is anything but ClientStatus::AuthGame. */ + if (this->status != ClientStatus::AuthGame) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); auto p = std::make_unique(this, PacketGameType::ServerEnableEncryption); this->authentication_handler->SendEnableEncryption(*p); @@ -492,11 +492,11 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome() { Debug(net, 9, "client[{}] SendWelcome()", this->client_id); - /* Invalid packet when status is anything but STATUS_NEWGRFS_CHECK. */ - if (this->status != STATUS_NEWGRFS_CHECK) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); + /* Invalid packet when status is anything but ClientStatus::NewGRFsCheck. */ + if (this->status != ClientStatus::NewGRFsCheck) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET); Debug(net, 9, "client[{}] status = AUTHORIZED", this->client_id); - this->status = STATUS_AUTHORIZED; + this->status = ClientStatus::Authorized; /* Reset 'lag' counters */ this->last_frame = this->last_frame_server = _frame_counter; @@ -509,7 +509,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome() /* Transmit info about all the active clients */ for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs != this && new_cs->status >= STATUS_AUTHORIZED) { + if (new_cs != this && new_cs->status >= ClientStatus::Authorized) { this->SendClientInfo(new_cs->GetInfo()); } } @@ -529,7 +529,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendWait() /* Count how many clients are waiting in the queue, in front of you! */ for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status != STATUS_MAP_WAIT) continue; + if (new_cs->status != ClientStatus::MapWait) 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++; } @@ -552,7 +552,7 @@ void ServerNetworkGameSocketHandler::CheckNextClientToSendMap(NetworkClientSocke for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { if (ignore_cs == new_cs) continue; - if (new_cs->status == STATUS_MAP_WAIT) { + if (new_cs->status == ClientStatus::MapWait) { 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; } @@ -562,12 +562,12 @@ void ServerNetworkGameSocketHandler::CheckNextClientToSendMap(NetworkClientSocke /* Is there someone else to join? */ if (best != nullptr) { /* Let the first start joining. */ - best->status = STATUS_AUTHORIZED; + best->status = ClientStatus::Authorized; best->SendMap(); /* And update the rest. */ for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status == STATUS_MAP_WAIT) new_cs->SendWait(); + if (new_cs->status == ClientStatus::MapWait) new_cs->SendWait(); } } } @@ -578,12 +578,12 @@ void ServerNetworkGameSocketHandler::CheckNextClientToSendMap(NetworkClientSocke */ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() { - if (this->status < STATUS_AUTHORIZED) { + if (this->status < ClientStatus::Authorized) { /* Illegal call, return error and ignore the packet */ return this->SendError(NetworkErrorCode::IllegalPacket); } - if (this->status == STATUS_AUTHORIZED) { + if (this->status == ClientStatus::Authorized) { Debug(net, 9, "client[{}] SendMap(): first_packet", this->client_id); WaitTillSaved(); @@ -596,7 +596,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() NetworkSyncCommandQueue(this); Debug(net, 9, "client[{}] status = MAP", this->client_id); - this->status = STATUS_MAP; + this->status = ClientStatus::Map; /* Mark the start of download */ this->last_frame = _frame_counter; this->last_frame_server = _frame_counter; @@ -605,7 +605,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() if (SaveWithFilter(this->savegame, true) != SaveLoadResult::Ok) UserError("network savedump failed"); } - if (this->status == STATUS_MAP) { + if (this->status == ClientStatus::Map) { bool last_packet = this->savegame->TransferToNetworkQueue(); if (last_packet) { Debug(net, 9, "client[{}] SendMap(): last_packet", this->client_id); @@ -617,7 +617,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap() /* Set the status to DONE_MAP, no we will wait for the client * to send it is ready (maybe that happens like never ;)) */ Debug(net, 9, "client[{}] status = DONE_MAP", this->client_id); - this->status = STATUS_DONE_MAP; + this->status = ClientStatus::DoneMap; this->CheckNextClientToSendMap(); } @@ -719,7 +719,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendChat(NetworkAction action, { Debug(net, 9, "client[{}] SendChat(): action={}, client_id={}, self_send={}", this->client_id, action, client_id, self_send); - if (this->status < STATUS_PRE_ACTIVE) return NETWORK_RECV_STATUS_OKAY; + if (this->status < ClientStatus::PreActive) return NETWORK_RECV_STATUS_OKAY; auto p = std::make_unique(this, PacketGameType::ServerChat); @@ -745,7 +745,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendExternalChat(std::string_v { Debug(net, 9, "client[{}] SendExternalChat(): source={}", this->client_id, source); - if (this->status < STATUS_PRE_ACTIVE) return NETWORK_RECV_STATUS_OKAY; + if (this->status < ClientStatus::PreActive) return NETWORK_RECV_STATUS_OKAY; auto p = std::make_unique(this, PacketGameType::ServerExternalChat); @@ -885,7 +885,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientGameInfo(Packet & NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientNewGRFsChecked(Packet &) { - if (this->status != STATUS_NEWGRFS_CHECK) { + if (this->status != ClientStatus::NewGRFsCheck) { /* Illegal call, return error and ignore the packet */ return this->SendError(NetworkErrorCode::NotExpected); } @@ -897,7 +897,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientNewGRFsChecked(Pa NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientJoin(Packet &p) { - if (this->status != STATUS_INACTIVE) { + if (this->status != ClientStatus::Inactive) { /* Illegal call, return error and ignore the packet */ return this->SendError(NetworkErrorCode::NotExpected); } @@ -923,7 +923,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientJoin(Packet &p) NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientIdentify(Packet &p) { - if (this->status != STATUS_IDENTIFY) return this->SendError(NetworkErrorCode::NotExpected); + if (this->status != ClientStatus::Identify) return this->SendError(NetworkErrorCode::NotExpected); Debug(net, 9, "client[{}] ReceiveClientIdentify()", this->client_id); @@ -1002,7 +1002,7 @@ static NetworkErrorCode GetErrorForAuthenticationMethod(NetworkAuthenticationMet NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientAuthenticationResponse(Packet &p) { - if (this->status != STATUS_AUTH_GAME) { + if (this->status != ClientStatus::AuthGame) { return this->SendError(NetworkErrorCode::NotExpected); } @@ -1030,7 +1030,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientAuthenticationRes this->authentication_handler = nullptr; Debug(net, 9, "client[{}] status = IDENTIFY", this->client_id); - this->status = STATUS_IDENTIFY; + this->status = ClientStatus::Identify; /* Reset 'lag' counters */ this->last_frame = this->last_frame_server = _frame_counter; @@ -1042,7 +1042,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientGetMap(Packet &) { /* The client was never joined.. so this is impossible, right? * Ignore the packet, give the client a warning, and close the connection */ - if (this->status < STATUS_AUTHORIZED || this->HasClientQuit()) { + if (this->status < ClientStatus::Authorized || this->HasClientQuit()) { return this->SendError(NetworkErrorCode::NotAuthorized); } @@ -1050,10 +1050,10 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientGetMap(Packet &) /* Check if someone else is receiving the map */ for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status == STATUS_MAP) { + if (new_cs->status == ClientStatus::Map) { /* Tell the new client to wait */ Debug(net, 9, "client[{}] status = MAP_WAIT", this->client_id); - this->status = STATUS_MAP_WAIT; + this->status = ClientStatus::MapWait; return this->SendWait(); } } @@ -1065,7 +1065,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientGetMap(Packet &) NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientMapOk(Packet &) { /* Client has the map, now start syncing */ - if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) { + if (this->status == ClientStatus::DoneMap && !this->HasClientQuit()) { Debug(net, 9, "client[{}] ReceiveClientMapOk()", this->client_id); std::string client_name = this->GetClientName(); @@ -1078,7 +1078,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientMapOk(Packet &) /* Mark the client as pre-active, and wait for an ACK * so we know it is done loading and in sync with us */ Debug(net, 9, "client[{}] status = PRE_ACTIVE", this->client_id); - this->status = STATUS_PRE_ACTIVE; + this->status = ClientStatus::PreActive; NetworkHandleCommandQueue(this); this->SendFrame(); this->SendSync(); @@ -1089,7 +1089,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientMapOk(Packet &) this->last_frame_server = _frame_counter; for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status >= STATUS_AUTHORIZED) { + if (new_cs->status >= ClientStatus::Authorized) { new_cs->SendClientInfo(this->GetInfo()); new_cs->SendJoin(this->client_id); } @@ -1109,7 +1109,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientCommand(Packet &p { /* The client was never joined.. so this is impossible, right? * Ignore the packet, give the client a warning, and close the connection */ - if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) { + if (this->status < ClientStatus::DoneMap || this->HasClientQuit()) { return this->SendError(NetworkErrorCode::NotExpected); } @@ -1200,7 +1200,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientError(Packet &p) Debug(net, 9, "client[{}] ReceiveClientError(): errorno={}", this->client_id, errorno); /* The client was never joined.. thank the client for the packet, but ignore it */ - if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) { + if (this->status < ClientStatus::DoneMap || this->HasClientQuit()) { return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT); } @@ -1212,7 +1212,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientError(Packet &p) NetworkTextMessage(NetworkAction::ClientLeave, CC_DEFAULT, false, client_name, "", strid); for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status >= STATUS_AUTHORIZED) { + if (new_cs->status >= ClientStatus::Authorized) { new_cs->SendErrorQuit(this->client_id, errorno); } } @@ -1225,7 +1225,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientError(Packet &p) NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientQuit(Packet &) { /* The client was never joined.. thank the client for the packet, but ignore it */ - if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) { + if (this->status < ClientStatus::DoneMap || this->HasClientQuit()) { return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT); } @@ -1236,7 +1236,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientQuit(Packet &) NetworkTextMessage(NetworkAction::ClientLeave, CC_DEFAULT, false, client_name, "", STR_NETWORK_MESSAGE_CLIENT_LEAVING); for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) { - if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) { + if (new_cs->status >= ClientStatus::Authorized && new_cs != this) { new_cs->SendQuit(this->client_id); } } @@ -1248,7 +1248,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientQuit(Packet &) NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientAck(Packet &p) { - if (this->status < STATUS_AUTHORIZED) { + if (this->status < ClientStatus::Authorized) { /* Illegal call, return error and ignore the packet */ return this->SendError(NetworkErrorCode::NotAuthorized); } @@ -1258,13 +1258,13 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientAck(Packet &p) Debug(net, 9, "client[{}] ReceiveClientAck(): frame={}", this->client_id, frame); /* The client is trying to catch up with the server */ - if (this->status == STATUS_PRE_ACTIVE) { + if (this->status == ClientStatus::PreActive) { /* The client is not yet caught up? */ if (frame + Ticks::DAY_TICKS < _frame_counter) return NETWORK_RECV_STATUS_OKAY; /* Now it is! Unpause the game */ Debug(net, 9, "client[{}] status = ACTIVE", this->client_id); - this->status = STATUS_ACTIVE; + this->status = ClientStatus::Active; this->last_token_frame = _frame_counter; /* Execute script for, e.g. MOTD */ @@ -1325,7 +1325,7 @@ void NetworkServerSendChat(NetworkAction action, NetworkChatDestinationType dest } else { /* Else find the client to send the message to */ for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->client_id == (ClientID)dest && cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) { + if (cs->client_id == (ClientID)dest && cs->status >= ServerNetworkGameSocketHandler::ClientStatus::Authorized) { cs->SendChat(action, from_id, false, msg, data); break; } @@ -1342,7 +1342,7 @@ void NetworkServerSendChat(NetworkAction action, NetworkChatDestinationType dest } } else { for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->client_id == from_id && cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) { + if (cs->client_id == from_id && cs->status >= ServerNetworkGameSocketHandler::ClientStatus::Authorized) { cs->SendChat(action, (ClientID)dest, true, msg, data); break; } @@ -1357,7 +1357,7 @@ void NetworkServerSendChat(NetworkAction action, NetworkChatDestinationType dest ci_to = nullptr; for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { ci = cs->GetInfo(); - if (ci != nullptr && ci->client_playas == (CompanyID)dest && cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) { + if (ci != nullptr && ci->client_playas == (CompanyID)dest && cs->status >= ServerNetworkGameSocketHandler::ClientStatus::Authorized) { cs->SendChat(action, from_id, false, msg, data); if (cs->client_id == from_id) show_local = false; ci_to = ci; // Remember a client that is in the company for company-name @@ -1388,7 +1388,7 @@ void NetworkServerSendChat(NetworkAction action, NetworkChatDestinationType dest NetworkTextMessage(action, GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data); } else { for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->client_id == from_id && cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) { + if (cs->client_id == from_id && cs->status >= ServerNetworkGameSocketHandler::ClientStatus::Authorized) { cs->SendChat(action, ci_to->client_id, true, msg, data); } } @@ -1402,7 +1402,7 @@ void NetworkServerSendChat(NetworkAction action, NetworkChatDestinationType dest case NetworkChatDestinationType::Broadcast: for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) cs->SendChat(action, from_id, false, msg, data); + if (cs->status >= ServerNetworkGameSocketHandler::ClientStatus::Authorized) cs->SendChat(action, from_id, false, msg, data); } NetworkAdminChat(action, desttype, from_id, msg, data, from_admin); @@ -1425,14 +1425,14 @@ void NetworkServerSendChat(NetworkAction action, NetworkChatDestinationType dest void NetworkServerSendExternalChat(std::string_view source, ExtendedTextColour colour, std::string_view user, std::string_view msg) { for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) cs->SendExternalChat(source, colour, user, msg); + if (cs->status >= ServerNetworkGameSocketHandler::ClientStatus::Authorized) cs->SendExternalChat(source, colour, user, msg); } NetworkTextMessage(NetworkAction::ChatExternal, colour, false, user, msg, source); } NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientChat(Packet &p) { - if (this->status < STATUS_PRE_ACTIVE) { + if (this->status < ClientStatus::PreActive) { /* Illegal call, return error and ignore the packet */ return this->SendError(NetworkErrorCode::NotAuthorized); } @@ -1462,7 +1462,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientChat(Packet &p) NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientSetName(Packet &p) { - if (this->status != STATUS_ACTIVE) { + if (this->status != ClientStatus::Active) { /* Illegal call, return error and ignore the packet */ return this->SendError(NetworkErrorCode::NotExpected); } @@ -1497,7 +1497,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientSetName(Packet &p NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientRemoteConsoleCommand(Packet &p) { - if (this->status != STATUS_ACTIVE) return this->SendError(NetworkErrorCode::NotExpected); + if (this->status != ClientStatus::Active) return this->SendError(NetworkErrorCode::NotExpected); Debug(net, 9, "client[{}] ReceiveClientRemoteConsoleCommand()", this->client_id); @@ -1523,7 +1523,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientRemoteConsoleComm NetworkRecvStatus ServerNetworkGameSocketHandler::ReceiveClientMove(Packet &p) { - if (this->status != STATUS_ACTIVE) return this->SendError(NetworkErrorCode::NotExpected); + if (this->status != ClientStatus::Active) return this->SendError(NetworkErrorCode::NotExpected); CompanyID company_id = (Owner)p.Recv_uint8(); @@ -1596,7 +1596,7 @@ void NetworkUpdateClientInfo(ClientID client_id) Debug(desync, 1, "client: {:08x}; {:02x}; {:02x}; {:04x}", TimerGameEconomy::date, TimerGameEconomy::date_fract, ci->client_playas, client_id); for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->status >= ServerNetworkGameSocketHandler::STATUS_AUTHORIZED) { + if (cs->status >= ServerNetworkGameSocketHandler::ClientStatus::Authorized) { cs->SendClientInfo(ci); } } @@ -1761,7 +1761,7 @@ void NetworkServer_Tick(bool send_frame) /* Check if the speed of the client is what we can expect from a client */ uint lag = NetworkCalculateLag(cs); switch (cs->status) { - case NetworkClientSocket::STATUS_ACTIVE: + case NetworkClientSocket::ClientStatus::Active: if (lag > _settings_client.network.max_lag_time) { /* Client did still not report in within the specified limit. */ @@ -1794,10 +1794,10 @@ void NetworkServer_Tick(bool send_frame) } break; - case NetworkClientSocket::STATUS_INACTIVE: - case NetworkClientSocket::STATUS_IDENTIFY: - case NetworkClientSocket::STATUS_NEWGRFS_CHECK: - case NetworkClientSocket::STATUS_AUTHORIZED: + case NetworkClientSocket::ClientStatus::Inactive: + case NetworkClientSocket::ClientStatus::Identify: + case NetworkClientSocket::ClientStatus::NewGRFsCheck: + case NetworkClientSocket::ClientStatus::Authorized: /* NewGRF check and authorized states should be handled almost instantly. * So give them some lee-way, likewise for the query with inactive. */ if (lag > _settings_client.network.max_init_time) { @@ -1807,7 +1807,7 @@ void NetworkServer_Tick(bool send_frame) } break; - case NetworkClientSocket::STATUS_MAP_WAIT: + case NetworkClientSocket::ClientStatus::MapWait: /* Send every two seconds a packet to the client, to make sure * it knows the server is still there; just someone else is * still receiving the map. */ @@ -1822,7 +1822,7 @@ void NetworkServer_Tick(bool send_frame) } break; - case NetworkClientSocket::STATUS_MAP: + case NetworkClientSocket::ClientStatus::Map: /* Downloading the map... this is the amount of time since starting the saving. */ if (lag > _settings_client.network.max_download_time) { IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it took longer than {} ticks to download the map.", cs->client_id, cs->GetClientIP(), _settings_client.network.max_download_time); @@ -1831,8 +1831,8 @@ void NetworkServer_Tick(bool send_frame) } break; - case NetworkClientSocket::STATUS_DONE_MAP: - case NetworkClientSocket::STATUS_PRE_ACTIVE: + case NetworkClientSocket::ClientStatus::DoneMap: + case NetworkClientSocket::ClientStatus::PreActive: /* The map has been sent, so this is for loading the map and syncing up. */ if (lag > _settings_client.network.max_join_time) { IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it took longer than {} ticks to join.", cs->client_id, cs->GetClientIP(), _settings_client.network.max_join_time); @@ -1841,7 +1841,7 @@ void NetworkServer_Tick(bool send_frame) } break; - case NetworkClientSocket::STATUS_AUTH_GAME: + case NetworkClientSocket::ClientStatus::AuthGame: /* These don't block? */ if (lag > _settings_client.network.max_password_time) { IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because it took longer than {} ticks to enter the password.", cs->client_id, cs->GetClientIP(), _settings_client.network.max_password_time); @@ -1850,12 +1850,12 @@ void NetworkServer_Tick(bool send_frame) } break; - case NetworkClientSocket::STATUS_END: + case NetworkClientSocket::ClientStatus::End: /* Bad server/code. */ NOT_REACHED(); } - if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) { + if (cs->status >= NetworkClientSocket::ClientStatus::PreActive) { /* Check if we can send command, and if we have anything in the queue */ NetworkHandleCommandQueue(cs); @@ -1985,7 +1985,7 @@ std::string_view ServerNetworkGameSocketHandler::GetClientIP() /** Show the status message of all clients on the console. */ void NetworkServerShowStatusToConsole() { - static const std::string_view stat_str[] = { + static const EnumIndexArray stat_str = { "inactive", "authorizing", "identifying client", @@ -1997,14 +1997,13 @@ void NetworkServerShowStatusToConsole() "ready", "active" }; - static_assert(lengthof(stat_str) == NetworkClientSocket::STATUS_END); for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { NetworkClientInfo *ci = cs->GetInfo(); if (ci == nullptr) continue; uint lag = NetworkCalculateLag(cs); - std::string_view status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown"); + std::string_view status = (to_underlying(cs->status) < std::size(stat_str) ? stat_str[cs->status] : "unknown"); IConsolePrint(CC_INFO, "Client #{} name: '{}' status: '{}' frame-lag: {} company: {} IP: {}", cs->client_id, ci->client_name, status, lag, ci->client_playas + (Company::IsValidID(ci->client_playas) ? 1 : 0), @@ -2018,7 +2017,7 @@ void NetworkServerShowStatusToConsole() void NetworkServerSendConfigUpdate() { for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) { - if (cs->status >= NetworkClientSocket::STATUS_PRE_ACTIVE) cs->SendConfigUpdate(); + if (cs->status >= NetworkClientSocket::ClientStatus::PreActive) cs->SendConfigUpdate(); } } @@ -2051,7 +2050,7 @@ void NetworkServerDoMove(ClientID client_id, CompanyID company_id) } else { NetworkClientSocket *cs = NetworkClientSocket::GetByClientID(client_id); /* When the company isn't authorized we can't move them yet. */ - if (cs->status < NetworkClientSocket::STATUS_AUTHORIZED) return; + if (cs->status < NetworkClientSocket::ClientStatus::Authorized) return; cs->SendMove(client_id, company_id); } diff --git a/src/network/network_server.h b/src/network/network_server.h index 27f469b4dc..10995d5bf6 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -50,24 +50,24 @@ protected: public: /** Status of a client */ - enum ClientStatus : uint8_t { - STATUS_INACTIVE, ///< The client is not connected nor active. - STATUS_AUTH_GAME, ///< The client is authorizing with game (server) password. - STATUS_IDENTIFY, ///< The client is identifying itself. - STATUS_NEWGRFS_CHECK, ///< The client is checking NewGRFs. - STATUS_AUTHORIZED, ///< The client is authorized. - STATUS_MAP_WAIT, ///< The client is waiting as someone else is downloading the map. - STATUS_MAP, ///< The client is downloading the map. - STATUS_DONE_MAP, ///< The client has downloaded the map. - STATUS_PRE_ACTIVE, ///< The client is catching up the delayed frames. - STATUS_ACTIVE, ///< The client is active within in the game. - STATUS_END, ///< Must ALWAYS be on the end of this list!! (period). + enum class ClientStatus : uint8_t { + Inactive, ///< The client is not connected nor active. + AuthGame, ///< The client is authorizing with game (server) password. + Identify, ///< The client is identifying itself. + NewGRFsCheck, ///< The client is checking NewGRFs. + Authorized, ///< The client is authorized. + MapWait, ///< The client is waiting as someone else is downloading the map. + Map, ///< The client is downloading the map. + DoneMap, ///< The client has downloaded the map. + PreActive, ///< The client is catching up the delayed frames. + Active, ///< The client is active within in the game. + End, ///< Must ALWAYS be on the end of this list!! (period). }; uint8_t lag_test = 0; ///< Byte used for lag-testing the client uint8_t last_token = 0; ///< The last random token we did send to verify the client is listening uint32_t last_token_frame = 0; ///< The last frame we received the right token - ClientStatus status = STATUS_INACTIVE; ///< Status of this client + ClientStatus status = ClientStatus::Inactive; ///< Status of this client CommandQueue outgoing_queue{}; ///< The command-queue awaiting delivery; conceptually more a bucket to gather commands in, after which the whole bucket is sent to the client. size_t receive_limit = 0; ///< Amount of bytes that we can receive at this moment