diff --git a/docs/manual.rst b/docs/manual.rst index 2be74a88e..e93da86b6 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -2302,7 +2302,7 @@ Its declaration looks like this:: void file_progress(std::vector& fp, int flags = 0); void get_download_queue(std::vector& queue) const; void get_peer_info(std::vector& v) const; - torrent_info const& get_torrent_info() const; + boost::intrusive_ptr torrent_file() const; bool is_valid() const; std::string name() const; @@ -3204,7 +3204,7 @@ Example code to pause and save resume data for all torrents and wait for the ale } torrent_handle h = rd->handle; - std::ofstream out((h.save_path() + "/" + h.get_torrent_info().name() + ".fastresume").c_str() + std::ofstream out((h.save_path() + "/" + h.torrent_file()->name() + ".fastresume").c_str() , std::ios_base::binary); out.unsetf(std::ios_base::skipws); bencode(std::ostream_iterator(out), *rd->resume_data); @@ -3347,18 +3347,18 @@ torrent_handle_ is invalid, it will throw libtorrent_exception_ exception. Each the vector contains information about that particular peer. See peer_info_. -get_torrent_info() ------------------- +torrent_file() +-------------- :: - torrent_info const& get_torrent_info() const; + boost::intrusive_ptr torrent_file() const; -Returns a const reference to the torrent_info_ object associated with this torrent. -This reference is valid as long as the torrent_handle_ is valid, no longer. If the -torrent_handle_ is invalid or if it doesn't have any metadata, libtorrent_exception_ -exception will be thrown. The torrent may be in a state without metadata only if -it was started without a .torrent file, i.e. by using the libtorrent extension of +Returns a pointer to the torrent_info_ object associated with this torrent. The +``torrent_info`` object is a copy of the internal object. If the torrent doesn't +have metadata, the object being returned will not be fully filled in. +The torrent may be in a state without metadata only if +it was started without a .torrent file, e.g. by using the libtorrent extension of just supplying a tracker and info-hash. @@ -3616,8 +3616,7 @@ set to priority 0, i.e. are not downloaded. ``has_metadata`` is true if this torrent has metadata (either it was started from a .torrent file or the metadata has been downloaded). The only scenario where this can be false is when the torrent was started torrent-less (i.e. with just an info-hash and tracker -ip, a magnet link for instance). Note that if the torrent doesn't have metadata, the member -`get_torrent_info()`_ will throw. +ip, a magnet link for instance). ``error`` may be set to an error message describing why the torrent was paused, in case it was paused by an error. If the torrent is not paused or if it's paused but @@ -7171,12 +7170,12 @@ code to do that:: torrent_handle h = alert->handle(); if (h.is_valid()) { - torrent_info const& ti = h.get_torrent_info(); - create_torrent ct(ti); + boost::intrusive_ptr ti = h.torrent_file(); + create_torrent ct(*ti); entry te = ct.generate(); std::vector buffer; bencode(std::back_inserter(buffer), te); - FILE* f = fopen((to_hex(ti.info_hash().to_string()) + ".torrent").c_str(), "w+"); + FILE* f = fopen((to_hex(ti->info_hash().to_string()) + ".torrent").c_str(), "w+"); if (f) { fwrite(&buffer[0], 1, buffer.size(), f); fclose(f); @@ -7594,7 +7593,7 @@ Examples usage:: // write fast resume data // ... - std::cout << a.handle.get_torrent_info().name() << "completed" + std::cout << a.handle.torrent_file()->name() << "completed" << std::endl; } }; diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 0868ae12c..574fdfe0d 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -884,6 +884,8 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a } #endif + boost::intrusive_ptr ti; + if (metadata_received_alert* p = alert_cast(a)) { // if we have a monitor dir, save the .torrent file we just received in it @@ -891,12 +893,12 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a // to keep the scan dir logic in sync so it's not removed, or added twice torrent_handle h = p->handle; if (h.is_valid()) { - torrent_info const& ti = h.get_torrent_info(); - create_torrent ct(ti); + if (!ti) ti = h.torrent_file(); + create_torrent ct(*ti); entry te = ct.generate(); std::vector buffer; bencode(std::back_inserter(buffer), te); - std::string filename = ti.name() + "." + to_hex(ti.info_hash().to_string()) + ".torrent"; + std::string filename = ti->name() + "." + to_hex(ti->info_hash().to_string()) + ".torrent"; filename = combine_path(monitor_dir, filename); save_file(filename, buffer); @@ -930,7 +932,9 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a h.set_max_uploads(-1); h.set_upload_limit(torrent_upload_limit); h.set_download_limit(torrent_download_limit); +#ifndef TORRENT_NO_DEPRECATE h.use_interface(outgoing_interface.c_str()); +#endif #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES h.resolve_countries(true); #endif @@ -2273,15 +2277,15 @@ int main(int argc, char* argv[]) { std::vector file_progress; h.file_progress(file_progress); - torrent_info const& info = h.get_torrent_info(); - for (int i = 0; i < info.num_files(); ++i) + boost::intrusive_ptr ti = h.torrent_file(); + for (int i = 0; i < ti->num_files(); ++i) { - bool pad_file = info.file_at(i).pad_file; + bool pad_file = ti->file_at(i).pad_file; if (!show_pad_files && pad_file) continue; - int progress = info.file_at(i).size > 0 - ?file_progress[i] * 1000 / info.file_at(i).size:1000; + int progress = ti->file_at(i).size > 0 + ?file_progress[i] * 1000 / ti->file_at(i).size:1000; - char const* color = (file_progress[i] == info.file_at(i).size) + char const* color = (file_progress[i] == ti->file_at(i).size) ?"32":"33"; snprintf(str, sizeof(str), "%s %s %-5.2f%% %s %s%s\n", @@ -2289,7 +2293,7 @@ int main(int argc, char* argv[]) , pad_file?esc("34"):"" , progress / 10.f , add_suffix(file_progress[i]).c_str() - , filename(info.files().file_path(info.file_at(i))).c_str() + , ti->files().file_name(i).c_str() , pad_file?esc("0"):""); out += str; } diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 695fee080..c5288c7b3 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -721,6 +721,8 @@ namespace libtorrent torrent_info const& torrent_file() const { return *m_torrent_file; } + boost::intrusive_ptr get_torrent_copy(); + std::string const& uuid() const { return m_uuid; } void set_uuid(std::string const& s) { m_uuid = s; } std::string const& url() const { return m_url; } diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 0b523c427..b93129a34 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -226,7 +226,7 @@ namespace libtorrent #endif bool set_metadata(char const* metadata, int size) const; - const torrent_info& get_torrent_info() const; + bool is_valid() const; enum pause_flags_t { graceful_pause = 1 }; @@ -264,12 +264,16 @@ namespace libtorrent storage_interface* get_storage_impl() const; - // all these are deprecated, use piece - // priority functions instead + boost::intrusive_ptr torrent_file() const; + +#ifndef TORRENT_NO_DEPRECATE // ================ start deprecation ============ -#ifndef TORRENT_NO_DEPRECATE + // use torrent_file() instead + TORRENT_DEPRECATED_PREFIX + const torrent_info& get_torrent_info() const TORRENT_DEPRECATED; + // deprecated in 0.16, feature will be removed TORRENT_DEPRECATED_PREFIX int get_peer_upload_limit(tcp::endpoint ip) const TORRENT_DEPRECATED; @@ -301,6 +305,8 @@ namespace libtorrent bool super_seeding() const TORRENT_DEPRECATED; // deprecated in 0.13 + // all these are deprecated, use piece + // priority functions instead // marks the piece with the given index as filtered // it will not be downloaded TORRENT_DEPRECATED_PREFIX @@ -316,6 +322,14 @@ namespace libtorrent TORRENT_DEPRECATED_PREFIX void filter_files(std::vector const& files) const TORRENT_DEPRECATED; + TORRENT_DEPRECATED_PREFIX + void use_interface(const char* net_interface) const TORRENT_DEPRECATED; + + // deprecated in 0.14 + // use save_resume_data() instead. It is async. and + // will return the resume data in an alert + TORRENT_DEPRECATED_PREFIX + entry write_resume_data() const TORRENT_DEPRECATED; // ================ end deprecation ============ #endif @@ -335,18 +349,6 @@ namespace libtorrent void prioritize_files(std::vector const& files) const; std::vector file_priorities() const; - // set the interface to bind outgoing connections - // to. - void use_interface(const char* net_interface) const; - -#ifndef TORRENT_NO_DEPRECATE - // deprecated in 0.14 - // use save_resume_data() instead. It is async. and - // will return the resume data in an alert - TORRENT_DEPRECATED_PREFIX - entry write_resume_data() const TORRENT_DEPRECATED; -#endif - // forces this torrent to reannounce // (make a rerequest from the tracker) void force_reannounce() const; diff --git a/src/torrent.cpp b/src/torrent.cpp index 1b8c5135f..7ddfb9fd3 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -5185,6 +5185,12 @@ namespace libtorrent } } } + + boost::intrusive_ptr torrent::get_torrent_copy() + { + // copy the torrent_info object + return boost::intrusive_ptr(new torrent_info(*m_torrent_file)); + } void torrent::write_resume_data(entry& ret) const { @@ -8197,7 +8203,7 @@ namespace libtorrent for (int i = 0; i < m_torrent_file->num_files(); ++i) { peer_request ret = m_torrent_file->files().map_file(i, 0, 0); - size_type size = m_torrent_file->files().at(i).size; + size_type size = m_torrent_file->files().file_size(i); // zero sized files are considered // 100% done all the time diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 4e8c0dca6..1b616b3f3 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -825,6 +825,23 @@ namespace libtorrent return r; } + bool torrent_handle::is_valid() const + { + INVARIANT_CHECK; + return !m_torrent.expired(); + } + + boost::intrusive_ptr torrent_handle::torrent_file() const + { + INVARIANT_CHECK; + TORRENT_SYNC_CALL_RET(boost::intrusive_ptr, boost::intrusive_ptr(), get_torrent_copy); + return r; + } + +#ifndef TORRENT_NO_DEPRECATE + // this function should either be removed, or return + // reference counted handle to the torrent_info which + // forces the torrent to stay loaded while the client holds it torrent_info const& torrent_handle::get_torrent_info() const { INVARIANT_CHECK; @@ -838,7 +855,6 @@ namespace libtorrent #else throw_invalid_handle(); #endif -// mutex::scoped_lock l(t->session().m_mutex); if (!t->valid_metadata()) #ifdef BOOST_NO_EXCEPTIONS return empty; @@ -848,13 +864,6 @@ namespace libtorrent return t->torrent_file(); } - bool torrent_handle::is_valid() const - { - INVARIANT_CHECK; - return !m_torrent.expired(); - } - -#ifndef TORRENT_NO_DEPRECATE entry torrent_handle::write_resume_data() const { INVARIANT_CHECK;