premiere-libtorrent/src/torrent_handle.cpp

742 lines
18 KiB
C++
Raw Normal View History

2003-10-30 00:28:09 +01:00
/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/pch.hpp"
2003-10-30 00:28:09 +01:00
#include <ctime>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <set>
#include <cctype>
#include <algorithm>
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
2003-10-30 00:28:09 +01:00
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/convenience.hpp>
2003-12-09 19:09:34 +01:00
#include <boost/optional.hpp>
2004-03-23 23:58:18 +01:00
#include <boost/bind.hpp>
2003-10-30 00:28:09 +01:00
#ifdef _MSC_VER
#pragma warning(pop)
#endif
2003-10-30 00:28:09 +01:00
#include "libtorrent/peer_id.hpp"
#include "libtorrent/bt_peer_connection.hpp"
2003-10-30 00:28:09 +01:00
#include "libtorrent/torrent_info.hpp"
2004-01-31 11:46:15 +01:00
#include "libtorrent/tracker_manager.hpp"
2003-10-30 00:28:09 +01:00
#include "libtorrent/bencode.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/aux_/session_impl.hpp"
2004-02-29 17:39:52 +01:00
#include "libtorrent/invariant_check.hpp"
2003-10-30 00:28:09 +01:00
#if defined(_MSC_VER) && _MSC_VER < 1300
namespace std
{
using ::srand;
2004-01-02 21:46:24 +01:00
using ::isalnum;
2003-10-30 00:28:09 +01:00
};
#endif
2004-10-29 15:21:09 +02:00
using boost::bind;
using boost::mutex;
using libtorrent::aux::session_impl;
2004-10-29 15:21:09 +02:00
#ifdef BOOST_NO_EXCEPTIONS
#define TORRENT_FORWARD(call) \
if (m_ses == 0) return; \
TORRENT_ASSERT(m_chk); \
session_impl::mutex_t::scoped_lock l1(m_ses->m_mutex); \
mutex::scoped_lock l2(m_chk->m_mutex); \
torrent* t = find_torrent(m_ses, m_chk, m_info_hash); \
if (t == 0) return; \
t->call
#define TORRENT_FORWARD_RETURN(call, def) \
if (m_ses == 0) return def; \
TORRENT_ASSERT(m_chk); \
session_impl::mutex_t::scoped_lock l1(m_ses->m_mutex); \
mutex::scoped_lock l2(m_chk->m_mutex); \
torrent* t = find_torrent(m_ses, m_chk, m_info_hash); \
if (t == 0) return def; \
return t->call
#define TORRENT_FORWARD_RETURN2(call, def) \
if (m_ses == 0) return def; \
TORRENT_ASSERT(m_chk); \
session_impl::mutex_t::scoped_lock l1(m_ses->m_mutex); \
mutex::scoped_lock l2(m_chk->m_mutex); \
torrent* t = find_torrent(m_ses, m_chk, m_info_hash); \
if (t == 0) return def; \
t->call
#else
#define TORRENT_FORWARD(call) \
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); \
torrent* t = find_torrent(m_ses, m_chk, m_info_hash); \
if (t == 0) throw_invalid_handle(); \
t->call
#define TORRENT_FORWARD_RETURN(call, def) \
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); \
torrent* t = find_torrent(m_ses, m_chk, m_info_hash); \
if (t == 0) return def; \
return t->call
#define TORRENT_FORWARD_RETURN2(call, def) \
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); \
torrent* t = find_torrent(m_ses, m_chk, m_info_hash); \
if (t == 0) return def; \
t->call
#endif
2003-10-30 00:28:09 +01:00
namespace libtorrent
{
2007-06-10 22:46:09 +02:00
namespace fs = boost::filesystem;
2004-03-23 23:58:18 +01:00
namespace
{
#ifndef BOOST_NO_EXCEPTIONS
void throw_invalid_handle()
2004-03-30 01:25:13 +02:00
{
2005-05-30 19:43:03 +02:00
throw invalid_handle();
}
#endif
torrent* find_torrent(
session_impl* ses
, aux::checker_impl* chk
, sha1_hash const& hash)
2004-03-30 01:25:13 +02:00
{
2007-09-19 20:34:01 +02:00
aux::piece_checker_data* d = chk->find_torrent(hash);
if (d != 0) return d->torrent_ptr.get();
2004-03-30 01:25:13 +02:00
2007-09-19 20:34:01 +02:00
boost::shared_ptr<torrent> t = ses->find_torrent(hash).lock();
if (t) return t.get();
return 0;
2004-03-23 23:58:18 +01:00
}
}
2004-02-29 17:39:52 +01:00
#ifndef NDEBUG
void torrent_handle::check_invariant() const
{
TORRENT_ASSERT((m_ses == 0 && m_chk == 0) || (m_ses != 0 && m_chk != 0));
2004-02-29 17:39:52 +01:00
}
#endif
void torrent_handle::set_max_uploads(int max_uploads) const
2003-12-14 06:56:12 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_ASSERT(max_uploads >= 2 || max_uploads == -1);
TORRENT_FORWARD(set_max_uploads(max_uploads));
2003-12-14 06:56:12 +01:00
}
2004-01-21 01:59:38 +01:00
void torrent_handle::use_interface(const char* net_interface) const
2004-02-26 01:27:06 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_FORWARD(use_interface(net_interface));
2004-02-26 01:27:06 +01:00
}
void torrent_handle::set_max_connections(int max_connections) const
2004-01-21 01:59:38 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_ASSERT(max_connections >= 2 || max_connections == -1);
TORRENT_FORWARD(set_max_connections(max_connections));
2004-01-21 01:59:38 +01:00
}
2003-10-30 00:28:09 +01:00
void torrent_handle::set_peer_upload_limit(tcp::endpoint ip, int limit) const
{
INVARIANT_CHECK;
TORRENT_ASSERT(limit >= -1);
TORRENT_FORWARD(set_peer_upload_limit(ip, limit));
}
void torrent_handle::set_peer_download_limit(tcp::endpoint ip, int limit) const
{
INVARIANT_CHECK;
TORRENT_ASSERT(limit >= -1);
TORRENT_FORWARD(set_peer_download_limit(ip, limit));
}
void torrent_handle::set_upload_limit(int limit) const
2003-11-28 18:29:27 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_ASSERT(limit >= -1);
TORRENT_FORWARD(set_upload_limit(limit));
2003-11-28 18:29:27 +01:00
}
int torrent_handle::upload_limit() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(upload_limit(), 0);
}
void torrent_handle::set_download_limit(int limit) const
{
INVARIANT_CHECK;
TORRENT_ASSERT(limit >= -1);
TORRENT_FORWARD(set_download_limit(limit));
}
int torrent_handle::download_limit() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(download_limit(), 0);
}
2007-06-10 22:46:09 +02:00
void torrent_handle::move_storage(
fs::path const& save_path) const
2004-07-18 02:39:58 +02:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(move_storage(save_path));
2004-07-18 02:39:58 +02:00
}
void torrent_handle::add_extension(
boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> const& ext
, void* userdata)
{
INVARIANT_CHECK;
TORRENT_FORWARD(add_extension(ext, userdata));
}
2004-09-10 02:47:30 +02:00
bool torrent_handle::has_metadata() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(valid_metadata(), false);
2004-09-10 02:47:30 +02:00
}
2004-09-08 01:16:11 +02:00
bool torrent_handle::is_seed() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(is_seed(), false);
2004-09-08 01:16:11 +02:00
}
2004-03-21 03:03:37 +01:00
bool torrent_handle::is_paused() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(is_paused(), false);
2004-03-21 03:03:37 +01:00
}
void torrent_handle::pause() const
2004-03-21 03:03:37 +01:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(pause());
2004-03-21 03:03:37 +01:00
}
void torrent_handle::resume() const
2004-03-21 03:03:37 +01:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(resume());
2004-03-23 23:58:18 +01:00
}
2004-03-21 03:03:37 +01:00
void torrent_handle::set_tracker_login(std::string const& name
, std::string const& password) const
2004-03-23 23:58:18 +01:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(resume());
2004-03-21 03:03:37 +01:00
}
void torrent_handle::file_progress(std::vector<float>& progress)
{
INVARIANT_CHECK;
TORRENT_FORWARD(file_progress(progress));
}
2004-03-23 23:58:18 +01:00
torrent_status torrent_handle::status() const
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
if (m_ses == 0)
#ifdef BOOST_NO_EXCEPTIONS
return torrent_status();
#else
throw_invalid_handle();
#endif
TORRENT_ASSERT(m_chk);
2007-09-19 20:34:01 +02:00
session_impl::mutex_t::scoped_lock l(m_ses->m_mutex);
mutex::scoped_lock l2(m_chk->m_mutex);
2007-09-19 20:34:01 +02:00
aux::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d != 0)
{
torrent_status st = d->torrent_ptr->status();
2004-03-23 23:58:18 +01:00
2007-09-19 20:34:01 +02:00
if (d->processing)
2004-02-29 13:26:05 +01:00
{
2007-09-19 20:34:01 +02:00
if (d->torrent_ptr->is_allocating())
st.state = torrent_status::allocating;
2004-03-23 23:58:18 +01:00
else
2007-09-19 20:34:01 +02:00
st.state = torrent_status::checking_files;
2004-02-29 13:26:05 +01:00
}
2007-09-19 20:34:01 +02:00
else
st.state = torrent_status::queued_for_checking;
st.progress = d->progress;
st.paused = d->torrent_ptr->is_paused();
return st;
}
2007-09-19 20:34:01 +02:00
boost::shared_ptr<torrent> t = m_ses->find_torrent(m_info_hash).lock();
if (t) return t->status();
#ifndef BOOST_NO_EXCEPTIONS
throw_invalid_handle();
#endif
2005-05-31 00:50:54 +02:00
return torrent_status();
}
void torrent_handle::set_sequential_download(bool sd) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(set_sequential_download(sd));
}
2007-03-20 02:59:00 +01:00
std::string torrent_handle::name() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(name(), "");
2007-03-20 02:59:00 +01:00
}
2007-05-30 08:52:59 +02:00
void torrent_handle::piece_availability(std::vector<int>& avail) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(piece_availability(avail));
2007-05-30 08:52:59 +02:00
}
2007-03-20 02:59:00 +01:00
void torrent_handle::piece_priority(int index, int priority) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(set_piece_priority(index, priority));
2007-03-20 02:59:00 +01:00
}
int torrent_handle::piece_priority(int index) const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(piece_priority(index), 0);
2007-03-20 02:59:00 +01:00
}
void torrent_handle::prioritize_pieces(std::vector<int> const& pieces) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(prioritize_pieces(pieces));
2007-03-20 02:59:00 +01:00
}
std::vector<int> torrent_handle::piece_priorities() const
{
INVARIANT_CHECK;
std::vector<int> ret;
TORRENT_FORWARD_RETURN2(piece_priorities(ret), ret);
2007-03-20 02:59:00 +01:00
return ret;
}
void torrent_handle::prioritize_files(std::vector<int> const& files) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(prioritize_files(files));
2007-03-20 02:59:00 +01:00
}
// ============ start deprecation ===============
void torrent_handle::filter_piece(int index, bool filter) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(filter_piece(index, filter));
}
void torrent_handle::filter_pieces(std::vector<bool> const& pieces) const
2005-06-23 01:04:37 +02:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(filter_pieces(pieces));
2005-06-23 01:04:37 +02:00
}
bool torrent_handle::is_piece_filtered(int index) const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(is_piece_filtered(index), false);
}
std::vector<bool> torrent_handle::filtered_pieces() const
{
INVARIANT_CHECK;
std::vector<bool> ret;
TORRENT_FORWARD_RETURN2(filtered_pieces(ret), ret);
return ret;
}
2005-07-02 10:47:46 +02:00
void torrent_handle::filter_files(std::vector<bool> const& files) const
2005-07-04 01:33:47 +02:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(filter_files(files));
2005-07-04 01:33:47 +02:00
}
2007-03-20 02:59:00 +01:00
// ============ end deprecation ===============
2004-09-12 12:12:16 +02:00
std::vector<announce_entry> const& torrent_handle::trackers() const
{
INVARIANT_CHECK;
const static std::vector<announce_entry> empty;
TORRENT_FORWARD_RETURN(trackers(), empty);
2004-09-12 12:12:16 +02:00
}
void torrent_handle::add_url_seed(std::string const& url) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(add_url_seed(url));
}
void torrent_handle::remove_url_seed(std::string const& url) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(remove_url_seed(url));
}
std::set<std::string> torrent_handle::url_seeds() const
{
INVARIANT_CHECK;
const static std::set<std::string> empty;
TORRENT_FORWARD_RETURN(url_seeds(), empty);
}
void torrent_handle::replace_trackers(
std::vector<announce_entry> const& urls) const
2004-09-12 12:12:16 +02:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(replace_trackers(urls));
2004-09-12 12:12:16 +02:00
}
torrent_info const& torrent_handle::get_torrent_info() const
2004-03-23 23:58:18 +01:00
{
INVARIANT_CHECK;
#ifdef BOOST_NO_EXCEPTIONS
const static torrent_info empty;
if (m_ses == 0) return empty;
#else
2007-09-19 20:34:01 +02:00
if (m_ses == 0) throw_invalid_handle();
#endif
TORRENT_ASSERT(m_chk);
2007-08-27 03:05:11 +02:00
session_impl::mutex_t::scoped_lock l1(m_ses->m_mutex);
mutex::scoped_lock l2(m_chk->m_mutex);
torrent* t = find_torrent(m_ses, m_chk, m_info_hash);
if (t == 0 || !t->valid_metadata())
#ifdef BOOST_NO_EXCEPTIONS
return empty;
#else
throw_invalid_handle();
#endif
return t->torrent_file();
2004-03-23 23:58:18 +01:00
}
2003-12-07 06:53:04 +01:00
bool torrent_handle::is_valid() const
2003-11-28 18:29:27 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
2003-11-28 18:29:27 +01:00
if (m_ses == 0) return false;
TORRENT_ASSERT(m_chk);
session_impl::mutex_t::scoped_lock l1(m_ses->m_mutex);
2007-09-19 20:34:01 +02:00
mutex::scoped_lock l2(m_chk->m_mutex);
torrent* t = find_torrent(m_ses, m_chk, m_info_hash);
return t;
2003-10-30 00:28:09 +01:00
}
2004-02-25 00:55:42 +01:00
entry torrent_handle::write_resume_data() const
2004-01-02 21:46:24 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
if (m_ses == 0)
#ifdef BOOST_NO_EXCEPTIONS
return entry();
#else
throw_invalid_handle();
#endif
TORRENT_ASSERT(m_chk);
2004-01-02 21:46:24 +01:00
session_impl::mutex_t::scoped_lock l(m_ses->m_mutex);
mutex::scoped_lock l2(m_chk->m_mutex);
torrent* t = find_torrent(m_ses, m_chk, m_info_hash);
if (!t || !t->valid_metadata())
#ifdef BOOST_NO_EXCEPTIONS
return entry();
#else
throw_invalid_handle();
#endif
2007-10-09 09:26:57 +02:00
std::vector<bool> have_pieces = t->pieces();
2004-01-02 21:46:24 +01:00
2004-01-07 01:48:02 +01:00
entry ret(entry::dictionary_t);
2004-01-02 21:46:24 +01:00
2004-03-05 13:04:47 +01:00
ret["file-format"] = "libtorrent resume file";
ret["file-version"] = 1;
2004-01-03 04:22:53 +01:00
2007-04-17 23:54:40 +02:00
ret["allocation"] = t->filesystem().compact_allocation()?"compact":"full";
2004-01-07 01:48:02 +01:00
const sha1_hash& info_hash = t->torrent_file().info_hash();
2004-03-05 13:04:47 +01:00
ret["info-hash"] = std::string((char*)info_hash.begin(), (char*)info_hash.end());
2004-01-02 21:46:24 +01:00
// blocks per piece
int num_blocks_per_piece =
static_cast<int>(t->torrent_file().piece_length()) / t->block_size();
ret["blocks per piece"] = num_blocks_per_piece;
// if this torrent is a seed, we won't have a piece picker
// and there will be no half-finished pieces.
if (!t->is_seed())
{
const piece_picker& p = t->picker();
2004-01-02 21:46:24 +01:00
const std::vector<piece_picker::downloading_piece>& q
= p.get_download_queue();
2004-01-02 21:46:24 +01:00
// unfinished pieces
ret["unfinished"] = entry::list_type();
entry::list_type& up = ret["unfinished"].list();
2004-01-02 21:46:24 +01:00
// info for each unfinished piece
for (std::vector<piece_picker::downloading_piece>::const_iterator i
= q.begin(); i != q.end(); ++i)
{
2007-05-08 13:13:13 +02:00
if (i->finished == 0) continue;
2004-01-24 18:14:03 +01:00
entry piece_struct(entry::dictionary_t);
2004-01-07 01:48:02 +01:00
// the unfinished piece's index
piece_struct["piece"] = i->index;
2004-01-02 21:46:24 +01:00
2007-10-09 09:26:57 +02:00
have_pieces[i->index] = true;
std::string bitmask;
const int num_bitmask_bytes
= (std::max)(num_blocks_per_piece / 8, 1);
2004-01-24 18:14:03 +01:00
for (int j = 0; j < num_bitmask_bytes; ++j)
{
unsigned char v = 0;
int bits = (std::min)(num_blocks_per_piece - j*8, 8);
2007-05-31 04:22:04 +02:00
for (int k = 0; k < bits; ++k)
2007-06-10 22:46:09 +02:00
v |= (i->info[j*8+k].state == piece_picker::block_info::state_finished)
? (1 << k) : 0;
bitmask.insert(bitmask.end(), v);
TORRENT_ASSERT(bits == 8 || j == num_bitmask_bytes - 1);
}
piece_struct["bitmask"] = bitmask;
2008-02-08 11:22:05 +01:00
/*
TORRENT_ASSERT(t->filesystem().slot_for(i->index) >= 0);
unsigned long adler
= t->filesystem().piece_crc(
t->filesystem().slot_for(i->index)
, t->block_size()
2007-05-08 13:13:13 +02:00
, i->info);
2004-01-24 18:14:03 +01:00
piece_struct["adler32"] = adler;
2008-02-08 11:22:05 +01:00
*/
// push the struct onto the unfinished-piece list
up.push_back(piece_struct);
}
2004-01-02 21:46:24 +01:00
}
std::vector<int> piece_index;
2007-10-09 09:26:57 +02:00
t->filesystem().export_piece_map(piece_index, have_pieces);
entry::list_type& slots = ret["slots"].list();
std::copy(piece_index.begin(), piece_index.end(), std::back_inserter(slots));
2004-01-12 04:05:10 +01:00
// write local peers
2004-03-05 13:04:47 +01:00
entry::list_type& peer_list = ret["peers"].list();
2007-10-03 19:37:15 +02:00
entry::list_type& banned_peer_list = ret["banned_peers"].list();
policy& pol = t->get_policy();
2004-01-12 04:05:10 +01:00
int max_failcount = t->settings().max_failcount;
for (policy::iterator i = pol.begin_peer()
, end(pol.end_peer()); i != end; ++i)
2004-01-12 04:05:10 +01:00
{
asio::error_code ec;
2007-10-03 19:37:15 +02:00
if (i->second.banned)
{
tcp::endpoint ip = i->second.ip;
entry peer(entry::dictionary_t);
peer["ip"] = ip.address().to_string(ec);
if (ec) continue;
2007-10-03 19:37:15 +02:00
peer["port"] = ip.port();
banned_peer_list.push_back(peer);
continue;
}
2004-01-12 04:05:10 +01:00
// we cannot save remote connection
// since we don't know their listen port
// unless they gave us their listen port
// through the extension handshake
// so, if the peer is not connectable (i.e. we
// don't know its listen port) or if it has
// been banned, don't save it.
2007-10-03 19:37:15 +02:00
if (i->second.type == policy::peer::not_connectable) continue;
// don't save peers that doesn't work
if (i->second.failcount >= max_failcount) continue;
tcp::endpoint ip = i->second.ip;
2004-03-17 13:14:44 +01:00
entry peer(entry::dictionary_t);
peer["ip"] = ip.address().to_string(ec);
if (ec) continue;
peer["port"] = ip.port();
2004-01-12 04:05:10 +01:00
peer_list.push_back(peer);
}
t->filesystem().write_resume_data(ret);
2004-01-17 21:04:19 +01:00
2004-01-07 01:48:02 +01:00
return ret;
2004-01-02 21:46:24 +01:00
}
2007-06-10 22:46:09 +02:00
fs::path torrent_handle::save_path() const
2003-12-07 06:53:04 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(save_path(), fs::path());
2003-12-07 06:53:04 +01:00
}
void torrent_handle::connect_peer(tcp::endpoint const& adr, int source) const
2004-01-08 18:03:04 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
if (m_ses == 0)
#ifdef BOOST_NO_EXCEPTIONS
return;
#else
throw_invalid_handle();
#endif
TORRENT_ASSERT(m_chk);
2004-01-08 18:03:04 +01:00
session_impl::mutex_t::scoped_lock l(m_ses->m_mutex);
boost::shared_ptr<torrent> t = m_ses->find_torrent(m_info_hash).lock();
2006-04-27 02:39:21 +02:00
if (!t)
{
// the torrent is being checked. Add the peer to its
// peer list. The entries in there will be connected
// once the checking is complete.
mutex::scoped_lock l2(m_chk->m_mutex);
aux::piece_checker_data* d = m_chk->find_torrent(m_info_hash);
if (d == 0)
#ifdef BOOST_NO_EXCEPTIONS
return;
#else
throw_invalid_handle();
#endif
d->peers.push_back(adr);
return;
}
2004-01-08 18:03:04 +01:00
peer_id id;
std::fill(id.begin(), id.end(), 0);
t->get_policy().peer_from_tracker(adr, id, source, 0);
2004-01-08 18:03:04 +01:00
}
2005-05-03 15:13:57 +02:00
void torrent_handle::force_reannounce(
boost::posix_time::time_duration duration) const
2005-05-03 15:13:57 +02:00
{
INVARIANT_CHECK;
TORRENT_FORWARD(force_tracker_request(time_now() + seconds(duration.total_seconds())));
2005-05-03 15:13:57 +02:00
}
2004-01-08 18:03:04 +01:00
void torrent_handle::force_reannounce() const
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_FORWARD(force_tracker_request());
2004-01-08 18:03:04 +01:00
}
2007-11-20 23:46:27 +01:00
void torrent_handle::scrape_tracker() const
{
INVARIANT_CHECK;
TORRENT_FORWARD(scrape_tracker());
2007-11-20 23:46:27 +01:00
}
void torrent_handle::set_ratio(float ratio) const
2004-01-12 04:05:10 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_ASSERT(ratio >= 0.f);
2004-01-12 04:05:10 +01:00
if (ratio < 1.f && ratio > 0.f)
ratio = 1.f;
TORRENT_FORWARD(set_ratio(ratio));
2004-01-12 04:05:10 +01:00
}
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
void torrent_handle::resolve_countries(bool r)
{
INVARIANT_CHECK;
TORRENT_FORWARD(resolve_countries(r));
}
bool torrent_handle::resolve_countries() const
{
INVARIANT_CHECK;
TORRENT_FORWARD_RETURN(resolving_countries(), false);
}
#endif
2003-12-07 06:53:04 +01:00
void torrent_handle::get_peer_info(std::vector<peer_info>& v) const
2003-10-30 00:28:09 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_FORWARD(get_peer_info(v));
2003-10-30 00:28:09 +01:00
}
2005-03-22 18:50:05 +01:00
2003-12-07 06:53:04 +01:00
void torrent_handle::get_download_queue(std::vector<partial_piece_info>& queue) const
2003-10-30 00:28:09 +01:00
{
2004-02-29 17:39:52 +01:00
INVARIANT_CHECK;
TORRENT_FORWARD(get_download_queue(queue));
}
2003-10-30 00:28:09 +01:00
}