Skip to content

Commit

Permalink
Revert network_transmission.cpp/hpp to its 1.14.7 version
Browse files Browse the repository at this point in the history
I re-ran the timing test discussed in PR #4201 (comment linked to below), and
saw similar numbers.  The old, simpler code downloads an 86.1MB addon in 25
seconds, the code with the extra thread takes 29 seconds.

#4201 (comment)
  • Loading branch information
stevecotton committed Aug 24, 2019
1 parent afab04d commit c4602d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 67 deletions.
76 changes: 22 additions & 54 deletions src/gui/dialogs/network_transmission.cpp
Expand Up @@ -27,63 +27,36 @@
#include "serialization/string_utils.hpp"
#include "wesnothd_connection.hpp"

#include <chrono>

namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(network_transmission)

network_transmission::pump_monitor::pump_monitor(connection_data*& connection)
: connection_(connection)
, window_()
, completed_(0)
, total_(0)
, stop_(false)
, poller_(std::async(std::launch::async, [this]() {
while(!stop_) {
// Check for updates
connection_->poll();

if(connection_->finished()) {
return;
}

completed_ = connection_->current();
total_ = connection_->total();

std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}))
{
}
REGISTER_DIALOG(network_transmission)

void network_transmission::pump_monitor::process(events::pump_info&)
{
if(!window_) {
if(!window_)
return;
}

// Check if the thread is complete. If it is, loading is done.
if(poller_.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
connection_->poll();
if(connection_->finished()) {
window_.get().set_retval(retval::OK);
return;
}

if(total_) {
find_widget<progress_bar>(&(window_.get()), "progress", false)
.set_percentage((completed_ * 100.) / total_);

std::ostringstream ss;
ss
<< utils::si_string(completed_, true, _("unit_byte^B")) << "/"
<< utils::si_string(total_, true, _("unit_byte^B"));

find_widget<label>(&(window_.get()), "numeric_progress", false)
.set_label(ss.str());

window_->invalidate_layout();
} else {
size_t completed, total;
completed = connection_->current();
total = connection_->total();
if(total) {
find_widget<progress_bar>(&(window_.get()), "progress", false)
.set_percentage((completed * 100.) / total);

std::stringstream ss;
ss << utils::si_string(completed, true, _("unit_byte^B")) << "/"
<< utils::si_string(total, true, _("unit_byte^B"));

find_widget<label>(&(window_.get()), "numeric_progress", false)
.set_label(ss.str());
window_->invalidate_layout();
}
}
}

Expand All @@ -103,8 +76,8 @@ void network_transmission::pre_show(window& window)
{
// ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
if(!subtitle_.empty()) {
label& subtitle_label = find_widget<label>(&window, "subtitle", false);

label& subtitle_label
= find_widget<label>(&window, "subtitle", false);
subtitle_label.set_label(subtitle_);
subtitle_label.set_use_markup(true);
}
Expand All @@ -117,11 +90,6 @@ void network_transmission::post_show(window& /*window*/)
pump_monitor_.window_.reset();

if(get_retval() == retval::CANCEL) {
// We need to wait for the current polling loop to conclude before exiting so we don't invalidate
// the pointer mid-loop, so signal that here.
pump_monitor_.stop_ = true;
pump_monitor_.poller_.wait();

connection_->cancel();
}
}
Expand Down
19 changes: 6 additions & 13 deletions src/gui/dialogs/network_transmission.hpp
Expand Up @@ -21,9 +21,6 @@

#include <boost/optional.hpp>

#include <atomic>
#include <future>

namespace gui2
{
namespace dialogs
Expand Down Expand Up @@ -56,19 +53,15 @@ class network_transmission : public modal_dialog
class pump_monitor : public events::pump_monitor
{
public:
virtual void process(events::pump_info&) override;

pump_monitor(connection_data*& connection);

connection_data*& connection_;
virtual void process(events::pump_info&);

boost::optional<window&> window_;
pump_monitor(connection_data*& connection)
: connection_(connection), window_()
{
}

std::atomic_size_t completed_, total_;

std::atomic_bool stop_;

std::future<void> poller_;
boost::optional<window&> window_;
} pump_monitor_;

public:
Expand Down

2 comments on commit c4602d7

@jostephd
Copy link
Member

Choose a reason for hiding this comment

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

Fwdport needed?

@stevecotton
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Vultraz I didn't forward port #4244 yet, but am waiting for further feedback on #4244. I think this should be applied to 1.15 too, please would you give feedback?

Please sign in to comment.