From d7302d684d9d40696a8714f247791705b2fe1de7 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 7 Nov 2017 20:32:36 +1100 Subject: [PATCH] Refactor general-purpose fetch-server-data-with-loading-screen functions This fully splits the wesnothd loading screen handling from the network_transmission dialog and into wesnothd_connection. Much cleaner this way. I've also added wait_and_receive_data as a general spin-and-wait-for-data-to-come-in function instead of having it local in open_connection. --- src/game_initialization/multiplayer.cpp | 14 ++----- src/gui/dialogs/multiplayer/mp_join_game.cpp | 6 +-- src/gui/dialogs/network_transmission.cpp | 39 -------------------- src/gui/dialogs/network_transmission.hpp | 6 +-- src/playmp_controller.cpp | 6 +-- src/wesnothd_connection.cpp | 37 +++++++++++++++++-- src/wesnothd_connection.hpp | 9 +++++ 7 files changed, 52 insertions(+), 65 deletions(-) diff --git a/src/game_initialization/multiplayer.cpp b/src/game_initialization/multiplayer.cpp index f0b4e48a8d91..ca2f929a67e3 100644 --- a/src/game_initialization/multiplayer.cpp +++ b/src/game_initialization/multiplayer.cpp @@ -95,14 +95,6 @@ wesnothd_connection_ptr open_connection(CVideo& video, std::string host) gui2::dialogs::loading_screen::progress(loading_stage::waiting); - const auto wait_for_server_to_send_data = [&sock, &data]() { - while(!sock->has_data_received()) { - SDL_Delay(1); - } - - sock->receive_data(data); - }; - // Then, log in and wait for the lobby/game join prompt. do { if(!sock) { @@ -110,7 +102,7 @@ wesnothd_connection_ptr open_connection(CVideo& video, std::string host) } data.clear(); - wait_for_server_to_send_data(); + sock->wait_and_receive_data(data); if(data.has_child("reject") || data.has_attribute("version")) { std::string version; @@ -184,7 +176,7 @@ wesnothd_connection_ptr open_connection(CVideo& video, std::string host) } sock->send_data(response); - wait_for_server_to_send_data(); + sock->wait_and_receive_data(data); gui2::dialogs::loading_screen::progress(loading_stage::login_response); @@ -262,7 +254,7 @@ wesnothd_connection_ptr open_connection(CVideo& video, std::string host) // Once again send our request... sock->send_data(response); - wait_for_server_to_send_data(); + sock->wait_and_receive_data(data); gui2::dialogs::loading_screen::progress(loading_stage::login_response); diff --git a/src/gui/dialogs/multiplayer/mp_join_game.cpp b/src/gui/dialogs/multiplayer/mp_join_game.cpp index a5a8f91cdbae..b21e21e7aca3 100644 --- a/src/gui/dialogs/multiplayer/mp_join_game.cpp +++ b/src/gui/dialogs/multiplayer/mp_join_game.cpp @@ -25,8 +25,8 @@ #include "gettext.hpp" #include "gui/auxiliary/find_widget.hpp" #include "gui/dialogs/helper.hpp" +#include "gui/dialogs/loading_screen.hpp" #include "gui/dialogs/multiplayer/faction_select.hpp" -#include "gui/dialogs/network_transmission.hpp" #include "gui/dialogs/transient_message.hpp" #include "gui/widgets/button.hpp" #include "gui/widgets/chatbox.hpp" @@ -97,8 +97,8 @@ bool mp_join_game::fetch_game_config(CVideo& video) bool has_scenario_and_controllers = false; while(!has_scenario_and_controllers) { config revc; - const bool data_res = gui2::dialogs::network_transmission::wesnothd_receive_dialog( - video, loading_stage::download_level_data, revc, network_connection_); + const bool data_res = + network_connection_.fetch_data_with_loading_screen(revc, loading_stage::download_level_data); if(!data_res) { return false; diff --git a/src/gui/dialogs/network_transmission.cpp b/src/gui/dialogs/network_transmission.cpp index c9088859d918..e4208f7a683d 100644 --- a/src/gui/dialogs/network_transmission.cpp +++ b/src/gui/dialogs/network_transmission.cpp @@ -23,7 +23,6 @@ #include "gui/widgets/progress_bar.hpp" #include "gui/widgets/label.hpp" #include "gui/widgets/settings.hpp" -#include "gui/dialogs/loading_screen.hpp" #include "gui/widgets/window.hpp" #include "log.hpp" #include "serialization/string_utils.hpp" @@ -99,43 +98,5 @@ void network_transmission::post_show(window& /*window*/) connection_->cancel(); } -void network_transmission::wesnothd_dialog(CVideo& video, network_transmission::connection_data& conn, loading_stage stage) -{ - if (video.faked()) { - while (!conn.finished()) { - conn.poll(); - SDL_Delay(1); - } - } - else { - loading_screen::display(video, [&]() { - loading_screen::progress(stage); - while(!conn.finished()) { - conn.poll(); - SDL_Delay(1); - } - }); - } -} - -struct read_wesnothd_connection_data : public network_transmission::connection_data -{ - read_wesnothd_connection_data(wesnothd_connection& conn) : conn_(conn) {} - size_t total() override { return conn_.bytes_to_read(); } - virtual size_t current() override { return conn_.bytes_read(); } - virtual bool finished() override { return conn_.has_data_received(); } - virtual void cancel() override { } - virtual void poll() override { } - wesnothd_connection& conn_; -}; - -bool network_transmission::wesnothd_receive_dialog(CVideo& video, loading_stage stage, config& cfg, wesnothd_connection& connection) -{ - assert(stage != loading_stage::none); - read_wesnothd_connection_data gui_data(connection); - wesnothd_dialog(video, gui_data, stage); - return connection.receive_data(cfg); -} - } // namespace dialogs } // namespace gui2 diff --git a/src/gui/dialogs/network_transmission.hpp b/src/gui/dialogs/network_transmission.hpp index 056594db441e..2ef6429ee5d3 100644 --- a/src/gui/dialogs/network_transmission.hpp +++ b/src/gui/dialogs/network_transmission.hpp @@ -15,7 +15,6 @@ #pragma once #include "events.hpp" -#include "gui/dialogs/loading_screen.hpp" #include "gui/dialogs/modal_dialog.hpp" #include "network_asio.hpp" #include "wesnothd_connection.hpp" @@ -36,7 +35,7 @@ namespace dialogs class network_transmission : public modal_dialog { public: - //A wrapper of either a wesnothd_connection or a network_asio::connection + /** A wrapper of either a wesnothd_connection or a network_asio::connection. */ class connection_data { public: @@ -48,10 +47,7 @@ class network_transmission : public modal_dialog virtual ~connection_data() {} }; - static bool wesnothd_receive_dialog(CVideo& video, loading_stage stage, config& cfg, wesnothd_connection& connection); - private: - static void wesnothd_dialog(CVideo& video, connection_data& conn, loading_stage stage); connection_data* connection_; class pump_monitor : public events::pump_monitor diff --git a/src/playmp_controller.cpp b/src/playmp_controller.cpp index ad182e8aa934..da6c0077f6cd 100644 --- a/src/playmp_controller.cpp +++ b/src/playmp_controller.cpp @@ -18,7 +18,7 @@ #include "actions/undo.hpp" #include "display_chat_manager.hpp" #include "game_end_exceptions.hpp" -#include "gui/dialogs/network_transmission.hpp" +#include "gui/dialogs/loading_screen.hpp" #include "gettext.hpp" #include "hotkey/hotkey_handler_mp.hpp" #include "log.hpp" @@ -287,8 +287,8 @@ void playmp_controller::wait_for_upload() network_reader_.set_source(playturn_network_adapter::get_source_from_config(cfg)); while(true) { try { - const bool res = gui2::dialogs::network_transmission::wesnothd_receive_dialog( - gui_->video(), loading_stage::next_scenario, cfg, mp_info_->connection); + const bool res = + mp_info_->connection.fetch_data_with_loading_screen(cfg, loading_stage::next_scenario); if(res) { if (turn_data_.process_network_data_from_reader() == turn_info::PROCESS_END_LINGER) { diff --git a/src/wesnothd_connection.cpp b/src/wesnothd_connection.cpp index 90919ece3997..b8bda33a4d51 100644 --- a/src/wesnothd_connection.cpp +++ b/src/wesnothd_connection.cpp @@ -12,14 +12,19 @@ See the COPYING file for more details. */ -#include -#include "utils/functional.hpp" -#include -#include "log.hpp" #include "wesnothd_connection.hpp" + +#include "gui/dialogs/loading_screen.hpp" +#include "log.hpp" #include "serialization/parser.hpp" +#include "utils/functional.hpp" +#include "video.hpp" #include +#include + +#include +#include static lg::log_domain log_network("network"); #define DBG_NW LOG_STREAM(debug, log_network) @@ -374,6 +379,30 @@ bool wesnothd_connection::receive_data(config& result) return false; } +bool wesnothd_connection::wait_and_receive_data(config& data) +{ + while(!has_data_received()) { + SDL_Delay(1); + } + + return receive_data(data); +}; + + +bool wesnothd_connection::fetch_data_with_loading_screen(config& cfg, loading_stage stage) +{ + assert(stage != loading_stage::none); + + bool res = false; + gui2::dialogs::loading_screen::display(CVideo::get_singleton(), [&]() { + gui2::dialogs::loading_screen::progress(stage); + + res = wait_and_receive_data(cfg); + }); + + return res; +} + wesnothd_connection::~wesnothd_connection() { MPTEST_LOG; diff --git a/src/wesnothd_connection.hpp b/src/wesnothd_connection.hpp index 7d396fceaf5c..9486f7c25e7e 100644 --- a/src/wesnothd_connection.hpp +++ b/src/wesnothd_connection.hpp @@ -42,6 +42,7 @@ namespace boost class config; class wesnothd_connection_ptr; +enum class loading_stage; /** A class that represents a TCP/IP connection to the wesnothd server. */ class wesnothd_connection : public std::enable_shared_from_this @@ -64,10 +65,18 @@ class wesnothd_connection : public std::enable_shared_from_this