diff --git a/src/client/component/server_list.cpp b/src/client/component/server_list.cpp index 14ae7112..332279ef 100644 --- a/src/client/component/server_list.cpp +++ b/src/client/component/server_list.cpp @@ -5,7 +5,6 @@ #include "game/game.hpp" #include -#include #include #include @@ -28,7 +27,7 @@ namespace server_list utils::concurrency::container master_state; - std::unordered_set favorite_servers{}; + utils::concurrency::container favorite_servers{}; void handle_server_list_response(const game::netadr_t& target, const network::data_view& data, state& s) @@ -98,13 +97,15 @@ namespace server_list void write_favorite_servers() { - std::string servers_buffer = ""; - for (auto itr : favorite_servers) + favorite_servers.access([&](std::unordered_set& servers) { - servers_buffer.append(utils::string::va("%i.%i.%i.%i:%u\n", itr.ipv4.a, itr.ipv4.b, itr.ipv4.c, itr.ipv4.d, itr.port)); - } - - utils::io::write_file(get_favorite_servers_file_path(), servers_buffer); + std::string servers_buffer = ""; + for (auto itr : servers) + { + servers_buffer.append(utils::string::va("%i.%i.%i.%i:%u\n", itr.ipv4.a, itr.ipv4.b, itr.ipv4.c, itr.ipv4.d, itr.port)); + } + utils::io::write_file(get_favorite_servers_file_path(), servers_buffer); + }); } void read_favorite_servers() @@ -115,18 +116,20 @@ namespace server_list return; } - favorite_servers.clear(); - - std::string filedata; - if (utils::io::read_file(path, &filedata)) - { - auto servers = utils::string::split(filedata, '\n'); - for (auto server_address : servers) + favorite_servers.access([&path](std::unordered_set& servers) + { + servers.clear(); + std::string filedata; + if (utils::io::read_file(path, &filedata)) { - auto server = network::address_from_string(server_address); - favorite_servers.insert(server); - } - } + auto srv = utils::string::split(filedata, '\n'); + for (auto server_address : srv) + { + auto server = network::address_from_string(server_address); + servers.insert(server); + } + } + }); } } @@ -157,25 +160,30 @@ namespace server_list void add_favorite_server(game::netadr_t addr) { - favorite_servers.insert(addr); + favorite_servers.access([&addr](std::unordered_set& servers) + { + servers.insert(addr); + }); write_favorite_servers(); } void remove_favorite_server(game::netadr_t addr) { - for (auto it = favorite_servers.begin(); it != favorite_servers.end(); ++it) - { - if (network::are_addresses_equal(*it, addr)) + favorite_servers.access([&addr](std::unordered_set& servers) + { + for (auto it = servers.begin(); it != servers.end(); ++it) { - favorite_servers.erase(it); - break; - } - } - + if (network::are_addresses_equal(*it, addr)) + { + servers.erase(it); + break; + } + } + }); write_favorite_servers(); } - std::unordered_set& get_favorite_servers() + utils::concurrency::container& get_favorite_servers() { return favorite_servers; } diff --git a/src/client/component/server_list.hpp b/src/client/component/server_list.hpp index a41ad1d4..aed5cc71 100644 --- a/src/client/component/server_list.hpp +++ b/src/client/component/server_list.hpp @@ -1,8 +1,10 @@ #pragma once -#include +#include + +#include namespace server_list -{ +{ bool get_master_server(game::netadr_t& address); using callback = std::function&)>; @@ -10,5 +12,6 @@ namespace server_list void add_favorite_server(game::netadr_t addr); void remove_favorite_server(game::netadr_t addr); - std::unordered_set& get_favorite_servers(); + using server_list = std::unordered_set; + utils::concurrency::container& get_favorite_servers(); } diff --git a/src/client/steam/interfaces/matchmaking_servers.cpp b/src/client/steam/interfaces/matchmaking_servers.cpp index 91191e90..e42d4a61 100644 --- a/src/client/steam/interfaces/matchmaking_servers.cpp +++ b/src/client/steam/interfaces/matchmaking_servers.cpp @@ -219,39 +219,41 @@ namespace steam { favorites_response = pRequestServersResponse; - const auto res = favorites_response.load(); - if (!res) + auto& srvs = server_list::get_favorite_servers(); + srvs.access([&](std::unordered_set s) { - return favorites_request; - } - - auto s = server_list::get_favorite_servers(); - - if (s.empty()) - { - res->RefreshComplete(favorites_request, eNoServersListedOnMasterServer); - return favorites_request; - } - - favorites_servers.access([s](servers& srvs) - { - srvs = {}; - srvs.reserve(s.size()); - - for (auto& address : s) + const auto res = favorites_response.load(); + if (!res) { - server new_server{}; - new_server.address = address; - new_server.server_item = create_server_item(address, {}, 0, false); - - srvs.push_back(new_server); + return; + } + + if (s.empty()) + { + res->RefreshComplete(favorites_request, eNoServersListedOnMasterServer); + return; } - }); - for (auto& srv : s) - { - ping_server(srv, handle_favorites_server_response); - } + favorites_servers.access([s](servers& srvs) + { + srvs = {}; + srvs.reserve(s.size()); + + for (auto& address : s) + { + server new_server{}; + new_server.address = address; + new_server.server_item = create_server_item(address, {}, 0, false); + + srvs.push_back(new_server); + } + }); + + for (auto& srv : s) + { + ping_server(srv, handle_favorites_server_response); + } + }); return favorites_request; }