From ef084487f47e59e932d636a5068afdfe9e19449a Mon Sep 17 00:00:00 2001 From: miguel Date: Tue, 3 Sep 2013 07:08:07 -0300 Subject: [PATCH] fix torrent/swarm path and add resume file --- libtorrent/src/storage.cpp | 2 +- libtorrent/src/torrent.cpp | 2 + src/twister.cpp | 96 ++++++++++++++++++++++++++++---------- 3 files changed, 75 insertions(+), 25 deletions(-) diff --git a/libtorrent/src/storage.cpp b/libtorrent/src/storage.cpp index fa4e1fc8..ec52642e 100644 --- a/libtorrent/src/storage.cpp +++ b/libtorrent/src/storage.cpp @@ -692,7 +692,7 @@ namespace libtorrent : m_info(info) , m_files(m_info->files()) , m_storage(sc(m_info->orig_files(), &m_info->files() != &m_info->orig_files() - ? &m_info->files() : 0, save_path + to_hex(m_info->info_hash().to_string()), fp, file_prio)) + ? &m_info->files() : 0, combine_path(save_path,to_hex(m_info->info_hash().to_string())), fp, file_prio)) , m_storage_mode(sm) , m_save_path(complete(save_path)) , m_state(state_none) diff --git a/libtorrent/src/torrent.cpp b/libtorrent/src/torrent.cpp index 95ad7150..4e2bd7d8 100644 --- a/libtorrent/src/torrent.cpp +++ b/libtorrent/src/torrent.cpp @@ -8422,9 +8422,11 @@ namespace libtorrent st->is_finished = is_finished(); st->super_seeding = m_super_seeding; st->has_metadata = valid_metadata(); + /* [MF] bytes_done(*st, flags & torrent_handle::query_accurate_download_counters); TORRENT_ASSERT(st->total_wanted_done >= 0); TORRENT_ASSERT(st->total_done >= st->total_wanted_done); + */ // payload transfer st->total_payload_download = m_stat.total_payload_download(); diff --git a/src/twister.cpp b/src/twister.cpp index 9d0db494..bce2703e 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -31,6 +31,7 @@ twister::twister() using namespace libtorrent; static session *ses = NULL; +static int num_outstanding_resume_data; static CCriticalSection cs_dhtgetMap; static map m_dhtgetMap; @@ -49,22 +50,6 @@ sha1_hash dhtTargetHash(std::string const &username, std::string const &resource return hasher(buf.data(), buf.size()).final(); } -torrent_handle startTorrentUser(std::string const &username) -{ - if( !m_userTorrent.count(username) ) { - sha1_hash ih = dhtTargetHash(username, "tracker", "m"); - - printf("adding torrent for [%s,tracker]\n", username.c_str()); - add_torrent_params tparams; - tparams.info_hash = ih; - tparams.name = username; - boost::filesystem::path torrentPath = GetDataDir() / "swarm" / ""; - tparams.save_path= torrentPath.string(); - m_userTorrent[username] = ses->add_torrent(tparams); - } - return m_userTorrent[username]; -} - int load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit = 8000000) { ec.clear(); @@ -142,6 +127,28 @@ int save_file(std::string const& filename, std::vector& v) return 0; } +torrent_handle startTorrentUser(std::string const &username) +{ + if( !m_userTorrent.count(username) ) { + sha1_hash ih = dhtTargetHash(username, "tracker", "m"); + + printf("adding torrent for [%s,tracker]\n", username.c_str()); + add_torrent_params tparams; + tparams.info_hash = ih; + tparams.name = username; + boost::filesystem::path torrentPath = GetDataDir() / "swarm"; + tparams.save_path= torrentPath.string(); + + error_code ec; + create_directory(tparams.save_path, ec); + + std::string filename = combine_path(tparams.save_path, to_hex(ih.to_string()) + ".resume"); + load_file(filename.c_str(), tparams.resume_data, ec); + + m_userTorrent[username] = ses->add_torrent(tparams); + } + return m_userTorrent[username]; +} void ThreadWaitExtIP() { @@ -371,18 +378,22 @@ void ThreadSessionAlerts() continue; } - /* - save_resume_data_alert const* rd = alert_cast(*i); - if (rd) { - if (!rd->resume_data) continue; + save_resume_data_alert const* rda = alert_cast(*i); + if (rda) { + num_outstanding_resume_data--; + if (!rda->resume_data) continue; - torrent_handle h = rd->handle; + torrent_handle h = rda->handle; torrent_status st = h.status(torrent_handle::query_save_path); std::vector out; - bencode(std::back_inserter(out), *rd->resume_data); - save_file(combine_path(st.save_path, combine_path(".resume", to_hex(st.info_hash.to_string()) + ".resume")), out); + bencode(std::back_inserter(out), *rda->resume_data); + save_file(combine_path(st.save_path, to_hex(st.info_hash.to_string()) + ".resume"), out); + } + + if (alert_cast(*i)) + { + --num_outstanding_resume_data; } - */ } } } @@ -426,11 +437,48 @@ void startSessionTorrent(boost::thread_group& threadGroup) encryptDecryptTest(); } +bool yes(libtorrent::torrent_status const&) +{ return true; } + void stopSessionTorrent() { if( ses ){ ses->pause(); + printf("saving resume data\n"); + std::vector temp; + ses->get_torrent_status(&temp, &yes, 0); + for (std::vector::iterator i = temp.begin(); + i != temp.end(); ++i) + { + torrent_status& st = *i; + if (!st.handle.is_valid()) + { + printf(" skipping, invalid handle\n"); + continue; + } + if (!st.has_metadata) + { + printf(" skipping %s, no metadata\n", st.name.c_str()); + continue; + } + if (!st.need_save_resume) + { + printf(" skipping %s, resume file up-to-date\n", st.name.c_str()); + continue; + } + + // save_resume_data will generate an alert when it's done + st.handle.save_resume_data(); + ++num_outstanding_resume_data; + printf("\r%d ", num_outstanding_resume_data); + } + printf("\nwaiting for resume data [%d]\n", num_outstanding_resume_data); + while (num_outstanding_resume_data > 0) + { + MilliSleep(100); + } + printf("\nsaving session state\n"); entry session_state;