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)
@ -98,13 +97,15 @@ namespace server_list
void write_favorite_servers() void write_favorite_servers()
{ {
std::string servers_buffer = ""; favorite_servers.access([&](std::unordered_set<game::netadr_t>& servers)
for (auto itr : favorite_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)); std::string servers_buffer = "";
} for (auto itr : servers)
{
utils::io::write_file(get_favorite_servers_file_path(), servers_buffer); 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() 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)
{
std::string filedata; servers.clear();
if (utils::io::read_file(path, &filedata)) std::string filedata;
{ if (utils::io::read_file(path, &filedata))
auto servers = utils::string::split(filedata, '\n');
for (auto server_address : servers)
{ {
auto server = network::address_from_string(server_address); auto srv = utils::string::split(filedata, '\n');
favorite_servers.insert(server); 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) 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)
{ {
if (network::are_addresses_equal(*it, addr)) for (auto it = servers.begin(); it != servers.end(); ++it)
{ {
favorite_servers.erase(it); if (network::are_addresses_equal(*it, addr))
break; {
} servers.erase(it);
} 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,8 +1,10 @@
#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);
using callback = std::function<void(bool, const std::unordered_set<game::netadr_t>&)>; using callback = std::function<void(bool, const std::unordered_set<game::netadr_t>&)>;
@ -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,39 +219,41 @@ namespace steam
{ {
favorites_response = pRequestServersResponse; favorites_response = pRequestServersResponse;
const auto res = favorites_response.load(); auto& srvs = server_list::get_favorite_servers();
if (!res) srvs.access([&](std::unordered_set<game::netadr_t> s)
{ {
return favorites_request; const auto res = favorites_response.load();
} if (!res)
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)
{ {
server new_server{}; return;
new_server.address = address; }
new_server.server_item = create_server_item(address, {}, 0, false);
if (s.empty())
srvs.push_back(new_server); {
res->RefreshComplete(favorites_request, eNoServersListedOnMasterServer);
return;
} }
});
for (auto& srv : s) favorites_servers.access([s](servers& srvs)
{ {
ping_server(srv, handle_favorites_server_response); 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; return favorites_request;
} }