1
0
Fork 0

Codechange: use std::string_view for connection strings

pull/14052/head
Rubidium 2025-04-20 12:17:45 +02:00 committed by rubidium42
parent 31433882a4
commit c13956a58a
17 changed files with 39 additions and 39 deletions

View File

@ -442,7 +442,7 @@ void NetworkAddress::Listen(int socktype, SocketList *sockets)
* @param company Pointer to the company variable to set iff indicated. * @param company Pointer to the company variable to set iff indicated.
* @return A valid ServerAddress of the parsed information. * @return A valid ServerAddress of the parsed information.
*/ */
/* static */ ServerAddress ServerAddress::Parse(const std::string &connection_string, uint16_t default_port, CompanyID *company_id) /* static */ ServerAddress ServerAddress::Parse(std::string_view connection_string, uint16_t default_port, CompanyID *company_id)
{ {
if (connection_string.starts_with("+")) { if (connection_string.starts_with("+")) {
std::string_view invite_code = ParseCompanyFromConnectionString(connection_string, company_id); std::string_view invite_code = ParseCompanyFromConnectionString(connection_string, company_id);
@ -451,5 +451,5 @@ void NetworkAddress::Listen(int socktype, SocketList *sockets)
uint16_t port = default_port; uint16_t port = default_port;
std::string_view ip = ParseFullConnectionString(connection_string, port, company_id); std::string_view ip = ParseFullConnectionString(connection_string, port, company_id);
return ServerAddress(SERVER_ADDRESS_DIRECT, std::string(ip) + ":" + std::to_string(port)); return ServerAddress(SERVER_ADDRESS_DIRECT, fmt::format("{}:{}", ip, port));
} }

View File

@ -194,13 +194,13 @@ private:
* @param type The type of the ServerAdress. * @param type The type of the ServerAdress.
* @param connection_string The connection_string that belongs to this ServerAddress type. * @param connection_string The connection_string that belongs to this ServerAddress type.
*/ */
ServerAddress(ServerAddressType type, const std::string &connection_string) : type(type), connection_string(connection_string) {} ServerAddress(ServerAddressType type, std::string &&connection_string) : type(type), connection_string(std::move(connection_string)) {}
public: public:
ServerAddressType type; ///< The type of this ServerAddress. ServerAddressType type; ///< The type of this ServerAddress.
std::string connection_string; ///< The connection string for this ServerAddress. std::string connection_string; ///< The connection string for this ServerAddress.
static ServerAddress Parse(const std::string &connection_string, uint16_t default_port, CompanyID *company_id = nullptr); static ServerAddress Parse(std::string_view connection_string, uint16_t default_port, CompanyID *company_id = nullptr);
}; };
#endif /* NETWORK_CORE_ADDRESS_H */ #endif /* NETWORK_CORE_ADDRESS_H */

View File

@ -120,7 +120,7 @@ private:
public: public:
TCPConnecter() {}; TCPConnecter() {};
TCPConnecter(const std::string &connection_string, uint16_t default_port, const NetworkAddress &bind_address = {}, int family = AF_UNSPEC); TCPConnecter(std::string_view connection_string, uint16_t default_port, const NetworkAddress &bind_address = {}, int family = AF_UNSPEC);
virtual ~TCPConnecter(); virtual ~TCPConnecter();
/** /**
@ -161,7 +161,7 @@ private:
public: public:
ServerAddress server_address; ///< Address we are connecting to. ServerAddress server_address; ///< Address we are connecting to.
TCPServerConnecter(const std::string &connection_string, uint16_t default_port); TCPServerConnecter(std::string_view connection_string, uint16_t default_port);
void SetConnected(SOCKET sock); void SetConnected(SOCKET sock);
void SetFailure(); void SetFailure();

View File

@ -26,7 +26,7 @@
* @param default_port If not indicated in connection_string, what port to use. * @param default_port If not indicated in connection_string, what port to use.
* @param bind_address The local bind address to use. Defaults to letting the OS find one. * @param bind_address The local bind address to use. Defaults to letting the OS find one.
*/ */
TCPConnecter::TCPConnecter(const std::string &connection_string, uint16_t default_port, const NetworkAddress &bind_address, int family) : TCPConnecter::TCPConnecter(std::string_view connection_string, uint16_t default_port, const NetworkAddress &bind_address, int family) :
bind_address(bind_address), bind_address(bind_address),
family(family) family(family)
{ {
@ -38,7 +38,7 @@ TCPConnecter::TCPConnecter(const std::string &connection_string, uint16_t defaul
* @param connection_string The address to connect to. * @param connection_string The address to connect to.
* @param default_port If not indicated in connection_string, what port to use. * @param default_port If not indicated in connection_string, what port to use.
*/ */
TCPServerConnecter::TCPServerConnecter(const std::string &connection_string, uint16_t default_port) : TCPServerConnecter::TCPServerConnecter(std::string_view connection_string, uint16_t default_port) :
server_address(ServerAddress::Parse(connection_string, default_port)) server_address(ServerAddress::Parse(connection_string, default_port))
{ {
switch (this->server_address.type) { switch (this->server_address.type) {

View File

@ -472,7 +472,7 @@ static void CheckPauseOnJoin()
* @param company_id The company ID to set, if available. * @param company_id The company ID to set, if available.
* @return A std::string_view into the connection string without the company part. * @return A std::string_view into the connection string without the company part.
*/ */
std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id) std::string_view ParseCompanyFromConnectionString(std::string_view connection_string, CompanyID *company_id)
{ {
std::string_view ip = connection_string; std::string_view ip = connection_string;
if (company_id == nullptr) return ip; if (company_id == nullptr) return ip;
@ -516,7 +516,7 @@ std::string_view ParseCompanyFromConnectionString(const std::string &connection_
* @param company_id The company ID to set, if available. * @param company_id The company ID to set, if available.
* @return A std::string_view into the connection string with the (IP) address part. * @return A std::string_view into the connection string with the (IP) address part.
*/ */
std::string_view ParseFullConnectionString(const std::string &connection_string, uint16_t &port, CompanyID *company_id) std::string_view ParseFullConnectionString(std::string_view connection_string, uint16_t &port, CompanyID *company_id)
{ {
std::string_view ip = ParseCompanyFromConnectionString(connection_string, company_id); std::string_view ip = ParseCompanyFromConnectionString(connection_string, company_id);
@ -536,11 +536,11 @@ std::string_view ParseFullConnectionString(const std::string &connection_string,
* @param default_port The port to use if none is given. * @param default_port The port to use if none is given.
* @return The normalized connection string. * @return The normalized connection string.
*/ */
std::string NormalizeConnectionString(const std::string &connection_string, uint16_t default_port) std::string NormalizeConnectionString(std::string_view connection_string, uint16_t default_port)
{ {
uint16_t port = default_port; uint16_t port = default_port;
std::string_view ip = ParseFullConnectionString(connection_string, port); std::string_view ip = ParseFullConnectionString(connection_string, port);
return std::string(ip) + ":" + std::to_string(port); return fmt::format("{}:{}", ip, port);
} }
/** /**
@ -551,7 +551,7 @@ std::string NormalizeConnectionString(const std::string &connection_string, uint
* @param default_port The default port to set port to if not in connection_string. * @param default_port The default port to set port to if not in connection_string.
* @return A valid NetworkAddress of the parsed information. * @return A valid NetworkAddress of the parsed information.
*/ */
NetworkAddress ParseConnectionString(const std::string &connection_string, uint16_t default_port) NetworkAddress ParseConnectionString(std::string_view connection_string, uint16_t default_port)
{ {
uint16_t port = default_port; uint16_t port = default_port;
std::string_view ip = ParseFullConnectionString(connection_string, port); std::string_view ip = ParseFullConnectionString(connection_string, port);
@ -642,7 +642,7 @@ private:
std::string connection_string; std::string connection_string;
public: public:
TCPQueryConnecter(const std::string &connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {} TCPQueryConnecter(std::string_view connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
void OnFailure() override void OnFailure() override
{ {
@ -667,7 +667,7 @@ public:
* Query a server to fetch the game-info. * Query a server to fetch the game-info.
* @param connection_string the address to query. * @param connection_string the address to query.
*/ */
void NetworkQueryServer(const std::string &connection_string) void NetworkQueryServer(std::string_view connection_string)
{ {
if (!_network_available) return; if (!_network_available) return;
@ -689,7 +689,7 @@ void NetworkQueryServer(const std::string &connection_string)
* @param never_expire Whether the entry can expire (removed when no longer found in the public listing). * @param never_expire Whether the entry can expire (removed when no longer found in the public listing).
* @return The entry on the game list. * @return The entry on the game list.
*/ */
NetworkGame *NetworkAddServer(const std::string &connection_string, bool manually, bool never_expire) NetworkGame *NetworkAddServer(std::string_view connection_string, bool manually, bool never_expire)
{ {
if (connection_string.empty()) return nullptr; if (connection_string.empty()) return nullptr;
@ -745,7 +745,7 @@ private:
std::string connection_string; std::string connection_string;
public: public:
TCPClientConnecter(const std::string &connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {} TCPClientConnecter(std::string_view connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
void OnFailure() override void OnFailure() override
{ {
@ -782,7 +782,7 @@ public:
* @param join_server_password The password for the server. * @param join_server_password The password for the server.
* @return Whether the join has started. * @return Whether the join has started.
*/ */
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password) bool NetworkClientConnectGame(std::string_view connection_string, CompanyID default_company, const std::string &join_server_password)
{ {
Debug(net, 9, "NetworkClientConnectGame(): connection_string={}", connection_string); Debug(net, 9, "NetworkClientConnectGame(): connection_string={}", connection_string);

View File

@ -109,7 +109,7 @@ void ClientNetworkEmergencySave()
* Create a new socket for the client side of the game connection. * Create a new socket for the client side of the game connection.
* @param s The socket to connect with. * @param s The socket to connect with.
*/ */
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s, const std::string &connection_string) : NetworkGameSocketHandler(s), connection_string(connection_string) ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s, std::string_view connection_string) : NetworkGameSocketHandler(s), connection_string(connection_string)
{ {
assert(ClientNetworkGameSocketHandler::my_client == nullptr); assert(ClientNetworkGameSocketHandler::my_client == nullptr);
ClientNetworkGameSocketHandler::my_client = this; ClientNetworkGameSocketHandler::my_client = this;

View File

@ -74,7 +74,7 @@ protected:
static NetworkRecvStatus SendIdentify(); static NetworkRecvStatus SendIdentify();
void CheckConnection(); void CheckConnection();
public: public:
ClientNetworkGameSocketHandler(SOCKET s, const std::string &connection_string); ClientNetworkGameSocketHandler(SOCKET s, std::string_view connection_string);
~ClientNetworkGameSocketHandler(); ~ClientNetworkGameSocketHandler();
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override; NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override;

View File

@ -718,7 +718,7 @@ public:
* Initiate the connecting. * Initiate the connecting.
* @param address The address of the server. * @param address The address of the server.
*/ */
NetworkContentConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_CONTENT_SERVER_PORT) {} NetworkContentConnecter(std::string_view connection_string) : TCPConnecter(connection_string, NETWORK_CONTENT_SERVER_PORT) {}
void OnFailure() override void OnFailure() override
{ {

View File

@ -106,7 +106,7 @@ public:
* Initiate the connecting. * Initiate the connecting.
* @param connection_string The address of the Game Coordinator server. * @param connection_string The address of the Game Coordinator server.
*/ */
NetworkCoordinatorConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_COORDINATOR_SERVER_PORT) {} NetworkCoordinatorConnecter(std::string_view connection_string) : TCPConnecter(connection_string, NETWORK_COORDINATOR_SERVER_PORT) {}
void OnFailure() override void OnFailure() override
{ {

View File

@ -44,13 +44,13 @@ void NetworkReboot();
void NetworkDisconnect(bool close_admins = true); void NetworkDisconnect(bool close_admins = true);
void NetworkGameLoop(); void NetworkGameLoop();
void NetworkBackgroundLoop(); void NetworkBackgroundLoop();
std::string_view ParseFullConnectionString(const std::string &connection_string, uint16_t &port, CompanyID *company_id = nullptr); std::string_view ParseFullConnectionString(std::string_view connection_string, uint16_t &port, CompanyID *company_id = nullptr);
using NetworkCompanyStatsArray = ReferenceThroughBaseContainer<std::array<NetworkCompanyStats, MAX_COMPANIES>>; using NetworkCompanyStatsArray = ReferenceThroughBaseContainer<std::array<NetworkCompanyStats, MAX_COMPANIES>>;
NetworkCompanyStatsArray NetworkGetCompanyStats(); NetworkCompanyStatsArray NetworkGetCompanyStats();
void NetworkUpdateClientInfo(ClientID client_id); void NetworkUpdateClientInfo(ClientID client_id);
void NetworkClientsToSpectators(CompanyID cid); void NetworkClientsToSpectators(CompanyID cid);
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password = ""); bool NetworkClientConnectGame(std::string_view connection_string, CompanyID default_company, const std::string &join_server_password = "");
void NetworkClientJoinGame(); void NetworkClientJoinGame();
void NetworkClientRequestMove(CompanyID company); void NetworkClientRequestMove(CompanyID company);
void NetworkClientSendRcon(std::string_view password, std::string_view command); void NetworkClientSendRcon(std::string_view password, std::string_view command);

View File

@ -29,7 +29,7 @@ int _network_game_list_version = 0; ///< Current version of all items in the lis
* @param connection_string the address of the to-be added item * @param connection_string the address of the to-be added item
* @return a point to the newly added or already existing item * @return a point to the newly added or already existing item
*/ */
NetworkGame *NetworkGameListAddItem(const std::string &connection_string) NetworkGame *NetworkGameListAddItem(std::string_view connection_string)
{ {
/* Parse the connection string to ensure the default port is there. */ /* Parse the connection string to ensure the default port is there. */
const std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT).connection_string; const std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT).connection_string;

View File

@ -25,7 +25,7 @@ enum NetworkGameStatus : uint8_t {
/** Structure with information shown in the game list (GUI) */ /** Structure with information shown in the game list (GUI) */
struct NetworkGame { struct NetworkGame {
NetworkGame(const std::string &connection_string) : connection_string(connection_string) {} NetworkGame(std::string_view connection_string) : connection_string(connection_string) {}
NetworkGameInfo info{}; ///< The game information of this server. NetworkGameInfo info{}; ///< The game information of this server.
std::string connection_string; ///< Address of the server. std::string connection_string; ///< Address of the server.
@ -38,7 +38,7 @@ struct NetworkGame {
extern std::vector<std::unique_ptr<NetworkGame>> _network_game_list; extern std::vector<std::unique_ptr<NetworkGame>> _network_game_list;
extern int _network_game_list_version; extern int _network_game_list_version;
NetworkGame *NetworkGameListAddItem(const std::string &connection_string); NetworkGame *NetworkGameListAddItem(std::string_view connection_string);
void NetworkGameListRemoveItem(NetworkGame *remove); void NetworkGameListRemoveItem(NetworkGame *remove);
void NetworkGameListRemoveExpired(); void NetworkGameListRemoveExpired();

View File

@ -81,10 +81,10 @@ extern std::string _network_server_name;
extern uint8_t _network_reconnect; extern uint8_t _network_reconnect;
void NetworkQueryServer(const std::string &connection_string); void NetworkQueryServer(std::string_view connection_string);
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port); void GetBindAddresses(NetworkAddressList *addresses, uint16_t port);
struct NetworkGame *NetworkAddServer(const std::string &connection_string, bool manually = true, bool never_expire = false); struct NetworkGame *NetworkAddServer(std::string_view connection_string, bool manually = true, bool never_expire = false);
void NetworkRebuildHostList(); void NetworkRebuildHostList();
void UpdateNetworkGameWindow(); void UpdateNetworkGameWindow();
@ -115,9 +115,9 @@ uint NetworkCalculateLag(const NetworkClientSocket *cs);
StringID GetNetworkErrorMsg(NetworkErrorCode err); StringID GetNetworkErrorMsg(NetworkErrorCode err);
bool NetworkMakeClientNameUnique(std::string &new_name); bool NetworkMakeClientNameUnique(std::string &new_name);
std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id); std::string_view ParseCompanyFromConnectionString(std::string_view connection_string, CompanyID *company_id);
NetworkAddress ParseConnectionString(const std::string &connection_string, uint16_t default_port); NetworkAddress ParseConnectionString(std::string_view connection_string, uint16_t default_port);
std::string NormalizeConnectionString(const std::string &connection_string, uint16_t default_port); std::string NormalizeConnectionString(std::string_view connection_string, uint16_t default_port);
void ClientNetworkEmergencySave(); void ClientNetworkEmergencySave();

View File

@ -36,14 +36,14 @@ public:
* @param s The socket to connect with. * @param s The socket to connect with.
* @param connection_string The connection string of the server. * @param connection_string The connection string of the server.
*/ */
QueryNetworkGameSocketHandler(SOCKET s, const std::string &connection_string) : NetworkGameSocketHandler(s), connection_string(connection_string) {} QueryNetworkGameSocketHandler(SOCKET s, std::string_view connection_string) : NetworkGameSocketHandler(s), connection_string(connection_string) {}
/** /**
* Start to query a server based on an open socket. * Start to query a server based on an open socket.
* @param s The socket to connect with. * @param s The socket to connect with.
* @param connection_string The connection string of the server. * @param connection_string The connection string of the server.
*/ */
static void QueryServer(SOCKET s, const std::string &connection_string) static void QueryServer(SOCKET s, std::string_view connection_string)
{ {
auto query = std::make_unique<QueryNetworkGameSocketHandler>(s, connection_string); auto query = std::make_unique<QueryNetworkGameSocketHandler>(s, connection_string);
query->SendGameInfo(); query->SendGameInfo();

View File

@ -28,7 +28,7 @@ public:
* @param stun_handler The handler for this request. * @param stun_handler The handler for this request.
* @param connection_string The address of the server. * @param connection_string The address of the server.
*/ */
NetworkStunConnecter(ClientNetworkStunSocketHandler *stun_handler, const std::string &connection_string, const std::string &token, uint8_t family) : NetworkStunConnecter(ClientNetworkStunSocketHandler *stun_handler, std::string_view connection_string, const std::string &token, uint8_t family) :
TCPConnecter(connection_string, NETWORK_STUN_SERVER_PORT, NetworkAddress(), family), TCPConnecter(connection_string, NETWORK_STUN_SERVER_PORT, NetworkAddress(), family),
stun_handler(stun_handler), stun_handler(stun_handler),
token(token), token(token),

View File

@ -28,7 +28,7 @@ public:
* Initiate the connecting. * Initiate the connecting.
* @param connection_string The address of the TURN server. * @param connection_string The address of the TURN server.
*/ */
NetworkTurnConnecter(ClientNetworkTurnSocketHandler *handler, const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_TURN_SERVER_PORT), handler(handler) {} NetworkTurnConnecter(ClientNetworkTurnSocketHandler *handler, std::string_view connection_string) : TCPConnecter(connection_string, NETWORK_TURN_SERVER_PORT), handler(handler) {}
void OnFailure() override void OnFailure() override
{ {
@ -96,7 +96,7 @@ void ClientNetworkTurnSocketHandler::Connect()
* @param connection_string Connection string of the TURN server. * @param connection_string Connection string of the TURN server.
* @return The handler for this TURN connection. * @return The handler for this TURN connection.
*/ */
/* static */ std::unique_ptr<ClientNetworkTurnSocketHandler> ClientNetworkTurnSocketHandler::Turn(const std::string &token, uint8_t tracking_number, const std::string &ticket, const std::string &connection_string) /* static */ std::unique_ptr<ClientNetworkTurnSocketHandler> ClientNetworkTurnSocketHandler::Turn(const std::string &token, uint8_t tracking_number, const std::string &ticket, std::string_view connection_string)
{ {
auto turn_handler = std::make_unique<ClientNetworkTurnSocketHandler>(token, tracking_number, connection_string); auto turn_handler = std::make_unique<ClientNetworkTurnSocketHandler>(token, tracking_number, connection_string);

View File

@ -27,7 +27,7 @@ public:
std::shared_ptr<TCPConnecter> connecter{}; ///< Connecter instance. std::shared_ptr<TCPConnecter> connecter{}; ///< Connecter instance.
bool connect_started = false; ///< Whether we started the connection. bool connect_started = false; ///< Whether we started the connection.
ClientNetworkTurnSocketHandler(const std::string &token, uint8_t tracking_number, const std::string &connection_string) : token(token), tracking_number(tracking_number), connection_string(connection_string) {} ClientNetworkTurnSocketHandler(const std::string &token, uint8_t tracking_number, std::string_view connection_string) : token(token), tracking_number(tracking_number), connection_string(connection_string) {}
NetworkRecvStatus CloseConnection(bool error = true) override; NetworkRecvStatus CloseConnection(bool error = true) override;
~ClientNetworkTurnSocketHandler() override; ~ClientNetworkTurnSocketHandler() override;
@ -36,7 +36,7 @@ public:
void Connect(); void Connect();
void ConnectFailure(); void ConnectFailure();
static std::unique_ptr<ClientNetworkTurnSocketHandler> Turn(const std::string &token, uint8_t tracking_number, const std::string &ticket, const std::string &connection_string); static std::unique_ptr<ClientNetworkTurnSocketHandler> Turn(const std::string &token, uint8_t tracking_number, const std::string &ticket, std::string_view connection_string);
}; };
#endif /* NETWORK_TURN_H */ #endif /* NETWORK_TURN_H */