2003-10-23 01:00:57 +02: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.
|
|
|
|
|
|
|
|
*/
|
2007-03-17 18:15:16 +01:00
|
|
|
|
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <cctype>
|
2003-10-30 00:28:09 +01:00
|
|
|
#include <numeric>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/filesystem/convenience.hpp>
|
2004-08-05 15:56:26 +02:00
|
|
|
#include <boost/bind.hpp>
|
2006-04-25 23:04:48 +02:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-10-26 18:35:23 +01:00
|
|
|
#include "libtorrent/torrent_handle.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2004-01-31 11:46:15 +01:00
|
|
|
#include "libtorrent/tracker_manager.hpp"
|
2008-05-17 16:19:34 +02:00
|
|
|
#include "libtorrent/parse_url.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/peer.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
#include "libtorrent/bt_peer_connection.hpp"
|
|
|
|
#include "libtorrent/web_peer_connection.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
2003-12-22 08:14:35 +01:00
|
|
|
#include "libtorrent/alert.hpp"
|
|
|
|
#include "libtorrent/identify_client.hpp"
|
2004-01-18 20:12:18 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2006-11-14 01:08:16 +01:00
|
|
|
#include "libtorrent/extensions.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2007-04-25 20:26:35 +02:00
|
|
|
#include "libtorrent/instantiate_connection.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
2006-01-07 14:48:14 +01:00
|
|
|
using boost::tuples::tuple;
|
|
|
|
using boost::tuples::get;
|
|
|
|
using boost::tuples::make_tuple;
|
2006-01-09 01:07:00 +01:00
|
|
|
using boost::bind;
|
2006-04-25 23:04:48 +02:00
|
|
|
using boost::mutex;
|
2006-10-11 16:02:21 +02:00
|
|
|
using libtorrent::aux::session_impl;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2004-01-12 04:05:10 +01:00
|
|
|
struct find_peer_by_ip
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
find_peer_by_ip(tcp::endpoint const& a, const torrent* t)
|
2004-01-25 13:37:15 +01:00
|
|
|
: ip(a)
|
|
|
|
, tor(t)
|
2007-10-05 02:30:00 +02:00
|
|
|
{ TORRENT_ASSERT(t != 0); }
|
2004-01-12 04:05:10 +01:00
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
bool operator()(session_impl::connection_map::value_type const& c) const
|
2004-01-12 04:05:10 +01:00
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
tcp::endpoint const& sender = c->remote();
|
2006-04-25 23:04:48 +02:00
|
|
|
if (sender.address() != ip.address()) return false;
|
2007-10-31 10:48:20 +01:00
|
|
|
if (tor != c->associated_torrent().lock().get()) return false;
|
2004-01-12 04:05:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
tcp::endpoint const& ip;
|
|
|
|
torrent const* tor;
|
2004-01-12 04:05:10 +01:00
|
|
|
};
|
|
|
|
|
2004-01-13 04:08:59 +01:00
|
|
|
struct peer_by_id
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
peer_by_id(const peer_id& i): pid(i) {}
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
bool operator()(session_impl::connection_map::value_type const& p) const
|
2004-01-13 04:08:59 +01:00
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
if (p->pid() != pid) return false;
|
2004-01-13 04:08:59 +01:00
|
|
|
// have a special case for all zeros. We can have any number
|
2006-04-25 23:04:48 +02:00
|
|
|
// of peers with that pid, since it's used to indicate no pid.
|
|
|
|
if (std::count(pid.begin(), pid.end(), 0) == 20) return false;
|
2004-01-13 04:08:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
peer_id const& pid;
|
2004-01-13 04:08:59 +01:00
|
|
|
};
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2003-12-07 06:53:04 +01:00
|
|
|
torrent::torrent(
|
2006-10-11 16:02:21 +02:00
|
|
|
session_impl& ses
|
2007-09-01 05:00:31 +02:00
|
|
|
, boost::intrusive_ptr<torrent_info> tf
|
2007-06-10 22:46:09 +02:00
|
|
|
, fs::path const& save_path
|
2006-04-25 23:04:48 +02:00
|
|
|
, tcp::endpoint const& net_interface
|
2007-10-08 22:01:36 +02:00
|
|
|
, storage_mode_t storage_mode
|
2006-04-25 23:04:48 +02:00
|
|
|
, int block_size
|
2007-08-22 07:31:42 +02:00
|
|
|
, storage_constructor_type sc
|
2008-03-08 07:06:31 +01:00
|
|
|
, bool paused
|
2008-07-01 01:14:31 +02:00
|
|
|
, std::vector<char>* resume_data
|
2008-04-24 05:28:48 +02:00
|
|
|
, int seq
|
|
|
|
, bool auto_managed)
|
2008-05-19 04:52:32 +02:00
|
|
|
: m_policy(this)
|
|
|
|
, m_active_time(seconds(0))
|
|
|
|
, m_seeding_time(seconds(0))
|
|
|
|
, m_total_uploaded(0)
|
|
|
|
, m_total_downloaded(0)
|
|
|
|
, m_started(time_now())
|
2008-05-19 06:06:25 +02:00
|
|
|
, m_last_scrape(min_time())
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_torrent_file(tf)
|
2004-06-14 01:30:42 +02:00
|
|
|
, m_storage(0)
|
2006-12-15 18:47:21 +01:00
|
|
|
, m_host_resolver(ses.m_io_service)
|
2008-07-12 10:25:19 +02:00
|
|
|
, m_lsd_announce_timer(ses.m_io_service)
|
|
|
|
, m_tracker_timer(ses.m_io_service)
|
2007-05-31 02:21:54 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2007-05-17 02:01:51 +02:00
|
|
|
, m_last_dht_announce(time_now() - minutes(15))
|
2007-05-31 02:21:54 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
, m_ses(ses)
|
2007-09-01 05:00:31 +02:00
|
|
|
, m_trackers(m_torrent_file->trackers())
|
2004-04-18 14:28:02 +02:00
|
|
|
, m_total_failed_bytes(0)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_total_redundant_bytes(0)
|
2006-05-20 17:30:40 +02:00
|
|
|
, m_net_interface(net_interface.address(), 0)
|
2004-10-18 12:36:47 +02:00
|
|
|
, m_save_path(complete(save_path))
|
2007-10-08 22:01:36 +02:00
|
|
|
, m_storage_mode(storage_mode)
|
2008-11-19 01:46:48 +01:00
|
|
|
, m_state(torrent_status::checking_resume_data)
|
2007-08-22 07:31:42 +02:00
|
|
|
, m_settings(ses.settings())
|
2007-03-16 06:29:23 +01:00
|
|
|
, m_storage_constructor(sc)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_progress(0.f)
|
|
|
|
, m_ratio(0.f)
|
2007-08-21 06:46:17 +02:00
|
|
|
, m_max_uploads((std::numeric_limits<int>::max)())
|
2007-08-17 09:37:08 +02:00
|
|
|
, m_num_uploads(0)
|
2007-08-21 06:46:17 +02:00
|
|
|
, m_max_connections((std::numeric_limits<int>::max)())
|
2008-06-18 14:34:39 +02:00
|
|
|
, m_block_size((std::min)(block_size, tf->piece_length()))
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_complete(-1)
|
|
|
|
, m_incomplete(-1)
|
2008-03-29 20:39:24 +01:00
|
|
|
, m_deficit_counter(0)
|
2008-04-24 05:28:48 +02:00
|
|
|
, m_sequence_number(seq)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_last_working_tracker(-1)
|
|
|
|
, m_time_scaler(0)
|
|
|
|
, m_abort(false)
|
|
|
|
, m_paused(paused)
|
|
|
|
, m_auto_managed(auto_managed)
|
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
|
|
|
, m_resolving_country(false)
|
|
|
|
, m_resolve_countries(false)
|
|
|
|
#endif
|
|
|
|
, m_sequential_download(false)
|
|
|
|
, m_got_tracker_response(false)
|
|
|
|
, m_connections_initialized(true)
|
2008-12-08 07:36:22 +01:00
|
|
|
, m_super_seeding(false)
|
2008-06-05 20:19:03 +02:00
|
|
|
, m_has_incoming(false)
|
2008-06-07 18:24:56 +02:00
|
|
|
, m_files_checked(false)
|
2008-07-12 10:25:19 +02:00
|
|
|
, m_announcing(false)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-09-21 04:08:04 +02:00
|
|
|
if (resume_data) m_resume_data.swap(*resume_data);
|
2008-08-21 01:05:12 +02:00
|
|
|
|
2008-12-08 10:13:21 +01:00
|
|
|
if (m_settings.prefer_udp_trackers)
|
|
|
|
prioritize_udp_trackers();
|
|
|
|
|
2008-08-21 01:05:12 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
hasher h;
|
|
|
|
h.update("req2", 4);
|
|
|
|
h.update((char*)&tf->info_hash()[0], 20);
|
|
|
|
m_obfuscated_hash = h.final();
|
|
|
|
#endif
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
torrent::torrent(
|
2006-10-11 16:02:21 +02:00
|
|
|
session_impl& ses
|
2004-06-14 01:30:42 +02:00
|
|
|
, char const* tracker_url
|
|
|
|
, sha1_hash const& info_hash
|
2006-11-14 01:08:16 +01:00
|
|
|
, char const* name
|
2007-06-10 22:46:09 +02:00
|
|
|
, fs::path const& save_path
|
2006-04-25 23:04:48 +02:00
|
|
|
, tcp::endpoint const& net_interface
|
2007-10-08 22:01:36 +02:00
|
|
|
, storage_mode_t storage_mode
|
2006-04-25 23:04:48 +02:00
|
|
|
, int block_size
|
2007-08-22 07:31:42 +02:00
|
|
|
, storage_constructor_type sc
|
2008-03-08 07:06:31 +01:00
|
|
|
, bool paused
|
2008-07-01 01:14:31 +02:00
|
|
|
, std::vector<char>* resume_data
|
2008-04-24 05:28:48 +02:00
|
|
|
, int seq
|
|
|
|
, bool auto_managed)
|
2008-05-19 04:52:32 +02:00
|
|
|
: m_policy(this)
|
|
|
|
, m_active_time(seconds(0))
|
|
|
|
, m_seeding_time(seconds(0))
|
|
|
|
, m_total_uploaded(0)
|
|
|
|
, m_total_downloaded(0)
|
|
|
|
, m_started(time_now())
|
2008-05-19 06:06:25 +02:00
|
|
|
, m_last_scrape(min_time())
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_torrent_file(new torrent_info(info_hash))
|
2004-06-14 01:30:42 +02:00
|
|
|
, m_storage(0)
|
2006-12-15 18:47:21 +01:00
|
|
|
, m_host_resolver(ses.m_io_service)
|
2008-07-12 10:25:19 +02:00
|
|
|
, m_lsd_announce_timer(ses.m_io_service)
|
|
|
|
, m_tracker_timer(ses.m_io_service)
|
2007-05-31 02:21:54 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2007-05-17 02:01:51 +02:00
|
|
|
, m_last_dht_announce(time_now() - minutes(15))
|
2007-05-31 02:21:54 +02:00
|
|
|
#endif
|
2004-06-14 01:30:42 +02:00
|
|
|
, m_ses(ses)
|
|
|
|
, m_total_failed_bytes(0)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_total_redundant_bytes(0)
|
2006-05-20 17:30:40 +02:00
|
|
|
, m_net_interface(net_interface.address(), 0)
|
2004-10-18 12:36:47 +02:00
|
|
|
, m_save_path(complete(save_path))
|
2007-10-08 22:01:36 +02:00
|
|
|
, m_storage_mode(storage_mode)
|
2008-11-19 01:46:48 +01:00
|
|
|
, m_state(torrent_status::checking_resume_data)
|
2007-08-22 07:31:42 +02:00
|
|
|
, m_settings(ses.settings())
|
2007-03-16 06:29:23 +01:00
|
|
|
, m_storage_constructor(sc)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_progress(0.f)
|
|
|
|
, m_ratio(0.f)
|
2007-08-21 06:46:17 +02:00
|
|
|
, m_max_uploads((std::numeric_limits<int>::max)())
|
2007-08-17 09:37:08 +02:00
|
|
|
, m_num_uploads(0)
|
2007-08-21 06:46:17 +02:00
|
|
|
, m_max_connections((std::numeric_limits<int>::max)())
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_block_size(block_size)
|
|
|
|
, m_complete(-1)
|
|
|
|
, m_incomplete(-1)
|
2008-03-29 20:39:24 +01:00
|
|
|
, m_deficit_counter(0)
|
2008-04-24 05:28:48 +02:00
|
|
|
, m_sequence_number(seq)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_last_working_tracker(-1)
|
|
|
|
, m_time_scaler(0)
|
|
|
|
, m_abort(false)
|
|
|
|
, m_paused(paused)
|
|
|
|
, m_auto_managed(auto_managed)
|
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
|
|
|
, m_resolving_country(false)
|
|
|
|
, m_resolve_countries(false)
|
|
|
|
#endif
|
|
|
|
, m_sequential_download(false)
|
|
|
|
, m_got_tracker_response(false)
|
|
|
|
, m_connections_initialized(false)
|
2008-12-08 07:36:22 +01:00
|
|
|
, m_super_seeding(false)
|
2008-06-05 20:19:03 +02:00
|
|
|
, m_has_incoming(false)
|
2008-07-12 10:25:19 +02:00
|
|
|
, m_files_checked(false)
|
|
|
|
, m_announcing(false)
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2008-09-21 04:08:04 +02:00
|
|
|
if (resume_data) m_resume_data.swap(*resume_data);
|
2008-08-21 01:05:12 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
hasher h;
|
|
|
|
h.update("req2", 4);
|
|
|
|
h.update((char*)&info_hash[0], 20);
|
|
|
|
m_obfuscated_hash = h.final();
|
|
|
|
#endif
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-14 07:09:44 +01:00
|
|
|
m_files_checked = false;
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
if (name) m_name.reset(new std::string(name));
|
|
|
|
|
2007-02-21 18:15:47 +01:00
|
|
|
if (tracker_url)
|
|
|
|
{
|
|
|
|
m_trackers.push_back(announce_entry(tracker_url));
|
2008-11-27 21:51:59 +01:00
|
|
|
m_trackers.back().fail_limit = 0;
|
2008-11-29 09:38:40 +01:00
|
|
|
m_trackers.back().source = announce_entry::source_magnet_link;
|
2007-09-01 05:00:31 +02:00
|
|
|
m_torrent_file->add_tracker(tracker_url);
|
2007-02-21 18:15:47 +01:00
|
|
|
}
|
2007-04-11 19:44:15 +02:00
|
|
|
}
|
2007-04-11 19:22:19 +02:00
|
|
|
|
2008-09-21 04:08:04 +02:00
|
|
|
void torrent::start()
|
2008-07-01 01:14:31 +02:00
|
|
|
{
|
2008-11-12 04:30:19 +01:00
|
|
|
TORRENT_ASSERT(!m_picker);
|
|
|
|
m_picker.reset(new piece_picker());
|
|
|
|
|
2008-09-21 04:08:04 +02:00
|
|
|
if (!m_resume_data.empty())
|
2008-07-01 01:14:31 +02:00
|
|
|
{
|
2008-09-21 04:08:04 +02:00
|
|
|
if (lazy_bdecode(&m_resume_data[0], &m_resume_data[0]
|
|
|
|
+ m_resume_data.size(), m_resume_entry) != 0)
|
2008-07-01 01:14:31 +02:00
|
|
|
{
|
2008-09-21 04:08:04 +02:00
|
|
|
std::vector<char>().swap(m_resume_data);
|
|
|
|
if (m_ses.m_alerts.should_post<fastresume_rejected_alert>())
|
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(fastresume_rejected_alert(get_handle(), "parse failed"));
|
2008-07-01 01:14:31 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2008-09-21 04:08:04 +02:00
|
|
|
(*m_ses.m_logger) << "fastresume data for "
|
|
|
|
<< torrent_file().name() << " rejected: parse failed\n";
|
2008-07-01 01:14:31 +02:00
|
|
|
#endif
|
2008-09-21 04:08:04 +02:00
|
|
|
}
|
2008-07-01 01:14:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-27 17:29:42 +02:00
|
|
|
// we need to start announcing since we don't have any
|
|
|
|
// metadata. To receive peers to ask for it.
|
2008-11-10 05:17:16 +01:00
|
|
|
if (m_torrent_file->is_valid())
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
2008-08-29 19:21:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
set_state(torrent_status::downloading_metadata);
|
|
|
|
if (!m_trackers.empty()) start_announcing();
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-02-12 10:20:49 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
bool torrent::should_announce_dht() const
|
|
|
|
{
|
2007-11-25 10:33:26 +01:00
|
|
|
if (m_ses.m_listen_sockets.empty()) return false;
|
|
|
|
|
|
|
|
if (!m_ses.m_dht) return false;
|
2008-08-30 00:58:47 +02:00
|
|
|
if (m_torrent_file->is_valid() && !m_files_checked) return false;
|
2007-11-25 10:33:26 +01:00
|
|
|
|
2007-02-12 10:20:49 +01:00
|
|
|
// don't announce private torrents
|
2007-09-01 05:00:31 +02:00
|
|
|
if (m_torrent_file->is_valid() && m_torrent_file->priv()) return false;
|
2007-02-12 10:20:49 +01:00
|
|
|
if (m_trackers.empty()) return true;
|
2008-11-29 09:38:40 +01:00
|
|
|
|
|
|
|
int verified_trackers = 0;
|
|
|
|
for (std::vector<announce_entry>::const_iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
if (i->verified) ++verified_trackers;
|
2007-02-12 10:20:49 +01:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
return verified_trackers == 0 || !m_settings.use_dht_as_fallback;
|
2007-02-12 10:20:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
torrent::~torrent()
|
|
|
|
{
|
2006-05-28 21:03:54 +02:00
|
|
|
// The invariant can't be maintained here, since the torrent
|
|
|
|
// is being destructed, all weak references to it have been
|
|
|
|
// reset, which means that all its peers already have an
|
|
|
|
// invalidated torrent pointer (so it cannot be verified to be correct)
|
|
|
|
|
|
|
|
// i.e. the invariant can only be maintained if all connections have
|
|
|
|
// been closed by the time the torrent is destructed. And they are
|
|
|
|
// supposed to be closed. So we can still do the invariant check.
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_connections.empty());
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-08-16 14:41:46 +02:00
|
|
|
for (peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end(); ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
(*(*i)->m_logger) << "*** DESTRUCTING TORRENT\n";
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_abort);
|
2005-11-08 01:56:26 +01:00
|
|
|
if (!m_connections.empty())
|
|
|
|
disconnect_all();
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
2008-12-07 22:04:19 +01:00
|
|
|
|
|
|
|
void torrent::add_piece(int piece, char const* data, int flags)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(piece >= 0 && piece < m_torrent_file->num_pieces());
|
|
|
|
int piece_size = m_torrent_file->piece_size(piece);
|
|
|
|
int blocks_in_piece = (piece_size + m_block_size - 1) / m_block_size;
|
|
|
|
|
|
|
|
peer_request p;
|
|
|
|
p.piece = piece;
|
|
|
|
p.start = 0;
|
|
|
|
picker().inc_refcount(piece);
|
|
|
|
for (int i = 0; i < blocks_in_piece; ++i, p.start += m_block_size)
|
|
|
|
{
|
|
|
|
if (picker().is_finished(piece_block(piece, i))
|
|
|
|
&& (flags & torrent::overwrite_existing) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
p.length = (std::min)(piece_size - p.start, m_block_size);
|
|
|
|
char* buffer = m_ses.allocate_disk_buffer();
|
|
|
|
// out of memory
|
|
|
|
if (buffer == 0) return;
|
|
|
|
disk_buffer_holder holder(m_ses, buffer);
|
|
|
|
std::memcpy(buffer, data + p.start, p.length);
|
|
|
|
filesystem().async_write(p, holder, bind(&torrent::on_disk_write_complete
|
|
|
|
, shared_from_this(), _1, _2, p));
|
|
|
|
piece_block block(piece, i);
|
|
|
|
picker().mark_as_downloading(block, 0, piece_picker::fast);
|
|
|
|
picker().mark_as_writing(block, 0);
|
|
|
|
}
|
|
|
|
picker().dec_refcount(piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::on_disk_write_complete(int ret, disk_io_job const& j
|
|
|
|
, peer_request p)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
if (m_abort)
|
|
|
|
{
|
|
|
|
piece_block block_finished(p.piece, p.start / m_block_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
piece_block block_finished(p.piece, p.start / m_block_size);
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
if (has_picker()) picker().write_failed(block_finished);
|
|
|
|
if (alerts().should_post<file_error_alert>())
|
|
|
|
alerts().post_alert(file_error_alert(j.error_file, get_handle(), j.str));
|
|
|
|
set_error(j.str);
|
|
|
|
pause();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
picker().mark_as_finished(block_finished, 0);
|
|
|
|
|
|
|
|
// did we just finish the piece?
|
|
|
|
if (picker().is_piece_finished(p.piece))
|
|
|
|
{
|
|
|
|
async_verify_piece(p.piece, bind(&torrent::piece_finished, shared_from_this()
|
|
|
|
, p.piece, _1));
|
|
|
|
}
|
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2007-10-22 22:58:42 +02:00
|
|
|
peer_request torrent::to_req(piece_block const& p)
|
|
|
|
{
|
|
|
|
int block_offset = p.block_index * m_block_size;
|
2008-04-05 23:18:27 +02:00
|
|
|
int block_size = (std::min)(torrent_file().piece_size(
|
2007-10-22 22:58:42 +02:00
|
|
|
p.piece_index) - block_offset, m_block_size);
|
|
|
|
TORRENT_ASSERT(block_size > 0);
|
|
|
|
TORRENT_ASSERT(block_size <= m_block_size);
|
|
|
|
|
|
|
|
peer_request r;
|
|
|
|
r.piece = p.piece_index;
|
|
|
|
r.start = block_offset;
|
|
|
|
r.length = block_size;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
std::string torrent::name() const
|
|
|
|
{
|
2007-09-01 05:00:31 +02:00
|
|
|
if (valid_metadata()) return m_torrent_file->name();
|
2006-11-14 01:08:16 +01:00
|
|
|
if (m_name) return *m_name;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2007-11-08 02:45:35 +01:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void torrent::add_extension(boost::shared_ptr<torrent_plugin> ext)
|
|
|
|
{
|
|
|
|
m_extensions.push_back(ext);
|
|
|
|
}
|
2007-11-08 02:45:35 +01:00
|
|
|
|
|
|
|
void torrent::add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> const& ext
|
|
|
|
, void* userdata)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent_plugin> 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<peer_plugin> 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();
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
// this may not be called from a constructor because of the call to
|
|
|
|
// shared_from_this()
|
2004-06-14 01:30:42 +02:00
|
|
|
void torrent::init()
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_torrent_file->is_valid());
|
|
|
|
TORRENT_ASSERT(m_torrent_file->num_files() > 0);
|
|
|
|
TORRENT_ASSERT(m_torrent_file->total_size() >= 0);
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2008-07-20 18:00:08 +02:00
|
|
|
m_file_priority.clear();
|
|
|
|
m_file_priority.resize(m_torrent_file->num_files(), 1);
|
|
|
|
|
2008-06-18 14:34:39 +02:00
|
|
|
m_block_size = (std::min)(m_block_size, m_torrent_file->piece_length());
|
|
|
|
|
2008-06-19 13:28:34 +02:00
|
|
|
if (m_torrent_file->num_pieces()
|
|
|
|
> piece_picker::max_pieces)
|
|
|
|
{
|
|
|
|
m_error = "too many pieces in torrent";
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
// the shared_from_this() will create an intentional
|
|
|
|
// cycle of ownership, se the hpp file for description.
|
|
|
|
m_owning_storage = new piece_manager(shared_from_this(), m_torrent_file
|
2008-03-08 07:06:31 +01:00
|
|
|
, m_save_path, m_ses.m_files, m_ses.m_disk_thread, m_storage_constructor
|
|
|
|
, m_storage_mode);
|
2007-06-10 22:46:09 +02:00
|
|
|
m_storage = m_owning_storage.get();
|
2008-06-18 14:34:39 +02:00
|
|
|
m_picker->init((std::max)(m_torrent_file->piece_length() / m_block_size, 1)
|
2008-06-07 04:58:28 +02:00
|
|
|
, int((m_torrent_file->total_size()+m_block_size-1)/m_block_size));
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
std::vector<std::string> const& url_seeds = m_torrent_file->url_seeds();
|
2006-04-25 23:04:48 +02:00
|
|
|
std::copy(url_seeds.begin(), url_seeds.end(), std::inserter(m_web_seeds
|
|
|
|
, m_web_seeds.begin()));
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-11-19 01:46:48 +01:00
|
|
|
set_state(torrent_status::checking_resume_data);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
if (m_resume_entry.type() == lazy_entry::dict_t)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
|
|
|
char const* error = 0;
|
2008-07-01 01:14:31 +02:00
|
|
|
if (m_resume_entry.dict_find_string_value("file-format") != "libtorrent resume file")
|
2008-05-28 10:44:40 +02:00
|
|
|
error = "invalid file format tag";
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
std::string info_hash = m_resume_entry.dict_find_string_value("info-hash");
|
|
|
|
if (!error && info_hash.empty())
|
2008-05-28 10:44:40 +02:00
|
|
|
error = "missing info-hash";
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
if (!error && sha1_hash(info_hash) != m_torrent_file->info_hash())
|
2008-05-28 10:44:40 +02:00
|
|
|
error = "mismatching info-hash";
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (error && m_ses.m_alerts.should_post<fastresume_rejected_alert>())
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(fastresume_rejected_alert(get_handle(), error));
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
(*m_ses.m_logger) << "fastresume data for "
|
|
|
|
<< torrent_file().name() << " rejected: "
|
|
|
|
<< error << "\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
std::vector<char>().swap(m_resume_data);
|
2008-07-02 23:23:02 +02:00
|
|
|
lazy_entry().swap(m_resume_entry);
|
2008-07-01 01:14:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_resume_data(m_resume_entry);
|
|
|
|
}
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-03 07:49:41 +02:00
|
|
|
TORRENT_ASSERT(m_block_size > 0);
|
|
|
|
int file = 0;
|
|
|
|
for (file_storage::iterator i = m_torrent_file->files().begin()
|
|
|
|
, end(m_torrent_file->files().end()); i != end; ++i, ++file)
|
|
|
|
{
|
|
|
|
if (!i->pad_file) continue;
|
|
|
|
|
|
|
|
peer_request pr = m_torrent_file->map_file(file, 0, m_torrent_file->file_at(file).size);
|
|
|
|
int off = pr.start % m_block_size;
|
|
|
|
if (off != 0) { pr.length -= m_block_size - off; pr.start += m_block_size - off; }
|
|
|
|
TORRENT_ASSERT((pr.start % m_block_size) == 0);
|
|
|
|
|
|
|
|
int blocks_per_piece = m_torrent_file->piece_length() / m_block_size;
|
|
|
|
piece_block pb(pr.piece, pr.start / m_block_size);
|
|
|
|
for (; pr.length >= m_block_size; pr.length -= m_block_size, ++pb.block_index)
|
|
|
|
{
|
|
|
|
if (pb.block_index == blocks_per_piece) { pb.block_index = 0; ++pb.piece_index; }
|
|
|
|
m_picker->mark_as_finished(pb, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
m_storage->async_check_fastresume(&m_resume_entry
|
2008-03-08 07:06:31 +01:00
|
|
|
, bind(&torrent::on_resume_data_checked
|
|
|
|
, shared_from_this(), _1, _2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::on_resume_data_checked(int ret, disk_io_job const& j)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
if (ret == piece_manager::fatal_disk_error)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<file_error_alert>())
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-04-13 00:08:07 +02:00
|
|
|
m_ses.m_alerts.post_alert(file_error_alert(j.error_file, get_handle(), j.str));
|
2008-03-08 07:06:31 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
(*m_ses.m_logger) << time_now_string() << ": fatal disk error ["
|
|
|
|
" error: " << j.str <<
|
|
|
|
" torrent: " << torrent_file().name() <<
|
|
|
|
" ]\n";
|
|
|
|
#endif
|
|
|
|
}
|
2008-05-20 09:57:44 +02:00
|
|
|
m_error = j.str;
|
2008-03-08 07:06:31 +01:00
|
|
|
pause();
|
2008-11-19 01:46:48 +01:00
|
|
|
set_state(torrent_status::queued_for_checking);
|
2008-07-02 23:23:02 +02:00
|
|
|
|
|
|
|
std::vector<char>().swap(m_resume_data);
|
|
|
|
lazy_entry().swap(m_resume_entry);
|
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
if (m_resume_entry.type() == lazy_entry::dict_t)
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
// parse out "peers" from the resume data and add them to the peer list
|
|
|
|
if (lazy_entry const* peers_entry = m_resume_entry.dict_find_list("peers"))
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
peer_id id(0);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
for (int i = 0; i < peers_entry->list_size(); ++i)
|
|
|
|
{
|
|
|
|
lazy_entry const* e = peers_entry->list_at(i);
|
|
|
|
if (e->type() != lazy_entry::dict_t) continue;
|
|
|
|
std::string ip = e->dict_find_string_value("ip");
|
|
|
|
int port = e->dict_find_int_value("port");
|
|
|
|
if (ip.empty() || port == 0) continue;
|
2008-11-05 06:39:18 +01:00
|
|
|
error_code ec;
|
|
|
|
tcp::endpoint a(address::from_string(ip, ec), (unsigned short)port);
|
|
|
|
if (ec) continue;
|
2008-07-01 01:14:31 +02:00
|
|
|
m_policy.peer_from_tracker(a, id, peer_info::resume_data, 0);
|
|
|
|
}
|
|
|
|
}
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
// parse out "banned_peers" and add them as banned
|
|
|
|
if (lazy_entry const* banned_peers_entry = m_resume_entry.dict_find_list("banned_peers"))
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
peer_id id(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < banned_peers_entry->list_size(); ++i)
|
|
|
|
{
|
|
|
|
lazy_entry const* e = banned_peers_entry->list_at(i);
|
|
|
|
if (e->type() != lazy_entry::dict_t) continue;
|
|
|
|
std::string ip = e->dict_find_string_value("ip");
|
|
|
|
int port = e->dict_find_int_value("port");
|
|
|
|
if (ip.empty() || port == 0) continue;
|
2008-11-05 06:39:18 +01:00
|
|
|
error_code ec;
|
|
|
|
tcp::endpoint a(address::from_string(ip, ec), (unsigned short)port);
|
|
|
|
if (ec) continue;
|
2008-07-01 01:14:31 +02:00
|
|
|
policy::peer* p = m_policy.peer_from_tracker(a, id, peer_info::resume_data, 0);
|
|
|
|
if (p) p->banned = true;
|
|
|
|
}
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fastresume_rejected = !j.str.empty();
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (fastresume_rejected && m_ses.m_alerts.should_post<fastresume_rejected_alert>())
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(fastresume_rejected_alert(get_handle(), j.str));
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
(*m_ses.m_logger) << "fastresume data for "
|
|
|
|
<< torrent_file().name() << " rejected: "
|
|
|
|
<< j.str << "\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
// there are either no files for this torrent
|
|
|
|
// or the resume_data was accepted
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
if (!fastresume_rejected && m_resume_entry.type() == lazy_entry::dict_t)
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-04-13 20:54:36 +02:00
|
|
|
// parse have bitmask
|
2008-07-01 01:14:31 +02:00
|
|
|
lazy_entry const* pieces = m_resume_entry.dict_find("pieces");
|
|
|
|
if (pieces && pieces->type() == lazy_entry::string_t
|
|
|
|
&& int(pieces->string_length()) == m_torrent_file->num_pieces())
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
char const* pieces_str = pieces->string_ptr();
|
|
|
|
for (int i = 0, end(pieces->string_length()); i < end; ++i)
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-05-28 04:35:02 +02:00
|
|
|
if ((pieces_str[i] & 1) == 0) continue;
|
2008-06-07 04:58:28 +02:00
|
|
|
m_picker->we_have(i);
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse unfinished pieces
|
|
|
|
int num_blocks_per_piece =
|
|
|
|
static_cast<int>(torrent_file().piece_length()) / block_size();
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
if (lazy_entry const* unfinished_ent = m_resume_entry.dict_find_list("unfinished"))
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
for (int i = 0; i < unfinished_ent->list_size(); ++i)
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
lazy_entry const* e = unfinished_ent->list_at(i);
|
|
|
|
if (e->type() != lazy_entry::dict_t) continue;
|
|
|
|
int piece = e->dict_find_int_value("piece", -1);
|
|
|
|
if (piece < 0 || piece > torrent_file().num_pieces()) continue;
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
if (m_picker->have_piece(piece))
|
|
|
|
m_picker->we_dont_have(piece);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
std::string bitmask = e->dict_find_string_value("bitmask");
|
|
|
|
if (bitmask.empty()) continue;
|
2008-03-08 07:06:31 +01:00
|
|
|
|
|
|
|
const int num_bitmask_bytes = (std::max)(num_blocks_per_piece / 8, 1);
|
|
|
|
if ((int)bitmask.size() != num_bitmask_bytes) continue;
|
|
|
|
for (int j = 0; j < num_bitmask_bytes; ++j)
|
|
|
|
{
|
|
|
|
unsigned char bits = bitmask[j];
|
|
|
|
int num_bits = (std::min)(num_blocks_per_piece - j*8, 8);
|
|
|
|
for (int k = 0; k < num_bits; ++k)
|
|
|
|
{
|
|
|
|
const int bit = j * 8 + k;
|
|
|
|
if (bits & (1 << k))
|
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
m_picker->mark_as_finished(piece_block(piece, bit), 0);
|
|
|
|
if (m_picker->is_piece_finished(piece))
|
|
|
|
async_verify_piece(piece, bind(&torrent::piece_finished
|
|
|
|
, shared_from_this(), piece, _1));
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
files_checked();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// either the fastresume data was rejected or there are
|
|
|
|
// some files
|
2008-11-19 01:46:48 +01:00
|
|
|
set_state(torrent_status::queued_for_checking);
|
|
|
|
if (should_check_files())
|
2008-09-20 19:42:25 +02:00
|
|
|
m_ses.check_torrent(shared_from_this());
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
2008-07-02 23:23:02 +02:00
|
|
|
|
|
|
|
std::vector<char>().swap(m_resume_data);
|
|
|
|
lazy_entry().swap(m_resume_entry);
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
|
|
|
|
2008-06-07 18:24:56 +02:00
|
|
|
void torrent::force_recheck()
|
|
|
|
{
|
2008-11-19 01:46:48 +01:00
|
|
|
// if the torrent is already queued to check its files
|
|
|
|
// don't do anything
|
|
|
|
if (should_check_files()
|
|
|
|
|| m_state == torrent_status::checking_resume_data)
|
2008-06-17 16:56:22 +02:00
|
|
|
return;
|
|
|
|
|
2008-11-19 01:46:48 +01:00
|
|
|
if ((is_paused() && !m_auto_managed))
|
|
|
|
{
|
|
|
|
// set the queued for checking state, so that it's
|
|
|
|
// checked as soon as it's resumed or made auto managed
|
|
|
|
set_state(torrent_status::queued_for_checking);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!m_error.empty()) m_error.clear();
|
|
|
|
|
2008-06-07 18:24:56 +02:00
|
|
|
disconnect_all();
|
|
|
|
|
|
|
|
m_owning_storage->async_release_files();
|
|
|
|
m_owning_storage = new piece_manager(shared_from_this(), m_torrent_file
|
|
|
|
, m_save_path, m_ses.m_files, m_ses.m_disk_thread, m_storage_constructor
|
|
|
|
, m_storage_mode);
|
|
|
|
m_storage = m_owning_storage.get();
|
2008-08-26 23:35:33 +02:00
|
|
|
if (!m_picker) m_picker.reset(new piece_picker());
|
2008-06-07 18:24:56 +02:00
|
|
|
m_picker->init(m_torrent_file->piece_length() / m_block_size
|
|
|
|
, int((m_torrent_file->total_size()+m_block_size-1)/m_block_size));
|
|
|
|
// assume that we don't have anything
|
|
|
|
m_files_checked = false;
|
2008-11-19 01:46:48 +01:00
|
|
|
set_state(torrent_status::checking_resume_data);
|
2008-06-17 16:56:22 +02:00
|
|
|
|
|
|
|
if (m_auto_managed)
|
|
|
|
set_queue_position((std::numeric_limits<int>::max)());
|
2008-06-07 18:24:56 +02:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
std::vector<char>().swap(m_resume_data);
|
2008-07-02 23:23:02 +02:00
|
|
|
lazy_entry().swap(m_resume_entry);
|
2008-07-01 01:14:31 +02:00
|
|
|
m_storage->async_check_fastresume(&m_resume_entry
|
2008-06-07 18:24:56 +02:00
|
|
|
, bind(&torrent::on_force_recheck
|
|
|
|
, shared_from_this(), _1, _2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::on_force_recheck(int ret, disk_io_job const& j)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
if (ret == piece_manager::fatal_disk_error)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<file_error_alert>())
|
2008-06-07 18:24:56 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(file_error_alert(j.error_file, get_handle(), j.str));
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
(*m_ses.m_logger) << time_now_string() << ": fatal disk error ["
|
|
|
|
" error: " << j.str <<
|
|
|
|
" torrent: " << torrent_file().name() <<
|
|
|
|
" ]\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
m_error = j.str;
|
|
|
|
pause();
|
|
|
|
return;
|
|
|
|
}
|
2008-09-20 19:42:25 +02:00
|
|
|
if (!is_torrent_paused() || is_auto_managed())
|
2008-11-24 20:18:53 +01:00
|
|
|
{
|
|
|
|
set_state(torrent_status::queued_for_checking);
|
2008-09-20 19:42:25 +02:00
|
|
|
m_ses.check_torrent(shared_from_this());
|
2008-11-24 20:18:53 +01:00
|
|
|
}
|
2008-06-07 18:24:56 +02:00
|
|
|
}
|
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
void torrent::start_checking()
|
|
|
|
{
|
2008-11-19 01:46:48 +01:00
|
|
|
TORRENT_ASSERT(should_check_files());
|
2008-07-03 12:05:51 +02:00
|
|
|
set_state(torrent_status::checking_files);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
|
|
|
m_storage->async_check_files(bind(
|
|
|
|
&torrent::on_piece_checked
|
|
|
|
, shared_from_this(), _1, _2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::on_piece_checked(int ret, disk_io_job const& j)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-06-09 06:46:34 +02:00
|
|
|
if (ret == piece_manager::disk_check_aborted)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-03-08 07:06:31 +01:00
|
|
|
if (ret == piece_manager::fatal_disk_error)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<file_error_alert>())
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-04-13 00:08:07 +02:00
|
|
|
m_ses.m_alerts.post_alert(file_error_alert(j.error_file, get_handle(), j.str));
|
2008-03-08 07:06:31 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
(*m_ses.m_logger) << time_now_string() << ": fatal disk error ["
|
|
|
|
" error: " << j.str <<
|
|
|
|
" torrent: " << torrent_file().name() <<
|
|
|
|
" ]\n";
|
|
|
|
#endif
|
|
|
|
}
|
2008-05-20 09:57:44 +02:00
|
|
|
m_error = j.str;
|
2008-03-08 07:06:31 +01:00
|
|
|
pause();
|
2008-11-19 01:46:48 +01:00
|
|
|
if (!m_abort)
|
|
|
|
{
|
|
|
|
m_ses.done_checking(shared_from_this());
|
|
|
|
set_state(torrent_status::queued_for_checking);
|
|
|
|
}
|
2008-03-08 07:06:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_progress = j.piece / float(torrent_file().num_pieces());
|
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
TORRENT_ASSERT(m_picker);
|
|
|
|
if (j.offset >= 0 && !m_picker->have_piece(j.offset))
|
2008-05-12 08:20:54 +02:00
|
|
|
m_picker->we_have(j.offset);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
|
|
|
// we're not done checking yet
|
|
|
|
// this handler will be called repeatedly until
|
|
|
|
// we're done, or encounter a failure
|
|
|
|
if (ret == piece_manager::need_full_check) return;
|
|
|
|
|
2008-10-26 10:23:00 +01:00
|
|
|
if (!m_abort) m_ses.done_checking(shared_from_this());
|
2008-03-08 07:06:31 +01:00
|
|
|
files_checked();
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
|
2004-02-26 01:27:06 +01:00
|
|
|
void torrent::use_interface(const char* net_interface)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-11-05 06:39:18 +01:00
|
|
|
error_code ec;
|
|
|
|
address a(address::from_string(net_interface, ec));
|
|
|
|
if (ec) return;
|
|
|
|
m_net_interface = tcp::endpoint(a, 0);
|
2004-02-26 01:27:06 +01:00
|
|
|
}
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
void torrent::on_tracker_announce_disp(boost::weak_ptr<torrent> p
|
2008-05-03 18:05:42 +02:00
|
|
|
, error_code const& e)
|
2007-04-04 04:06:07 +02:00
|
|
|
{
|
|
|
|
if (e) return;
|
2007-04-11 19:22:19 +02:00
|
|
|
boost::shared_ptr<torrent> t = p.lock();
|
|
|
|
if (!t) return;
|
2008-07-12 10:25:19 +02:00
|
|
|
t->on_tracker_announce();
|
2007-04-11 19:22:19 +02:00
|
|
|
}
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
void torrent::on_tracker_announce()
|
2007-04-11 19:22:19 +02:00
|
|
|
{
|
2008-07-12 10:25:19 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2007-11-11 20:09:29 +01:00
|
|
|
if (m_abort) return;
|
2008-07-12 10:25:19 +02:00
|
|
|
announce_with_tracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::on_lsd_announce_disp(boost::weak_ptr<torrent> p
|
|
|
|
, error_code const& e)
|
|
|
|
{
|
|
|
|
if (e) return;
|
|
|
|
boost::shared_ptr<torrent> t = p.lock();
|
|
|
|
if (!t) return;
|
|
|
|
t->on_lsd_announce();
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::on_lsd_announce()
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
if (m_abort) return;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(!m_torrent_file->priv());
|
|
|
|
if (m_torrent_file->is_valid() && m_torrent_file->priv())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (is_paused()) return;
|
2007-11-11 20:09:29 +01:00
|
|
|
|
2007-04-11 19:22:19 +02:00
|
|
|
boost::weak_ptr<torrent> self(shared_from_this());
|
2007-04-04 04:06:07 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-04-04 04:06:07 +02:00
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
// announce on local network every 5 minutes
|
|
|
|
m_lsd_announce_timer.expires_from_now(minutes(5), ec);
|
|
|
|
m_lsd_announce_timer.async_wait(
|
|
|
|
bind(&torrent::on_lsd_announce_disp, self, _1));
|
|
|
|
|
|
|
|
// announce with the local discovery service
|
|
|
|
m_ses.announce_lsd(m_torrent_file->info_hash());
|
2007-04-04 04:06:07 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (!m_ses.m_dht) return;
|
2007-05-17 02:01:51 +02:00
|
|
|
ptime now = time_now();
|
|
|
|
if (should_announce_dht() && now - m_last_dht_announce > minutes(14))
|
2007-04-04 04:06:07 +02:00
|
|
|
{
|
2007-05-17 02:01:51 +02:00
|
|
|
m_last_dht_announce = now;
|
2007-09-01 05:00:31 +02:00
|
|
|
m_ses.m_dht->announce(m_torrent_file->info_hash()
|
2007-09-22 18:27:29 +02:00
|
|
|
, m_ses.m_listen_sockets.front().external_port
|
2008-01-08 06:47:43 +01:00
|
|
|
, bind(&torrent::on_dht_announce_response_disp, self, _1));
|
2007-04-04 04:06:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
|
2006-08-29 03:15:24 +02:00
|
|
|
void torrent::on_dht_announce_response_disp(boost::weak_ptr<libtorrent::torrent> t
|
|
|
|
, std::vector<tcp::endpoint> const& peers)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<libtorrent::torrent> tor = t.lock();
|
|
|
|
if (!tor) return;
|
|
|
|
tor->on_dht_announce_response(peers);
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
void torrent::on_dht_announce_response(std::vector<tcp::endpoint> const& peers)
|
|
|
|
{
|
2007-02-01 08:33:04 +01:00
|
|
|
if (peers.empty()) return;
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<dht_reply_alert>())
|
2007-02-01 08:33:04 +01:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
m_ses.m_alerts.post_alert(dht_reply_alert(
|
|
|
|
get_handle(), peers.size()));
|
2007-02-01 08:33:04 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
std::for_each(peers.begin(), peers.end(), bind(
|
2007-04-10 23:23:13 +02:00
|
|
|
&policy::peer_from_tracker, boost::ref(m_policy), _1, peer_id(0)
|
|
|
|
, peer_info::dht, 0));
|
2006-08-01 17:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
void torrent::announce_with_tracker(tracker_request::event_t e)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-07-14 15:09:07 +02:00
|
|
|
if (m_trackers.empty()) return;
|
2008-07-12 10:25:19 +02:00
|
|
|
|
2008-08-11 16:31:58 +02:00
|
|
|
if (m_abort) e = tracker_request::stopped;
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
tracker_request req;
|
|
|
|
req.info_hash = m_torrent_file->info_hash();
|
|
|
|
req.pid = m_ses.get_peer_id();
|
|
|
|
req.downloaded = m_stat.total_payload_download();
|
|
|
|
req.uploaded = m_stat.total_payload_upload();
|
|
|
|
req.left = bytes_left();
|
|
|
|
if (req.left == -1) req.left = 16*1024;
|
|
|
|
req.event = e;
|
|
|
|
tcp::endpoint ep = m_ses.get_ipv6_interface();
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
2008-07-12 10:25:19 +02:00
|
|
|
if (ep != tcp::endpoint())
|
2008-10-21 10:45:42 +02:00
|
|
|
req.ipv6 = ep.address().to_string(ec);
|
2008-07-12 10:25:19 +02:00
|
|
|
|
|
|
|
// if we are aborting. we don't want any new peers
|
|
|
|
req.num_want = (req.event == tracker_request::stopped)
|
|
|
|
?0:m_settings.num_want;
|
|
|
|
|
|
|
|
req.listen_port = m_ses.m_listen_sockets.empty()
|
|
|
|
?0:m_ses.m_listen_sockets.front().external_port;
|
|
|
|
req.key = m_ses.m_key;
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
ptime now = time_now();
|
|
|
|
|
|
|
|
int tier = INT_MAX;
|
|
|
|
for (int i = 0; i < m_trackers.size(); ++i)
|
2008-07-12 10:25:19 +02:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
announce_entry& ae = m_trackers[i];
|
|
|
|
if (ae.tier > tier) break;
|
|
|
|
if (ae.is_working()) tier = ae.tier;
|
|
|
|
if (!ae.can_announce(now))
|
|
|
|
{
|
|
|
|
if (ae.is_working() && !m_settings.announce_to_all_trackers) break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
req.url = ae.url;
|
|
|
|
req.event = e;
|
|
|
|
if (req.event == tracker_request::none)
|
|
|
|
{
|
|
|
|
if (!ae.start_sent) req.event = tracker_request::started;
|
|
|
|
if (!ae.complete_sent && is_seed()) req.event = tracker_request::completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
|
|
|
if (m_abort)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<aux::tracker_logger> tl(new aux::tracker_logger(m_ses));
|
|
|
|
m_ses.m_tracker_manager.queue_request(m_ses.m_io_service, m_ses.m_half_open, req
|
|
|
|
, tracker_login(), m_ses.m_listen_interface.address(), tl);
|
|
|
|
}
|
|
|
|
else
|
2008-07-12 10:25:19 +02:00
|
|
|
#endif
|
2008-11-29 09:38:40 +01:00
|
|
|
m_ses.m_tracker_manager.queue_request(m_ses.m_io_service, m_ses.m_half_open, req
|
|
|
|
, tracker_login(), m_ses.m_listen_interface.address()
|
|
|
|
, m_abort?boost::shared_ptr<torrent>():shared_from_this());
|
|
|
|
ae.updating = true;
|
2008-07-12 10:25:19 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
if (m_ses.m_alerts.should_post<tracker_announce_alert>())
|
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
|
|
|
tracker_announce_alert(get_handle(), req.url, req.event));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ae.is_working() && !m_settings.announce_to_all_trackers) break;
|
2008-07-12 10:25:19 +02:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
update_tracker_timer();
|
2008-07-12 10:25:19 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 23:46:27 +01:00
|
|
|
void torrent::scrape_tracker()
|
|
|
|
{
|
|
|
|
if (m_trackers.empty()) return;
|
|
|
|
|
2008-11-27 21:48:29 +01:00
|
|
|
int i = m_last_working_tracker;
|
|
|
|
if (i == -1) i = 0;
|
2007-11-20 23:46:27 +01:00
|
|
|
|
|
|
|
tracker_request req;
|
|
|
|
req.info_hash = m_torrent_file->info_hash();
|
|
|
|
req.kind = tracker_request::scrape_request;
|
2008-11-27 21:48:29 +01:00
|
|
|
req.url = m_trackers[i].url;
|
2008-01-08 06:47:43 +01:00
|
|
|
m_ses.m_tracker_manager.queue_request(m_ses.m_io_service, m_ses.m_half_open, req
|
2007-11-20 23:46:27 +01:00
|
|
|
, tracker_login(), m_ses.m_listen_interface.address(), shared_from_this());
|
2008-05-19 06:06:25 +02:00
|
|
|
|
|
|
|
m_last_scrape = time_now();
|
2007-11-20 23:46:27 +01:00
|
|
|
}
|
|
|
|
|
2008-04-23 03:54:21 +02:00
|
|
|
void torrent::tracker_warning(tracker_request const& req, std::string const& msg)
|
2005-08-11 01:32:39 +02:00
|
|
|
{
|
2007-03-31 00:00:26 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<tracker_warning_alert>())
|
2008-04-23 03:54:21 +02:00
|
|
|
m_ses.m_alerts.post_alert(tracker_warning_alert(get_handle(), req.url, msg));
|
2005-08-11 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 23:46:27 +01:00
|
|
|
void torrent::tracker_scrape_response(tracker_request const& req
|
|
|
|
, int complete, int incomplete, int downloaded)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
TORRENT_ASSERT(req.kind == tracker_request::scrape_request);
|
|
|
|
|
|
|
|
if (complete >= 0) m_complete = complete;
|
|
|
|
if (incomplete >= 0) m_incomplete = incomplete;
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<scrape_reply_alert>())
|
2007-11-20 23:46:27 +01:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(scrape_reply_alert(
|
2008-07-06 14:22:56 +02:00
|
|
|
get_handle(), m_incomplete, m_complete, req.url));
|
2007-11-20 23:46:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-21 14:16:11 +01:00
|
|
|
void torrent::tracker_response(
|
2007-11-21 00:19:30 +01:00
|
|
|
tracker_request const& r
|
2008-12-01 09:48:54 +01:00
|
|
|
, address const& tracker_ip
|
2005-03-24 13:13:47 +01:00
|
|
|
, std::vector<peer_entry>& peer_list
|
2005-02-21 14:59:24 +01:00
|
|
|
, int interval
|
|
|
|
, int complete
|
2008-03-29 23:45:55 +01:00
|
|
|
, int incomplete
|
|
|
|
, address const& external_ip)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2007-03-31 00:00:26 +02:00
|
|
|
INVARIANT_CHECK;
|
2007-11-20 23:46:27 +01:00
|
|
|
TORRENT_ASSERT(r.kind == tracker_request::announce_request);
|
2007-03-31 00:00:26 +02:00
|
|
|
|
2008-03-29 23:45:55 +01:00
|
|
|
if (external_ip != address())
|
|
|
|
m_ses.set_external_address(external_ip);
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
ptime now = time_now();
|
2008-08-02 00:34:37 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
if (interval < m_settings.min_announce_interval)
|
|
|
|
interval = m_settings.min_announce_interval;
|
2004-01-25 13:37:15 +01:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
announce_entry* ae = find_tracker(r);
|
|
|
|
if (ae)
|
2008-11-27 21:48:29 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
if (!ae->start_sent && r.event == tracker_request::started)
|
|
|
|
ae->start_sent = true;
|
|
|
|
if (!ae->complete_sent && r.event == tracker_request::completed)
|
|
|
|
ae->complete_sent = true;
|
|
|
|
ae->verified = true;
|
|
|
|
ae->updating = false;
|
|
|
|
ae->fails = 0;
|
|
|
|
ae->next_announce = now + seconds(interval);
|
|
|
|
int tracker_index = ae - &m_trackers[0];
|
|
|
|
m_last_working_tracker = prioritize_tracker(tracker_index);
|
2008-11-27 21:48:29 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
update_tracker_timer();
|
2003-10-23 18:55:52 +02:00
|
|
|
|
2005-02-21 14:59:24 +01:00
|
|
|
if (complete >= 0) m_complete = complete;
|
|
|
|
if (incomplete >= 0) m_incomplete = incomplete;
|
2008-05-19 06:06:25 +02:00
|
|
|
if (complete >= 0 && incomplete >= 0)
|
2008-11-29 09:38:40 +01:00
|
|
|
m_last_scrape = now;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2004-01-21 14:16:11 +01:00
|
|
|
std::stringstream s;
|
2004-01-22 23:45:52 +01:00
|
|
|
s << "TRACKER RESPONSE:\n"
|
2008-11-29 09:38:40 +01:00
|
|
|
"interval: " << interval << "\n"
|
2004-01-22 23:45:52 +01:00
|
|
|
"peers:\n";
|
2004-01-21 14:16:11 +01:00
|
|
|
for (std::vector<peer_entry>::const_iterator i = peer_list.begin();
|
2005-07-06 02:58:23 +02:00
|
|
|
i != peer_list.end(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-21 14:16:11 +01:00
|
|
|
s << " " << std::setfill(' ') << std::setw(16) << i->ip
|
2004-01-22 23:45:52 +01:00
|
|
|
<< " " << std::setw(5) << std::dec << i->port << " ";
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!i->pid.is_all_zeros()) s << " " << i->pid << " " << identify_client(i->pid);
|
2004-01-22 23:45:52 +01:00
|
|
|
s << "\n";
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2008-03-29 23:45:55 +01:00
|
|
|
s << "external ip: " << external_ip << "\n";
|
2004-01-21 14:16:11 +01:00
|
|
|
debug_log(s.str());
|
|
|
|
#endif
|
|
|
|
// for each of the peers we got from the tracker
|
|
|
|
for (std::vector<peer_entry>::iterator i = peer_list.begin();
|
2005-07-06 02:58:23 +02:00
|
|
|
i != peer_list.end(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-21 14:16:11 +01:00
|
|
|
// don't make connections to ourself
|
2006-04-25 23:04:48 +02:00
|
|
|
if (i->pid == m_ses.get_peer_id())
|
2004-01-21 14:16:11 +01:00
|
|
|
continue;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-04-07 04:51:21 +02:00
|
|
|
tcp::endpoint a(address::from_string(i->ip, ec), i->port);
|
|
|
|
|
|
|
|
if (ec)
|
2005-07-06 02:58:23 +02:00
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
// assume this is because we got a hostname instead of
|
|
|
|
// an ip address from the tracker
|
2007-03-07 19:50:38 +01:00
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
tcp::resolver::query q(i->ip, boost::lexical_cast<std::string>(i->port));
|
|
|
|
m_host_resolver.async_resolve(q,
|
2008-05-06 23:27:06 +02:00
|
|
|
bind(&torrent::on_peer_name_lookup, shared_from_this(), _1, _2, i->pid));
|
2008-04-07 04:51:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-01 09:48:54 +01:00
|
|
|
// ignore local addresses from the tracker (unless the tracker is local too)
|
|
|
|
if (is_local(a.address()) && !is_local(tracker_ip)) continue;
|
2007-10-31 10:48:20 +01:00
|
|
|
m_policy.peer_from_tracker(a, i->pid, peer_info::tracker, 0);
|
2007-03-07 19:50:38 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<tracker_reply_alert>())
|
2005-03-10 10:59:12 +01:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(tracker_reply_alert(
|
2008-07-06 14:22:56 +02:00
|
|
|
get_handle(), peer_list.size(), r.url));
|
2005-03-10 10:59:12 +01:00
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
m_got_tracker_response = true;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void torrent::on_peer_name_lookup(error_code const& e, tcp::resolver::iterator host
|
2008-04-07 04:51:21 +02:00
|
|
|
, peer_id pid)
|
2007-03-07 19:50:38 +01:00
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
if (e || host == tcp::resolver::iterator() ||
|
|
|
|
m_ses.is_aborted()) return;
|
|
|
|
|
|
|
|
if (m_ses.m_ip_filter.access(host->endpoint().address()) & ip_filter::blocked)
|
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2008-10-23 06:10:23 +02:00
|
|
|
error_code ec;
|
|
|
|
debug_log("blocked ip from tracker: " + host->endpoint().address().to_string(ec));
|
2007-03-07 19:50:38 +01:00
|
|
|
#endif
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<peer_blocked_alert>())
|
2007-04-17 07:56:43 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
m_ses.m_alerts.post_alert(peer_blocked_alert(host->endpoint().address()));
|
2007-04-17 07:56:43 +02:00
|
|
|
}
|
|
|
|
|
2007-03-07 19:50:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
m_policy.peer_from_tracker(*host, pid, peer_info::tracker, 0);
|
2007-03-07 19:50:38 +01:00
|
|
|
}
|
|
|
|
|
2004-01-18 02:58:33 +01:00
|
|
|
size_type torrent::bytes_left() const
|
2003-12-07 06:53:04 +01:00
|
|
|
{
|
2004-06-14 01:30:42 +02:00
|
|
|
// if we don't have the metadata yet, we
|
|
|
|
// cannot tell how big the torrent is.
|
|
|
|
if (!valid_metadata()) return -1;
|
2007-09-01 05:00:31 +02:00
|
|
|
return m_torrent_file->total_size()
|
2006-09-24 02:48:31 +02:00
|
|
|
- quantized_bytes_done();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type torrent::quantized_bytes_done() const
|
|
|
|
{
|
2006-12-11 13:48:33 +01:00
|
|
|
// INVARIANT_CHECK;
|
2006-09-24 02:48:31 +02:00
|
|
|
|
|
|
|
if (!valid_metadata()) return 0;
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
if (m_torrent_file->num_pieces() == 0)
|
2006-09-24 02:48:31 +02:00
|
|
|
return 0;
|
2006-12-04 13:20:34 +01:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
if (is_seed()) return m_torrent_file->total_size();
|
2006-12-04 13:20:34 +01:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
const int last_piece = m_torrent_file->num_pieces() - 1;
|
2006-09-24 02:48:31 +02:00
|
|
|
|
|
|
|
size_type total_done
|
2008-06-07 16:03:21 +02:00
|
|
|
= size_type(num_have()) * m_torrent_file->piece_length();
|
2006-09-24 02:48:31 +02:00
|
|
|
|
|
|
|
// if we have the last piece, we have to correct
|
|
|
|
// the amount we have, since the first calculation
|
|
|
|
// assumed all pieces were of equal size
|
2008-06-07 04:58:28 +02:00
|
|
|
if (m_picker->have_piece(last_piece))
|
2006-09-24 02:48:31 +02:00
|
|
|
{
|
2007-09-01 05:00:31 +02:00
|
|
|
int corr = m_torrent_file->piece_size(last_piece)
|
|
|
|
- m_torrent_file->piece_length();
|
2006-09-24 02:48:31 +02:00
|
|
|
total_done += corr;
|
|
|
|
}
|
|
|
|
return total_done;
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
// the first value is the total number of bytes downloaded
|
|
|
|
// the second value is the number of bytes of those that haven't
|
|
|
|
// been filtered as not wanted we have downloaded
|
2006-01-07 14:48:14 +01:00
|
|
|
tuple<size_type, size_type> torrent::bytes_done() const
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2006-05-28 21:03:54 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
if (!valid_metadata() || m_torrent_file->num_pieces() == 0)
|
2006-01-07 14:48:14 +01:00
|
|
|
return tuple<size_type, size_type>(0,0);
|
2007-03-17 00:28:26 +01:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
const int last_piece = m_torrent_file->num_pieces() - 1;
|
2007-11-27 00:08:59 +01:00
|
|
|
const int piece_size = m_torrent_file->piece_length();
|
2004-01-15 17:45:34 +01:00
|
|
|
|
2006-12-04 13:20:34 +01:00
|
|
|
if (is_seed())
|
2007-09-01 05:00:31 +02:00
|
|
|
return make_tuple(m_torrent_file->total_size()
|
|
|
|
, m_torrent_file->total_size());
|
2006-12-04 13:20:34 +01:00
|
|
|
|
2008-06-07 16:03:21 +02:00
|
|
|
TORRENT_ASSERT(num_have() >= m_picker->num_have_filtered());
|
|
|
|
size_type wanted_done = size_type(num_have() - m_picker->num_have_filtered())
|
2007-11-27 00:08:59 +01:00
|
|
|
* piece_size;
|
2007-12-09 20:38:17 +01:00
|
|
|
TORRENT_ASSERT(wanted_done >= 0);
|
2005-05-30 19:43:03 +02:00
|
|
|
|
2004-01-18 02:58:33 +01:00
|
|
|
size_type total_done
|
2008-06-07 16:03:21 +02:00
|
|
|
= size_type(num_have()) * piece_size;
|
|
|
|
TORRENT_ASSERT(num_have() < m_torrent_file->num_pieces());
|
2004-01-15 17:45:34 +01:00
|
|
|
|
|
|
|
// if we have the last piece, we have to correct
|
|
|
|
// the amount we have, since the first calculation
|
|
|
|
// assumed all pieces were of equal size
|
2008-06-07 04:58:28 +02:00
|
|
|
if (m_picker->have_piece(last_piece))
|
2003-12-07 06:53:04 +01:00
|
|
|
{
|
2007-11-27 00:08:59 +01:00
|
|
|
TORRENT_ASSERT(total_done >= piece_size);
|
2007-09-01 05:00:31 +02:00
|
|
|
int corr = m_torrent_file->piece_size(last_piece)
|
2007-11-27 00:08:59 +01:00
|
|
|
- piece_size;
|
|
|
|
TORRENT_ASSERT(corr <= 0);
|
|
|
|
TORRENT_ASSERT(corr > -piece_size);
|
2005-05-30 19:43:03 +02:00
|
|
|
total_done += corr;
|
2007-03-15 23:03:56 +01:00
|
|
|
if (m_picker->piece_priority(last_piece) != 0)
|
2007-11-27 00:08:59 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(wanted_done >= piece_size);
|
2005-05-30 19:43:03 +02:00
|
|
|
wanted_done += corr;
|
2007-11-27 00:08:59 +01:00
|
|
|
}
|
2003-12-07 06:53:04 +01:00
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(total_done <= m_torrent_file->total_size());
|
|
|
|
TORRENT_ASSERT(wanted_done <= m_torrent_file->total_size());
|
2007-12-09 20:38:17 +01:00
|
|
|
TORRENT_ASSERT(total_done >= wanted_done);
|
2006-12-11 13:48:33 +01:00
|
|
|
|
2004-01-15 17:45:34 +01:00
|
|
|
const std::vector<piece_picker::downloading_piece>& dl_queue
|
2004-06-14 01:30:42 +02:00
|
|
|
= m_picker->get_download_queue();
|
2004-01-15 17:45:34 +01:00
|
|
|
|
2008-04-05 23:18:27 +02:00
|
|
|
const int blocks_per_piece = piece_size / m_block_size;
|
2004-01-15 17:45:34 +01:00
|
|
|
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i =
|
2005-05-30 19:43:03 +02:00
|
|
|
dl_queue.begin(); i != dl_queue.end(); ++i)
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2005-05-30 19:43:03 +02:00
|
|
|
int corr = 0;
|
2006-12-31 15:48:18 +01:00
|
|
|
int index = i->index;
|
2008-06-07 04:58:28 +02:00
|
|
|
if (m_picker->have_piece(index)) continue;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->finished <= m_picker->blocks_in_piece(index));
|
2006-12-14 17:12:31 +01:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-12-14 17:12:31 +01:00
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator j = boost::next(i);
|
|
|
|
j != dl_queue.end(); ++j)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(j->index != index);
|
2006-12-14 17:12:31 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-01-15 17:45:34 +01:00
|
|
|
|
|
|
|
for (int j = 0; j < blocks_per_piece; ++j)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker->is_finished(piece_block(index, j)) == (i->info[j].state == piece_picker::block_info::state_finished));
|
2007-06-10 22:46:09 +02:00
|
|
|
corr += (i->info[j].state == piece_picker::block_info::state_finished) * m_block_size;
|
2007-11-27 00:08:59 +01:00
|
|
|
TORRENT_ASSERT(corr >= 0);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index != last_piece || j < m_picker->blocks_in_last_piece()
|
2007-06-10 22:46:09 +02:00
|
|
|
|| i->info[j].state != piece_picker::block_info::state_finished);
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// correction if this was the last piece
|
|
|
|
// and if we have the last block
|
|
|
|
if (i->index == last_piece
|
2007-06-10 22:46:09 +02:00
|
|
|
&& i->info[m_picker->blocks_in_last_piece()-1].state
|
|
|
|
== piece_picker::block_info::state_finished)
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2005-05-30 19:43:03 +02:00
|
|
|
corr -= m_block_size;
|
2007-09-01 05:00:31 +02:00
|
|
|
corr += m_torrent_file->piece_size(last_piece) % m_block_size;
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
2005-05-30 19:43:03 +02:00
|
|
|
total_done += corr;
|
2007-03-15 23:03:56 +01:00
|
|
|
if (m_picker->piece_priority(index) != 0)
|
2005-05-30 19:43:03 +02:00
|
|
|
wanted_done += corr;
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(total_done <= m_torrent_file->total_size());
|
|
|
|
TORRENT_ASSERT(wanted_done <= m_torrent_file->total_size());
|
2006-12-15 13:29:47 +01:00
|
|
|
|
2004-09-16 03:14:16 +02:00
|
|
|
std::map<piece_block, int> downloading_piece;
|
|
|
|
for (const_peer_iterator i = begin(); i != end(); ++i)
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_connection* pc = *i;
|
2004-01-15 17:45:34 +01:00
|
|
|
boost::optional<piece_block_progress> p
|
2006-04-25 23:04:48 +02:00
|
|
|
= pc->downloading_piece_progress();
|
2004-01-15 17:45:34 +01:00
|
|
|
if (p)
|
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
if (m_picker->have_piece(p->piece_index))
|
2004-01-15 17:45:34 +01:00
|
|
|
continue;
|
2004-09-16 03:14:16 +02:00
|
|
|
|
|
|
|
piece_block block(p->piece_index, p->block_index);
|
|
|
|
if (m_picker->is_finished(block))
|
2004-01-15 17:45:34 +01:00
|
|
|
continue;
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2004-09-16 03:14:16 +02:00
|
|
|
std::map<piece_block, int>::iterator dp
|
|
|
|
= downloading_piece.find(block);
|
|
|
|
if (dp != downloading_piece.end())
|
|
|
|
{
|
|
|
|
if (dp->second < p->bytes_downloaded)
|
|
|
|
dp->second = p->bytes_downloaded;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
downloading_piece[block] = p->bytes_downloaded;
|
|
|
|
}
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->bytes_downloaded <= p->full_block_bytes);
|
2007-09-01 05:00:31 +02:00
|
|
|
int last_piece = m_torrent_file->num_pieces() - 1;
|
2006-12-18 02:23:30 +01:00
|
|
|
if (p->piece_index == last_piece
|
2007-09-01 05:00:31 +02:00
|
|
|
&& p->block_index == m_torrent_file->piece_size(last_piece) / block_size())
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->full_block_bytes == m_torrent_file->piece_size(last_piece) % block_size());
|
2006-12-18 02:23:30 +01:00
|
|
|
else
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->full_block_bytes == block_size());
|
2006-12-18 02:23:30 +01:00
|
|
|
#endif
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
|
|
|
}
|
2004-09-16 03:14:16 +02:00
|
|
|
for (std::map<piece_block, int>::iterator i = downloading_piece.begin();
|
|
|
|
i != downloading_piece.end(); ++i)
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2004-09-16 03:14:16 +02:00
|
|
|
total_done += i->second;
|
2007-03-15 23:03:56 +01:00
|
|
|
if (m_picker->piece_priority(i->first.piece_index) != 0)
|
2005-05-30 19:43:03 +02:00
|
|
|
wanted_done += i->second;
|
|
|
|
}
|
2006-12-15 13:29:47 +01:00
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
TORRENT_ASSERT(total_done <= m_torrent_file->total_size());
|
|
|
|
TORRENT_ASSERT(wanted_done <= m_torrent_file->total_size());
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-12-18 02:23:30 +01:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
if (total_done >= m_torrent_file->total_size())
|
2006-12-18 02:23:30 +01:00
|
|
|
{
|
2007-10-01 04:09:12 +02:00
|
|
|
// Thist happens when a piece has been downloaded completely
|
|
|
|
// but not yet verified against the hash
|
2008-06-07 16:03:21 +02:00
|
|
|
std::cerr << "num_have: " << num_have() << std::endl;
|
2006-12-18 02:23:30 +01:00
|
|
|
|
|
|
|
std::cerr << "unfinished:" << std::endl;
|
|
|
|
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i =
|
|
|
|
dl_queue.begin(); i != dl_queue.end(); ++i)
|
|
|
|
{
|
|
|
|
std::cerr << " " << i->index << " ";
|
|
|
|
for (int j = 0; j < blocks_per_piece; ++j)
|
|
|
|
{
|
2007-06-10 22:46:09 +02:00
|
|
|
std::cerr << (i->info[j].state == piece_picker::block_info::state_finished ? "1" : "0");
|
2006-12-18 02:23:30 +01:00
|
|
|
}
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "downloading pieces:" << std::endl;
|
|
|
|
|
|
|
|
for (std::map<piece_block, int>::iterator i = downloading_piece.begin();
|
|
|
|
i != downloading_piece.end(); ++i)
|
|
|
|
{
|
|
|
|
std::cerr << " " << i->first.piece_index << ":" << i->first.block_index
|
|
|
|
<< " " << i->second << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(total_done <= m_torrent_file->total_size());
|
|
|
|
TORRENT_ASSERT(wanted_done <= m_torrent_file->total_size());
|
2006-12-18 02:23:30 +01:00
|
|
|
|
|
|
|
#endif
|
2006-12-15 13:29:47 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(total_done >= wanted_done);
|
2006-01-07 14:48:14 +01:00
|
|
|
return make_tuple(total_done, wanted_done);
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2008-04-13 00:08:07 +02:00
|
|
|
// passed_hash_check
|
|
|
|
// 0: success, piece passed check
|
|
|
|
// -1: disk failure
|
|
|
|
// -2: piece failed check
|
|
|
|
void torrent::piece_finished(int index, int passed_hash_check)
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2007-10-18 02:32:16 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
|
|
|
(*m_ses.m_logger) << time_now_string() << " *** PIECE_FINISHED [ p: "
|
2008-04-20 19:17:58 +02:00
|
|
|
<< index << " chk: " << ((passed_hash_check == 0)
|
|
|
|
?"passed":passed_hash_check == -1
|
|
|
|
?"disk failed":"failed") << " ]\n";
|
2007-10-18 02:32:16 +02:00
|
|
|
#endif
|
|
|
|
|
2008-06-23 20:54:58 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-04-13 00:08:07 +02:00
|
|
|
if (passed_hash_check == 0)
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
|
|
|
// the following call may cause picker to become invalid
|
|
|
|
// in case we just became a seed
|
2008-06-23 20:54:58 +02:00
|
|
|
piece_passed(index);
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
2008-04-13 00:08:07 +02:00
|
|
|
else if (passed_hash_check == -2)
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
2008-06-22 22:28:03 +02:00
|
|
|
// piece_failed() will restore the piece
|
2007-06-10 22:46:09 +02:00
|
|
|
piece_failed(index);
|
|
|
|
}
|
2008-04-13 00:08:07 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(passed_hash_check == -1);
|
|
|
|
m_picker->restore_piece(index);
|
2008-06-23 16:45:01 +02:00
|
|
|
restore_piece_state(index);
|
2008-04-13 00:08:07 +02:00
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
|
|
|
|
2008-06-23 20:54:58 +02:00
|
|
|
void torrent::piece_passed(int index)
|
|
|
|
{
|
|
|
|
// INVARIANT_CHECK;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<piece_finished_alert>())
|
2008-06-23 20:54:58 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(piece_finished_alert(get_handle()
|
2008-07-06 14:22:56 +02:00
|
|
|
, index));
|
2008-06-23 20:54:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool was_finished = m_picker->num_filtered() + num_have()
|
|
|
|
== torrent_file().num_pieces();
|
|
|
|
|
|
|
|
std::vector<void*> downloaders;
|
|
|
|
m_picker->get_downloaders(downloaders, index);
|
|
|
|
|
|
|
|
// increase the trust point of all peers that sent
|
|
|
|
// parts of this piece.
|
|
|
|
std::set<void*> peers;
|
|
|
|
std::copy(downloaders.begin(), downloaders.end(), std::inserter(peers, peers.begin()));
|
|
|
|
|
|
|
|
m_picker->we_have(index);
|
2008-07-18 12:03:42 +02:00
|
|
|
for (peer_iterator i = m_connections.begin(); i != m_connections.end();)
|
|
|
|
{
|
|
|
|
peer_connection* p = *i;
|
|
|
|
++i;
|
|
|
|
p->announce_piece(index);
|
|
|
|
}
|
2008-06-23 20:54:58 +02:00
|
|
|
|
|
|
|
for (std::set<void*>::iterator i = peers.begin()
|
|
|
|
, end(peers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
policy::peer* p = static_cast<policy::peer*>(*i);
|
|
|
|
if (p == 0) continue;
|
|
|
|
p->on_parole = false;
|
|
|
|
++p->trust_points;
|
|
|
|
// TODO: make this limit user settable
|
|
|
|
if (p->trust_points > 20) p->trust_points = 20;
|
|
|
|
if (p->connection) p->connection->received_valid_data(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
(*i)->on_piece_pass(index);
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// since this piece just passed, we might have
|
|
|
|
// become uninterested in some peers where this
|
|
|
|
// was the last piece we were interested in
|
2008-09-18 01:14:59 +02:00
|
|
|
for (peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end();)
|
2008-06-23 20:54:58 +02:00
|
|
|
{
|
|
|
|
peer_connection* p = *i;
|
2008-09-18 01:14:59 +02:00
|
|
|
// update_interest may disconnect the peer and
|
|
|
|
// invalidate the iterator
|
|
|
|
++i;
|
2008-06-23 20:54:58 +02:00
|
|
|
// if we're not interested already, no need to check
|
|
|
|
if (!p->is_interesting()) continue;
|
|
|
|
// if the peer doesn't have the piece we just got, it
|
|
|
|
// wouldn't affect our interest
|
|
|
|
if (!p->has_piece(index)) continue;
|
|
|
|
p->update_interest();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!was_finished && is_finished())
|
|
|
|
{
|
|
|
|
// torrent finished
|
|
|
|
// i.e. all the pieces we're interested in have
|
|
|
|
// been downloaded. Release the files (they will open
|
|
|
|
// in read only mode if needed)
|
|
|
|
finished();
|
|
|
|
// if we just became a seed, picker is now invalid, since it
|
|
|
|
// is deallocated by the torrent once it starts seeding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-01 22:27:27 +01:00
|
|
|
void torrent::piece_failed(int index)
|
|
|
|
{
|
2006-12-14 17:12:31 +01:00
|
|
|
// if the last piece fails the peer connection will still
|
|
|
|
// think that it has received all of it until this function
|
|
|
|
// resets the download queue. So, we cannot do the
|
|
|
|
// invariant check here since it assumes:
|
2007-09-01 05:00:31 +02:00
|
|
|
// (total_done == m_torrent_file->total_size()) => is_seed()
|
2007-10-31 10:48:20 +01:00
|
|
|
INVARIANT_CHECK;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_storage);
|
|
|
|
TORRENT_ASSERT(m_storage->refcount() > 0);
|
|
|
|
TORRENT_ASSERT(m_picker.get());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
2004-01-25 13:37:15 +01:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<hash_failed_alert>())
|
|
|
|
m_ses.m_alerts.post_alert(hash_failed_alert(get_handle(), index));
|
|
|
|
|
2004-04-18 14:28:02 +02:00
|
|
|
// increase the total amount of failed bytes
|
2008-07-11 09:30:04 +02:00
|
|
|
add_failed_bytes(m_torrent_file->piece_size(index));
|
2004-04-18 14:28:02 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
std::vector<void*> downloaders;
|
2004-06-14 01:30:42 +02:00
|
|
|
m_picker->get_downloaders(downloaders, index);
|
2003-12-01 22:27:27 +01:00
|
|
|
|
|
|
|
// decrease the trust point of all peers that sent
|
|
|
|
// parts of this piece.
|
2004-09-12 12:12:16 +02:00
|
|
|
// first, build a set of all peers that participated
|
2007-07-04 04:16:49 +02:00
|
|
|
std::set<void*> peers;
|
2004-09-12 12:12:16 +02:00
|
|
|
std::copy(downloaders.begin(), downloaders.end(), std::inserter(peers, peers.begin()));
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-18 07:04:54 +01:00
|
|
|
for (std::vector<void*>::iterator i = downloaders.begin()
|
|
|
|
, end(downloaders.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
policy::peer* p = (policy::peer*)*i;
|
|
|
|
if (p && p->connection)
|
|
|
|
{
|
|
|
|
p->connection->piece_failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
(*i)->on_piece_failed(index);
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
for (std::set<void*>::iterator i = peers.begin()
|
2004-09-12 12:12:16 +02:00
|
|
|
, end(peers.end()); i != end; ++i)
|
2003-12-01 22:27:27 +01:00
|
|
|
{
|
2007-07-04 04:16:49 +02:00
|
|
|
policy::peer* p = static_cast<policy::peer*>(*i);
|
|
|
|
if (p == 0) continue;
|
|
|
|
if (p->connection) p->connection->received_invalid_data(index);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2004-09-12 12:12:16 +02:00
|
|
|
// either, we have received too many failed hashes
|
|
|
|
// or this was the only peer that sent us this piece.
|
2006-04-25 23:04:48 +02:00
|
|
|
// TODO: make this a changable setting
|
2007-07-04 04:16:49 +02:00
|
|
|
if (p->trust_points <= -7
|
2007-05-25 21:42:10 +02:00
|
|
|
|| peers.size() == 1)
|
2003-12-01 22:27:27 +01:00
|
|
|
{
|
2003-12-14 06:56:12 +01:00
|
|
|
// we don't trust this peer anymore
|
|
|
|
// ban it.
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<peer_ban_alert>())
|
2004-03-21 03:03:37 +01:00
|
|
|
{
|
2008-08-19 16:53:50 +02:00
|
|
|
peer_id pid(0);
|
2008-07-08 11:30:10 +02:00
|
|
|
if (p->connection) pid = p->connection->pid();
|
2004-03-21 03:03:37 +01:00
|
|
|
m_ses.m_alerts.post_alert(peer_ban_alert(
|
2008-07-14 13:15:35 +02:00
|
|
|
get_handle(), p->ip(), pid));
|
2004-03-21 03:03:37 +01:00
|
|
|
}
|
2007-04-13 03:53:25 +02:00
|
|
|
|
|
|
|
// mark the peer as banned
|
2007-07-04 04:16:49 +02:00
|
|
|
p->banned = true;
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
if (p->connection)
|
|
|
|
{
|
2007-12-19 22:36:54 +01:00
|
|
|
#ifdef TORRENT_LOGGING
|
2008-07-14 13:15:35 +02:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " *** BANNING PEER [ " << p->ip()
|
2007-12-19 22:36:54 +01:00
|
|
|
<< " ] 'too many corrupt pieces'\n";
|
2008-02-07 08:09:52 +01:00
|
|
|
#endif
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2008-07-14 13:15:35 +02:00
|
|
|
(*p->connection->m_logger) << "*** BANNING PEER [ " << p->ip()
|
2007-07-04 04:16:49 +02:00
|
|
|
<< " ] 'too many corrupt pieces'\n";
|
2005-09-01 23:04:21 +02:00
|
|
|
#endif
|
2008-01-07 02:10:46 +01:00
|
|
|
p->connection->disconnect("too many corrupt pieces, banning peer");
|
2007-07-04 04:16:49 +02:00
|
|
|
}
|
2003-12-01 22:27:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have to let the piece_picker know that
|
|
|
|
// this piece failed the check as it can restore it
|
|
|
|
// and mark it as being interesting for download
|
|
|
|
// TODO: do this more intelligently! and keep track
|
|
|
|
// of how much crap (data that failed hash-check) and
|
|
|
|
// how much redundant data we have downloaded
|
|
|
|
// if some clients has sent more than one piece
|
|
|
|
// start with redownloading the pieces that the client
|
|
|
|
// that has sent the least number of pieces
|
2004-06-14 01:30:42 +02:00
|
|
|
m_picker->restore_piece(index);
|
2008-06-23 16:45:01 +02:00
|
|
|
restore_piece_state(index);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_storage);
|
2004-01-09 11:50:22 +01:00
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
TORRENT_ASSERT(m_picker->have_piece(index) == false);
|
2007-12-18 07:04:54 +01:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-18 07:04:54 +01:00
|
|
|
for (std::vector<void*>::iterator i = downloaders.begin()
|
|
|
|
, end(downloaders.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
policy::peer* p = (policy::peer*)*i;
|
|
|
|
if (p && p->connection)
|
|
|
|
{
|
|
|
|
p->connection->piece_failed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2003-12-01 22:27:27 +01:00
|
|
|
}
|
|
|
|
|
2008-06-23 16:45:01 +02:00
|
|
|
void torrent::restore_piece_state(int index)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(has_picker());
|
|
|
|
for (peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end(); ++i)
|
|
|
|
{
|
|
|
|
peer_connection* p = *i;
|
2008-07-07 14:04:06 +02:00
|
|
|
std::deque<pending_block> const& dq = p->download_queue();
|
2008-06-23 16:45:01 +02:00
|
|
|
std::deque<piece_block> const& rq = p->request_queue();
|
2008-07-07 14:04:06 +02:00
|
|
|
for (std::deque<pending_block>::const_iterator k = dq.begin()
|
2008-06-23 16:45:01 +02:00
|
|
|
, end(dq.end()); k != end; ++k)
|
|
|
|
{
|
2008-07-07 14:04:06 +02:00
|
|
|
if (k->block.piece_index != index) continue;
|
|
|
|
m_picker->mark_as_downloading(k->block, p->peer_info_struct()
|
2008-06-23 16:45:01 +02:00
|
|
|
, (piece_picker::piece_state_t)p->peer_speed());
|
|
|
|
}
|
|
|
|
for (std::deque<piece_block>::const_iterator k = rq.begin()
|
|
|
|
, end(rq.end()); k != end; ++k)
|
|
|
|
{
|
|
|
|
if (k->piece_index != index) continue;
|
|
|
|
m_picker->mark_as_downloading(*k, p->peer_info_struct()
|
|
|
|
, (piece_picker::piece_state_t)p->peer_speed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-05 15:17:17 +01:00
|
|
|
void torrent::abort()
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2005-03-05 15:17:17 +01:00
|
|
|
m_abort = true;
|
2006-04-25 23:04:48 +02:00
|
|
|
// if the torrent is paused, it doesn't need
|
|
|
|
// to announce with even=stopped again.
|
2008-06-29 21:08:30 +02:00
|
|
|
if (!is_paused())
|
2008-07-12 10:25:19 +02:00
|
|
|
{
|
|
|
|
stop_announcing();
|
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-08-16 14:41:46 +02:00
|
|
|
for (peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end(); ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
(*(*i)->m_logger) << "*** ABORTING TORRENT\n";
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
// disconnect all peers and close all
|
|
|
|
// files belonging to the torrents
|
2005-03-05 15:17:17 +01:00
|
|
|
disconnect_all();
|
2007-10-02 20:34:34 +02:00
|
|
|
if (m_owning_storage.get())
|
2008-11-17 02:19:46 +01:00
|
|
|
{
|
2007-10-02 20:34:34 +02:00
|
|
|
m_storage->async_release_files(
|
|
|
|
bind(&torrent::on_files_released, shared_from_this(), _1, _2));
|
2008-11-17 02:19:46 +01:00
|
|
|
m_storage->abort_disk_io();
|
|
|
|
}
|
2008-10-26 10:23:00 +01:00
|
|
|
|
|
|
|
if (m_state == torrent_status::checking_files)
|
2008-11-19 01:46:48 +01:00
|
|
|
{
|
2008-10-26 10:23:00 +01:00
|
|
|
m_ses.done_checking(shared_from_this());
|
2008-11-19 01:46:48 +01:00
|
|
|
set_state(torrent_status::queued_for_checking);
|
|
|
|
}
|
2008-10-26 10:23:00 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
m_owning_storage = 0;
|
2007-11-11 20:09:29 +01:00
|
|
|
m_host_resolver.cancel();
|
2005-03-05 15:17:17 +01:00
|
|
|
}
|
2003-12-01 22:27:27 +01:00
|
|
|
|
2008-12-08 07:36:22 +01:00
|
|
|
void torrent::super_seeding(bool on)
|
|
|
|
{
|
|
|
|
if (on == m_super_seeding) return;
|
|
|
|
|
|
|
|
// don't turn on super seeding if we're not a seed
|
|
|
|
TORRENT_ASSERT(!on || is_seed() || !m_files_checked);
|
|
|
|
if (on && !is_seed() && m_files_checked) return;
|
|
|
|
m_super_seeding = on;
|
|
|
|
|
|
|
|
if (m_super_seeding) return;
|
|
|
|
|
|
|
|
// disable super seeding for all peers
|
|
|
|
for (peer_iterator i = begin(); i != end(); ++i)
|
|
|
|
{
|
|
|
|
(*i)->superseed_piece(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int torrent::get_piece_to_super_seed(bitfield const& bits)
|
|
|
|
{
|
|
|
|
// return a piece with low availability that is not in
|
|
|
|
// the bitfield and that is not currently being super
|
|
|
|
// seeded by any peer
|
|
|
|
TORRENT_ASSERT(m_super_seeding);
|
|
|
|
|
|
|
|
// do a linear search from the first piece
|
|
|
|
int min_availability = 9999;
|
|
|
|
std::vector<int> avail_vec;
|
|
|
|
for (int i = 0; i < m_torrent_file->num_pieces(); ++i)
|
|
|
|
{
|
|
|
|
if (bits[i]) continue;
|
|
|
|
|
|
|
|
int availability = 0;
|
|
|
|
for (const_peer_iterator j = begin(); j != end(); ++j)
|
|
|
|
{
|
|
|
|
if ((*j)->superseed_piece() == i)
|
|
|
|
{
|
|
|
|
// avoid superseeding the same piece to more than one
|
|
|
|
// peer if we can avoid it. Do this by artificially
|
|
|
|
// increase the availability
|
|
|
|
availability = 999;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((*j)->has_piece(i)) ++availability;
|
|
|
|
}
|
|
|
|
if (availability > min_availability) continue;
|
|
|
|
if (availability == min_availability)
|
|
|
|
{
|
|
|
|
avail_vec.push_back(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(availability < min_availability);
|
|
|
|
min_availability = availability;
|
|
|
|
avail_vec.clear();
|
|
|
|
avail_vec.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_availability > 1)
|
|
|
|
{
|
|
|
|
// if the minimum availability is 2 or more,
|
|
|
|
// we shouldn't be super seeding any more
|
|
|
|
super_seeding(false);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return avail_vec[rand() % avail_vec.size()];
|
|
|
|
}
|
|
|
|
|
2007-10-13 05:33:33 +02:00
|
|
|
void torrent::on_files_deleted(int ret, disk_io_job const& j)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (ret != 0)
|
2007-10-13 05:33:33 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<torrent_delete_failed_alert>())
|
|
|
|
alerts().post_alert(torrent_delete_failed_alert(get_handle(), j.str));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (alerts().should_post<torrent_deleted_alert>())
|
|
|
|
alerts().post_alert(torrent_deleted_alert(get_handle()));
|
2007-10-13 05:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-11 23:24:14 +02:00
|
|
|
void torrent::on_files_released(int ret, disk_io_job const& j)
|
2007-10-02 20:34:34 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<torrent_paused_alert>())
|
2007-10-02 20:34:34 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
alerts().post_alert(torrent_paused_alert(get_handle()));
|
2007-10-02 20:34:34 +02:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
void torrent::on_save_resume_data(int ret, disk_io_job const& j)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (!j.resume_data && alerts().should_post<save_resume_data_failed_alert>())
|
2008-04-13 20:54:36 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
alerts().post_alert(save_resume_data_failed_alert(get_handle(), j.str));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j.resume_data && alerts().should_post<save_resume_data_alert>())
|
|
|
|
{
|
|
|
|
write_resume_data(*j.resume_data);
|
2008-04-13 20:54:36 +02:00
|
|
|
alerts().post_alert(save_resume_data_alert(j.resume_data
|
2008-07-06 14:22:56 +02:00
|
|
|
, get_handle()));
|
2008-04-13 20:54:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
void torrent::on_file_renamed(int ret, disk_io_job const& j)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
{
|
|
|
|
if (ret == 0)
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
|
|
|
if (alerts().should_post<file_renamed_alert>())
|
|
|
|
alerts().post_alert(file_renamed_alert(get_handle(), j.str, j.piece));
|
2008-10-01 01:37:42 +02:00
|
|
|
m_torrent_file->files().rename_file(j.piece, j.str);
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
2008-05-28 10:44:40 +02:00
|
|
|
else
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
|
|
|
if (alerts().should_post<file_rename_failed_alert>())
|
|
|
|
alerts().post_alert(file_rename_failed_alert(get_handle(), j.str, j.piece));
|
|
|
|
}
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-02 20:34:34 +02:00
|
|
|
void torrent::on_torrent_paused(int ret, disk_io_job const& j)
|
2007-06-11 23:24:14 +02:00
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<torrent_paused_alert>())
|
|
|
|
alerts().post_alert(torrent_paused_alert(get_handle()));
|
2007-06-11 23:24:14 +02:00
|
|
|
}
|
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
std::string torrent::tracker_login() const
|
2004-02-22 23:40:45 +01:00
|
|
|
{
|
|
|
|
if (m_username.empty() && m_password.empty()) return "";
|
|
|
|
return m_username + ":" + m_password;
|
|
|
|
}
|
|
|
|
|
2007-05-30 08:52:59 +02:00
|
|
|
void torrent::piece_availability(std::vector<int>& avail) const
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-03-20 02:59:00 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2007-05-30 08:52:59 +02:00
|
|
|
if (is_seed())
|
|
|
|
{
|
|
|
|
avail.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_picker->get_availability(avail);
|
|
|
|
}
|
2007-03-20 02:59:00 +01:00
|
|
|
|
|
|
|
void torrent::set_piece_priority(int index, int priority)
|
|
|
|
{
|
2007-08-09 00:38:30 +02:00
|
|
|
// INVARIANT_CHECK;
|
2007-03-20 02:59:00 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2007-03-20 02:59:00 +01:00
|
|
|
if (is_seed()) return;
|
|
|
|
|
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
2007-03-20 02:59:00 +01:00
|
|
|
|
2008-04-13 09:39:37 +02:00
|
|
|
bool was_finished = is_finished();
|
2007-08-10 23:13:35 +02:00
|
|
|
bool filter_updated = m_picker->set_piece_priority(index, priority);
|
2008-06-07 16:03:21 +02:00
|
|
|
TORRENT_ASSERT(num_have() >= m_picker->num_have_filtered());
|
2008-04-13 09:39:37 +02:00
|
|
|
if (filter_updated) update_peer_interest(was_finished);
|
2007-03-20 02:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int torrent::piece_priority(int index) const
|
|
|
|
{
|
2007-08-09 00:38:30 +02:00
|
|
|
// INVARIANT_CHECK;
|
2007-03-20 02:59:00 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2007-03-20 02:59:00 +01:00
|
|
|
if (is_seed()) return 1;
|
|
|
|
|
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
2007-03-20 02:59:00 +01:00
|
|
|
|
|
|
|
return m_picker->piece_priority(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::prioritize_pieces(std::vector<int> const& pieces)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2007-03-20 02:59:00 +01:00
|
|
|
if (is_seed()) return;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
2007-03-20 02:59:00 +01:00
|
|
|
|
|
|
|
int index = 0;
|
2007-08-10 23:13:35 +02:00
|
|
|
bool filter_updated = false;
|
2008-04-13 09:39:37 +02:00
|
|
|
bool was_finished = is_finished();
|
2007-03-20 02:59:00 +01:00
|
|
|
for (std::vector<int>::const_iterator i = pieces.begin()
|
|
|
|
, end(pieces.end()); i != end; ++i, ++index)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*i >= 0);
|
|
|
|
TORRENT_ASSERT(*i <= 7);
|
2007-08-10 23:13:35 +02:00
|
|
|
filter_updated |= m_picker->set_piece_priority(index, *i);
|
2008-06-07 16:03:21 +02:00
|
|
|
TORRENT_ASSERT(num_have() >= m_picker->num_have_filtered());
|
2007-03-20 02:59:00 +01:00
|
|
|
}
|
2008-04-13 09:39:37 +02:00
|
|
|
if (filter_updated) update_peer_interest(was_finished);
|
2007-03-20 02:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::piece_priorities(std::vector<int>& pieces) const
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2007-03-20 02:59:00 +01:00
|
|
|
if (is_seed())
|
|
|
|
{
|
|
|
|
pieces.clear();
|
2007-09-01 05:00:31 +02:00
|
|
|
pieces.resize(m_torrent_file->num_pieces(), 1);
|
2007-03-20 02:59:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
2007-03-20 02:59:00 +01:00
|
|
|
m_picker->piece_priorities(pieces);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void set_if_greater(int& piece_prio, int file_prio)
|
|
|
|
{
|
|
|
|
if (file_prio > piece_prio) piece_prio = file_prio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::prioritize_files(std::vector<int> const& files)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
// this call is only valid on torrents with metadata
|
|
|
|
if (!valid_metadata() || is_seed()) return;
|
|
|
|
|
|
|
|
// the bitmask need to have exactly one bit for every file
|
|
|
|
// in the torrent
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(int(files.size()) == m_torrent_file->num_files());
|
2007-03-20 02:59:00 +01:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
if (m_torrent_file->num_pieces() == 0) return;
|
2007-03-21 03:09:50 +01:00
|
|
|
|
2008-07-20 18:00:08 +02:00
|
|
|
std::copy(files.begin(), files.end(), m_file_priority.begin());
|
|
|
|
update_piece_priorities();
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::set_file_priority(int index, int prio)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_files());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
if (m_file_priority[index] == prio) return;
|
|
|
|
m_file_priority[index] = prio;
|
|
|
|
update_piece_priorities();
|
|
|
|
}
|
|
|
|
|
|
|
|
int torrent::file_priority(int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_files());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
return m_file_priority[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::file_priorities(std::vector<int>& files) const
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
files.resize(m_file_priority.size());
|
|
|
|
std::copy(m_file_priority.begin(), m_file_priority.end(), files.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::update_piece_priorities()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
if (m_torrent_file->num_pieces() == 0) return;
|
2008-04-13 09:39:37 +02:00
|
|
|
|
2008-07-20 18:00:08 +02:00
|
|
|
size_type position = 0;
|
2007-09-01 05:00:31 +02:00
|
|
|
int piece_length = m_torrent_file->piece_length();
|
2007-03-21 03:09:50 +01:00
|
|
|
// initialize the piece priorities to 0, then only allow
|
|
|
|
// setting higher priorities
|
2007-09-01 05:00:31 +02:00
|
|
|
std::vector<int> pieces(m_torrent_file->num_pieces(), 0);
|
2008-07-20 18:00:08 +02:00
|
|
|
for (int i = 0; i < int(m_file_priority.size()); ++i)
|
2007-03-20 02:59:00 +01:00
|
|
|
{
|
2007-03-21 03:09:50 +01:00
|
|
|
size_type start = position;
|
2008-05-28 10:44:40 +02:00
|
|
|
size_type size = m_torrent_file->files().at(i).size;
|
2007-04-18 00:56:14 +02:00
|
|
|
if (size == 0) continue;
|
|
|
|
position += size;
|
2008-09-19 21:46:04 +02:00
|
|
|
if (m_file_priority[i] == 0) continue;
|
|
|
|
|
2007-03-21 03:09:50 +01:00
|
|
|
// mark all pieces of the file with this file's priority
|
|
|
|
// but only if the priority is higher than the pieces
|
|
|
|
// already set (to avoid problems with overlapping pieces)
|
|
|
|
int start_piece = int(start / piece_length);
|
2007-04-18 00:56:14 +02:00
|
|
|
int last_piece = int((position - 1) / piece_length);
|
2008-07-20 18:00:08 +02:00
|
|
|
TORRENT_ASSERT(last_piece < int(pieces.size()));
|
2007-03-21 03:09:50 +01:00
|
|
|
// if one piece spans several files, we might
|
|
|
|
// come here several times with the same start_piece, end_piece
|
|
|
|
std::for_each(pieces.begin() + start_piece
|
|
|
|
, pieces.begin() + last_piece + 1
|
2008-07-20 18:00:08 +02:00
|
|
|
, bind(&set_if_greater, _1, m_file_priority[i]));
|
2007-03-20 02:59:00 +01:00
|
|
|
}
|
2007-03-21 03:09:50 +01:00
|
|
|
prioritize_pieces(pieces);
|
2007-03-20 02:59:00 +01:00
|
|
|
}
|
|
|
|
|
2008-04-13 09:39:37 +02:00
|
|
|
// this is called when piece priorities have been updated
|
2007-03-21 03:09:50 +01:00
|
|
|
// updates the interested flag in peers
|
2008-04-13 09:39:37 +02:00
|
|
|
void torrent::update_peer_interest(bool was_finished)
|
2007-03-21 03:09:50 +01:00
|
|
|
{
|
2008-09-18 01:14:59 +02:00
|
|
|
for (peer_iterator i = begin(); i != end();)
|
|
|
|
{
|
|
|
|
peer_connection* p = *i;
|
|
|
|
// update_interest may disconnect the peer and
|
|
|
|
// invalidate the iterator
|
|
|
|
++i;
|
2008-09-18 19:53:44 +02:00
|
|
|
p->update_interest();
|
2008-09-18 01:14:59 +02:00
|
|
|
}
|
2008-03-29 19:47:24 +01:00
|
|
|
|
2008-04-13 09:39:37 +02:00
|
|
|
// the torrent just became finished
|
|
|
|
if (is_finished() && !was_finished)
|
2008-08-18 22:02:50 +02:00
|
|
|
{
|
2008-04-13 09:39:37 +02:00
|
|
|
finished();
|
2008-08-18 22:02:50 +02:00
|
|
|
}
|
2008-05-20 05:21:45 +02:00
|
|
|
else if (!is_finished() && was_finished)
|
2008-08-18 22:02:50 +02:00
|
|
|
{
|
|
|
|
// if we used to be finished, but we aren't anymore
|
|
|
|
// we may need to connect to peers again
|
2008-05-20 05:21:45 +02:00
|
|
|
resume_download();
|
2008-08-18 22:02:50 +02:00
|
|
|
m_policy.recalculate_connect_candidates();
|
|
|
|
}
|
2007-03-21 03:09:50 +01:00
|
|
|
}
|
2007-03-20 02:59:00 +01:00
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
void torrent::filter_piece(int index, bool filter)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2006-12-11 13:48:33 +01:00
|
|
|
if (is_seed()) return;
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2008-04-13 09:39:37 +02:00
|
|
|
bool was_finished = is_finished();
|
2007-03-15 23:03:56 +01:00
|
|
|
m_picker->set_piece_priority(index, filter ? 1 : 0);
|
2008-04-13 09:39:37 +02:00
|
|
|
update_peer_interest(was_finished);
|
2005-05-25 12:01:01 +02:00
|
|
|
}
|
|
|
|
|
2005-06-23 01:04:37 +02:00
|
|
|
void torrent::filter_pieces(std::vector<bool> const& bitmask)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2005-06-23 01:04:37 +02:00
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2006-12-11 13:48:33 +01:00
|
|
|
if (is_seed()) return;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
2005-06-23 01:04:37 +02:00
|
|
|
|
2008-04-13 09:39:37 +02:00
|
|
|
bool was_finished = is_finished();
|
2005-06-23 01:04:37 +02:00
|
|
|
int index = 0;
|
|
|
|
for (std::vector<bool>::const_iterator i = bitmask.begin()
|
|
|
|
, end(bitmask.end()); i != end; ++i, ++index)
|
|
|
|
{
|
2007-03-15 23:03:56 +01:00
|
|
|
if ((m_picker->piece_priority(index) == 0) == *i) continue;
|
2005-09-01 23:04:21 +02:00
|
|
|
if (*i)
|
2007-03-15 23:03:56 +01:00
|
|
|
m_picker->set_piece_priority(index, 0);
|
2005-09-01 23:04:21 +02:00
|
|
|
else
|
2007-03-15 23:03:56 +01:00
|
|
|
m_picker->set_piece_priority(index, 1);
|
2005-06-23 01:04:37 +02:00
|
|
|
}
|
2008-04-13 09:39:37 +02:00
|
|
|
update_peer_interest(was_finished);
|
2005-06-23 01:04:37 +02:00
|
|
|
}
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
bool torrent::is_piece_filtered(int index) const
|
|
|
|
{
|
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2006-12-11 13:48:33 +01:00
|
|
|
if (is_seed()) return false;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
return m_picker->piece_priority(index) == 0;
|
2005-05-25 12:01:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::filtered_pieces(std::vector<bool>& bitmask) const
|
|
|
|
{
|
2006-05-28 21:03:54 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
// this call is only valid on torrents with metadata
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2006-12-11 13:48:33 +01:00
|
|
|
if (is_seed())
|
|
|
|
{
|
|
|
|
bitmask.clear();
|
2007-09-01 05:00:31 +02:00
|
|
|
bitmask.resize(m_torrent_file->num_pieces(), false);
|
2006-12-11 13:48:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_picker.get());
|
2005-05-25 12:01:01 +02:00
|
|
|
m_picker->filtered_pieces(bitmask);
|
|
|
|
}
|
|
|
|
|
2005-07-04 01:33:47 +02:00
|
|
|
void torrent::filter_files(std::vector<bool> const& bitmask)
|
|
|
|
{
|
2006-05-28 21:03:54 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2005-07-04 01:33:47 +02:00
|
|
|
// this call is only valid on torrents with metadata
|
2006-12-11 13:48:33 +01:00
|
|
|
if (!valid_metadata() || is_seed()) return;
|
2005-07-04 18:27:14 +02:00
|
|
|
|
2005-07-04 18:33:54 +02:00
|
|
|
// the bitmask need to have exactly one bit for every file
|
|
|
|
// in the torrent
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((int)bitmask.size() == m_torrent_file->num_files());
|
2005-07-04 18:33:54 +02:00
|
|
|
|
2005-07-08 16:04:14 +02:00
|
|
|
size_type position = 0;
|
2005-07-04 18:27:14 +02:00
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
if (m_torrent_file->num_pieces())
|
2005-07-04 18:33:54 +02:00
|
|
|
{
|
2007-09-01 05:00:31 +02:00
|
|
|
int piece_length = m_torrent_file->piece_length();
|
2005-07-04 18:33:54 +02:00
|
|
|
// mark all pieces as filtered, then clear the bits for files
|
|
|
|
// that should be downloaded
|
2007-09-01 05:00:31 +02:00
|
|
|
std::vector<bool> piece_filter(m_torrent_file->num_pieces(), true);
|
2005-07-06 02:58:23 +02:00
|
|
|
for (int i = 0; i < (int)bitmask.size(); ++i)
|
2005-07-04 18:27:14 +02:00
|
|
|
{
|
2005-07-08 16:04:14 +02:00
|
|
|
size_type start = position;
|
2008-05-28 10:44:40 +02:00
|
|
|
position += m_torrent_file->files().at(i).size;
|
2005-07-04 18:33:54 +02:00
|
|
|
// is the file selected for download?
|
|
|
|
if (!bitmask[i])
|
|
|
|
{
|
|
|
|
// mark all pieces of the file as downloadable
|
|
|
|
int start_piece = int(start / piece_length);
|
|
|
|
int last_piece = int(position / piece_length);
|
|
|
|
// if one piece spans several files, we might
|
|
|
|
// come here several times with the same start_piece, end_piece
|
|
|
|
std::fill(piece_filter.begin() + start_piece, piece_filter.begin()
|
|
|
|
+ last_piece + 1, false);
|
2005-07-04 18:27:14 +02:00
|
|
|
}
|
|
|
|
}
|
2005-07-04 18:33:54 +02:00
|
|
|
filter_pieces(piece_filter);
|
2005-07-04 18:27:14 +02:00
|
|
|
}
|
2005-07-04 01:33:47 +02:00
|
|
|
}
|
|
|
|
|
2004-09-12 12:12:16 +02:00
|
|
|
void torrent::replace_trackers(std::vector<announce_entry> const& urls)
|
|
|
|
{
|
|
|
|
m_trackers = urls;
|
|
|
|
m_last_working_tracker = -1;
|
2008-11-29 09:38:40 +01:00
|
|
|
for (std::vector<announce_entry>::iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
if (i->source == 0) i->source = announce_entry::source_client;
|
|
|
|
|
2008-12-08 10:13:21 +01:00
|
|
|
if (m_settings.prefer_udp_trackers)
|
|
|
|
prioritize_udp_trackers();
|
|
|
|
|
2008-09-24 04:22:12 +02:00
|
|
|
if (!m_trackers.empty()) start_announcing();
|
|
|
|
else stop_announcing();
|
2004-09-12 12:12:16 +02:00
|
|
|
}
|
|
|
|
|
2008-12-08 10:13:21 +01:00
|
|
|
void torrent::prioritize_udp_trackers()
|
|
|
|
{
|
|
|
|
// look for udp-trackers
|
|
|
|
for (std::vector<announce_entry>::iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->url.substr(0, 6) != "udp://") continue;
|
|
|
|
// now, look for trackers with the same hostname
|
|
|
|
// that is has higher priority than this one
|
|
|
|
// if we find one, swap with the udp-tracker
|
|
|
|
std::string udp_hostname;
|
|
|
|
using boost::tuples::ignore;
|
|
|
|
boost::tie(ignore, ignore, udp_hostname, ignore, ignore, ignore)
|
|
|
|
= parse_url_components(i->url);
|
|
|
|
for (std::vector<announce_entry>::iterator j = m_trackers.begin();
|
|
|
|
j != i; ++j)
|
|
|
|
{
|
|
|
|
std::string hostname;
|
|
|
|
boost::tie(ignore, ignore, hostname, ignore, ignore, ignore)
|
|
|
|
= parse_url_components(j->url);
|
|
|
|
if (hostname != udp_hostname) continue;
|
|
|
|
if (j->url.substr(0, 6) == "udp://") continue;
|
|
|
|
using std::swap;
|
|
|
|
using std::iter_swap;
|
|
|
|
swap(i->tier, j->tier);
|
|
|
|
iter_swap(i, j);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-11-26 02:42:14 +01:00
|
|
|
void torrent::add_tracker(announce_entry const& url)
|
|
|
|
{
|
|
|
|
std::vector<announce_entry>::iterator k = std::find_if(m_trackers.begin()
|
|
|
|
, m_trackers.end(), boost::bind(&announce_entry::url, _1) == url.url);
|
2008-11-29 09:38:40 +01:00
|
|
|
if (k != m_trackers.end())
|
|
|
|
{
|
|
|
|
k->source |= url.source;
|
|
|
|
return;
|
|
|
|
}
|
2008-11-26 02:42:14 +01:00
|
|
|
k = std::upper_bound(m_trackers.begin(), m_trackers.end(), url
|
|
|
|
, boost::bind(&announce_entry::tier, _1) < boost::bind(&announce_entry::tier, _2));
|
|
|
|
if (k - m_trackers.begin() < m_last_working_tracker) ++m_last_working_tracker;
|
2008-11-29 09:38:40 +01:00
|
|
|
k = m_trackers.insert(k, url);
|
|
|
|
if (k->source == 0) k->source = announce_entry::source_client;
|
2008-11-26 02:42:14 +01:00
|
|
|
if (!m_trackers.empty()) start_announcing();
|
|
|
|
}
|
|
|
|
|
2007-08-17 09:37:08 +02:00
|
|
|
void torrent::choke_peer(peer_connection& c)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!c.is_choked());
|
|
|
|
TORRENT_ASSERT(m_num_uploads > 0);
|
2007-08-17 09:37:08 +02:00
|
|
|
c.send_choke();
|
|
|
|
--m_num_uploads;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool torrent::unchoke_peer(peer_connection& c)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(c.is_choked());
|
2007-08-17 09:37:08 +02:00
|
|
|
if (m_num_uploads >= m_max_uploads) return false;
|
2008-08-29 19:21:56 +02:00
|
|
|
if (!c.send_unchoke()) return false;
|
2007-08-17 09:37:08 +02:00
|
|
|
++m_num_uploads;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
void torrent::cancel_block(piece_block block)
|
|
|
|
{
|
2008-03-14 11:17:27 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
for (peer_iterator i = m_connections.begin()
|
|
|
|
, end(m_connections.end()); i != end; ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
(*i)->cancel_request(block);
|
2007-07-06 19:15:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
void torrent::remove_peer(peer_connection* p)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-08-17 04:15:01 +02:00
|
|
|
// INVARIANT_CHECK;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p != 0);
|
2004-01-25 13:37:15 +01:00
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_iterator i = m_connections.find(p);
|
2007-04-12 12:21:55 +02:00
|
|
|
if (i == m_connections.end())
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2007-04-12 12:21:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-10-16 23:14:08 +02:00
|
|
|
if (ready_for_connections())
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->associated_torrent().lock().get() == this);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-04-15 04:14:02 +02:00
|
|
|
if (p->is_seed())
|
|
|
|
{
|
|
|
|
if (m_picker.get())
|
|
|
|
{
|
|
|
|
m_picker->dec_refcount_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
if (m_picker.get())
|
2007-04-15 04:14:02 +02:00
|
|
|
{
|
2008-05-28 04:35:02 +02:00
|
|
|
bitfield const& pieces = p->get_bitfield();
|
|
|
|
TORRENT_ASSERT(pieces.count() < int(pieces.size()));
|
2008-01-31 18:52:29 +01:00
|
|
|
m_picker->dec_refcount(pieces);
|
2007-04-15 04:14:02 +02:00
|
|
|
}
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 09:37:08 +02:00
|
|
|
if (!p->is_choked())
|
2008-10-14 03:00:05 +02:00
|
|
|
{
|
2007-08-17 09:37:08 +02:00
|
|
|
--m_num_uploads;
|
2008-10-14 03:00:05 +02:00
|
|
|
m_ses.m_unchoke_time_scaler = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->peer_info_struct() && p->peer_info_struct()->optimistically_unchoked)
|
|
|
|
{
|
|
|
|
m_ses.m_optimistic_unchoke_time_scaler = 0;
|
|
|
|
}
|
2007-08-17 09:37:08 +02:00
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
m_policy.connection_closed(*p);
|
2007-04-12 12:21:55 +02:00
|
|
|
p->set_peer_info(0);
|
2007-10-31 10:48:20 +01:00
|
|
|
TORRENT_ASSERT(i != m_connections.end());
|
2003-10-23 01:00:57 +02:00
|
|
|
m_connections.erase(i);
|
2008-01-06 01:58:51 +01:00
|
|
|
|
|
|
|
// remove from bandwidth request-queue
|
|
|
|
for (int c = 0; c < 2; ++c)
|
|
|
|
{
|
|
|
|
for (queue_t::iterator i = m_bandwidth_queue[c].begin()
|
|
|
|
, end(m_bandwidth_queue[c].end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->peer != p) continue;
|
|
|
|
m_bandwidth_queue[c].erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
void torrent::connect_to_url_seed(std::string const& url)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2007-10-24 00:11:02 +02:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " resolving web seed: " << url << "\n";
|
2006-06-29 01:27:44 +02:00
|
|
|
#endif
|
|
|
|
|
2008-04-09 09:51:41 +02:00
|
|
|
std::string protocol;
|
|
|
|
std::string auth;
|
|
|
|
std::string hostname;
|
|
|
|
int port;
|
|
|
|
std::string path;
|
2008-05-17 16:19:34 +02:00
|
|
|
char const* error;
|
|
|
|
boost::tie(protocol, auth, hostname, port, path, error)
|
2008-04-09 09:51:41 +02:00
|
|
|
= parse_url_components(url);
|
|
|
|
|
2008-05-17 16:19:34 +02:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
|
|
|
(*m_ses.m_logger) << time_now_string() << " failed to parse web seed url: " << error << "\n";
|
|
|
|
#endif
|
|
|
|
// never try it again
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-09 09:51:41 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (protocol != "http" && protocol != "https")
|
|
|
|
#else
|
|
|
|
if (protocol != "http")
|
|
|
|
#endif
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<url_seed_alert>())
|
2008-04-09 09:51:41 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
|
|
|
url_seed_alert(get_handle(), url, "unknown protocol"));
|
|
|
|
}
|
|
|
|
// never try it again
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hostname.empty())
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<url_seed_alert>())
|
2008-04-09 09:51:41 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
|
|
|
url_seed_alert(get_handle(), url, "invalid hostname"));
|
|
|
|
}
|
|
|
|
// never try it again
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port == 0)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<url_seed_alert>())
|
2008-04-09 09:51:41 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
|
|
|
url_seed_alert(get_handle(), url, "invalid port"));
|
|
|
|
}
|
|
|
|
// never try it again
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-05-20 17:30:40 +02:00
|
|
|
m_resolving_web_seeds.insert(url);
|
2007-04-25 20:26:35 +02:00
|
|
|
proxy_settings const& ps = m_ses.web_seed_proxy();
|
|
|
|
if (ps.type == proxy_settings::http
|
|
|
|
|| ps.type == proxy_settings::http_pw)
|
|
|
|
{
|
|
|
|
// use proxy
|
|
|
|
tcp::resolver::query q(ps.hostname
|
|
|
|
, boost::lexical_cast<std::string>(ps.port));
|
2008-01-08 06:47:43 +01:00
|
|
|
m_host_resolver.async_resolve(q,
|
|
|
|
bind(&torrent::on_proxy_name_lookup, shared_from_this(), _1, _2, url));
|
2007-04-25 20:26:35 +02:00
|
|
|
}
|
|
|
|
else
|
2006-07-27 20:07:51 +02:00
|
|
|
{
|
2008-05-15 07:13:09 +02:00
|
|
|
if (m_ses.m_port_filter.access(port) & port_filter::blocked)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<url_seed_alert>())
|
2008-05-15 07:13:09 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
|
|
|
url_seed_alert(get_handle(), url, "port blocked by port-filter"));
|
|
|
|
}
|
|
|
|
// never try it again
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
tcp::resolver::query q(hostname, boost::lexical_cast<std::string>(port));
|
2008-01-08 06:47:43 +01:00
|
|
|
m_host_resolver.async_resolve(q,
|
2007-02-12 06:46:29 +01:00
|
|
|
bind(&torrent::on_name_lookup, shared_from_this(), _1, _2, url
|
2008-01-08 06:47:43 +01:00
|
|
|
, tcp::endpoint()));
|
2006-07-27 20:07:51 +02:00
|
|
|
}
|
2007-02-12 06:46:29 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void torrent::on_proxy_name_lookup(error_code const& e, tcp::resolver::iterator host
|
2008-04-07 04:51:21 +02:00
|
|
|
, std::string url)
|
2007-02-12 06:46:29 +01:00
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2007-04-10 09:52:58 +02:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " completed resolve proxy hostname for: " << url << "\n";
|
2007-02-12 06:46:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (e || host == tcp::resolver::iterator())
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<url_seed_alert>())
|
2007-02-12 06:46:29 +01:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
2008-05-17 16:19:34 +02:00
|
|
|
url_seed_alert(get_handle(), url, e.message()));
|
2007-02-12 06:46:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// the name lookup failed for the http host. Don't try
|
|
|
|
// this host again
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ses.is_aborted()) return;
|
|
|
|
|
|
|
|
tcp::endpoint a(host->endpoint());
|
2007-05-22 22:44:18 +02:00
|
|
|
|
|
|
|
using boost::tuples::ignore;
|
2007-02-12 06:46:29 +01:00
|
|
|
std::string hostname;
|
|
|
|
int port;
|
2008-05-17 16:19:34 +02:00
|
|
|
char const* error;
|
|
|
|
boost::tie(ignore, ignore, hostname, port, ignore, error)
|
2007-02-12 06:46:29 +01:00
|
|
|
= parse_url_components(url);
|
|
|
|
|
2008-05-17 16:19:34 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<url_seed_alert>())
|
2008-05-17 16:19:34 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
|
|
|
url_seed_alert(get_handle(), url, error));
|
|
|
|
}
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-17 07:56:43 +02:00
|
|
|
if (m_ses.m_ip_filter.access(a.address()) & ip_filter::blocked)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<peer_blocked_alert>())
|
|
|
|
m_ses.m_alerts.post_alert(peer_blocked_alert(a.address()));
|
2007-04-17 07:56:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-12 06:46:29 +01:00
|
|
|
tcp::resolver::query q(hostname, boost::lexical_cast<std::string>(port));
|
2008-01-08 06:47:43 +01:00
|
|
|
m_host_resolver.async_resolve(q,
|
|
|
|
bind(&torrent::on_name_lookup, shared_from_this(), _1, _2, url, a));
|
2007-02-12 06:46:29 +01:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void torrent::on_name_lookup(error_code const& e, tcp::resolver::iterator host
|
2008-04-07 04:51:21 +02:00
|
|
|
, std::string url, tcp::endpoint proxy)
|
2007-02-12 06:46:29 +01:00
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2007-04-10 09:52:58 +02:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " completed resolve: " << url << "\n";
|
2007-02-12 06:46:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::set<std::string>::iterator i = m_resolving_web_seeds.find(url);
|
|
|
|
if (i != m_resolving_web_seeds.end()) m_resolving_web_seeds.erase(i);
|
|
|
|
|
|
|
|
if (e || host == tcp::resolver::iterator())
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<url_seed_alert>())
|
2007-02-12 06:46:29 +01:00
|
|
|
{
|
|
|
|
std::stringstream msg;
|
|
|
|
msg << "HTTP seed hostname lookup failed: " << e.message();
|
|
|
|
m_ses.m_alerts.post_alert(
|
|
|
|
url_seed_alert(get_handle(), url, msg.str()));
|
|
|
|
}
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-02-12 06:46:29 +01:00
|
|
|
(*m_ses.m_logger) << " ** HOSTNAME LOOKUP FAILED!**: " << url << "\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// the name lookup failed for the http host. Don't try
|
|
|
|
// this host again
|
|
|
|
remove_url_seed(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ses.is_aborted()) return;
|
|
|
|
|
|
|
|
tcp::endpoint a(host->endpoint());
|
|
|
|
|
|
|
|
if (m_ses.m_ip_filter.access(a.address()) & ip_filter::blocked)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<peer_blocked_alert>())
|
|
|
|
m_ses.m_alerts.post_alert(peer_blocked_alert(a.address()));
|
2007-02-12 06:46:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
boost::shared_ptr<socket_type> s(new (std::nothrow) socket_type(m_ses.m_io_service));
|
|
|
|
if (!s) return;
|
2007-10-22 06:17:26 +02:00
|
|
|
|
|
|
|
bool ret = instantiate_connection(m_ses.m_io_service, m_ses.web_seed_proxy(), *s);
|
2008-08-19 16:53:50 +02:00
|
|
|
(void)ret;
|
2007-10-22 06:17:26 +02:00
|
|
|
TORRENT_ASSERT(ret);
|
|
|
|
|
2007-04-25 20:26:35 +02:00
|
|
|
if (m_ses.web_seed_proxy().type == proxy_settings::http
|
|
|
|
|| m_ses.web_seed_proxy().type == proxy_settings::http_pw)
|
|
|
|
{
|
|
|
|
// the web seed connection will talk immediately to
|
|
|
|
// the proxy, without requiring CONNECT support
|
2008-10-18 09:12:04 +02:00
|
|
|
s->get<http_stream>()->set_no_connect(true);
|
2007-04-25 20:26:35 +02:00
|
|
|
}
|
2008-02-28 08:34:07 +01:00
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
boost::intrusive_ptr<peer_connection> c(new (std::nothrow) web_peer_connection(
|
2007-04-25 20:26:35 +02:00
|
|
|
m_ses, shared_from_this(), s, a, url, 0));
|
2008-04-07 04:51:21 +02:00
|
|
|
if (!c) return;
|
2007-02-12 06:46:29 +01:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-02-12 06:46:29 +01:00
|
|
|
c->m_in_constructor = false;
|
|
|
|
#endif
|
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<peer_plugin> pp((*i)->new_connection(c.get()));
|
|
|
|
if (pp) c->add_extension(pp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
// add the newly connected peer to this torrent's peer list
|
|
|
|
m_connections.insert(boost::get_pointer(c));
|
|
|
|
m_ses.m_connections.insert(c);
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2007-02-12 06:46:29 +01:00
|
|
|
try
|
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
#endif
|
2007-02-12 06:46:29 +01:00
|
|
|
// add the newly connected peer to this torrent's peer list
|
2007-10-31 10:48:20 +01:00
|
|
|
m_connections.insert(boost::get_pointer(c));
|
|
|
|
m_ses.m_connections.insert(c);
|
2008-04-01 21:17:09 +02:00
|
|
|
c->start();
|
2007-02-12 06:46:29 +01:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
m_ses.m_half_open.enqueue(
|
|
|
|
bind(&peer_connection::connect, c, _1)
|
|
|
|
, bind(&peer_connection::timed_out, c)
|
|
|
|
, seconds(settings().peer_connect_timeout));
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-07-27 20:07:51 +02:00
|
|
|
}
|
2007-02-12 06:46:29 +01:00
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-02-12 06:46:29 +01:00
|
|
|
(*m_ses.m_logger) << " ** HOSTNAME LOOKUP FAILED!**: " << e.what() << "\n";
|
|
|
|
#endif
|
2008-05-12 08:03:31 +02:00
|
|
|
c->disconnect(e.what(), 1);
|
2007-02-12 06:46:29 +01:00
|
|
|
}
|
2007-05-10 03:50:11 +02:00
|
|
|
#endif
|
2008-04-07 04:51:21 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-05-02 21:47:38 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2007-08-07 09:17:13 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
unsigned long swap_bytes(unsigned long a)
|
|
|
|
{
|
|
|
|
return (a >> 24) | ((a & 0xff0000) >> 8) | ((a & 0xff00) << 8) | (a << 24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
void torrent::resolve_peer_country(boost::intrusive_ptr<peer_connection> const& p) const
|
|
|
|
{
|
|
|
|
if (m_resolving_country
|
|
|
|
|| p->has_country()
|
|
|
|
|| p->is_connecting()
|
|
|
|
|| p->is_queued()
|
2007-08-07 09:17:13 +02:00
|
|
|
|| p->in_handshake()
|
|
|
|
|| p->remote().address().is_v6()) return;
|
2007-01-29 08:39:33 +01:00
|
|
|
|
2007-08-07 09:17:13 +02:00
|
|
|
asio::ip::address_v4 reversed(swap_bytes(p->remote().address().to_v4().to_ulong()));
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
|
|
|
tcp::resolver::query q(reversed.to_string(ec) + ".zz.countries.nerd.dk", "0");
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
p->set_country("!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_resolving_country = true;
|
2008-01-08 06:47:43 +01:00
|
|
|
m_host_resolver.async_resolve(q,
|
|
|
|
bind(&torrent::on_country_lookup, shared_from_this(), _1, _2, p));
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2007-05-06 00:55:34 +02:00
|
|
|
struct country_entry
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
2007-05-06 00:55:34 +02:00
|
|
|
int code;
|
|
|
|
char const* name;
|
|
|
|
};
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void torrent::on_country_lookup(error_code const& error, tcp::resolver::iterator i
|
2007-01-29 08:39:33 +01:00
|
|
|
, intrusive_ptr<peer_connection> p) const
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
m_resolving_country = false;
|
|
|
|
|
|
|
|
// must be ordered in increasing order
|
2007-09-26 02:03:22 +02:00
|
|
|
static const country_entry country_map[] =
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
2007-05-06 00:55:34 +02:00
|
|
|
{ 4, "AF"}, { 8, "AL"}, { 10, "AQ"}, { 12, "DZ"}, { 16, "AS"}
|
|
|
|
, { 20, "AD"}, { 24, "AO"}, { 28, "AG"}, { 31, "AZ"}, { 32, "AR"}
|
|
|
|
, { 36, "AU"}, { 40, "AT"}, { 44, "BS"}, { 48, "BH"}, { 50, "BD"}
|
|
|
|
, { 51, "AM"}, { 52, "BB"}, { 56, "BE"}, { 60, "BM"}, { 64, "BT"}
|
|
|
|
, { 68, "BO"}, { 70, "BA"}, { 72, "BW"}, { 74, "BV"}, { 76, "BR"}
|
|
|
|
, { 84, "BZ"}, { 86, "IO"}, { 90, "SB"}, { 92, "VG"}, { 96, "BN"}
|
|
|
|
, {100, "BG"}, {104, "MM"}, {108, "BI"}, {112, "BY"}, {116, "KH"}
|
|
|
|
, {120, "CM"}, {124, "CA"}, {132, "CV"}, {136, "KY"}, {140, "CF"}
|
|
|
|
, {144, "LK"}, {148, "TD"}, {152, "CL"}, {156, "CN"}, {158, "TW"}
|
|
|
|
, {162, "CX"}, {166, "CC"}, {170, "CO"}, {174, "KM"}, {175, "YT"}
|
|
|
|
, {178, "CG"}, {180, "CD"}, {184, "CK"}, {188, "CR"}, {191, "HR"}
|
|
|
|
, {192, "CU"}, {203, "CZ"}, {204, "BJ"}, {208, "DK"}, {212, "DM"}
|
|
|
|
, {214, "DO"}, {218, "EC"}, {222, "SV"}, {226, "GQ"}, {231, "ET"}
|
|
|
|
, {232, "ER"}, {233, "EE"}, {234, "FO"}, {238, "FK"}, {239, "GS"}
|
|
|
|
, {242, "FJ"}, {246, "FI"}, {248, "AX"}, {250, "FR"}, {254, "GF"}
|
|
|
|
, {258, "PF"}, {260, "TF"}, {262, "DJ"}, {266, "GA"}, {268, "GE"}
|
|
|
|
, {270, "GM"}, {275, "PS"}, {276, "DE"}, {288, "GH"}, {292, "GI"}
|
|
|
|
, {296, "KI"}, {300, "GR"}, {304, "GL"}, {308, "GD"}, {312, "GP"}
|
|
|
|
, {316, "GU"}, {320, "GT"}, {324, "GN"}, {328, "GY"}, {332, "HT"}
|
|
|
|
, {334, "HM"}, {336, "VA"}, {340, "HN"}, {344, "HK"}, {348, "HU"}
|
|
|
|
, {352, "IS"}, {356, "IN"}, {360, "ID"}, {364, "IR"}, {368, "IQ"}
|
|
|
|
, {372, "IE"}, {376, "IL"}, {380, "IT"}, {384, "CI"}, {388, "JM"}
|
|
|
|
, {392, "JP"}, {398, "KZ"}, {400, "JO"}, {404, "KE"}, {408, "KP"}
|
|
|
|
, {410, "KR"}, {414, "KW"}, {417, "KG"}, {418, "LA"}, {422, "LB"}
|
|
|
|
, {426, "LS"}, {428, "LV"}, {430, "LR"}, {434, "LY"}, {438, "LI"}
|
|
|
|
, {440, "LT"}, {442, "LU"}, {446, "MO"}, {450, "MG"}, {454, "MW"}
|
|
|
|
, {458, "MY"}, {462, "MV"}, {466, "ML"}, {470, "MT"}, {474, "MQ"}
|
|
|
|
, {478, "MR"}, {480, "MU"}, {484, "MX"}, {492, "MC"}, {496, "MN"}
|
|
|
|
, {498, "MD"}, {500, "MS"}, {504, "MA"}, {508, "MZ"}, {512, "OM"}
|
|
|
|
, {516, "NA"}, {520, "NR"}, {524, "NP"}, {528, "NL"}, {530, "AN"}
|
|
|
|
, {533, "AW"}, {540, "NC"}, {548, "VU"}, {554, "NZ"}, {558, "NI"}
|
|
|
|
, {562, "NE"}, {566, "NG"}, {570, "NU"}, {574, "NF"}, {578, "NO"}
|
|
|
|
, {580, "MP"}, {581, "UM"}, {583, "FM"}, {584, "MH"}, {585, "PW"}
|
|
|
|
, {586, "PK"}, {591, "PA"}, {598, "PG"}, {600, "PY"}, {604, "PE"}
|
|
|
|
, {608, "PH"}, {612, "PN"}, {616, "PL"}, {620, "PT"}, {624, "GW"}
|
|
|
|
, {626, "TL"}, {630, "PR"}, {634, "QA"}, {634, "QA"}, {638, "RE"}
|
|
|
|
, {642, "RO"}, {643, "RU"}, {646, "RW"}, {654, "SH"}, {659, "KN"}
|
|
|
|
, {660, "AI"}, {662, "LC"}, {666, "PM"}, {670, "VC"}, {674, "SM"}
|
|
|
|
, {678, "ST"}, {682, "SA"}, {686, "SN"}, {690, "SC"}, {694, "SL"}
|
|
|
|
, {702, "SG"}, {703, "SK"}, {704, "VN"}, {705, "SI"}, {706, "SO"}
|
|
|
|
, {710, "ZA"}, {716, "ZW"}, {724, "ES"}, {732, "EH"}, {736, "SD"}
|
|
|
|
, {740, "SR"}, {744, "SJ"}, {748, "SZ"}, {752, "SE"}, {756, "CH"}
|
|
|
|
, {760, "SY"}, {762, "TJ"}, {764, "TH"}, {768, "TG"}, {772, "TK"}
|
|
|
|
, {776, "TO"}, {780, "TT"}, {784, "AE"}, {788, "TN"}, {792, "TR"}
|
|
|
|
, {795, "TM"}, {796, "TC"}, {798, "TV"}, {800, "UG"}, {804, "UA"}
|
|
|
|
, {807, "MK"}, {818, "EG"}, {826, "GB"}, {834, "TZ"}, {840, "US"}
|
|
|
|
, {850, "VI"}, {854, "BF"}, {858, "UY"}, {860, "UZ"}, {862, "VE"}
|
|
|
|
, {876, "WF"}, {882, "WS"}, {887, "YE"}, {891, "CS"}, {894, "ZM"}
|
2007-01-29 08:39:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (error || i == tcp::resolver::iterator())
|
|
|
|
{
|
|
|
|
// this is used to indicate that we shouldn't
|
|
|
|
// try to resolve it again
|
|
|
|
p->set_country("--");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i != tcp::resolver::iterator()
|
|
|
|
&& !i->endpoint().address().is_v4()) ++i;
|
|
|
|
if (i != tcp::resolver::iterator())
|
|
|
|
{
|
|
|
|
// country is an ISO 3166 country code
|
|
|
|
int country = i->endpoint().address().to_v4().to_ulong() & 0xffff;
|
|
|
|
|
|
|
|
// look up the country code in the map
|
|
|
|
const int size = sizeof(country_map)/sizeof(country_map[0]);
|
2007-05-06 00:55:34 +02:00
|
|
|
country_entry tmp = {country, ""};
|
2007-09-26 17:32:49 +02:00
|
|
|
country_entry const* i =
|
2007-05-06 00:55:34 +02:00
|
|
|
std::lower_bound(country_map, country_map + size, tmp
|
|
|
|
, bind(&country_entry::code, _1) < bind(&country_entry::code, _2));
|
2007-01-29 08:39:33 +01:00
|
|
|
if (i == country_map + size
|
2007-05-06 00:55:34 +02:00
|
|
|
|| i->code != country)
|
2007-01-29 08:39:33 +01:00
|
|
|
{
|
|
|
|
// unknown country!
|
|
|
|
p->set_country("!!");
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-01-29 08:39:33 +01:00
|
|
|
(*m_ses.m_logger) << "IP " << p->remote().address() << " was mapped to unknown country: " << country << "\n";
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-06 00:55:34 +02:00
|
|
|
p->set_country(i->name);
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
2007-05-02 21:47:38 +02:00
|
|
|
#endif
|
2007-01-29 08:39:33 +01:00
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
void torrent::read_resume_data(lazy_entry const& rd)
|
2008-04-16 08:31:05 +02:00
|
|
|
{
|
2008-07-01 01:14:31 +02:00
|
|
|
m_total_uploaded = rd.dict_find_int_value("total_uploaded");
|
|
|
|
m_total_downloaded = rd.dict_find_int_value("total_downloaded");
|
|
|
|
m_active_time = seconds(rd.dict_find_int_value("active_time"));
|
|
|
|
m_seeding_time = seconds(rd.dict_find_int_value("seeding_time"));
|
|
|
|
m_complete = rd.dict_find_int_value("num_seeds", -1);
|
|
|
|
m_incomplete = rd.dict_find_int_value("num_downloaders", -1);
|
2008-07-19 13:12:40 +02:00
|
|
|
set_upload_limit(rd.dict_find_int_value("upload_rate_limit", -1));
|
|
|
|
set_download_limit(rd.dict_find_int_value("download_rate_limit", -1));
|
|
|
|
set_max_connections(rd.dict_find_int_value("max_connections", -1));
|
|
|
|
set_max_uploads(rd.dict_find_int_value("max_uploads", -1));
|
|
|
|
|
2008-07-20 18:00:08 +02:00
|
|
|
lazy_entry const* file_priority = rd.dict_find_list("file_priority");
|
|
|
|
if (file_priority && file_priority->list_size()
|
|
|
|
== m_torrent_file->num_files())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < file_priority->list_size(); ++i)
|
|
|
|
m_file_priority[i] = file_priority->list_int_value_at(i, 1);
|
|
|
|
update_piece_priorities();
|
|
|
|
}
|
2008-07-19 13:12:40 +02:00
|
|
|
lazy_entry const* piece_priority = rd.dict_find_string("piece_priority");
|
|
|
|
if (piece_priority && piece_priority->string_length()
|
|
|
|
== m_torrent_file->num_pieces())
|
|
|
|
{
|
|
|
|
char const* p = piece_priority->string_ptr();
|
|
|
|
for (int i = 0; i < piece_priority->string_length(); ++i)
|
|
|
|
m_picker->set_piece_priority(i, p[i]);
|
|
|
|
}
|
|
|
|
|
2008-09-23 05:21:35 +02:00
|
|
|
int auto_managed_ = rd.dict_find_int_value("auto_managed", -1);
|
|
|
|
if (auto_managed_ != -1) auto_managed(auto_managed_);
|
2008-09-23 05:52:49 +02:00
|
|
|
|
|
|
|
int sequential_ = rd.dict_find_int_value("sequential_download", -1);
|
|
|
|
if (sequential_ != -1) set_sequential_download(sequential_);
|
|
|
|
|
2008-09-24 04:29:24 +02:00
|
|
|
int paused_ = rd.dict_find_int_value("paused", -1);
|
|
|
|
if (paused_ == 1) pause();
|
|
|
|
else if (paused_ == 0) resume();
|
2008-09-22 19:39:18 +02:00
|
|
|
|
|
|
|
lazy_entry const* trackers = rd.dict_find_list("trackers");
|
|
|
|
if (trackers)
|
|
|
|
{
|
|
|
|
int tier = 0;
|
|
|
|
for (int i = 0; i < trackers->list_size(); ++i)
|
|
|
|
{
|
|
|
|
lazy_entry const* tier_list = trackers->list_at(i);
|
|
|
|
if (tier_list == 0 || tier_list->type() != lazy_entry::list_t)
|
|
|
|
continue;
|
|
|
|
for (int j = 0; j < tier_list->list_size(); ++j)
|
|
|
|
{
|
|
|
|
announce_entry e(tier_list->list_string_value_at(j));
|
|
|
|
if (std::find_if(m_trackers.begin(), m_trackers.end()
|
|
|
|
, boost::bind(&announce_entry::url, _1) == e.url) != m_trackers.end())
|
|
|
|
continue;
|
|
|
|
e.tier = tier;
|
|
|
|
m_trackers.push_back(e);
|
|
|
|
}
|
|
|
|
++tier;
|
|
|
|
}
|
|
|
|
std::sort(m_trackers.begin(), m_trackers.end(), boost::bind(&announce_entry::tier, _1)
|
|
|
|
< boost::bind(&announce_entry::tier, _2));
|
2008-12-08 10:13:21 +01:00
|
|
|
|
|
|
|
if (m_settings.prefer_udp_trackers)
|
|
|
|
prioritize_udp_trackers();
|
2008-09-22 19:39:18 +02:00
|
|
|
}
|
|
|
|
|
2008-10-01 01:37:42 +02:00
|
|
|
lazy_entry const* mapped_files = rd.dict_find_list("mapped_files");
|
|
|
|
if (mapped_files && mapped_files->list_size() == m_torrent_file->num_files())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_torrent_file->num_files(); ++i)
|
|
|
|
{
|
|
|
|
std::string new_filename = mapped_files->list_string_value_at(i);
|
|
|
|
if (new_filename.empty()) continue;
|
|
|
|
m_torrent_file->files().rename_file(i, new_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-22 19:39:18 +02:00
|
|
|
lazy_entry const* url_list = rd.dict_find_list("url-list");
|
|
|
|
if (url_list)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < url_list->list_size(); ++i)
|
|
|
|
{
|
|
|
|
std::string url = url_list->list_string_value_at(i);
|
|
|
|
if (url.empty()) continue;
|
|
|
|
m_web_seeds.insert(url);
|
|
|
|
}
|
|
|
|
}
|
2008-04-16 08:31:05 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
void torrent::write_resume_data(entry& ret) const
|
|
|
|
{
|
|
|
|
ret["file-format"] = "libtorrent resume file";
|
|
|
|
ret["file-version"] = 1;
|
|
|
|
|
2008-04-16 08:31:05 +02:00
|
|
|
ret["total_uploaded"] = m_total_uploaded;
|
|
|
|
ret["total_downloaded"] = m_total_downloaded;
|
|
|
|
|
|
|
|
ret["active_time"] = total_seconds(m_active_time);
|
|
|
|
ret["seeding_time"] = total_seconds(m_seeding_time);
|
2008-05-18 21:31:06 +02:00
|
|
|
|
|
|
|
int seeds = 0;
|
|
|
|
int downloaders = 0;
|
|
|
|
if (m_complete >= 0) seeds = m_complete;
|
|
|
|
else seeds = m_policy.num_seeds();
|
|
|
|
if (m_incomplete >= 0) downloaders = m_incomplete;
|
|
|
|
else downloaders = m_policy.num_peers() - m_policy.num_seeds();
|
|
|
|
|
|
|
|
ret["num_seeds"] = seeds;
|
|
|
|
ret["num_downloaders"] = downloaders;
|
2008-09-23 05:52:49 +02:00
|
|
|
|
|
|
|
ret["sequential_download"] = m_sequential_download;
|
2008-04-16 08:31:05 +02:00
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
const sha1_hash& info_hash = torrent_file().info_hash();
|
|
|
|
ret["info-hash"] = std::string((char*)info_hash.begin(), (char*)info_hash.end());
|
|
|
|
|
|
|
|
// blocks per piece
|
|
|
|
int num_blocks_per_piece =
|
|
|
|
static_cast<int>(torrent_file().piece_length()) / 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 (!is_seed())
|
|
|
|
{
|
|
|
|
const std::vector<piece_picker::downloading_piece>& q
|
|
|
|
= m_picker->get_download_queue();
|
|
|
|
|
|
|
|
// unfinished pieces
|
|
|
|
ret["unfinished"] = entry::list_type();
|
|
|
|
entry::list_type& up = ret["unfinished"].list();
|
|
|
|
|
|
|
|
// info for each unfinished piece
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i
|
|
|
|
= q.begin(); i != q.end(); ++i)
|
|
|
|
{
|
|
|
|
if (i->finished == 0) continue;
|
|
|
|
|
|
|
|
entry piece_struct(entry::dictionary_t);
|
|
|
|
|
|
|
|
// the unfinished piece's index
|
|
|
|
piece_struct["piece"] = i->index;
|
|
|
|
|
|
|
|
std::string bitmask;
|
|
|
|
const int num_bitmask_bytes
|
|
|
|
= (std::max)(num_blocks_per_piece / 8, 1);
|
|
|
|
|
|
|
|
for (int j = 0; j < num_bitmask_bytes; ++j)
|
|
|
|
{
|
|
|
|
unsigned char v = 0;
|
|
|
|
int bits = (std::min)(num_blocks_per_piece - j*8, 8);
|
|
|
|
for (int k = 0; k < bits; ++k)
|
|
|
|
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;
|
|
|
|
// push the struct onto the unfinished-piece list
|
|
|
|
up.push_back(piece_struct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-22 19:39:18 +02:00
|
|
|
// save trackers
|
|
|
|
if (!m_trackers.empty())
|
|
|
|
{
|
|
|
|
entry::list_type& tr_list = ret["trackers"].list();
|
|
|
|
tr_list.push_back(entry::list_type());
|
|
|
|
int tier = 0;
|
|
|
|
for (std::vector<announce_entry>::const_iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->tier == tier)
|
|
|
|
{
|
|
|
|
tr_list.back().list().push_back(i->url);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_list.push_back(entry::list_t);
|
|
|
|
tr_list.back().list().push_back(i->url);
|
|
|
|
tier = i->tier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save web seeds
|
|
|
|
if (!m_web_seeds.empty())
|
|
|
|
{
|
|
|
|
entry::list_type& url_list = ret["url-list"].list();
|
|
|
|
for (std::set<std::string>::const_iterator i = m_web_seeds.begin()
|
|
|
|
, end(m_web_seeds.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
url_list.push_back(*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
// write have bitmask
|
|
|
|
entry::string_type& pieces = ret["pieces"].string();
|
|
|
|
pieces.resize(m_torrent_file->num_pieces());
|
2008-06-07 04:58:28 +02:00
|
|
|
if (is_seed())
|
|
|
|
{
|
2008-07-19 13:12:40 +02:00
|
|
|
std::memset(&pieces[0], 1, pieces.size());
|
2008-06-07 04:58:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0, end(pieces.size()); i < end; ++i)
|
|
|
|
pieces[i] = m_picker->have_piece(i) ? 1 : 0;
|
|
|
|
}
|
2008-04-13 20:54:36 +02:00
|
|
|
|
|
|
|
// write local peers
|
|
|
|
|
|
|
|
entry::list_type& peer_list = ret["peers"].list();
|
|
|
|
entry::list_type& banned_peer_list = ret["banned_peers"].list();
|
|
|
|
|
|
|
|
int max_failcount = m_ses.m_settings.max_failcount;
|
|
|
|
|
|
|
|
for (policy::const_iterator i = m_policy.begin_peer()
|
|
|
|
, end(m_policy.end_peer()); i != end; ++i)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-04-13 20:54:36 +02:00
|
|
|
if (i->second.banned)
|
|
|
|
{
|
|
|
|
entry peer(entry::dictionary_t);
|
2008-07-14 13:15:35 +02:00
|
|
|
peer["ip"] = i->second.addr.to_string(ec);
|
2008-04-13 20:54:36 +02:00
|
|
|
if (ec) continue;
|
2008-07-14 13:15:35 +02:00
|
|
|
peer["port"] = i->second.port;
|
2008-04-13 20:54:36 +02:00
|
|
|
banned_peer_list.push_back(peer);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
if (i->second.type == policy::peer::not_connectable) continue;
|
|
|
|
|
|
|
|
// don't save peers that doesn't work
|
|
|
|
if (i->second.failcount >= max_failcount) continue;
|
|
|
|
|
|
|
|
entry peer(entry::dictionary_t);
|
2008-07-14 13:15:35 +02:00
|
|
|
peer["ip"] = i->second.addr.to_string(ec);
|
2008-04-13 20:54:36 +02:00
|
|
|
if (ec) continue;
|
2008-07-14 13:15:35 +02:00
|
|
|
peer["port"] = i->second.port;
|
2008-04-13 20:54:36 +02:00
|
|
|
peer_list.push_back(peer);
|
|
|
|
}
|
2008-07-19 13:12:40 +02:00
|
|
|
|
|
|
|
ret["upload_rate_limit"] = upload_limit();
|
|
|
|
ret["download_rate_limit"] = download_limit();
|
|
|
|
ret["max_connections"] = max_connections();
|
|
|
|
ret["max_uploads"] = max_uploads();
|
|
|
|
ret["paused"] = m_paused;
|
|
|
|
ret["auto_managed"] = m_auto_managed;
|
|
|
|
|
|
|
|
// write piece priorities
|
|
|
|
entry::string_type& piece_priority = ret["piece_priority"].string();
|
|
|
|
piece_priority.resize(m_torrent_file->num_pieces());
|
|
|
|
if (is_seed())
|
|
|
|
{
|
|
|
|
std::memset(&piece_priority[0], 1, pieces.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0, end(piece_priority.size()); i < end; ++i)
|
|
|
|
piece_priority[i] = m_picker->piece_priority(i);
|
|
|
|
}
|
2008-07-20 18:00:08 +02:00
|
|
|
|
|
|
|
// write file priorities
|
|
|
|
entry::list_type& file_priority = ret["file_priority"].list();
|
|
|
|
file_priority.clear();
|
|
|
|
for (int i = 0, end(m_file_priority.size()); i < end; ++i)
|
|
|
|
file_priority.push_back(m_file_priority[i]);
|
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 08:32:48 +02:00
|
|
|
void torrent::get_full_peer_list(std::vector<peer_list_entry>& v) const
|
|
|
|
{
|
|
|
|
v.clear();
|
|
|
|
v.reserve(m_policy.num_peers());
|
|
|
|
for (policy::const_iterator i = m_policy.begin_peer();
|
|
|
|
i != m_policy.end_peer(); ++i)
|
|
|
|
{
|
|
|
|
peer_list_entry e;
|
2008-07-14 13:15:35 +02:00
|
|
|
e.ip = i->second.ip();
|
2008-04-13 08:32:48 +02:00
|
|
|
e.flags = i->second.banned ? peer_list_entry::banned : 0;
|
|
|
|
e.failcount = i->second.failcount;
|
|
|
|
e.source = i->second.source;
|
|
|
|
v.push_back(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-25 19:48:43 +01:00
|
|
|
void torrent::get_peer_info(std::vector<peer_info>& v)
|
|
|
|
{
|
|
|
|
v.clear();
|
|
|
|
for (peer_iterator i = begin();
|
|
|
|
i != end(); ++i)
|
|
|
|
{
|
|
|
|
peer_connection* peer = *i;
|
|
|
|
|
|
|
|
// incoming peers that haven't finished the handshake should
|
|
|
|
// not be included in this list
|
|
|
|
if (peer->associated_torrent().expired()) continue;
|
|
|
|
|
|
|
|
v.push_back(peer_info());
|
|
|
|
peer_info& p = v.back();
|
|
|
|
|
|
|
|
peer->get_peer_info(p);
|
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
|
|
|
if (resolving_countries())
|
|
|
|
resolve_peer_country(intrusive_ptr<peer_connection>(peer));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::get_download_queue(std::vector<partial_piece_info>& queue)
|
|
|
|
{
|
|
|
|
queue.clear();
|
|
|
|
if (!valid_metadata() || is_seed()) return;
|
|
|
|
piece_picker const& p = picker();
|
|
|
|
std::vector<piece_picker::downloading_piece> const& q
|
|
|
|
= p.get_download_queue();
|
|
|
|
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i
|
|
|
|
= q.begin(); i != q.end(); ++i)
|
|
|
|
{
|
|
|
|
partial_piece_info pi;
|
|
|
|
pi.piece_state = (partial_piece_info::state_t)i->state;
|
|
|
|
pi.blocks_in_piece = p.blocks_in_piece(i->index);
|
|
|
|
pi.finished = (int)i->finished;
|
|
|
|
pi.writing = (int)i->writing;
|
|
|
|
pi.requested = (int)i->requested;
|
2008-04-05 23:18:27 +02:00
|
|
|
int piece_size = int(torrent_file().piece_size(i->index));
|
2008-10-05 09:23:46 +02:00
|
|
|
int num_blocks = (std::min)(pi.blocks_in_piece, int(partial_piece_info::max_blocks_per_piece));
|
|
|
|
for (int j = 0; j < num_blocks; ++j)
|
2007-11-25 19:48:43 +01:00
|
|
|
{
|
|
|
|
block_info& bi = pi.blocks[j];
|
|
|
|
bi.state = i->info[j].state;
|
|
|
|
bi.block_size = j < pi.blocks_in_piece - 1 ? m_block_size
|
|
|
|
: piece_size - (j * m_block_size);
|
|
|
|
bool complete = bi.state == block_info::writing
|
|
|
|
|| bi.state == block_info::finished;
|
|
|
|
if (i->info[j].peer == 0)
|
|
|
|
{
|
|
|
|
bi.peer = tcp::endpoint();
|
|
|
|
bi.bytes_progress = complete ? bi.block_size : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
policy::peer* p = static_cast<policy::peer*>(i->info[j].peer);
|
|
|
|
if (p->connection)
|
|
|
|
{
|
|
|
|
bi.peer = p->connection->remote();
|
|
|
|
if (bi.state == block_info::requested)
|
|
|
|
{
|
|
|
|
boost::optional<piece_block_progress> pbp
|
|
|
|
= p->connection->downloading_piece_progress();
|
|
|
|
if (pbp && pbp->piece_index == i->index && pbp->block_index == j)
|
|
|
|
{
|
|
|
|
bi.bytes_progress = pbp->bytes_downloaded;
|
|
|
|
TORRENT_ASSERT(bi.bytes_progress <= bi.block_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bi.bytes_progress = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bi.bytes_progress = complete ? bi.block_size : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 13:15:35 +02:00
|
|
|
bi.peer = p->ip();
|
2007-11-25 19:48:43 +01:00
|
|
|
bi.bytes_progress = complete ? bi.block_size : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pi.blocks[j].num_peers = i->info[j].num_peers;
|
|
|
|
}
|
|
|
|
pi.piece_index = i->index;
|
|
|
|
queue.push_back(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool torrent::connect_to_peer(policy::peer* peerinfo)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-05-02 19:38:37 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(peerinfo);
|
|
|
|
TORRENT_ASSERT(peerinfo->connection == 0);
|
2008-03-29 20:39:24 +01:00
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
peerinfo->connected = time_now();
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-05-24 20:53:55 +02:00
|
|
|
// this asserts that we don't have duplicates in the policy's peer list
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_iterator i_ = std::find_if(m_connections.begin(), m_connections.end()
|
2008-07-14 13:15:35 +02:00
|
|
|
, bind(&peer_connection::remote, _1) == peerinfo->ip());
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i_ == m_connections.end()
|
2007-10-31 10:48:20 +01:00
|
|
|
|| dynamic_cast<bt_peer_connection*>(*i_) == 0);
|
2007-05-12 22:25:44 +02:00
|
|
|
#endif
|
2007-05-04 18:35:29 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(want_more_peers());
|
|
|
|
TORRENT_ASSERT(m_ses.num_connections() < m_ses.max_connections());
|
2007-05-02 19:38:37 +02:00
|
|
|
|
2008-07-14 13:15:35 +02:00
|
|
|
tcp::endpoint a(peerinfo->ip());
|
|
|
|
TORRENT_ASSERT((m_ses.m_ip_filter.access(peerinfo->addr) & ip_filter::blocked) == 0);
|
2006-11-24 15:22:52 +01:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
boost::shared_ptr<socket_type> s(new socket_type(m_ses.m_io_service));
|
2007-10-22 06:17:26 +02:00
|
|
|
|
|
|
|
bool ret = instantiate_connection(m_ses.m_io_service, m_ses.peer_proxy(), *s);
|
2008-08-19 16:53:50 +02:00
|
|
|
(void)ret;
|
2007-10-22 06:17:26 +02:00
|
|
|
TORRENT_ASSERT(ret);
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::intrusive_ptr<peer_connection> c(new bt_peer_connection(
|
2007-04-10 23:23:13 +02:00
|
|
|
m_ses, shared_from_this(), s, a, peerinfo));
|
2007-06-20 20:41:53 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-04-25 23:04:48 +02:00
|
|
|
c->m_in_constructor = false;
|
|
|
|
#endif
|
|
|
|
|
2008-04-07 10:15:31 +02:00
|
|
|
c->add_stat(peerinfo->prev_amount_download, peerinfo->prev_amount_upload);
|
|
|
|
peerinfo->prev_amount_download = 0;
|
|
|
|
peerinfo->prev_amount_upload = 0;
|
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2008-04-07 04:51:21 +02:00
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
2007-10-31 10:48:20 +01:00
|
|
|
boost::shared_ptr<peer_plugin> pp((*i)->new_connection(c.get()));
|
|
|
|
if (pp) c->add_extension(pp);
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
#endif
|
|
|
|
}
|
2007-10-31 10:48:20 +01:00
|
|
|
#endif
|
2007-09-19 08:05:14 +02:00
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
// add the newly connected peer to this torrent's peer list
|
|
|
|
m_connections.insert(boost::get_pointer(c));
|
|
|
|
m_ses.m_connections.insert(c);
|
2008-05-15 04:29:26 +02:00
|
|
|
peerinfo->connection = c.get();
|
2008-04-07 04:51:21 +02:00
|
|
|
c->start();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
int timeout = settings().peer_connect_timeout;
|
|
|
|
if (peerinfo) timeout += 3 * peerinfo->failcount;
|
2007-10-15 07:03:29 +02:00
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
2007-05-05 02:29:33 +02:00
|
|
|
m_ses.m_half_open.enqueue(
|
|
|
|
bind(&peer_connection::connect, c, _1)
|
|
|
|
, bind(&peer_connection::timed_out, c)
|
2007-10-15 07:03:29 +02:00
|
|
|
, seconds(timeout));
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
std::set<peer_connection*>::iterator i
|
|
|
|
= m_connections.find(boost::get_pointer(c));
|
2006-04-25 23:04:48 +02:00
|
|
|
if (i != m_connections.end()) m_connections.erase(i);
|
2008-01-07 02:10:46 +01:00
|
|
|
c->disconnect(e.what());
|
2007-10-31 10:48:20 +01:00
|
|
|
return false;
|
2006-01-06 21:20:20 +01:00
|
|
|
}
|
2008-04-07 04:51:21 +02:00
|
|
|
#endif
|
2008-09-25 09:40:55 +02:00
|
|
|
|
|
|
|
return peerinfo->connection;
|
2003-12-01 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2008-09-24 04:32:33 +02:00
|
|
|
bool torrent::set_metadata(char const* metadata_buf, int metadata_size)
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
2007-06-10 22:46:09 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-24 04:32:33 +02:00
|
|
|
if (m_torrent_file->is_valid()) return false;
|
|
|
|
|
|
|
|
hasher h;
|
|
|
|
h.update(metadata_buf, metadata_size);
|
|
|
|
sha1_hash info_hash = h.final();
|
|
|
|
|
|
|
|
if (info_hash != m_torrent_file->info_hash())
|
|
|
|
{
|
|
|
|
if (alerts().should_post<metadata_failed_alert>())
|
|
|
|
{
|
|
|
|
alerts().post_alert(metadata_failed_alert(get_handle()));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_entry metadata;
|
|
|
|
std::string error = "parser error";
|
|
|
|
int ret = lazy_bdecode(metadata_buf, metadata_buf + metadata_size, metadata);
|
|
|
|
if (ret != 0 || !m_torrent_file->parse_info_section(metadata, error))
|
2007-12-28 21:11:10 +01:00
|
|
|
{
|
2008-09-24 04:32:33 +02:00
|
|
|
// this means the metadata is correct, since we
|
|
|
|
// verified it against the info-hash, but we
|
|
|
|
// failed to parse it. Pause the torrent
|
|
|
|
if (alerts().should_post<metadata_failed_alert>())
|
|
|
|
{
|
|
|
|
alerts().post_alert(metadata_failed_alert(get_handle()));
|
|
|
|
}
|
|
|
|
set_error("invalid metadata: " + error);
|
|
|
|
pause();
|
2007-12-28 21:11:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<metadata_received_alert>())
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(metadata_received_alert(
|
2008-07-06 14:22:56 +02:00
|
|
|
get_handle()));
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
2008-03-08 07:06:31 +01:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
2007-12-29 08:16:36 +01:00
|
|
|
return true;
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
|
2008-04-07 04:51:21 +02:00
|
|
|
bool torrent::attach_peer(peer_connection* p)
|
2003-12-01 06:01:40 +01:00
|
|
|
{
|
2007-08-16 14:41:46 +02:00
|
|
|
// INVARIANT_CHECK;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p != 0);
|
|
|
|
TORRENT_ASSERT(!p->is_local());
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2008-06-05 20:19:03 +02:00
|
|
|
m_has_incoming = true;
|
|
|
|
|
2008-03-14 11:17:27 +01:00
|
|
|
if ((m_state == torrent_status::queued_for_checking
|
2008-11-19 01:46:48 +01:00
|
|
|
|| m_state == torrent_status::checking_files
|
|
|
|
|| m_state == torrent_status::checking_resume_data)
|
2008-03-14 11:17:27 +01:00
|
|
|
&& valid_metadata())
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
|
|
|
p->disconnect("torrent is not ready to accept peers");
|
2008-04-07 04:51:21 +02:00
|
|
|
return false;
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
if (m_ses.m_connections.find(p) == m_ses.m_connections.end())
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
p->disconnect("peer is not properly constructed");
|
2008-04-07 04:51:21 +02:00
|
|
|
return false;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
if (m_ses.is_aborted())
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
p->disconnect("session is closing");
|
2008-04-07 04:51:21 +02:00
|
|
|
return false;
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
|
|
|
|
2007-12-27 11:19:36 +01:00
|
|
|
if (int(m_connections.size()) >= m_max_connections)
|
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
p->disconnect("reached connection limit");
|
2008-04-07 04:51:21 +02:00
|
|
|
return false;
|
2007-12-27 11:19:36 +01:00
|
|
|
}
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-04-25 23:04:48 +02:00
|
|
|
try
|
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<peer_plugin> pp((*i)->new_connection(p));
|
|
|
|
if (pp) p->add_extension(pp);
|
|
|
|
}
|
|
|
|
#endif
|
2008-01-19 20:00:54 +01:00
|
|
|
if (!m_policy.new_connection(*p))
|
2008-04-07 04:51:21 +02:00
|
|
|
return false;
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
#if defined TORRENT_LOGGING
|
2007-12-19 22:36:54 +01:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " CLOSING CONNECTION "
|
|
|
|
<< p->remote() << " policy::new_connection threw: " << e.what() << "\n";
|
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
p->disconnect(e.what());
|
2008-04-07 04:51:21 +02:00
|
|
|
return false;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
2008-01-19 20:00:54 +01:00
|
|
|
TORRENT_ASSERT(m_connections.find(p) == m_connections.end());
|
|
|
|
peer_iterator ci = m_connections.insert(p).first;
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(p->remote() == p->get_socket()->remote_endpoint(ec) || ec);
|
|
|
|
#endif
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
|
2007-10-31 10:48:20 +01:00
|
|
|
m_policy.check_invariant();
|
2005-10-01 13:20:47 +02:00
|
|
|
#endif
|
2008-04-07 04:51:21 +02:00
|
|
|
return true;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-05-02 19:38:37 +02:00
|
|
|
bool torrent::want_more_peers() const
|
|
|
|
{
|
2007-08-16 14:41:46 +02:00
|
|
|
return int(m_connections.size()) < m_max_connections
|
2008-06-29 21:08:30 +02:00
|
|
|
&& !is_paused()
|
2008-11-19 01:46:48 +01:00
|
|
|
&& ((m_state != torrent_status::checking_files
|
|
|
|
&& m_state != torrent_status::checking_resume_data
|
|
|
|
&& m_state != torrent_status::queued_for_checking)
|
2008-05-18 20:30:26 +02:00
|
|
|
|| !valid_metadata())
|
2008-10-30 18:13:10 +01:00
|
|
|
&& m_policy.num_connect_candidates() > 0
|
|
|
|
&& !m_abort;
|
2007-05-02 19:38:37 +02:00
|
|
|
}
|
|
|
|
|
2004-01-20 23:59:21 +01:00
|
|
|
void torrent::disconnect_all()
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-09-27 06:58:40 +02:00
|
|
|
// doesn't work with the m_paused -> m_num_peers == 0 condition
|
|
|
|
// INVARIANT_CHECK;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
while (!m_connections.empty())
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_connection* p = *m_connections.begin();
|
|
|
|
TORRENT_ASSERT(p->associated_torrent().lock().get() == this);
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_abort)
|
2007-10-31 10:48:20 +01:00
|
|
|
(*p->m_logger) << "*** CLOSING CONNECTION 'aborting'\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
else
|
2007-10-31 10:48:20 +01:00
|
|
|
(*p->m_logger) << "*** CLOSING CONNECTION 'pausing'\n";
|
2005-09-01 23:04:21 +02:00
|
|
|
#endif
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-04-25 23:04:48 +02:00
|
|
|
std::size_t size = m_connections.size();
|
|
|
|
#endif
|
2008-01-17 21:03:59 +01:00
|
|
|
if (p->is_disconnecting())
|
2008-01-17 22:25:19 +01:00
|
|
|
m_connections.erase(m_connections.begin());
|
2008-01-17 21:03:59 +01:00
|
|
|
else
|
|
|
|
p->disconnect(m_abort?"stopping torrent":"pausing torrent");
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_connections.size() <= size);
|
2004-01-20 12:01:50 +01:00
|
|
|
}
|
|
|
|
}
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2008-05-12 07:17:11 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// this returns true if lhs is a better disconnect candidate than rhs
|
|
|
|
bool compare_disconnect_peer(peer_connection const* lhs, peer_connection const* rhs)
|
|
|
|
{
|
|
|
|
// prefer to disconnect peers we're not interested in
|
|
|
|
if (lhs->is_interesting() != rhs->is_interesting())
|
|
|
|
return rhs->is_interesting();
|
|
|
|
|
|
|
|
// prefer to disconnect peers that are not seeds
|
|
|
|
if (lhs->is_seed() != rhs->is_seed())
|
|
|
|
return rhs->is_seed();
|
|
|
|
|
|
|
|
// prefer to disconnect peers that are on parole
|
|
|
|
if (lhs->on_parole() != rhs->on_parole())
|
|
|
|
return lhs->on_parole();
|
|
|
|
|
|
|
|
// prefer to disconnect peers that send data at a lower rate
|
|
|
|
size_type lhs_transferred = lhs->statistics().total_payload_download();
|
|
|
|
size_type rhs_transferred = rhs->statistics().total_payload_download();
|
|
|
|
|
|
|
|
if (lhs_transferred != rhs_transferred
|
|
|
|
&& lhs_transferred > 0
|
|
|
|
&& rhs_transferred > 0)
|
|
|
|
{
|
|
|
|
ptime now = time_now();
|
|
|
|
size_type lhs_time_connected = total_seconds(now - lhs->connected_time());
|
|
|
|
size_type rhs_time_connected = total_seconds(now - rhs->connected_time());
|
|
|
|
|
|
|
|
double lhs_rate = double(lhs_transferred) / (lhs_time_connected + 1);
|
|
|
|
double rhs_rate = double(rhs_transferred) / (rhs_time_connected + 1);
|
|
|
|
|
|
|
|
return lhs_rate < rhs_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefer to disconnect peers that chokes us
|
|
|
|
if (lhs->is_choked() != rhs->is_choked())
|
|
|
|
return lhs->is_choked();
|
|
|
|
|
|
|
|
return lhs->last_received() < rhs->last_received();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int torrent::disconnect_peers(int num)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
// buils a list of all connected peers and sort it by 'disconnectability'.
|
|
|
|
std::vector<peer_connection*> peers(m_connections.size());
|
|
|
|
std::copy(m_connections.begin(), m_connections.end(), peers.begin());
|
|
|
|
std::sort(peers.begin(), peers.end(), boost::bind(&compare_disconnect_peer, _1, _2));
|
|
|
|
|
|
|
|
// never disconnect peers that connected less than 90 seconds ago
|
|
|
|
ptime cut_off = time_now() - seconds(90);
|
|
|
|
|
|
|
|
for (std::vector<peer_connection*>::iterator i = peers.begin()
|
|
|
|
, end(peers.end()); i != end && ret < num; ++i)
|
|
|
|
{
|
|
|
|
peer_connection* p = *i;
|
|
|
|
if (p->connected_time() > cut_off) continue;
|
|
|
|
++ret;
|
|
|
|
p->disconnect("optimistic disconnect");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-20 00:06:20 +01:00
|
|
|
int torrent::bandwidth_throttle(int channel) const
|
|
|
|
{
|
|
|
|
return m_bandwidth_limit[channel].throttle();
|
2007-02-19 01:01:39 +01:00
|
|
|
}
|
|
|
|
|
2008-01-15 09:37:48 +01:00
|
|
|
int torrent::bandwidth_queue_size(int channel) const
|
|
|
|
{
|
|
|
|
return (int)m_bandwidth_queue[channel].size();
|
|
|
|
}
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
void torrent::request_bandwidth(int channel
|
2007-07-03 01:48:06 +02:00
|
|
|
, boost::intrusive_ptr<peer_connection> const& p
|
2008-01-17 18:40:46 +01:00
|
|
|
, int max_block_size, int priority)
|
2007-01-10 16:02:25 +01:00
|
|
|
{
|
2008-01-07 05:47:20 +01:00
|
|
|
TORRENT_ASSERT(max_block_size > 0);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_bandwidth_limit[channel].throttle() > 0);
|
2007-12-31 10:41:50 +01:00
|
|
|
TORRENT_ASSERT(p->max_assignable_bandwidth(channel) > 0);
|
2008-01-14 00:46:43 +01:00
|
|
|
TORRENT_ASSERT(p->m_channel_state[channel] == peer_info::bw_torrent);
|
2008-01-07 05:47:20 +01:00
|
|
|
int block_size = (std::min)(m_bandwidth_limit[channel].throttle() / 10
|
|
|
|
, max_block_size);
|
2007-08-27 01:40:58 +02:00
|
|
|
if (block_size <= 0) block_size = 1;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
if (m_bandwidth_limit[channel].max_assignable() > 0)
|
2007-01-10 16:02:25 +01:00
|
|
|
{
|
2008-01-17 18:40:46 +01:00
|
|
|
perform_bandwidth_request(channel, p, block_size, priority);
|
2007-01-10 16:02:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-03 01:48:06 +02:00
|
|
|
// skip forward in the queue until we find a prioritized peer
|
|
|
|
// or hit the front of it.
|
|
|
|
queue_t::reverse_iterator i = m_bandwidth_queue[channel].rbegin();
|
2008-01-17 18:40:46 +01:00
|
|
|
while (i != m_bandwidth_queue[channel].rend() && priority > i->priority)
|
|
|
|
{
|
|
|
|
++i->priority;
|
|
|
|
++i;
|
|
|
|
}
|
2008-01-27 22:03:33 +01:00
|
|
|
m_bandwidth_queue[channel].insert(i.base(), bw_queue_entry<peer_connection, torrent>(
|
2008-01-17 18:40:46 +01:00
|
|
|
p, block_size, priority));
|
2007-01-10 16:02:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::expire_bandwidth(int channel, int amount)
|
|
|
|
{
|
2007-01-30 18:56:42 +01:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-01-10 23:13:23 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(amount > 0);
|
2007-01-10 16:02:25 +01:00
|
|
|
m_bandwidth_limit[channel].expire(amount);
|
2007-12-31 10:41:50 +01:00
|
|
|
queue_t tmp;
|
2007-07-03 01:48:06 +02:00
|
|
|
while (!m_bandwidth_queue[channel].empty())
|
2007-01-10 16:02:25 +01:00
|
|
|
{
|
2008-01-27 22:03:33 +01:00
|
|
|
bw_queue_entry<peer_connection, torrent> qe = m_bandwidth_queue[channel].front();
|
2007-07-03 01:48:06 +02:00
|
|
|
if (m_bandwidth_limit[channel].max_assignable() == 0)
|
|
|
|
break;
|
2007-01-10 16:02:25 +01:00
|
|
|
m_bandwidth_queue[channel].pop_front();
|
2007-12-31 10:41:50 +01:00
|
|
|
if (qe.peer->max_assignable_bandwidth(channel) <= 0)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_ses.m_bandwidth_manager[channel]->is_in_history(qe.peer.get()));
|
|
|
|
if (!qe.peer->is_disconnecting()) tmp.push_back(qe);
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
perform_bandwidth_request(channel, qe.peer
|
2008-01-17 18:40:46 +01:00
|
|
|
, qe.max_block_size, qe.priority);
|
2007-01-10 16:02:25 +01:00
|
|
|
}
|
2007-12-31 10:41:50 +01:00
|
|
|
m_bandwidth_queue[channel].insert(m_bandwidth_queue[channel].begin(), tmp.begin(), tmp.end());
|
2007-01-10 16:02:25 +01:00
|
|
|
}
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
void torrent::perform_bandwidth_request(int channel
|
|
|
|
, boost::intrusive_ptr<peer_connection> const& p
|
|
|
|
, int block_size
|
2008-01-17 18:40:46 +01:00
|
|
|
, int priority)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2008-01-14 00:46:43 +01:00
|
|
|
TORRENT_ASSERT(p->m_channel_state[channel] == peer_info::bw_torrent);
|
|
|
|
p->m_channel_state[channel] = peer_info::bw_global;
|
2007-07-03 01:48:06 +02:00
|
|
|
m_ses.m_bandwidth_manager[channel]->request_bandwidth(p
|
2008-01-17 18:40:46 +01:00
|
|
|
, block_size, priority);
|
2007-07-03 01:48:06 +02:00
|
|
|
m_bandwidth_limit[channel].assign(block_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::assign_bandwidth(int channel, int amount, int blk)
|
2007-01-10 16:02:25 +01:00
|
|
|
{
|
2007-01-30 18:56:42 +01:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(amount > 0);
|
|
|
|
TORRENT_ASSERT(amount <= blk);
|
2007-07-03 01:48:06 +02:00
|
|
|
if (amount < blk)
|
|
|
|
expire_bandwidth(channel, blk - amount);
|
2007-01-10 16:02:25 +01:00
|
|
|
}
|
|
|
|
|
2007-10-18 02:32:16 +02:00
|
|
|
// called when torrent is finished (all interesting
|
|
|
|
// pieces have been downloaded)
|
2005-06-11 01:12:50 +02:00
|
|
|
void torrent::finished()
|
2004-01-20 12:01:50 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-10-05 03:44:57 +02:00
|
|
|
TORRENT_ASSERT(is_finished());
|
|
|
|
TORRENT_ASSERT(m_state != torrent_status::finished && m_state != torrent_status::seeding);
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<torrent_finished_alert>())
|
2004-01-21 14:16:11 +01:00
|
|
|
{
|
|
|
|
alerts().post_alert(torrent_finished_alert(
|
2008-07-06 14:22:56 +02:00
|
|
|
get_handle()));
|
2004-01-21 14:16:11 +01:00
|
|
|
}
|
|
|
|
|
2008-07-03 12:05:51 +02:00
|
|
|
set_state(torrent_status::finished);
|
2008-05-29 05:37:19 +02:00
|
|
|
set_queue_position(-1);
|
|
|
|
|
|
|
|
// we have to call completed() before we start
|
|
|
|
// disconnecting peers, since there's an assert
|
|
|
|
// to make sure we're cleared the piece picker
|
|
|
|
if (is_seed()) completed();
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-08-18 22:02:50 +02:00
|
|
|
// disconnect all seeds
|
|
|
|
// TODO: should disconnect all peers that have the pieces we have
|
|
|
|
// not just seeds
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<peer_connection*> seeds;
|
2004-01-20 12:01:50 +01:00
|
|
|
for (peer_iterator i = m_connections.begin();
|
2005-06-11 01:12:50 +02:00
|
|
|
i != m_connections.end(); ++i)
|
2004-01-20 12:01:50 +01:00
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_connection* p = *i;
|
|
|
|
TORRENT_ASSERT(p->associated_torrent().lock().get() == this);
|
2008-05-20 05:21:45 +02:00
|
|
|
if (p->upload_only())
|
2005-09-01 23:04:21 +02:00
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-10-31 10:48:20 +01:00
|
|
|
(*p->m_logger) << "*** SEED, CLOSING CONNECTION\n";
|
2005-09-01 23:04:21 +02:00
|
|
|
#endif
|
2007-10-31 10:48:20 +01:00
|
|
|
seeds.push_back(p);
|
2005-09-01 23:04:21 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
std::for_each(seeds.begin(), seeds.end()
|
2008-05-12 08:03:31 +02:00
|
|
|
, bind(&peer_connection::disconnect, _1, "torrent finished, disconnecting seed", 0));
|
2004-01-21 14:16:11 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_storage);
|
2007-10-02 20:34:34 +02:00
|
|
|
// we need to keep the object alive during this operation
|
|
|
|
m_storage->async_release_files(
|
|
|
|
bind(&torrent::on_files_released, shared_from_this(), _1, _2));
|
2005-06-11 01:12:50 +02:00
|
|
|
}
|
2008-05-20 05:21:45 +02:00
|
|
|
|
|
|
|
// this is called when we were finished, but some files were
|
|
|
|
// marked for downloading, and we are no longer finished
|
|
|
|
void torrent::resume_download()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2005-06-11 01:12:50 +02:00
|
|
|
|
2008-06-08 15:47:35 +02:00
|
|
|
TORRENT_ASSERT(!is_finished());
|
2008-07-03 12:05:51 +02:00
|
|
|
set_state(torrent_status::downloading);
|
2008-05-29 05:37:19 +02:00
|
|
|
set_queue_position((std::numeric_limits<int>::max)());
|
2008-05-20 05:21:45 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 01:12:50 +02:00
|
|
|
// called when torrent is complete (all pieces downloaded)
|
|
|
|
void torrent::completed()
|
|
|
|
{
|
2008-05-29 05:37:19 +02:00
|
|
|
m_picker.reset();
|
|
|
|
|
2008-07-03 12:05:51 +02:00
|
|
|
set_state(torrent_status::seeding);
|
2008-11-29 09:38:40 +01:00
|
|
|
if (!m_announcing) return;
|
|
|
|
|
|
|
|
ptime now = time_now();
|
|
|
|
for (std::vector<announce_entry>::iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->complete_sent) continue;
|
|
|
|
i->next_announce = now;
|
|
|
|
}
|
|
|
|
announce_with_tracker();
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2004-09-12 12:12:16 +02:00
|
|
|
// this will move the tracker with the given index
|
|
|
|
// to a prioritized position in the list (move it towards
|
|
|
|
// the begining) and return the new index to the tracker.
|
|
|
|
int torrent::prioritize_tracker(int index)
|
|
|
|
{
|
2006-05-28 21:03:54 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
2008-11-27 21:48:29 +01:00
|
|
|
TORRENT_ASSERT(index < m_trackers.size());
|
|
|
|
if (index >= (int)m_trackers.size()) return -1;
|
2004-09-12 12:12:16 +02:00
|
|
|
|
|
|
|
while (index > 0 && m_trackers[index].tier == m_trackers[index-1].tier)
|
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
using std::swap;
|
|
|
|
swap(m_trackers[index], m_trackers[index-1]);
|
2008-12-12 11:00:20 +01:00
|
|
|
if (m_last_working_tracker == index) --m_last_working_tracker;
|
|
|
|
else if (m_last_working_tracker == index - 1) ++m_last_working_tracker;
|
2004-09-12 12:12:16 +02:00
|
|
|
--index;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
int torrent::deprioritize_tracker(int index)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_trackers.size());
|
|
|
|
if (index >= (int)m_trackers.size()) return -1;
|
2008-07-12 10:25:19 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
while (index < m_trackers.size() - 1 && m_trackers[index].tier == m_trackers[index + 1].tier)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
using std::swap;
|
|
|
|
swap(m_trackers[index], m_trackers[index + 1]);
|
2008-12-12 11:00:20 +01:00
|
|
|
if (m_last_working_tracker == index) ++m_last_working_tracker;
|
|
|
|
else if (m_last_working_tracker == index + 1) --m_last_working_tracker;
|
2008-11-29 09:38:40 +01:00
|
|
|
++index;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
return index;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
void torrent::files_checked()
|
2005-10-13 09:59:05 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_torrent_file->is_valid());
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
2003-12-07 16:03:06 +01:00
|
|
|
|
2008-10-05 03:44:57 +02:00
|
|
|
// we might be finished already, in which case we should
|
|
|
|
// not switch to downloading mode.
|
|
|
|
if (m_state != torrent_status::finished)
|
|
|
|
set_state(torrent_status::downloading);
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2008-08-18 22:02:50 +02:00
|
|
|
if (m_ses.m_alerts.should_post<torrent_checked_alert>())
|
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(torrent_checked_alert(
|
|
|
|
get_handle()));
|
|
|
|
}
|
|
|
|
|
2006-12-04 13:20:34 +01:00
|
|
|
if (!is_seed())
|
|
|
|
{
|
2008-12-08 07:36:22 +01:00
|
|
|
// turn off super seeding if we're not a seed
|
|
|
|
if (m_super_seeding) m_super_seeding = false;
|
|
|
|
|
2008-06-23 20:56:55 +02:00
|
|
|
// if we just finished checking and we're not a seed, we are
|
|
|
|
// likely to be unpaused
|
|
|
|
if (m_ses.m_auto_manage_time_scaler > 1)
|
|
|
|
m_ses.m_auto_manage_time_scaler = 1;
|
2008-08-18 22:02:50 +02:00
|
|
|
|
2008-10-05 06:44:29 +02:00
|
|
|
if (is_finished() && m_state != torrent_status::finished) finished();
|
2008-08-18 22:02:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
for (std::vector<announce_entry>::iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
i->complete_sent = true;
|
2008-10-05 06:44:29 +02:00
|
|
|
if (m_state != torrent_status::finished) finished();
|
2006-12-04 13:20:34 +01:00
|
|
|
}
|
2007-05-10 00:54:26 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2008-02-14 04:48:20 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
(*i)->on_files_checked();
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
#endif
|
2007-05-10 00:54:26 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
if (!m_connections_initialized)
|
|
|
|
{
|
|
|
|
m_connections_initialized = true;
|
|
|
|
// all peer connections have to initialize themselves now that the metadata
|
|
|
|
// is available
|
2008-07-18 12:03:42 +02:00
|
|
|
for (torrent::peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end();)
|
2005-10-16 11:15:46 +02:00
|
|
|
{
|
2008-07-18 12:03:42 +02:00
|
|
|
peer_connection* pc = *i;
|
2008-04-07 04:51:21 +02:00
|
|
|
++i;
|
2008-08-29 19:21:56 +02:00
|
|
|
if (pc->is_disconnecting()) continue;
|
|
|
|
pc->on_metadata_impl();
|
|
|
|
if (pc->is_disconnecting()) continue;
|
2008-07-18 12:03:42 +02:00
|
|
|
pc->init();
|
2005-10-16 11:15:46 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-23 03:01:00 +02:00
|
|
|
|
2007-12-14 07:09:44 +01:00
|
|
|
m_files_checked = true;
|
2008-07-12 10:25:19 +02:00
|
|
|
|
|
|
|
start_announcing();
|
2003-11-07 02:44:30 +01:00
|
|
|
}
|
|
|
|
|
2004-01-07 01:48:02 +01:00
|
|
|
alert_manager& torrent::alerts() const
|
|
|
|
{
|
|
|
|
return m_ses.m_alerts;
|
|
|
|
}
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
fs::path torrent::save_path() const
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2008-03-08 07:06:31 +01:00
|
|
|
return m_save_path;
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
bool torrent::rename_file(int index, std::string const& name)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < m_torrent_file->num_files());
|
|
|
|
|
|
|
|
if (!m_owning_storage.get()) return false;
|
|
|
|
|
|
|
|
m_owning_storage->async_rename_file(index, name
|
|
|
|
, bind(&torrent::on_file_renamed, shared_from_this(), _1, _2));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void torrent::move_storage(fs::path const& save_path)
|
2004-07-18 02:39:58 +02:00
|
|
|
{
|
2006-05-28 21:03:54 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
if (m_owning_storage.get())
|
2005-01-10 12:14:22 +01:00
|
|
|
{
|
2007-06-11 23:24:14 +02:00
|
|
|
m_owning_storage->async_move_storage(save_path
|
|
|
|
, bind(&torrent::on_storage_moved, shared_from_this(), _1, _2));
|
2005-01-10 12:14:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_save_path = save_path;
|
|
|
|
}
|
2004-07-18 02:39:58 +02:00
|
|
|
}
|
|
|
|
|
2007-06-11 23:24:14 +02:00
|
|
|
void torrent::on_storage_moved(int ret, disk_io_job const& j)
|
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<storage_moved_alert>())
|
2007-06-11 23:24:14 +02:00
|
|
|
{
|
|
|
|
alerts().post_alert(storage_moved_alert(get_handle(), j.str));
|
|
|
|
}
|
2008-06-15 22:52:46 +02:00
|
|
|
m_save_path = j.str;
|
2007-06-11 23:24:14 +02:00
|
|
|
}
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
piece_manager& torrent::filesystem()
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_owning_storage.get());
|
2008-06-22 22:28:03 +02:00
|
|
|
TORRENT_ASSERT(m_storage);
|
|
|
|
return *m_storage;
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-09 22:09:36 +02:00
|
|
|
torrent_handle torrent::get_handle()
|
2004-01-07 01:48:02 +01:00
|
|
|
{
|
2008-04-09 22:09:36 +02:00
|
|
|
return torrent_handle(shared_from_this());
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
2006-05-15 00:30:05 +02:00
|
|
|
session_settings const& torrent::settings() const
|
|
|
|
{
|
2006-10-11 16:02:21 +02:00
|
|
|
return m_ses.settings();
|
2006-05-15 00:30:05 +02:00
|
|
|
}
|
2004-01-07 01:48:02 +01:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2004-06-14 01:30:42 +02:00
|
|
|
void torrent::check_invariant() const
|
2003-12-14 06:56:12 +01:00
|
|
|
{
|
2007-10-03 19:40:18 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2008-09-26 00:08:18 +02:00
|
|
|
if (is_paused()) TORRENT_ASSERT(num_peers() == 0);
|
|
|
|
|
2008-11-19 01:46:48 +01:00
|
|
|
if (!should_check_files())
|
|
|
|
TORRENT_ASSERT(m_state != torrent_status::checking_files);
|
|
|
|
|
2008-08-10 11:34:39 +02:00
|
|
|
if (!m_ses.m_queued_for_checking.empty())
|
|
|
|
{
|
|
|
|
// if there are torrents waiting to be checked
|
|
|
|
// assert that there's a torrent that is being
|
|
|
|
// processed right now
|
2008-08-10 12:22:36 +02:00
|
|
|
int found = 0;
|
2008-11-18 12:01:30 +01:00
|
|
|
int found_active = 0;
|
2008-08-10 11:34:39 +02:00
|
|
|
for (aux::session_impl::torrent_map::iterator i = m_ses.m_torrents.begin()
|
|
|
|
, end(m_ses.m_torrents.end()); i != end; ++i)
|
2008-11-18 12:01:30 +01:00
|
|
|
if (i->second->m_state == torrent_status::checking_files)
|
|
|
|
{
|
|
|
|
++found;
|
|
|
|
if (i->second->should_check_files()) ++found_active;
|
|
|
|
}
|
2008-08-10 17:00:56 +02:00
|
|
|
// the case of 2 is in the special case where one switches over from
|
2008-11-18 12:01:30 +01:00
|
|
|
// checking to complete.
|
|
|
|
TORRENT_ASSERT(found_active == 1 || found_active == 2);
|
|
|
|
TORRENT_ASSERT(found >= 1);
|
2008-08-10 11:34:39 +02:00
|
|
|
}
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
TORRENT_ASSERT(m_resume_entry.type() == lazy_entry::dict_t
|
|
|
|
|| m_resume_entry.type() == lazy_entry::none_t);
|
2008-04-13 00:08:07 +02:00
|
|
|
|
2008-01-06 01:58:51 +01:00
|
|
|
TORRENT_ASSERT(m_bandwidth_queue[0].size() <= m_connections.size());
|
|
|
|
TORRENT_ASSERT(m_bandwidth_queue[1].size() <= m_connections.size());
|
|
|
|
|
2008-01-17 18:40:46 +01:00
|
|
|
for (int c = 0; c < 2; ++c)
|
|
|
|
{
|
|
|
|
queue_t::const_iterator j = m_bandwidth_queue[c].begin();
|
2008-01-19 19:12:02 +01:00
|
|
|
if (j == m_bandwidth_queue[c].end()) continue;
|
2008-01-17 18:40:46 +01:00
|
|
|
++j;
|
|
|
|
for (queue_t::const_iterator i = m_bandwidth_queue[c].begin()
|
|
|
|
, end(m_bandwidth_queue[c].end()); i != end && j != end; ++i, ++j)
|
|
|
|
TORRENT_ASSERT(i->priority >= j->priority);
|
|
|
|
}
|
|
|
|
|
2007-08-17 09:37:08 +02:00
|
|
|
int num_uploads = 0;
|
2007-07-08 22:45:42 +02:00
|
|
|
std::map<piece_block, int> num_requests;
|
2006-04-25 23:04:48 +02:00
|
|
|
for (const_peer_iterator i = begin(); i != end(); ++i)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
2008-07-12 19:51:59 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-07 02:10:46 +01:00
|
|
|
// make sure this peer is not a dangling pointer
|
|
|
|
TORRENT_ASSERT(m_ses.has_peer(*i));
|
2008-07-12 19:51:59 +02:00
|
|
|
#endif
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_connection const& p = *(*i);
|
2007-07-08 22:45:42 +02:00
|
|
|
for (std::deque<piece_block>::const_iterator i = p.request_queue().begin()
|
|
|
|
, end(p.request_queue().end()); i != end; ++i)
|
|
|
|
++num_requests[*i];
|
2008-07-07 14:04:06 +02:00
|
|
|
for (std::deque<pending_block>::const_iterator i = p.download_queue().begin()
|
2007-07-08 22:45:42 +02:00
|
|
|
, end(p.download_queue().end()); i != end; ++i)
|
2008-07-07 14:04:06 +02:00
|
|
|
++num_requests[i->block];
|
2007-08-17 09:37:08 +02:00
|
|
|
if (!p.is_choked()) ++num_uploads;
|
2006-05-28 21:03:54 +02:00
|
|
|
torrent* associated_torrent = p.associated_torrent().lock().get();
|
|
|
|
if (associated_torrent != this)
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(num_uploads == m_num_uploads);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-07-08 22:45:42 +02:00
|
|
|
if (has_picker())
|
|
|
|
{
|
|
|
|
for (std::map<piece_block, int>::iterator i = num_requests.begin()
|
|
|
|
, end(num_requests.end()); i != end; ++i)
|
|
|
|
{
|
2008-01-02 04:18:29 +01:00
|
|
|
if (!m_picker->is_downloaded(i->first))
|
|
|
|
TORRENT_ASSERT(m_picker->num_peers(i->first) == i->second);
|
2007-07-08 22:45:42 +02:00
|
|
|
}
|
2008-06-07 16:03:21 +02:00
|
|
|
TORRENT_ASSERT(num_have() >= m_picker->num_have_filtered());
|
2007-07-08 22:45:42 +02:00
|
|
|
}
|
|
|
|
|
2006-12-11 13:48:33 +01:00
|
|
|
if (valid_metadata())
|
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
TORRENT_ASSERT(m_abort || !m_picker || m_picker->num_pieces() == m_torrent_file->num_pieces());
|
2006-12-11 13:48:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-12 23:24:55 +01:00
|
|
|
TORRENT_ASSERT(m_abort || !m_picker || m_picker->num_pieces() == 0);
|
2006-12-11 13:48:33 +01:00
|
|
|
}
|
|
|
|
|
2008-07-12 19:51:59 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2007-10-31 10:48:20 +01:00
|
|
|
for (policy::const_iterator i = m_policy.begin_peer()
|
|
|
|
, end(m_policy.end_peer()); i != end; ++i)
|
2007-09-19 08:05:14 +02:00
|
|
|
{
|
2008-07-30 10:52:39 +02:00
|
|
|
TORRENT_ASSERT(i->second.addr == i->first);
|
2007-09-19 08:05:14 +02:00
|
|
|
}
|
2008-07-12 19:51:59 +02:00
|
|
|
#endif
|
2007-10-31 10:48:20 +01:00
|
|
|
|
2006-12-11 13:48:33 +01:00
|
|
|
size_type total_done = quantized_bytes_done();
|
2007-09-01 05:00:31 +02:00
|
|
|
if (m_torrent_file->is_valid())
|
2007-02-15 04:03:50 +01:00
|
|
|
{
|
|
|
|
if (is_seed())
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(total_done == m_torrent_file->total_size());
|
2007-02-15 04:03:50 +01:00
|
|
|
else
|
2008-06-07 04:58:28 +02:00
|
|
|
TORRENT_ASSERT(total_done != m_torrent_file->total_size() || !m_files_checked);
|
2008-06-18 14:34:39 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(m_block_size <= m_torrent_file->piece_length());
|
2007-02-15 04:03:50 +01:00
|
|
|
}
|
2006-12-11 13:48:33 +01:00
|
|
|
else
|
2007-02-15 04:03:50 +01:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(total_done == 0);
|
2007-02-15 04:03:50 +01:00
|
|
|
}
|
2006-12-11 13:48:33 +01:00
|
|
|
|
2007-10-18 06:18:09 +02:00
|
|
|
if (m_picker && !m_abort)
|
2007-10-18 02:32:16 +02:00
|
|
|
{
|
|
|
|
// make sure that pieces that have completed the download
|
|
|
|
// of all their blocks are in the disk io thread's queue
|
|
|
|
// to be checked.
|
|
|
|
const std::vector<piece_picker::downloading_piece>& dl_queue
|
|
|
|
= m_picker->get_download_queue();
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i =
|
|
|
|
dl_queue.begin(); i != dl_queue.end(); ++i)
|
|
|
|
{
|
|
|
|
const int blocks_per_piece = m_picker->blocks_in_piece(i->index);
|
|
|
|
|
|
|
|
bool complete = true;
|
|
|
|
for (int j = 0; j < blocks_per_piece; ++j)
|
|
|
|
{
|
|
|
|
if (i->info[j].state == piece_picker::block_info::state_finished)
|
|
|
|
continue;
|
|
|
|
complete = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// This check is very expensive.
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!valid_metadata() || m_block_size > 0);
|
|
|
|
TORRENT_ASSERT(!valid_metadata() || (m_torrent_file->piece_length() % m_block_size) == 0);
|
|
|
|
// if (is_seed()) TORRENT_ASSERT(m_picker.get() == 0);
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
void torrent::set_sequential_download(bool sd)
|
2008-09-06 23:04:57 +02:00
|
|
|
{ m_sequential_download = sd; }
|
2006-09-04 19:17:45 +02:00
|
|
|
|
2008-05-29 05:37:19 +02:00
|
|
|
void torrent::set_queue_position(int p)
|
|
|
|
{
|
2008-06-17 18:49:55 +02:00
|
|
|
TORRENT_ASSERT((p == -1) == is_finished()
|
|
|
|
|| (!m_auto_managed && p == -1)
|
|
|
|
|| (m_abort && p == -1));
|
2008-06-08 15:47:35 +02:00
|
|
|
if (is_finished() && p != -1) return;
|
2008-05-29 05:37:19 +02:00
|
|
|
if (p == m_sequence_number) return;
|
|
|
|
|
|
|
|
session_impl::torrent_map& torrents = m_ses.m_torrents;
|
2008-06-17 00:27:14 +02:00
|
|
|
if (p >= 0 && m_sequence_number == -1)
|
|
|
|
{
|
2008-06-21 17:51:03 +02:00
|
|
|
int max_seq = -1;
|
2008-06-17 00:27:14 +02:00
|
|
|
for (session_impl::torrent_map::iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent* t = i->second.get();
|
|
|
|
if (t->m_sequence_number > max_seq) max_seq = t->m_sequence_number;
|
|
|
|
}
|
|
|
|
m_sequence_number = (std::min)(max_seq + 1, p);
|
|
|
|
}
|
|
|
|
else if (p < 0)
|
2008-05-29 05:37:19 +02:00
|
|
|
{
|
|
|
|
for (session_impl::torrent_map::iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent* t = i->second.get();
|
|
|
|
if (t == this) continue;
|
|
|
|
if (t->m_sequence_number >= m_sequence_number
|
|
|
|
&& t->m_sequence_number != -1)
|
|
|
|
--t->m_sequence_number;
|
|
|
|
}
|
|
|
|
m_sequence_number = p;
|
|
|
|
}
|
|
|
|
else if (p < m_sequence_number)
|
|
|
|
{
|
|
|
|
for (session_impl::torrent_map::iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent* t = i->second.get();
|
|
|
|
if (t == this) continue;
|
2008-06-11 14:14:10 +02:00
|
|
|
if (t->m_sequence_number >= p
|
|
|
|
&& t->m_sequence_number < m_sequence_number
|
2008-05-29 05:37:19 +02:00
|
|
|
&& t->m_sequence_number != -1)
|
|
|
|
++t->m_sequence_number;
|
|
|
|
}
|
|
|
|
m_sequence_number = p;
|
|
|
|
}
|
|
|
|
else if (p > m_sequence_number)
|
|
|
|
{
|
|
|
|
int max_seq = 0;
|
|
|
|
for (session_impl::torrent_map::iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent* t = i->second.get();
|
|
|
|
int pos = t->m_sequence_number;
|
2008-06-11 14:14:10 +02:00
|
|
|
if (pos > max_seq) max_seq = pos;
|
|
|
|
if (t == this) continue;
|
|
|
|
|
2008-05-29 05:37:19 +02:00
|
|
|
if (pos <= p
|
2008-06-17 00:27:14 +02:00
|
|
|
&& pos > m_sequence_number
|
|
|
|
&& pos != -1)
|
2008-05-29 05:37:19 +02:00
|
|
|
--t->m_sequence_number;
|
2008-06-11 14:14:10 +02:00
|
|
|
|
2008-05-29 05:37:19 +02:00
|
|
|
}
|
2008-06-11 14:14:10 +02:00
|
|
|
m_sequence_number = (std::min)(max_seq, p);
|
2008-05-29 05:37:19 +02:00
|
|
|
}
|
2008-06-03 22:21:47 +02:00
|
|
|
|
|
|
|
if (m_ses.m_auto_manage_time_scaler > 2)
|
|
|
|
m_ses.m_auto_manage_time_scaler = 2;
|
2008-05-29 05:37:19 +02:00
|
|
|
}
|
2006-09-04 19:17:45 +02:00
|
|
|
|
2004-10-29 15:21:09 +02:00
|
|
|
void torrent::set_max_uploads(int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2007-08-21 06:46:17 +02:00
|
|
|
if (limit <= 0) limit = (std::numeric_limits<int>::max)();
|
2007-08-16 14:41:46 +02:00
|
|
|
m_max_uploads = limit;
|
2004-10-29 15:21:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::set_max_connections(int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2007-08-21 06:46:17 +02:00
|
|
|
if (limit <= 0) limit = (std::numeric_limits<int>::max)();
|
2007-08-16 14:41:46 +02:00
|
|
|
m_max_connections = limit;
|
2004-10-29 15:21:09 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void torrent::set_peer_upload_limit(tcp::endpoint ip, int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_iterator i = std::find_if(m_connections.begin(), m_connections.end()
|
|
|
|
, bind(&peer_connection::remote, _1) == ip);
|
|
|
|
if (i == m_connections.end()) return;
|
|
|
|
(*i)->set_upload_limit(limit);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::set_peer_download_limit(tcp::endpoint ip, int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_iterator i = std::find_if(m_connections.begin(), m_connections.end()
|
|
|
|
, bind(&peer_connection::remote, _1) == ip);
|
|
|
|
if (i == m_connections.end()) return;
|
|
|
|
(*i)->set_download_limit(limit);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
void torrent::set_upload_limit(int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2007-08-21 06:46:17 +02:00
|
|
|
if (limit <= 0) limit = (std::numeric_limits<int>::max)();
|
2004-07-24 13:54:17 +02:00
|
|
|
if (limit < num_peers() * 10) limit = num_peers() * 10;
|
2007-01-10 16:02:25 +01:00
|
|
|
m_bandwidth_limit[peer_connection::upload_channel].throttle(limit);
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
|
|
|
|
2007-04-10 11:25:17 +02:00
|
|
|
int torrent::upload_limit() const
|
|
|
|
{
|
|
|
|
int limit = m_bandwidth_limit[peer_connection::upload_channel].throttle();
|
2007-08-21 06:46:17 +02:00
|
|
|
if (limit == (std::numeric_limits<int>::max)()) limit = -1;
|
2007-04-10 11:25:17 +02:00
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
void torrent::set_download_limit(int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2007-08-21 06:46:17 +02:00
|
|
|
if (limit <= 0) limit = (std::numeric_limits<int>::max)();
|
2004-07-24 13:54:17 +02:00
|
|
|
if (limit < num_peers() * 10) limit = num_peers() * 10;
|
2007-01-10 16:02:25 +01:00
|
|
|
m_bandwidth_limit[peer_connection::download_channel].throttle(limit);
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
|
|
|
|
2007-04-10 11:25:17 +02:00
|
|
|
int torrent::download_limit() const
|
|
|
|
{
|
|
|
|
int limit = m_bandwidth_limit[peer_connection::download_channel].throttle();
|
2007-08-21 06:46:17 +02:00
|
|
|
if (limit == (std::numeric_limits<int>::max)()) limit = -1;
|
2007-04-10 11:25:17 +02:00
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2007-10-13 05:33:33 +02:00
|
|
|
void torrent::delete_files()
|
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2007-10-13 05:33:33 +02:00
|
|
|
for (peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end(); ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
(*(*i)->m_logger) << "*** DELETING FILES IN TORRENT\n";
|
2007-10-13 05:33:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
disconnect_all();
|
2008-07-12 10:25:19 +02:00
|
|
|
stop_announcing();
|
2007-10-13 05:33:33 +02:00
|
|
|
|
|
|
|
if (m_owning_storage.get())
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_storage);
|
|
|
|
m_storage->async_delete_files(
|
|
|
|
bind(&torrent::on_files_deleted, shared_from_this(), _1, _2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-12 19:00:52 +02:00
|
|
|
void torrent::clear_error()
|
|
|
|
{
|
|
|
|
if (m_error.empty()) return;
|
2008-11-19 01:46:48 +01:00
|
|
|
bool checking_files = should_check_files();
|
2008-07-12 19:00:52 +02:00
|
|
|
if (m_ses.m_auto_manage_time_scaler > 2)
|
|
|
|
m_ses.m_auto_manage_time_scaler = 2;
|
|
|
|
m_error.clear();
|
2008-11-19 01:46:48 +01:00
|
|
|
if (!checking_files && should_check_files())
|
|
|
|
m_ses.check_torrent(shared_from_this());
|
2008-07-12 19:00:52 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
void torrent::auto_managed(bool a)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
if (m_auto_managed == a) return;
|
2008-09-20 19:42:25 +02:00
|
|
|
bool checking_files = should_check_files();
|
2008-04-24 05:28:48 +02:00
|
|
|
m_auto_managed = a;
|
|
|
|
// recalculate which torrents should be
|
|
|
|
// paused
|
|
|
|
m_ses.m_auto_manage_time_scaler = 0;
|
2008-09-20 19:42:25 +02:00
|
|
|
|
|
|
|
if (!checking_files && should_check_files())
|
|
|
|
m_ses.check_torrent(shared_from_this());
|
|
|
|
else if (checking_files && !should_check_files())
|
|
|
|
{
|
2008-11-18 12:01:30 +01:00
|
|
|
// stop checking
|
|
|
|
m_storage->abort_disk_io();
|
|
|
|
m_ses.done_checking(shared_from_this());
|
2008-11-19 01:46:48 +01:00
|
|
|
set_state(torrent_status::queued_for_checking);
|
2008-09-20 19:42:25 +02:00
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
|
|
|
|
2008-05-06 20:03:41 +02:00
|
|
|
// the higher seed rank, the more important to seed
|
|
|
|
int torrent::seed_rank(session_settings const& s) const
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
2008-05-06 20:03:41 +02:00
|
|
|
enum flags
|
|
|
|
{
|
|
|
|
seed_ratio_not_met = 0x400000,
|
|
|
|
recently_started = 0x200000,
|
|
|
|
no_seeds = 0x100000,
|
|
|
|
prio_mask = 0xfffff
|
|
|
|
};
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2008-10-19 10:05:36 +02:00
|
|
|
if (!is_finished()) return 0;
|
|
|
|
|
|
|
|
int scale = 100;
|
|
|
|
if (!is_seed()) scale = 50;
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2008-05-06 20:03:41 +02:00
|
|
|
int ret = 0;
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2008-05-06 20:03:41 +02:00
|
|
|
ptime now(time_now());
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2008-05-06 20:03:41 +02:00
|
|
|
int seed_time = total_seconds(m_seeding_time);
|
|
|
|
int download_time = total_seconds(m_active_time) - seed_time;
|
|
|
|
|
|
|
|
// if we haven't yet met the seed limits, set the seed_ratio_not_met
|
|
|
|
// flag. That will make this seed prioritized
|
2008-04-24 05:28:48 +02:00
|
|
|
size_type downloaded = (std::max)(m_total_downloaded, m_torrent_file->total_size());
|
2008-05-07 19:05:16 +02:00
|
|
|
if (seed_time < s.seed_time_limit
|
|
|
|
&& (seed_time > 1 && download_time / float(seed_time) < s.seed_time_ratio_limit)
|
|
|
|
&& m_total_uploaded / downloaded < s.share_ratio_limit)
|
2008-05-06 20:03:41 +02:00
|
|
|
ret |= seed_ratio_not_met;
|
|
|
|
|
|
|
|
// if this torrent is running, and it was started less
|
|
|
|
// than 30 minutes ago, give it priority, to avoid oscillation
|
|
|
|
if (!is_paused() && now - m_started < minutes(30))
|
|
|
|
ret |= recently_started;
|
|
|
|
|
|
|
|
// if we have any scrape data, use it to calculate
|
|
|
|
// seed rank
|
|
|
|
int seeds = 0;
|
|
|
|
int downloaders = 0;
|
|
|
|
|
|
|
|
if (m_complete >= 0) seeds = m_complete;
|
|
|
|
else seeds = m_policy.num_seeds();
|
|
|
|
|
|
|
|
if (m_incomplete >= 0) downloaders = m_incomplete;
|
|
|
|
else downloaders = m_policy.num_peers() - m_policy.num_seeds();
|
|
|
|
|
|
|
|
if (seeds == 0)
|
|
|
|
{
|
|
|
|
ret |= no_seeds;
|
|
|
|
ret |= downloaders & prio_mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-19 10:05:36 +02:00
|
|
|
ret |= (downloaders * scale / seeds) & prio_mask;
|
2008-05-06 20:03:41 +02:00
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
// this is an async operation triggered by the client
|
|
|
|
void torrent::save_resume_data()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-10-17 03:56:36 +02:00
|
|
|
if (!m_owning_storage.get())
|
2008-04-13 20:54:36 +02:00
|
|
|
{
|
2008-10-17 03:56:36 +02:00
|
|
|
if (alerts().should_post<save_resume_data_failed_alert>())
|
2008-06-24 14:48:28 +02:00
|
|
|
{
|
2008-10-17 03:56:36 +02:00
|
|
|
alerts().post_alert(save_resume_data_failed_alert(get_handle()
|
|
|
|
, "save resume data failed, torrent is being destructed"));
|
2008-06-24 14:48:28 +02:00
|
|
|
}
|
2008-10-17 03:56:36 +02:00
|
|
|
return;
|
2008-04-13 20:54:36 +02:00
|
|
|
}
|
2008-10-17 03:56:36 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(m_storage);
|
|
|
|
if (m_state == torrent_status::queued_for_checking
|
2008-11-19 01:46:48 +01:00
|
|
|
|| m_state == torrent_status::checking_files
|
|
|
|
|| m_state == torrent_status::checking_resume_data)
|
2008-04-13 20:54:36 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<save_resume_data_failed_alert>())
|
2008-04-13 20:54:36 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
alerts().post_alert(save_resume_data_failed_alert(get_handle()
|
2008-10-17 03:56:36 +02:00
|
|
|
, "won't save resume data, torrent does not have a complete resume state yet"));
|
2008-04-13 20:54:36 +02:00
|
|
|
}
|
2008-10-17 03:56:36 +02:00
|
|
|
return;
|
2008-04-13 20:54:36 +02:00
|
|
|
}
|
2008-10-17 03:56:36 +02:00
|
|
|
m_storage->async_save_resume_data(
|
|
|
|
bind(&torrent::on_save_resume_data, shared_from_this(), _1, _2));
|
2008-04-13 20:54:36 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
bool torrent::should_check_files() const
|
|
|
|
{
|
2008-11-18 12:01:30 +01:00
|
|
|
return (m_state == torrent_status::checking_files
|
|
|
|
|| m_state == torrent_status::queued_for_checking)
|
2008-11-19 01:46:48 +01:00
|
|
|
&& (!is_paused() || m_auto_managed)
|
|
|
|
&& m_error.empty();
|
2008-09-20 19:42:25 +02:00
|
|
|
}
|
|
|
|
|
2008-06-29 21:08:30 +02:00
|
|
|
bool torrent::is_paused() const
|
|
|
|
{
|
|
|
|
return m_paused || m_ses.is_paused();
|
|
|
|
}
|
|
|
|
|
2004-03-21 03:03:37 +01:00
|
|
|
void torrent::pause()
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2004-12-21 13:30:09 +01:00
|
|
|
if (m_paused) return;
|
2008-09-20 19:42:25 +02:00
|
|
|
bool checking_files = should_check_files();
|
2008-06-29 21:08:30 +02:00
|
|
|
m_paused = true;
|
|
|
|
if (m_ses.is_paused()) return;
|
|
|
|
do_pause();
|
2008-09-20 19:42:25 +02:00
|
|
|
if (checking_files && !should_check_files())
|
|
|
|
{
|
2008-11-18 12:01:30 +01:00
|
|
|
// stop checking
|
|
|
|
m_storage->abort_disk_io();
|
|
|
|
m_ses.done_checking(shared_from_this());
|
2008-11-19 01:46:48 +01:00
|
|
|
set_state(torrent_status::queued_for_checking);
|
2008-09-20 19:42:25 +02:00
|
|
|
}
|
2008-06-29 21:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::do_pause()
|
|
|
|
{
|
|
|
|
if (!is_paused()) return;
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
if ((*i)->on_pause()) return;
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-08-16 14:41:46 +02:00
|
|
|
for (peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end(); ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
(*(*i)->m_logger) << "*** PAUSING TORRENT\n";
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-05 15:17:17 +01:00
|
|
|
// this will make the storage close all
|
|
|
|
// files and flush all cached data
|
2007-06-10 22:46:09 +02:00
|
|
|
if (m_owning_storage.get())
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_storage);
|
2007-06-11 23:24:14 +02:00
|
|
|
m_storage->async_release_files(
|
2007-10-02 20:34:34 +02:00
|
|
|
bind(&torrent::on_torrent_paused, shared_from_this(), _1, _2));
|
2008-07-18 17:31:22 +02:00
|
|
|
m_storage->async_clear_read_cache();
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
2008-03-30 17:44:31 +02:00
|
|
|
else
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<torrent_paused_alert>())
|
|
|
|
alerts().post_alert(torrent_paused_alert(get_handle()));
|
2008-03-30 17:44:31 +02:00
|
|
|
}
|
2008-07-12 10:25:19 +02:00
|
|
|
|
|
|
|
disconnect_all();
|
|
|
|
stop_announcing();
|
2004-03-21 03:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::resume()
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2004-12-21 13:30:09 +01:00
|
|
|
if (!m_paused) return;
|
2008-09-20 19:42:25 +02:00
|
|
|
bool checking_files = should_check_files();
|
2008-06-29 21:08:30 +02:00
|
|
|
m_paused = false;
|
|
|
|
do_resume();
|
2008-09-20 19:42:25 +02:00
|
|
|
if (!checking_files && should_check_files())
|
|
|
|
m_ses.check_torrent(shared_from_this());
|
2008-06-29 21:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::do_resume()
|
|
|
|
{
|
|
|
|
if (is_paused()) return;
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
if ((*i)->on_resume()) return;
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
if (alerts().should_post<torrent_resumed_alert>())
|
|
|
|
alerts().post_alert(torrent_resumed_alert(get_handle()));
|
|
|
|
|
2008-05-06 20:03:41 +02:00
|
|
|
m_started = time_now();
|
2008-05-20 09:57:44 +02:00
|
|
|
m_error.clear();
|
2008-07-12 10:25:19 +02:00
|
|
|
start_announcing();
|
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
void torrent::update_tracker_timer()
|
2008-07-12 10:25:19 +02:00
|
|
|
{
|
|
|
|
if (!m_announcing) return;
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
ptime next_announce = max_time();
|
|
|
|
int tier = INT_MAX;
|
|
|
|
for (std::vector<announce_entry>::iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->tier > tier) break;
|
|
|
|
if (i->is_working()) tier = i->tier;
|
|
|
|
if (i->fails >= i->fail_limit && i->fail_limit != 0) continue;
|
|
|
|
if (i->updating) continue;
|
|
|
|
if (i->next_announce < next_announce) next_announce = i->next_announce;
|
|
|
|
if (i->is_working() && !m_settings.announce_to_all_trackers) break;
|
|
|
|
}
|
|
|
|
if (next_announce == max_time()) return;
|
|
|
|
|
|
|
|
// since we don't know if we have to re-issue the async_wait or not
|
|
|
|
// always do it
|
|
|
|
// if (m_tracker_timer.expires_at() <= next_announce) return;
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
error_code ec;
|
|
|
|
boost::weak_ptr<torrent> self(shared_from_this());
|
2008-11-29 09:38:40 +01:00
|
|
|
m_tracker_timer.expires_at(next_announce, ec);
|
2008-07-12 10:25:19 +02:00
|
|
|
m_tracker_timer.async_wait(bind(&torrent::on_tracker_announce_disp, self, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::start_announcing()
|
|
|
|
{
|
|
|
|
if (is_paused()) return;
|
2008-08-27 17:15:41 +02:00
|
|
|
// if we don't have metadata, we need to announce
|
|
|
|
// before checking files, to get peers to
|
|
|
|
// request the metadata from
|
|
|
|
if (!m_files_checked && valid_metadata()) return;
|
2008-07-12 10:25:19 +02:00
|
|
|
if (m_announcing) return;
|
|
|
|
|
|
|
|
m_announcing = true;
|
|
|
|
|
|
|
|
if (!m_trackers.empty())
|
|
|
|
{
|
|
|
|
// tell the tracker that we're back
|
2008-11-29 09:38:40 +01:00
|
|
|
std::for_each(m_trackers.begin(), m_trackers.end()
|
|
|
|
, bind(&announce_entry::reset, _1));
|
2008-10-15 19:31:20 +02:00
|
|
|
m_stat.clear();
|
2008-07-12 10:25:19 +02:00
|
|
|
announce_with_tracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
// private torrents are never announced on LSD
|
|
|
|
// or on DHT, we don't need this timer.
|
|
|
|
if (!m_torrent_file->is_valid() || !m_torrent_file->priv())
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
boost::weak_ptr<torrent> self(shared_from_this());
|
|
|
|
m_lsd_announce_timer.expires_from_now(seconds(1), ec);
|
|
|
|
m_lsd_announce_timer.async_wait(
|
|
|
|
bind(&torrent::on_lsd_announce_disp, self, _1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::stop_announcing()
|
|
|
|
{
|
|
|
|
if (!m_announcing) return;
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
m_lsd_announce_timer.cancel(ec);
|
|
|
|
m_tracker_timer.cancel(ec);
|
2008-06-08 07:14:40 +02:00
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
m_announcing = false;
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
ptime now = time_now();
|
|
|
|
for (std::vector<announce_entry>::iterator i = m_trackers.begin()
|
|
|
|
, end(m_trackers.end()); i != end; ++i)
|
|
|
|
i->next_announce = now;
|
|
|
|
announce_with_tracker(tracker_request::stopped);
|
2004-03-21 03:03:37 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void torrent::second_tick(stat& accumulator, float tick_interval)
|
2003-12-01 06:01:40 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
|
|
|
(*i)->tick();
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
} catch (std::exception&) {}
|
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
if (is_paused())
|
2005-03-30 01:47:13 +02:00
|
|
|
{
|
|
|
|
// let the stats fade out to 0
|
2006-04-25 23:04:48 +02:00
|
|
|
m_stat.second_tick(tick_interval);
|
2005-03-30 01:47:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-16 08:31:05 +02:00
|
|
|
time_duration since_last_tick = microsec(tick_interval * 1000000L);
|
|
|
|
if (is_seed()) m_seeding_time += since_last_tick;
|
|
|
|
m_active_time += since_last_tick;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// ---- WEB SEEDS ----
|
|
|
|
|
2008-07-12 10:25:19 +02:00
|
|
|
// re-insert urls that are to be retrieds into the m_web_seeds
|
2007-11-28 03:54:46 +01:00
|
|
|
typedef std::map<std::string, ptime>::iterator iter_t;
|
|
|
|
for (iter_t i = m_web_seeds_next_retry.begin(); i != m_web_seeds_next_retry.end();)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-11-28 03:54:46 +01:00
|
|
|
iter_t erase_element = i++;
|
|
|
|
if (erase_element->second <= time_now())
|
2007-11-27 04:37:47 +01:00
|
|
|
{
|
2007-11-28 03:54:46 +01:00
|
|
|
m_web_seeds.insert(erase_element->first);
|
|
|
|
m_web_seeds_next_retry.erase(erase_element);
|
2007-11-27 04:37:47 +01:00
|
|
|
}
|
2007-11-28 03:54:46 +01:00
|
|
|
}
|
2007-11-27 04:37:47 +01:00
|
|
|
|
2007-11-28 03:54:46 +01:00
|
|
|
// if we have everything we want we don't need to connect to any web-seed
|
|
|
|
if (!is_finished() && !m_web_seeds.empty())
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
// keep trying web-seeds if there are any
|
|
|
|
// first find out which web seeds we are connected to
|
|
|
|
std::set<std::string> web_seeds;
|
|
|
|
for (peer_iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end(); ++i)
|
|
|
|
{
|
|
|
|
web_peer_connection* p
|
2007-10-31 10:48:20 +01:00
|
|
|
= dynamic_cast<web_peer_connection*>(*i);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!p) continue;
|
|
|
|
web_seeds.insert(p->url());
|
|
|
|
}
|
|
|
|
|
2006-05-20 17:30:40 +02:00
|
|
|
for (std::set<std::string>::iterator i = m_resolving_web_seeds.begin()
|
|
|
|
, end(m_resolving_web_seeds.end()); i != end; ++i)
|
|
|
|
web_seeds.insert(web_seeds.begin(), *i);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// from the list of available web seeds, subtract the ones we are
|
|
|
|
// already connected to.
|
|
|
|
std::vector<std::string> not_connected_web_seeds;
|
|
|
|
std::set_difference(m_web_seeds.begin(), m_web_seeds.end(), web_seeds.begin()
|
|
|
|
, web_seeds.end(), std::back_inserter(not_connected_web_seeds));
|
|
|
|
|
|
|
|
// connect to all of those that we aren't connected to
|
|
|
|
std::for_each(not_connected_web_seeds.begin(), not_connected_web_seeds.end()
|
|
|
|
, bind(&torrent::connect_to_url_seed, this, _1));
|
|
|
|
}
|
|
|
|
|
2004-01-13 04:08:59 +01:00
|
|
|
for (peer_iterator i = m_connections.begin();
|
2007-05-24 19:07:43 +02:00
|
|
|
i != m_connections.end();)
|
2003-12-07 06:53:04 +01:00
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_connection* p = *i;
|
2007-05-24 19:07:43 +02:00
|
|
|
++i;
|
2004-03-23 23:58:18 +01:00
|
|
|
m_stat += p->statistics();
|
2004-03-28 19:45:37 +02:00
|
|
|
// updates the peer connection's ul/dl bandwidth
|
|
|
|
// resource requests
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2007-05-24 19:07:43 +02:00
|
|
|
try
|
|
|
|
{
|
2008-04-07 04:51:21 +02:00
|
|
|
#endif
|
2007-05-24 19:07:43 +02:00
|
|
|
p->second_tick(tick_interval);
|
2008-04-07 04:51:21 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2007-05-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2008-02-17 21:57:16 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-05-24 19:07:43 +02:00
|
|
|
(*p->m_logger) << "**ERROR**: " << e.what() << "\n";
|
|
|
|
#endif
|
2008-05-12 08:03:31 +02:00
|
|
|
p->disconnect(e.what(), 1);
|
2007-05-24 19:07:43 +02:00
|
|
|
}
|
2008-04-07 04:51:21 +02:00
|
|
|
#endif
|
2003-12-07 06:53:04 +01:00
|
|
|
}
|
2004-04-18 15:41:08 +02:00
|
|
|
accumulator += m_stat;
|
2008-04-16 08:31:05 +02:00
|
|
|
m_total_uploaded += m_stat.last_payload_uploaded();
|
|
|
|
m_total_downloaded += m_stat.last_payload_downloaded();
|
2006-07-26 12:21:25 +02:00
|
|
|
m_stat.second_tick(tick_interval);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-10-29 15:21:09 +02:00
|
|
|
m_time_scaler--;
|
|
|
|
if (m_time_scaler <= 0)
|
|
|
|
{
|
2007-08-16 14:41:46 +02:00
|
|
|
m_time_scaler = 10;
|
2007-10-31 10:48:20 +01:00
|
|
|
m_policy.pulse();
|
2004-10-29 15:21:09 +02:00
|
|
|
}
|
2004-03-23 23:58:18 +01:00
|
|
|
}
|
|
|
|
|
2007-11-27 04:37:47 +01:00
|
|
|
void torrent::retry_url_seed(std::string const& url)
|
|
|
|
{
|
|
|
|
m_web_seeds_next_retry[url] = time_now()
|
|
|
|
+ seconds(m_ses.settings().urlseed_wait_retry);
|
|
|
|
}
|
|
|
|
|
2007-08-16 14:41:46 +02:00
|
|
|
bool torrent::try_connect_peer()
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(want_more_peers());
|
2008-03-29 20:39:24 +01:00
|
|
|
if (m_deficit_counter < 100) return false;
|
|
|
|
m_deficit_counter -= 100;
|
|
|
|
bool ret = m_policy.connect_one_peer();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::give_connect_points(int points)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(points <= 100);
|
|
|
|
TORRENT_ASSERT(points > 0);
|
|
|
|
TORRENT_ASSERT(want_more_peers());
|
|
|
|
m_deficit_counter += points;
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 00:08:07 +02:00
|
|
|
void torrent::async_verify_piece(int piece_index, boost::function<void(int)> const& f)
|
2003-12-07 06:53:04 +01:00
|
|
|
{
|
2007-10-18 02:32:16 +02:00
|
|
|
// INVARIANT_CHECK;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_storage);
|
|
|
|
TORRENT_ASSERT(m_storage->refcount() > 0);
|
|
|
|
TORRENT_ASSERT(piece_index >= 0);
|
|
|
|
TORRENT_ASSERT(piece_index < m_torrent_file->num_pieces());
|
2008-06-07 04:58:28 +02:00
|
|
|
TORRENT_ASSERT(piece_index < (int)m_picker->num_pieces());
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-02-18 04:07:14 +01:00
|
|
|
if (m_picker)
|
|
|
|
{
|
|
|
|
int blocks_in_piece = m_picker->blocks_in_piece(piece_index);
|
|
|
|
for (int i = 0; i < blocks_in_piece; ++i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_picker->num_peers(piece_block(piece_index, i)) == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-01-25 13:37:15 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
m_storage->async_hash(piece_index, bind(&torrent::on_piece_verified
|
|
|
|
, shared_from_this(), _1, _2, f));
|
2008-11-29 22:33:21 +01:00
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
|
2007-10-18 02:32:16 +02:00
|
|
|
check_invariant();
|
|
|
|
#endif
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void torrent::on_piece_verified(int ret, disk_io_job const& j
|
2008-04-13 00:08:07 +02:00
|
|
|
, boost::function<void(int)> f)
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
2008-04-13 00:08:07 +02:00
|
|
|
|
|
|
|
// return value:
|
|
|
|
// 0: success, piece passed hash check
|
|
|
|
// -1: disk failure
|
|
|
|
// -2: hash check failed
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (alerts().should_post<file_error_alert>())
|
2008-04-13 00:08:07 +02:00
|
|
|
alerts().post_alert(file_error_alert(j.error_file, get_handle(), j.str));
|
2008-05-20 09:57:44 +02:00
|
|
|
m_error = j.str;
|
2008-04-13 00:08:07 +02:00
|
|
|
pause();
|
|
|
|
}
|
|
|
|
f(ret);
|
2003-12-01 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
const tcp::endpoint& torrent::current_tracker() const
|
2004-03-01 01:50:00 +01:00
|
|
|
{
|
|
|
|
return m_tracker_address;
|
|
|
|
}
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
announce_entry* torrent::find_tracker(tracker_request const& r)
|
|
|
|
{
|
|
|
|
std::vector<announce_entry>::iterator i = std::find_if(
|
|
|
|
m_trackers.begin(), m_trackers.end()
|
|
|
|
, bind(&announce_entry::url, _1) == r.url);
|
|
|
|
if (i == m_trackers.end()) return 0;
|
|
|
|
return &*i;
|
|
|
|
}
|
|
|
|
|
2006-06-12 01:24:36 +02:00
|
|
|
void torrent::file_progress(std::vector<float>& fp) const
|
2008-07-12 15:38:22 +02:00
|
|
|
{
|
|
|
|
fp.clear();
|
|
|
|
fp.resize(m_torrent_file->num_files(), 1.f);
|
|
|
|
if (is_seed()) return;
|
|
|
|
|
|
|
|
std::vector<size_type> progress;
|
|
|
|
file_progress(progress);
|
|
|
|
for (int i = 0; i < m_torrent_file->num_files(); ++i)
|
|
|
|
{
|
|
|
|
file_entry const& f = m_torrent_file->file_at(i);
|
|
|
|
if (f.size == 0) fp[i] = 1.f;
|
|
|
|
else fp[i] = float(progress[i]) / f.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::file_progress(std::vector<size_type>& fp) const
|
2006-06-12 01:24:36 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(valid_metadata());
|
2006-06-12 01:24:36 +02:00
|
|
|
|
2008-07-12 15:38:22 +02:00
|
|
|
fp.resize(m_torrent_file->num_files(), 0);
|
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
if (is_seed())
|
|
|
|
{
|
2008-07-12 15:38:22 +02:00
|
|
|
for (int i = 0; i < m_torrent_file->num_files(); ++i)
|
|
|
|
fp[i] = m_torrent_file->files().at(i).size;
|
2008-06-07 04:58:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-06-12 01:24:36 +02:00
|
|
|
|
2008-07-30 10:52:39 +02:00
|
|
|
TORRENT_ASSERT(has_picker());
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
for (int i = 0; i < m_torrent_file->num_files(); ++i)
|
2006-06-12 01:24:36 +02:00
|
|
|
{
|
2008-05-28 10:44:40 +02:00
|
|
|
peer_request ret = m_torrent_file->files().map_file(i, 0, 0);
|
|
|
|
size_type size = m_torrent_file->files().at(i).size;
|
2006-10-08 22:36:05 +02:00
|
|
|
|
|
|
|
// zero sized files are considered
|
|
|
|
// 100% done all the time
|
|
|
|
if (size == 0)
|
|
|
|
{
|
2008-07-12 15:38:22 +02:00
|
|
|
fp[i] = 0;
|
2006-10-08 22:36:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-06-12 01:24:36 +02:00
|
|
|
size_type done = 0;
|
2006-08-27 20:38:30 +02:00
|
|
|
while (size > 0)
|
2006-06-12 01:24:36 +02:00
|
|
|
{
|
2008-02-25 11:28:53 +01:00
|
|
|
size_type bytes_step = (std::min)(size_type(m_torrent_file->piece_size(ret.piece)
|
|
|
|
- ret.start), size);
|
2008-06-07 04:58:28 +02:00
|
|
|
if (m_picker->have_piece(ret.piece)) done += bytes_step;
|
2006-06-12 01:24:36 +02:00
|
|
|
++ret.piece;
|
|
|
|
ret.start = 0;
|
2006-08-27 20:38:30 +02:00
|
|
|
size -= bytes_step;
|
2006-06-12 01:24:36 +02:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(size == 0);
|
2006-10-08 22:36:05 +02:00
|
|
|
|
2008-07-12 15:38:22 +02:00
|
|
|
fp[i] = done;
|
2006-06-12 01:24:36 +02:00
|
|
|
}
|
2008-07-09 12:45:07 +02:00
|
|
|
|
|
|
|
const std::vector<piece_picker::downloading_piece>& q
|
|
|
|
= m_picker->get_download_queue();
|
|
|
|
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator
|
|
|
|
i = q.begin(), end(q.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
size_type offset = size_type(i->index) * m_torrent_file->piece_length();
|
|
|
|
torrent_info::file_iterator file = m_torrent_file->file_at_offset(offset);
|
|
|
|
int file_index = file - m_torrent_file->begin_files();
|
|
|
|
int num_blocks = m_picker->blocks_in_piece(i->index);
|
|
|
|
piece_picker::block_info const* info = i->info;
|
|
|
|
for (int k = 0; k < num_blocks; ++k)
|
|
|
|
{
|
2008-07-13 18:44:14 +02:00
|
|
|
TORRENT_ASSERT(file != m_torrent_file->end_files());
|
2008-07-11 16:10:33 +02:00
|
|
|
TORRENT_ASSERT(offset == size_type(i->index) * m_torrent_file->piece_length()
|
|
|
|
+ k * m_block_size);
|
2008-07-13 18:44:14 +02:00
|
|
|
TORRENT_ASSERT(offset < m_torrent_file->total_size());
|
|
|
|
while (offset >= file->offset + file->size)
|
|
|
|
{
|
|
|
|
++file;
|
|
|
|
++file_index;
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(file != m_torrent_file->end_files());
|
2008-07-11 16:10:33 +02:00
|
|
|
|
|
|
|
size_type block_size = m_block_size;
|
|
|
|
|
|
|
|
if (info[k].state == piece_picker::block_info::state_none)
|
|
|
|
{
|
|
|
|
offset += m_block_size;
|
2008-07-09 12:45:07 +02:00
|
|
|
continue;
|
2008-07-11 16:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (info[k].state == piece_picker::block_info::state_requested)
|
|
|
|
{
|
|
|
|
block_size = 0;
|
|
|
|
policy::peer* p = static_cast<policy::peer*>(info[k].peer);
|
|
|
|
if (p && p->connection)
|
|
|
|
{
|
|
|
|
boost::optional<piece_block_progress> pbp
|
|
|
|
= p->connection->downloading_piece_progress();
|
|
|
|
if (pbp && pbp->piece_index == i->index && pbp->block_index == k)
|
|
|
|
block_size = pbp->bytes_downloaded;
|
2008-07-13 18:44:14 +02:00
|
|
|
TORRENT_ASSERT(block_size <= m_block_size);
|
2008-07-11 16:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (block_size == 0)
|
|
|
|
{
|
|
|
|
offset += m_block_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 18:44:14 +02:00
|
|
|
if (offset + block_size > file->offset + file->size)
|
2008-07-09 12:45:07 +02:00
|
|
|
{
|
2008-07-11 16:10:33 +02:00
|
|
|
int left_over = m_block_size - block_size;
|
2008-07-09 12:45:07 +02:00
|
|
|
// split the block on multiple files
|
2008-07-13 18:44:14 +02:00
|
|
|
while (block_size > 0)
|
2008-07-09 12:45:07 +02:00
|
|
|
{
|
2008-07-09 17:23:42 +02:00
|
|
|
TORRENT_ASSERT(offset <= file->offset + file->size);
|
2008-07-13 09:58:32 +02:00
|
|
|
size_type slice = (std::min)(file->offset + file->size - offset
|
|
|
|
, block_size);
|
2008-07-12 15:38:22 +02:00
|
|
|
fp[file_index] += slice;
|
2008-07-09 12:45:07 +02:00
|
|
|
offset += slice;
|
|
|
|
block_size -= slice;
|
2008-07-13 18:44:14 +02:00
|
|
|
TORRENT_ASSERT(offset <= file->offset + file->size);
|
|
|
|
if (offset == file->offset + file->size)
|
|
|
|
{
|
|
|
|
++file;
|
|
|
|
++file_index;
|
|
|
|
if (file == m_torrent_file->end_files())
|
|
|
|
{
|
|
|
|
offset += block_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-07-09 12:45:07 +02:00
|
|
|
}
|
2008-07-11 16:10:33 +02:00
|
|
|
offset += left_over;
|
2008-07-13 18:44:14 +02:00
|
|
|
TORRENT_ASSERT(offset == size_type(i->index) * m_torrent_file->piece_length()
|
|
|
|
+ (k+1) * m_block_size);
|
2008-07-09 12:45:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-12 15:38:22 +02:00
|
|
|
fp[file_index] += block_size;
|
2008-07-09 12:45:07 +02:00
|
|
|
offset += m_block_size;
|
|
|
|
}
|
2008-07-13 18:44:14 +02:00
|
|
|
TORRENT_ASSERT(file_index <= m_torrent_file->num_files());
|
2008-07-09 12:45:07 +02:00
|
|
|
}
|
|
|
|
}
|
2006-06-12 01:24:36 +02:00
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2008-07-03 12:05:51 +02:00
|
|
|
void torrent::set_state(torrent_status::state_t s)
|
|
|
|
{
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-10-05 03:44:57 +02:00
|
|
|
if (s == torrent_status::seeding)
|
|
|
|
TORRENT_ASSERT(is_seed());
|
|
|
|
if (s == torrent_status::finished)
|
|
|
|
TORRENT_ASSERT(is_finished());
|
|
|
|
if (s == torrent_status::downloading && m_state == torrent_status::finished)
|
|
|
|
TORRENT_ASSERT(!is_finished());
|
|
|
|
#endif
|
|
|
|
|
2008-07-03 12:05:51 +02:00
|
|
|
if (m_state == s) return;
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_ses.m_alerts.should_post<state_changed_alert>())
|
2008-12-01 01:19:05 +01:00
|
|
|
m_ses.m_alerts.post_alert(state_changed_alert(get_handle(), s, m_state));
|
|
|
|
m_state = s;
|
2008-07-03 12:05:51 +02:00
|
|
|
}
|
|
|
|
|
2003-10-31 05:02:51 +01:00
|
|
|
torrent_status torrent::status() const
|
2003-10-26 18:35:23 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-05-19 06:06:25 +02:00
|
|
|
ptime now = time_now();
|
|
|
|
|
2004-01-15 17:45:34 +01:00
|
|
|
torrent_status st;
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2008-06-05 20:19:03 +02:00
|
|
|
st.has_incoming = m_has_incoming;
|
2008-05-20 09:57:44 +02:00
|
|
|
st.error = m_error;
|
|
|
|
|
2008-05-19 06:06:25 +02:00
|
|
|
if (m_last_scrape == min_time())
|
|
|
|
{
|
|
|
|
st.last_scrape = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st.last_scrape = total_seconds(now - m_last_scrape);
|
|
|
|
}
|
2008-01-17 18:40:46 +01:00
|
|
|
st.up_bandwidth_queue = (int)m_bandwidth_queue[peer_connection::upload_channel].size();
|
|
|
|
st.down_bandwidth_queue = (int)m_bandwidth_queue[peer_connection::download_channel].size();
|
|
|
|
|
2007-11-20 23:46:27 +01:00
|
|
|
st.num_peers = (int)std::count_if(m_connections.begin(), m_connections.end()
|
|
|
|
, !boost::bind(&peer_connection::is_connecting, _1));
|
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
st.list_peers = m_policy.num_peers();
|
|
|
|
st.list_seeds = m_policy.num_seeds();
|
2008-03-29 19:47:24 +01:00
|
|
|
st.connect_candidates = m_policy.num_connect_candidates();
|
2008-05-06 20:03:41 +02:00
|
|
|
st.seed_rank = seed_rank(m_ses.m_settings);
|
2005-11-05 11:56:47 +01:00
|
|
|
|
2008-04-16 08:31:05 +02:00
|
|
|
st.all_time_upload = m_total_uploaded;
|
|
|
|
st.all_time_download = m_total_downloaded;
|
|
|
|
|
|
|
|
st.active_time = total_seconds(m_active_time);
|
|
|
|
st.seeding_time = total_seconds(m_seeding_time);
|
|
|
|
|
2007-10-08 22:01:36 +02:00
|
|
|
st.storage_mode = m_storage_mode;
|
2007-07-23 06:36:56 +02:00
|
|
|
|
2005-02-23 21:38:29 +01:00
|
|
|
st.num_complete = m_complete;
|
|
|
|
st.num_incomplete = m_incomplete;
|
2004-03-21 03:03:37 +01:00
|
|
|
st.paused = m_paused;
|
2005-05-30 19:43:03 +02:00
|
|
|
boost::tie(st.total_done, st.total_wanted_done) = bytes_done();
|
2008-05-12 08:20:54 +02:00
|
|
|
TORRENT_ASSERT(st.total_wanted_done >= 0);
|
2007-12-09 20:38:17 +01:00
|
|
|
TORRENT_ASSERT(st.total_done >= st.total_wanted_done);
|
2003-10-31 13:07:07 +01:00
|
|
|
|
2003-12-22 08:14:35 +01:00
|
|
|
// payload transfer
|
|
|
|
st.total_payload_download = m_stat.total_payload_download();
|
|
|
|
st.total_payload_upload = m_stat.total_payload_upload();
|
|
|
|
|
|
|
|
// total transfer
|
|
|
|
st.total_download = m_stat.total_payload_download()
|
|
|
|
+ m_stat.total_protocol_download();
|
|
|
|
st.total_upload = m_stat.total_payload_upload()
|
|
|
|
+ m_stat.total_protocol_upload();
|
|
|
|
|
2004-04-18 14:28:02 +02:00
|
|
|
// failed bytes
|
|
|
|
st.total_failed_bytes = m_total_failed_bytes;
|
2006-04-25 23:04:48 +02:00
|
|
|
st.total_redundant_bytes = m_total_redundant_bytes;
|
2004-04-18 14:28:02 +02:00
|
|
|
|
2003-12-22 08:14:35 +01:00
|
|
|
// transfer rate
|
2003-12-07 06:53:04 +01:00
|
|
|
st.download_rate = m_stat.download_rate();
|
|
|
|
st.upload_rate = m_stat.upload_rate();
|
2004-04-18 14:28:02 +02:00
|
|
|
st.download_payload_rate = m_stat.download_payload_rate();
|
|
|
|
st.upload_payload_rate = m_stat.upload_payload_rate();
|
2003-12-22 08:14:35 +01:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
st.next_announce = boost::posix_time::seconds(
|
2008-05-19 06:06:25 +02:00
|
|
|
total_seconds(next_announce() - now));
|
2008-07-19 00:57:49 +02:00
|
|
|
if (st.next_announce.is_negative() || is_paused())
|
2007-04-05 00:27:36 +02:00
|
|
|
st.next_announce = boost::posix_time::seconds(0);
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
st.announce_interval = boost::posix_time::seconds(0);
|
2004-01-17 21:04:19 +01:00
|
|
|
|
2005-03-11 18:21:56 +01:00
|
|
|
if (m_last_working_tracker >= 0)
|
|
|
|
{
|
2008-11-26 02:31:54 +01:00
|
|
|
TORRENT_ASSERT(m_last_working_tracker < m_trackers.size());
|
2008-11-29 09:38:40 +01:00
|
|
|
st.current_tracker = m_trackers[m_last_working_tracker].url;
|
2005-03-11 18:21:56 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
else
|
2008-11-26 02:31:54 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
std::vector<announce_entry>::const_iterator i;
|
|
|
|
for (i = m_trackers.begin(); i != m_trackers.end(); ++i)
|
|
|
|
if (i->updating) break;
|
|
|
|
if (i != m_trackers.end()) st.current_tracker = i->url;
|
2008-11-26 02:31:54 +01:00
|
|
|
}
|
2005-03-11 18:21:56 +01:00
|
|
|
|
2007-08-17 09:37:08 +02:00
|
|
|
st.num_uploads = m_num_uploads;
|
2007-08-16 14:41:46 +02:00
|
|
|
st.uploads_limit = m_max_uploads;
|
|
|
|
st.num_connections = int(m_connections.size());
|
|
|
|
st.connections_limit = m_max_connections;
|
2004-06-14 01:30:42 +02:00
|
|
|
// if we don't have any metadata, stop here
|
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
st.state = m_state;
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
if (!valid_metadata())
|
|
|
|
{
|
2008-08-03 17:32:15 +02:00
|
|
|
st.state = torrent_status::downloading_metadata;
|
2008-03-08 07:06:31 +01:00
|
|
|
st.progress = m_progress;
|
2006-04-25 23:04:48 +02:00
|
|
|
st.block_size = 0;
|
2004-06-14 01:30:42 +02:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
st.block_size = block_size();
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
// fill in status that depends on metadata
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
st.total_wanted = m_torrent_file->total_size();
|
2008-05-12 08:20:54 +02:00
|
|
|
TORRENT_ASSERT(st.total_wanted >= 0);
|
2008-05-27 10:07:59 +02:00
|
|
|
TORRENT_ASSERT(st.total_wanted >= m_torrent_file->piece_length()
|
|
|
|
* (m_torrent_file->num_pieces() - 1));
|
2005-05-30 19:43:03 +02:00
|
|
|
|
|
|
|
if (m_picker.get() && (m_picker->num_filtered() > 0
|
|
|
|
|| m_picker->num_have_filtered() > 0))
|
|
|
|
{
|
2008-05-27 10:07:59 +02:00
|
|
|
int num_filtered_pieces = m_picker->num_filtered()
|
2005-05-30 19:43:03 +02:00
|
|
|
+ m_picker->num_have_filtered();
|
2007-09-01 05:00:31 +02:00
|
|
|
int last_piece_index = m_torrent_file->num_pieces() - 1;
|
2007-03-15 23:03:56 +01:00
|
|
|
if (m_picker->piece_priority(last_piece_index) == 0)
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2007-09-01 05:00:31 +02:00
|
|
|
st.total_wanted -= m_torrent_file->piece_size(last_piece_index);
|
2008-05-27 10:07:59 +02:00
|
|
|
--num_filtered_pieces;
|
2005-05-30 19:43:03 +02:00
|
|
|
}
|
|
|
|
|
2008-05-27 10:07:59 +02:00
|
|
|
st.total_wanted -= size_type(num_filtered_pieces) * m_torrent_file->piece_length();
|
2005-05-30 19:43:03 +02:00
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(st.total_wanted >= st.total_wanted_done);
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
if (m_state == torrent_status::checking_files)
|
|
|
|
st.progress = m_progress;
|
|
|
|
else if (st.total_wanted == 0) st.progress = 1.f;
|
2005-08-15 00:04:58 +02:00
|
|
|
else st.progress = st.total_wanted_done
|
2008-04-05 23:18:27 +02:00
|
|
|
/ static_cast<float>(st.total_wanted);
|
2004-01-15 02:01:09 +01:00
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
if (has_picker())
|
|
|
|
{
|
|
|
|
int num_pieces = m_picker->num_pieces();
|
|
|
|
st.pieces.resize(num_pieces, false);
|
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
if (m_picker->have_piece(i)) st.pieces.set_bit(i);
|
|
|
|
}
|
2008-06-07 16:03:21 +02:00
|
|
|
st.num_pieces = num_have();
|
2004-08-05 15:56:26 +02:00
|
|
|
st.num_seeds = num_seeds();
|
2006-12-04 13:20:34 +01:00
|
|
|
if (m_picker.get())
|
|
|
|
st.distributed_copies = m_picker->distributed_copies();
|
|
|
|
else
|
|
|
|
st.distributed_copies = -1;
|
2003-10-31 05:02:51 +01:00
|
|
|
return st;
|
2003-10-26 18:35:23 +01:00
|
|
|
}
|
|
|
|
|
2008-07-11 09:30:04 +02:00
|
|
|
void torrent::add_redundant_bytes(int b)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(b > 0);
|
|
|
|
m_total_redundant_bytes += b;
|
|
|
|
m_ses.add_redundant_bytes(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent::add_failed_bytes(int b)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(b > 0);
|
|
|
|
m_total_failed_bytes += b;
|
|
|
|
m_ses.add_failed_bytes(b);
|
|
|
|
}
|
|
|
|
|
2004-08-05 15:56:26 +02:00
|
|
|
int torrent::num_seeds() const
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-06-03 07:57:05 +02:00
|
|
|
return (int)std::count_if(m_connections.begin(), m_connections.end()
|
2007-10-31 10:48:20 +01:00
|
|
|
, boost::bind(&peer_connection::is_seed, _1));
|
2004-09-12 15:53:00 +02:00
|
|
|
}
|
|
|
|
|
2005-03-24 13:13:47 +01:00
|
|
|
void torrent::tracker_request_timed_out(
|
2007-11-20 23:46:27 +01:00
|
|
|
tracker_request const& r)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
2007-03-31 00:00:26 +02:00
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2003-12-22 08:14:35 +01:00
|
|
|
debug_log("*** tracker timed out");
|
|
|
|
#endif
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (r.kind == tracker_request::announce_request)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
announce_entry* ae = find_tracker(r);
|
|
|
|
if (ae)
|
2007-11-20 23:46:27 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
ae->failed();
|
|
|
|
int tracker_index = ae - &m_trackers[0];
|
|
|
|
deprioritize_tracker(tracker_index);
|
2007-11-20 23:46:27 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
if (m_ses.m_alerts.should_post<tracker_error_alert>())
|
2008-11-27 21:51:59 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
m_ses.m_alerts.post_alert(tracker_error_alert(get_handle()
|
|
|
|
, ae?ae->fails:0, 0, r.url, "tracker timed out"));
|
2008-11-27 21:51:59 +01:00
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
else if (r.kind == tracker_request::scrape_request)
|
|
|
|
{
|
|
|
|
if (m_ses.m_alerts.should_post<scrape_failed_alert>())
|
2007-11-20 23:46:27 +01:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
m_ses.m_alerts.post_alert(scrape_failed_alert(get_handle()
|
|
|
|
, r.url, "tracker timed out"));
|
2007-11-20 23:46:27 +01:00
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
update_tracker_timer();
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
|
|
|
|
2004-01-26 11:29:00 +01:00
|
|
|
// TODO: with some response codes, we should just consider
|
2003-12-22 08:14:35 +01:00
|
|
|
// the tracker as a failure and not retry
|
|
|
|
// it anymore
|
2007-11-20 23:46:27 +01:00
|
|
|
void torrent::tracker_request_error(tracker_request const& r
|
2005-03-24 13:13:47 +01:00
|
|
|
, int response_code, const std::string& str)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2007-03-31 00:00:26 +02:00
|
|
|
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2003-12-22 08:14:35 +01:00
|
|
|
debug_log(std::string("*** tracker error: ") + str);
|
|
|
|
#endif
|
2008-07-06 14:22:56 +02:00
|
|
|
if (r.kind == tracker_request::announce_request)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
announce_entry* ae = find_tracker(r);
|
|
|
|
if (ae)
|
2007-11-20 23:46:27 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
ae->failed();
|
|
|
|
int tracker_index = ae - &m_trackers[0];
|
|
|
|
deprioritize_tracker(tracker_index);
|
2007-11-20 23:46:27 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
if (m_ses.m_alerts.should_post<tracker_error_alert>())
|
2008-11-27 21:51:59 +01:00
|
|
|
{
|
2008-11-29 09:38:40 +01:00
|
|
|
m_ses.m_alerts.post_alert(tracker_error_alert(get_handle()
|
|
|
|
, ae?ae->fails:0, response_code, r.url, str));
|
2008-11-27 21:51:59 +01:00
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
else if (r.kind == tracker_request::scrape_request)
|
|
|
|
{
|
|
|
|
if (m_ses.m_alerts.should_post<scrape_failed_alert>())
|
2007-11-20 23:46:27 +01:00
|
|
|
{
|
2008-04-23 03:54:21 +02:00
|
|
|
m_ses.m_alerts.post_alert(scrape_failed_alert(get_handle(), r.url, str));
|
2007-11-20 23:46:27 +01:00
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
update_tracker_timer();
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2003-11-20 20:58:29 +01:00
|
|
|
void torrent::debug_log(const std::string& line)
|
|
|
|
{
|
2007-10-26 09:14:19 +02:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " " << line << "\n";
|
2003-11-20 20:58:29 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-06-15 14:54:35 +02:00
|
|
|
}
|
|
|
|
|