fix merge

This commit is contained in:
Jari van der Kaap 2023-04-22 21:34:32 +02:00
parent 9147501b8d
commit dc463bc812

View File

@ -7,6 +7,7 @@
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/concurrency.hpp> #include <utils/concurrency.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
#include <utils/io.hpp>
#include "network.hpp" #include "network.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
@ -20,7 +21,7 @@ namespace server_list
struct state struct state
{ {
game::netadr_t address{}; game::netadr_t address{};
bool requesting{false}; bool requesting{ false };
std::chrono::high_resolution_clock::time_point query_start{}; std::chrono::high_resolution_clock::time_point query_start{};
callback callback{}; callback callback{};
}; };
@ -30,7 +31,7 @@ namespace server_list
utils::concurrency::container<server_list> 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)
{ {
if (!s.requesting || s.address != target) if (!s.requesting || s.address != target)
{ {
@ -142,20 +143,20 @@ namespace server_list
void request_servers(callback callback) void request_servers(callback callback)
{ {
master_state.access([&callback](state& s) master_state.access([&callback](state& s)
{
game::netadr_t addr{};
if (!get_master_server(addr))
{ {
return; game::netadr_t addr{};
} if (!get_master_server(addr))
{
return;
}
s.requesting = true; s.requesting = true;
s.address = addr; s.address = addr;
s.callback = std::move(callback); s.callback = std::move(callback);
s.query_start = std::chrono::high_resolution_clock::now(); s.query_start = std::chrono::high_resolution_clock::now();
network::send(s.address, "getservers", utils::string::va("T7 %i full empty", PROTOCOL)); network::send(s.address, "getservers", utils::string::va("T7 %i full empty", PROTOCOL));
}); });
} }
void add_favorite_server(game::netadr_t addr) void add_favorite_server(game::netadr_t addr)
@ -193,44 +194,49 @@ namespace server_list
void post_unpack() override void post_unpack() override
{ {
network::on("getServersResponse", [](const game::netadr_t& target, const network::data_view& data) network::on("getServersResponse", [](const game::netadr_t& target, const network::data_view& data)
{
master_state.access([&](state& s)
{ {
handle_server_list_response(target, data, s); master_state.access([&](state& s)
{
handle_server_list_response(target, data, s);
});
}); });
});
scheduler::loop([] scheduler::loop([]
{
master_state.access([](state& s)
{ {
if (!s.requesting) master_state.access([](state& s)
{ {
return; if (!s.requesting)
} {
return;
}
const auto now = std::chrono::high_resolution_clock::now(); const auto now = std::chrono::high_resolution_clock::now();
if ((now - s.query_start) < 2s) if ((now - s.query_start) < 2s)
{ {
return; return;
} }
s.requesting = false; s.requesting = false;
s.callback(false, {}); s.callback(false, {});
s.callback = {}; s.callback = {};
}); });
}, scheduler::async, 200ms); }, scheduler::async, 200ms);
lua_serverinfo_to_table_hook.create(0x141F1FD10_g, lua_serverinfo_to_table_stub); lua_serverinfo_to_table_hook.create(0x141F1FD10_g, lua_serverinfo_to_table_stub);
scheduler::once([]
{
read_favorite_servers();
}, scheduler::main);
} }
void pre_destroy() override void pre_destroy() override
{ {
master_state.access([](state& s) master_state.access([](state& s)
{ {
s.requesting = false; s.requesting = false;
s.callback = {}; s.callback = {};
}); });
} }
}; };
} }