Avoid keeping player_iterator that might get invalidated before use

This commit is contained in:
Pentarctagon 2024-12-10 07:41:25 -06:00 committed by pentarctagon
parent 66e94c9a6b
commit 02304cd86f
5 changed files with 18 additions and 8 deletions

View file

@ -210,10 +210,10 @@ std::string fuh::get_tournaments(){
return conn_.get_tournaments();
}
void fuh::async_get_and_send_game_history(boost::asio::io_service& io_service, wesnothd::server& s, wesnothd::player_iterator player, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content) {
boost::asio::post([this, &s, player, player_id, offset, &io_service, search_game_name, search_content_type, search_content] {
boost::asio::post(io_service, [player, &s, doc = conn_.get_game_history(player_id, offset, search_game_name, search_content_type, search_content)]{
s.send_to_player(player, *doc);
void fuh::async_get_and_send_game_history(boost::asio::io_service& io_service, wesnothd::server& s, any_socket_ptr socket, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content) {
boost::asio::post([this, &s, socket, player_id, offset, &io_service, search_game_name, search_content_type, search_content] {
boost::asio::post(io_service, [socket, &s, doc = conn_.get_game_history(player_id, offset, search_game_name, search_content_type, search_content)]{
s.send_to_player(socket, *doc);
});
});
}

View file

@ -126,14 +126,14 @@ public:
*
* @param io_service The boost io_service to use to post the query results back to the main boost::asio thread.
* @param s The server instance the player is connected to.
* @param player The player iterator used to communicate with the player's client.
* @param socket The socket used to communicate with the player's client.
* @param player_id The forum ID of the player to get the game history for.
* @param offset Where to start returning rows to the client from the query results.
* @param search_game_name Query for games matching this name. Supports leading and/or trailing wildcards.
* @param search_content_type The content type to query for (ie: scenario)
* @param search_content Query for games using this content ID. Supports leading and/or trailing wildcards.
*/
void async_get_and_send_game_history(boost::asio::io_service& io_service, wesnothd::server& s, wesnothd::player_iterator player, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content);
void async_get_and_send_game_history(boost::asio::io_service& io_service, wesnothd::server& s, any_socket_ptr socket, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content);
/**
* Inserts game related information.

View file

@ -126,7 +126,7 @@ public:
virtual std::string get_uuid() = 0;
virtual std::string get_tournaments() = 0;
virtual void async_get_and_send_game_history(boost::asio::io_service& io_service, wesnothd::server& s, wesnothd::player_iterator player, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content) =0;
virtual void async_get_and_send_game_history(boost::asio::io_service& io_service, wesnothd::server& s, any_socket_ptr socket, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content) =0;
virtual void db_insert_game_info(const std::string& uuid, int game_id, const std::string& version, const std::string& name, int reload, int observers, int is_public, int has_password) = 0;
virtual void db_update_game_end(const std::string& uuid, int game_id, const std::string& replay_location) = 0;
virtual void db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source, const std::string& current_user, const std::string& leaders) = 0;

View file

@ -1183,7 +1183,7 @@ void server::handle_player_in_lobby(player_iterator player, simple_wml::document
std::string search_content = request->attr("search_content").to_string();
LOG_SERVER << "Querying game history requested by player `" << player->info().name() << "` for player id `" << player_id << "`."
<< "Searching for game name `" << search_game_name << "`, search content type `" << search_content_type << "`, search content `" << search_content << "`.";
user_handler_->async_get_and_send_game_history(io_service_, *this, player, player_id, offset, search_game_name, search_content_type, search_content);
user_handler_->async_get_and_send_game_history(io_service_, *this, player->socket(), player_id, offset, search_game_name, search_content_type, search_content);
}
return;
}

View file

@ -19,6 +19,7 @@
#include "server/common/user_handler.hpp"
#include "server/wesnothd/metrics.hpp"
#include "server/wesnothd/ban.hpp"
#include "server/wesnothd/player.hpp"
#include "server/common/simple_wml.hpp"
#include "server/common/server_base.hpp"
#include "server/wesnothd/player_connection.hpp"
@ -83,6 +84,15 @@ public:
player->socket()
);
}
void send_to_player(any_socket_ptr socket, simple_wml::document& data) {
if(player_connections_.get<socket_t>().find(socket) != player_connections_.end())
{
utils::visit(
[this, &data](auto&& socket) { async_send_doc_queued(socket, data); },
socket
);
}
}
void send_server_message_to_lobby(const std::string& message, utils::optional<player_iterator> exclude = {});
void send_server_message_to_all(const std::string& message, utils::optional<player_iterator> exclude = {});