Made the network transmission dialog track upload progress when uploading addons

instead of tracking the progress of downloading the server's responce.
This commit is contained in:
Sergey Popov 2011-08-07 20:17:29 +00:00
parent 6a5a967bb6
commit f7da87291a
5 changed files with 59 additions and 12 deletions

View file

@ -528,7 +528,7 @@ namespace {
LOG_NET << "uploading add-on...\n";
connection.transfer(request, response);
gui2::tnetwork_transmission upload_dialog(connection, _("Sending add-on"), "");
gui2::tnetwork_transmission upload_dialog(connection, _("Sending add-on"), "", true);
result = upload_dialog.show(disp.video());
if(!result) return;

View file

@ -38,9 +38,15 @@ void tnetwork_transmission::pump_monitor::process(events::pump_info&)
if(connection_.done()) {
window_.get().set_retval(twindow::OK);
} else {
if(connection_.bytes_to_read()) {
size_t completed = connection_.bytes_read();
size_t total = connection_.bytes_to_read();
size_t completed, total;
if(track_upload_) {
completed = connection_.bytes_written();
total = connection_.bytes_to_write();
} else {
completed = connection_.bytes_read();
total = connection_.bytes_to_read();
}
if(total) {
find_widget<tprogress_bar>(&(window_.get()), "progress", false)
.set_percentage((completed*100)/total);
@ -60,9 +66,10 @@ void tnetwork_transmission::pump_monitor::process(events::pump_info&)
tnetwork_transmission::tnetwork_transmission(
network_asio::connection& connection
, const std::string& title
, const std::string& subtitle)
, const std::string& subtitle
, bool track_upload)
: connection_(connection)
, pump_monitor(connection)
, pump_monitor(connection, track_upload)
, subtitle_(subtitle)
{
register_label("title", true, title, false);

View file

@ -38,10 +38,12 @@ class tnetwork_transmission : public tdialog
class pump_monitor : public events::pump_monitor
{
network_asio::connection& connection_;
bool track_upload_;
virtual void process(events::pump_info&);
public:
pump_monitor(network_asio::connection& connection)
pump_monitor(network_asio::connection& connection, bool track_upload)
: connection_(connection)
, track_upload_(track_upload)
, window_()
{
}
@ -53,7 +55,8 @@ public:
tnetwork_transmission(
network_asio::connection& connection
, const std::string& title
, const std::string& subtitle);
, const std::string& subtitle
, bool track_upload = false);
void set_subtitle(const std::string&);

View file

@ -33,6 +33,8 @@ connection::connection(const std::string& host, const std::string& service)
, write_buf_()
, read_buf_()
, handshake_response_()
, bytes_to_write_(0)
, bytes_written_(0)
, bytes_to_read_(0)
, bytes_read_(0)
{
@ -107,13 +109,33 @@ void connection::transfer(const config& request, config& response)
std::ostream os(&write_buf_);
write_gz(os, request);
std::size_t size = write_buf_.size();
size = htonl(size);
bytes_to_write_ = write_buf_.size();
bytes_written_ = 0;
std::size_t size = htonl(bytes_to_write_);
boost::asio::write(socket_, boost::asio::buffer(reinterpret_cast<const char*>(&size), 4));
boost::asio::async_write(socket_, write_buf_, boost::bind(&connection::handle_write, this, _1, _2));
boost::asio::async_write(socket_, write_buf_,
boost::bind(&connection::is_write_complete, this, _1, _2),
boost::bind(&connection::handle_write, this, _1, _2)
);
boost::asio::async_read(socket_, read_buf_,
boost::bind(&connection::is_read_complete, this, _1, _2),
boost::bind(&connection::handle_read, this, _1, _2, boost::ref(response)));
boost::bind(&connection::handle_read, this, _1, _2, boost::ref(response))
);
}
std::size_t connection::is_write_complete(
const boost::system::error_code& ec,
std::size_t bytes_transferred
)
{
if(ec)
throw system_error(ec);
bytes_written_ = bytes_transferred;
#if BOOST_VERSION >= 103700
return bytes_to_write_ - bytes_transferred;
#else
return bytes_to_write_ == bytes_transferred;
#endif
}
void connection::handle_write(
@ -159,6 +181,7 @@ void connection::handle_read(
{
std::cout << "Read " << bytes_transferred << " bytes.\n";
bytes_to_read_ = 0;
bytes_to_write_ = 0;
done_ = true;
if(ec && ec != boost::asio::error::eof)
throw system_error(ec);

View file

@ -65,6 +65,10 @@ class connection
boost::uint32_t num;
} handshake_response_;
std::size_t is_write_complete(
const boost::system::error_code& error,
std::size_t bytes_transferred
);
void handle_write(
const boost::system::error_code& ec,
std::size_t bytes_transferred
@ -78,6 +82,8 @@ class connection
std::size_t bytes_transferred,
config& response
);
std::size_t bytes_to_write_;
std::size_t bytes_written_;
std::size_t bytes_to_read_;
std::size_t bytes_read_;
@ -115,6 +121,14 @@ class connection
/** True if connected and no high-level operation is in progress */
bool done() const { return done_; }
std::size_t bytes_to_write() const
{
return bytes_to_write_;
}
std::size_t bytes_written() const
{
return bytes_written_;
}
std::size_t bytes_to_read() const
{
return bytes_to_read_;