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::clear_error() const
325 TORRENT_FORWARD(clear_error());
328 void torrent_handle::set_tracker_login(std::string
const& name
329 , std::string
const& password
) const
332 TORRENT_FORWARD(set_tracker_login(name
, password
));
335 void torrent_handle::file_progress(std::vector
<float>& progress
) const
338 TORRENT_FORWARD(file_progress(progress
));
341 void torrent_handle::file_progress(std::vector
<size_type
>& progress
) const
344 TORRENT_FORWARD(file_progress(progress
));
347 torrent_status
torrent_handle::status() const
350 TORRENT_FORWARD_RETURN(status(), torrent_status());
353 void torrent_handle::set_sequential_download(bool sd
) const
356 TORRENT_FORWARD(set_sequential_download(sd
));
359 bool torrent_handle::is_sequential_download() const
362 TORRENT_FORWARD_RETURN(is_sequential_download(), false);
365 std::string
torrent_handle::name() const
368 TORRENT_FORWARD_RETURN(name(), "");
371 void torrent_handle::piece_availability(std::vector
<int>& avail
) const
374 TORRENT_FORWARD(piece_availability(avail
));
377 void torrent_handle::piece_priority(int index
, int priority
) const
380 TORRENT_FORWARD(set_piece_priority(index
, priority
));
383 int torrent_handle::piece_priority(int index
) const
386 TORRENT_FORWARD_RETURN(piece_priority(index
), 0);
389 void torrent_handle::prioritize_pieces(std::vector
<int> const& pieces
) const
392 TORRENT_FORWARD(prioritize_pieces(pieces
));
395 std::vector
<int> torrent_handle::piece_priorities() const
398 std::vector
<int> ret
;
399 TORRENT_FORWARD_RETURN2(piece_priorities(ret
), ret
);
403 void torrent_handle::file_priority(int index
, int priority
) const
406 TORRENT_FORWARD(set_file_priority(index
, priority
));
409 int torrent_handle::file_priority(int index
) const
412 TORRENT_FORWARD_RETURN(file_priority(index
), 0);
415 void torrent_handle::prioritize_files(std::vector
<int> const& files
) const
418 TORRENT_FORWARD(prioritize_files(files
));
421 std::vector
<int> torrent_handle::file_priorities() const
424 std::vector
<int> ret
;
425 TORRENT_FORWARD_RETURN2(file_priorities(ret
), ret
);
429 // ============ start deprecation ===============
431 void torrent_handle::filter_piece(int index
, bool filter
) const
434 TORRENT_FORWARD(filter_piece(index
, filter
));
437 void torrent_handle::filter_pieces(std::vector
<bool> const& pieces
) const
440 TORRENT_FORWARD(filter_pieces(pieces
));
443 bool torrent_handle::is_piece_filtered(int index
) const
446 TORRENT_FORWARD_RETURN(is_piece_filtered(index
), false);
449 std::vector
<bool> torrent_handle::filtered_pieces() const
452 std::vector
<bool> ret
;
453 TORRENT_FORWARD_RETURN2(filtered_pieces(ret
), ret
);
457 void torrent_handle::filter_files(std::vector
<bool> const& files
) const
460 TORRENT_FORWARD(filter_files(files
));
463 // ============ end deprecation ===============
466 std::vector
<announce_entry
> const& torrent_handle::trackers() const
469 const static std::vector
<announce_entry
> empty
;
470 TORRENT_FORWARD_RETURN(trackers(), empty
);
473 void torrent_handle::add_url_seed(std::string
const& url
) const
476 TORRENT_FORWARD(add_url_seed(url
));
479 void torrent_handle::remove_url_seed(std::string
const& url
) const
482 TORRENT_FORWARD(remove_url_seed(url
));
485 std::set
<std::string
> torrent_handle::url_seeds() const
488 const static std::set
<std::string
> empty
;
489 TORRENT_FORWARD_RETURN(url_seeds(), empty
);
492 void torrent_handle::replace_trackers(
493 std::vector
<announce_entry
> const& urls
) const
496 TORRENT_FORWARD(replace_trackers(urls
));
499 torrent_info
const& torrent_handle::get_torrent_info() const
502 #ifdef BOOST_NO_EXCEPTIONS
503 const static torrent_info empty
;
505 boost::shared_ptr
<torrent
> t
= m_torrent
.lock();
507 #ifdef BOOST_NO_EXCEPTIONS
510 throw_invalid_handle();
512 session_impl::mutex_t::scoped_lock
l(t
->session().m_mutex
);
513 if (!t
->valid_metadata())
514 #ifdef BOOST_NO_EXCEPTIONS
517 throw_invalid_handle();
519 return t
->torrent_file();
522 bool torrent_handle::is_valid() const
525 return !m_torrent
.expired();
528 entry
torrent_handle::write_resume_data() const
532 entry
ret(entry::dictionary_t
);
533 TORRENT_FORWARD(write_resume_data(ret
));
534 t
->filesystem().write_resume_data(ret
);
540 fs::path
torrent_handle::save_path() const
543 TORRENT_FORWARD_RETURN(save_path(), fs::path());
546 void torrent_handle::connect_peer(tcp::endpoint
const& adr
, int source
) const
550 boost::shared_ptr
<torrent
> t
= m_torrent
.lock();
552 #ifdef BOOST_NO_EXCEPTIONS
555 throw_invalid_handle();
557 session_impl::mutex_t::scoped_lock
l(t
->session().m_mutex
);
560 std::fill(id
.begin(), id
.end(), 0);
561 t
->get_policy().peer_from_tracker(adr
, id
, source
, 0);
564 void torrent_handle::force_reannounce(
565 boost::posix_time::time_duration duration
) const
568 TORRENT_FORWARD(force_tracker_request(time_now() + seconds(duration
.total_seconds())));
571 void torrent_handle::force_reannounce() const
574 TORRENT_FORWARD(force_tracker_request());
577 void torrent_handle::scrape_tracker() const
580 TORRENT_FORWARD(scrape_tracker());
583 void torrent_handle::set_ratio(float ratio
) const
587 TORRENT_ASSERT(ratio
>= 0.f
);
588 if (ratio
< 1.f
&& ratio
> 0.f
)
590 TORRENT_FORWARD(set_ratio(ratio
));
593 #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
594 void torrent_handle::resolve_countries(bool r
)
597 TORRENT_FORWARD(resolve_countries(r
));
600 bool torrent_handle::resolve_countries() const
603 TORRENT_FORWARD_RETURN(resolving_countries(), false);
607 void torrent_handle::get_full_peer_list(std::vector
<peer_list_entry
>& v
) const
610 TORRENT_FORWARD(get_full_peer_list(v
));
613 void torrent_handle::get_peer_info(std::vector
<peer_info
>& v
) const
616 TORRENT_FORWARD(get_peer_info(v
));
619 void torrent_handle::get_download_queue(std::vector
<partial_piece_info
>& queue
) const
622 TORRENT_FORWARD(get_download_queue(queue
));