Skip to content

Commit

Permalink
Merge branch 'HorizonMW:main' into Add-build-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
moi15moi authored Sep 21, 2024
2 parents a6b1a83 + 434c752 commit e1f6a22
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 28 deletions.
126 changes: 99 additions & 27 deletions src/client/component/server_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ namespace server_list
int current_page = 0;
bool is_loading_page = false;

// Threading
std::mutex server_list_mutex;
std::condition_variable server_list_cv;
int active_threads = 0; // Count of active fetch threads

// Used for when we're refreshing the server / favourites list
bool getting_server_list = false;
bool getting_favourites = false;
Expand Down Expand Up @@ -145,7 +150,7 @@ namespace server_list

void refresh_server_list()
{
if (tcp::is_loading_page) {
if (tcp::getting_server_list || tcp::getting_favourites || tcp::is_loading_page) {
return;
}

Expand All @@ -157,6 +162,9 @@ namespace server_list
tcp::current_page = 0;

server_list_page = 0;

tcp::interrupt_favourites = false;
tcp::interrupt_server_list = false;
}

ui_scripting::notify("updateGameList", {});
Expand Down Expand Up @@ -195,6 +203,8 @@ namespace server_list
bool canJoin = server_list::tcp::check_can_join(servers[i].connect_address);
if (canJoin) {
//command::execute("connect " + servers[i].connect_address);
tcp::interrupt_favourites = true;
tcp::interrupt_server_list = true;
party::connect(servers[i].address);
}
else {
Expand Down Expand Up @@ -277,8 +287,22 @@ namespace server_list

void insert_server(server_info&& server)
{
// Do not exceed the server limit
if (servers.size() >= tcp::server_limit_per_page) {
return;
}

std::lock_guard<std::mutex> _(mutex);
servers.emplace_back(std::move(server));

// Duplicate handling
auto it = std::find_if(servers.begin(), servers.end(),
[&server](const server_info& existing_server) {
return existing_server.connect_address == server.connect_address;
});

if (it == servers.end()) {
servers.emplace_back(std::move(server));
}
}

bool is_server_list_open()
Expand Down Expand Up @@ -352,6 +376,10 @@ namespace server_list
}

void tcp::sort_current_page(int sort_type) {
if (getting_server_list || getting_favourites || is_loading_page) {
return;
}

auto servers_cache = servers;

{
Expand Down Expand Up @@ -391,6 +419,47 @@ namespace server_list
}, scheduler::pipeline::main, 125ms);
}

bool tcp::is_getting_server_list()
{
return getting_server_list;
}

bool tcp::is_getting_favourites()
{
return getting_favourites;
}

bool tcp::is_loading_a_page()
{
return is_loading_page;
}

void tcp::fetch_game_server_info(const std::string& connect_address, int server_index) {
if (interrupt_server_list || interrupt_favourites) {
return;
}

std::string game_server_info = connect_address + "/getInfo";
std::string game_server_response = hmw_tcp_utils::GET_url(game_server_info.c_str(), true);

if (!game_server_response.empty()) {
if (interrupt_server_list || interrupt_favourites) {
return; // We got interrupted mid request.
}

std::lock_guard<std::mutex> lock(server_list_mutex);
tcp::add_server_to_list(game_server_response, connect_address, server_index);
ui_scripting::notify("updateGameList", {});
}

// Lock and decrement the active thread count
{
std::lock_guard<std::mutex> lock(server_list_mutex);
active_threads--;
}
server_list_cv.notify_one(); // Notify that a thread has finished
}

int get_player_count()
{
std::lock_guard<std::mutex> _(mutex);
Expand Down Expand Up @@ -527,8 +596,9 @@ namespace server_list

int server_index = 0;

#ifdef _DEBUG
// @Aphrodite todo, update this to be dynamic and not hard coded to 27017
// Don't lock this behind debug.
//#ifdef _DEBUG
// @CB todo, update this to be dynamic and not hard coded to 27017
console::info("Checking if localhost server is running on default port (27017)");
std::string port = "27017"; // Change this to the dynamic port range @todo
bool localhost = hmw_tcp_utils::GameServer::is_localhost(port);
Expand All @@ -538,7 +608,7 @@ namespace server_list
ui_scripting::notify("updateGameList", {});
server_index++;
}
#endif
//#endif

// Master server did not respond
if (master_server_list.empty()) {
Expand All @@ -553,33 +623,34 @@ namespace server_list

nlohmann::json master_server_response_json = nlohmann::json::parse(master_server_list);

// Parse server list
for (const auto& element : master_server_response_json)
{
if (interrupt_server_list)
{
for (const auto& element : master_server_response_json) {
if (interrupt_server_list) {
break;
}

std::string connect_address = element.get<std::string>();
std::string game_server_info = connect_address + "/getInfo";
std::string game_server_response = hmw_tcp_utils::GET_url(game_server_info.c_str(), true);

if (game_server_response.empty())
std::string connect_address = element.get<std::string>();

{
continue;
std::lock_guard<std::mutex> lock(server_list_mutex);
active_threads++;
}

tcp::add_server_to_list(game_server_response, connect_address, server_index);
ui_scripting::notify("updateGameList", {});
std::thread([connect_address, server_index]() {
fetch_game_server_info(connect_address, server_index);
}).detach();

server_index++;
}

std::unique_lock<std::mutex> lock(server_list_mutex);
server_list_cv.wait(lock, [] { return active_threads == 0; });

load_page(0, false);
interrupt_server_list = getting_server_list = false;
ui_scripting::notify("updateGameList", {});
ui_scripting::notify("hideRefreshingNotification", {});
ui_scripting::notify("updateRefreshTimer", {});

// Auto sort on completion not working
//sort_current_page(list_sort_type); // Sort after populating
}
Expand Down Expand Up @@ -683,7 +754,7 @@ namespace server_list

void tcp::next_page()
{
if (is_loading_page) {
if (getting_server_list || getting_favourites || is_loading_page) {
return;
}

Expand All @@ -697,7 +768,7 @@ namespace server_list

void tcp::previous_page()
{
if (is_loading_page) {
if (getting_server_list || getting_favourites || is_loading_page) {
return;
}

Expand Down Expand Up @@ -949,6 +1020,7 @@ namespace server_list
}

int server_index = 0;

for (auto& element : obj)
{
if (interrupt_favourites)
Expand All @@ -962,17 +1034,17 @@ namespace server_list
}

std::string connect_address = element;
std::string game_server_info = connect_address + "/getInfo";
std::string game_server_response = hmw_tcp_utils::GET_url(game_server_info.c_str(), true);

// Don't show any non TCP servers
if (game_server_response.empty())
{
continue;
std::lock_guard<std::mutex> lock(server_list_mutex);
active_threads++; // Increment active thread count
}

tcp::add_server_to_list(game_server_response, connect_address, server_index);
ui_scripting::notify("updateGameList", {});
// Detach a thread to fetch game server info
std::thread([connect_address, server_index]() {
fetch_game_server_info(connect_address, server_index);
}).detach();

server_index++;
}

Expand Down
7 changes: 7 additions & 0 deletions src/client/component/server_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ namespace server_list

void sort_current_page(int sort_type);

// Functions to pass these to lua
bool is_getting_server_list();
bool is_getting_favourites();
bool is_loading_a_page();

void fetch_game_server_info(const std::string& connect_address, int server_index);

}
}
4 changes: 3 additions & 1 deletion src/client/component/ui_scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ namespace ui_scripting
//server_list::sort_serverlist(sort_type);
server_list::tcp::sort_current_page(sort_type);
};

server_list_table["isgettingserverlist"] = server_list::tcp::is_getting_server_list;
server_list_table["isgettingfavourites"] = server_list::tcp::is_getting_favourites;
server_list_table["isloadingpage"] = server_list::tcp::is_loading_a_page;

auto download_table = table();
lua["download"] = download_table;
Expand Down

0 comments on commit e1f6a22

Please sign in to comment.