Skip to content

Commit

Permalink
Refactor general-purpose fetch-server-data-with-loading-screen functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Vultraz committed Nov 7, 2017
1 parent e07a32e commit d7302d6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 65 deletions.
14 changes: 3 additions & 11 deletions src/game_initialization/multiplayer.cpp
Expand Up @@ -95,22 +95,14 @@ 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) {
return sock;
}

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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/gui/dialogs/multiplayer/mp_join_game.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 0 additions & 39 deletions src/gui/dialogs/network_transmission.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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
6 changes: 1 addition & 5 deletions src/gui/dialogs/network_transmission.hpp
Expand Up @@ -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"
Expand All @@ -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:
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/playmp_controller.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
37 changes: 33 additions & 4 deletions src/wesnothd_connection.cpp
Expand Up @@ -12,14 +12,19 @@
See the COPYING file for more details.
*/

#include <deque>
#include "utils/functional.hpp"
#include <cstdint>
#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 <boost/thread.hpp>
#include <SDL_timer.h>

#include <cstdint>
#include <deque>

static lg::log_domain log_network("network");
#define DBG_NW LOG_STREAM(debug, log_network)
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/wesnothd_connection.hpp
Expand Up @@ -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<wesnothd_connection>
Expand All @@ -64,10 +65,18 @@ class wesnothd_connection : public std::enable_shared_from_this<wesnothd_connect
public:
static wesnothd_connection_ptr create(const std::string& host, const std::string& service);

bool fetch_data_with_loading_screen(config& cfg, loading_stage stage);

void send_data(const configr_of& request);

bool receive_data(config& result);

/**
* Helper function that spins until data has been received.
* Should be used in tandem with the loading screen or other multi-threaded components.
*/
bool wait_and_receive_data(config& data);

/** Handle all pending asynchonous events and return */
std::size_t poll();
/** Run asio's event loop
Expand Down

1 comment on commit d7302d6

@GregoryLundberg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New on Windows with this ...

wesnothd_connection.cpp
C:\Users\lundberg\Source\Repos\external\include\boost/asio/detail/impl/socket_ops.ipp(2358): warning C4996: 'gethostbyaddr': Use getnameinfo() or GetNameInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings (compiling source file ..\..\src\wesnothd_connection.cpp)
C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\winsock2.h(2196): note: see declaration of 'gethostbyaddr' (compiling source file ..\..\src\wesnothd_connection.cpp)
C:\Users\lundberg\Source\Repos\external\include\boost/asio/detail/impl/socket_ops.ipp(2407): warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings (compiling source file ..\..\src\wesnothd_connection.cpp)
C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\winsock2.h(2218): note: see declaration of 'gethostbyname' (compiling source file ..\..\src\wesnothd_connection.cpp)

Please sign in to comment.