diff --git a/src/control.cc b/src/control.cc index 78798021..49be45a2 100644 --- a/src/control.cc +++ b/src/control.cc @@ -87,6 +87,7 @@ Control::initialize() { m_core->get_poll_manager()->get_http_stack()->set_user_agent(std::string(PACKAGE "/" VERSION "/") + torrent::version()); m_core->initialize_second(); + m_core->listen_open(); m_ui->init(this); diff --git a/src/core/download.cc b/src/core/download.cc index 5af33f1d..76217045 100644 --- a/src/core/download.cc +++ b/src/core/download.cc @@ -46,10 +46,29 @@ namespace core { -Download::Download() : +Download::Download(torrent::Download d) : + m_download(d), + m_chunksFailed(0), m_connectionLeech(torrent::Download::CONNECTION_LEECH), m_connectionSeed(torrent::Download::CONNECTION_SEED) { + + m_connTrackerSucceded = m_download.signal_tracker_succeded(sigc::bind(sigc::mem_fun(*this, &Download::receive_tracker_msg), "")); + m_connTrackerFailed = m_download.signal_tracker_failed(sigc::mem_fun(*this, &Download::receive_tracker_msg)); + m_connStorageError = m_download.signal_storage_error(sigc::mem_fun(*this, &Download::receive_storage_error)); + + m_download.signal_chunk_failed(sigc::mem_fun(*this, &Download::receive_chunk_failed)); +} + +Download::~Download() { + if (!m_download.is_valid()) + return; + + m_connTrackerSucceded.disconnect(); + m_connTrackerFailed.disconnect(); + m_connStorageError.disconnect(); + + m_download = torrent::Download(); } void @@ -65,17 +84,6 @@ Download::start() { m_download.start(); } -void -Download::set_download(torrent::Download d) { - m_download = d; - - m_connTrackerSucceded = m_download.signal_tracker_succeded(sigc::bind(sigc::mem_fun(*this, &Download::receive_tracker_msg), "")); - m_connTrackerFailed = m_download.signal_tracker_failed(sigc::mem_fun(*this, &Download::receive_tracker_msg)); - m_connStorageError = m_download.signal_storage_error(sigc::mem_fun(*this, &Download::receive_storage_error)); - - m_download.signal_chunk_failed(sigc::mem_fun(*this, &Download::receive_chunk_failed)); -} - void Download::set_root_directory(const std::string& d) { m_download.set_root_dir(d + @@ -93,18 +101,6 @@ Download::enable_udp_trackers(bool state) { m_download.tracker(i).disable(); } -void -Download::release_download() { - if (!m_download.is_valid()) - return; - - m_connTrackerSucceded.disconnect(); - m_connTrackerFailed.disconnect(); - m_connStorageError.disconnect(); - - m_download = torrent::Download(); -} - void Download::receive_finished() { m_download.set_connection_type(m_connectionSeed); diff --git a/src/core/download.h b/src/core/download.h index 2906e11d..b21683b4 100644 --- a/src/core/download.h +++ b/src/core/download.h @@ -47,17 +47,14 @@ class Download { public: typedef torrent::Download::ConnectionType ConnType; - Download(); - ~Download() { release_download(); } + Download(torrent::Download d); + ~Download(); bool is_open() { return m_download.is_open(); } inline bool is_done(); void start(); - void set_download(torrent::Download d); - void release_download(); - torrent::Download& get_download() { return m_download; } const torrent::Download& get_download() const { return m_download; } std::string get_hash() { return m_download.info_hash(); } diff --git a/src/core/download_list.cc b/src/core/download_list.cc index 933f9ad6..4c4c1292 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -51,8 +51,7 @@ DownloadList::iterator DownloadList::insert(std::istream* str) { torrent::Download d = torrent::download_add(str); - iterator itr = Base::insert(end(), new Download); - (*itr)->set_download(d); + iterator itr = Base::insert(end(), new Download(d)); (*itr)->get_download().signal_download_done(sigc::bind(sigc::mem_fun(*this, &DownloadList::finished), *itr)); m_slotMapInsert.for_each(*itr); @@ -65,7 +64,6 @@ DownloadList::erase(iterator itr) { m_slotMapErase.for_each(*itr); torrent::download_remove((*itr)->get_download()); - (*itr)->release_download(); delete *itr; return Base::erase(itr); diff --git a/src/core/manager.cc b/src/core/manager.cc index 60a55902..d9bbc3ec 100644 --- a/src/core/manager.cc +++ b/src/core/manager.cc @@ -98,7 +98,6 @@ Manager::initialize_second() { m_httpQueue.slot_factory(m_pollManager->get_http_stack()->get_http_factory()); CurlStack::global_init(); - listen_open(); // Register slots to be called when a download is inserted/erased, // opened or closed. diff --git a/src/display/utils.cc b/src/display/utils.cc index 0d23a6ae..eaface6f 100644 --- a/src/display/utils.cc +++ b/src/display/utils.cc @@ -66,21 +66,21 @@ print_hhmmss(char* buf, unsigned int length, time_t t) { if (u == NULL) return "inv_time"; - unsigned int s = snprintf(buf, length, "%2u:%02u:%02u", u->tm_hour, u->tm_min, u->tm_sec); + int s = snprintf(buf, length, "%2u:%02u:%02u", u->tm_hour, u->tm_min, u->tm_sec); - return buf + std::min(s, length); + return buf + std::max(s, 0); } char* print_ddhhmm(char* buf, unsigned int length, time_t t) { - unsigned int s; + int s; if (t / (24 * 3600) < 100) s = snprintf(buf, length, "%2i:%02i:%02i", (int)t / (24 * 3600), ((int)t / 3600) % 24, ((int)t / 60) % 60); else s = snprintf(buf, length, "--:--:--"); - return buf + std::min(s, length); + return buf + std::max(s, 0); } char* @@ -90,37 +90,36 @@ print_ddmmyyyy(char* buf, unsigned int length, time_t t) { if (u == NULL) return "inv_time"; - unsigned int s = snprintf(buf, length, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)); + int s = snprintf(buf, length, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)); - return buf + std::min(s, length); + return buf + std::max(s, 0); } char* print_download_title(char* buf, unsigned int length, core::Download* d) { - return buf + std::max(0, snprintf(buf, length, "%s", - d->get_download().name().c_str())); + return buf + std::max(snprintf(buf, length, "%s", d->get_download().name().c_str()), 0); } char* print_download_info(char* buf, unsigned int length, core::Download* d) { char* last = buf + length; - buf += std::max(0, snprintf(buf, last - buf, "Torrent: ")); + buf += std::max(snprintf(buf, last - buf, "Torrent: "), 0); if (!d->get_download().is_open()) - buf += std::max(0, snprintf(buf, last - buf, "closed ")); + buf += std::max(snprintf(buf, last - buf, "closed "), 0); else if (d->is_done()) - buf += std::max(0, snprintf(buf, last - buf, "done %10.1f MB", - (double)d->get_download().bytes_total() / (double)(1 << 20))); + buf += std::max(snprintf(buf, last - buf, "done %10.1f MB", + (double)d->get_download().bytes_total() / (double)(1 << 20)), 0); else - buf += std::max(0, snprintf(buf, last - buf, "%6.1f / %6.1f MB", - (double)d->get_download().bytes_done() / (double)(1 << 20), - (double)d->get_download().bytes_total() / (double)(1 << 20))); + buf += std::max(snprintf(buf, last - buf, "%6.1f / %6.1f MB", + (double)d->get_download().bytes_done() / (double)(1 << 20), + (double)d->get_download().bytes_total() / (double)(1 << 20)), 0); - buf += std::max(0, snprintf(buf, last - buf, " Rate: %5.1f / %5.1f KB Uploaded: %7.1f MB ", - (double)d->get_download().up_rate()->rate() / (1 << 10), - (double)d->get_download().down_rate()->rate() / (1 << 10), - (double)d->get_download().up_rate()->total() / (1 << 20))); + buf += std::max(snprintf(buf, last - buf, " Rate: %5.1f / %5.1f KB Uploaded: %7.1f MB ", + (double)d->get_download().up_rate()->rate() / (1 << 10), + (double)d->get_download().down_rate()->rate() / (1 << 10), + (double)d->get_download().up_rate()->total() / (1 << 20)), 0); //buf += std::max(0, snprintf(buf, length, " Left: ")); buf = print_download_time_left(buf, length, d); @@ -131,21 +130,21 @@ print_download_info(char* buf, unsigned int length, core::Download* d) { char* print_download_status(char* buf, unsigned int length, core::Download* d) { if (!d->get_download().is_active()) - buf += std::max(0, snprintf(buf, length, "Inactive: ")); + buf += std::max(snprintf(buf, length, "Inactive: "), 0); if (d->get_download().is_hash_checking()) - buf += std::max(0, snprintf(buf, length, "Checking hash [%2i%%]", - (d->get_download().chunks_hashed() * 100) / d->get_download().chunks_total())); + buf += std::max(snprintf(buf, length, "Checking hash [%2i%%]", + (d->get_download().chunks_hashed() * 100) / d->get_download().chunks_total()), 0); else if (d->get_download().is_tracker_busy() && d->get_download().tracker_focus() < d->get_download().size_trackers()) - buf += std::max(0, snprintf(buf, length, "Tracker[%i:%i]: Connecting to %s", - d->get_download().tracker(d->get_download().tracker_focus()).group(), - d->get_download().tracker_focus(), - d->get_download().tracker(d->get_download().tracker_focus()).url().c_str())); + buf += std::max(snprintf(buf, length, "Tracker[%i:%i]: Connecting to %s", + d->get_download().tracker(d->get_download().tracker_focus()).group(), + d->get_download().tracker_focus(), + d->get_download().tracker(d->get_download().tracker_focus()).url().c_str()), 0); else if (!d->get_message().empty()) - buf += std::max(0, snprintf(buf, length, "%s", d->get_message().c_str())); + buf += std::max(snprintf(buf, length, "%s", d->get_message().c_str()), 0); else buf[0] = '\0'; diff --git a/src/option_handler_rules.cc b/src/option_handler_rules.cc index e1717c32..a0e41367 100644 --- a/src/option_handler_rules.cc +++ b/src/option_handler_rules.cc @@ -158,15 +158,14 @@ apply_max_open_sockets(Control* m, int arg) { void apply_bind(Control* m, const std::string& arg) { - bool reopenListen = torrent::listen_port(); - - if (reopenListen) + if (torrent::listen_port() != 0) { torrent::listen_close(); - - torrent::set_bind_address(arg); - - if (reopenListen) + torrent::set_bind_address(arg); m->core()->listen_open(); + + } else { + torrent::set_bind_address(arg); + } } void