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 <utils/string.hpp>
#include <utils/concurrency.hpp>
#include <utils/hook.hpp>
#include <utils/io.hpp>
@ -28,7 +27,7 @@ namespace server_list
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,
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<game::netadr_t>& 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<game::netadr_t>& 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<game::netadr_t>& 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<game::netadr_t>& 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<game::netadr_t>& get_favorite_servers()
utils::concurrency::container<server_list>& get_favorite_servers()
{
return favorite_servers;
}

View File

@ -1,8 +1,10 @@
#pragma once
#include <game/game.hpp>
#include <game/game.hpp>
#include <utils/concurrency.hpp>
namespace server_list
{
{
bool get_master_server(game::netadr_t& address);
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 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;
const auto res = favorites_response.load();
if (!res)
auto& srvs = server_list::get_favorite_servers();
srvs.access([&](std::unordered_set<game::netadr_t> 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;
}