From d99edf2bbc9e4af791677e394aed9b4ce1dbeb81 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 20 Apr 2025 11:23:58 +0200 Subject: [PATCH] Codechange: use std::string_view for kicking/banning --- src/network/network_func.h | 6 +++--- src/network/network_server.cpp | 10 +++++----- src/network/network_server.h | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/network/network_func.h b/src/network/network_func.h index 037a0deea1..088b7f790e 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -79,9 +79,9 @@ void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const std::string &msg, ClientID from_id, int64_t data = 0, bool from_admin = false); void NetworkServerSendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg); -void NetworkServerKickClient(ClientID client_id, const std::string &reason); -uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &reason); -uint NetworkServerKickOrBanIP(const std::string &ip, bool ban, const std::string &reason); +void NetworkServerKickClient(ClientID client_id, std::string_view reason); +uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, std::string_view reason); +uint NetworkServerKickOrBanIP(std::string_view ip, bool ban, std::string_view reason); void NetworkInitChatMessage(); void NetworkReInitChatBoxSize(); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 5eb29ad2a0..7cf69dea53 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -356,7 +356,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendGameInfo() * @param error The error to disconnect for. * @param reason In case of kicking a client, specifies the reason for kicking the client. */ -NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const std::string &reason) +NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, std::string_view reason) { Debug(net, 9, "client[{}] SendError(): error={}", this->client_id, error); @@ -1917,7 +1917,7 @@ static IntervalTimer _economy_network_daily({TimerGameEconomy: * Get the IP address/hostname of the connected client. * @return The IP address. */ -const std::string &ServerNetworkGameSocketHandler::GetClientIP() +std::string_view ServerNetworkGameSocketHandler::GetClientIP() { return this->client_address.GetHostname(); } @@ -2028,7 +2028,7 @@ void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std * @param client_id The client to kick. * @param reason In case of kicking a client, specifies the reason for kicking the client. */ -void NetworkServerKickClient(ClientID client_id, const std::string &reason) +void NetworkServerKickClient(ClientID client_id, std::string_view reason) { if (client_id == CLIENT_ID_SERVER) return; NetworkClientSocket::GetByClientID(client_id)->SendError(NETWORK_ERROR_KICKED, reason); @@ -2040,7 +2040,7 @@ void NetworkServerKickClient(ClientID client_id, const std::string &reason) * @param ban Whether to ban or kick. * @param reason In case of kicking a client, specifies the reason for kicking the client. */ -uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &reason) +uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, std::string_view reason) { return NetworkServerKickOrBanIP(NetworkClientSocket::GetByClientID(client_id)->GetClientIP(), ban, reason); } @@ -2051,7 +2051,7 @@ uint NetworkServerKickOrBanIP(ClientID client_id, bool ban, const std::string &r * @param ban Whether to ban or just kick. * @param reason In case of kicking a client, specifies the reason for kicking the client. */ -uint NetworkServerKickOrBanIP(const std::string &ip, bool ban, const std::string &reason) +uint NetworkServerKickOrBanIP(std::string_view ip, bool ban, std::string_view reason) { /* Add address to ban-list */ if (ban) { diff --git a/src/network/network_server.h b/src/network/network_server.h index cd5c48e0e9..cf7552742c 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -93,7 +93,7 @@ public: NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id); NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci); - NetworkRecvStatus SendError(NetworkErrorCode error, const std::string &reason = {}); + NetworkRecvStatus SendError(NetworkErrorCode error, std::string_view reason = {}); NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64_t data); NetworkRecvStatus SendExternalChat(const std::string &source, TextColour colour, const std::string &user, const std::string &msg); NetworkRecvStatus SendJoin(ClientID client_id); @@ -115,7 +115,7 @@ public: return "server"; } - const std::string &GetClientIP(); + std::string_view GetClientIP(); std::string_view GetPeerPublicKey() const { return this->peer_public_key; } static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);