Skip to content

Commit

Permalink
Manual forward-port of #4244.
Browse files Browse the repository at this point in the history
This seems to have been applied to 1.14, but not 1.15/master at the time.
  • Loading branch information
Pentarctagon committed Jun 19, 2022
1 parent f00ee60 commit 5acc110
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 71 deletions.
79 changes: 21 additions & 58 deletions src/gui/dialogs/network_transmission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,68 +27,35 @@
#include "log.hpp"
#include "serialization/string_utils.hpp"

#include <chrono>
#include <thread>

namespace gui2::dialogs
{
using namespace std::chrono_literals;

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(10ms);
}
}))
{
}

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(0ms) == std::future_status::ready) {
// The worker returns void, so this is only to handle any exceptions thrown from the worker.
// worker_result_.valid() will return false after.
if(poller_.valid()) {
poller_.get();
}

connection_->poll();
if(connection_->finished()) {
window_->set_retval(retval::OK);
return;
}

if(total_) {
find_widget<progress_bar>(window_.ptr(), "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_.ptr(), "numeric_progress", false)
.set_label(ss.str());
} else {
size_t completed, total;
completed = connection_->current();
total = connection_->total();
if(total) {
find_widget<progress_bar>(window_.ptr(), "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_.ptr(), "numeric_progress", false)
.set_label(ss.str());
window_->invalidate_layout();
}
}
}

Expand All @@ -108,7 +75,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 @@ -125,11 +93,6 @@ void network_transmission::post_show(window& /*window*/)
pump_monitor_.window_ = std::nullopt;

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
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
#include "network_asio.hpp"
#include "utils/optional_reference.hpp"

#include <atomic>
#include <future>

namespace gui2::dialogs
{

Expand Down Expand Up @@ -53,19 +50,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&);

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

std::atomic_size_t completed_, total_;

std::atomic_bool stop_;

std::future<void> poller_;
utils::optional_reference<window> window_;
} pump_monitor_;

public:
Expand Down

0 comments on commit 5acc110

Please sign in to comment.