diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 99bbefb82..5ee5ddb03 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -128,6 +128,8 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS void add_extension(boost::shared_ptr); + void add_extension(boost::function(torrent*, void*)> const& ext + , void* userdata); #endif // this is called when the torrent has metadata. diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 8ae2f7f41..7ddb218a6 100755 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -64,6 +64,8 @@ namespace libtorrent struct checker_impl; } + struct torrent_plugin; + struct TORRENT_EXPORT duplicate_torrent: std::exception { virtual const char* what() const throw() @@ -279,6 +281,11 @@ namespace libtorrent void remove_url_seed(std::string const& url) const; std::set url_seeds() const; +#ifndef TORRENT_DISABLE_EXTENSIONS + void add_extension(boost::function(torrent*, void*)> const& ext + , void* userdata = 0); +#endif + bool has_metadata() const; const torrent_info& get_torrent_info() const; bool is_valid() const; diff --git a/src/torrent.cpp b/src/torrent.cpp index c38111908..84f30aa2d 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -346,10 +346,34 @@ namespace libtorrent } #ifndef TORRENT_DISABLE_EXTENSIONS + void torrent::add_extension(boost::shared_ptr ext) { m_extensions.push_back(ext); } + + void torrent::add_extension(boost::function(torrent*, void*)> const& ext + , void* userdata) + { + boost::shared_ptr tp(ext(this, userdata)); + if (!tp) return; + + add_extension(tp); + + for (peer_iterator i = m_connections.begin(); + i != m_connections.end(); ++i) + { + peer_connection* p = *i; + boost::shared_ptr pp(tp->new_connection(p)); + if (pp) p->add_extension(pp); + } + + // if files are checked for this torrent, call the extension + // to let it initialize itself + if (m_connections_initialized) + tp->on_files_checked(); + } + #endif // this may not be called from a constructor because of the call to diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index dd6dd3c21..b19e05bb4 100755 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -247,6 +247,20 @@ namespace libtorrent find_torrent(m_ses, m_chk, m_info_hash)->move_storage(save_path); } + void torrent_handle::add_extension( + boost::function(torrent*, void*)> const& ext + , void* userdata) + { + INVARIANT_CHECK; + + if (m_ses == 0) throw_invalid_handle(); + TORRENT_ASSERT(m_chk); + + session_impl::mutex_t::scoped_lock l1(m_ses->m_mutex); + mutex::scoped_lock l2(m_chk->m_mutex); + return find_torrent(m_ses, m_chk, m_info_hash)->add_extension(ext, userdata); + } + bool torrent_handle::has_metadata() const { INVARIANT_CHECK;