changed favorite servers list to concurrency container

This commit is contained in:
Jari van der Kaap 2023-04-22 18:53:56 +02:00
parent f68c8269e3
commit 5b71627d74
3 changed files with 74 additions and 61 deletions

View File

@ -5,7 +5,6 @@
#include "game/game.hpp" #include "game/game.hpp"
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/concurrency.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
#include <utils/io.hpp> #include <utils/io.hpp>
@ -28,7 +27,7 @@ namespace server_list
utils::concurrency::container<state> master_state; utils::concurrency::container<state> master_state;
std::unordered_set<game::netadr_t> favorite_servers{}; utils::concurrency::container<server_list> favorite_servers{};
void handle_server_list_response(const game::netadr_t& target, void handle_server_list_response(const game::netadr_t& target,
const network::data_view& data, state& s) const network::data_view& data, state& s)
@ -97,14 +96,16 @@ namespace server_list
} }
void write_favorite_servers() void write_favorite_servers()
{
favorite_servers.access([&](std::unordered_set<game::netadr_t>& servers)
{ {
std::string servers_buffer = ""; std::string servers_buffer = "";
for (auto itr : favorite_servers) 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)); 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); utils::io::write_file(get_favorite_servers_file_path(), servers_buffer);
});
} }
void read_favorite_servers() void read_favorite_servers()
@ -115,18 +116,20 @@ namespace server_list
return; return;
} }
favorite_servers.clear(); favorite_servers.access([&path](std::unordered_set<game::netadr_t>& servers)
{
servers.clear();
std::string filedata; std::string filedata;
if (utils::io::read_file(path, &filedata)) if (utils::io::read_file(path, &filedata))
{ {
auto servers = utils::string::split(filedata, '\n'); auto srv = utils::string::split(filedata, '\n');
for (auto server_address : servers) for (auto server_address : srv)
{ {
auto server = network::address_from_string(server_address); auto server = network::address_from_string(server_address);
favorite_servers.insert(server); servers.insert(server);
} }
} }
});
} }
} }
@ -157,25 +160,30 @@ namespace server_list
void add_favorite_server(game::netadr_t addr) void add_favorite_server(game::netadr_t addr)
{ {
favorite_servers.insert(addr); favorite_servers.access([&addr](std::unordered_set<game::netadr_t>& servers)
{
servers.insert(addr);
});
write_favorite_servers(); write_favorite_servers();
} }
void remove_favorite_server(game::netadr_t addr) void remove_favorite_server(game::netadr_t addr)
{ {
for (auto it = favorite_servers.begin(); it != favorite_servers.end(); ++it) favorite_servers.access([&addr](std::unordered_set<game::netadr_t>& servers)
{
for (auto it = servers.begin(); it != servers.end(); ++it)
{ {
if (network::are_addresses_equal(*it, addr)) if (network::are_addresses_equal(*it, addr))
{ {
favorite_servers.erase(it); servers.erase(it);
break; break;
} }
} }
});
write_favorite_servers(); write_favorite_servers();
} }
std::unordered_set<game::netadr_t>& get_favorite_servers() utils::concurrency::container<server_list>& get_favorite_servers()
{ {
return favorite_servers; return favorite_servers;
} }

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <game/game.hpp> #include <game/game.hpp>
#include <utils/concurrency.hpp>
namespace server_list namespace server_list
{ {
bool get_master_server(game::netadr_t& address); bool get_master_server(game::netadr_t& address);
@ -10,5 +12,6 @@ namespace server_list
void add_favorite_server(game::netadr_t addr); void add_favorite_server(game::netadr_t addr);
void remove_favorite_server(game::netadr_t addr); void remove_favorite_server(game::netadr_t addr);
std::unordered_set<game::netadr_t>& get_favorite_servers(); using server_list = std::unordered_set<game::netadr_t>;
utils::concurrency::container<server_list>& get_favorite_servers();
} }

View File

@ -219,18 +219,19 @@ namespace steam
{ {
favorites_response = pRequestServersResponse; favorites_response = pRequestServersResponse;
auto& srvs = server_list::get_favorite_servers();
srvs.access([&](std::unordered_set<game::netadr_t> s)
{
const auto res = favorites_response.load(); const auto res = favorites_response.load();
if (!res) if (!res)
{ {
return favorites_request; return;
} }
auto s = server_list::get_favorite_servers();
if (s.empty()) if (s.empty())
{ {
res->RefreshComplete(favorites_request, eNoServersListedOnMasterServer); res->RefreshComplete(favorites_request, eNoServersListedOnMasterServer);
return favorites_request; return;
} }
favorites_servers.access([s](servers& srvs) favorites_servers.access([s](servers& srvs)
@ -252,6 +253,7 @@ namespace steam
{ {
ping_server(srv, handle_favorites_server_response); ping_server(srv, handle_favorites_server_response);
} }
});
return favorites_request; return favorites_request;
} }