* common function to do case insensitive compare

* some const + ref in different places
* some logs, comments & refactoring
This commit is contained in:
otavepto 2024-03-30 07:55:56 +02:00 committed by otavepto
parent 574e7a7781
commit f423b07eef
10 changed files with 53 additions and 62 deletions

View File

@ -133,10 +133,10 @@ public:
void setAppID(uint32 appid); void setAppID(uint32 appid);
void Run(); void Run();
// send to a specific user, if 0 was passed to set_dest_id() then this will be broadcasted to all users on the network // send to a specific user, set_dest_id() must be called
bool sendTo(Common_Message *msg, bool reliable, Connection *conn = NULL); bool sendTo(Common_Message *msg, bool reliable, Connection *conn = NULL);
// send to all users whose account type is Individual, no need to call set_dest_id(), this is done automatically // send to all users whose account type is Individual, no need to call set_dest_id(), this is done automatically
bool sendToAllIndividuals(Common_Message *msg, bool reliable); bool sendToAllIndividuals(Common_Message *msg, bool reliable);
// send to all active/current connections, no need to call set_dest_id(), this is done automatically // send to all active/current connections, no need to call set_dest_id(), this is done automatically

View File

@ -315,11 +315,11 @@ public:
std::set<PublishedFileId_t> modSet(); std::set<PublishedFileId_t> modSet();
//leaderboards //leaderboards
void setLeaderboard(std::string leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type); void setLeaderboard(const std::string &leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type);
std::map<std::string, Leaderboard_config> getLeaderboards(); const std::map<std::string, Leaderboard_config>& getLeaderboards() const;
//stats //stats
const std::map<std::string, Stat_config>& getStats(); const std::map<std::string, Stat_config>& getStats() const;
void setStatDefiniton(const std::string &name, const struct Stat_config &stat_config); void setStatDefiniton(const std::string &name, const struct Stat_config &stat_config);
//images //images

View File

@ -100,12 +100,9 @@ google::protobuf::Map<std::string,std::string>::const_iterator caseinsensitive_f
{ {
auto x = map.begin(); auto x = map.begin();
while (x != map.end()) { while (x != map.end()) {
if (key.size() == x->first.size() && std::equal(x->first.begin(), x->first.end(), key.begin(), if (common_helpers::str_cmp_insensitive(key, x->first)) {
[](char a, char b) { break;
return tolower(a) == tolower(b); }
})) {
break;
}
++x; ++x;
} }

View File

@ -216,7 +216,7 @@ message Steam_Messages {
} }
message GameServerStats_Messages { message GameServerStats_Messages {
// --- baisc definitions // --- basic definitions
message StatInfo { message StatInfo {
enum Stat_Type { enum Stat_Type {
STAT_TYPE_INT = 0; STAT_TYPE_INT = 0;

View File

@ -612,28 +612,11 @@ bool Networking::handle_tcp(Common_Message *msg, struct TCP_Socket &socket)
struct Connection *Networking::find_connection(CSteamID search_id, uint32 appid) struct Connection *Networking::find_connection(CSteamID search_id, uint32 appid)
{ {
if (appid) { auto conn = std::find_if(connections.begin(), connections.end(), [&search_id, appid](struct Connection const& conn) {
auto conn = std::find_if(connections.begin(), connections.end(), [&search_id, &appid](struct Connection const& conn) { if (appid && (conn.appid != appid)) return false;
if (conn.appid != appid) return false;
for (auto &id: conn.ids) { for (const auto &id: conn.ids) {
if (search_id == id) { if (search_id == id) return true;
return true;
}
}
return false;
});
if (connections.end() != conn)
return &(*conn);
}
auto conn = std::find_if(connections.begin(), connections.end(), [&search_id](struct Connection const& conn) {
for (auto &id: conn.ids) {
if (search_id == id) {
return true;
}
} }
return false; return false;
@ -642,7 +625,7 @@ struct Connection *Networking::find_connection(CSteamID search_id, uint32 appid)
if (connections.end() != conn) if (connections.end() != conn)
return &(*conn); return &(*conn);
return NULL; return nullptr;
} }
bool Networking::add_id_connection(struct Connection *connection, CSteamID steam_id) bool Networking::add_id_connection(struct Connection *connection, CSteamID steam_id)
@ -653,6 +636,7 @@ bool Networking::add_id_connection(struct Connection *connection, CSteamID steam
if (id != connection->ids.end()) if (id != connection->ids.end())
return false; return false;
PRINT_DEBUG("Networking::add_id_connection ADDED ID %llu\n", (uint64)steam_id.ConvertToUint64());
connection->ids.push_back(steam_id); connection->ids.push_back(steam_id);
if (connection->connected) { if (connection->connected) {
run_callback_user(steam_id, true, connection->appid); run_callback_user(steam_id, true, connection->appid);
@ -671,6 +655,7 @@ struct Connection *Networking::new_connection(CSteamID search_id, uint32 appid)
connection.appid = appid; connection.appid = appid;
connection.last_received = std::chrono::high_resolution_clock::now(); connection.last_received = std::chrono::high_resolution_clock::now();
PRINT_DEBUG("Networking::new_connection ADDED ID %llu\n", (uint64)search_id.ConvertToUint64());
connections.push_back(connection); connections.push_back(connection);
return &(connections[connections.size() - 1]); return &(connections[connections.size() - 1]);
} }
@ -681,7 +666,10 @@ bool Networking::handle_announce(Common_Message *msg, IP_PORT ip_port)
if (!conn || conn->appid != msg->announce().appid()) { if (!conn || conn->appid != msg->announce().appid()) {
conn = new_connection((uint64)msg->source_id(), msg->announce().appid()); conn = new_connection((uint64)msg->source_id(), msg->announce().appid());
if (!conn) return false; if (!conn) return false;
PRINT_DEBUG("New Connection Created\n"); PRINT_DEBUG(
"Networking::handle_announce new connection created: user %llu, appid %lu\n",
(uint64)msg->source_id(), msg->announce().appid()
);
} }
PRINT_DEBUG("Handle Announce: %u, " "%" PRIu64 ", %u, %u\n", conn->appid, msg->source_id(), msg->announce().appid(), msg->announce().type()); PRINT_DEBUG("Handle Announce: %u, " "%" PRIu64 ", %u, %u\n", conn->appid, msg->source_id(), msg->announce().appid(), msg->announce().type());
@ -860,6 +848,7 @@ Networking::Networking(CSteamID id, uint32 appid, uint16 port, std::set<IP_PORT>
enabled = true; enabled = true;
} }
PRINT_DEBUG("Networking::Networking ADDED ID %llu\n", (uint64)id.ConvertToUint64());
ids.push_back(id); ids.push_back(id);
reset_last_error(); reset_last_error();
@ -1172,7 +1161,7 @@ void Networking::addListenId(CSteamID id)
return; return;
} }
PRINT_DEBUG("ADDED ID\n"); PRINT_DEBUG("Networking::addListenId ADDED ID %llu\n", (uint64)id.ConvertToUint64());
ids.push_back(id); ids.push_back(id);
send_announce_broadcasts(); send_announce_broadcasts();
return; return;
@ -1243,10 +1232,9 @@ bool Networking::sendTo(Common_Message *msg, bool reliable, Connection *conn)
ret = true; ret = true;
} }
} else { } else {
char *buffer = new char[size]; std::vector<char> buffer(size, 0);
msg->SerializeToArray(buffer, size); msg->SerializeToArray(&buffer[0], size);
send_packet_to(udp_socket, conn->udp_ip_port, buffer, size); send_packet_to(udp_socket, conn->udp_ip_port, &buffer[0], size);
delete[] buffer;
ret = true; ret = true;
} }
} }
@ -1299,7 +1287,7 @@ void Networking::run_callback_user(CSteamID steam_id, bool online, uint32 appid)
//only give callbacks for right game accounts //only give callbacks for right game accounts
if (steam_id.BIndividualAccount() && appid != this->appid && appid != LOBBY_CONNECT_APPID) return; if (steam_id.BIndividualAccount() && appid != this->appid && appid != LOBBY_CONNECT_APPID) return;
Common_Message msg; Common_Message msg{};
msg.set_source_id(steam_id.ConvertToUint64()); msg.set_source_id(steam_id.ConvertToUint64());
msg.set_allocated_low_level(new Low_Level()); msg.set_allocated_low_level(new Low_Level());
if (online) { if (online) {

View File

@ -79,11 +79,13 @@ Settings::Settings(CSteamID steam_id, CGameID game_id, const std::string &name,
this->offline = offline; this->offline = offline;
} }
// user id
CSteamID Settings::get_local_steam_id() CSteamID Settings::get_local_steam_id()
{ {
return steam_id; return steam_id;
} }
// game id
CGameID Settings::get_local_game_id() CGameID Settings::get_local_game_id()
{ {
return game_id; return game_id;
@ -278,21 +280,21 @@ std::string Settings::getAppInstallPath(AppId_t appID)
return app_paths[appID]; return app_paths[appID];
} }
void Settings::setLeaderboard(std::string leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type) void Settings::setLeaderboard(const std::string &leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type)
{ {
Leaderboard_config leader; Leaderboard_config leader{};
leader.sort_method = sort_method; leader.sort_method = sort_method;
leader.display_type = display_type; leader.display_type = display_type;
leaderboards[leaderboard] = leader; leaderboards[leaderboard] = leader;
} }
std::map<std::string, Leaderboard_config> Settings::getLeaderboards() const std::map<std::string, Leaderboard_config>& Settings::getLeaderboards() const
{ {
return leaderboards; return leaderboards;
} }
const std::map<std::string, Stat_config>& Settings::getStats() const std::map<std::string, Stat_config>& Settings::getStats() const
{ {
return stats; return stats;
} }

View File

@ -599,7 +599,7 @@ static void parse_leaderboards(class Settings *settings_client, Settings *settin
line.pop_back(); line.pop_back();
} }
std::string leaderboard; std::string leaderboard{};
unsigned int sort_method = 0; unsigned int sort_method = 0;
unsigned int display_type = 0; unsigned int display_type = 0;
@ -618,7 +618,7 @@ static void parse_leaderboards(class Settings *settings_client, Settings *settin
settings_client->setLeaderboard(leaderboard, (ELeaderboardSortMethod)sort_method, (ELeaderboardDisplayType)display_type); settings_client->setLeaderboard(leaderboard, (ELeaderboardSortMethod)sort_method, (ELeaderboardDisplayType)display_type);
settings_server->setLeaderboard(leaderboard, (ELeaderboardSortMethod)sort_method, (ELeaderboardDisplayType)display_type); settings_server->setLeaderboard(leaderboard, (ELeaderboardSortMethod)sort_method, (ELeaderboardDisplayType)display_type);
} else { } else {
PRINT_DEBUG("Error adding leaderboard for: %s, are sort method %u or display type %u valid?\n", leaderboard.c_str(), sort_method, display_type); PRINT_DEBUG("Error adding leaderboard for: '%s', are sort method %u or display type %u valid?\n", leaderboard.c_str(), sort_method, display_type);
} }
} }
} }

View File

@ -25,16 +25,16 @@ void Steam_GameServerStats::steam_gameserverstats_network_callback(void *object,
{ {
// PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_network_callback\n"); // PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_network_callback\n");
auto steam_gameserverstats = (Steam_GameServerStats *)object; auto inst = (Steam_GameServerStats *)object;
steam_gameserverstats->network_callback(msg); inst->network_callback(msg);
} }
void Steam_GameServerStats::steam_gameserverstats_run_every_runcb(void *object) void Steam_GameServerStats::steam_gameserverstats_run_every_runcb(void *object)
{ {
// PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_run_every_runcb\n"); // PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_run_every_runcb\n");
auto steam_gameserverstats = (Steam_GameServerStats *)object; auto inst = (Steam_GameServerStats *)object;
steam_gameserverstats->steam_run_callback(); inst->steam_run_callback();
} }
Steam_GameServerStats::CachedStat* Steam_GameServerStats::find_stat(CSteamID steamIDUser, const std::string &key) Steam_GameServerStats::CachedStat* Steam_GameServerStats::find_stat(CSteamID steamIDUser, const std::string &key)
@ -46,11 +46,7 @@ Steam_GameServerStats::CachedStat* Steam_GameServerStats::find_stat(CSteamID ste
it_data->second.stats.begin(), it_data->second.stats.end(), it_data->second.stats.begin(), it_data->second.stats.end(),
[&key](std::pair<const std::string, CachedStat> &item) { [&key](std::pair<const std::string, CachedStat> &item) {
const std::string &name = item.first; const std::string &name = item.first;
return key.size() == name.size() && return common_helpers::str_cmp_insensitive(key, name);
std::equal(
name.begin(), name.end(), key.begin(),
[](char a, char b) { return std::tolower(a) == std::tolower(b); }
);
} }
); );
@ -68,11 +64,7 @@ Steam_GameServerStats::CachedAchievement* Steam_GameServerStats::find_ach(CSteam
it_data->second.achievements.begin(), it_data->second.achievements.end(), it_data->second.achievements.begin(), it_data->second.achievements.end(),
[&key](std::pair<const std::string, CachedAchievement> &item) { [&key](std::pair<const std::string, CachedAchievement> &item) {
const std::string &name = item.first; const std::string &name = item.first;
return key.size() == name.size() && return common_helpers::str_cmp_insensitive(key, name);
std::equal(
name.begin(), name.end(), key.begin(),
[](char a, char b) { return std::tolower(a) == std::tolower(b); }
);
} }
); );

View File

@ -3,6 +3,7 @@
#include <cwchar> #include <cwchar>
#include <algorithm> #include <algorithm>
#include <thread> #include <thread>
#include <cctype>
static bool create_dir_impl(std::filesystem::path &dirpath) static bool create_dir_impl(std::filesystem::path &dirpath)
{ {
@ -142,9 +143,18 @@ std::string common_helpers::uint8_vector_to_hex_string(const std::vector<uint8_t
return result; return result;
} }
bool common_helpers::str_cmp_insensitive(const std::string &str1, const std::string &str2)
{
if (str1.size() != str2.size()) return false;
return std::equal(str1.begin(), str1.end(), str2.begin(), [](const char c1, const char c2){
return std::toupper(c1) == std::toupper(c2);
});
}
std::string common_helpers::ascii_to_lowercase(std::string data) { std::string common_helpers::ascii_to_lowercase(std::string data) {
std::transform(data.begin(), data.end(), data.begin(), std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c){ return std::tolower(c); }); [](char c){ return std::tolower(c); });
return data; return data;
} }

View File

@ -31,6 +31,8 @@ std::string string_strip(const std::string& str);
std::string uint8_vector_to_hex_string(const std::vector<uint8_t>& v); std::string uint8_vector_to_hex_string(const std::vector<uint8_t>& v);
bool str_cmp_insensitive(const std::string &str1, const std::string &str2);
std::string ascii_to_lowercase(std::string data); std::string ascii_to_lowercase(std::string data);
void thisThreadYieldFor(std::chrono::microseconds u); void thisThreadYieldFor(std::chrono::microseconds u);