3 Copyright (c) 2003, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/pch.hpp"
46 #pragma warning(push, 1)
49 #include <boost/lexical_cast.hpp>
50 #include <boost/filesystem/convenience.hpp>
51 #include <boost/optional.hpp>
52 #include <boost/bind.hpp>
58 #include "libtorrent/peer_id.hpp"
59 #include "libtorrent/bt_peer_connection.hpp"
60 #include "libtorrent/torrent_info.hpp"
61 #include "libtorrent/tracker_manager.hpp"
62 #include "libtorrent/bencode.hpp"
63 #include "libtorrent/hasher.hpp"
64 #include "libtorrent/entry.hpp"
65 #include "libtorrent/session.hpp"
66 #include "libtorrent/aux_/session_impl.hpp"
67 #include "libtorrent/invariant_check.hpp"
69 #if defined(_MSC_VER) && _MSC_VER < 1300
79 using libtorrent::aux::session_impl
;
81 #ifdef BOOST_NO_EXCEPTIONS
83 #define TORRENT_FORWARD(call) \
84 boost::shared_ptr<torrent> t = m_torrent.lock(); \
86 session_impl::mutex_t::scoped_lock l(t->session().m_mutex); \
89 #define TORRENT_FORWARD_RETURN(call, def) \
90 boost::shared_ptr<torrent> t = m_torrent.lock(); \
92 session_impl::mutex_t::scoped_lock l(t->session().m_mutex); \
95 #define TORRENT_FORWARD_RETURN2(call, def) \
96 boost::shared_ptr<torrent> t = m_torrent.lock(); \
98 session_impl::mutex_t::scoped_lock l(t->session().m_mutex); \
103 #define TORRENT_FORWARD(call) \
104 boost::shared_ptr<torrent> t = m_torrent.lock(); \
105 if (!t) throw_invalid_handle(); \
106 session_impl::mutex_t::scoped_lock l(t->session().m_mutex); \
109 #define TORRENT_FORWARD_RETURN(call, def) \
110 boost::shared_ptr<torrent> t = m_torrent.lock(); \
111 if (!t) throw_invalid_handle(); \
112 session_impl::mutex_t::scoped_lock l(t->session().m_mutex); \
115 #define TORRENT_FORWARD_RETURN2(call, def) \
116 boost::shared_ptr<torrent> t = m_torrent.lock(); \
117 if (!t) throw_invalid_handle(); \
118 session_impl::mutex_t::scoped_lock l(t->session().m_mutex); \
125 namespace fs
= boost::filesystem
;
129 #ifndef BOOST_NO_EXCEPTIONS
130 void throw_invalid_handle()
132 throw invalid_handle();
139 void torrent_handle::check_invariant() const
144 sha1_hash
torrent_handle::info_hash() const
147 const static sha1_hash empty
;
148 TORRENT_FORWARD_RETURN(torrent_file().info_hash(), empty
);
151 void torrent_handle::set_max_uploads(int max_uploads
) const
154 TORRENT_ASSERT(max_uploads
>= 2 || max_uploads
== -1);
155 TORRENT_FORWARD(set_max_uploads(max_uploads
));
158 void torrent_handle::use_interface(const char* net_interface
) const
161 TORRENT_FORWARD(use_interface(net_interface
));
164 void torrent_handle::set_max_connections(int max_connections
) const
167 TORRENT_ASSERT(max_connections
>= 2 || max_connections
== -1);
168 TORRENT_FORWARD(set_max_connections(max_connections
));
171 void torrent_handle::set_peer_upload_limit(tcp::endpoint ip
, int limit
) const
174 TORRENT_ASSERT(limit
>= -1);
175 TORRENT_FORWARD(set_peer_upload_limit(ip
, limit
));
178 void torrent_handle::set_peer_download_limit(tcp::endpoint ip
, int limit
) const
181 TORRENT_ASSERT(limit
>= -1);
182 TORRENT_FORWARD(set_peer_download_limit(ip
, limit
));
185 void torrent_handle::set_upload_limit(int limit
) const
188 TORRENT_ASSERT(limit
>= -1);
189 TORRENT_FORWARD(set_upload_limit(limit
));
192 int torrent_handle::upload_limit() const
195 TORRENT_FORWARD_RETURN(upload_limit(), 0);
198 void torrent_handle::set_download_limit(int limit
) const
201 TORRENT_ASSERT(limit
>= -1);
202 TORRENT_FORWARD(set_download_limit(limit
));
205 int torrent_handle::download_limit() const
208 TORRENT_FORWARD_RETURN(download_limit(), 0);
211 void torrent_handle::move_storage(
212 fs::path
const& save_path
) const
215 TORRENT_FORWARD(move_storage(save_path
));
218 void torrent_handle::rename_file(int index
, fs::path
const& new_name
) const
221 TORRENT_FORWARD(rename_file(index
, new_name
.string()));
224 void torrent_handle::add_extension(
225 boost::function
<boost::shared_ptr
<torrent_plugin
>(torrent
*, void*)> const& ext
229 TORRENT_FORWARD(add_extension(ext
, userdata
));
232 bool torrent_handle::has_metadata() const
235 TORRENT_FORWARD_RETURN(valid_metadata(), false);
238 bool torrent_handle::is_seed() const
241 TORRENT_FORWARD_RETURN(is_seed(), false);
244 bool torrent_handle::is_finished() const
247 TORRENT_FORWARD_RETURN(is_finished(), false);
250 bool torrent_handle::is_paused() const
253 TORRENT_FORWARD_RETURN(is_paused(), false);
256 void torrent_handle::pause() const
259 TORRENT_FORWARD(pause());
262 void torrent_handle::save_resume_data() const
265 TORRENT_FORWARD(save_resume_data());
268 void torrent_handle::force_recheck() const
271 TORRENT_FORWARD(force_recheck());
274 void torrent_handle::resume() const
277 TORRENT_FORWARD(resume());
280 bool torrent_handle::is_auto_managed() const
283 TORRENT_FORWARD_RETURN(is_auto_managed(), true);
286 void torrent_handle::auto_managed(bool m
) const
289 TORRENT_FORWARD(auto_managed(m
));
292 int torrent_handle::queue_position() const
295 TORRENT_FORWARD_RETURN(queue_position(), -1);
298 void torrent_handle::queue_position_up() const
301 TORRENT_FORWARD(set_queue_position(t
->queue_position() - 1));
304 void torrent_handle::queue_position_down() const
307 TORRENT_FORWARD(set_queue_position(t
->queue_position() + 1));
310 void torrent_handle::queue_position_top() const
313 TORRENT_FORWARD(set_queue_position(0));
316 void torrent_handle::queue_position_bottom() const
319 TORRENT_FORWARD(set_queue_position((std::numeric_limits
<int>::max
)()));
322 void torrent_handle::set_tracker_login(std::string
const& name
323 , std::string
const& password
) const
326 TORRENT_FORWARD(set_tracker_login(name
, password
));
329 void torrent_handle::file_progress(std::vector
<float>& progress
) const
332 TORRENT_FORWARD(file_progress(progress
));
335 void torrent_handle::file_progress(std::vector
<size_type
>& progress
) const
338 TORRENT_FORWARD(file_progress(progress
));
341 torrent_status
torrent_handle::status() const
344 TORRENT_FORWARD_RETURN(status(), torrent_status());
347 void torrent_handle::set_sequential_download(bool sd
) const
350 TORRENT_FORWARD(set_sequential_download(sd
));
353 bool torrent_handle::is_sequential_download() const
356 TORRENT_FORWARD_RETURN(is_sequential_download(), false);
359 std::string
torrent_handle::name() const
362 TORRENT_FORWARD_RETURN(name(), "");
365 void torrent_handle::piece_availability(std::vector
<int>& avail
) const
368 TORRENT_FORWARD(piece_availability(avail
));
371 void torrent_handle::piece_priority(int index
, int priority
) const
374 TORRENT_FORWARD(set_piece_priority(index
, priority
));
377 int torrent_handle::piece_priority(int index
) const
380 TORRENT_FORWARD_RETURN(piece_priority(index
), 0);
383 void torrent_handle::prioritize_pieces(std::vector
<int> const& pieces
) const
386 TORRENT_FORWARD(prioritize_pieces(pieces
));
389 std::vector
<int> torrent_handle::piece_priorities() const
392 std::vector
<int> ret
;
393 TORRENT_FORWARD_RETURN2(piece_priorities(ret
), ret
);
397 void torrent_handle::prioritize_files(std::vector
<int> const& files
) const
400 TORRENT_FORWARD(prioritize_files(files
));
403 // ============ start deprecation ===============
405 void torrent_handle::filter_piece(int index
, bool filter
) const
408 TORRENT_FORWARD(filter_piece(index
, filter
));
411 void torrent_handle::filter_pieces(std::vector
<bool> const& pieces
) const
414 TORRENT_FORWARD(filter_pieces(pieces
));
417 bool torrent_handle::is_piece_filtered(int index
) const
420 TORRENT_FORWARD_RETURN(is_piece_filtered(index
), false);
423 std::vector
<bool> torrent_handle::filtered_pieces() const
426 std::vector
<bool> ret
;
427 TORRENT_FORWARD_RETURN2(filtered_pieces(ret
), ret
);
431 void torrent_handle::filter_files(std::vector
<bool> const& files
) const
434 TORRENT_FORWARD(filter_files(files
));
437 // ============ end deprecation ===============
440 std::vector
<announce_entry
> const& torrent_handle::trackers() const
443 const static std::vector
<announce_entry
> empty
;
444 TORRENT_FORWARD_RETURN(trackers(), empty
);
447 void torrent_handle::add_url_seed(std::string
const& url
) const
450 TORRENT_FORWARD(add_url_seed(url
));
453 void torrent_handle::remove_url_seed(std::string
const& url
) const
456 TORRENT_FORWARD(remove_url_seed(url
));
459 std::set
<std::string
> torrent_handle::url_seeds() const
462 const static std::set
<std::string
> empty
;
463 TORRENT_FORWARD_RETURN(url_seeds(), empty
);
466 void torrent_handle::replace_trackers(
467 std::vector
<announce_entry
> const& urls
) const
470 TORRENT_FORWARD(replace_trackers(urls
));
473 torrent_info
const& torrent_handle::get_torrent_info() const
476 #ifdef BOOST_NO_EXCEPTIONS
477 const static torrent_info empty
;
479 boost::shared_ptr
<torrent
> t
= m_torrent
.lock();
481 #ifdef BOOST_NO_EXCEPTIONS
484 throw_invalid_handle();
486 session_impl::mutex_t::scoped_lock
l(t
->session().m_mutex
);
487 if (!t
->valid_metadata())
488 #ifdef BOOST_NO_EXCEPTIONS
491 throw_invalid_handle();
493 return t
->torrent_file();
496 bool torrent_handle::is_valid() const
499 return !m_torrent
.expired();
502 entry
torrent_handle::write_resume_data() const
506 entry
ret(entry::dictionary_t
);
507 TORRENT_FORWARD(write_resume_data(ret
));
508 t
->filesystem().write_resume_data(ret
);
514 fs::path
torrent_handle::save_path() const
517 TORRENT_FORWARD_RETURN(save_path(), fs::path());
520 void torrent_handle::connect_peer(tcp::endpoint
const& adr
, int source
) const
524 boost::shared_ptr
<torrent
> t
= m_torrent
.lock();
526 #ifdef BOOST_NO_EXCEPTIONS
529 throw_invalid_handle();
531 session_impl::mutex_t::scoped_lock
l(t
->session().m_mutex
);
534 std::fill(id
.begin(), id
.end(), 0);
535 t
->get_policy().peer_from_tracker(adr
, id
, source
, 0);
538 void torrent_handle::force_reannounce(
539 boost::posix_time::time_duration duration
) const
542 TORRENT_FORWARD(force_tracker_request(time_now() + seconds(duration
.total_seconds())));
545 void torrent_handle::force_reannounce() const
548 TORRENT_FORWARD(force_tracker_request());
551 void torrent_handle::scrape_tracker() const
554 TORRENT_FORWARD(scrape_tracker());
557 void torrent_handle::set_ratio(float ratio
) const
561 TORRENT_ASSERT(ratio
>= 0.f
);
562 if (ratio
< 1.f
&& ratio
> 0.f
)
564 TORRENT_FORWARD(set_ratio(ratio
));
567 #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
568 void torrent_handle::resolve_countries(bool r
)
571 TORRENT_FORWARD(resolve_countries(r
));
574 bool torrent_handle::resolve_countries() const
577 TORRENT_FORWARD_RETURN(resolving_countries(), false);
581 void torrent_handle::get_full_peer_list(std::vector
<peer_list_entry
>& v
) const
584 TORRENT_FORWARD(get_full_peer_list(v
));
587 void torrent_handle::get_peer_info(std::vector
<peer_info
>& v
) const
590 TORRENT_FORWARD(get_peer_info(v
));
593 void torrent_handle::get_download_queue(std::vector
<partial_piece_info
>& queue
) const
596 TORRENT_FORWARD(get_download_queue(queue
));