2006-10-11 22:57:54 +02:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2006-2014, Arvid Norberg, Magnus Jonsson
|
2006-10-11 22:57:54 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <cctype>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/limits.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
2008-09-22 01:19:58 +02:00
|
|
|
#include <boost/function_equal.hpp>
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2013-10-02 23:51:30 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "libtorrent/peer_id.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/tracker_manager.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/fingerprint.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/file.hpp"
|
|
|
|
#include "libtorrent/bt_peer_connection.hpp"
|
|
|
|
#include "libtorrent/ip_filter.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2010-12-29 03:17:44 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2006-10-11 22:57:54 +02:00
|
|
|
#include "libtorrent/kademlia/dht_tracker.hpp"
|
2010-12-29 03:17:44 +01:00
|
|
|
#endif
|
2007-09-22 18:27:29 +02:00
|
|
|
#include "libtorrent/enum_net.hpp"
|
2008-04-24 05:28:48 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2008-12-21 20:15:53 +01:00
|
|
|
#include "libtorrent/utf8.hpp"
|
2008-12-26 08:00:21 +01:00
|
|
|
#include "libtorrent/upnp.hpp"
|
|
|
|
#include "libtorrent/natpmp.hpp"
|
|
|
|
#include "libtorrent/lsd.hpp"
|
2009-04-09 03:04:49 +02:00
|
|
|
#include "libtorrent/instantiate_connection.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2010-10-10 04:22:57 +02:00
|
|
|
#include "libtorrent/settings.hpp"
|
2010-10-24 02:44:07 +02:00
|
|
|
#include "libtorrent/build_config.hpp"
|
2011-01-29 11:37:21 +01:00
|
|
|
#include "libtorrent/extensions.hpp"
|
2011-02-26 08:55:51 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2012-03-08 10:54:44 +01:00
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2011-10-22 19:44:40 +02:00
|
|
|
#if defined TORRENT_STATS && defined __MACH__
|
2011-10-17 19:12:08 +02:00
|
|
|
#include <mach/task.h>
|
|
|
|
#endif
|
|
|
|
|
2008-05-16 17:19:38 +02:00
|
|
|
#ifndef TORRENT_WINDOWS
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
2010-04-14 20:52:13 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
|
2010-10-22 07:39:32 +02:00
|
|
|
// for logging stat layout
|
|
|
|
#include "libtorrent/stat.hpp"
|
|
|
|
|
2010-04-14 20:52:13 +02:00
|
|
|
// for logging the size of DHT structures
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
#include <libtorrent/kademlia/find_data.hpp>
|
|
|
|
#include <libtorrent/kademlia/refresh.hpp>
|
|
|
|
#include <libtorrent/kademlia/node.hpp>
|
|
|
|
#include <libtorrent/kademlia/observer.hpp>
|
2014-02-24 01:31:13 +01:00
|
|
|
#include <libtorrent/kademlia/item.hpp>
|
2010-04-14 20:52:13 +02:00
|
|
|
#endif // TORRENT_DISABLE_DHT
|
|
|
|
|
2010-11-16 07:41:35 +01:00
|
|
|
#include "libtorrent/http_tracker_connection.hpp"
|
|
|
|
#include "libtorrent/udp_tracker_connection.hpp"
|
|
|
|
|
2010-04-14 20:52:13 +02:00
|
|
|
#include "libtorrent/debug.hpp"
|
|
|
|
|
|
|
|
#if TORRENT_USE_IOSTREAM
|
|
|
|
namespace libtorrent {
|
|
|
|
std::ofstream logger::log_file;
|
|
|
|
std::string logger::open_filename;
|
|
|
|
mutex logger::file_mutex;
|
|
|
|
}
|
|
|
|
#endif // TORRENT_USE_IOSTREAM
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
#ifdef TORRENT_USE_GCRYPT
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
GCRY_THREAD_OPTION_PTHREAD_IMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// libgcrypt requires this to initialize the library
|
|
|
|
struct gcrypt_setup
|
|
|
|
{
|
|
|
|
gcrypt_setup()
|
|
|
|
{
|
|
|
|
gcry_check_version(0);
|
|
|
|
gcry_error_t e = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
|
|
|
|
if (e != 0) fprintf(stderr, "libcrypt ERROR: %s\n", gcry_strerror(e));
|
|
|
|
e = gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
|
|
|
|
if (e != 0) fprintf(stderr, "initialization finished error: %s\n", gcry_strerror(e));
|
|
|
|
}
|
|
|
|
} gcrypt_global_constructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_USE_GCRYPT
|
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2007-07-07 01:06:58 +02:00
|
|
|
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// openssl requires this to clean up internal
|
|
|
|
// structures it allocates
|
|
|
|
struct openssl_cleanup
|
|
|
|
{
|
|
|
|
~openssl_cleanup() { CRYPTO_cleanup_all_ex_data(); }
|
|
|
|
} openssl_global_destructor;
|
|
|
|
}
|
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
#endif // TORRENT_USE_OPENSSL
|
|
|
|
|
2009-07-01 01:03:54 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2007-10-15 21:02:54 +02:00
|
|
|
// for ERROR_SEM_TIMEOUT
|
|
|
|
#include <winerror.h>
|
2007-07-07 01:06:58 +02:00
|
|
|
#endif
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
using boost::shared_ptr;
|
|
|
|
using boost::weak_ptr;
|
|
|
|
using libtorrent::aux::session_impl;
|
|
|
|
|
2009-02-11 18:45:12 +01:00
|
|
|
#ifdef BOOST_NO_EXCEPTIONS
|
|
|
|
namespace boost {
|
|
|
|
void throw_exception(std::exception const& e) { ::abort(); }
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
std::map<std::string, async_t> _async_ops;
|
2010-12-24 19:15:01 +01:00
|
|
|
int _async_ops_nthreads = 0;
|
|
|
|
mutex _async_ops_mutex;
|
2010-11-28 02:47:30 +01:00
|
|
|
#endif
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
namespace detail
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
std::string generate_auth_string(std::string const& user
|
|
|
|
, std::string const& passwd)
|
|
|
|
{
|
|
|
|
if (user.empty()) return std::string();
|
|
|
|
return user + ":" + passwd;
|
|
|
|
}
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
2009-05-25 04:45:51 +02:00
|
|
|
|
2008-03-08 07:06:31 +01:00
|
|
|
namespace aux {
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2011-10-22 19:44:40 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
void get_vm_stats(vm_statistics_data_t* vm_stat)
|
|
|
|
{
|
|
|
|
memset(vm_stat, 0, sizeof(*vm_stat));
|
|
|
|
#if defined __MACH__
|
|
|
|
mach_port_t host_port = mach_host_self();
|
|
|
|
mach_msg_type_number_t host_count = HOST_VM_INFO_COUNT;
|
|
|
|
kern_return_t error = host_statistics(host_port, HOST_VM_INFO,
|
|
|
|
(host_info_t)vm_stat, &host_count);
|
2014-03-23 20:13:21 +01:00
|
|
|
TORRENT_ASSERT_VAL(error == KERN_SUCCESS, error);
|
2011-10-22 19:44:40 +02:00
|
|
|
#elif defined TORRENT_LINUX
|
|
|
|
char buffer[4096];
|
|
|
|
char string[1024];
|
|
|
|
boost::uint32_t value;
|
|
|
|
FILE* f = fopen("/proc/vmstat", "r");
|
|
|
|
int ret = 0;
|
|
|
|
while ((ret = fscanf(f, "%s %u\n", string, &value)) != EOF)
|
|
|
|
{
|
|
|
|
if (ret != 2) continue;
|
|
|
|
if (strcmp(string, "nr_active_anon") == 0) vm_stat->active_count += value;
|
|
|
|
else if (strcmp(string, "nr_active_file") == 0) vm_stat->active_count += value;
|
|
|
|
else if (strcmp(string, "nr_inactive_anon") == 0) vm_stat->inactive_count += value;
|
|
|
|
else if (strcmp(string, "nr_inactive_file") == 0) vm_stat->inactive_count += value;
|
|
|
|
else if (strcmp(string, "nr_free_pages") == 0) vm_stat->free_count = value;
|
|
|
|
else if (strcmp(string, "nr_unevictable") == 0) vm_stat->wire_count = value;
|
|
|
|
else if (strcmp(string, "pswpin") == 0) vm_stat->pageins = value;
|
|
|
|
else if (strcmp(string, "pswpout") == 0) vm_stat->pageouts = value;
|
|
|
|
else if (strcmp(string, "pgfault") == 0) vm_stat->faults = value;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
#endif
|
|
|
|
// TOOD: windows?
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_thread_cpu_usage(thread_cpu_usage* tu)
|
|
|
|
{
|
|
|
|
#if defined __MACH__
|
|
|
|
task_thread_times_info t_info;
|
|
|
|
mach_msg_type_number_t t_info_count = TASK_THREAD_TIMES_INFO_COUNT;
|
|
|
|
task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&t_info, &t_info_count);
|
|
|
|
|
|
|
|
tu->user_time = min_time()
|
|
|
|
+ seconds(t_info.user_time.seconds)
|
|
|
|
+ microsec(t_info.user_time.microseconds);
|
|
|
|
tu->system_time = min_time()
|
|
|
|
+ seconds(t_info.system_time.seconds)
|
|
|
|
+ microsec(t_info.system_time.microseconds);
|
|
|
|
#elif defined TORRENT_LINUX
|
|
|
|
struct rusage ru;
|
|
|
|
getrusage(RUSAGE_THREAD, &ru);
|
|
|
|
tu->user_time = min_time()
|
|
|
|
+ seconds(ru.ru_utime.tv_sec)
|
|
|
|
+ microsec(ru.ru_utime.tv_usec);
|
|
|
|
tu->system_time = min_time()
|
|
|
|
+ seconds(ru.ru_stime.tv_sec)
|
|
|
|
+ microsec(ru.ru_stime.tv_usec);
|
|
|
|
#elif defined TORRENT_WINDOWS
|
|
|
|
FILETIME system_time;
|
|
|
|
FILETIME user_time;
|
|
|
|
FILETIME creation_time;
|
|
|
|
FILETIME exit_time;
|
|
|
|
GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &user_time, &system_time);
|
|
|
|
|
|
|
|
boost::uint64_t utime = (boost::uint64_t(user_time.dwHighDateTime) << 32)
|
|
|
|
+ user_time.dwLowDateTime;
|
|
|
|
boost::uint64_t stime = (boost::uint64_t(system_time.dwHighDateTime) << 32)
|
|
|
|
+ system_time.dwLowDateTime;
|
|
|
|
|
|
|
|
tu->user_time = min_time() + microsec(utime / 10);
|
|
|
|
tu->system_time = min_time() + microsec(stime / 10);
|
|
|
|
#endif
|
|
|
|
}
|
2014-02-05 10:38:32 +01:00
|
|
|
#endif //TORRENT_STATS
|
2011-10-22 19:44:40 +02:00
|
|
|
|
2006-11-15 22:39:58 +01:00
|
|
|
struct seed_random_generator
|
|
|
|
{
|
|
|
|
seed_random_generator()
|
2006-11-30 12:56:19 +01:00
|
|
|
{
|
2014-02-05 10:38:32 +01:00
|
|
|
random_seed((unsigned int)((total_microseconds(
|
|
|
|
time_now_hires() - min_time())) & 0xffffffff));
|
2006-11-30 12:56:19 +01:00
|
|
|
}
|
2006-11-15 22:39:58 +01:00
|
|
|
};
|
|
|
|
|
2014-07-05 01:40:31 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
|
|
|
#endif
|
|
|
|
|
2009-12-03 06:11:57 +01:00
|
|
|
#define TORRENT_SETTING(t, x) {#x, offsetof(session_settings,x), t},
|
|
|
|
|
2012-02-25 09:02:52 +01:00
|
|
|
bencode_map_entry session_settings_map[] =
|
2009-12-03 06:11:57 +01:00
|
|
|
{
|
|
|
|
TORRENT_SETTING(std_string, user_agent)
|
|
|
|
TORRENT_SETTING(integer, tracker_completion_timeout)
|
|
|
|
TORRENT_SETTING(integer, tracker_receive_timeout)
|
|
|
|
TORRENT_SETTING(integer, stop_tracker_timeout)
|
|
|
|
TORRENT_SETTING(integer, tracker_maximum_response_length)
|
|
|
|
TORRENT_SETTING(integer, piece_timeout)
|
|
|
|
TORRENT_SETTING(integer, request_timeout)
|
|
|
|
TORRENT_SETTING(integer, request_queue_time)
|
|
|
|
TORRENT_SETTING(integer, max_allowed_in_request_queue)
|
|
|
|
TORRENT_SETTING(integer, max_out_request_queue)
|
|
|
|
TORRENT_SETTING(integer, whole_pieces_threshold)
|
|
|
|
TORRENT_SETTING(integer, peer_timeout)
|
|
|
|
TORRENT_SETTING(integer, urlseed_timeout)
|
|
|
|
TORRENT_SETTING(integer, urlseed_pipeline_size)
|
|
|
|
TORRENT_SETTING(integer, urlseed_wait_retry)
|
|
|
|
TORRENT_SETTING(integer, file_pool_size)
|
|
|
|
TORRENT_SETTING(boolean, allow_multiple_connections_per_ip)
|
|
|
|
TORRENT_SETTING(integer, max_failcount)
|
|
|
|
TORRENT_SETTING(integer, min_reconnect_time)
|
|
|
|
TORRENT_SETTING(integer, peer_connect_timeout)
|
|
|
|
TORRENT_SETTING(boolean, ignore_limits_on_local_network)
|
|
|
|
TORRENT_SETTING(integer, connection_speed)
|
|
|
|
TORRENT_SETTING(boolean, send_redundant_have)
|
|
|
|
TORRENT_SETTING(boolean, lazy_bitfields)
|
|
|
|
TORRENT_SETTING(integer, inactivity_timeout)
|
|
|
|
TORRENT_SETTING(integer, unchoke_interval)
|
|
|
|
TORRENT_SETTING(integer, optimistic_unchoke_interval)
|
|
|
|
TORRENT_SETTING(std_string, announce_ip)
|
|
|
|
TORRENT_SETTING(integer, num_want)
|
|
|
|
TORRENT_SETTING(integer, initial_picker_threshold)
|
|
|
|
TORRENT_SETTING(integer, allowed_fast_set_size)
|
2010-01-15 17:45:42 +01:00
|
|
|
TORRENT_SETTING(integer, suggest_mode)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, max_queued_disk_bytes)
|
2011-02-04 04:02:23 +01:00
|
|
|
TORRENT_SETTING(integer, max_queued_disk_bytes_low_watermark)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, handshake_timeout)
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
TORRENT_SETTING(boolean, use_dht_as_fallback)
|
|
|
|
#endif
|
|
|
|
TORRENT_SETTING(boolean, free_torrent_hashes)
|
|
|
|
TORRENT_SETTING(boolean, upnp_ignore_nonrouters)
|
2012-03-07 03:59:50 +01:00
|
|
|
TORRENT_SETTING(integer, send_buffer_low_watermark)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, send_buffer_watermark)
|
2013-08-26 07:50:26 +02:00
|
|
|
TORRENT_SETTING(integer, send_buffer_watermark_factor)
|
2010-02-09 04:04:41 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(boolean, auto_upload_slots)
|
|
|
|
TORRENT_SETTING(boolean, auto_upload_slots_rate_based)
|
2010-02-09 04:04:41 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_SETTING(integer, choking_algorithm)
|
2010-10-09 23:11:03 +02:00
|
|
|
TORRENT_SETTING(integer, seed_choking_algorithm)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(boolean, use_parole_mode)
|
|
|
|
TORRENT_SETTING(integer, cache_size)
|
|
|
|
TORRENT_SETTING(integer, cache_buffer_chunk_size)
|
|
|
|
TORRENT_SETTING(integer, cache_expiry)
|
|
|
|
TORRENT_SETTING(boolean, use_read_cache)
|
2010-01-15 17:45:42 +01:00
|
|
|
TORRENT_SETTING(boolean, explicit_read_cache)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, disk_io_write_mode)
|
|
|
|
TORRENT_SETTING(integer, disk_io_read_mode)
|
|
|
|
TORRENT_SETTING(boolean, coalesce_reads)
|
|
|
|
TORRENT_SETTING(boolean, coalesce_writes)
|
|
|
|
TORRENT_SETTING(character, peer_tos)
|
|
|
|
TORRENT_SETTING(integer, active_downloads)
|
|
|
|
TORRENT_SETTING(integer, active_seeds)
|
2010-03-29 02:34:04 +02:00
|
|
|
TORRENT_SETTING(integer, active_dht_limit)
|
|
|
|
TORRENT_SETTING(integer, active_tracker_limit)
|
|
|
|
TORRENT_SETTING(integer, active_lsd_limit)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, active_limit)
|
|
|
|
TORRENT_SETTING(boolean, auto_manage_prefer_seeds)
|
|
|
|
TORRENT_SETTING(boolean, dont_count_slow_torrents)
|
|
|
|
TORRENT_SETTING(integer, auto_manage_interval)
|
|
|
|
TORRENT_SETTING(floating_point, share_ratio_limit)
|
|
|
|
TORRENT_SETTING(floating_point, seed_time_ratio_limit)
|
|
|
|
TORRENT_SETTING(integer, seed_time_limit)
|
|
|
|
TORRENT_SETTING(floating_point, peer_turnover)
|
|
|
|
TORRENT_SETTING(floating_point, peer_turnover_cutoff)
|
|
|
|
TORRENT_SETTING(boolean, close_redundant_connections)
|
|
|
|
TORRENT_SETTING(integer, auto_scrape_interval)
|
|
|
|
TORRENT_SETTING(integer, auto_scrape_min_interval)
|
|
|
|
TORRENT_SETTING(integer, max_peerlist_size)
|
|
|
|
TORRENT_SETTING(integer, max_paused_peerlist_size)
|
|
|
|
TORRENT_SETTING(integer, min_announce_interval)
|
|
|
|
TORRENT_SETTING(boolean, prioritize_partial_pieces)
|
|
|
|
TORRENT_SETTING(integer, auto_manage_startup)
|
|
|
|
TORRENT_SETTING(boolean, rate_limit_ip_overhead)
|
|
|
|
TORRENT_SETTING(boolean, announce_to_all_trackers)
|
|
|
|
TORRENT_SETTING(boolean, announce_to_all_tiers)
|
|
|
|
TORRENT_SETTING(boolean, prefer_udp_trackers)
|
|
|
|
TORRENT_SETTING(boolean, strict_super_seeding)
|
|
|
|
TORRENT_SETTING(integer, seeding_piece_quota)
|
|
|
|
TORRENT_SETTING(integer, max_sparse_regions)
|
|
|
|
#ifndef TORRENT_DISABLE_MLOCK
|
|
|
|
TORRENT_SETTING(boolean, lock_disk_cache)
|
|
|
|
#endif
|
|
|
|
TORRENT_SETTING(integer, max_rejects)
|
|
|
|
TORRENT_SETTING(integer, recv_socket_buffer_size)
|
|
|
|
TORRENT_SETTING(integer, send_socket_buffer_size)
|
|
|
|
TORRENT_SETTING(boolean, optimize_hashing_for_speed)
|
|
|
|
TORRENT_SETTING(integer, file_checks_delay_per_block)
|
|
|
|
TORRENT_SETTING(integer, disk_cache_algorithm)
|
|
|
|
TORRENT_SETTING(integer, read_cache_line_size)
|
|
|
|
TORRENT_SETTING(integer, write_cache_line_size)
|
|
|
|
TORRENT_SETTING(integer, optimistic_disk_retry)
|
|
|
|
TORRENT_SETTING(boolean, disable_hash_checks)
|
|
|
|
TORRENT_SETTING(boolean, allow_reordered_disk_operations)
|
|
|
|
TORRENT_SETTING(boolean, allow_i2p_mixed)
|
|
|
|
TORRENT_SETTING(integer, max_suggest_pieces)
|
2010-01-24 23:00:36 +01:00
|
|
|
TORRENT_SETTING(boolean, drop_skipped_requests)
|
|
|
|
TORRENT_SETTING(boolean, low_prio_disk)
|
2010-02-06 09:14:18 +01:00
|
|
|
TORRENT_SETTING(integer, local_service_announce_interval)
|
2012-12-14 05:38:44 +01:00
|
|
|
TORRENT_SETTING(integer, dht_announce_interval)
|
2010-02-06 09:14:18 +01:00
|
|
|
TORRENT_SETTING(integer, udp_tracker_token_expiry)
|
|
|
|
TORRENT_SETTING(boolean, volatile_read_cache)
|
|
|
|
TORRENT_SETTING(boolean, guided_read_cache)
|
|
|
|
TORRENT_SETTING(integer, default_cache_min_age)
|
|
|
|
TORRENT_SETTING(integer, num_optimistic_unchoke_slots)
|
|
|
|
TORRENT_SETTING(boolean, no_atime_storage)
|
2010-02-21 02:34:23 +01:00
|
|
|
TORRENT_SETTING(integer, default_est_reciprocation_rate)
|
|
|
|
TORRENT_SETTING(integer, increase_est_reciprocation_rate)
|
|
|
|
TORRENT_SETTING(integer, decrease_est_reciprocation_rate)
|
|
|
|
TORRENT_SETTING(boolean, incoming_starts_queued_torrents)
|
|
|
|
TORRENT_SETTING(boolean, report_true_downloaded)
|
|
|
|
TORRENT_SETTING(boolean, strict_end_game_mode)
|
2010-04-13 06:30:34 +02:00
|
|
|
TORRENT_SETTING(boolean, broadcast_lsd)
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_SETTING(boolean, enable_outgoing_utp)
|
|
|
|
TORRENT_SETTING(boolean, enable_incoming_utp)
|
|
|
|
TORRENT_SETTING(boolean, enable_outgoing_tcp)
|
|
|
|
TORRENT_SETTING(boolean, enable_incoming_tcp)
|
|
|
|
TORRENT_SETTING(integer, max_pex_peers)
|
2010-04-11 23:02:43 +02:00
|
|
|
TORRENT_SETTING(boolean, ignore_resume_timestamps)
|
2011-02-06 01:50:12 +01:00
|
|
|
TORRENT_SETTING(boolean, no_recheck_incomplete_resume)
|
2010-04-13 06:30:34 +02:00
|
|
|
TORRENT_SETTING(boolean, anonymous_mode)
|
2013-02-19 07:48:53 +01:00
|
|
|
TORRENT_SETTING(boolean, force_proxy)
|
2010-05-03 10:54:03 +02:00
|
|
|
TORRENT_SETTING(integer, tick_interval)
|
2012-12-14 05:38:44 +01:00
|
|
|
TORRENT_SETTING(boolean, report_web_seed_downloads)
|
|
|
|
TORRENT_SETTING(integer, share_mode_target)
|
2010-10-09 21:09:38 +02:00
|
|
|
TORRENT_SETTING(integer, upload_rate_limit)
|
|
|
|
TORRENT_SETTING(integer, download_rate_limit)
|
|
|
|
TORRENT_SETTING(integer, local_upload_rate_limit)
|
|
|
|
TORRENT_SETTING(integer, local_download_rate_limit)
|
2011-03-14 08:47:24 +01:00
|
|
|
TORRENT_SETTING(integer, dht_upload_rate_limit)
|
2010-10-09 21:09:38 +02:00
|
|
|
TORRENT_SETTING(integer, unchoke_slots_limit)
|
|
|
|
TORRENT_SETTING(integer, half_open_limit)
|
|
|
|
TORRENT_SETTING(integer, connections_limit)
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_SETTING(integer, utp_target_delay)
|
|
|
|
TORRENT_SETTING(integer, utp_gain_factor)
|
|
|
|
TORRENT_SETTING(integer, utp_syn_resends)
|
|
|
|
TORRENT_SETTING(integer, utp_fin_resends)
|
|
|
|
TORRENT_SETTING(integer, utp_num_resends)
|
|
|
|
TORRENT_SETTING(integer, utp_connect_timeout)
|
2012-06-23 08:04:46 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_SETTING(integer, utp_delayed_ack)
|
2012-06-23 08:04:46 +02:00
|
|
|
#endif
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_SETTING(boolean, utp_dynamic_sock_buf)
|
|
|
|
TORRENT_SETTING(integer, mixed_mode_algorithm)
|
|
|
|
TORRENT_SETTING(boolean, rate_limit_utp)
|
2010-11-28 23:36:26 +01:00
|
|
|
TORRENT_SETTING(integer, listen_queue_size)
|
2010-12-17 04:16:30 +01:00
|
|
|
TORRENT_SETTING(boolean, announce_double_nat)
|
|
|
|
TORRENT_SETTING(integer, torrent_connect_boost)
|
2010-12-17 04:20:36 +01:00
|
|
|
TORRENT_SETTING(boolean, seeding_outgoing_connections)
|
2011-01-23 19:00:52 +01:00
|
|
|
TORRENT_SETTING(boolean, no_connect_privileged_ports)
|
|
|
|
TORRENT_SETTING(integer, alert_queue_size)
|
2011-01-29 11:37:21 +01:00
|
|
|
TORRENT_SETTING(integer, max_metadata_size)
|
2011-08-01 06:10:01 +02:00
|
|
|
TORRENT_SETTING(boolean, smooth_connects)
|
2011-03-14 08:47:24 +01:00
|
|
|
TORRENT_SETTING(boolean, always_send_user_agent)
|
|
|
|
TORRENT_SETTING(boolean, apply_ip_filter_to_trackers)
|
2011-03-20 20:17:59 +01:00
|
|
|
TORRENT_SETTING(integer, read_job_every)
|
2011-06-21 06:39:04 +02:00
|
|
|
TORRENT_SETTING(boolean, use_disk_read_ahead)
|
|
|
|
TORRENT_SETTING(boolean, lock_files)
|
2012-03-29 05:51:22 +02:00
|
|
|
TORRENT_SETTING(integer, ssl_listen)
|
|
|
|
TORRENT_SETTING(integer, tracker_backoff)
|
2012-10-19 03:28:47 +02:00
|
|
|
TORRENT_SETTING(boolean, ban_web_seeds)
|
|
|
|
TORRENT_SETTING(integer, max_http_recv_buffer_size)
|
2009-12-03 06:11:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#undef TORRENT_SETTING
|
|
|
|
#define TORRENT_SETTING(t, x) {#x, offsetof(proxy_settings,x), t},
|
|
|
|
|
|
|
|
bencode_map_entry proxy_settings_map[] =
|
|
|
|
{
|
|
|
|
TORRENT_SETTING(std_string, hostname)
|
2013-12-03 10:10:16 +01:00
|
|
|
TORRENT_SETTING(integer16, port)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(std_string, username)
|
|
|
|
TORRENT_SETTING(std_string, password)
|
2013-12-03 10:10:16 +01:00
|
|
|
TORRENT_SETTING(character, type)
|
2011-01-24 04:24:28 +01:00
|
|
|
TORRENT_SETTING(boolean, proxy_hostnames)
|
|
|
|
TORRENT_SETTING(boolean, proxy_peer_connections)
|
2009-12-03 06:11:57 +01:00
|
|
|
};
|
|
|
|
#undef TORRENT_SETTING
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
#define TORRENT_SETTING(t, x) {#x, offsetof(dht_settings,x), t},
|
|
|
|
bencode_map_entry dht_settings_map[] =
|
|
|
|
{
|
|
|
|
TORRENT_SETTING(integer, max_peers_reply)
|
|
|
|
TORRENT_SETTING(integer, search_branching)
|
2010-05-30 03:33:03 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, service_port)
|
2010-05-30 03:33:03 +02:00
|
|
|
#endif
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, max_fail_count)
|
2012-09-22 23:40:16 +02:00
|
|
|
TORRENT_SETTING(integer, max_torrents)
|
|
|
|
TORRENT_SETTING(integer, max_dht_items)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(integer, max_torrent_search_reply)
|
2012-09-22 23:40:16 +02:00
|
|
|
TORRENT_SETTING(boolean, restrict_routing_ips)
|
|
|
|
TORRENT_SETTING(boolean, restrict_search_ips)
|
|
|
|
TORRENT_SETTING(boolean, extended_routing_table)
|
2009-12-03 06:11:57 +01:00
|
|
|
};
|
|
|
|
#undef TORRENT_SETTING
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
#define TORRENT_SETTING(t, x) {#x, offsetof(pe_settings,x), t},
|
|
|
|
bencode_map_entry pe_settings_map[] =
|
|
|
|
{
|
2013-12-03 10:10:16 +01:00
|
|
|
TORRENT_SETTING(character, out_enc_policy)
|
|
|
|
TORRENT_SETTING(character, in_enc_policy)
|
|
|
|
TORRENT_SETTING(character, allowed_enc_level)
|
2009-12-03 06:11:57 +01:00
|
|
|
TORRENT_SETTING(boolean, prefer_rc4)
|
|
|
|
};
|
|
|
|
#undef TORRENT_SETTING
|
|
|
|
#endif
|
|
|
|
|
2010-03-10 03:28:40 +01:00
|
|
|
struct session_category
|
|
|
|
{
|
|
|
|
char const* name;
|
|
|
|
bencode_map_entry const* map;
|
|
|
|
int num_entries;
|
|
|
|
int flag;
|
|
|
|
int offset;
|
2010-10-10 04:22:57 +02:00
|
|
|
int default_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
// the names in here need to match the names in session_impl
|
|
|
|
// to make the macro simpler
|
|
|
|
struct all_default_values
|
|
|
|
{
|
|
|
|
session_settings m_settings;
|
|
|
|
proxy_settings m_proxy;
|
2010-10-12 10:57:43 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2010-10-10 04:22:57 +02:00
|
|
|
pe_settings m_pe_settings;
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
2010-10-10 04:22:57 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
dht_settings m_dht_settings;
|
|
|
|
#endif
|
2010-03-10 03:28:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#define lenof(x) sizeof(x)/sizeof(x[0])
|
|
|
|
#define TORRENT_CATEGORY(name, flag, member, map) \
|
2010-10-10 04:22:57 +02:00
|
|
|
{ name, map, lenof(map), session:: flag , offsetof(session_impl, member), offsetof(all_default_values, member) },
|
2010-03-10 03:28:40 +01:00
|
|
|
|
|
|
|
session_category all_settings[] =
|
|
|
|
{
|
|
|
|
TORRENT_CATEGORY("settings", save_settings, m_settings, session_settings_map)
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2010-10-10 04:22:57 +02:00
|
|
|
TORRENT_CATEGORY("dht", save_dht_settings, m_dht_settings, dht_settings_map)
|
2010-03-10 03:28:40 +01:00
|
|
|
#endif
|
2010-10-10 04:22:57 +02:00
|
|
|
TORRENT_CATEGORY("proxy", save_proxy, m_proxy, proxy_settings_map)
|
2010-03-10 03:28:40 +01:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
// TORRENT_CATEGORY("i2p", save_i2p_proxy, m_i2p_proxy, proxy_settings_map)
|
|
|
|
#endif
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
TORRENT_CATEGORY("encryption", save_encryption_settings, m_pe_settings, pe_settings_map)
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2010-12-26 09:03:02 +01:00
|
|
|
std::pair<bencode_map_entry*, int> settings_map()
|
|
|
|
{
|
2010-12-29 03:17:44 +01:00
|
|
|
return std::make_pair(session_settings_map, lenof(session_settings_map));
|
2010-12-26 09:03:02 +01:00
|
|
|
}
|
2010-03-10 03:28:40 +01:00
|
|
|
#undef lenof
|
|
|
|
|
2014-07-05 01:40:31 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2009-05-07 22:30:20 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
int session_impl::logging_allocator::allocations = 0;
|
|
|
|
int session_impl::logging_allocator::allocated_bytes = 0;
|
|
|
|
#endif
|
|
|
|
|
2012-04-06 06:11:04 +02:00
|
|
|
#if defined TORRENT_USE_OPENSSL && BOOST_VERSION >= 104700 && OPENSSL_VERSION_NUMBER >= 0x90812f
|
2012-01-14 17:04:25 +01:00
|
|
|
// when running bittorrent over SSL, the SNI (server name indication)
|
|
|
|
// extension is used to know which torrent the incoming connection is
|
|
|
|
// trying to connect to. The 40 first bytes in the name is expected to
|
|
|
|
// be the hex encoded info-hash
|
|
|
|
int servername_callback(SSL *s, int *ad, void *arg)
|
|
|
|
{
|
|
|
|
session_impl* ses = (session_impl*)arg;
|
|
|
|
const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
|
|
|
|
|
|
|
|
if (!servername || strlen(servername) < 40)
|
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
|
|
|
|
sha1_hash info_hash;
|
|
|
|
bool valid = from_hex(servername, 40, (char*)&info_hash[0]);
|
|
|
|
|
|
|
|
// the server name is not a valid hex-encoded info-hash
|
|
|
|
if (!valid)
|
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
|
|
|
|
// see if there is a torrent with this info-hash
|
|
|
|
boost::shared_ptr<torrent> t = ses->find_torrent(info_hash).lock();
|
|
|
|
|
|
|
|
// if there isn't, fail
|
|
|
|
if (!t) return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
|
|
|
|
// if the torrent we found isn't an SSL torrent, also fail.
|
|
|
|
if (!t->is_ssl_torrent()) return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
|
2013-07-28 17:06:28 +02:00
|
|
|
// if the torrent doesn't have an SSL context and should not allow
|
|
|
|
// incoming SSL connections
|
|
|
|
if (!t->ssl_ctx()) return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
// use this torrent's certificate
|
2013-08-05 18:20:25 +02:00
|
|
|
SSL_CTX *torrent_context = t->ssl_ctx()->native_handle();
|
|
|
|
|
|
|
|
SSL_set_SSL_CTX(s, torrent_context);
|
|
|
|
SSL_set_verify(s, SSL_CTX_get_verify_mode(torrent_context), SSL_CTX_get_verify_callback(torrent_context));
|
2012-01-14 17:04:25 +01:00
|
|
|
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
session_impl::session_impl(
|
|
|
|
std::pair<int, int> listen_port_range
|
|
|
|
, fingerprint const& cl_fprint
|
2007-11-16 22:21:28 +01:00
|
|
|
, char const* listen_interface
|
2011-10-08 11:52:36 +02:00
|
|
|
, boost::uint32_t alert_mask
|
2007-11-16 22:21:28 +01:00
|
|
|
)
|
2009-05-24 23:49:19 +02:00
|
|
|
: m_ipv4_peer_pool(500)
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
, m_ipv6_peer_pool(500)
|
|
|
|
#endif
|
2008-04-09 07:19:11 +02:00
|
|
|
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
|
2009-05-07 00:36:24 +02:00
|
|
|
, m_send_buffers(send_buffer_size)
|
2008-04-09 07:19:11 +02:00
|
|
|
#endif
|
2009-05-07 00:36:24 +02:00
|
|
|
, m_files(40)
|
2008-02-08 11:22:05 +01:00
|
|
|
, m_io_service()
|
2010-10-12 10:57:43 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2012-01-14 17:04:25 +01:00
|
|
|
, m_ssl_ctx(m_io_service, asio::ssl::context::sslv23)
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
2013-07-20 01:31:44 +02:00
|
|
|
, m_alerts(m_settings.alert_queue_size, alert_mask)
|
2010-01-23 04:02:32 +01:00
|
|
|
, m_disk_thread(m_io_service, boost::bind(&session_impl::on_disk_queue, this), m_files)
|
2007-05-24 20:53:55 +02:00
|
|
|
, m_half_open(m_io_service)
|
2009-04-26 02:21:59 +02:00
|
|
|
, m_download_rate(peer_connection::download_channel)
|
2008-01-12 19:47:26 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
2009-04-26 02:21:59 +02:00
|
|
|
, m_upload_rate(peer_connection::upload_channel, true)
|
2008-01-12 19:47:26 +01:00
|
|
|
#else
|
2009-04-26 02:21:59 +02:00
|
|
|
, m_upload_rate(peer_connection::upload_channel)
|
2008-01-12 19:47:26 +01:00
|
|
|
#endif
|
2010-08-23 08:27:18 +02:00
|
|
|
, m_tracker_manager(*this, m_proxy)
|
2012-06-21 05:51:39 +02:00
|
|
|
, m_num_active_downloading(0)
|
|
|
|
, m_num_active_finished(0)
|
2013-08-31 23:06:43 +02:00
|
|
|
, m_key(0)
|
2007-09-22 18:27:29 +02:00
|
|
|
, m_listen_port_retries(listen_port_range.second - listen_port_range.first)
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
, m_i2p_conn(m_io_service)
|
|
|
|
#endif
|
2008-01-13 12:18:18 +01:00
|
|
|
, m_allowed_upload_slots(8)
|
2007-08-16 14:41:46 +02:00
|
|
|
, m_num_unchoked(0)
|
|
|
|
, m_unchoke_time_scaler(0)
|
2008-04-24 05:28:48 +02:00
|
|
|
, m_auto_manage_time_scaler(0)
|
2007-08-16 14:41:46 +02:00
|
|
|
, m_optimistic_unchoke_time_scaler(0)
|
2008-04-22 02:05:23 +02:00
|
|
|
, m_disconnect_time_scaler(90)
|
2008-05-19 06:06:25 +02:00
|
|
|
, m_auto_scrape_time_scaler(180)
|
2010-01-15 17:45:42 +01:00
|
|
|
, m_next_explicit_cache_torrent(0)
|
|
|
|
, m_cache_rotation_timer(0)
|
2010-02-09 04:04:41 +01:00
|
|
|
, m_peak_up_rate(0)
|
|
|
|
, m_peak_down_rate(0)
|
2009-05-25 04:45:51 +02:00
|
|
|
, m_created(time_now_hires())
|
2009-04-30 07:49:46 +02:00
|
|
|
, m_last_tick(m_created)
|
2010-10-17 20:36:37 +02:00
|
|
|
, m_last_second_tick(m_created - milliseconds(900))
|
2011-01-23 03:09:54 +01:00
|
|
|
, m_last_disk_performance_warning(min_time())
|
2011-03-16 08:45:51 +01:00
|
|
|
, m_last_disk_queue_performance_warning(min_time())
|
2009-04-30 07:49:46 +02:00
|
|
|
, m_last_choke(m_created)
|
2011-01-18 04:41:54 +01:00
|
|
|
, m_next_rss_update(min_time())
|
2007-03-15 23:03:56 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2010-02-14 02:39:55 +01:00
|
|
|
, m_dht_announce_timer(m_io_service)
|
2012-08-03 07:13:40 +02:00
|
|
|
, m_dht_interval_update_torrents(0)
|
2007-03-15 23:03:56 +01:00
|
|
|
#endif
|
2010-05-30 03:33:03 +02:00
|
|
|
, m_external_udp_port(0)
|
2012-06-22 06:21:20 +02:00
|
|
|
, m_udp_socket(m_io_service, m_half_open)
|
2013-08-31 22:19:03 +02:00
|
|
|
// TODO: 4 in order to support SSL over uTP, the utp_socket manager either
|
|
|
|
// needs to be able to receive packets on multiple ports, or we need to
|
|
|
|
// peek into the first few bytes the payload stream of a socket to determine
|
|
|
|
// whether or not it's an SSL connection. (The former is simpler but won't
|
|
|
|
// do as well with NATs)
|
2010-11-29 02:33:05 +01:00
|
|
|
, m_utp_socket_manager(m_settings, m_udp_socket
|
|
|
|
, boost::bind(&session_impl::incoming_connection, this, _1))
|
2010-12-17 04:10:56 +01:00
|
|
|
, m_boost_connections(0)
|
2006-12-15 18:47:21 +01:00
|
|
|
, m_timer(m_io_service)
|
2010-02-05 09:23:17 +01:00
|
|
|
, m_lsd_announce_timer(m_io_service)
|
2010-02-14 02:39:55 +01:00
|
|
|
, m_host_resolver(m_io_service)
|
2012-07-04 22:41:22 +02:00
|
|
|
, m_current_connect_attempts(0)
|
2010-02-14 02:39:55 +01:00
|
|
|
, m_tick_residual(0)
|
2011-03-04 08:00:27 +01:00
|
|
|
, m_non_filtered_torrents(0)
|
2008-02-17 23:51:03 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2013-03-10 10:19:58 +01:00
|
|
|
, m_logpath(".")
|
2008-04-05 06:53:22 +02:00
|
|
|
#endif
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
2008-04-11 10:46:43 +02:00
|
|
|
, m_asnum_db(0)
|
|
|
|
, m_country_db(0)
|
2007-11-16 22:21:28 +01:00
|
|
|
#endif
|
2008-07-11 09:30:04 +02:00
|
|
|
, m_total_failed_bytes(0)
|
|
|
|
, m_total_redundant_bytes(0)
|
2012-11-03 04:50:12 +01:00
|
|
|
, m_pending_auto_manage(false)
|
|
|
|
, m_need_auto_manage(false)
|
2013-11-26 03:00:02 +01:00
|
|
|
, m_abort(false)
|
|
|
|
, m_paused(false)
|
|
|
|
, m_incoming_connection(false)
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS && defined BOOST_HAS_PTHREADS
|
2010-12-04 23:20:31 +01:00
|
|
|
, m_network_thread(0)
|
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2012-10-06 16:31:14 +02:00
|
|
|
m_posting_torrent_updates = false;
|
|
|
|
#endif
|
|
|
|
|
2011-11-16 03:29:59 +01:00
|
|
|
memset(m_redundant_bytes, 0, sizeof(m_redundant_bytes));
|
2011-03-14 08:47:24 +01:00
|
|
|
m_udp_socket.set_rate_limit(m_settings.dht_upload_rate_limit);
|
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
m_udp_socket.subscribe(&m_tracker_manager);
|
|
|
|
m_udp_socket.subscribe(&m_utp_socket_manager);
|
|
|
|
m_udp_socket.subscribe(this);
|
|
|
|
|
2011-01-30 11:04:15 +01:00
|
|
|
m_disk_queues[0] = 0;
|
|
|
|
m_disk_queues[1] = 0;
|
|
|
|
|
2011-12-29 13:15:29 +01:00
|
|
|
#ifdef TORRENT_REQUEST_LOGGING
|
|
|
|
char log_filename[200];
|
2013-06-18 05:18:47 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
const int pid = GetCurrentProcessId();
|
|
|
|
#else
|
|
|
|
const int pid = getpid();
|
|
|
|
#endif
|
|
|
|
snprintf(log_filename, sizeof(log_filename), "requests-%d.log", pid);
|
2011-12-29 13:15:29 +01:00
|
|
|
m_request_log = fopen(log_filename, "w+");
|
|
|
|
if (m_request_log == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to open request log file: (%d) %s\n", errno, strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-10 10:19:58 +01:00
|
|
|
error_code ec;
|
|
|
|
if (!listen_interface) listen_interface = "0.0.0.0";
|
|
|
|
m_listen_interface = tcp::endpoint(address::from_string(listen_interface, ec), listen_port_range.first);
|
|
|
|
TORRENT_ASSERT_VAL(!ec, ec);
|
|
|
|
|
|
|
|
// ---- generate a peer id ----
|
|
|
|
static seed_random_generator seeder;
|
|
|
|
|
|
|
|
std::string print = cl_fprint.to_string();
|
|
|
|
TORRENT_ASSERT_VAL(print.length() <= 20, print.length());
|
|
|
|
|
|
|
|
// the client's fingerprint
|
|
|
|
std::copy(
|
|
|
|
print.begin()
|
|
|
|
, print.begin() + print.length()
|
|
|
|
, m_peer_id.begin());
|
|
|
|
|
|
|
|
url_random((char*)&m_peer_id[print.length()], (char*)&m_peer_id[0] + 20);
|
|
|
|
|
|
|
|
update_rate_settings();
|
|
|
|
update_connections_limit();
|
|
|
|
update_unchoke_limit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::start_session()
|
|
|
|
{
|
2010-05-09 08:03:52 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
m_logger = create_log("main_session", listen_port(), false);
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("log created");
|
2010-05-09 08:03:52 +02:00
|
|
|
#endif
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
error_code ec;
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
m_ssl_ctx.set_verify_mode(asio::ssl::context::verify_none, ec);
|
2012-02-26 01:01:53 +01:00
|
|
|
#if BOOST_VERSION >= 104700
|
2012-04-06 06:11:04 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x90812f
|
2012-01-14 17:04:25 +01:00
|
|
|
SSL_CTX_set_tlsext_servername_callback(m_ssl_ctx.native_handle(), servername_callback);
|
|
|
|
SSL_CTX_set_tlsext_servername_arg(m_ssl_ctx.native_handle(), this);
|
2012-04-06 06:11:04 +02:00
|
|
|
#endif // OPENSSL_VERSION_NUMBER
|
2012-02-25 09:31:25 +01:00
|
|
|
#endif // BOOST_VERSION
|
2010-10-12 10:57:43 +02:00
|
|
|
#endif
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
m_next_dht_torrent = m_torrents.begin();
|
|
|
|
#endif
|
2010-02-05 09:23:17 +01:00
|
|
|
m_next_lsd_torrent = m_torrents.begin();
|
|
|
|
m_next_connect_torrent = m_torrents.begin();
|
2011-03-27 22:35:38 +02:00
|
|
|
m_next_disk_peer = m_connections.begin();
|
2010-02-05 09:23:17 +01:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
m_tcp_mapping[0] = -1;
|
|
|
|
m_tcp_mapping[1] = -1;
|
|
|
|
m_udp_mapping[0] = -1;
|
|
|
|
m_udp_mapping[1] = -1;
|
2012-01-14 17:04:25 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
m_ssl_mapping[0] = -1;
|
|
|
|
m_ssl_mapping[1] = -1;
|
|
|
|
#endif
|
2007-09-10 03:57:40 +02:00
|
|
|
#ifdef WIN32
|
2007-10-15 03:15:58 +02:00
|
|
|
// windows XP has a limit on the number of
|
|
|
|
// simultaneous half-open TCP connections
|
2010-03-31 04:50:09 +02:00
|
|
|
// here's a table:
|
|
|
|
|
|
|
|
// windows version half-open connections limit
|
|
|
|
// --------------------- ---------------------------
|
|
|
|
// XP sp1 and earlier infinite
|
|
|
|
// earlier than vista 8
|
|
|
|
// vista sp1 and earlier 5
|
|
|
|
// vista sp2 and later infinite
|
|
|
|
|
|
|
|
// windows release version number
|
|
|
|
// ----------------------------------- --------------
|
|
|
|
// Windows 7 6.1
|
|
|
|
// Windows Server 2008 R2 6.1
|
|
|
|
// Windows Server 2008 6.0
|
|
|
|
// Windows Vista 6.0
|
|
|
|
// Windows Server 2003 R2 5.2
|
|
|
|
// Windows Home Server 5.2
|
|
|
|
// Windows Server 2003 5.2
|
|
|
|
// Windows XP Professional x64 Edition 5.2
|
|
|
|
// Windows XP 5.1
|
|
|
|
// Windows 2000 5.0
|
|
|
|
|
|
|
|
OSVERSIONINFOEX osv;
|
|
|
|
memset(&osv, 0, sizeof(osv));
|
|
|
|
osv.dwOSVersionInfoSize = sizeof(osv);
|
|
|
|
GetVersionEx((OSVERSIONINFO*)&osv);
|
|
|
|
|
|
|
|
// the low two bytes of windows_version is the actual
|
|
|
|
// version.
|
|
|
|
boost::uint32_t windows_version
|
|
|
|
= ((osv.dwMajorVersion & 0xff) << 16)
|
2010-03-31 06:22:48 +02:00
|
|
|
| ((osv.dwMinorVersion & 0xff) << 8)
|
2010-03-31 04:50:09 +02:00
|
|
|
| (osv.wServicePackMajor & 0xff);
|
|
|
|
|
|
|
|
// this is the format of windows_version
|
|
|
|
// xx xx xx
|
|
|
|
// | | |
|
|
|
|
// | | + service pack version
|
|
|
|
// | + minor version
|
|
|
|
// + major version
|
|
|
|
|
|
|
|
// the least significant byte is the major version
|
|
|
|
// and the most significant one is the minor version
|
|
|
|
if (windows_version >= 0x060100)
|
|
|
|
{
|
|
|
|
// windows 7 and up doesn't have a half-open limit
|
|
|
|
m_half_open.limit(0);
|
|
|
|
}
|
|
|
|
else if (windows_version >= 0x060002)
|
|
|
|
{
|
|
|
|
// on vista SP 2 and up, there's no limit
|
|
|
|
m_half_open.limit(0);
|
|
|
|
}
|
|
|
|
else if (windows_version >= 0x060000)
|
2007-10-15 03:15:58 +02:00
|
|
|
{
|
|
|
|
// on vista the limit is 5 (in home edition)
|
|
|
|
m_half_open.limit(4);
|
|
|
|
}
|
2010-03-31 04:50:09 +02:00
|
|
|
else if (windows_version >= 0x050102)
|
|
|
|
{
|
|
|
|
// on XP SP2 the limit is 10
|
|
|
|
m_half_open.limit(9);
|
|
|
|
}
|
2007-10-15 03:15:58 +02:00
|
|
|
else
|
|
|
|
{
|
2010-03-31 04:50:09 +02:00
|
|
|
// before XP SP2, there was no limit
|
|
|
|
m_half_open.limit(0);
|
2007-10-15 03:15:58 +02:00
|
|
|
}
|
2010-10-09 21:09:38 +02:00
|
|
|
m_settings.half_open_limit = m_half_open.limit();
|
2007-09-10 03:57:40 +02:00
|
|
|
#endif
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
m_bandwidth_channel[peer_connection::download_channel] = &m_download_channel;
|
|
|
|
m_bandwidth_channel[peer_connection::upload_channel] = &m_upload_channel;
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2008-10-22 03:12:14 +02:00
|
|
|
#ifdef TORRENT_UPNP_LOGGING
|
|
|
|
m_upnp_log.open("upnp.log", std::ios::in | std::ios::out | std::ios::trunc);
|
|
|
|
#endif
|
2008-05-19 04:52:32 +02:00
|
|
|
|
2010-05-09 08:03:52 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-24 02:44:07 +02:00
|
|
|
|
2010-11-15 05:43:53 +01:00
|
|
|
char tmp[300];
|
|
|
|
snprintf(tmp, sizeof(tmp), "libtorrent configuration: %s\n"
|
2011-02-15 11:05:25 +01:00
|
|
|
"libtorrent version: %s\n"
|
|
|
|
"libtorrent revision: %s\n\n"
|
2010-11-15 05:43:53 +01:00
|
|
|
, TORRENT_CFG_STRING
|
|
|
|
, LIBTORRENT_VERSION
|
|
|
|
, LIBTORRENT_REVISION);
|
|
|
|
(*m_logger) << tmp;
|
2010-10-24 02:44:07 +02:00
|
|
|
|
2013-12-02 11:00:03 +01:00
|
|
|
#endif // TORRENT_VERBOSE_LOGGING
|
2007-05-14 00:01:21 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_STATS
|
2011-04-06 08:27:42 +02:00
|
|
|
|
2011-04-26 09:05:26 +02:00
|
|
|
m_stats_logger = 0;
|
2011-06-01 09:06:20 +02:00
|
|
|
m_log_seq = 0;
|
2011-04-06 08:27:42 +02:00
|
|
|
m_stats_logging_enabled = true;
|
2011-06-29 00:20:34 +02:00
|
|
|
|
|
|
|
memset(&m_last_cache_status, 0, sizeof(m_last_cache_status));
|
|
|
|
get_vm_stats(&m_last_vm_stat);
|
|
|
|
|
2011-08-27 11:47:36 +02:00
|
|
|
m_last_failed = 0;
|
|
|
|
m_last_redundant = 0;
|
|
|
|
m_last_uploaded = 0;
|
|
|
|
m_last_downloaded = 0;
|
2011-11-28 02:27:55 +01:00
|
|
|
get_thread_cpu_usage(&m_network_thread_cpu_usage);
|
2011-08-27 11:47:36 +02:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
reset_stat_counters();
|
2011-02-11 18:39:22 +01:00
|
|
|
rotate_stats_log();
|
2011-02-03 05:09:50 +01:00
|
|
|
#endif
|
|
|
|
#ifdef TORRENT_DISK_STATS
|
|
|
|
m_buffer_usage_logger.open("buffer_stats.log", std::ios::trunc);
|
2008-02-12 09:34:57 +01:00
|
|
|
m_buffer_allocations = 0;
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
|
|
|
|
2009-10-03 21:02:31 +02:00
|
|
|
#if defined TORRENT_BSD || defined TORRENT_LINUX
|
|
|
|
// ---- auto-cap open files ----
|
|
|
|
|
|
|
|
struct rlimit rl;
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" max number of open files: %d", rl.rlim_cur);
|
2009-10-03 21:02:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// deduct some margin for epoll/kqueue, log files,
|
|
|
|
// futexes, shared objects etc.
|
|
|
|
rl.rlim_cur -= 20;
|
|
|
|
|
|
|
|
// 80% of the available file descriptors should go
|
2010-10-09 21:09:38 +02:00
|
|
|
m_settings.connections_limit = (std::min)(m_settings.connections_limit
|
|
|
|
, int(rl.rlim_cur * 8 / 10));
|
2009-10-03 21:02:31 +02:00
|
|
|
// 20% goes towards regular files
|
|
|
|
m_files.resize((std::min)(m_files.size_limit(), int(rl.rlim_cur * 2 / 10)));
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-09 21:09:38 +02:00
|
|
|
(*m_logger) << time_now_string() << " max connections: " << m_settings.connections_limit << "\n";
|
2009-10-03 21:02:31 +02:00
|
|
|
(*m_logger) << time_now_string() << " max files: " << m_files.size_limit() << "\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif // TORRENT_BSD || TORRENT_LINUX
|
|
|
|
|
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" generated peer ID: %s", m_peer_id.to_string().c_str());
|
2010-12-24 01:37:01 +01:00
|
|
|
#endif
|
|
|
|
|
2013-03-10 10:19:58 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
session_log(" spawning network thread");
|
|
|
|
#endif
|
|
|
|
m_thread.reset(new thread(boost::bind(&session_impl::main_thread, this)));
|
2010-12-04 23:20:31 +01:00
|
|
|
}
|
2010-10-09 21:09:38 +02:00
|
|
|
|
2011-02-11 18:39:22 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
void session_impl::rotate_stats_log()
|
|
|
|
{
|
|
|
|
if (m_stats_logger)
|
|
|
|
{
|
|
|
|
++m_log_seq;
|
|
|
|
fclose(m_stats_logger);
|
|
|
|
}
|
2011-11-13 09:12:11 +01:00
|
|
|
|
2012-03-04 12:18:27 +01:00
|
|
|
// make these cumulative for easier reading of graphs
|
|
|
|
// reset them every time the log is rotated though,
|
|
|
|
// to make them cumulative per one-hour graph
|
|
|
|
m_error_peers = 0;
|
|
|
|
m_disconnected_peers = 0;
|
|
|
|
m_eof_peers = 0;
|
|
|
|
m_connreset_peers = 0;
|
|
|
|
m_connrefused_peers = 0;
|
|
|
|
m_connaborted_peers = 0;
|
|
|
|
m_perm_peers = 0;
|
|
|
|
m_buffer_peers = 0;
|
|
|
|
m_unreachable_peers = 0;
|
|
|
|
m_broken_pipe_peers = 0;
|
|
|
|
m_addrinuse_peers = 0;
|
|
|
|
m_no_access_peers = 0;
|
|
|
|
m_invalid_arg_peers = 0;
|
|
|
|
m_aborted_peers = 0;
|
|
|
|
m_error_incoming_peers = 0;
|
|
|
|
m_error_outgoing_peers = 0;
|
|
|
|
m_error_rc4_peers = 0;
|
|
|
|
m_error_encrypted_peers = 0;
|
|
|
|
m_error_tcp_peers = 0;
|
|
|
|
m_error_utp_peers = 0;
|
|
|
|
m_connect_timeouts = 0;
|
|
|
|
m_uninteresting_peers = 0;
|
|
|
|
m_transport_timeout_peers = 0;
|
|
|
|
m_timeout_peers = 0;
|
|
|
|
m_no_memory_peers = 0;
|
|
|
|
m_too_many_peers = 0;
|
|
|
|
|
2011-02-11 18:39:22 +01:00
|
|
|
error_code ec;
|
|
|
|
char filename[100];
|
2011-11-13 09:12:11 +01:00
|
|
|
create_directory("session_stats", ec);
|
2013-06-18 10:48:19 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
const int pid = GetCurrentProcessId();
|
|
|
|
#else
|
|
|
|
const int pid = getpid();
|
|
|
|
#endif
|
|
|
|
snprintf(filename, sizeof(filename), "session_stats/%d.%04d.log", pid, m_log_seq);
|
2011-02-11 18:39:22 +01:00
|
|
|
m_stats_logger = fopen(filename, "w+");
|
|
|
|
if (m_stats_logger == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to create session stats log file \"%s\": (%d) %s\n"
|
|
|
|
, filename, errno, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_last_log_rotation = time_now();
|
|
|
|
|
2011-03-27 22:58:43 +02:00
|
|
|
fputs("second:uploaded bytes:downloaded bytes:downloading torrents:seeding torrents"
|
2011-02-11 18:39:22 +01:00
|
|
|
":peers:connecting peers:disk block buffers:num list peers"
|
2011-02-12 23:15:03 +01:00
|
|
|
":peer allocations:peer storage bytes"
|
2012-03-11 06:35:29 +01:00
|
|
|
":checking torrents"
|
|
|
|
":stopped torrents"
|
|
|
|
":upload-only torrents"
|
|
|
|
":queued seed torrents"
|
|
|
|
":queued download torrents"
|
2011-02-11 18:39:22 +01:00
|
|
|
":peers bw-up:peers bw-down:peers disk-up:peers disk-down"
|
2011-03-27 22:58:43 +02:00
|
|
|
":upload rate:download rate:disk write queued bytes"
|
2011-02-11 18:39:22 +01:00
|
|
|
":peers down 0:peers down 0-2:peers down 2-5:peers down 5-10:peers down 10-50"
|
|
|
|
":peers down 50-100:peers down 100-"
|
2011-03-17 06:31:06 +01:00
|
|
|
":peers up 0:peers up 0-2:peers up 2-5:peers up 5-10:peers up 10-50:peers up 50-100"
|
2011-02-11 18:39:22 +01:00
|
|
|
":peers up 100-:error peers"
|
|
|
|
":peers down interesting:peers down unchoked:peers down requests"
|
|
|
|
":peers up interested:peers up unchoked:peers up requests"
|
|
|
|
":peer disconnects:peers eof:peers connection reset"
|
|
|
|
":outstanding requests:outstanding end-game requests"
|
|
|
|
":outstanding writing blocks"
|
|
|
|
":end game piece picker blocks"
|
|
|
|
":piece picker blocks"
|
|
|
|
":piece picks"
|
|
|
|
":reject piece picks"
|
|
|
|
":unchoke piece picks"
|
|
|
|
":incoming redundant piece picks"
|
|
|
|
":incoming piece picks"
|
|
|
|
":end game piece picks"
|
|
|
|
":snubbed piece picks"
|
2011-02-14 05:48:02 +01:00
|
|
|
":connect timeouts"
|
|
|
|
":uninteresting peers disconnect"
|
2011-02-14 06:38:59 +01:00
|
|
|
":timeout peers"
|
2011-02-11 18:39:22 +01:00
|
|
|
":% failed payload bytes"
|
|
|
|
":% wasted payload bytes"
|
|
|
|
":% protocol bytes"
|
2011-03-11 08:37:12 +01:00
|
|
|
":disk read time"
|
|
|
|
":disk write time"
|
|
|
|
":disk queue time"
|
|
|
|
":disk queue size"
|
|
|
|
":disk queued bytes"
|
|
|
|
":read cache hits"
|
|
|
|
":disk block read"
|
2011-03-12 22:41:05 +01:00
|
|
|
":disk block written"
|
2011-03-13 05:34:57 +01:00
|
|
|
":failed bytes"
|
|
|
|
":redundant bytes"
|
2011-03-13 20:06:19 +01:00
|
|
|
":error torrents"
|
2011-03-14 04:35:18 +01:00
|
|
|
":read disk cache size"
|
|
|
|
":disk cache size"
|
|
|
|
":disk buffer allocations"
|
2011-03-14 06:21:46 +01:00
|
|
|
":disk hash time"
|
2011-03-18 04:07:10 +01:00
|
|
|
":disk job time"
|
2011-03-15 03:21:28 +01:00
|
|
|
":disk sort time"
|
2011-03-17 06:31:06 +01:00
|
|
|
":connection attempts"
|
|
|
|
":banned peers"
|
|
|
|
":banned for hash failure"
|
|
|
|
":cache size"
|
|
|
|
":max connections"
|
|
|
|
":connect candidates"
|
2011-03-18 06:49:31 +01:00
|
|
|
":disk queue limit"
|
|
|
|
":disk queue low watermark"
|
2011-03-20 06:47:27 +01:00
|
|
|
":% read time"
|
|
|
|
":% write time"
|
|
|
|
":% hash time"
|
|
|
|
":% sort time"
|
|
|
|
":disk read back"
|
|
|
|
":% read back"
|
2011-03-20 10:04:03 +01:00
|
|
|
":disk read queue size"
|
2011-03-25 08:49:32 +01:00
|
|
|
":tick interval"
|
|
|
|
":tick residual"
|
2011-03-27 10:21:26 +02:00
|
|
|
":max unchoked"
|
|
|
|
":read job queue size limit"
|
2011-03-27 22:58:43 +02:00
|
|
|
":smooth upload rate"
|
|
|
|
":smooth download rate"
|
2011-06-13 18:20:21 +02:00
|
|
|
":num end-game peers"
|
2011-06-20 06:33:46 +02:00
|
|
|
":TCP up rate"
|
|
|
|
":TCP down rate"
|
|
|
|
":TCP up limit"
|
2011-06-21 09:44:13 +02:00
|
|
|
":TCP down limit"
|
2011-06-20 06:33:46 +02:00
|
|
|
":uTP up rate"
|
|
|
|
":uTP down rate"
|
2011-06-21 09:44:13 +02:00
|
|
|
":uTP peak send delay"
|
|
|
|
":uTP avg send delay"
|
2011-09-28 02:03:12 +02:00
|
|
|
":uTP peak recv delay"
|
|
|
|
":uTP avg recv delay"
|
2011-06-26 21:00:40 +02:00
|
|
|
":read ops/s"
|
|
|
|
":write ops/s"
|
2011-06-29 00:20:34 +02:00
|
|
|
":active resident pages"
|
|
|
|
":inactive resident pages"
|
|
|
|
":pinned resident pages"
|
|
|
|
":free pages"
|
|
|
|
":pageins"
|
|
|
|
":pageouts"
|
|
|
|
":page faults"
|
2011-07-03 19:21:45 +02:00
|
|
|
":smooth read ops/s"
|
|
|
|
":smooth write ops/s"
|
2011-09-23 22:57:42 +02:00
|
|
|
":pending reading bytes"
|
2011-10-17 07:17:21 +02:00
|
|
|
":read_counter"
|
|
|
|
":write_counter"
|
|
|
|
":tick_counter"
|
|
|
|
":lsd_counter"
|
|
|
|
":lsd_peer_counter"
|
|
|
|
":udp_counter"
|
|
|
|
":accept_counter"
|
|
|
|
":disk_queue_counter"
|
|
|
|
":disk_read_counter"
|
|
|
|
":disk_write_counter"
|
2011-10-17 08:54:02 +02:00
|
|
|
":up 8:up 16:up 32:up 64:up 128:up 256:up 512:up 1024:up 2048:up 4096:up 8192:up 16384:up 32768:up 65536:up 131072:up 262144:up 524288:up 1048576"
|
|
|
|
":down 8:down 16:down 32:down 64:down 128:down 256:down 512:down 1024:down 2048:down 4096:down 8192:down 16384:down 32768:down 65536:down 131072:down 262144:down 524288:down 1048576"
|
2011-10-17 19:12:08 +02:00
|
|
|
":network thread system time"
|
|
|
|
":network thread user+system time"
|
2011-11-16 03:29:59 +01:00
|
|
|
|
|
|
|
":redundant timed-out"
|
|
|
|
":redundant cancelled"
|
|
|
|
":redundant unknown"
|
|
|
|
":redundant seed"
|
|
|
|
":redundant end-game"
|
|
|
|
":redundant closing"
|
2011-11-24 18:50:57 +01:00
|
|
|
":no memory peer errors"
|
|
|
|
":too many peers"
|
|
|
|
":transport timeout peers"
|
2012-01-17 04:11:16 +01:00
|
|
|
":uTP idle"
|
|
|
|
":uTP syn-sent"
|
|
|
|
":uTP connected"
|
|
|
|
":uTP fin-sent"
|
|
|
|
":uTP close-wait"
|
2012-03-02 09:52:54 +01:00
|
|
|
|
|
|
|
":tcp peers"
|
|
|
|
":utp peers"
|
|
|
|
|
|
|
|
":connection refused peers"
|
|
|
|
":connection aborted peers"
|
|
|
|
":permission denied peers"
|
|
|
|
":no buffer peers"
|
|
|
|
":host unreachable peers"
|
|
|
|
":broken pipe peers"
|
|
|
|
":address in use peers"
|
|
|
|
":access denied peers"
|
|
|
|
":invalid argument peers"
|
|
|
|
":operation aborted peers"
|
|
|
|
|
|
|
|
":error incoming peers"
|
|
|
|
":error outgoing peers"
|
|
|
|
":error rc4 peers"
|
|
|
|
":error encrypted peers"
|
|
|
|
":error tcp peers"
|
|
|
|
":error utp peers"
|
2012-03-04 12:18:27 +01:00
|
|
|
|
|
|
|
":total peers"
|
2012-03-05 11:05:20 +01:00
|
|
|
":pending incoming block requests"
|
|
|
|
":average pending incoming block requests"
|
2012-03-06 11:34:18 +01:00
|
|
|
|
|
|
|
":torrents want more peers"
|
|
|
|
":average peers per limit"
|
2012-03-09 07:24:01 +01:00
|
|
|
|
|
|
|
":piece requests"
|
|
|
|
":max piece requests"
|
|
|
|
":invalid piece requests"
|
|
|
|
":choked piece requests"
|
|
|
|
":cancelled piece requests"
|
|
|
|
":piece rejects"
|
|
|
|
|
2012-03-09 18:03:10 +01:00
|
|
|
":peers up send buffer"
|
|
|
|
|
2013-09-14 12:06:48 +02:00
|
|
|
":packet_loss"
|
|
|
|
":timeout"
|
|
|
|
":packets_in"
|
|
|
|
":packets_out"
|
|
|
|
":fast_retransmit"
|
|
|
|
":packet_resend"
|
|
|
|
":samples_above_target"
|
|
|
|
":samples_below_target"
|
|
|
|
":payload_pkts_in"
|
|
|
|
":payload_pkts_out"
|
|
|
|
":invalid_pkts_in"
|
|
|
|
":redundant_pkts_in"
|
|
|
|
|
2011-02-11 18:39:22 +01:00
|
|
|
"\n\n", m_stats_logger);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-03 04:50:12 +01:00
|
|
|
void session_impl::trigger_auto_manage()
|
|
|
|
{
|
2013-02-17 23:56:39 +01:00
|
|
|
if (m_pending_auto_manage || m_abort) return;
|
2012-11-03 04:50:12 +01:00
|
|
|
|
|
|
|
m_pending_auto_manage = true;
|
|
|
|
m_need_auto_manage = true;
|
|
|
|
m_io_service.post(boost::bind(&session_impl::on_trigger_auto_manage, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::on_trigger_auto_manage()
|
|
|
|
{
|
2013-02-07 07:41:11 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2012-11-03 04:50:12 +01:00
|
|
|
assert(m_pending_auto_manage);
|
|
|
|
m_pending_auto_manage = false;
|
|
|
|
if (!m_need_auto_manage) return;
|
|
|
|
recalculate_auto_managed_torrents();
|
|
|
|
}
|
|
|
|
|
2012-08-03 07:13:40 +02:00
|
|
|
void session_impl::update_dht_announce_interval()
|
|
|
|
{
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2013-10-10 11:08:56 +02:00
|
|
|
if (!m_dht) return;
|
2012-08-03 07:13:40 +02:00
|
|
|
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_dht_announce");
|
|
|
|
#endif
|
|
|
|
m_dht_interval_update_torrents = m_torrents.size();
|
|
|
|
error_code ec;
|
|
|
|
int delay = (std::max)(m_settings.dht_announce_interval
|
|
|
|
/ (std::max)(int(m_torrents.size()), 1), 1);
|
|
|
|
m_dht_announce_timer.expires_from_now(seconds(delay), ec);
|
|
|
|
m_dht_announce_timer.async_wait(
|
|
|
|
boost::bind(&session_impl::on_dht_announce, this, _1));
|
|
|
|
TORRENT_ASSERT(!ec);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:20:31 +01:00
|
|
|
void session_impl::init()
|
2010-09-25 22:07:27 +02:00
|
|
|
{
|
2010-10-17 18:15:32 +02:00
|
|
|
#if defined TORRENT_LOGGING || defined TORRENT_VERBOSE_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" *** session thread init");
|
2010-10-17 18:15:32 +02:00
|
|
|
#endif
|
|
|
|
|
2010-09-25 22:07:27 +02:00
|
|
|
// this is where we should set up all async operations. This
|
|
|
|
// is called from within the network thread as opposed to the
|
|
|
|
// constructor which is called from the main thread
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
2010-12-24 19:15:01 +01:00
|
|
|
async_inc_threads();
|
2010-11-28 02:47:30 +01:00
|
|
|
add_outstanding_async("session_impl::on_tick");
|
|
|
|
#endif
|
2010-09-25 22:07:27 +02:00
|
|
|
error_code ec;
|
2010-10-17 18:15:32 +02:00
|
|
|
m_io_service.post(boost::bind(&session_impl::on_tick, this, ec));
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_lsd_announce");
|
|
|
|
#endif
|
2010-02-05 09:23:17 +01:00
|
|
|
int delay = (std::max)(m_settings.local_service_announce_interval
|
|
|
|
/ (std::max)(int(m_torrents.size()), 1), 1);
|
|
|
|
m_lsd_announce_timer.expires_from_now(seconds(delay), ec);
|
|
|
|
m_lsd_announce_timer.async_wait(
|
2010-04-30 21:08:16 +02:00
|
|
|
boost::bind(&session_impl::on_lsd_announce, this, _1));
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(!ec);
|
2010-02-05 09:23:17 +01:00
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2012-08-03 07:13:40 +02:00
|
|
|
update_dht_announce_interval();
|
2010-02-14 02:39:55 +01:00
|
|
|
#endif
|
|
|
|
|
2010-10-17 18:15:32 +02:00
|
|
|
#if defined TORRENT_LOGGING || defined TORRENT_VERBOSE_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" open listen port");
|
2010-10-17 18:15:32 +02:00
|
|
|
#endif
|
2011-02-16 07:35:53 +01:00
|
|
|
// no reuse_address and allow system defined port
|
|
|
|
open_listen_port(0, ec);
|
2010-10-17 18:15:32 +02:00
|
|
|
#if defined TORRENT_LOGGING || defined TORRENT_VERBOSE_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" done starting session");
|
2010-10-17 18:15:32 +02:00
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
void session_impl::save_state(entry* eptr, boost::uint32_t flags) const
|
2009-12-03 06:11:57 +01:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
entry& e = *eptr;
|
|
|
|
|
2010-10-10 04:22:57 +02:00
|
|
|
all_default_values def;
|
|
|
|
|
2011-02-15 11:05:25 +01:00
|
|
|
for (int i = 0; i < int(sizeof(all_settings)/sizeof(all_settings[0])); ++i)
|
2010-03-06 08:49:40 +01:00
|
|
|
{
|
2010-03-10 03:28:40 +01:00
|
|
|
session_category const& c = all_settings[i];
|
|
|
|
if ((flags & c.flag) == 0) continue;
|
2010-10-10 04:22:57 +02:00
|
|
|
save_struct(e[c.name], reinterpret_cast<char const*>(this) + c.offset
|
|
|
|
, c.map, c.num_entries, reinterpret_cast<char const*>(&def) + c.default_offset);
|
2010-03-06 08:49:40 +01:00
|
|
|
}
|
2010-03-10 03:28:40 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2010-03-06 08:49:40 +01:00
|
|
|
if (m_dht && (flags & session::save_dht_state))
|
2010-03-04 17:42:39 +01:00
|
|
|
{
|
2010-07-14 06:16:38 +02:00
|
|
|
e["dht state"] = m_dht->state();
|
2010-03-04 17:42:39 +01:00
|
|
|
}
|
2009-12-03 06:11:57 +01:00
|
|
|
#endif
|
2010-03-10 03:28:40 +01:00
|
|
|
|
2009-12-03 06:11:57 +01:00
|
|
|
#if TORRENT_USE_I2P
|
2010-03-06 08:49:40 +01:00
|
|
|
if (flags & session::save_i2p_proxy)
|
|
|
|
{
|
|
|
|
save_struct(e["i2p"], &i2p_proxy(), proxy_settings_map
|
2010-10-10 04:22:57 +02:00
|
|
|
, sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0])
|
|
|
|
, &def.m_proxy);
|
2010-03-06 08:49:40 +01:00
|
|
|
}
|
2009-12-03 06:11:57 +01:00
|
|
|
#endif
|
2010-03-04 17:42:39 +01:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
2010-03-06 08:49:40 +01:00
|
|
|
if (flags & session::save_as_map)
|
2010-03-04 17:42:39 +01:00
|
|
|
{
|
2010-03-06 08:49:40 +01:00
|
|
|
entry::dictionary_type& as_map = e["AS map"].dict();
|
|
|
|
char buf[10];
|
|
|
|
for (std::map<int, int>::const_iterator i = m_as_peak.begin()
|
|
|
|
, end(m_as_peak.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->second == 0) continue;
|
|
|
|
sprintf(buf, "%05d", i->first);
|
|
|
|
as_map[buf] = i->second;
|
|
|
|
}
|
2010-03-04 17:42:39 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-18 04:41:54 +01:00
|
|
|
if (flags & session::save_feeds)
|
|
|
|
{
|
|
|
|
entry::list_type& feeds = e["feeds"].list();
|
|
|
|
for (std::vector<boost::shared_ptr<feed> >::const_iterator i =
|
|
|
|
m_feeds.begin(), end(m_feeds.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
feeds.push_back(entry());
|
|
|
|
(*i)->save_state(feeds.back());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-29 11:37:21 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (ses_extension_list_t::const_iterator i = m_ses_extensions.begin()
|
|
|
|
, end(m_ses_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
(*i)->save_state(*eptr);
|
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2011-01-29 11:37:21 +01:00
|
|
|
}
|
|
|
|
#endif
|
2009-12-03 06:11:57 +01:00
|
|
|
}
|
|
|
|
|
2010-08-23 08:27:18 +02:00
|
|
|
void session_impl::set_proxy(proxy_settings const& s)
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2010-08-23 08:27:18 +02:00
|
|
|
m_proxy = s;
|
|
|
|
// in case we just set a socks proxy, we might have to
|
|
|
|
// open the socks incoming connection
|
|
|
|
if (!m_socks_listen_socket) open_new_incoming_socks_connection();
|
|
|
|
m_udp_socket.set_proxy_settings(m_proxy);
|
|
|
|
}
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
void session_impl::load_state(lazy_entry const* e)
|
2009-12-03 06:11:57 +01:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2009-12-03 06:11:57 +01:00
|
|
|
lazy_entry const* settings;
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
if (e->type() != lazy_entry::dict_t) return;
|
2010-03-04 17:42:39 +01:00
|
|
|
|
2011-02-15 11:05:25 +01:00
|
|
|
for (int i = 0; i < int(sizeof(all_settings)/sizeof(all_settings[0])); ++i)
|
2009-12-03 06:11:57 +01:00
|
|
|
{
|
2010-03-10 03:28:40 +01:00
|
|
|
session_category const& c = all_settings[i];
|
2010-07-14 06:16:38 +02:00
|
|
|
settings = e->dict_find_dict(c.name);
|
2010-03-10 08:14:10 +01:00
|
|
|
if (!settings) continue;
|
|
|
|
load_struct(*settings, reinterpret_cast<char*>(this) + c.offset, c.map, c.num_entries);
|
2009-12-03 06:11:57 +01:00
|
|
|
}
|
2010-10-09 21:09:38 +02:00
|
|
|
|
|
|
|
update_rate_settings();
|
|
|
|
update_connections_limit();
|
|
|
|
update_unchoke_limit();
|
2011-03-07 01:36:51 +01:00
|
|
|
m_alerts.set_alert_queue_size_limit(m_settings.alert_queue_size);
|
2010-10-09 21:09:38 +02:00
|
|
|
|
|
|
|
// in case we just set a socks proxy, we might have to
|
|
|
|
// open the socks incoming connection
|
|
|
|
if (!m_socks_listen_socket) open_new_incoming_socks_connection();
|
|
|
|
m_udp_socket.set_proxy_settings(m_proxy);
|
|
|
|
|
2009-12-03 06:11:57 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2010-07-14 06:16:38 +02:00
|
|
|
settings = e->dict_find_dict("dht state");
|
2010-03-04 17:42:39 +01:00
|
|
|
if (settings)
|
|
|
|
{
|
|
|
|
m_dht_state = *settings;
|
|
|
|
}
|
2009-12-03 06:11:57 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TORRENT_USE_I2P
|
2010-07-14 06:16:38 +02:00
|
|
|
settings = e->dict_find_dict("i2p");
|
2009-12-03 06:11:57 +01:00
|
|
|
if (settings)
|
|
|
|
{
|
|
|
|
proxy_settings s;
|
|
|
|
load_struct(*settings, &s, proxy_settings_map
|
|
|
|
, sizeof(proxy_settings_map)/sizeof(proxy_settings_map[0]));
|
|
|
|
set_i2p_proxy(s);
|
|
|
|
}
|
|
|
|
#endif
|
2010-03-04 17:42:39 +01:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
2010-07-14 06:16:38 +02:00
|
|
|
settings = e->dict_find_dict("AS map");
|
2010-03-04 17:42:39 +01:00
|
|
|
if (settings)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < settings->dict_size(); ++i)
|
|
|
|
{
|
|
|
|
std::pair<std::string, lazy_entry const*> item = settings->dict_at(i);
|
2010-03-04 23:34:49 +01:00
|
|
|
int as_num = atoi(item.first.c_str());
|
|
|
|
if (item.second->type() != lazy_entry::int_t || item.second->int_value() == 0) continue;
|
2010-03-04 17:42:39 +01:00
|
|
|
int& peak = m_as_peak[as_num];
|
2010-03-04 23:34:49 +01:00
|
|
|
if (peak < item.second->int_value()) peak = item.second->int_value();
|
2010-03-04 17:42:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-03-26 18:45:16 +01:00
|
|
|
|
|
|
|
if (m_settings.connection_speed < 0) m_settings.connection_speed = 200;
|
|
|
|
|
2010-03-10 08:14:10 +01:00
|
|
|
update_disk_thread_settings();
|
2011-01-18 04:41:54 +01:00
|
|
|
|
|
|
|
settings = e->dict_find_list("feeds");
|
|
|
|
if (settings)
|
|
|
|
{
|
|
|
|
m_feeds.reserve(settings->list_size());
|
|
|
|
for (int i = 0; i < settings->list_size(); ++i)
|
|
|
|
{
|
|
|
|
if (settings->list_at(i)->type() != lazy_entry::dict_t) continue;
|
2012-02-21 00:44:34 +01:00
|
|
|
boost::shared_ptr<feed> f(new_feed(*this, feed_settings()));
|
2011-01-18 04:41:54 +01:00
|
|
|
f->load_state(*settings->list_at(i));
|
2011-03-21 05:59:45 +01:00
|
|
|
f->update_feed();
|
2011-01-18 04:41:54 +01:00
|
|
|
m_feeds.push_back(f);
|
|
|
|
}
|
2011-03-21 05:59:45 +01:00
|
|
|
update_rss_feeds();
|
2011-01-18 04:41:54 +01:00
|
|
|
}
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
|
|
|
|
, end(m_ses_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
(*i)->load_state(*e);
|
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2011-01-29 11:37:21 +01:00
|
|
|
}
|
|
|
|
#endif
|
2009-12-03 06:11:57 +01:00
|
|
|
}
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct free_ptr
|
|
|
|
{
|
|
|
|
void* ptr_;
|
|
|
|
free_ptr(void* p): ptr_(p) {}
|
|
|
|
~free_ptr() { free(ptr_); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-04-11 10:46:43 +02:00
|
|
|
char const* session_impl::country_for_ip(address const& a)
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-04-11 10:46:43 +02:00
|
|
|
if (!a.is_v4() || m_country_db == 0) return 0;
|
|
|
|
return GeoIP_country_code_by_ipnum(m_country_db, a.to_v4().to_ulong());
|
|
|
|
}
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
int session_impl::as_for_ip(address const& a)
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-04-11 10:46:43 +02:00
|
|
|
if (!a.is_v4() || m_asnum_db == 0) return 0;
|
|
|
|
char* name = GeoIP_name_by_ipnum(m_asnum_db, a.to_v4().to_ulong());
|
2008-04-05 06:53:22 +02:00
|
|
|
if (name == 0) return 0;
|
|
|
|
free_ptr p(name);
|
|
|
|
// GeoIP returns the name as AS??? where ? is the AS-number
|
|
|
|
return atoi(name + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string session_impl::as_name_for_ip(address const& a)
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-04-11 10:46:43 +02:00
|
|
|
if (!a.is_v4() || m_asnum_db == 0) return std::string();
|
|
|
|
char* name = GeoIP_name_by_ipnum(m_asnum_db, a.to_v4().to_ulong());
|
2008-04-05 06:53:22 +02:00
|
|
|
if (name == 0) return std::string();
|
|
|
|
free_ptr p(name);
|
|
|
|
char* tmp = std::strchr(name, ' ');
|
|
|
|
if (tmp == 0) return std::string();
|
|
|
|
return tmp + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<const int, int>* session_impl::lookup_as(int as)
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
std::map<int, int>::iterator i = m_as_peak.lower_bound(as);
|
|
|
|
|
|
|
|
if (i == m_as_peak.end() || i->first != as)
|
|
|
|
{
|
|
|
|
// we don't have any data for this AS, insert a new entry
|
|
|
|
i = m_as_peak.insert(i, std::pair<int, int>(as, 0));
|
|
|
|
}
|
|
|
|
return &(*i);
|
|
|
|
}
|
|
|
|
|
2010-07-14 09:38:35 +02:00
|
|
|
void session_impl::load_asnum_db(std::string file)
|
2008-04-05 06:53:22 +02:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-04-11 10:46:43 +02:00
|
|
|
if (m_asnum_db) GeoIP_delete(m_asnum_db);
|
2010-07-14 09:38:35 +02:00
|
|
|
m_asnum_db = GeoIP_open(file.c_str(), GEOIP_STANDARD);
|
|
|
|
// return m_asnum_db;
|
2008-04-11 10:46:43 +02:00
|
|
|
}
|
|
|
|
|
2009-10-29 19:12:43 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
2013-08-02 07:03:22 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2010-07-14 09:38:35 +02:00
|
|
|
void session_impl::load_asnum_dbw(std::wstring file)
|
2008-12-21 09:48:06 +01:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-12-21 09:48:06 +01:00
|
|
|
if (m_asnum_db) GeoIP_delete(m_asnum_db);
|
|
|
|
std::string utf8;
|
|
|
|
wchar_utf8(file, utf8);
|
2008-12-21 20:15:53 +01:00
|
|
|
m_asnum_db = GeoIP_open(utf8.c_str(), GEOIP_STANDARD);
|
2010-07-14 09:38:35 +02:00
|
|
|
// return m_asnum_db;
|
2008-12-21 09:48:06 +01:00
|
|
|
}
|
|
|
|
|
2010-07-14 09:38:35 +02:00
|
|
|
void session_impl::load_country_dbw(std::wstring file)
|
2008-04-11 10:46:43 +02:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-04-11 10:46:43 +02:00
|
|
|
if (m_country_db) GeoIP_delete(m_country_db);
|
2009-04-04 10:32:23 +02:00
|
|
|
std::string utf8;
|
|
|
|
wchar_utf8(file, utf8);
|
|
|
|
m_country_db = GeoIP_open(utf8.c_str(), GEOIP_STANDARD);
|
2010-07-14 09:38:35 +02:00
|
|
|
// return m_country_db;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2013-08-02 07:03:22 +02:00
|
|
|
#endif // TORRENT_NO_DEPRECATE
|
2009-10-29 19:12:43 +01:00
|
|
|
#endif // TORRENT_USE_WSTRING
|
2008-04-05 06:53:22 +02:00
|
|
|
|
2010-07-14 09:38:35 +02:00
|
|
|
void session_impl::load_country_db(std::string file)
|
2008-12-21 09:48:06 +01:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-12-21 09:48:06 +01:00
|
|
|
if (m_country_db) GeoIP_delete(m_country_db);
|
2010-07-14 09:38:35 +02:00
|
|
|
m_country_db = GeoIP_open(file.c_str(), GEOIP_STANDARD);
|
|
|
|
// return m_country_db;
|
2008-12-21 09:48:06 +01:00
|
|
|
}
|
|
|
|
|
2009-10-29 19:12:43 +01:00
|
|
|
#endif // TORRENT_DISABLE_GEO_IP
|
2008-04-05 06:53:22 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2013-01-07 05:19:19 +01:00
|
|
|
|
|
|
|
typedef boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> ext_function_t;
|
|
|
|
|
|
|
|
struct session_plugin_wrapper : plugin
|
|
|
|
{
|
|
|
|
session_plugin_wrapper(ext_function_t const& f) : m_f(f) {}
|
|
|
|
|
|
|
|
virtual boost::shared_ptr<torrent_plugin> new_torrent(torrent* t, void* user)
|
|
|
|
{ return m_f(t, user); }
|
|
|
|
ext_function_t m_f;
|
|
|
|
};
|
|
|
|
|
|
|
|
void session_impl::add_extension(ext_function_t ext)
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2010-09-25 19:46:13 +02:00
|
|
|
TORRENT_ASSERT_VAL(ext, ext);
|
2008-09-22 01:19:58 +02:00
|
|
|
|
2013-01-07 05:19:19 +01:00
|
|
|
boost::shared_ptr<plugin> p(new session_plugin_wrapper(ext));
|
2008-09-22 01:19:58 +02:00
|
|
|
|
2013-01-07 05:19:19 +01:00
|
|
|
m_ses_extensions.push_back(p);
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
void session_impl::add_ses_extension(boost::shared_ptr<plugin> ext)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
TORRENT_ASSERT_VAL(ext, ext);
|
|
|
|
|
|
|
|
m_ses_extensions.push_back(ext);
|
|
|
|
m_alerts.add_extension(ext);
|
2014-02-21 05:30:59 +01:00
|
|
|
ext->added(this);
|
2011-01-29 11:37:21 +01:00
|
|
|
}
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-18 04:41:54 +01:00
|
|
|
feed_handle session_impl::add_feed(feed_settings const& sett)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
|
|
|
// look for duplicates. If we already have a feed with this
|
|
|
|
// URL, return a handle to the existing one
|
|
|
|
for (std::vector<boost::shared_ptr<feed> >::const_iterator i
|
|
|
|
= m_feeds.begin(), end(m_feeds.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (sett.url != (*i)->m_settings.url) continue;
|
|
|
|
return feed_handle(*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<feed> f(new_feed(*this, sett));
|
|
|
|
m_feeds.push_back(f);
|
|
|
|
update_rss_feeds();
|
|
|
|
return feed_handle(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::remove_feed(feed_handle h)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
|
|
|
boost::shared_ptr<feed> f = h.m_feed_ptr.lock();
|
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
std::vector<boost::shared_ptr<feed> >::iterator i
|
|
|
|
= std::find(m_feeds.begin(), m_feeds.end(), f);
|
|
|
|
|
|
|
|
if (i == m_feeds.end()) return;
|
|
|
|
|
|
|
|
m_feeds.erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::get_feeds(std::vector<feed_handle>* ret) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
|
|
|
ret->clear();
|
|
|
|
ret->reserve(m_feeds.size());
|
|
|
|
for (std::vector<boost::shared_ptr<feed> >::const_iterator i = m_feeds.begin()
|
|
|
|
, end(m_feeds.end()); i != end; ++i)
|
|
|
|
ret->push_back(feed_handle(*i));
|
|
|
|
}
|
|
|
|
|
2008-06-29 21:08:30 +02:00
|
|
|
void session_impl::pause()
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-06-29 21:08:30 +02:00
|
|
|
if (m_paused) return;
|
2008-12-30 09:20:25 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" *** session paused ***");
|
2008-12-30 09:20:25 +01:00
|
|
|
#endif
|
2008-06-29 21:08:30 +02:00
|
|
|
m_paused = true;
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent& t = *i->second;
|
2011-06-03 10:40:13 +02:00
|
|
|
t.do_pause();
|
2008-06-29 21:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::resume()
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-06-29 21:08:30 +02:00
|
|
|
if (!m_paused) return;
|
|
|
|
m_paused = false;
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent& t = *i->second;
|
|
|
|
t.do_resume();
|
2011-06-05 05:06:34 +02:00
|
|
|
if (t.should_check_files()) t.queue_torrent_check();
|
2008-06-29 21:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
void session_impl::abort()
|
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2007-10-07 20:06:56 +02:00
|
|
|
if (m_abort) return;
|
2008-12-30 09:20:25 +01:00
|
|
|
#if defined TORRENT_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" *** ABORT CALLED ***");
|
2007-10-07 20:06:56 +02:00
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
// abort the main thread
|
|
|
|
m_abort = true;
|
2009-09-16 05:46:36 +02:00
|
|
|
error_code ec;
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2009-09-16 05:46:36 +02:00
|
|
|
m_i2p_conn.close(ec);
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
2008-06-24 14:17:42 +02:00
|
|
|
m_queued_for_checking.clear();
|
2011-10-25 07:55:32 +02:00
|
|
|
stop_lsd();
|
|
|
|
stop_upnp();
|
|
|
|
stop_natpmp();
|
2007-10-07 20:06:56 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2012-06-25 05:31:11 +02:00
|
|
|
stop_dht();
|
2010-02-14 02:39:55 +01:00
|
|
|
m_dht_announce_timer.cancel(ec);
|
2007-10-07 20:06:56 +02:00
|
|
|
#endif
|
2008-04-07 03:22:26 +02:00
|
|
|
m_timer.cancel(ec);
|
2010-02-05 09:23:17 +01:00
|
|
|
m_lsd_announce_timer.cancel(ec);
|
2007-10-26 09:14:19 +02:00
|
|
|
|
2013-08-21 17:55:24 +02:00
|
|
|
for (std::set<boost::shared_ptr<socket_type> >::iterator i = m_incoming_sockets.begin()
|
|
|
|
, end(m_incoming_sockets.end()); i != end; ++i)
|
|
|
|
{
|
2013-09-14 12:26:55 +02:00
|
|
|
(*i)->close(ec);
|
|
|
|
TORRENT_ASSERT(!ec);
|
2013-08-21 17:55:24 +02:00
|
|
|
}
|
|
|
|
m_incoming_sockets.clear();
|
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
// close the listen sockets
|
|
|
|
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
|
|
|
|
, end(m_listen_sockets.end()); i != end; ++i)
|
|
|
|
{
|
2008-04-07 03:22:26 +02:00
|
|
|
i->sock->close(ec);
|
2010-12-24 19:15:01 +01:00
|
|
|
TORRENT_ASSERT(!ec);
|
2007-10-26 09:14:19 +02:00
|
|
|
}
|
2010-12-24 19:15:01 +01:00
|
|
|
m_listen_sockets.clear();
|
2012-07-15 04:30:13 +02:00
|
|
|
if (m_socks_listen_socket && m_socks_listen_socket->is_open())
|
2013-09-14 12:26:55 +02:00
|
|
|
{
|
|
|
|
m_socks_listen_socket->close(ec);
|
|
|
|
TORRENT_ASSERT(!ec);
|
|
|
|
}
|
2012-07-15 04:30:13 +02:00
|
|
|
m_socks_listen_socket.reset();
|
2012-07-31 18:53:37 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_I2P
|
2012-07-15 04:30:13 +02:00
|
|
|
if (m_i2p_listen_socket && m_i2p_listen_socket->is_open())
|
2013-09-14 12:26:55 +02:00
|
|
|
{
|
|
|
|
m_i2p_listen_socket->close(ec);
|
|
|
|
TORRENT_ASSERT(!ec);
|
|
|
|
}
|
2012-07-15 04:30:13 +02:00
|
|
|
m_i2p_listen_socket.reset();
|
2012-07-31 18:53:37 +02:00
|
|
|
#endif
|
2007-10-26 09:14:19 +02:00
|
|
|
|
2007-10-29 01:29:43 +01:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" aborting all torrents (%d)", m_torrents.size());
|
2007-10-29 01:29:43 +01:00
|
|
|
#endif
|
2007-10-07 20:06:56 +02:00
|
|
|
// abort all torrents
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
i->second->abort();
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2007-10-29 01:29:43 +01:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" aborting all tracker requests");
|
2007-10-29 01:29:43 +01:00
|
|
|
#endif
|
2007-11-02 01:27:53 +01:00
|
|
|
m_tracker_manager.abort_all_requests();
|
|
|
|
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" sending event=stopped to trackers");
|
2007-11-02 01:27:53 +01:00
|
|
|
#endif
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin();
|
|
|
|
i != m_torrents.end(); ++i)
|
2007-10-29 01:29:43 +01:00
|
|
|
{
|
2007-11-02 01:27:53 +01:00
|
|
|
torrent& t = *i->second;
|
2008-07-12 10:25:19 +02:00
|
|
|
t.abort();
|
2007-10-29 01:29:43 +01:00
|
|
|
}
|
2008-07-12 10:25:19 +02:00
|
|
|
|
2007-11-02 01:27:53 +01:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" aborting all connections (%d)", m_connections.size());
|
2007-11-02 01:27:53 +01:00
|
|
|
#endif
|
2012-02-20 08:51:36 +01:00
|
|
|
m_half_open.close();
|
2008-10-30 18:13:10 +01:00
|
|
|
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" connection queue: %d", m_half_open.size());
|
2008-10-30 18:13:10 +01:00
|
|
|
#endif
|
|
|
|
|
2007-11-02 01:27:53 +01:00
|
|
|
// abort all connections
|
|
|
|
while (!m_connections.empty())
|
|
|
|
{
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-11-02 01:27:53 +01:00
|
|
|
int conn = m_connections.size();
|
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
(*m_connections.begin())->disconnect(errors::stopping_torrent);
|
2010-09-25 19:46:13 +02:00
|
|
|
TORRENT_ASSERT_VAL(conn == int(m_connections.size()) + 1, conn);
|
2007-11-02 01:27:53 +01:00
|
|
|
}
|
2007-10-26 09:14:19 +02:00
|
|
|
|
2008-10-30 18:13:10 +01:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" connection queue: %d", m_half_open.size());
|
2008-10-30 18:13:10 +01:00
|
|
|
#endif
|
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" shutting down connection queue");
|
2007-10-26 09:14:19 +02:00
|
|
|
#endif
|
2007-10-08 01:20:38 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
m_download_rate.close();
|
|
|
|
m_upload_rate.close();
|
2010-05-30 03:33:03 +02:00
|
|
|
|
|
|
|
// #error closing the udp socket here means that
|
|
|
|
// the uTP connections cannot be closed gracefully
|
|
|
|
m_udp_socket.close();
|
|
|
|
m_external_udp_port = 0;
|
2010-07-14 06:16:38 +02:00
|
|
|
|
2013-12-05 08:42:32 +01:00
|
|
|
m_undead_peers.clear();
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
if (m_asnum_db) GeoIP_delete(m_asnum_db);
|
|
|
|
if (m_country_db) GeoIP_delete(m_country_db);
|
|
|
|
m_asnum_db = 0;
|
|
|
|
m_country_db = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_disk_thread.abort();
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2007-06-01 03:05:57 +02:00
|
|
|
void session_impl::set_port_filter(port_filter const& f)
|
|
|
|
{
|
|
|
|
m_port_filter = f;
|
2009-06-18 18:16:41 +02:00
|
|
|
// TODO: recalculate all connect candidates for all torrents
|
2007-06-01 03:05:57 +02:00
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
void session_impl::set_ip_filter(ip_filter const& f)
|
|
|
|
{
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
m_ip_filter = f;
|
|
|
|
|
|
|
|
// Close connections whose endpoint is filtered
|
|
|
|
// by the new ip-filter
|
2007-07-26 09:04:35 +02:00
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
i->second->ip_filter_updated();
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2009-07-21 03:52:37 +02:00
|
|
|
ip_filter const& session_impl::get_ip_filter() const
|
|
|
|
{
|
2009-07-21 06:42:19 +02:00
|
|
|
return m_ip_filter;
|
2009-07-21 03:52:37 +02:00
|
|
|
}
|
|
|
|
|
2010-03-10 08:14:10 +01:00
|
|
|
void session_impl::update_disk_thread_settings()
|
|
|
|
{
|
|
|
|
disk_io_job j;
|
2012-04-06 05:02:50 +02:00
|
|
|
j.buffer = (char*)new session_settings(m_settings);
|
2010-03-10 08:14:10 +01:00
|
|
|
j.action = disk_io_job::update_settings;
|
|
|
|
m_disk_thread.add_job(j);
|
|
|
|
}
|
|
|
|
|
2013-09-03 10:39:30 +02:00
|
|
|
template <class Socket>
|
|
|
|
void static set_socket_buffer_size(Socket& s, session_settings const& sett, error_code& ec)
|
|
|
|
{
|
|
|
|
if (sett.send_socket_buffer_size)
|
|
|
|
{
|
|
|
|
stream_socket::send_buffer_size prev_option;
|
|
|
|
s.get_option(prev_option, ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
stream_socket::send_buffer_size option(
|
|
|
|
sett.send_socket_buffer_size);
|
|
|
|
s.set_option(option, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
// restore previous value
|
|
|
|
s.set_option(prev_option, ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sett.recv_socket_buffer_size)
|
|
|
|
{
|
|
|
|
stream_socket::receive_buffer_size prev_option;
|
|
|
|
s.get_option(prev_option, ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
stream_socket::receive_buffer_size option(
|
|
|
|
sett.recv_socket_buffer_size);
|
|
|
|
s.set_option(option, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
// restore previous value
|
|
|
|
s.set_option(prev_option, ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
void session_impl::set_settings(session_settings const& s)
|
|
|
|
{
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2007-08-21 20:33:28 +02:00
|
|
|
|
2010-09-25 19:46:13 +02:00
|
|
|
TORRENT_ASSERT_VAL(s.file_pool_size > 0, s.file_pool_size);
|
2007-06-08 00:37:58 +02:00
|
|
|
|
|
|
|
// less than 5 seconds unchoke interval is insane
|
2010-09-25 19:46:13 +02:00
|
|
|
TORRENT_ASSERT_VAL(s.unchoke_interval >= 5, s.unchoke_interval);
|
2009-01-10 06:46:02 +01:00
|
|
|
|
|
|
|
// if disk io thread settings were changed
|
|
|
|
// post a notification to that thread
|
|
|
|
bool update_disk_io_thread = false;
|
|
|
|
if (m_settings.cache_size != s.cache_size
|
|
|
|
|| m_settings.cache_expiry != s.cache_expiry
|
2009-05-19 08:51:30 +02:00
|
|
|
|| m_settings.optimize_hashing_for_speed != s.optimize_hashing_for_speed
|
2009-05-22 08:32:39 +02:00
|
|
|
|| m_settings.file_checks_delay_per_block != s.file_checks_delay_per_block
|
2009-05-23 09:35:45 +02:00
|
|
|
|| m_settings.disk_cache_algorithm != s.disk_cache_algorithm
|
2009-05-23 21:27:27 +02:00
|
|
|
|| m_settings.read_cache_line_size != s.read_cache_line_size
|
2009-05-24 02:12:53 +02:00
|
|
|
|| m_settings.write_cache_line_size != s.write_cache_line_size
|
2009-05-25 09:46:31 +02:00
|
|
|
|| m_settings.coalesce_writes != s.coalesce_writes
|
|
|
|
|| m_settings.coalesce_reads != s.coalesce_reads
|
2009-06-10 10:30:55 +02:00
|
|
|
|| m_settings.max_queued_disk_bytes != s.max_queued_disk_bytes
|
2011-02-04 04:02:23 +01:00
|
|
|
|| m_settings.max_queued_disk_bytes_low_watermark != s.max_queued_disk_bytes_low_watermark
|
2009-08-02 08:40:45 +02:00
|
|
|
|| m_settings.disable_hash_checks != s.disable_hash_checks
|
2010-01-15 17:45:42 +01:00
|
|
|
|| m_settings.explicit_read_cache != s.explicit_read_cache
|
2009-02-06 10:46:13 +01:00
|
|
|
#ifndef TORRENT_DISABLE_MLOCK
|
|
|
|
|| m_settings.lock_disk_cache != s.lock_disk_cache
|
|
|
|
#endif
|
2010-01-23 04:02:32 +01:00
|
|
|
|| m_settings.use_read_cache != s.use_read_cache
|
2011-09-28 20:14:48 +02:00
|
|
|
|| m_settings.disk_io_write_mode != s.disk_io_write_mode
|
|
|
|
|| m_settings.disk_io_read_mode != s.disk_io_read_mode
|
2010-01-23 04:02:32 +01:00
|
|
|
|| m_settings.allow_reordered_disk_operations != s.allow_reordered_disk_operations
|
|
|
|
|| m_settings.file_pool_size != s.file_pool_size
|
2010-01-30 04:50:17 +01:00
|
|
|
|| m_settings.volatile_read_cache != s.volatile_read_cache
|
2010-02-02 20:44:52 +01:00
|
|
|
|| m_settings.no_atime_storage!= s.no_atime_storage
|
2010-06-25 16:06:28 +02:00
|
|
|
|| m_settings.ignore_resume_timestamps != s.ignore_resume_timestamps
|
2011-02-06 01:50:12 +01:00
|
|
|
|| m_settings.no_recheck_incomplete_resume != s.no_recheck_incomplete_resume
|
2011-07-10 03:36:18 +02:00
|
|
|
|| m_settings.low_prio_disk != s.low_prio_disk
|
2013-03-21 02:45:12 +01:00
|
|
|
|| m_settings.lock_files != s.lock_files
|
|
|
|
|| m_settings.use_disk_cache_pool != s.use_disk_cache_pool)
|
2009-01-10 06:46:02 +01:00
|
|
|
update_disk_io_thread = true;
|
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
bool connections_limit_changed = m_settings.connections_limit != s.connections_limit;
|
|
|
|
bool unchoke_limit_changed = m_settings.unchoke_slots_limit != s.unchoke_slots_limit;
|
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
// support deprecated choker settings
|
|
|
|
if (s.choking_algorithm == session_settings::rate_based_choker)
|
|
|
|
{
|
|
|
|
if (s.auto_upload_slots && !s.auto_upload_slots_rate_based)
|
|
|
|
m_settings.choking_algorithm = session_settings::auto_expand_choker;
|
|
|
|
else if (!s.auto_upload_slots)
|
|
|
|
m_settings.choking_algorithm = session_settings::fixed_slots_choker;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-31 20:16:22 +01:00
|
|
|
// safety check
|
2010-01-31 17:35:38 +01:00
|
|
|
if (m_settings.volatile_read_cache
|
2010-01-31 20:16:22 +01:00
|
|
|
&& (m_settings.suggest_mode == session_settings::suggest_read_cache
|
|
|
|
|| m_settings.explicit_read_cache))
|
2010-01-31 17:35:38 +01:00
|
|
|
{
|
|
|
|
// If you hit this assert, you're trying to set your cache to be
|
2010-01-31 20:16:22 +01:00
|
|
|
// volatile and to suggest pieces out of it (or to make the cache
|
|
|
|
// explicit) at the same time this is a bad configuration, don't do it
|
2014-03-23 08:40:43 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(false);
|
2010-01-31 17:35:38 +01:00
|
|
|
m_settings.volatile_read_cache = false;
|
|
|
|
}
|
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
if (m_settings.choking_algorithm != s.choking_algorithm)
|
|
|
|
{
|
|
|
|
// trigger recalculation of the unchoked peers
|
|
|
|
m_unchoke_time_scaler = 0;
|
|
|
|
}
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (m_settings.dht_announce_interval != s.dht_announce_interval)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_dht_announce");
|
|
|
|
#endif
|
2010-02-14 02:39:55 +01:00
|
|
|
error_code ec;
|
|
|
|
int delay = (std::max)(s.dht_announce_interval
|
|
|
|
/ (std::max)(int(m_torrents.size()), 1), 1);
|
|
|
|
m_dht_announce_timer.expires_from_now(seconds(delay), ec);
|
|
|
|
m_dht_announce_timer.async_wait(
|
2010-04-30 21:08:16 +02:00
|
|
|
boost::bind(&session_impl::on_dht_announce, this, _1));
|
2010-02-14 02:39:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-13 17:29:17 +01:00
|
|
|
if (m_settings.local_service_announce_interval != s.local_service_announce_interval)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_lsd_announce");
|
|
|
|
#endif
|
2010-02-13 17:29:17 +01:00
|
|
|
error_code ec;
|
|
|
|
int delay = (std::max)(s.local_service_announce_interval
|
|
|
|
/ (std::max)(int(m_torrents.size()), 1), 1);
|
|
|
|
m_lsd_announce_timer.expires_from_now(seconds(delay), ec);
|
|
|
|
m_lsd_announce_timer.async_wait(
|
2010-04-30 21:08:16 +02:00
|
|
|
boost::bind(&session_impl::on_lsd_announce, this, _1));
|
2010-02-13 17:29:17 +01:00
|
|
|
}
|
|
|
|
|
2008-06-12 08:58:42 +02:00
|
|
|
// if queuing settings were changed, recalculate
|
|
|
|
// queued torrents sooner
|
|
|
|
if ((m_settings.active_downloads != s.active_downloads
|
2008-06-23 00:49:18 +02:00
|
|
|
|| m_settings.active_seeds != s.active_seeds
|
2011-12-23 21:02:59 +01:00
|
|
|
|| m_settings.active_limit != s.active_limit))
|
2008-06-12 08:58:42 +02:00
|
|
|
m_auto_manage_time_scaler = 2;
|
2010-04-13 06:30:34 +02:00
|
|
|
|
2010-07-15 08:27:44 +02:00
|
|
|
if (m_settings.report_web_seed_downloads != s.report_web_seed_downloads)
|
|
|
|
{
|
|
|
|
// if this flag changed, update all web seed connections
|
|
|
|
for (connection_map::iterator i = m_connections.begin()
|
|
|
|
, end(m_connections.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
int type = (*i)->type();
|
|
|
|
if (type == peer_connection::url_seed_connection
|
|
|
|
|| type == peer_connection::http_seed_connection)
|
|
|
|
(*i)->ignore_stats(!s.report_web_seed_downloads);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-23 19:00:52 +01:00
|
|
|
if (m_settings.alert_queue_size != s.alert_queue_size)
|
|
|
|
m_alerts.set_alert_queue_size_limit(s.alert_queue_size);
|
|
|
|
|
2011-03-14 08:47:24 +01:00
|
|
|
if (m_settings.dht_upload_rate_limit != s.dht_upload_rate_limit)
|
|
|
|
m_udp_socket.set_rate_limit(s.dht_upload_rate_limit);
|
|
|
|
|
2014-06-15 19:44:27 +02:00
|
|
|
if (m_settings.peer_tos != s.peer_tos && s.peer_tos != 0)
|
2012-01-29 21:59:20 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
m_udp_socket.set_option(type_of_service(s.peer_tos), ec);
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << ">>> SET_TOS[ udp_socket tos: " << s.peer_tos << " e: " << ec.message() << " ]\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-02 11:24:34 +02:00
|
|
|
{
|
|
|
|
error_code ec;
|
2013-09-03 10:39:30 +02:00
|
|
|
set_socket_buffer_size(m_udp_socket, m_settings, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
if (m_alerts.should_post<udp_error_alert>())
|
|
|
|
m_alerts.post_alert(udp_error_alert(udp::endpoint(), ec));
|
|
|
|
}
|
2013-09-02 11:24:34 +02:00
|
|
|
}
|
|
|
|
|
2013-08-18 00:29:34 +02:00
|
|
|
bool reopen_listen_port = false;
|
|
|
|
if (m_settings.ssl_listen != s.ssl_listen)
|
|
|
|
reopen_listen_port = true;
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
m_settings = s;
|
2010-04-13 06:30:34 +02:00
|
|
|
|
2011-03-20 05:56:20 +01:00
|
|
|
if (m_settings.cache_buffer_chunk_size <= 0)
|
|
|
|
m_settings.cache_buffer_chunk_size = 1;
|
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
update_rate_settings();
|
|
|
|
|
|
|
|
if (connections_limit_changed) update_connections_limit();
|
|
|
|
if (unchoke_limit_changed) update_unchoke_limit();
|
|
|
|
|
2013-02-19 09:14:16 +01:00
|
|
|
bool anonymous_mode = (m_settings.anonymous_mode != s.anonymous_mode && s.anonymous_mode);
|
|
|
|
if (anonymous_mode)
|
|
|
|
{
|
|
|
|
m_settings.user_agent.clear();
|
|
|
|
url_random((char*)&m_peer_id[0], (char*)&m_peer_id[0] + 20);
|
|
|
|
}
|
|
|
|
|
2013-02-19 07:48:53 +01:00
|
|
|
bool force_proxy = (m_settings.force_proxy != s.force_proxy && s.force_proxy);
|
|
|
|
|
|
|
|
m_udp_socket.set_force_proxy(s.force_proxy);
|
|
|
|
|
|
|
|
// in force_proxy mode, we don't want to accept any incoming
|
2010-04-13 06:30:34 +02:00
|
|
|
// connections, except through a proxy.
|
2013-02-19 07:48:53 +01:00
|
|
|
if (force_proxy)
|
2010-04-13 06:30:34 +02:00
|
|
|
{
|
|
|
|
stop_lsd();
|
|
|
|
stop_upnp();
|
|
|
|
stop_natpmp();
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
stop_dht();
|
|
|
|
#endif
|
|
|
|
// close the listen sockets
|
|
|
|
error_code ec;
|
|
|
|
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
|
|
|
|
, end(m_listen_sockets.end()); i != end; ++i)
|
|
|
|
i->sock->close(ec);
|
|
|
|
m_listen_sockets.clear();
|
|
|
|
}
|
2010-02-06 22:40:55 +01:00
|
|
|
if (m_settings.connection_speed < 0) m_settings.connection_speed = 200;
|
2008-02-11 04:55:53 +01:00
|
|
|
|
2009-01-10 06:46:02 +01:00
|
|
|
if (update_disk_io_thread)
|
2010-03-10 08:14:10 +01:00
|
|
|
update_disk_thread_settings();
|
2009-01-10 06:46:02 +01:00
|
|
|
|
2010-08-27 16:52:42 +02:00
|
|
|
if (m_settings.num_optimistic_unchoke_slots >= m_allowed_upload_slots / 2)
|
2010-02-06 08:39:45 +01:00
|
|
|
{
|
|
|
|
if (m_alerts.should_post<performance_alert>())
|
|
|
|
m_alerts.post_alert(performance_alert(torrent_handle()
|
|
|
|
, performance_alert::too_many_optimistic_unchoke_slots));
|
|
|
|
}
|
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
if (s.choking_algorithm == session_settings::fixed_slots_choker)
|
2010-10-09 21:09:38 +02:00
|
|
|
m_allowed_upload_slots = m_settings.unchoke_slots_limit;
|
2010-02-09 04:04:41 +01:00
|
|
|
else if (s.choking_algorithm == session_settings::auto_expand_choker
|
2010-10-09 21:09:38 +02:00
|
|
|
&& m_allowed_upload_slots < m_settings.unchoke_slots_limit)
|
|
|
|
m_allowed_upload_slots = m_settings.unchoke_slots_limit;
|
2012-08-29 03:58:06 +02:00
|
|
|
if (m_allowed_upload_slots < 0)
|
|
|
|
m_allowed_upload_slots = (std::numeric_limits<int>::max)();
|
2010-02-09 04:04:41 +01:00
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
// replace all occurances of '\n' with ' '.
|
|
|
|
std::string::iterator i = m_settings.user_agent.begin();
|
|
|
|
while ((i = std::find(i, m_settings.user_agent.end(), '\n'))
|
|
|
|
!= m_settings.user_agent.end())
|
|
|
|
*i = ' ';
|
2013-08-18 00:29:34 +02:00
|
|
|
|
|
|
|
if (reopen_listen_port)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
open_listen_port(0, ec);
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2007-09-22 18:27:29 +02:00
|
|
|
tcp::endpoint session_impl::get_ipv6_interface() const
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2007-09-22 18:27:29 +02:00
|
|
|
return m_ipv6_interface;
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2009-04-12 02:37:06 +02:00
|
|
|
tcp::endpoint session_impl::get_ipv4_interface() const
|
|
|
|
{
|
|
|
|
return m_ipv4_interface;
|
|
|
|
}
|
|
|
|
|
2011-09-12 05:51:49 +02:00
|
|
|
void session_impl::setup_listener(listen_socket_t* s, tcp::endpoint ep
|
2012-10-08 01:34:44 +02:00
|
|
|
, int& retries, bool v6_only, int flags, error_code& ec)
|
2007-09-22 18:27:29 +02:00
|
|
|
{
|
2013-06-04 02:35:42 +02:00
|
|
|
int last_op = 0;
|
2013-10-06 08:32:33 +02:00
|
|
|
listen_failed_alert::socket_type_t sock_type = s->ssl ? listen_failed_alert::tcp_ssl : listen_failed_alert::tcp;
|
2011-09-12 05:51:49 +02:00
|
|
|
s->sock.reset(new socket_acceptor(m_io_service));
|
|
|
|
s->sock->open(ep.protocol(), ec);
|
2013-06-04 02:35:42 +02:00
|
|
|
last_op = listen_failed_alert::open;
|
2010-08-25 08:22:49 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
2013-06-04 02:35:42 +02:00
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
2013-10-06 08:32:33 +02:00
|
|
|
m_alerts.post_alert(listen_failed_alert(ep, last_op, ec, sock_type));
|
2011-02-16 07:35:53 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("failed to open socket: %s: %s"
|
|
|
|
, print_endpoint(ep).c_str(), ec.message().c_str());
|
2010-08-25 08:22:49 +02:00
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
return;
|
2011-02-16 07:35:53 +01:00
|
|
|
}
|
2012-06-28 08:51:18 +02:00
|
|
|
|
2013-08-02 03:25:52 +02:00
|
|
|
// SO_REUSEADDR on windows is a bit special. It actually allows
|
|
|
|
// two active sockets to bind to the same port. That means we
|
|
|
|
// may end up binding to the same socket as some other random
|
|
|
|
// application. Don't do it!
|
|
|
|
#ifndef TORRENT_WINDOWS
|
2012-06-28 08:51:18 +02:00
|
|
|
error_code err; // ignore errors here
|
|
|
|
s->sock->set_option(socket_acceptor::reuse_address(true), err);
|
2013-08-02 03:25:52 +02:00
|
|
|
#endif
|
2012-06-28 08:51:18 +02:00
|
|
|
|
2009-04-04 18:59:53 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2009-01-21 02:39:13 +01:00
|
|
|
if (ep.protocol() == tcp::v6())
|
|
|
|
{
|
2011-02-16 07:35:53 +01:00
|
|
|
error_code err; // ignore errors here
|
2012-11-19 05:58:46 +01:00
|
|
|
#ifdef IPV6_V6ONLY
|
2011-09-12 05:51:49 +02:00
|
|
|
s->sock->set_option(v6only(v6_only), err);
|
2012-11-19 05:58:46 +01:00
|
|
|
#endif
|
2009-01-21 02:39:13 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2010-02-14 05:05:18 +01:00
|
|
|
|
|
|
|
#ifndef PROTECTION_LEVEL_UNRESTRICTED
|
|
|
|
#define PROTECTION_LEVEL_UNRESTRICTED 10
|
|
|
|
#endif
|
2009-01-21 02:39:13 +01:00
|
|
|
// enable Teredo on windows
|
2011-09-12 05:51:49 +02:00
|
|
|
s->sock->set_option(v6_protection_level(PROTECTION_LEVEL_UNRESTRICTED), err);
|
2009-01-21 02:39:13 +01:00
|
|
|
#endif
|
|
|
|
}
|
2009-04-04 18:59:53 +02:00
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
s->sock->bind(ep, ec);
|
2007-09-22 18:27:29 +02:00
|
|
|
while (ec && retries > 0)
|
|
|
|
{
|
2010-08-25 08:22:49 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("failed to bind to interface \"%s\": %s"
|
2010-08-25 08:22:49 +02:00
|
|
|
, print_endpoint(ep).c_str(), ec.message().c_str());
|
|
|
|
#endif
|
2011-02-16 07:35:53 +01:00
|
|
|
ec.clear();
|
2010-09-25 19:46:13 +02:00
|
|
|
TORRENT_ASSERT_VAL(!ec, ec);
|
2007-09-22 18:27:29 +02:00
|
|
|
--retries;
|
|
|
|
ep.port(ep.port() + 1);
|
2011-09-12 05:51:49 +02:00
|
|
|
s->sock->bind(ep, ec);
|
2013-06-04 02:35:42 +02:00
|
|
|
last_op = listen_failed_alert::bind;
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
2011-02-16 07:35:53 +01:00
|
|
|
if (ec && !(flags & session::listen_no_system_port))
|
2007-09-22 18:27:29 +02:00
|
|
|
{
|
2011-02-16 07:35:53 +01:00
|
|
|
// instead of giving up, trying
|
2007-09-22 18:27:29 +02:00
|
|
|
// let the OS pick a port
|
|
|
|
ep.port(0);
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code();
|
2011-09-12 05:51:49 +02:00
|
|
|
s->sock->bind(ep, ec);
|
2013-06-04 02:35:42 +02:00
|
|
|
last_op = listen_failed_alert::bind;
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
// not even that worked, give up
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
2013-10-06 08:32:33 +02:00
|
|
|
m_alerts.post_alert(listen_failed_alert(ep, last_op, ec, sock_type));
|
2010-05-30 03:33:03 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("cannot bind to interface \"%s\": %s"
|
2009-04-04 11:52:25 +02:00
|
|
|
, print_endpoint(ep).c_str(), ec.message().c_str());
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
return;
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
2011-09-12 05:51:49 +02:00
|
|
|
s->external_port = s->sock->local_endpoint(ec).port();
|
2013-08-18 00:29:34 +02:00
|
|
|
TORRENT_ASSERT(s->external_port == ep.port() || ep.port() == 0);
|
2013-06-04 02:35:42 +02:00
|
|
|
last_op = listen_failed_alert::get_peer_name;
|
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
s->sock->listen(m_settings.listen_queue_size, ec);
|
|
|
|
last_op = listen_failed_alert::listen;
|
|
|
|
}
|
2007-09-22 18:27:29 +02:00
|
|
|
if (ec)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
2013-10-06 08:32:33 +02:00
|
|
|
m_alerts.post_alert(listen_failed_alert(ep, last_op, ec, sock_type));
|
2010-05-30 03:33:03 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("cannot listen on interface \"%s\": %s"
|
2009-04-04 11:52:25 +02:00
|
|
|
, print_endpoint(ep).c_str(), ec.message().c_str());
|
2007-09-22 18:27:29 +02:00
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
return;
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:30 +01:00
|
|
|
// if we asked the system to listen on port 0, which
|
|
|
|
// socket did it end up choosing?
|
|
|
|
if (ep.port() == 0)
|
2013-06-04 02:35:42 +02:00
|
|
|
{
|
2012-02-20 05:13:39 +01:00
|
|
|
ep.port(s->sock->local_endpoint(ec).port());
|
2013-06-04 02:35:42 +02:00
|
|
|
last_op = listen_failed_alert::get_peer_name;
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
2013-10-06 08:32:33 +02:00
|
|
|
m_alerts.post_alert(listen_failed_alert(ep, last_op, ec, sock_type));
|
2013-06-04 02:35:42 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
char msg[200];
|
|
|
|
snprintf(msg, 200, "failed to get peer name \"%s\": %s"
|
|
|
|
, print_endpoint(ep).c_str(), ec.message().c_str());
|
|
|
|
(*m_logger) << time_now_string() << msg << "\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2011-03-07 08:02:30 +01:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<listen_succeeded_alert>())
|
2013-10-06 08:32:33 +02:00
|
|
|
m_alerts.post_alert(listen_succeeded_alert(ep, s->ssl ? listen_succeeded_alert::tcp_ssl : listen_succeeded_alert::tcp));
|
2007-09-22 18:27:29 +02:00
|
|
|
|
2010-05-30 03:33:03 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" listening on: %s external port: %d"
|
|
|
|
, print_endpoint(ep).c_str(), s->external_port);
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
|
|
|
|
2011-02-16 07:35:53 +01:00
|
|
|
void session_impl::open_listen_port(int flags, error_code& ec)
|
2007-09-22 18:27:29 +02:00
|
|
|
{
|
2010-09-25 22:07:27 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2012-10-15 08:20:42 +02:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2012-10-08 01:34:44 +02:00
|
|
|
retry:
|
|
|
|
|
2007-09-22 18:27:29 +02:00
|
|
|
// close the open listen sockets
|
2012-10-15 08:20:42 +02:00
|
|
|
// close the listen sockets
|
|
|
|
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
|
|
|
|
, end(m_listen_sockets.end()); i != end; ++i)
|
|
|
|
i->sock->close(ec);
|
2007-09-22 18:27:29 +02:00
|
|
|
m_listen_sockets.clear();
|
|
|
|
m_incoming_connection = false;
|
2012-10-15 08:20:42 +02:00
|
|
|
ec.clear();
|
|
|
|
|
|
|
|
if (m_abort) return;
|
2007-09-22 18:27:29 +02:00
|
|
|
|
2009-04-12 02:37:06 +02:00
|
|
|
m_ipv6_interface = tcp::endpoint();
|
|
|
|
m_ipv4_interface = tcp::endpoint();
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
tcp::endpoint ssl_interface = m_listen_interface;
|
|
|
|
ssl_interface.port(m_settings.ssl_listen);
|
|
|
|
#endif
|
|
|
|
|
2007-09-22 18:27:29 +02:00
|
|
|
if (is_any(m_listen_interface.address()))
|
|
|
|
{
|
|
|
|
// this means we should open two listen sockets
|
|
|
|
// one for IPv4 and one for IPv6
|
|
|
|
|
2011-09-12 05:51:49 +02:00
|
|
|
listen_socket_t s;
|
|
|
|
setup_listener(&s, tcp::endpoint(address_v4::any(), m_listen_interface.port())
|
2011-02-16 07:35:53 +01:00
|
|
|
, m_listen_port_retries, false, flags, ec);
|
2007-09-22 18:27:29 +02:00
|
|
|
|
|
|
|
if (s.sock)
|
|
|
|
{
|
2010-08-25 08:22:49 +02:00
|
|
|
// update the listen_interface member with the
|
2010-05-30 03:33:03 +02:00
|
|
|
// actual port we ended up listening on, so that the other
|
|
|
|
// sockets can be bound to the same one
|
2012-01-14 17:04:25 +01:00
|
|
|
m_listen_interface.port(s.external_port);
|
2010-05-30 03:33:03 +02:00
|
|
|
|
2012-10-15 08:20:42 +02:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2007-09-22 18:27:29 +02:00
|
|
|
m_listen_sockets.push_back(s);
|
2012-01-14 17:04:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (m_settings.ssl_listen)
|
|
|
|
{
|
|
|
|
listen_socket_t s;
|
|
|
|
s.ssl = true;
|
2012-10-10 07:52:08 +02:00
|
|
|
int retries = 10;
|
|
|
|
setup_listener(&s, ssl_interface, retries, false, flags, ec);
|
2012-01-14 17:04:25 +01:00
|
|
|
|
|
|
|
if (s.sock)
|
|
|
|
{
|
2012-10-15 08:20:42 +02:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2012-01-14 17:04:25 +01:00
|
|
|
m_listen_sockets.push_back(s);
|
|
|
|
}
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
2012-01-14 17:04:25 +01:00
|
|
|
#endif
|
2007-09-22 18:27:29 +02:00
|
|
|
|
2009-03-31 09:45:54 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2009-03-27 19:44:40 +01:00
|
|
|
// only try to open the IPv6 port if IPv6 is installed
|
2009-04-13 07:11:44 +02:00
|
|
|
if (supports_ipv6())
|
2007-09-22 18:27:29 +02:00
|
|
|
{
|
2011-09-12 05:51:49 +02:00
|
|
|
setup_listener(&s, tcp::endpoint(address_v6::any(), m_listen_interface.port())
|
2011-02-16 07:35:53 +01:00
|
|
|
, m_listen_port_retries, true, flags, ec);
|
2009-04-13 07:11:44 +02:00
|
|
|
|
|
|
|
if (s.sock)
|
|
|
|
{
|
2012-10-15 08:20:42 +02:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2009-04-13 07:11:44 +02:00
|
|
|
m_listen_sockets.push_back(s);
|
|
|
|
}
|
2012-01-14 17:04:25 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (m_settings.ssl_listen)
|
|
|
|
{
|
|
|
|
listen_socket_t s;
|
|
|
|
s.ssl = true;
|
2012-10-10 07:52:08 +02:00
|
|
|
int retries = 10;
|
2012-01-14 17:04:25 +01:00
|
|
|
setup_listener(&s, tcp::endpoint(address_v6::any(), ssl_interface.port())
|
2012-10-10 07:52:08 +02:00
|
|
|
, retries, false, flags, ec);
|
2012-01-14 17:04:25 +01:00
|
|
|
|
|
|
|
if (s.sock)
|
|
|
|
{
|
2012-10-15 08:20:42 +02:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2012-01-14 17:04:25 +01:00
|
|
|
m_listen_sockets.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // TORRENT_USE_OPENSSL
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
2009-03-31 09:45:54 +02:00
|
|
|
#endif // TORRENT_USE_IPV6
|
2009-04-12 02:37:06 +02:00
|
|
|
|
|
|
|
// set our main IPv4 and IPv6 interfaces
|
|
|
|
// used to send to the tracker
|
|
|
|
std::vector<ip_interface> ifs = enum_net_interfaces(m_io_service, ec);
|
|
|
|
for (std::vector<ip_interface>::const_iterator i = ifs.begin()
|
|
|
|
, end(ifs.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
address const& addr = i->interface_address;
|
|
|
|
if (addr.is_v6() && !is_local(addr) && !is_loopback(addr))
|
|
|
|
m_ipv6_interface = tcp::endpoint(addr, m_listen_interface.port());
|
|
|
|
else if (addr.is_v4() && !is_local(addr) && !is_loopback(addr))
|
|
|
|
m_ipv4_interface = tcp::endpoint(addr, m_listen_interface.port());
|
|
|
|
}
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we should only open a single listen socket, that
|
|
|
|
// binds to the given interface
|
|
|
|
|
2011-09-12 05:51:49 +02:00
|
|
|
listen_socket_t s;
|
|
|
|
setup_listener(&s, m_listen_interface, m_listen_port_retries, false, flags, ec);
|
2007-09-22 18:27:29 +02:00
|
|
|
|
|
|
|
if (s.sock)
|
|
|
|
{
|
2012-10-15 08:20:42 +02:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2007-09-22 18:27:29 +02:00
|
|
|
m_listen_sockets.push_back(s);
|
2009-04-12 02:37:06 +02:00
|
|
|
|
|
|
|
if (m_listen_interface.address().is_v6())
|
|
|
|
m_ipv6_interface = m_listen_interface;
|
|
|
|
else
|
|
|
|
m_ipv4_interface = m_listen_interface;
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
2012-01-14 17:04:25 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (m_settings.ssl_listen)
|
|
|
|
{
|
|
|
|
listen_socket_t s;
|
|
|
|
s.ssl = true;
|
2012-10-10 07:52:08 +02:00
|
|
|
int retries = 10;
|
|
|
|
setup_listener(&s, ssl_interface, retries, false, flags, ec);
|
2012-01-14 17:04:25 +01:00
|
|
|
|
|
|
|
if (s.sock)
|
|
|
|
{
|
2012-10-15 08:20:42 +02:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2012-01-14 17:04:25 +01:00
|
|
|
m_listen_sockets.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-30 03:33:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_udp_socket.bind(udp::endpoint(m_listen_interface.address(), m_listen_interface.port()), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("cannot bind to UDP interface \"%s\": %s"
|
2010-05-30 03:33:03 +02:00
|
|
|
, print_endpoint(m_listen_interface).c_str(), ec.message().c_str());
|
|
|
|
#endif
|
2012-10-08 01:34:44 +02:00
|
|
|
if (m_listen_port_retries > 0)
|
|
|
|
{
|
|
|
|
m_listen_interface.port(m_listen_interface.port() + 1);
|
2012-10-18 09:42:15 +02:00
|
|
|
--m_listen_port_retries;
|
2012-10-08 01:34:44 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
2013-06-04 02:35:42 +02:00
|
|
|
m_alerts.post_alert(listen_failed_alert(m_listen_interface
|
2013-10-06 08:32:33 +02:00
|
|
|
, listen_failed_alert::bind, ec, listen_failed_alert::udp));
|
2010-05-30 03:33:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_external_udp_port = m_udp_socket.local_port();
|
|
|
|
maybe_update_udp_mapping(0, m_listen_interface.port(), m_listen_interface.port());
|
|
|
|
maybe_update_udp_mapping(1, m_listen_interface.port(), m_listen_interface.port());
|
2013-10-06 08:32:33 +02:00
|
|
|
if (m_alerts.should_post<listen_succeeded_alert>())
|
|
|
|
m_alerts.post_alert(listen_succeeded_alert(m_listen_interface, listen_succeeded_alert::udp));
|
2007-09-22 18:27:29 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 19:44:27 +02:00
|
|
|
if (m_settings.peer_tos != 0) {
|
|
|
|
m_udp_socket.set_option(type_of_service(m_settings.peer_tos), ec);
|
2012-01-29 21:59:20 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2014-06-15 19:44:27 +02:00
|
|
|
(*m_logger) << ">>> SET_TOS[ udp_socket tos: " << m_settings.peer_tos << " e: " << ec.message() << " ]\n";
|
2012-01-29 21:59:20 +01:00
|
|
|
#endif
|
2014-06-15 19:44:27 +02:00
|
|
|
}
|
2012-01-29 21:59:20 +01:00
|
|
|
ec.clear();
|
2013-09-03 10:39:30 +02:00
|
|
|
|
|
|
|
set_socket_buffer_size(m_udp_socket, m_settings, ec);
|
|
|
|
if (ec)
|
2013-09-02 11:24:34 +02:00
|
|
|
{
|
2013-09-03 10:39:30 +02:00
|
|
|
if (m_alerts.should_post<udp_error_alert>())
|
|
|
|
m_alerts.post_alert(udp_error_alert(udp::endpoint(), ec));
|
2013-09-02 11:24:34 +02:00
|
|
|
}
|
2012-01-29 21:59:20 +01:00
|
|
|
|
2012-10-18 09:42:15 +02:00
|
|
|
// initiate accepting on the listen sockets
|
|
|
|
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
|
|
|
|
, end(m_listen_sockets.end()); i != end; ++i)
|
|
|
|
async_accept(i->sock, i->ssl);
|
|
|
|
|
2009-04-09 03:04:49 +02:00
|
|
|
open_new_incoming_socks_connection();
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
open_new_incoming_i2p_connection();
|
|
|
|
#endif
|
2009-04-09 03:04:49 +02:00
|
|
|
|
2007-09-22 18:27:29 +02:00
|
|
|
if (!m_listen_sockets.empty())
|
|
|
|
{
|
|
|
|
tcp::endpoint local = m_listen_sockets.front().sock->local_endpoint(ec);
|
2012-01-14 17:04:25 +01:00
|
|
|
if (!ec) remap_tcp_ports(3, local.port(), ssl_listen_port());
|
2007-09-10 21:10:38 +02:00
|
|
|
}
|
2008-08-25 23:17:24 +02:00
|
|
|
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
m_logger = create_log("main_session", listen_port(), false);
|
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
void session_impl::remap_tcp_ports(boost::uint32_t mask, int tcp_port, int ssl_port)
|
|
|
|
{
|
|
|
|
if ((mask & 1) && m_natpmp.get())
|
|
|
|
{
|
|
|
|
if (m_tcp_mapping[0] != -1) m_natpmp->delete_mapping(m_tcp_mapping[0]);
|
|
|
|
m_tcp_mapping[0] = m_natpmp->add_mapping(natpmp::tcp, tcp_port, tcp_port);
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (m_ssl_mapping[0] != -1) m_natpmp->delete_mapping(m_ssl_mapping[0]);
|
2014-06-15 19:30:40 +02:00
|
|
|
if (ssl_port > 0) m_ssl_mapping[0] = m_natpmp->add_mapping(natpmp::tcp
|
|
|
|
, ssl_port, ssl_port);
|
2012-01-14 17:04:25 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if ((mask & 2) && m_upnp.get())
|
|
|
|
{
|
|
|
|
if (m_tcp_mapping[1] != -1) m_upnp->delete_mapping(m_tcp_mapping[1]);
|
|
|
|
m_tcp_mapping[1] = m_upnp->add_mapping(upnp::tcp, tcp_port, tcp_port);
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (m_ssl_mapping[1] != -1) m_upnp->delete_mapping(m_ssl_mapping[1]);
|
2014-06-15 19:30:40 +02:00
|
|
|
if (ssl_port > 0) m_ssl_mapping[1] = m_upnp->add_mapping(upnp::tcp
|
|
|
|
, ssl_port, ssl_port);
|
2012-01-14 17:04:25 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-09 03:04:49 +02:00
|
|
|
void session_impl::open_new_incoming_socks_connection()
|
|
|
|
{
|
2010-08-23 08:27:18 +02:00
|
|
|
if (m_proxy.type != proxy_settings::socks5
|
|
|
|
&& m_proxy.type != proxy_settings::socks5_pw
|
|
|
|
&& m_proxy.type != proxy_settings::socks4)
|
2009-04-09 03:04:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_socks_listen_socket) return;
|
|
|
|
|
2009-08-18 20:47:44 +02:00
|
|
|
m_socks_listen_socket = boost::shared_ptr<socket_type>(new socket_type(m_io_service));
|
2010-08-23 08:27:18 +02:00
|
|
|
bool ret = instantiate_connection(m_io_service, m_proxy
|
2009-04-09 03:04:49 +02:00
|
|
|
, *m_socks_listen_socket);
|
2010-09-25 19:46:13 +02:00
|
|
|
TORRENT_ASSERT_VAL(ret, ret);
|
2009-04-09 03:04:49 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_socks_accept");
|
|
|
|
#endif
|
2009-04-09 03:04:49 +02:00
|
|
|
socks5_stream& s = *m_socks_listen_socket->get<socks5_stream>();
|
|
|
|
s.set_command(2); // 2 means BIND (as opposed to CONNECT)
|
2010-04-13 06:30:34 +02:00
|
|
|
m_socks_listen_port = m_listen_interface.port();
|
2011-02-26 08:55:51 +01:00
|
|
|
if (m_socks_listen_port == 0) m_socks_listen_port = 2000 + random() % 60000;
|
2010-04-13 06:30:34 +02:00
|
|
|
s.async_connect(tcp::endpoint(address_v4::any(), m_socks_listen_port)
|
2009-04-09 03:04:49 +02:00
|
|
|
, boost::bind(&session_impl::on_socks_accept, this, m_socks_listen_socket, _1));
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2013-10-27 20:56:37 +01:00
|
|
|
void session_impl::set_i2p_proxy(proxy_settings const& s)
|
|
|
|
{
|
|
|
|
// we need this socket to be open before we
|
|
|
|
// can make name lookups for trackers for instance.
|
|
|
|
// pause the session now and resume it once we've
|
|
|
|
// established the i2p SAM connection
|
|
|
|
m_i2p_conn.open(s, boost::bind(&session_impl::on_i2p_open, this, _1));
|
|
|
|
open_new_incoming_i2p_connection();
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
void session_impl::on_i2p_open(error_code const& ec)
|
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2014-03-15 23:20:19 +01:00
|
|
|
if (m_alerts.should_post<i2p_alert>())
|
|
|
|
m_alerts.post_alert(i2p_alert(ec));
|
|
|
|
|
2013-10-27 20:56:37 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
char msg[200];
|
|
|
|
snprintf(msg, sizeof(msg), "i2p open failed (%d) %s", ec.value(), ec.message().c_str());
|
|
|
|
(*m_logger) << msg << "\n";
|
|
|
|
#endif
|
|
|
|
}
|
2013-10-28 03:41:54 +01:00
|
|
|
// now that we have our i2p connection established
|
|
|
|
// it's OK to start torrents and use this socket to
|
|
|
|
// do i2p name lookups
|
2013-10-27 20:56:37 +01:00
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
open_new_incoming_i2p_connection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::open_new_incoming_i2p_connection()
|
|
|
|
{
|
|
|
|
if (!m_i2p_conn.is_open()) return;
|
|
|
|
|
|
|
|
if (m_i2p_listen_socket) return;
|
|
|
|
|
|
|
|
m_i2p_listen_socket = boost::shared_ptr<socket_type>(new socket_type(m_io_service));
|
|
|
|
bool ret = instantiate_connection(m_io_service, m_i2p_conn.proxy()
|
|
|
|
, *m_i2p_listen_socket);
|
2010-09-25 19:46:13 +02:00
|
|
|
TORRENT_ASSERT_VAL(ret, ret);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_i2p_accept");
|
|
|
|
#endif
|
2009-08-20 05:19:12 +02:00
|
|
|
i2p_stream& s = *m_i2p_listen_socket->get<i2p_stream>();
|
|
|
|
s.set_command(i2p_stream::cmd_accept);
|
|
|
|
s.set_session_id(m_i2p_conn.session_id());
|
|
|
|
s.async_connect(tcp::endpoint(address_v4::any(), m_listen_interface.port())
|
|
|
|
, boost::bind(&session_impl::on_i2p_accept, this, m_i2p_listen_socket, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::on_i2p_accept(boost::shared_ptr<socket_type> const& s
|
|
|
|
, error_code const& e)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::on_i2p_accept");
|
|
|
|
#endif
|
2009-08-20 05:19:12 +02:00
|
|
|
m_i2p_listen_socket.reset();
|
|
|
|
if (e == asio::error::operation_aborted) return;
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
|
|
|
m_alerts.post_alert(listen_failed_alert(tcp::endpoint(
|
2013-10-06 08:32:33 +02:00
|
|
|
address_v4::any(), m_listen_interface.port()), listen_failed_alert::accept
|
|
|
|
, e, listen_failed_alert::i2p));
|
2010-05-30 03:33:03 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("cannot bind to port %d: %s"
|
2010-05-30 03:33:03 +02:00
|
|
|
, m_listen_interface.port(), e.message().c_str());
|
|
|
|
#endif
|
2009-08-20 05:19:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
open_new_incoming_i2p_connection();
|
|
|
|
incoming_connection(s);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
bool session_impl::incoming_packet(error_code const& ec
|
|
|
|
, udp::endpoint const& ep, char const* buf, int size)
|
2007-12-09 05:15:24 +01:00
|
|
|
{
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_num_messages[on_udp_counter];
|
|
|
|
#endif
|
2008-05-08 02:22:17 +02:00
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
2011-08-21 03:55:38 +02:00
|
|
|
// don't bubble up operation aborted errors to the user
|
2012-06-22 06:21:20 +02:00
|
|
|
if (ec != asio::error::operation_aborted
|
2011-08-21 03:55:38 +02:00
|
|
|
&& m_alerts.should_post<udp_error_alert>())
|
2012-06-22 06:21:20 +02:00
|
|
|
m_alerts.post_alert(udp_error_alert(ep, ec));
|
2012-09-25 04:49:40 +02:00
|
|
|
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("UDP socket error: (%d) %s", ec.value(), ec.message().c_str());
|
2012-09-25 04:49:40 +02:00
|
|
|
#endif
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
2012-06-23 07:46:51 +02:00
|
|
|
return false;
|
2010-08-03 11:08:37 +02:00
|
|
|
}
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
void session_impl::async_accept(boost::shared_ptr<socket_acceptor> const& listener, bool ssl)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-12-24 19:15:01 +01:00
|
|
|
TORRENT_ASSERT(!m_abort);
|
2008-01-27 23:39:50 +01:00
|
|
|
shared_ptr<socket_type> c(new socket_type(m_io_service));
|
2012-01-14 17:04:25 +01:00
|
|
|
stream_socket* str = 0;
|
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (ssl)
|
|
|
|
{
|
|
|
|
// accept connections initializing the SSL connection to
|
|
|
|
// use the generic m_ssl_ctx context. However, since it has
|
|
|
|
// the servername callback set on it, we will switch away from
|
|
|
|
// this context into a specific torrent once we start handshaking
|
|
|
|
c->instantiate<ssl_stream<stream_socket> >(m_io_service, &m_ssl_ctx);
|
|
|
|
str = &c->get<ssl_stream<stream_socket> >()->next_layer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
c->instantiate<stream_socket>(m_io_service);
|
|
|
|
str = c->get<stream_socket>();
|
|
|
|
}
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_accept_connection");
|
|
|
|
#endif
|
2012-01-14 17:04:25 +01:00
|
|
|
listener->async_accept(*str
|
2010-07-22 18:49:40 +02:00
|
|
|
, boost::bind(&session_impl::on_accept_connection, this, c
|
2012-01-14 17:04:25 +01:00
|
|
|
, boost::weak_ptr<socket_acceptor>(listener), _1, ssl));
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2009-04-09 03:04:49 +02:00
|
|
|
void session_impl::on_accept_connection(shared_ptr<socket_type> const& s
|
2012-01-14 17:04:25 +01:00
|
|
|
, weak_ptr<socket_acceptor> listen_socket, error_code const& e, bool ssl)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::on_accept_connection");
|
2011-10-17 07:17:21 +02:00
|
|
|
#endif
|
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_num_messages[on_accept_counter];
|
2010-11-28 02:47:30 +01:00
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2007-09-22 18:27:29 +02:00
|
|
|
boost::shared_ptr<socket_acceptor> listener = listen_socket.lock();
|
|
|
|
if (!listener) return;
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2007-09-22 18:27:29 +02:00
|
|
|
if (e == asio::error::operation_aborted) return;
|
2006-10-11 22:57:54 +02:00
|
|
|
|
|
|
|
if (m_abort) return;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2006-10-11 22:57:54 +02:00
|
|
|
if (e)
|
|
|
|
{
|
2007-09-22 18:27:29 +02:00
|
|
|
tcp::endpoint ep = listener->local_endpoint(ec);
|
2006-10-11 22:57:54 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("error accepting connection on '%s': %s"
|
|
|
|
, print_endpoint(ep).c_str(), e.message().c_str());
|
2007-10-15 21:02:54 +02:00
|
|
|
#endif
|
2008-04-24 05:28:48 +02:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2007-10-15 21:02:54 +02:00
|
|
|
// Windows sometimes generates this error. It seems to be
|
|
|
|
// non-fatal and we have to do another async_accept.
|
|
|
|
if (e.value() == ERROR_SEM_TIMEOUT)
|
|
|
|
{
|
2012-01-14 17:04:25 +01:00
|
|
|
async_accept(listener, ssl);
|
2007-10-15 21:02:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-04-03 06:35:56 +02:00
|
|
|
#endif
|
|
|
|
#ifdef TORRENT_BSD
|
|
|
|
// Leopard sometimes generates an "invalid argument" error. It seems to be
|
|
|
|
// non-fatal and we have to do another async_accept.
|
|
|
|
if (e.value() == EINVAL)
|
|
|
|
{
|
2012-01-14 17:04:25 +01:00
|
|
|
async_accept(listener, ssl);
|
2008-04-03 06:35:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
2010-03-09 18:50:10 +01:00
|
|
|
if (e == boost::system::errc::too_many_files_open)
|
|
|
|
{
|
|
|
|
// if we failed to accept an incoming connection
|
|
|
|
// because we have too many files open, try again
|
|
|
|
// and lower the number of file descriptors used
|
|
|
|
// elsewere.
|
2010-10-09 21:09:38 +02:00
|
|
|
if (m_settings.connections_limit > 10)
|
2011-03-23 09:49:26 +01:00
|
|
|
{
|
|
|
|
// now, disconnect a random peer
|
|
|
|
torrent_map::iterator i = std::max_element(m_torrents.begin()
|
|
|
|
, m_torrents.end(), boost::bind(&torrent::num_peers
|
|
|
|
, boost::bind(&torrent_map::value_type::second, _1)));
|
|
|
|
|
2012-03-04 12:18:27 +01:00
|
|
|
if (m_alerts.should_post<performance_alert>())
|
|
|
|
m_alerts.post_alert(performance_alert(
|
|
|
|
torrent_handle(), performance_alert::too_few_file_descriptors));
|
|
|
|
|
2011-03-23 09:49:26 +01:00
|
|
|
if (i != m_torrents.end())
|
|
|
|
{
|
2012-03-04 11:54:55 +01:00
|
|
|
i->second->disconnect_peers(1, e);
|
2011-03-23 09:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_settings.connections_limit = m_connections.size();
|
|
|
|
}
|
2010-03-09 18:50:10 +01:00
|
|
|
// try again, but still alert the user of the problem
|
2012-01-14 17:04:25 +01:00
|
|
|
async_accept(listener, ssl);
|
2010-03-09 18:50:10 +01:00
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
2013-10-06 08:32:33 +02:00
|
|
|
m_alerts.post_alert(listen_failed_alert(ep, listen_failed_alert::accept, e
|
|
|
|
, ssl ? listen_failed_alert::tcp_ssl : listen_failed_alert::tcp));
|
2006-10-11 22:57:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-01-14 17:04:25 +01:00
|
|
|
async_accept(listener, ssl);
|
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (ssl)
|
|
|
|
{
|
|
|
|
// for SSL connections, incoming_connection() is called
|
|
|
|
// after the handshake is done
|
2013-08-21 17:41:19 +02:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::ssl_handshake");
|
|
|
|
#endif
|
2012-01-14 17:04:25 +01:00
|
|
|
s->get<ssl_stream<stream_socket> >()->async_accept_handshake(
|
|
|
|
boost::bind(&session_impl::ssl_handshake, this, _1, s));
|
2013-08-21 17:55:24 +02:00
|
|
|
m_incoming_sockets.insert(s);
|
2012-01-14 17:04:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
incoming_connection(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
|
|
|
|
// to test SSL connections, one can use this openssl command template:
|
|
|
|
//
|
2014-07-05 16:10:25 +02:00
|
|
|
// openssl s_client -cert <client-cert>.pem -key <client-private-key>.pem
|
|
|
|
// -CAfile <torrent-cert>.pem -debug -connect 127.0.0.1:4433 -tls1
|
2012-01-14 17:04:25 +01:00
|
|
|
// -servername <hex-encoded-info-hash>
|
|
|
|
|
|
|
|
void session_impl::ssl_handshake(error_code const& ec, boost::shared_ptr<socket_type> s)
|
|
|
|
{
|
2013-08-21 17:41:19 +02:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::ssl_handshake");
|
|
|
|
#endif
|
2013-08-21 17:55:24 +02:00
|
|
|
m_incoming_sockets.erase(s);
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
error_code e;
|
|
|
|
tcp::endpoint endp = s->remote_endpoint(e);
|
|
|
|
if (e) return;
|
|
|
|
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" *** peer SSL handshake done [ ip: %s ec: %s socket: %s ]"
|
|
|
|
, print_endpoint(endp).c_str(), ec.message().c_str(), s->type_name());
|
2012-01-14 17:04:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
if (m_alerts.should_post<peer_error_alert>())
|
|
|
|
{
|
|
|
|
m_alerts.post_alert(peer_error_alert(torrent_handle(), endp
|
|
|
|
, peer_id(), ec));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2009-04-09 03:04:49 +02:00
|
|
|
incoming_connection(s);
|
|
|
|
}
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
#endif // TORRENT_USE_OPENSSL
|
|
|
|
|
2009-04-09 03:04:49 +02:00
|
|
|
void session_impl::incoming_connection(boost::shared_ptr<socket_type> const& s)
|
|
|
|
{
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2012-01-16 00:34:43 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
// add the current time to the PRNG, to add more unpredictability
|
|
|
|
boost::uint64_t now = total_microseconds(time_now_hires() - min_time());
|
|
|
|
// assume 12 bits of entropy (i.e. about 8 milliseconds)
|
|
|
|
RAND_add(&now, 8, 1.5);
|
|
|
|
#endif
|
|
|
|
|
2011-01-23 19:00:52 +01:00
|
|
|
if (m_paused)
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" <== INCOMING CONNECTION [ ignored, paused ]");
|
2011-01-23 19:00:52 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-09 03:04:49 +02:00
|
|
|
error_code ec;
|
2006-10-11 22:57:54 +02:00
|
|
|
// we got a connection request!
|
2007-09-22 18:27:29 +02:00
|
|
|
tcp::endpoint endp = s->remote_endpoint(ec);
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("%s <== INCOMING CONNECTION FAILED, could "
|
|
|
|
"not retrieve remote endpoint "
|
|
|
|
, print_endpoint(endp).c_str(), ec.message().c_str());
|
2007-09-22 18:27:29 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" <== INCOMING CONNECTION %s type: %s"
|
|
|
|
, print_endpoint(endp).c_str(), s->type_name());
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
2007-11-19 19:05:47 +01:00
|
|
|
|
2011-09-17 03:44:05 +02:00
|
|
|
if (m_alerts.should_post<incoming_connection_alert>())
|
|
|
|
{
|
|
|
|
m_alerts.post_alert(incoming_connection_alert(s->type(), endp));
|
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
if (!m_settings.enable_incoming_utp
|
|
|
|
&& s->get<utp_stream>())
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" rejected uTP connection");
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
if (m_alerts.should_post<peer_blocked_alert>())
|
2014-02-07 18:35:56 +01:00
|
|
|
m_alerts.post_alert(peer_blocked_alert(torrent_handle()
|
|
|
|
, endp.address(), peer_blocked_alert::utp_disabled));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_settings.enable_incoming_tcp
|
|
|
|
&& s->get<stream_socket>())
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" rejected TCP connection");
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
if (m_alerts.should_post<peer_blocked_alert>())
|
2014-02-07 18:35:56 +01:00
|
|
|
m_alerts.post_alert(peer_blocked_alert(torrent_handle()
|
|
|
|
, endp.address(), peer_blocked_alert::tcp_disabled));
|
2010-11-29 02:33:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-19 19:05:47 +01:00
|
|
|
// local addresses do not count, since it's likely
|
|
|
|
// coming from our own client through local service discovery
|
|
|
|
// and it does not reflect whether or not a router is open
|
|
|
|
// for incoming connections or not.
|
|
|
|
if (!is_local(endp.address()))
|
|
|
|
m_incoming_connection = true;
|
|
|
|
|
2011-03-02 18:37:10 +01:00
|
|
|
// this filter is ignored if a single torrent
|
|
|
|
// is set to ignore the filter, since this peer might be
|
|
|
|
// for that torrent
|
2011-03-07 02:20:23 +01:00
|
|
|
if (m_non_filtered_torrents == 0
|
2011-03-02 18:37:10 +01:00
|
|
|
&& (m_ip_filter.access(endp.address()) & ip_filter::blocked))
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("filtered blocked ip");
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<peer_blocked_alert>())
|
2014-02-07 18:35:56 +01:00
|
|
|
m_alerts.post_alert(peer_blocked_alert(torrent_handle()
|
|
|
|
, endp.address(), peer_blocked_alert::ip_filter));
|
2006-10-11 22:57:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-31 07:54:54 +01:00
|
|
|
// check if we have any active torrents
|
|
|
|
// if we don't reject the connection
|
|
|
|
if (m_torrents.empty())
|
|
|
|
{
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
|
|
|
session_log(" There are no torrents, disconnect");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-27 23:21:44 +02:00
|
|
|
// don't allow more connections than the max setting
|
2010-01-31 19:35:46 +01:00
|
|
|
bool reject = false;
|
|
|
|
if (m_settings.ignore_limits_on_local_network && is_local(endp.address()))
|
2010-10-09 21:09:38 +02:00
|
|
|
reject = m_settings.connections_limit < INT_MAX / 12
|
|
|
|
&& num_connections() >= m_settings.connections_limit * 12 / 10;
|
2010-01-31 19:35:46 +01:00
|
|
|
else
|
2012-12-31 07:54:54 +01:00
|
|
|
reject = num_connections() >= m_settings.connections_limit + m_settings.connections_slack;
|
2010-01-31 19:35:46 +01:00
|
|
|
|
|
|
|
if (reject)
|
2007-10-27 23:21:44 +02:00
|
|
|
{
|
2010-02-06 22:40:55 +01:00
|
|
|
if (m_alerts.should_post<peer_disconnected_alert>())
|
|
|
|
{
|
|
|
|
m_alerts.post_alert(
|
|
|
|
peer_disconnected_alert(torrent_handle(), endp, peer_id()
|
|
|
|
, error_code(errors::too_many_connections, get_libtorrent_category())));
|
|
|
|
}
|
2007-10-27 23:21:44 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-12-31 07:54:54 +01:00
|
|
|
session_log("number of connections limit exceeded (conns: %d"
|
|
|
|
", limit: %d slack: %d), connection rejected\n"
|
|
|
|
, num_connections(), m_settings.connections_limit, m_settings.connections_slack);
|
2007-10-27 23:21:44 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-11 05:39:04 +01:00
|
|
|
// if we don't have any active torrents, there's no
|
|
|
|
// point in accepting this connection. If, however,
|
|
|
|
// the setting to start up queued torrents when they
|
|
|
|
// get an incoming connection is enabled, we cannot
|
|
|
|
// perform this check.
|
|
|
|
if (!m_settings.incoming_starts_queued_torrents)
|
2007-09-04 00:27:52 +02:00
|
|
|
{
|
2010-02-11 05:39:04 +01:00
|
|
|
bool has_active_torrent = false;
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
2007-09-04 00:27:52 +02:00
|
|
|
{
|
2010-03-29 02:34:04 +02:00
|
|
|
if (i->second->allows_peers())
|
2010-02-11 05:39:04 +01:00
|
|
|
{
|
|
|
|
has_active_torrent = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-09-04 00:27:52 +02:00
|
|
|
}
|
2010-02-11 05:39:04 +01:00
|
|
|
if (!has_active_torrent)
|
|
|
|
{
|
2008-05-12 12:10:39 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" There are no _active_ torrents, disconnect");
|
2008-05-12 12:10:39 +02:00
|
|
|
#endif
|
2010-02-11 05:39:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-05-12 12:10:39 +02:00
|
|
|
}
|
2007-09-04 00:27:52 +02:00
|
|
|
|
2009-05-01 06:59:15 +02:00
|
|
|
setup_socket_buffers(*s);
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
boost::intrusive_ptr<peer_connection> c(
|
2014-04-23 01:37:21 +02:00
|
|
|
new bt_peer_connection(*this, s, endp, 0, get_peer_id()));
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2006-10-11 22:57:54 +02:00
|
|
|
c->m_in_constructor = false;
|
|
|
|
#endif
|
|
|
|
|
2008-03-31 06:46:24 +02:00
|
|
|
if (!c->is_disconnecting())
|
|
|
|
{
|
2012-12-31 07:54:54 +01:00
|
|
|
// in case we've exceeded the limit, let this peer know that
|
|
|
|
// as soon as it's received the handshake, it needs to either
|
|
|
|
// disconnect or pick another peer to disconnect
|
|
|
|
if (num_connections() >= m_settings.connections_limit)
|
|
|
|
c->peer_exceeds_limit();
|
|
|
|
|
2013-11-02 04:26:53 +01:00
|
|
|
TORRENT_ASSERT(!c->m_in_constructor);
|
2008-03-31 06:46:24 +02:00
|
|
|
m_connections.insert(c);
|
|
|
|
c->start();
|
2011-03-27 22:35:38 +02:00
|
|
|
// update the next disk peer round-robin cursor
|
|
|
|
if (m_next_disk_peer == m_connections.end()) m_next_disk_peer = m_connections.begin();
|
2008-03-31 06:46:24 +02:00
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
2009-04-09 03:04:49 +02:00
|
|
|
|
2009-05-01 06:59:15 +02:00
|
|
|
void session_impl::setup_socket_buffers(socket_type& s)
|
|
|
|
{
|
|
|
|
error_code ec;
|
2013-09-03 10:39:30 +02:00
|
|
|
set_socket_buffer_size(s, m_settings, ec);
|
2009-05-01 06:59:15 +02:00
|
|
|
}
|
|
|
|
|
2009-04-09 03:04:49 +02:00
|
|
|
void session_impl::on_socks_accept(boost::shared_ptr<socket_type> const& s
|
|
|
|
, error_code const& e)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::on_socks_accept");
|
|
|
|
#endif
|
2009-04-09 03:04:49 +02:00
|
|
|
m_socks_listen_socket.reset();
|
|
|
|
if (e == asio::error::operation_aborted) return;
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
|
|
|
m_alerts.post_alert(listen_failed_alert(tcp::endpoint(
|
2013-10-06 08:32:33 +02:00
|
|
|
address_v4::any(), m_listen_interface.port()), listen_failed_alert::accept, e
|
|
|
|
, listen_failed_alert::socks5));
|
2009-04-09 03:04:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
open_new_incoming_socks_connection();
|
|
|
|
incoming_connection(s);
|
|
|
|
}
|
|
|
|
|
2008-01-07 02:10:46 +01:00
|
|
|
void session_impl::close_connection(peer_connection const* p
|
2009-06-12 18:40:38 +02:00
|
|
|
, error_code const& ec)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2013-12-05 08:42:32 +01:00
|
|
|
// someone else is holding a reference, it's important that
|
|
|
|
// it's destructed from the network thread. Make sure the
|
|
|
|
// last reference is held by the network thread.
|
2013-12-09 09:00:21 +01:00
|
|
|
if (p->refcount() != 1)
|
2013-12-05 08:42:32 +01:00
|
|
|
m_undead_peers.push_back((peer_connection*)p);
|
|
|
|
|
2007-08-21 21:18:06 +02:00
|
|
|
// too expensive
|
|
|
|
// INVARIANT_CHECK;
|
2007-08-21 20:33:28 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-01-07 02:10:46 +01:00
|
|
|
// for (aux::session_impl::torrent_map::const_iterator i = m_torrents.begin()
|
|
|
|
// , end(m_torrents.end()); i != end; ++i)
|
|
|
|
// TORRENT_ASSERT(!i->second->has_peer((peer_connection*)p));
|
|
|
|
#endif
|
|
|
|
|
2007-12-19 22:36:54 +01:00
|
|
|
#if defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" CLOSING CONNECTION %s : %s"
|
|
|
|
, print_endpoint(p->remote()).c_str(), ec.message().c_str());
|
2007-12-19 22:36:54 +01:00
|
|
|
#endif
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->is_disconnecting());
|
2008-01-07 02:10:46 +01:00
|
|
|
|
2009-01-28 07:14:21 +01:00
|
|
|
if (!p->is_choked() && !p->ignore_unchoke_slots()) --m_num_unchoked;
|
2011-01-25 09:03:35 +01:00
|
|
|
TORRENT_ASSERT(p->refcount() > 0);
|
|
|
|
|
|
|
|
boost::intrusive_ptr<peer_connection> sp((peer_connection*)p);
|
|
|
|
connection_map::iterator i = m_connections.find(sp);
|
2011-03-27 22:35:38 +02:00
|
|
|
// make sure the next disk peer round-robin cursor stays valid
|
|
|
|
if (m_next_disk_peer == i) ++m_next_disk_peer;
|
2008-01-07 02:10:46 +01:00
|
|
|
if (i != m_connections.end()) m_connections.erase(i);
|
2011-03-27 22:35:38 +02:00
|
|
|
if (m_next_disk_peer == m_connections.end()) m_next_disk_peer = m_connections.begin();
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 07:39:35 +02:00
|
|
|
// implements alert_dispatcher
|
|
|
|
bool session_impl::post_alert(alert* a)
|
|
|
|
{
|
|
|
|
if (!m_alerts.should_post(a)) return false;
|
|
|
|
m_alerts.post_alert_ptr(a);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
void session_impl::set_peer_id(peer_id const& id)
|
|
|
|
{
|
|
|
|
m_peer_id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_key(int key)
|
|
|
|
{
|
|
|
|
m_key = key;
|
|
|
|
}
|
|
|
|
|
2008-12-26 08:00:21 +01:00
|
|
|
void session_impl::unchoke_peer(peer_connection& c)
|
|
|
|
{
|
2009-01-28 07:14:21 +01:00
|
|
|
TORRENT_ASSERT(!c.ignore_unchoke_slots());
|
2008-12-26 08:00:21 +01:00
|
|
|
torrent* t = c.associated_torrent().lock().get();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
if (t->unchoke_peer(c))
|
|
|
|
++m_num_unchoked;
|
|
|
|
}
|
|
|
|
|
2009-07-23 06:38:52 +02:00
|
|
|
void session_impl::choke_peer(peer_connection& c)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!c.ignore_unchoke_slots());
|
|
|
|
torrent* t = c.associated_torrent().lock().get();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
if (t->choke_peer(c))
|
|
|
|
--m_num_unchoked;
|
|
|
|
}
|
|
|
|
|
2008-02-28 08:34:07 +01:00
|
|
|
int session_impl::next_port()
|
|
|
|
{
|
|
|
|
std::pair<int, int> const& out_ports = m_settings.outgoing_ports;
|
|
|
|
if (m_next_port < out_ports.first || m_next_port > out_ports.second)
|
|
|
|
m_next_port = out_ports.first;
|
|
|
|
|
|
|
|
int port = m_next_port;
|
|
|
|
++m_next_port;
|
|
|
|
if (m_next_port > out_ports.second) m_next_port = out_ports.first;
|
|
|
|
#if defined TORRENT_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" *** BINDING OUTGOING CONNECTION [ port: %d ]", port);
|
2008-02-28 08:34:07 +01:00
|
|
|
#endif
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
// this function is called from the disk-io thread
|
|
|
|
// when the disk queue is low enough to post new
|
|
|
|
// write jobs to it. It will go through all peer
|
|
|
|
// connections that are blocked on the disk and
|
|
|
|
// wake them up
|
|
|
|
void session_impl::on_disk_queue()
|
|
|
|
{
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_num_messages[on_disk_queue_counter];
|
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2011-03-27 22:35:38 +02:00
|
|
|
// just to play it safe
|
|
|
|
if (m_next_disk_peer == m_connections.end()) m_next_disk_peer = m_connections.begin();
|
2009-06-10 10:30:55 +02:00
|
|
|
|
2011-03-27 22:35:38 +02:00
|
|
|
// never loop more times than there are connections
|
|
|
|
// keep in mind that connections may disconnect
|
|
|
|
// while we're looping, that's why this is a reliable
|
|
|
|
// way of limiting it
|
|
|
|
int limit = m_connections.size();
|
2011-03-21 09:19:00 +01:00
|
|
|
|
2011-04-01 10:08:41 +02:00
|
|
|
while (m_next_disk_peer != m_connections.end() && limit > 0 && can_write_to_disk())
|
2010-12-05 00:24:42 +01:00
|
|
|
{
|
2011-03-27 22:35:38 +02:00
|
|
|
--limit;
|
|
|
|
peer_connection* p = m_next_disk_peer->get();
|
|
|
|
++m_next_disk_peer;
|
|
|
|
if (m_next_disk_peer == m_connections.end()) m_next_disk_peer = m_connections.begin();
|
2011-10-29 01:08:52 +02:00
|
|
|
if ((p->m_channel_state[peer_connection::download_channel]
|
|
|
|
& peer_info::bw_disk) == 0) continue;
|
2011-03-27 22:35:38 +02:00
|
|
|
p->on_disk();
|
2011-04-01 10:08:41 +02:00
|
|
|
}
|
2011-03-20 08:43:57 +01:00
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
}
|
|
|
|
|
2009-11-30 08:03:34 +01:00
|
|
|
// used to cache the current time
|
|
|
|
// every 100 ms. This is cheaper
|
|
|
|
// than a system call and can be
|
|
|
|
// used where more accurate time
|
|
|
|
// is not necessary
|
|
|
|
extern ptime g_current_time;
|
|
|
|
|
2009-12-13 17:32:07 +01:00
|
|
|
initialize_timer::initialize_timer()
|
|
|
|
{
|
|
|
|
g_current_time = time_now_hires();
|
|
|
|
}
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
void session_impl::on_tick(error_code const& e)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::on_tick");
|
|
|
|
#endif
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_num_messages[on_tick_counter];
|
|
|
|
#endif
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2009-05-25 04:45:51 +02:00
|
|
|
ptime now = time_now_hires();
|
|
|
|
aux::g_current_time = now;
|
2007-09-29 18:14:03 +02:00
|
|
|
// too expensive
|
|
|
|
// INVARIANT_CHECK;
|
2007-08-21 20:33:28 +02:00
|
|
|
|
2012-01-23 06:02:12 +01:00
|
|
|
// we have to keep ticking the utp socket manager
|
|
|
|
// until they're all closed
|
|
|
|
if (m_abort && m_utp_socket_manager.num_sockets() == 0)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
fprintf(stderr, "uTP sockets left: %d\n", m_utp_socket_manager.num_sockets());
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-10-07 20:06:56 +02:00
|
|
|
|
2010-03-17 20:01:08 +01:00
|
|
|
if (e == asio::error::operation_aborted) return;
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
if (e)
|
|
|
|
{
|
2010-10-17 18:15:32 +02:00
|
|
|
#if defined TORRENT_LOGGING || defined TORRENT_VERBOSE_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("*** TICK TIMER FAILED %s", e.message().c_str());
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
2008-02-28 08:34:07 +01:00
|
|
|
::abort();
|
2006-10-11 22:57:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_tick");
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2010-05-03 10:54:03 +02:00
|
|
|
m_timer.expires_at(now + milliseconds(m_settings.tick_interval), ec);
|
2009-04-30 07:49:46 +02:00
|
|
|
m_timer.async_wait(bind(&session_impl::on_tick, this, _1));
|
2009-04-26 02:21:59 +02:00
|
|
|
|
|
|
|
m_download_rate.update_quotas(now - m_last_tick);
|
|
|
|
m_upload_rate.update_quotas(now - m_last_tick);
|
|
|
|
|
|
|
|
m_last_tick = now;
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
m_utp_socket_manager.tick(now);
|
|
|
|
|
2009-04-30 07:49:46 +02:00
|
|
|
// only tick the following once per second
|
2009-04-26 02:21:59 +02:00
|
|
|
if (now - m_last_second_tick < seconds(1)) return;
|
|
|
|
|
2012-08-03 07:13:40 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (m_dht_interval_update_torrents < 40
|
2014-07-05 16:10:25 +02:00
|
|
|
&& m_dht_interval_update_torrents != int(m_torrents.size()))
|
2012-08-03 07:13:40 +02:00
|
|
|
update_dht_announce_interval();
|
|
|
|
#endif
|
|
|
|
|
2013-12-05 08:42:32 +01:00
|
|
|
// remove undead peers that only have this list as their reference keeping them alive
|
|
|
|
std::vector<boost::intrusive_ptr<peer_connection> >::iterator i = std::remove_if(
|
|
|
|
m_undead_peers.begin(), m_undead_peers.end()
|
|
|
|
, boost::bind(&peer_connection::refcount, _1) == 1);
|
|
|
|
m_undead_peers.erase(i, m_undead_peers.end());
|
|
|
|
|
2014-05-10 05:23:05 +02:00
|
|
|
int tick_interval_ms = int(total_milliseconds(now - m_last_second_tick));
|
2009-04-26 02:21:59 +02:00
|
|
|
m_last_second_tick = now;
|
2010-02-14 02:39:55 +01:00
|
|
|
m_tick_residual += tick_interval_ms - 1000;
|
2007-05-14 00:01:21 +02:00
|
|
|
|
2014-05-10 05:23:05 +02:00
|
|
|
boost::int64_t session_time = total_seconds(now - m_created);
|
2009-04-30 07:49:46 +02:00
|
|
|
if (session_time > 65000)
|
|
|
|
{
|
|
|
|
// we're getting close to the point where our timestamps
|
|
|
|
// in policy::peer are wrapping. We need to step all counters back
|
|
|
|
// four hours. This means that any timestamp that refers to a time
|
|
|
|
// more than 18.2 - 4 = 14.2 hours ago, will be incremented to refer to
|
|
|
|
// 14.2 hours ago.
|
|
|
|
|
|
|
|
m_created += hours(4);
|
|
|
|
|
|
|
|
const int four_hours = 60 * 60 * 4;
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
policy& p = i->second->get_policy();
|
|
|
|
for (policy::iterator j = p.begin_peer()
|
|
|
|
, end(p.end_peer()); j != end; ++j)
|
|
|
|
{
|
2009-05-13 18:14:20 +02:00
|
|
|
policy::peer* pe = *j;
|
2009-04-30 07:49:46 +02:00
|
|
|
|
|
|
|
if (pe->last_optimistically_unchoked < four_hours)
|
|
|
|
pe->last_optimistically_unchoked = 0;
|
|
|
|
else
|
|
|
|
pe->last_optimistically_unchoked -= four_hours;
|
|
|
|
|
|
|
|
if (pe->last_connected < four_hours)
|
|
|
|
pe->last_connected = 0;
|
|
|
|
else
|
|
|
|
pe->last_connected -= four_hours;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-29 11:37:21 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (ses_extension_list_t::const_iterator i = m_ses_extensions.begin()
|
|
|
|
, end(m_ses_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
(*i)->on_tick();
|
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2011-01-29 11:37:21 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-21 00:44:34 +01:00
|
|
|
// don't do any of the following while we're shutting down
|
|
|
|
if (m_abort) return;
|
|
|
|
|
2011-01-18 04:41:54 +01:00
|
|
|
// --------------------------------------------------------------
|
|
|
|
// RSS feeds
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
if (now > m_next_rss_update)
|
|
|
|
update_rss_feeds();
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
switch (m_settings.mixed_mode_algorithm)
|
|
|
|
{
|
|
|
|
case session_settings::prefer_tcp:
|
|
|
|
m_tcp_upload_channel.throttle(0);
|
|
|
|
m_tcp_download_channel.throttle(0);
|
|
|
|
break;
|
|
|
|
case session_settings::peer_proportional:
|
|
|
|
{
|
2011-10-11 07:00:35 +02:00
|
|
|
int num_peers[2][2] = {{0, 0}, {0, 0}};
|
2010-11-29 02:33:05 +01:00
|
|
|
for (connection_map::iterator i = m_connections.begin()
|
|
|
|
, end(m_connections.end());i != end; ++i)
|
|
|
|
{
|
|
|
|
peer_connection& p = *(*i);
|
|
|
|
if (p.in_handshake()) continue;
|
2011-10-11 07:00:35 +02:00
|
|
|
int protocol = 0;
|
2012-10-05 05:20:40 +02:00
|
|
|
if (is_utp(*p.get_socket())) protocol = 1;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2011-10-11 07:00:35 +02:00
|
|
|
if (p.download_queue().size() + p.request_queue().size() > 0)
|
|
|
|
++num_peers[protocol][peer_connection::download_channel];
|
|
|
|
if (p.upload_queue().size() > 0)
|
|
|
|
++num_peers[protocol][peer_connection::upload_channel];
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
2011-10-11 07:00:35 +02:00
|
|
|
|
|
|
|
bandwidth_channel* tcp_channel[] = { &m_tcp_upload_channel, &m_tcp_download_channel };
|
|
|
|
int stat_rate[] = {m_stat.upload_rate(), m_stat.download_rate() };
|
2011-10-11 07:05:09 +02:00
|
|
|
// never throttle below this
|
|
|
|
int lower_limit[] = {5000, 30000};
|
2011-10-11 07:00:35 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i)
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
2011-10-11 07:00:35 +02:00
|
|
|
// if there are no uploading uTP peers, don't throttle TCP up
|
|
|
|
if (num_peers[1][i] == 0)
|
|
|
|
{
|
|
|
|
tcp_channel[i]->throttle(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (num_peers[0][i] == 0) num_peers[0][i] = 1;
|
|
|
|
int total_peers = num_peers[0][i] + num_peers[1][i];
|
|
|
|
// this are 64 bits since it's multiplied by the number
|
|
|
|
// of peers, which otherwise might overflow an int
|
2013-11-02 04:26:53 +01:00
|
|
|
boost::uint64_t rate = stat_rate[i];
|
|
|
|
tcp_channel[i]->throttle((std::max)(int(rate * num_peers[0][i] / total_peers), lower_limit[i]));
|
2011-10-11 07:00:35 +02:00
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-25 08:49:32 +01:00
|
|
|
// --------------------------------------------------------------
|
|
|
|
// auto managed torrent
|
|
|
|
// --------------------------------------------------------------
|
2011-12-24 21:15:22 +01:00
|
|
|
if (!m_paused) m_auto_manage_time_scaler--;
|
2011-12-23 21:02:59 +01:00
|
|
|
if (m_auto_manage_time_scaler < 0)
|
2011-03-25 08:49:32 +01:00
|
|
|
{
|
|
|
|
m_auto_manage_time_scaler = settings().auto_manage_interval;
|
|
|
|
recalculate_auto_managed_torrents();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
// check for incoming connections that might have timed out
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
|
|
|
|
for (connection_map::iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end();)
|
|
|
|
{
|
|
|
|
peer_connection* p = (*i).get();
|
|
|
|
++i;
|
|
|
|
// ignore connections that already have a torrent, since they
|
|
|
|
// are ticked through the torrents' second_tick
|
|
|
|
if (!p->associated_torrent().expired()) continue;
|
2013-02-07 07:41:11 +01:00
|
|
|
// TODO: have a separate list for these connections, instead of having to loop through all of them
|
2011-03-25 08:49:32 +01:00
|
|
|
if (m_last_tick - p->connected_time() > seconds(m_settings.handshake_timeout))
|
|
|
|
p->disconnect(errors::timed_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
// second_tick every torrent
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
|
|
|
|
int congested_torrents = 0;
|
|
|
|
int uncongested_torrents = 0;
|
|
|
|
|
|
|
|
// count the number of seeding torrents vs. downloading
|
|
|
|
// torrents we are running
|
|
|
|
int num_seeds = 0;
|
|
|
|
int num_downloads = 0;
|
|
|
|
|
|
|
|
// count the number of peers of downloading torrents
|
|
|
|
int num_downloads_peers = 0;
|
|
|
|
|
|
|
|
torrent_map::iterator least_recently_scraped = m_torrents.end();
|
|
|
|
int num_paused_auto_managed = 0;
|
|
|
|
|
|
|
|
int num_checking = 0;
|
|
|
|
int num_queued = 0;
|
2014-06-06 03:26:18 +02:00
|
|
|
|
|
|
|
#if TORRENT_DEBUG_STREAMING > 0
|
|
|
|
printf("\033[2J\033[0;0H");
|
|
|
|
#endif
|
|
|
|
|
2011-03-25 08:49:32 +01:00
|
|
|
for (torrent_map::iterator i = m_torrents.begin();
|
|
|
|
i != m_torrents.end();)
|
|
|
|
{
|
|
|
|
torrent& t = *i->second;
|
|
|
|
TORRENT_ASSERT(!t.is_aborted());
|
2011-05-16 09:47:21 +02:00
|
|
|
if (t.statistics().upload_rate() * 11 / 10 > t.upload_limit())
|
2011-03-25 08:49:32 +01:00
|
|
|
++congested_torrents;
|
|
|
|
else
|
|
|
|
++uncongested_torrents;
|
|
|
|
|
|
|
|
if (t.state() == torrent_status::checking_files) ++num_checking;
|
|
|
|
else if (t.state() == torrent_status::queued_for_checking && !t.is_paused()) ++num_queued;
|
|
|
|
|
|
|
|
if (t.is_auto_managed() && t.is_paused() && !t.has_error())
|
|
|
|
{
|
|
|
|
++num_paused_auto_managed;
|
|
|
|
if (least_recently_scraped == m_torrents.end()
|
|
|
|
|| least_recently_scraped->second->seconds_since_last_scrape()
|
|
|
|
< t.seconds_since_last_scrape())
|
|
|
|
{
|
|
|
|
least_recently_scraped = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t.is_finished())
|
|
|
|
{
|
|
|
|
++num_seeds;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++num_downloads;
|
|
|
|
num_downloads_peers += t.num_peers();
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
2013-08-15 08:10:41 +02:00
|
|
|
t.second_tick(m_stat, tick_interval_ms);
|
2011-03-25 08:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// some people claim that there sometimes can be cases where
|
|
|
|
// there is no torrent being checked, but there are torrents
|
|
|
|
// waiting to be checked. I have never seen this, and I can't
|
|
|
|
// see a way for it to happen. But, if it does, start one of
|
|
|
|
// the queued torrents
|
2011-06-05 05:06:34 +02:00
|
|
|
if (num_checking == 0 && num_queued > 0 && !m_paused)
|
2011-03-25 08:49:32 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(false);
|
|
|
|
check_queue_t::iterator i = std::min_element(m_queued_for_checking.begin()
|
|
|
|
, m_queued_for_checking.end(), boost::bind(&torrent::queue_position, _1)
|
|
|
|
< boost::bind(&torrent::queue_position, _2));
|
|
|
|
if (i != m_queued_for_checking.end())
|
|
|
|
{
|
|
|
|
(*i)->start_checking();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (m_dht)
|
|
|
|
{
|
|
|
|
int dht_down;
|
|
|
|
int dht_up;
|
|
|
|
m_dht->network_stats(dht_up, dht_down);
|
|
|
|
m_stat.sent_dht_bytes(dht_up);
|
|
|
|
m_stat.received_dht_bytes(dht_down);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m_settings.rate_limit_ip_overhead)
|
|
|
|
{
|
|
|
|
m_download_channel.use_quota(
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
m_stat.download_dht() +
|
|
|
|
#endif
|
|
|
|
m_stat.download_tracker());
|
|
|
|
|
|
|
|
m_upload_channel.use_quota(
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
m_stat.upload_dht() +
|
|
|
|
#endif
|
|
|
|
m_stat.upload_tracker());
|
|
|
|
|
|
|
|
int up_limit = m_upload_channel.throttle();
|
|
|
|
int down_limit = m_download_channel.throttle();
|
|
|
|
|
|
|
|
if (down_limit > 0
|
|
|
|
&& m_stat.download_ip_overhead() >= down_limit
|
|
|
|
&& m_alerts.should_post<performance_alert>())
|
|
|
|
{
|
|
|
|
m_alerts.post_alert(performance_alert(torrent_handle()
|
|
|
|
, performance_alert::download_limit_too_low));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up_limit > 0
|
|
|
|
&& m_stat.upload_ip_overhead() >= up_limit
|
|
|
|
&& m_alerts.should_post<performance_alert>())
|
|
|
|
{
|
|
|
|
m_alerts.post_alert(performance_alert(torrent_handle()
|
|
|
|
, performance_alert::upload_limit_too_low));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_peak_up_rate = (std::max)(m_stat.upload_rate(), m_peak_up_rate);
|
|
|
|
m_peak_down_rate = (std::max)(m_stat.download_rate(), m_peak_down_rate);
|
|
|
|
|
|
|
|
m_stat.second_tick(tick_interval_ms);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(least_recently_scraped == m_torrents.end()
|
|
|
|
|| (least_recently_scraped->second->is_paused()
|
|
|
|
&& least_recently_scraped->second->is_auto_managed()));
|
|
|
|
|
2007-05-14 00:01:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
2011-02-06 04:07:00 +01:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
if (m_stats_logging_enabled)
|
2007-05-14 00:01:21 +02:00
|
|
|
{
|
2011-04-06 08:27:42 +02:00
|
|
|
print_log_line(tick_interval_ms, now);
|
|
|
|
}
|
|
|
|
#endif
|
2011-02-06 04:07:00 +01:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
// --------------------------------------------------------------
|
|
|
|
// scrape paused torrents that are auto managed
|
|
|
|
// (unless the session is paused)
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
if (!is_paused())
|
|
|
|
{
|
|
|
|
--m_auto_scrape_time_scaler;
|
|
|
|
if (m_auto_scrape_time_scaler <= 0)
|
2011-02-06 04:07:00 +01:00
|
|
|
{
|
2011-04-06 08:27:42 +02:00
|
|
|
m_auto_scrape_time_scaler = m_settings.auto_scrape_interval
|
|
|
|
/ (std::max)(1, num_paused_auto_managed);
|
|
|
|
if (m_auto_scrape_time_scaler < m_settings.auto_scrape_min_interval)
|
|
|
|
m_auto_scrape_time_scaler = m_settings.auto_scrape_min_interval;
|
|
|
|
|
|
|
|
if (least_recently_scraped != m_torrents.end())
|
2011-02-06 04:07:00 +01:00
|
|
|
{
|
2011-04-06 08:27:42 +02:00
|
|
|
least_recently_scraped->second->scrape_tracker();
|
2011-02-06 04:07:00 +01:00
|
|
|
}
|
|
|
|
}
|
2007-05-14 00:01:21 +02:00
|
|
|
}
|
2011-04-06 08:27:42 +02:00
|
|
|
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
// refresh explicit disk read cache
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
--m_cache_rotation_timer;
|
|
|
|
if (m_settings.explicit_read_cache
|
|
|
|
&& m_cache_rotation_timer <= 0)
|
2007-05-14 06:01:30 +02:00
|
|
|
{
|
2011-04-06 08:27:42 +02:00
|
|
|
m_cache_rotation_timer = m_settings.explicit_cache_interval;
|
|
|
|
|
|
|
|
torrent_map::iterator least_recently_refreshed = m_torrents.begin();
|
|
|
|
if (m_next_explicit_cache_torrent >= int(m_torrents.size()))
|
|
|
|
m_next_explicit_cache_torrent = 0;
|
|
|
|
|
|
|
|
std::advance(least_recently_refreshed, m_next_explicit_cache_torrent);
|
|
|
|
|
|
|
|
// how many blocks does this torrent get?
|
|
|
|
int cache_size = (std::max)(0, m_settings.cache_size * 9 / 10);
|
|
|
|
|
|
|
|
if (m_connections.empty())
|
2008-12-27 03:22:20 +01:00
|
|
|
{
|
2011-04-06 08:27:42 +02:00
|
|
|
// if we don't have any connections at all, split the
|
|
|
|
// cache evenly across all torrents
|
|
|
|
cache_size = cache_size / (std::max)(int(m_torrents.size()), 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cache_size = cache_size * least_recently_refreshed->second->num_peers()
|
|
|
|
/ m_connections.size();
|
2008-12-27 03:22:20 +01:00
|
|
|
}
|
2011-02-03 05:09:50 +01:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
if (least_recently_refreshed != m_torrents.end())
|
|
|
|
least_recently_refreshed->second->refresh_explicit_cache(cache_size);
|
|
|
|
++m_next_explicit_cache_torrent;
|
|
|
|
}
|
2011-02-03 07:22:22 +01:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
// --------------------------------------------------------------
|
|
|
|
// connect new peers
|
|
|
|
// --------------------------------------------------------------
|
2011-02-03 07:22:22 +01:00
|
|
|
|
2012-07-04 22:41:22 +02:00
|
|
|
try_connect_more_peers(num_downloads, num_downloads_peers);
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2007-08-16 14:41:46 +02:00
|
|
|
// --------------------------------------------------------------
|
2008-10-19 00:35:10 +02:00
|
|
|
// unchoke set calculations
|
2007-08-16 14:41:46 +02:00
|
|
|
// --------------------------------------------------------------
|
|
|
|
m_unchoke_time_scaler--;
|
|
|
|
if (m_unchoke_time_scaler <= 0 && !m_connections.empty())
|
|
|
|
{
|
|
|
|
m_unchoke_time_scaler = settings().unchoke_interval;
|
2008-04-24 05:28:48 +02:00
|
|
|
recalculate_unchoke_slots(congested_torrents
|
|
|
|
, uncongested_torrents);
|
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2008-10-19 00:35:10 +02:00
|
|
|
// --------------------------------------------------------------
|
|
|
|
// optimistic unchoke calculation
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
m_optimistic_unchoke_time_scaler--;
|
|
|
|
if (m_optimistic_unchoke_time_scaler <= 0)
|
|
|
|
{
|
|
|
|
m_optimistic_unchoke_time_scaler
|
|
|
|
= settings().optimistic_unchoke_interval;
|
2010-02-02 19:39:32 +01:00
|
|
|
recalculate_optimistic_unchoke_slots();
|
2008-10-19 00:35:10 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
// --------------------------------------------------------------
|
|
|
|
// disconnect peers when we have too many
|
|
|
|
// --------------------------------------------------------------
|
|
|
|
--m_disconnect_time_scaler;
|
|
|
|
if (m_disconnect_time_scaler <= 0)
|
|
|
|
{
|
2010-10-30 17:59:57 +02:00
|
|
|
m_disconnect_time_scaler = m_settings.peer_turnover_interval;
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
if (num_connections() >= m_settings.connections_limit * m_settings.peer_turnover_cutoff
|
2008-05-12 07:17:11 +02:00
|
|
|
&& !m_torrents.empty())
|
2007-08-16 14:41:46 +02:00
|
|
|
{
|
2010-10-03 12:07:38 +02:00
|
|
|
// every 90 seconds, disconnect the worst peers
|
|
|
|
// if we have reached the connection limit
|
2008-04-24 05:28:48 +02:00
|
|
|
torrent_map::iterator i = std::max_element(m_torrents.begin(), m_torrents.end()
|
2009-11-23 00:55:54 +01:00
|
|
|
, boost::bind(&torrent::num_peers, boost::bind(&torrent_map::value_type::second, _1))
|
|
|
|
< boost::bind(&torrent::num_peers, boost::bind(&torrent_map::value_type::second, _2)));
|
2008-04-24 05:28:48 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(i != m_torrents.end());
|
2010-10-03 12:07:38 +02:00
|
|
|
int peers_to_disconnect = (std::min)((std::max)(
|
|
|
|
int(i->second->num_peers() * m_settings.peer_turnover), 1)
|
2008-05-12 07:17:11 +02:00
|
|
|
, i->second->get_policy().num_connect_candidates());
|
2010-10-03 12:07:38 +02:00
|
|
|
i->second->disconnect_peers(peers_to_disconnect
|
|
|
|
, error_code(errors::optimistic_disconnect, get_libtorrent_category()));
|
2008-05-12 07:17:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if we haven't reached the global max. see if any torrent
|
|
|
|
// has reached its local limit
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = i->second;
|
|
|
|
if (t->num_peers() < t->max_connections() * m_settings.peer_turnover_cutoff)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int peers_to_disconnect = (std::min)((std::max)(int(i->second->num_peers()
|
|
|
|
* m_settings.peer_turnover), 1)
|
|
|
|
, i->second->get_policy().num_connect_candidates());
|
2010-10-03 12:07:38 +02:00
|
|
|
t->disconnect_peers(peers_to_disconnect
|
|
|
|
, error_code(errors::optimistic_disconnect, get_libtorrent_category()));
|
2008-05-12 07:17:11 +02:00
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
2009-05-11 22:23:47 +02:00
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
while (m_tick_residual >= 1000) m_tick_residual -= 1000;
|
2009-05-11 22:23:47 +02:00
|
|
|
// m_peer_pool.release_memory();
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
#ifdef TORRENT_STATS
|
2011-06-29 00:20:34 +02:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
void session_impl::enable_stats_logging(bool s)
|
|
|
|
{
|
|
|
|
if (m_stats_logging_enabled == s) return;
|
|
|
|
|
|
|
|
m_stats_logging_enabled = s;
|
|
|
|
|
|
|
|
reset_stat_counters();
|
2011-04-06 09:33:10 +02:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
if (m_stats_logger) fclose(m_stats_logger);
|
2011-04-06 09:58:29 +02:00
|
|
|
m_stats_logger = 0;
|
2011-04-06 09:33:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rotate_stats_log();
|
2011-10-17 19:12:08 +02:00
|
|
|
get_thread_cpu_usage(&m_network_thread_cpu_usage);
|
2011-04-06 09:33:10 +02:00
|
|
|
}
|
2011-04-06 08:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::reset_stat_counters()
|
|
|
|
{
|
|
|
|
m_end_game_piece_picker_blocks = 0;
|
|
|
|
m_piece_picker_blocks = 0;
|
|
|
|
m_piece_picks = 0;
|
|
|
|
m_reject_piece_picks = 0;
|
|
|
|
m_unchoke_piece_picks = 0;
|
|
|
|
m_incoming_redundant_piece_picks = 0;
|
|
|
|
m_incoming_piece_picks = 0;
|
|
|
|
m_end_game_piece_picks = 0;
|
|
|
|
m_snubbed_piece_picks = 0;
|
|
|
|
m_connection_attempts = 0;
|
|
|
|
m_num_banned_peers = 0;
|
|
|
|
m_banned_for_hash_failure = 0;
|
2011-10-17 07:17:21 +02:00
|
|
|
|
2012-03-09 07:24:01 +01:00
|
|
|
m_piece_requests = 0;
|
|
|
|
m_max_piece_requests = 0;
|
|
|
|
m_invalid_piece_requests = 0;
|
|
|
|
m_choked_piece_requests = 0;
|
|
|
|
m_cancelled_piece_requests = 0;
|
|
|
|
m_piece_rejects = 0;
|
|
|
|
|
2011-10-17 07:17:21 +02:00
|
|
|
memset(m_num_messages, 0, sizeof(m_num_messages));
|
2011-10-17 08:54:02 +02:00
|
|
|
memset(m_send_buffer_sizes, 0, sizeof(m_send_buffer_sizes));
|
|
|
|
memset(m_recv_buffer_sizes, 0, sizeof(m_recv_buffer_sizes));
|
2011-04-06 08:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::print_log_line(int tick_interval_ms, ptime now)
|
|
|
|
{
|
|
|
|
int connect_candidates = 0;
|
2012-03-11 06:35:29 +01:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
int checking_torrents = 0;
|
|
|
|
int stopped_torrents = 0;
|
|
|
|
int upload_only_torrents = 0;
|
2012-03-11 06:35:29 +01:00
|
|
|
int downloading_torrents = 0;
|
|
|
|
int seeding_torrents = 0;
|
|
|
|
int queued_seed_torrents = 0;
|
|
|
|
int queued_download_torrents = 0;
|
|
|
|
int error_torrents = 0;
|
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
int num_peers = 0;
|
|
|
|
int peer_dl_rate_buckets[7];
|
|
|
|
int peer_ul_rate_buckets[7];
|
|
|
|
memset(peer_dl_rate_buckets, 0, sizeof(peer_dl_rate_buckets));
|
|
|
|
memset(peer_ul_rate_buckets, 0, sizeof(peer_ul_rate_buckets));
|
|
|
|
int outstanding_requests = 0;
|
|
|
|
int outstanding_end_game_requests = 0;
|
|
|
|
int outstanding_write_blocks = 0;
|
|
|
|
|
|
|
|
int peers_up_interested = 0;
|
|
|
|
int peers_down_interesting = 0;
|
|
|
|
int peers_up_requests = 0;
|
|
|
|
int peers_down_requests = 0;
|
2012-03-09 18:03:10 +01:00
|
|
|
int peers_up_send_buffer = 0;
|
2011-04-06 08:27:42 +02:00
|
|
|
|
2012-03-06 11:34:18 +01:00
|
|
|
// number of torrents that want more peers
|
|
|
|
int num_want_more_peers = 0;
|
|
|
|
|
|
|
|
// number of peers among torrents with a peer limit
|
|
|
|
int num_limited_peers = 0;
|
|
|
|
// sum of limits of all torrents with a peer limit
|
2014-03-25 10:17:48 +01:00
|
|
|
boost::uint64_t total_peers_limit = 0;
|
2012-03-06 11:34:18 +01:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
std::vector<partial_piece_info> dq;
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
2012-03-11 06:35:29 +01:00
|
|
|
torrent* t = i->second.get();
|
|
|
|
int connection_slots = (std::max)(t->max_connections() - t->num_peers(), 0);
|
|
|
|
int candidates = t->get_policy().num_connect_candidates();
|
2011-04-06 08:27:42 +02:00
|
|
|
connect_candidates += (std::min)(candidates, connection_slots);
|
2012-03-11 06:35:29 +01:00
|
|
|
num_peers += t->get_policy().num_peers();
|
2012-03-06 11:34:18 +01:00
|
|
|
|
2012-03-11 06:35:29 +01:00
|
|
|
if (t->want_more_peers()) ++num_want_more_peers;
|
|
|
|
if (t->max_connections() > 0)
|
2012-03-06 11:34:18 +01:00
|
|
|
{
|
2012-03-11 06:35:29 +01:00
|
|
|
num_limited_peers += t->num_peers();
|
2014-03-25 10:17:48 +01:00
|
|
|
total_peers_limit += t->max_connections();
|
2012-03-06 11:34:18 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 06:35:29 +01:00
|
|
|
if (t->has_error())
|
2011-04-06 08:27:42 +02:00
|
|
|
++error_torrents;
|
2012-03-11 06:35:29 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (t->is_paused())
|
|
|
|
{
|
|
|
|
if (!t->is_auto_managed())
|
|
|
|
++stopped_torrents;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (t->is_seed())
|
|
|
|
++queued_seed_torrents;
|
|
|
|
else
|
|
|
|
++queued_download_torrents;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i->second->state() == torrent_status::checking_files
|
|
|
|
|| i->second->state() == torrent_status::queued_for_checking)
|
|
|
|
++checking_torrents;
|
|
|
|
else if (i->second->is_seed())
|
|
|
|
++seeding_torrents;
|
|
|
|
else if (i->second->is_upload_only())
|
|
|
|
++upload_only_torrents;
|
|
|
|
else
|
|
|
|
++downloading_torrents;
|
|
|
|
}
|
|
|
|
}
|
2011-04-06 08:27:42 +02:00
|
|
|
|
|
|
|
dq.clear();
|
2011-11-21 07:21:06 +01:00
|
|
|
i->second->get_download_queue(&dq);
|
2011-04-06 08:27:42 +02:00
|
|
|
for (std::vector<partial_piece_info>::iterator j = dq.begin()
|
|
|
|
, end(dq.end()); j != end; ++j)
|
|
|
|
{
|
|
|
|
for (int k = 0; k < j->blocks_in_piece; ++k)
|
|
|
|
{
|
|
|
|
block_info& bi = j->blocks[k];
|
|
|
|
if (bi.state == block_info::requested)
|
|
|
|
{
|
|
|
|
++outstanding_requests;
|
|
|
|
if (bi.num_peers > 1) ++outstanding_end_game_requests;
|
|
|
|
}
|
|
|
|
else if (bi.state == block_info::writing)
|
|
|
|
++outstanding_write_blocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-20 06:33:46 +02:00
|
|
|
int tcp_up_rate = 0;
|
|
|
|
int tcp_down_rate = 0;
|
|
|
|
int utp_up_rate = 0;
|
|
|
|
int utp_down_rate = 0;
|
2011-06-21 09:44:13 +02:00
|
|
|
int utp_peak_send_delay = 0;
|
2011-09-28 02:03:12 +02:00
|
|
|
int utp_peak_recv_delay = 0;
|
2011-06-21 09:44:13 +02:00
|
|
|
boost::uint64_t utp_send_delay_sum = 0;
|
2011-09-28 02:03:12 +02:00
|
|
|
boost::uint64_t utp_recv_delay_sum = 0;
|
2012-03-02 09:52:54 +01:00
|
|
|
int num_utp_peers = 0;
|
|
|
|
int num_tcp_peers = 0;
|
2011-06-21 09:44:13 +02:00
|
|
|
int utp_num_delay_sockets = 0;
|
2011-09-28 02:03:12 +02:00
|
|
|
int utp_num_recv_delay_sockets = 0;
|
2011-04-06 08:27:42 +02:00
|
|
|
int num_complete_connections = 0;
|
|
|
|
int num_half_open = 0;
|
|
|
|
int peers_down_unchoked = 0;
|
|
|
|
int peers_up_unchoked = 0;
|
2011-06-13 18:20:21 +02:00
|
|
|
int num_end_game_peers = 0;
|
2011-09-23 22:57:42 +02:00
|
|
|
int reading_bytes = 0;
|
2012-03-05 11:05:20 +01:00
|
|
|
int pending_incoming_reqs = 0;
|
2012-03-09 18:03:10 +01:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
for (connection_map::iterator i = m_connections.begin()
|
|
|
|
, end(m_connections.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
peer_connection* p = i->get();
|
|
|
|
if (p->is_connecting())
|
|
|
|
{
|
|
|
|
++num_half_open;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++num_complete_connections;
|
|
|
|
if (!p->is_choked()) ++peers_up_unchoked;
|
|
|
|
if (!p->has_peer_choked()) ++peers_down_unchoked;
|
|
|
|
if (!p->download_queue().empty()) ++peers_down_requests;
|
|
|
|
if (p->is_peer_interested()) ++peers_up_interested;
|
|
|
|
if (p->is_interesting()) ++peers_down_interesting;
|
2012-03-09 02:22:45 +01:00
|
|
|
if (p->send_buffer_size() > 100 || !p->upload_queue().empty() || p->num_reading_bytes() > 0)
|
2011-04-06 08:27:42 +02:00
|
|
|
++peers_up_requests;
|
2011-06-16 02:28:36 +02:00
|
|
|
if (p->endgame()) ++num_end_game_peers;
|
2011-09-23 22:57:42 +02:00
|
|
|
reading_bytes += p->num_reading_bytes();
|
2012-03-05 11:05:20 +01:00
|
|
|
|
|
|
|
pending_incoming_reqs += int(p->upload_queue().size());
|
2011-04-06 08:27:42 +02:00
|
|
|
|
|
|
|
int dl_bucket = 0;
|
|
|
|
int dl_rate = p->statistics().download_payload_rate();
|
|
|
|
if (dl_rate == 0) dl_bucket = 0;
|
|
|
|
else if (dl_rate < 2000) dl_bucket = 1;
|
|
|
|
else if (dl_rate < 5000) dl_bucket = 2;
|
|
|
|
else if (dl_rate < 10000) dl_bucket = 3;
|
|
|
|
else if (dl_rate < 50000) dl_bucket = 4;
|
|
|
|
else if (dl_rate < 100000) dl_bucket = 5;
|
|
|
|
else dl_bucket = 6;
|
|
|
|
|
|
|
|
int ul_rate = p->statistics().upload_payload_rate();
|
|
|
|
int ul_bucket = 0;
|
|
|
|
if (ul_rate == 0) ul_bucket = 0;
|
|
|
|
else if (ul_rate < 2000) ul_bucket = 1;
|
|
|
|
else if (ul_rate < 5000) ul_bucket = 2;
|
|
|
|
else if (ul_rate < 10000) ul_bucket = 3;
|
|
|
|
else if (ul_rate < 50000) ul_bucket = 4;
|
|
|
|
else if (ul_rate < 100000) ul_bucket = 5;
|
|
|
|
else ul_bucket = 6;
|
|
|
|
|
|
|
|
++peer_dl_rate_buckets[dl_bucket];
|
|
|
|
++peer_ul_rate_buckets[ul_bucket];
|
2011-06-20 06:33:46 +02:00
|
|
|
|
2012-03-09 18:03:10 +01:00
|
|
|
boost::uint64_t upload_rate = int(p->statistics().upload_rate());
|
|
|
|
int buffer_size_watermark = upload_rate
|
|
|
|
* m_settings.send_buffer_watermark_factor / 100;
|
|
|
|
if (buffer_size_watermark < m_settings.send_buffer_low_watermark)
|
|
|
|
buffer_size_watermark = m_settings.send_buffer_low_watermark;
|
|
|
|
else if (buffer_size_watermark > m_settings.send_buffer_watermark)
|
|
|
|
buffer_size_watermark = m_settings.send_buffer_watermark;
|
|
|
|
if (p->send_buffer_size() + p->num_reading_bytes() >= buffer_size_watermark)
|
|
|
|
++peers_up_send_buffer;
|
|
|
|
|
2011-06-21 09:44:13 +02:00
|
|
|
utp_stream* utp_socket = p->get_socket()->get<utp_stream>();
|
2012-10-05 05:20:40 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2012-11-11 06:07:19 +01:00
|
|
|
if (!utp_socket)
|
|
|
|
{
|
|
|
|
ssl_stream<utp_stream>* ssl_str = p->get_socket()->get<ssl_stream<utp_stream> >();
|
|
|
|
if (ssl_str) utp_socket = &ssl_str->next_layer();
|
|
|
|
}
|
2012-10-05 05:20:40 +02:00
|
|
|
#endif
|
2011-06-21 09:44:13 +02:00
|
|
|
if (utp_socket)
|
2011-06-20 06:33:46 +02:00
|
|
|
{
|
|
|
|
utp_up_rate += ul_rate;
|
2011-06-21 09:44:13 +02:00
|
|
|
utp_down_rate += dl_rate;
|
|
|
|
int send_delay = utp_socket->send_delay();
|
2011-09-28 02:03:12 +02:00
|
|
|
int recv_delay = utp_socket->recv_delay();
|
2011-06-21 09:44:13 +02:00
|
|
|
utp_peak_send_delay = (std::max)(utp_peak_send_delay, send_delay);
|
2011-09-28 02:03:12 +02:00
|
|
|
utp_peak_recv_delay = (std::max)(utp_peak_recv_delay, recv_delay);
|
2011-06-21 09:44:13 +02:00
|
|
|
if (send_delay > 0)
|
|
|
|
{
|
|
|
|
utp_send_delay_sum += send_delay;
|
|
|
|
++utp_num_delay_sockets;
|
|
|
|
}
|
2011-09-28 02:03:12 +02:00
|
|
|
if (recv_delay > 0)
|
|
|
|
{
|
|
|
|
utp_recv_delay_sum += recv_delay;
|
|
|
|
++utp_num_recv_delay_sockets;
|
|
|
|
}
|
2012-03-02 09:52:54 +01:00
|
|
|
++num_utp_peers;
|
2011-06-20 06:33:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcp_up_rate += ul_rate;
|
|
|
|
tcp_down_rate += dl_rate;
|
2012-03-02 09:52:54 +01:00
|
|
|
++num_tcp_peers;
|
2011-06-20 06:33:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int low_watermark = m_settings.max_queued_disk_bytes_low_watermark == 0
|
2011-09-14 08:22:45 +02:00
|
|
|
|| m_settings.max_queued_disk_bytes_low_watermark >= m_settings.max_queued_disk_bytes
|
|
|
|
? size_type(m_settings.max_queued_disk_bytes) * 7 / 8
|
2011-04-06 08:27:42 +02:00
|
|
|
: m_settings.max_queued_disk_bytes_low_watermark;
|
|
|
|
|
|
|
|
if (now - m_last_log_rotation > hours(1))
|
|
|
|
rotate_stats_log();
|
2011-06-29 00:20:34 +02:00
|
|
|
|
|
|
|
// system memory stats
|
|
|
|
vm_statistics_data_t vm_stat;
|
|
|
|
get_vm_stats(&vm_stat);
|
2011-10-17 19:12:08 +02:00
|
|
|
thread_cpu_usage cur_cpu_usage;
|
|
|
|
get_thread_cpu_usage(&cur_cpu_usage);
|
2011-06-29 00:20:34 +02:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
if (m_stats_logger)
|
|
|
|
{
|
|
|
|
cache_status cs = m_disk_thread.status();
|
2012-01-17 04:11:16 +01:00
|
|
|
session_status sst = status();
|
2011-04-06 08:27:42 +02:00
|
|
|
|
2011-07-03 19:21:45 +02:00
|
|
|
m_read_ops.add_sample((cs.reads - m_last_cache_status.reads) * 1000.0 / float(tick_interval_ms));
|
|
|
|
m_write_ops.add_sample((cs.writes - m_last_cache_status.writes) * 1000.0 / float(tick_interval_ms));
|
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
int total_job_time = cs.cumulative_job_time == 0 ? 1 : cs.cumulative_job_time;
|
|
|
|
|
2013-10-02 23:51:30 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
2013-10-03 17:39:19 +02:00
|
|
|
#define STAT_LOGL(type, val) VALGRIND_CHECK_VALUE_IS_DEFINED(val); fprintf(m_stats_logger, "%" #type "\t", val)
|
2013-10-02 23:51:30 +02:00
|
|
|
#else
|
2013-10-03 17:39:19 +02:00
|
|
|
#define STAT_LOGL(type, val) fprintf(m_stats_logger, "%" #type "\t", val)
|
2013-10-02 23:51:30 +02:00
|
|
|
#endif
|
2013-10-03 17:39:19 +02:00
|
|
|
#define STAT_LOG(type, val) fprintf(m_stats_logger, "%" #type "\t", val)
|
2011-06-26 20:43:23 +02:00
|
|
|
|
2011-06-26 21:45:33 +02:00
|
|
|
STAT_LOG(f, total_milliseconds(now - m_last_log_rotation) / 1000.f);
|
2012-01-05 22:11:55 +01:00
|
|
|
size_type uploaded = m_stat.total_upload() - m_last_uploaded;
|
|
|
|
STAT_LOG(d, int(uploaded));
|
|
|
|
size_type downloaded = m_stat.total_download() - m_last_downloaded;
|
|
|
|
STAT_LOG(d, int(downloaded));
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, downloading_torrents);
|
|
|
|
STAT_LOGL(d, seeding_torrents);
|
|
|
|
STAT_LOGL(d, num_complete_connections);
|
|
|
|
STAT_LOGL(d, num_half_open);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(d, m_disk_thread.disk_allocations());
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, num_peers);
|
|
|
|
STAT_LOGL(d, logging_allocator::allocations);
|
|
|
|
STAT_LOGL(d, logging_allocator::allocated_bytes);
|
|
|
|
STAT_LOGL(d, checking_torrents);
|
|
|
|
STAT_LOGL(d, stopped_torrents);
|
|
|
|
STAT_LOGL(d, upload_only_torrents);
|
|
|
|
STAT_LOGL(d, queued_seed_torrents);
|
|
|
|
STAT_LOGL(d, queued_download_torrents);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(d, m_upload_rate.queue_size());
|
|
|
|
STAT_LOG(d, m_download_rate.queue_size());
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, m_disk_queues[peer_connection::upload_channel]);
|
|
|
|
STAT_LOGL(d, m_disk_queues[peer_connection::download_channel]);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(d, m_stat.upload_rate());
|
|
|
|
STAT_LOG(d, m_stat.download_rate());
|
|
|
|
STAT_LOG(d, int(m_disk_thread.queue_buffer_size()));
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, peer_dl_rate_buckets[0]);
|
|
|
|
STAT_LOGL(d, peer_dl_rate_buckets[1]);
|
|
|
|
STAT_LOGL(d, peer_dl_rate_buckets[2]);
|
|
|
|
STAT_LOGL(d, peer_dl_rate_buckets[3]);
|
|
|
|
STAT_LOGL(d, peer_dl_rate_buckets[4]);
|
|
|
|
STAT_LOGL(d, peer_dl_rate_buckets[5]);
|
|
|
|
STAT_LOGL(d, peer_dl_rate_buckets[6]);
|
|
|
|
STAT_LOGL(d, peer_ul_rate_buckets[0]);
|
|
|
|
STAT_LOGL(d, peer_ul_rate_buckets[1]);
|
|
|
|
STAT_LOGL(d, peer_ul_rate_buckets[2]);
|
|
|
|
STAT_LOGL(d, peer_ul_rate_buckets[3]);
|
|
|
|
STAT_LOGL(d, peer_ul_rate_buckets[4]);
|
|
|
|
STAT_LOGL(d, peer_ul_rate_buckets[5]);
|
|
|
|
STAT_LOGL(d, peer_ul_rate_buckets[6]);
|
|
|
|
STAT_LOGL(d, m_error_peers);
|
|
|
|
STAT_LOGL(d, peers_down_interesting);
|
|
|
|
STAT_LOGL(d, peers_down_unchoked);
|
|
|
|
STAT_LOGL(d, peers_down_requests);
|
|
|
|
STAT_LOGL(d, peers_up_interested);
|
|
|
|
STAT_LOGL(d, peers_up_unchoked);
|
|
|
|
STAT_LOGL(d, peers_up_requests);
|
|
|
|
STAT_LOGL(d, m_disconnected_peers);
|
|
|
|
STAT_LOGL(d, m_eof_peers);
|
|
|
|
STAT_LOGL(d, m_connreset_peers);
|
|
|
|
STAT_LOGL(d, outstanding_requests);
|
|
|
|
STAT_LOGL(d, outstanding_end_game_requests);
|
|
|
|
STAT_LOGL(d, outstanding_write_blocks);
|
|
|
|
STAT_LOGL(d, m_end_game_piece_picker_blocks);
|
|
|
|
STAT_LOGL(d, m_piece_picker_blocks);
|
|
|
|
STAT_LOGL(d, m_piece_picks);
|
|
|
|
STAT_LOGL(d, m_reject_piece_picks);
|
|
|
|
STAT_LOGL(d, m_unchoke_piece_picks);
|
|
|
|
STAT_LOGL(d, m_incoming_redundant_piece_picks);
|
|
|
|
STAT_LOGL(d, m_incoming_piece_picks);
|
|
|
|
STAT_LOGL(d, m_end_game_piece_picks);
|
|
|
|
STAT_LOGL(d, m_snubbed_piece_picks);
|
|
|
|
STAT_LOGL(d, m_connect_timeouts);
|
|
|
|
STAT_LOGL(d, m_uninteresting_peers);
|
|
|
|
STAT_LOGL(d, m_timeout_peers);
|
2012-04-02 07:30:15 +02:00
|
|
|
STAT_LOG(f, (float(m_total_failed_bytes) * 100.f / (m_stat.total_payload_download() == 0 ? 1 : m_stat.total_payload_download())));
|
|
|
|
STAT_LOG(f, (float(m_total_redundant_bytes) * 100.f / (m_stat.total_payload_download() == 0 ? 1 : m_stat.total_payload_download())));
|
|
|
|
STAT_LOG(f, (float(m_stat.total_protocol_download()) * 100.f / (m_stat.total_download() == 0 ? 1 : m_stat.total_download())));
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(f, float(cs.average_read_time) / 1000000.f);
|
|
|
|
STAT_LOG(f, float(cs.average_write_time) / 1000000.f);
|
|
|
|
STAT_LOG(f, float(cs.average_queue_time) / 1000000.f);
|
|
|
|
STAT_LOG(d, int(cs.job_queue_length));
|
|
|
|
STAT_LOG(d, int(cs.queued_bytes));
|
|
|
|
STAT_LOG(d, int(cs.blocks_read_hit - m_last_cache_status.blocks_read_hit));
|
|
|
|
STAT_LOG(d, int(cs.blocks_read - m_last_cache_status.blocks_read));
|
|
|
|
STAT_LOG(d, int(cs.blocks_written - m_last_cache_status.blocks_written));
|
|
|
|
STAT_LOG(d, int(m_total_failed_bytes - m_last_failed));
|
|
|
|
STAT_LOG(d, int(m_total_redundant_bytes - m_last_redundant));
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, error_torrents);
|
|
|
|
STAT_LOGL(d, cs.read_cache_size);
|
|
|
|
STAT_LOGL(d, cs.cache_size);
|
|
|
|
STAT_LOGL(d, cs.total_used_buffers);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(f, float(cs.average_hash_time) / 1000000.f);
|
|
|
|
STAT_LOG(f, float(cs.average_job_time) / 1000000.f);
|
|
|
|
STAT_LOG(f, float(cs.average_sort_time) / 1000000.f);
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, m_connection_attempts);
|
|
|
|
STAT_LOGL(d, m_num_banned_peers);
|
|
|
|
STAT_LOGL(d, m_banned_for_hash_failure);
|
|
|
|
STAT_LOGL(d, m_settings.cache_size);
|
|
|
|
STAT_LOGL(d, m_settings.connections_limit);
|
|
|
|
STAT_LOGL(d, connect_candidates);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(d, int(m_settings.max_queued_disk_bytes));
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, low_watermark);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(f, float(cs.cumulative_read_time * 100.f / total_job_time));
|
|
|
|
STAT_LOG(f, float(cs.cumulative_write_time * 100.f / total_job_time));
|
|
|
|
STAT_LOG(f, float(cs.cumulative_hash_time * 100.f / total_job_time));
|
|
|
|
STAT_LOG(f, float(cs.cumulative_sort_time * 100.f / total_job_time));
|
|
|
|
STAT_LOG(d, int(cs.total_read_back - m_last_cache_status.total_read_back));
|
|
|
|
STAT_LOG(f, float(cs.total_read_back * 100.f / (cs.blocks_written == 0 ? 1: cs.blocks_written)));
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, cs.read_queue_size);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(f, float(tick_interval_ms) / 1000.f);
|
|
|
|
STAT_LOG(f, float(m_tick_residual) / 1000.f);
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, m_allowed_upload_slots);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(d, m_settings.unchoke_slots_limit * 2);
|
|
|
|
STAT_LOG(d, m_stat.low_pass_upload_rate());
|
|
|
|
STAT_LOG(d, m_stat.low_pass_download_rate());
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, num_end_game_peers);
|
|
|
|
STAT_LOGL(d, tcp_up_rate);
|
|
|
|
STAT_LOGL(d, tcp_down_rate);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(d, int(m_tcp_upload_channel.throttle()));
|
|
|
|
STAT_LOG(d, int(m_tcp_download_channel.throttle()));
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, utp_up_rate);
|
|
|
|
STAT_LOGL(d, utp_down_rate);
|
2011-06-26 20:43:23 +02:00
|
|
|
STAT_LOG(f, float(utp_peak_send_delay) / 1000000.f);
|
|
|
|
STAT_LOG(f, float(utp_num_delay_sockets ? float(utp_send_delay_sum) / float(utp_num_delay_sockets) : 0) / 1000000.f);
|
2011-09-28 02:03:12 +02:00
|
|
|
STAT_LOG(f, float(utp_peak_recv_delay) / 1000000.f);
|
|
|
|
STAT_LOG(f, float(utp_num_recv_delay_sockets ? float(utp_recv_delay_sum) / float(utp_num_recv_delay_sockets) : 0) / 1000000.f);
|
2011-06-26 21:45:33 +02:00
|
|
|
STAT_LOG(f, float(cs.reads - m_last_cache_status.reads) * 1000.0 / float(tick_interval_ms));
|
|
|
|
STAT_LOG(f, float(cs.writes - m_last_cache_status.writes) * 1000.0 / float(tick_interval_ms));
|
2011-06-29 00:20:34 +02:00
|
|
|
|
|
|
|
STAT_LOG(d, int(vm_stat.active_count));
|
|
|
|
STAT_LOG(d, int(vm_stat.inactive_count));
|
|
|
|
STAT_LOG(d, int(vm_stat.wire_count));
|
|
|
|
STAT_LOG(d, int(vm_stat.free_count));
|
|
|
|
STAT_LOG(d, int(vm_stat.pageins - m_last_vm_stat.pageins));
|
|
|
|
STAT_LOG(d, int(vm_stat.pageouts - m_last_vm_stat.pageouts));
|
|
|
|
STAT_LOG(d, int(vm_stat.faults - m_last_vm_stat.faults));
|
|
|
|
|
2011-07-03 19:21:45 +02:00
|
|
|
STAT_LOG(d, m_read_ops.mean());
|
|
|
|
STAT_LOG(d, m_write_ops.mean());
|
|
|
|
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, reading_bytes);
|
2011-09-23 22:57:42 +02:00
|
|
|
|
2011-10-17 07:17:21 +02:00
|
|
|
for (int i = 0; i < max_messages; ++i)
|
2013-10-03 17:39:19 +02:00
|
|
|
{
|
|
|
|
STAT_LOGL(d, m_num_messages[i]);
|
|
|
|
}
|
2011-10-17 08:54:02 +02:00
|
|
|
int num_max = sizeof(m_send_buffer_sizes)/sizeof(m_send_buffer_sizes[0]);
|
|
|
|
for (int i = 0; i < num_max; ++i)
|
2013-10-03 17:39:19 +02:00
|
|
|
{
|
|
|
|
STAT_LOGL(d, m_send_buffer_sizes[i]);
|
|
|
|
}
|
2011-10-17 08:54:02 +02:00
|
|
|
num_max = sizeof(m_recv_buffer_sizes)/sizeof(m_recv_buffer_sizes[0]);
|
|
|
|
for (int i = 0; i < num_max; ++i)
|
2013-10-03 17:39:19 +02:00
|
|
|
{
|
|
|
|
STAT_LOGL(d, m_recv_buffer_sizes[i]);
|
|
|
|
}
|
2011-10-17 07:17:21 +02:00
|
|
|
|
2012-03-09 02:22:45 +01:00
|
|
|
STAT_LOG(f, total_microseconds(cur_cpu_usage.user_time
|
|
|
|
- m_network_thread_cpu_usage.user_time) / double(tick_interval_ms * 10));
|
|
|
|
STAT_LOG(f, (total_microseconds(cur_cpu_usage.system_time
|
2011-10-17 19:12:08 +02:00
|
|
|
- m_network_thread_cpu_usage.system_time)
|
2012-03-09 02:22:45 +01:00
|
|
|
+ total_microseconds(cur_cpu_usage.user_time
|
|
|
|
- m_network_thread_cpu_usage.user_time))
|
|
|
|
/ double(tick_interval_ms * 10));
|
2011-10-17 19:12:08 +02:00
|
|
|
|
2011-11-16 03:29:59 +01:00
|
|
|
for (int i = 0; i < torrent::waste_reason_max; ++i)
|
2013-10-03 17:39:19 +02:00
|
|
|
{
|
2012-04-02 07:30:15 +02:00
|
|
|
STAT_LOG(f, (m_redundant_bytes[i] * 100.) / double(m_total_redundant_bytes == 0 ? 1 : m_total_redundant_bytes));
|
2013-10-03 17:39:19 +02:00
|
|
|
}
|
2011-11-16 03:29:59 +01:00
|
|
|
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, m_no_memory_peers);
|
|
|
|
STAT_LOGL(d, m_too_many_peers);
|
|
|
|
STAT_LOGL(d, m_transport_timeout_peers);
|
|
|
|
|
|
|
|
STAT_LOGL(d, sst.utp_stats.num_idle);
|
|
|
|
STAT_LOGL(d, sst.utp_stats.num_syn_sent);
|
|
|
|
STAT_LOGL(d, sst.utp_stats.num_connected);
|
|
|
|
STAT_LOGL(d, sst.utp_stats.num_fin_sent);
|
|
|
|
STAT_LOGL(d, sst.utp_stats.num_close_wait);
|
|
|
|
|
|
|
|
STAT_LOGL(d, num_tcp_peers);
|
|
|
|
STAT_LOGL(d, num_utp_peers);
|
|
|
|
|
|
|
|
STAT_LOGL(d, m_connrefused_peers);
|
|
|
|
STAT_LOGL(d, m_connaborted_peers);
|
|
|
|
STAT_LOGL(d, m_perm_peers);
|
|
|
|
STAT_LOGL(d, m_buffer_peers);
|
|
|
|
STAT_LOGL(d, m_unreachable_peers);
|
|
|
|
STAT_LOGL(d, m_broken_pipe_peers);
|
|
|
|
STAT_LOGL(d, m_addrinuse_peers);
|
|
|
|
STAT_LOGL(d, m_no_access_peers);
|
|
|
|
STAT_LOGL(d, m_invalid_arg_peers);
|
|
|
|
STAT_LOGL(d, m_aborted_peers);
|
|
|
|
|
|
|
|
STAT_LOGL(d, m_error_incoming_peers);
|
|
|
|
STAT_LOGL(d, m_error_outgoing_peers);
|
|
|
|
STAT_LOGL(d, m_error_rc4_peers);
|
|
|
|
STAT_LOGL(d, m_error_encrypted_peers);
|
|
|
|
STAT_LOGL(d, m_error_tcp_peers);
|
|
|
|
STAT_LOGL(d, m_error_utp_peers);
|
2012-03-02 09:52:54 +01:00
|
|
|
|
2012-03-04 12:18:27 +01:00
|
|
|
STAT_LOG(d, int(m_connections.size()));
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, pending_incoming_reqs);
|
2012-03-05 11:05:20 +01:00
|
|
|
STAT_LOG(f, num_complete_connections == 0 ? 0.f : (float(pending_incoming_reqs) / num_complete_connections));
|
2012-03-04 12:18:27 +01:00
|
|
|
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, num_want_more_peers);
|
2012-03-06 11:34:18 +01:00
|
|
|
STAT_LOG(f, total_peers_limit == 0 ? 0 : float(num_limited_peers) / total_peers_limit);
|
|
|
|
|
2013-10-03 17:39:19 +02:00
|
|
|
STAT_LOGL(d, m_piece_requests);
|
|
|
|
STAT_LOGL(d, m_max_piece_requests);
|
|
|
|
STAT_LOGL(d, m_invalid_piece_requests);
|
|
|
|
STAT_LOGL(d, m_choked_piece_requests);
|
|
|
|
STAT_LOGL(d, m_cancelled_piece_requests);
|
|
|
|
STAT_LOGL(d, m_piece_rejects);
|
|
|
|
|
|
|
|
STAT_LOGL(d, peers_up_send_buffer);
|
|
|
|
|
2014-03-25 08:19:21 +01:00
|
|
|
STAT_LOG(d, int(sst.utp_stats.packet_loss));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.timeout));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.packets_in));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.packets_out));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.fast_retransmit));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.packet_resend));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.samples_above_target));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.samples_below_target));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.payload_pkts_in));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.payload_pkts_out));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.invalid_pkts_in));
|
|
|
|
STAT_LOG(d, int(sst.utp_stats.redundant_pkts_in));
|
2013-09-14 12:06:48 +02:00
|
|
|
|
2011-06-26 20:43:23 +02:00
|
|
|
fprintf(m_stats_logger, "\n");
|
|
|
|
|
|
|
|
#undef STAT_LOG
|
2013-10-03 17:39:19 +02:00
|
|
|
#undef STAT_LOGL
|
2011-06-26 20:43:23 +02:00
|
|
|
|
2011-04-06 08:27:42 +02:00
|
|
|
m_last_cache_status = cs;
|
2011-06-29 00:20:34 +02:00
|
|
|
m_last_vm_stat = vm_stat;
|
2011-10-17 19:12:08 +02:00
|
|
|
m_network_thread_cpu_usage = cur_cpu_usage;
|
2011-04-06 08:27:42 +02:00
|
|
|
m_last_failed = m_total_failed_bytes;
|
|
|
|
m_last_redundant = m_total_redundant_bytes;
|
|
|
|
m_last_uploaded = m_stat.total_upload();
|
|
|
|
m_last_downloaded = m_stat.total_download();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset_stat_counters();
|
|
|
|
}
|
|
|
|
#endif // TORRENT_STATS
|
|
|
|
|
2011-01-18 04:41:54 +01:00
|
|
|
void session_impl::update_rss_feeds()
|
|
|
|
{
|
|
|
|
time_t now_posix = time(0);
|
|
|
|
ptime min_update = max_time();
|
|
|
|
ptime now = time_now();
|
|
|
|
for (std::vector<boost::shared_ptr<feed> >::iterator i
|
|
|
|
= m_feeds.begin(), end(m_feeds.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
feed& f = **i;
|
|
|
|
int delta = f.next_update(now_posix);
|
|
|
|
if (delta <= 0)
|
2012-02-22 09:26:42 +01:00
|
|
|
delta = f.update_feed();
|
|
|
|
TORRENT_ASSERT(delta >= 0);
|
2011-01-18 04:41:54 +01:00
|
|
|
ptime next_update = now + seconds(delta);
|
|
|
|
if (next_update < min_update) min_update = next_update;
|
|
|
|
}
|
|
|
|
m_next_rss_update = min_update;
|
|
|
|
}
|
|
|
|
|
2012-11-03 04:50:12 +01:00
|
|
|
void session_impl::prioritize_connections(boost::weak_ptr<torrent> t)
|
|
|
|
{
|
|
|
|
m_prio_torrents.push_back(std::make_pair(t, 10));
|
|
|
|
}
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
|
2012-11-03 04:50:12 +01:00
|
|
|
void session_impl::add_dht_node(udp::endpoint n)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
|
|
|
if (m_dht) m_dht->add_node(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::prioritize_dht(boost::weak_ptr<torrent> t)
|
|
|
|
{
|
2013-10-10 11:08:56 +02:00
|
|
|
TORRENT_ASSERT(m_dht);
|
2012-11-03 04:50:12 +01:00
|
|
|
m_dht_torrents.push_back(t);
|
|
|
|
// trigger a DHT announce right away if we just
|
|
|
|
// added a new torrent and there's no back-log
|
|
|
|
if (m_dht_torrents.size() == 1)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_dht_announce");
|
|
|
|
#endif
|
|
|
|
error_code ec;
|
|
|
|
m_dht_announce_timer.expires_from_now(seconds(0), ec);
|
|
|
|
m_dht_announce_timer.async_wait(
|
|
|
|
bind(&session_impl::on_dht_announce, this, _1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
void session_impl::on_dht_announce(error_code const& e)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::on_dht_announce");
|
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2010-02-14 02:39:55 +01:00
|
|
|
if (e) return;
|
|
|
|
|
|
|
|
if (m_abort) return;
|
|
|
|
|
2013-10-10 11:08:56 +02:00
|
|
|
TORRENT_ASSERT(m_dht);
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_dht_announce");
|
|
|
|
#endif
|
2010-02-14 02:39:55 +01:00
|
|
|
// announce to DHT every 15 minutes
|
|
|
|
int delay = (std::max)(m_settings.dht_announce_interval
|
|
|
|
/ (std::max)(int(m_torrents.size()), 1), 1);
|
2012-11-03 04:50:12 +01:00
|
|
|
|
|
|
|
if (!m_dht_torrents.empty())
|
|
|
|
{
|
|
|
|
// we have prioritized torrents that need
|
|
|
|
// an initial DHT announce. Don't wait too long
|
|
|
|
// until we announce those.
|
|
|
|
delay = (std::min)(4, delay);
|
|
|
|
}
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
error_code ec;
|
|
|
|
m_dht_announce_timer.expires_from_now(seconds(delay), ec);
|
|
|
|
m_dht_announce_timer.async_wait(
|
|
|
|
bind(&session_impl::on_dht_announce, this, _1));
|
|
|
|
|
2012-08-03 07:13:40 +02:00
|
|
|
if (!m_dht_torrents.empty())
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
t = m_dht_torrents.front().lock();
|
|
|
|
m_dht_torrents.pop_front();
|
|
|
|
}
|
|
|
|
while (!t && !m_dht_torrents.empty());
|
|
|
|
if (t)
|
|
|
|
{
|
|
|
|
t->dht_announce();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-02-14 18:24:53 +01:00
|
|
|
if (m_torrents.empty()) return;
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
if (m_next_dht_torrent == m_torrents.end())
|
|
|
|
m_next_dht_torrent = m_torrents.begin();
|
|
|
|
m_next_dht_torrent->second->dht_announce();
|
|
|
|
++m_next_dht_torrent;
|
|
|
|
if (m_next_dht_torrent == m_torrents.end())
|
|
|
|
m_next_dht_torrent = m_torrents.begin();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-05 09:23:17 +01:00
|
|
|
void session_impl::on_lsd_announce(error_code const& e)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::on_lsd_announce");
|
2011-10-17 07:17:21 +02:00
|
|
|
#endif
|
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_num_messages[on_lsd_counter];
|
2010-11-28 02:47:30 +01:00
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2010-02-05 09:23:17 +01:00
|
|
|
if (e) return;
|
|
|
|
|
2010-02-13 17:29:17 +01:00
|
|
|
if (m_abort) return;
|
2010-02-05 09:23:17 +01:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_lsd_announce");
|
|
|
|
#endif
|
2010-02-05 09:23:17 +01:00
|
|
|
// announce on local network every 5 minutes
|
|
|
|
int delay = (std::max)(m_settings.local_service_announce_interval
|
|
|
|
/ (std::max)(int(m_torrents.size()), 1), 1);
|
|
|
|
error_code ec;
|
|
|
|
m_lsd_announce_timer.expires_from_now(seconds(delay), ec);
|
|
|
|
m_lsd_announce_timer.async_wait(
|
|
|
|
bind(&session_impl::on_lsd_announce, this, _1));
|
|
|
|
|
2010-02-14 18:24:53 +01:00
|
|
|
if (m_torrents.empty()) return;
|
|
|
|
|
2010-02-13 17:29:17 +01:00
|
|
|
if (m_next_lsd_torrent == m_torrents.end())
|
|
|
|
m_next_lsd_torrent = m_torrents.begin();
|
2010-02-05 09:23:17 +01:00
|
|
|
m_next_lsd_torrent->second->lsd_announce();
|
|
|
|
++m_next_lsd_torrent;
|
|
|
|
if (m_next_lsd_torrent == m_torrents.end())
|
|
|
|
m_next_lsd_torrent = m_torrents.begin();
|
|
|
|
}
|
|
|
|
|
2010-03-29 02:34:04 +02:00
|
|
|
void session_impl::auto_manage_torrents(std::vector<torrent*>& list
|
|
|
|
, int& dht_limit, int& tracker_limit, int& lsd_limit
|
|
|
|
, int& hard_limit, int type_limit)
|
2009-05-27 21:27:12 +02:00
|
|
|
{
|
|
|
|
for (std::vector<torrent*>::iterator i = list.begin()
|
|
|
|
, end(list.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent* t = *i;
|
2011-02-04 07:55:05 +01:00
|
|
|
|
|
|
|
if ((t->state() == torrent_status::checking_files
|
|
|
|
|| t->state() == torrent_status::queued_for_checking))
|
|
|
|
continue;
|
|
|
|
|
2014-05-23 00:07:01 +02:00
|
|
|
--dht_limit;
|
|
|
|
--lsd_limit;
|
|
|
|
--tracker_limit;
|
|
|
|
t->set_announce_to_dht(dht_limit >= 0);
|
|
|
|
t->set_announce_to_trackers(tracker_limit >= 0);
|
|
|
|
t->set_announce_to_lsd(lsd_limit >= 0);
|
|
|
|
|
|
|
|
if (!t->is_paused() && t->is_inactive()
|
|
|
|
&& hard_limit > 0)
|
|
|
|
{
|
|
|
|
// the hard limit takes inactive torrents into account, but the
|
|
|
|
// download and seed limits don't.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-05-27 21:27:12 +02:00
|
|
|
if (type_limit > 0 && hard_limit > 0)
|
|
|
|
{
|
|
|
|
--hard_limit;
|
|
|
|
--type_limit;
|
2012-09-27 19:39:41 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2012-09-28 01:50:15 +02:00
|
|
|
t->log_to_all_peers("AUTO MANAGER STARTING TORRENT");
|
2010-08-18 19:14:40 +02:00
|
|
|
#endif
|
2010-03-31 04:40:00 +02:00
|
|
|
t->set_allow_peers(true);
|
2009-05-27 21:27:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-27 19:39:41 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2012-09-28 01:50:15 +02:00
|
|
|
t->log_to_all_peers("AUTO MANAGER PAUSING TORRENT");
|
2010-08-18 19:14:40 +02:00
|
|
|
#endif
|
2010-10-30 10:36:18 +02:00
|
|
|
// use graceful pause for auto-managed torrents
|
|
|
|
t->set_allow_peers(false, true);
|
2009-05-27 21:27:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-21 14:31:28 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
void session_impl::recalculate_auto_managed_torrents()
|
|
|
|
{
|
2012-06-21 05:51:39 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2012-11-03 04:50:12 +01:00
|
|
|
m_need_auto_manage = false;
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
// these vectors are filled with auto managed torrents
|
|
|
|
std::vector<torrent*> downloaders;
|
|
|
|
downloaders.reserve(m_torrents.size());
|
|
|
|
std::vector<torrent*> seeds;
|
|
|
|
seeds.reserve(m_torrents.size());
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
// these counters are set to the number of torrents
|
|
|
|
// of each kind we're allowed to have active
|
|
|
|
int num_downloaders = settings().active_downloads;
|
|
|
|
int num_seeds = settings().active_seeds;
|
2010-03-29 02:34:04 +02:00
|
|
|
int dht_limit = settings().active_dht_limit;
|
|
|
|
int tracker_limit = settings().active_tracker_limit;
|
|
|
|
int lsd_limit = settings().active_lsd_limit;
|
2008-06-21 14:31:28 +02:00
|
|
|
int hard_limit = settings().active_limit;
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2008-08-29 02:44:55 +02:00
|
|
|
if (num_downloaders == -1)
|
|
|
|
num_downloaders = (std::numeric_limits<int>::max)();
|
|
|
|
if (num_seeds == -1)
|
|
|
|
num_seeds = (std::numeric_limits<int>::max)();
|
|
|
|
if (hard_limit == -1)
|
|
|
|
hard_limit = (std::numeric_limits<int>::max)();
|
2012-12-31 04:12:11 +01:00
|
|
|
if (dht_limit == -1)
|
|
|
|
dht_limit = (std::numeric_limits<int>::max)();
|
|
|
|
if (lsd_limit == -1)
|
|
|
|
lsd_limit = (std::numeric_limits<int>::max)();
|
|
|
|
if (tracker_limit == -1)
|
|
|
|
tracker_limit = (std::numeric_limits<int>::max)();
|
2008-06-17 17:44:04 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent* t = i->second.get();
|
|
|
|
TORRENT_ASSERT(t);
|
2011-02-04 07:55:05 +01:00
|
|
|
|
|
|
|
// checking torrents are not subject to auto-management
|
|
|
|
if (t->state() == torrent_status::checking_files
|
|
|
|
|| t->state() == torrent_status::queued_for_checking)
|
2011-08-06 19:54:33 +02:00
|
|
|
{
|
|
|
|
if (t->is_auto_managed() && t->is_paused()) t->resume();
|
2011-02-04 07:55:05 +01:00
|
|
|
continue;
|
2011-08-06 19:54:33 +02:00
|
|
|
}
|
2008-05-20 09:57:44 +02:00
|
|
|
if (t->is_auto_managed() && !t->has_error())
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
2011-06-09 07:46:47 +02:00
|
|
|
TORRENT_ASSERT(t->m_resume_data_loaded || !t->valid_metadata());
|
2008-04-24 05:28:48 +02:00
|
|
|
// this torrent is auto managed, add it to
|
|
|
|
// the list (depending on if it's a seed or not)
|
|
|
|
if (t->is_finished())
|
|
|
|
seeds.push_back(t);
|
|
|
|
else
|
|
|
|
downloaders.push_back(t);
|
|
|
|
}
|
|
|
|
else if (!t->is_paused())
|
2008-01-13 12:18:18 +01:00
|
|
|
{
|
2011-06-09 07:46:47 +02:00
|
|
|
TORRENT_ASSERT(t->m_resume_data_loaded || !t->valid_metadata());
|
2008-06-21 14:31:28 +02:00
|
|
|
--hard_limit;
|
2008-01-13 12:18:18 +01:00
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool handled_by_extension = false;
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2013-01-21 00:21:53 +01:00
|
|
|
// TODO: 0 allow extensions to sort torrents for queuing
|
2008-04-24 05:28:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!handled_by_extension)
|
|
|
|
{
|
|
|
|
std::sort(downloaders.begin(), downloaders.end()
|
2010-07-22 18:49:40 +02:00
|
|
|
, boost::bind(&torrent::sequence_number, _1) < boost::bind(&torrent::sequence_number, _2));
|
2008-04-24 05:28:48 +02:00
|
|
|
|
|
|
|
std::sort(seeds.begin(), seeds.end()
|
2010-07-22 18:49:40 +02:00
|
|
|
, boost::bind(&torrent::seed_rank, _1, boost::ref(m_settings))
|
|
|
|
> boost::bind(&torrent::seed_rank, _2, boost::ref(m_settings)));
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
|
|
|
|
2009-05-27 21:27:12 +02:00
|
|
|
if (settings().auto_manage_prefer_seeds)
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
2010-03-29 02:34:04 +02:00
|
|
|
auto_manage_torrents(seeds, dht_limit, tracker_limit, lsd_limit
|
|
|
|
, hard_limit, num_seeds);
|
|
|
|
auto_manage_torrents(downloaders, dht_limit, tracker_limit, lsd_limit
|
|
|
|
, hard_limit, num_downloaders);
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
2009-05-27 21:27:12 +02:00
|
|
|
else
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
2010-03-29 02:34:04 +02:00
|
|
|
auto_manage_torrents(downloaders, dht_limit, tracker_limit, lsd_limit
|
|
|
|
, hard_limit, num_downloaders);
|
|
|
|
auto_manage_torrents(seeds, dht_limit, tracker_limit, lsd_limit
|
|
|
|
, hard_limit, num_seeds);
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2010-02-02 19:39:32 +01:00
|
|
|
void session_impl::recalculate_optimistic_unchoke_slots()
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2008-10-19 00:35:10 +02:00
|
|
|
if (m_allowed_upload_slots == 0) return;
|
|
|
|
|
2010-02-02 19:39:32 +01:00
|
|
|
std::vector<policy::peer*> opt_unchoke;
|
2008-10-19 00:35:10 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
for (connection_map::iterator i = m_connections.begin()
|
|
|
|
, end(m_connections.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
peer_connection* p = i->get();
|
2008-10-14 04:03:54 +02:00
|
|
|
TORRENT_ASSERT(p);
|
2008-10-19 00:35:10 +02:00
|
|
|
policy::peer* pi = p->peer_info_struct();
|
|
|
|
if (!pi) continue;
|
2012-04-12 02:02:35 +02:00
|
|
|
if (pi->web_seed) continue;
|
2008-10-19 00:35:10 +02:00
|
|
|
torrent* t = p->associated_torrent().lock().get();
|
|
|
|
if (!t) continue;
|
2010-10-30 10:36:18 +02:00
|
|
|
if (t->is_paused()) continue;
|
2008-10-19 00:35:10 +02:00
|
|
|
|
|
|
|
if (pi->optimistically_unchoked)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!p->is_choked());
|
2010-02-02 19:39:32 +01:00
|
|
|
opt_unchoke.push_back(pi);
|
2008-10-19 00:35:10 +02:00
|
|
|
}
|
|
|
|
|
2010-02-02 19:39:32 +01:00
|
|
|
if (!p->is_connecting()
|
2008-10-19 00:35:10 +02:00
|
|
|
&& !p->is_disconnecting()
|
|
|
|
&& p->is_peer_interested()
|
|
|
|
&& t->free_upload_slots()
|
|
|
|
&& p->is_choked()
|
2009-01-28 07:14:21 +01:00
|
|
|
&& !p->ignore_unchoke_slots()
|
2008-10-19 00:35:10 +02:00
|
|
|
&& t->valid_metadata())
|
|
|
|
{
|
2010-02-02 19:39:32 +01:00
|
|
|
opt_unchoke.push_back(pi);
|
2008-10-19 00:35:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-02 19:39:32 +01:00
|
|
|
// find the peers that has been waiting the longest to be optimistically
|
|
|
|
// unchoked
|
|
|
|
|
|
|
|
// avoid having a bias towards peers that happen to be sorted first
|
|
|
|
std::random_shuffle(opt_unchoke.begin(), opt_unchoke.end());
|
|
|
|
|
|
|
|
// sort all candidates based on when they were last optimistically
|
|
|
|
// unchoked.
|
|
|
|
std::sort(opt_unchoke.begin(), opt_unchoke.end()
|
|
|
|
, boost::bind(&policy::peer::last_optimistically_unchoked, _1)
|
|
|
|
< boost::bind(&policy::peer::last_optimistically_unchoked, _2));
|
|
|
|
|
2014-05-12 09:28:34 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
|
|
|
|
, end(m_ses_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_optimistic_unchoke(opt_unchoke))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-02 19:39:32 +01:00
|
|
|
int num_opt_unchoke = m_settings.num_optimistic_unchoke_slots;
|
|
|
|
if (num_opt_unchoke == 0) num_opt_unchoke = (std::max)(1, m_allowed_upload_slots / 5);
|
|
|
|
|
|
|
|
// unchoke the first num_opt_unchoke peers in the candidate set
|
|
|
|
// and make sure that the others are choked
|
|
|
|
for (std::vector<policy::peer*>::iterator i = opt_unchoke.begin()
|
|
|
|
, end(opt_unchoke.end()); i != end; ++i)
|
2008-10-19 00:35:10 +02:00
|
|
|
{
|
2010-02-02 19:39:32 +01:00
|
|
|
policy::peer* pi = *i;
|
|
|
|
if (num_opt_unchoke > 0)
|
2008-10-19 00:35:10 +02:00
|
|
|
{
|
2010-02-02 19:39:32 +01:00
|
|
|
--num_opt_unchoke;
|
|
|
|
if (!pi->optimistically_unchoked)
|
|
|
|
{
|
|
|
|
torrent* t = pi->connection->associated_torrent().lock().get();
|
2010-10-04 06:06:14 +02:00
|
|
|
bool ret = t->unchoke_peer(*pi->connection, true);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
pi->optimistically_unchoked = true;
|
|
|
|
++m_num_unchoked;
|
2014-05-10 05:23:05 +02:00
|
|
|
pi->last_optimistically_unchoked = boost::uint16_t(session_time());
|
2010-10-04 06:06:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we failed to unchoke it, increment the count again
|
|
|
|
++num_opt_unchoke;
|
|
|
|
}
|
2010-02-02 19:39:32 +01:00
|
|
|
}
|
2008-10-19 00:35:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-02 19:39:32 +01:00
|
|
|
if (pi->optimistically_unchoked)
|
|
|
|
{
|
|
|
|
torrent* t = pi->connection->associated_torrent().lock().get();
|
|
|
|
pi->optimistically_unchoked = false;
|
|
|
|
t->choke_peer(*pi->connection);
|
|
|
|
--m_num_unchoked;
|
|
|
|
}
|
2008-10-19 00:35:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 22:41:22 +02:00
|
|
|
void session_impl::try_connect_more_peers(int num_downloads, int num_downloads_peers)
|
|
|
|
{
|
|
|
|
// let torrents connect to peers if they want to
|
|
|
|
// if there are any torrents and any free slots
|
|
|
|
|
|
|
|
// this loop will "hand out" max(connection_speed
|
|
|
|
// , half_open.free_slots()) to the torrents, in a
|
|
|
|
// round robin fashion, so that every torrent is
|
|
|
|
// equally likely to connect to a peer
|
|
|
|
|
|
|
|
int free_slots = m_half_open.free_slots();
|
|
|
|
int max_connections = m_settings.connection_speed;
|
|
|
|
// boost connections are connections made by torrent connection
|
|
|
|
// boost, which are done immediately on a tracker response. These
|
|
|
|
// connections needs to be deducted from this second
|
|
|
|
if (m_boost_connections > 0)
|
|
|
|
{
|
|
|
|
if (m_boost_connections > max_connections)
|
|
|
|
{
|
|
|
|
m_boost_connections -= max_connections;
|
|
|
|
max_connections = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_connections -= m_boost_connections;
|
|
|
|
m_boost_connections = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this logic is here to smooth out the number of new connection
|
|
|
|
// attempts over time, to prevent connecting a large number of
|
|
|
|
// sockets, wait 10 seconds, and then try again
|
|
|
|
int limit = (std::min)(m_settings.connections_limit - num_connections(), free_slots);
|
|
|
|
if (m_settings.smooth_connects && max_connections > (limit+1) / 2)
|
|
|
|
max_connections = (limit+1) / 2;
|
|
|
|
|
2012-09-30 17:38:37 +02:00
|
|
|
// TODO: use a lower limit than m_settings.connections_limit
|
|
|
|
// to allocate the to 10% or so of connection slots for incoming
|
|
|
|
// connections
|
2012-07-04 22:41:22 +02:00
|
|
|
if (!m_torrents.empty()
|
|
|
|
&& free_slots > -m_half_open.limit()
|
|
|
|
&& num_connections() < m_settings.connections_limit
|
|
|
|
&& !m_abort
|
|
|
|
&& m_settings.connection_speed > 0
|
|
|
|
&& max_connections > 0)
|
|
|
|
{
|
|
|
|
// this is the maximum number of connections we will
|
|
|
|
// attempt this tick
|
2014-07-05 01:40:31 +02:00
|
|
|
// int average_peers = 0;
|
|
|
|
// if (num_downloads > 0)
|
|
|
|
// average_peers = num_downloads_peers / num_downloads;
|
2012-07-04 22:41:22 +02:00
|
|
|
|
|
|
|
if (m_next_connect_torrent == m_torrents.end())
|
|
|
|
m_next_connect_torrent = m_torrents.begin();
|
|
|
|
|
|
|
|
int steps_since_last_connect = 0;
|
|
|
|
int num_torrents = int(m_torrents.size());
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
torrent& t = *m_next_connect_torrent->second;
|
|
|
|
if (t.want_more_peers())
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(t.allows_peers());
|
|
|
|
// have a bias to give more connection attempts
|
|
|
|
// to downloading torrents than seed, and even
|
|
|
|
// more to downloading torrents with less than
|
|
|
|
// average number of connections
|
|
|
|
int num_attempts = 1;
|
|
|
|
if (!t.is_finished())
|
|
|
|
{
|
|
|
|
// TODO: make this bias configurable
|
|
|
|
// TODO: also take average_peers into account, to create a bias for downloading torrents with < average peers
|
|
|
|
TORRENT_ASSERT(m_num_active_downloading > 0);
|
|
|
|
num_attempts += m_num_active_finished / m_num_active_downloading;
|
|
|
|
}
|
|
|
|
while (m_current_connect_attempts < num_attempts)
|
|
|
|
{
|
|
|
|
TORRENT_TRY
|
|
|
|
{
|
|
|
|
++m_current_connect_attempts;
|
|
|
|
if (t.try_connect_peer())
|
|
|
|
{
|
|
|
|
--max_connections;
|
|
|
|
--free_slots;
|
|
|
|
steps_since_last_connect = 0;
|
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_connection_attempts;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TORRENT_CATCH(std::bad_alloc&)
|
|
|
|
{
|
|
|
|
// we ran out of memory trying to connect to a peer
|
|
|
|
// lower the global limit to the number of peers
|
|
|
|
// we already have
|
|
|
|
m_settings.connections_limit = num_connections();
|
|
|
|
if (m_settings.connections_limit < 2) m_settings.connections_limit = 2;
|
|
|
|
}
|
|
|
|
if (!t.want_more_peers()) break;
|
|
|
|
if (free_slots <= -m_half_open.limit()) return;
|
|
|
|
if (max_connections == 0) return;
|
|
|
|
if (num_connections() >= m_settings.connections_limit) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++m_next_connect_torrent;
|
|
|
|
m_current_connect_attempts = 0;
|
|
|
|
++steps_since_last_connect;
|
|
|
|
if (m_next_connect_torrent == m_torrents.end())
|
|
|
|
m_next_connect_torrent = m_torrents.begin();
|
|
|
|
|
|
|
|
// if we have gone a whole loop without
|
|
|
|
// handing out a single connection, break
|
|
|
|
if (steps_since_last_connect > num_torrents + 1) break;
|
|
|
|
// if there are no more free connection slots, abort
|
|
|
|
if (free_slots <= -m_half_open.limit()) break;
|
|
|
|
// if we should not make any more connections
|
|
|
|
// attempts this tick, abort
|
|
|
|
if (max_connections == 0) break;
|
|
|
|
// maintain the global limit on number of connections
|
|
|
|
if (num_connections() >= m_settings.connections_limit) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-19 00:35:10 +02:00
|
|
|
void session_impl::recalculate_unchoke_slots(int congested_torrents
|
|
|
|
, int uncongested_torrents)
|
|
|
|
{
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2008-10-19 00:35:10 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2009-04-04 09:55:34 +02:00
|
|
|
ptime now = time_now();
|
|
|
|
time_duration unchoke_interval = now - m_last_choke;
|
|
|
|
m_last_choke = now;
|
|
|
|
|
|
|
|
// build list of all peers that are
|
2011-04-30 22:33:35 +02:00
|
|
|
// unchokable.
|
2008-10-19 00:35:10 +02:00
|
|
|
std::vector<peer_connection*> peers;
|
|
|
|
for (connection_map::iterator i = m_connections.begin();
|
|
|
|
i != m_connections.end();)
|
|
|
|
{
|
|
|
|
boost::intrusive_ptr<peer_connection> p = *i;
|
|
|
|
TORRENT_ASSERT(p);
|
|
|
|
++i;
|
2008-04-24 05:28:48 +02:00
|
|
|
torrent* t = p->associated_torrent().lock().get();
|
2009-04-04 09:55:34 +02:00
|
|
|
policy::peer* pi = p->peer_info_struct();
|
2010-02-09 04:04:41 +01:00
|
|
|
|
2012-04-12 02:02:35 +02:00
|
|
|
if (p->ignore_unchoke_slots() || t == 0 || pi == 0 || pi->web_seed || t->is_paused())
|
|
|
|
continue;
|
2009-04-04 09:55:34 +02:00
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
if (m_settings.choking_algorithm == session_settings::bittyrant_choker)
|
|
|
|
{
|
2010-10-09 23:11:03 +02:00
|
|
|
if (!p->is_choked() && p->is_interesting())
|
2010-02-09 04:04:41 +01:00
|
|
|
{
|
|
|
|
if (!p->has_peer_choked())
|
|
|
|
{
|
|
|
|
// we're unchoked, we may want to lower our estimated
|
|
|
|
// reciprocation rate
|
|
|
|
p->decrease_est_reciprocation_rate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we've unchoked this peer, and it hasn't reciprocated
|
|
|
|
// we may want to increase our estimated reciprocation rate
|
|
|
|
p->increase_est_reciprocation_rate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-04 09:55:34 +02:00
|
|
|
if (!p->is_peer_interested()
|
2008-04-24 05:28:48 +02:00
|
|
|
|| p->is_disconnecting()
|
2014-05-03 19:11:55 +02:00
|
|
|
|| p->is_connecting())
|
2007-08-16 14:41:46 +02:00
|
|
|
{
|
2009-04-04 09:55:34 +02:00
|
|
|
// this peer is not unchokable. So, if it's unchoked
|
|
|
|
// already, make sure to choke it.
|
|
|
|
if (p->is_choked()) continue;
|
|
|
|
if (pi && pi->optimistically_unchoked)
|
2007-08-16 14:41:46 +02:00
|
|
|
{
|
2009-04-04 09:55:34 +02:00
|
|
|
pi->optimistically_unchoked = false;
|
|
|
|
// force a new optimistic unchoke
|
|
|
|
m_optimistic_unchoke_time_scaler = 0;
|
2014-05-03 19:11:55 +02:00
|
|
|
// TODO: post a message to have this happen
|
|
|
|
// immediately instead of waiting for the next tick
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
2009-04-04 09:55:34 +02:00
|
|
|
t->choke_peer(*p);
|
2009-05-02 05:15:52 +02:00
|
|
|
continue;
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
2008-10-19 00:35:10 +02:00
|
|
|
peers.push_back(p.get());
|
2008-04-24 05:28:48 +02:00
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
if (m_settings.choking_algorithm == session_settings::rate_based_choker)
|
2009-04-04 09:55:34 +02:00
|
|
|
{
|
|
|
|
m_allowed_upload_slots = 0;
|
|
|
|
std::sort(peers.begin(), peers.end()
|
2010-07-22 18:49:40 +02:00
|
|
|
, boost::bind(&peer_connection::upload_rate_compare, _1, _2));
|
2009-04-04 09:55:34 +02:00
|
|
|
|
2009-05-02 05:15:52 +02:00
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
for (std::vector<peer_connection*>::const_iterator i = peers.begin()
|
|
|
|
, end(peers.end()), prev(peers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (prev != end)
|
|
|
|
{
|
2010-06-25 16:14:36 +02:00
|
|
|
boost::shared_ptr<torrent> t1 = (*prev)->associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t1);
|
|
|
|
boost::shared_ptr<torrent> t2 = (*i)->associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t2);
|
2013-06-02 00:33:11 +02:00
|
|
|
TORRENT_ASSERT((*prev)->uploaded_in_last_round() * 1000
|
2010-07-15 03:46:50 +02:00
|
|
|
* (1 + t1->priority()) / total_milliseconds(unchoke_interval)
|
2013-06-02 00:33:11 +02:00
|
|
|
>= (*i)->uploaded_in_last_round() * 1000
|
2010-07-15 03:46:50 +02:00
|
|
|
* (1 + t2->priority()) / total_milliseconds(unchoke_interval));
|
2009-05-02 05:15:52 +02:00
|
|
|
}
|
|
|
|
prev = i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-04 09:55:34 +02:00
|
|
|
// TODO: make configurable
|
|
|
|
int rate_threshold = 1024;
|
|
|
|
|
|
|
|
for (std::vector<peer_connection*>::const_iterator i = peers.begin()
|
|
|
|
, end(peers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
peer_connection const& p = **i;
|
2013-06-02 00:33:11 +02:00
|
|
|
int rate = int(p.uploaded_in_last_round()
|
2011-02-21 06:24:41 +01:00
|
|
|
* 1000 / total_milliseconds(unchoke_interval));
|
2009-05-02 05:15:52 +02:00
|
|
|
|
2009-07-26 04:27:52 +02:00
|
|
|
if (rate < rate_threshold) break;
|
2009-07-19 00:55:07 +02:00
|
|
|
|
2009-05-02 05:15:52 +02:00
|
|
|
++m_allowed_upload_slots;
|
2009-04-04 09:55:34 +02:00
|
|
|
|
|
|
|
// TODO: make configurable
|
|
|
|
rate_threshold += 1024;
|
|
|
|
}
|
|
|
|
// allow one optimistic unchoke
|
|
|
|
++m_allowed_upload_slots;
|
|
|
|
}
|
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
if (m_settings.choking_algorithm == session_settings::bittyrant_choker)
|
|
|
|
{
|
|
|
|
// if we're using the bittyrant choker, sort peers by their return
|
|
|
|
// on investment. i.e. download rate / upload rate
|
|
|
|
std::sort(peers.begin(), peers.end()
|
2010-07-22 18:49:40 +02:00
|
|
|
, boost::bind(&peer_connection::bittyrant_unchoke_compare, _1, _2));
|
2010-02-09 04:04:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// sorts the peers that are eligible for unchoke by download rate and secondary
|
|
|
|
// by total upload. The reason for this is, if all torrents are being seeded,
|
|
|
|
// the download rate will be 0, and the peers we have sent the least to should
|
|
|
|
// be unchoked
|
|
|
|
std::sort(peers.begin(), peers.end()
|
2010-07-22 18:49:40 +02:00
|
|
|
, boost::bind(&peer_connection::unchoke_compare, _1, _2));
|
2010-02-09 04:04:41 +01:00
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
// auto unchoke
|
2009-04-26 02:21:59 +02:00
|
|
|
int upload_limit = m_bandwidth_channel[peer_connection::upload_channel]->throttle();
|
2010-02-09 04:04:41 +01:00
|
|
|
if (m_settings.choking_algorithm == session_settings::auto_expand_choker
|
2009-04-26 02:21:59 +02:00
|
|
|
&& upload_limit > 0)
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
|
|
|
// if our current upload rate is less than 90% of our
|
|
|
|
// limit AND most torrents are not "congested", i.e.
|
|
|
|
// they are not holding back because of a per-torrent
|
|
|
|
// limit
|
|
|
|
if (m_stat.upload_rate() < upload_limit * 0.9f
|
|
|
|
&& m_allowed_upload_slots <= m_num_unchoked + 1
|
2008-10-14 02:56:44 +02:00
|
|
|
&& congested_torrents < uncongested_torrents
|
2009-04-26 02:21:59 +02:00
|
|
|
&& m_upload_rate.queue_size() < 2)
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
|
|
|
++m_allowed_upload_slots;
|
|
|
|
}
|
2009-04-26 02:21:59 +02:00
|
|
|
else if (m_upload_rate.queue_size() > 1
|
2011-12-19 06:53:11 +01:00
|
|
|
&& m_allowed_upload_slots > m_settings.unchoke_slots_limit
|
|
|
|
&& m_settings.unchoke_slots_limit >= 0)
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
|
|
|
--m_allowed_upload_slots;
|
|
|
|
}
|
|
|
|
}
|
2007-08-17 09:37:08 +02:00
|
|
|
|
2010-02-02 19:39:32 +01:00
|
|
|
int num_opt_unchoke = m_settings.num_optimistic_unchoke_slots;
|
|
|
|
if (num_opt_unchoke == 0) num_opt_unchoke = (std::max)(1, m_allowed_upload_slots / 5);
|
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
// reserve some upload slots for optimistic unchokes
|
2010-02-02 19:39:32 +01:00
|
|
|
int unchoke_set_size = m_allowed_upload_slots - num_opt_unchoke;
|
2007-08-17 09:37:08 +02:00
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
int upload_capacity_left = 0;
|
|
|
|
if (m_settings.choking_algorithm == session_settings::bittyrant_choker)
|
|
|
|
{
|
|
|
|
upload_capacity_left = m_upload_channel.throttle();
|
|
|
|
if (upload_capacity_left == 0)
|
|
|
|
{
|
|
|
|
// we don't know at what rate we can upload. If we have a
|
|
|
|
// measurement of the peak, use that + 10kB/s, otherwise
|
|
|
|
// assume 20 kB/s
|
|
|
|
upload_capacity_left = (std::max)(20000, m_peak_up_rate + 10000);
|
|
|
|
if (m_alerts.should_post<performance_alert>())
|
|
|
|
m_alerts.post_alert(performance_alert(torrent_handle()
|
|
|
|
, performance_alert::bittyrant_with_no_uplimit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
m_num_unchoked = 0;
|
|
|
|
// go through all the peers and unchoke the first ones and choke
|
|
|
|
// all the other ones.
|
|
|
|
for (std::vector<peer_connection*>::iterator i = peers.begin()
|
|
|
|
, end(peers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
peer_connection* p = *i;
|
|
|
|
TORRENT_ASSERT(p);
|
2009-04-04 09:55:34 +02:00
|
|
|
TORRENT_ASSERT(!p->ignore_unchoke_slots());
|
2009-05-02 05:15:52 +02:00
|
|
|
|
|
|
|
// this will update the m_uploaded_at_last_unchoke
|
2010-02-09 04:04:41 +01:00
|
|
|
// #error this should be called for all peers!
|
2009-05-02 05:15:52 +02:00
|
|
|
p->reset_choke_counters();
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
torrent* t = p->associated_torrent().lock().get();
|
|
|
|
TORRENT_ASSERT(t);
|
2010-02-09 04:04:41 +01:00
|
|
|
|
|
|
|
// if this peer should be unchoked depends on different things
|
|
|
|
// in different unchoked schemes
|
|
|
|
bool unchoke = false;
|
|
|
|
if (m_settings.choking_algorithm == session_settings::bittyrant_choker)
|
|
|
|
{
|
|
|
|
unchoke = p->est_reciprocation_rate() <= upload_capacity_left;
|
|
|
|
}
|
|
|
|
else
|
2008-04-24 05:28:48 +02:00
|
|
|
{
|
2010-02-09 04:04:41 +01:00
|
|
|
unchoke = unchoke_set_size > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unchoke)
|
|
|
|
{
|
|
|
|
upload_capacity_left -= p->est_reciprocation_rate();
|
|
|
|
|
2009-04-04 09:55:34 +02:00
|
|
|
// yes, this peer should be unchoked
|
2008-04-24 05:28:48 +02:00
|
|
|
if (p->is_choked())
|
|
|
|
{
|
|
|
|
if (!t->unchoke_peer(*p))
|
|
|
|
continue;
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
--unchoke_set_size;
|
|
|
|
++m_num_unchoked;
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
TORRENT_ASSERT(p->peer_info_struct());
|
|
|
|
if (p->peer_info_struct()->optimistically_unchoked)
|
|
|
|
{
|
|
|
|
// force a new optimistic unchoke
|
2008-10-14 02:56:44 +02:00
|
|
|
// since this one just got promoted into the
|
|
|
|
// proper unchoke set
|
2008-04-24 05:28:48 +02:00
|
|
|
m_optimistic_unchoke_time_scaler = 0;
|
|
|
|
p->peer_info_struct()->optimistically_unchoked = false;
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
else
|
|
|
|
{
|
2009-04-04 09:55:34 +02:00
|
|
|
// no, this peer should be shoked
|
2008-04-24 05:28:48 +02:00
|
|
|
TORRENT_ASSERT(p->peer_info_struct());
|
|
|
|
if (!p->is_choked() && !p->peer_info_struct()->optimistically_unchoked)
|
|
|
|
t->choke_peer(*p);
|
|
|
|
if (!p->is_choked())
|
|
|
|
++m_num_unchoked;
|
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
2006-12-21 23:20:28 +01:00
|
|
|
|
2013-07-19 21:06:27 +02:00
|
|
|
#if defined _MSC_VER && defined TORRENT_DEBUG
|
|
|
|
static void straight_to_debugger(unsigned int, _EXCEPTION_POINTERS*)
|
|
|
|
{ throw; }
|
|
|
|
#endif
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void session_impl::main_thread()
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2013-07-19 21:06:27 +02:00
|
|
|
#if defined _MSC_VER && defined TORRENT_DEBUG
|
|
|
|
// workaround for microsofts
|
|
|
|
// hardware exceptions that makes
|
|
|
|
// it hard to debug stuff
|
|
|
|
::_set_se_translator(straight_to_debugger);
|
|
|
|
#endif
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS && defined BOOST_HAS_PTHREADS
|
2010-07-14 06:16:38 +02:00
|
|
|
m_network_thread = pthread_self();
|
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2010-09-25 22:07:27 +02:00
|
|
|
// initialize async operations
|
2010-12-04 23:20:31 +01:00
|
|
|
init();
|
2010-09-25 22:07:27 +02:00
|
|
|
|
2010-06-24 23:43:00 +02:00
|
|
|
bool stop_loop = false;
|
|
|
|
while (!stop_loop)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2008-11-05 06:39:18 +01:00
|
|
|
error_code ec;
|
|
|
|
m_io_service.run(ec);
|
|
|
|
if (ec)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2009-10-20 04:49:56 +02:00
|
|
|
fprintf(stderr, "%s\n", ec.message().c_str());
|
2008-11-05 06:39:18 +01:00
|
|
|
std::string err = ec.message();
|
2007-02-12 19:21:14 +01:00
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
2009-03-31 09:57:57 +02:00
|
|
|
m_io_service.reset();
|
2010-06-24 23:43:00 +02:00
|
|
|
|
|
|
|
stop_loop = m_abort;
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2007-05-23 03:02:46 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" locking mutex");
|
2007-05-23 03:02:46 +02:00
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
/*
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-10-11 22:57:54 +02:00
|
|
|
for (torrent_map::iterator i = m_torrents.begin();
|
|
|
|
i != m_torrents.end(); ++i)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->second->num_peers() == 0);
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-01-27 23:39:50 +01:00
|
|
|
*/
|
2007-05-23 03:02:46 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" cleaning up torrents");
|
2007-05-23 03:02:46 +02:00
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
m_torrents.clear();
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_torrents.empty());
|
|
|
|
TORRENT_ASSERT(m_connections.empty());
|
2010-12-04 23:20:31 +01:00
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS && defined BOOST_HAS_PTHREADS
|
2010-12-04 23:20:31 +01:00
|
|
|
m_network_thread = 0;
|
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// the return value from this function is valid only as long as the
|
|
|
|
// session is locked!
|
2013-01-02 09:09:21 +01:00
|
|
|
boost::weak_ptr<torrent> session_impl::find_torrent(sha1_hash const& info_hash) const
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2013-01-02 09:09:21 +01:00
|
|
|
torrent_map::const_iterator i = m_torrents.find(info_hash);
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2013-01-02 09:09:21 +01:00
|
|
|
for (torrent_map::const_iterator j
|
2006-10-11 22:57:54 +02:00
|
|
|
= m_torrents.begin(); j != m_torrents.end(); ++j)
|
|
|
|
{
|
|
|
|
torrent* p = boost::get_pointer(j->second);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p);
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (i != m_torrents.end()) return i->second;
|
|
|
|
return boost::weak_ptr<torrent>();
|
|
|
|
}
|
|
|
|
|
2013-01-02 09:09:21 +01:00
|
|
|
boost::weak_ptr<torrent> session_impl::find_torrent(std::string const& uuid) const
|
2011-01-18 04:41:54 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2013-01-02 09:09:21 +01:00
|
|
|
std::map<std::string, boost::shared_ptr<torrent> >::const_iterator i
|
2011-01-18 04:41:54 +01:00
|
|
|
= m_uuids.find(uuid);
|
|
|
|
if (i != m_uuids.end()) return i->second;
|
|
|
|
return boost::weak_ptr<torrent>();
|
|
|
|
}
|
|
|
|
|
2013-01-02 08:48:09 +01:00
|
|
|
// returns true if lhs is a better disconnect candidate than rhs
|
|
|
|
bool compare_disconnect_torrent(session_impl::torrent_map::value_type const& lhs
|
|
|
|
, session_impl::torrent_map::value_type const& rhs)
|
|
|
|
{
|
|
|
|
// a torrent with 0 peers is never a good disconnect candidate
|
|
|
|
// since there's nothing to disconnect
|
2013-01-02 09:09:21 +01:00
|
|
|
if ((lhs.second->num_peers() == 0) != (rhs.second->num_peers() == 0))
|
2013-01-02 08:48:09 +01:00
|
|
|
return lhs.second->num_peers() != 0;
|
|
|
|
|
|
|
|
// other than that, always prefer to disconnect peers from seeding torrents
|
|
|
|
// in order to not harm downloading ones
|
|
|
|
if (lhs.second->is_seed() != rhs.second->is_seed())
|
|
|
|
return lhs.second->is_seed();
|
|
|
|
|
|
|
|
return lhs.second->num_peers() > rhs.second->num_peers();
|
|
|
|
}
|
|
|
|
|
2013-01-02 09:09:21 +01:00
|
|
|
boost::weak_ptr<torrent> session_impl::find_disconnect_candidate_torrent() const
|
2013-01-02 08:39:02 +01:00
|
|
|
{
|
2013-01-02 09:09:21 +01:00
|
|
|
aux::session_impl::torrent_map::const_iterator i = std::min_element(m_torrents.begin(), m_torrents.end()
|
2013-01-02 08:48:09 +01:00
|
|
|
, boost::bind(&compare_disconnect_torrent, _1, _2));
|
2013-01-02 08:39:02 +01:00
|
|
|
|
|
|
|
TORRENT_ASSERT(i != m_torrents.end());
|
|
|
|
if (i == m_torrents.end()) return boost::shared_ptr<torrent>();
|
|
|
|
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
2008-02-17 23:51:03 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2006-11-15 22:39:58 +01:00
|
|
|
boost::shared_ptr<logger> session_impl::create_log(std::string const& name
|
|
|
|
, int instance, bool append)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2012-10-07 01:45:36 +02:00
|
|
|
error_code ec;
|
2006-10-11 22:57:54 +02:00
|
|
|
// current options are file_logger, cout_logger and null_logger
|
2012-10-07 01:45:36 +02:00
|
|
|
return boost::shared_ptr<logger>(new logger(m_logpath, name, instance, append));
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
2012-10-18 17:14:18 +02:00
|
|
|
|
|
|
|
void session_impl::session_log(char const* fmt, ...) const
|
|
|
|
{
|
|
|
|
if (!m_logger) return;
|
|
|
|
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
|
|
|
|
|
|
|
char usr[400];
|
|
|
|
vsnprintf(usr, sizeof(usr), fmt, v);
|
|
|
|
va_end(v);
|
|
|
|
char buf[450];
|
|
|
|
snprintf(buf, sizeof(buf), "%s: %s\n", time_now_string(), usr);
|
|
|
|
(*m_logger) << buf;
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
void session_impl::get_torrent_status(std::vector<torrent_status>* ret
|
|
|
|
, boost::function<bool(torrent_status const&)> const& pred
|
|
|
|
, boost::uint32_t flags) const
|
|
|
|
{
|
2011-12-21 22:21:19 +01:00
|
|
|
for (torrent_map::const_iterator i
|
2011-02-01 10:48:28 +01:00
|
|
|
= m_torrents.begin(), end(m_torrents.end());
|
|
|
|
i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->second->is_aborted()) continue;
|
|
|
|
torrent_status st;
|
|
|
|
i->second->status(&st, flags);
|
|
|
|
if (!pred(st)) continue;
|
|
|
|
ret->push_back(st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::refresh_torrent_status(std::vector<torrent_status>* ret
|
|
|
|
, boost::uint32_t flags) const
|
|
|
|
{
|
|
|
|
for (std::vector<torrent_status>::iterator i
|
|
|
|
= ret->begin(), end(ret->end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = i->handle.m_torrent.lock();
|
|
|
|
if (!t) continue;
|
|
|
|
t->status(&*i, flags);
|
|
|
|
}
|
|
|
|
}
|
2011-11-15 03:34:00 +01:00
|
|
|
|
|
|
|
void session_impl::post_torrent_updates()
|
|
|
|
{
|
2012-01-20 06:40:32 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2011-11-15 03:34:00 +01:00
|
|
|
std::auto_ptr<state_update_alert> alert(new state_update_alert());
|
|
|
|
alert->status.reserve(m_state_updates.size());
|
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2012-10-06 16:31:14 +02:00
|
|
|
m_posting_torrent_updates = true;
|
|
|
|
#endif
|
|
|
|
|
2011-11-15 03:34:00 +01:00
|
|
|
for (std::vector<boost::weak_ptr<torrent> >::iterator i = m_state_updates.begin()
|
|
|
|
, end(m_state_updates.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = i->lock();
|
|
|
|
if (!t) continue;
|
|
|
|
alert->status.push_back(torrent_status());
|
|
|
|
t->status(&alert->status.back(), 0xffffffff);
|
2012-01-20 06:40:32 +01:00
|
|
|
t->clear_in_state_update();
|
2011-11-15 03:34:00 +01:00
|
|
|
}
|
|
|
|
m_state_updates.clear();
|
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2012-10-06 16:31:14 +02:00
|
|
|
m_posting_torrent_updates = false;
|
|
|
|
#endif
|
|
|
|
|
2011-11-15 03:34:00 +01:00
|
|
|
m_alerts.post_alert_ptr(alert.release());
|
|
|
|
}
|
2011-02-01 10:48:28 +01:00
|
|
|
|
|
|
|
std::vector<torrent_handle> session_impl::get_torrents() const
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
|
|
|
std::vector<torrent_handle> ret;
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2011-12-21 22:21:19 +01:00
|
|
|
for (torrent_map::const_iterator i
|
2006-10-11 22:57:54 +02:00
|
|
|
= m_torrents.begin(), end(m_torrents.end());
|
|
|
|
i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->second->is_aborted()) continue;
|
2008-04-09 22:09:36 +02:00
|
|
|
ret.push_back(torrent_handle(i->second));
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
torrent_handle session_impl::find_torrent_handle(sha1_hash const& info_hash)
|
|
|
|
{
|
2008-04-09 22:09:36 +02:00
|
|
|
return torrent_handle(find_torrent(info_hash));
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
|
2011-10-12 12:27:17 +02:00
|
|
|
void session_impl::async_add_torrent(add_torrent_params* params)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
torrent_handle handle = add_torrent(*params, ec);
|
|
|
|
delete params;
|
|
|
|
}
|
|
|
|
|
2012-03-08 10:54:44 +01:00
|
|
|
torrent_handle session_impl::add_torrent(add_torrent_params const& p
|
2009-02-23 02:21:19 +01:00
|
|
|
, error_code& ec)
|
2012-11-08 03:07:10 +01:00
|
|
|
{
|
|
|
|
torrent_handle h = add_torrent_impl(p, ec);
|
|
|
|
m_alerts.post_alert(add_torrent_alert(h, p, ec));
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent_handle session_impl::add_torrent_impl(add_torrent_params const& p
|
|
|
|
, error_code& ec)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2012-03-08 10:54:44 +01:00
|
|
|
TORRENT_ASSERT(!p.save_path.empty());
|
2007-09-22 18:27:29 +02:00
|
|
|
|
2011-11-15 03:34:00 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2012-03-08 10:54:44 +01:00
|
|
|
p.update_flags();
|
2011-11-15 03:34:00 +01:00
|
|
|
#endif
|
|
|
|
|
2012-03-08 10:54:44 +01:00
|
|
|
add_torrent_params params = p;
|
|
|
|
if (string_begins_no_case("magnet:", params.url.c_str()))
|
|
|
|
{
|
|
|
|
parse_magnet_uri(params.url, params, ec);
|
|
|
|
if (ec) return torrent_handle();
|
|
|
|
params.url.clear();
|
|
|
|
}
|
|
|
|
|
2011-02-21 04:07:37 +01:00
|
|
|
if (params.ti && params.ti->is_valid() && params.ti->num_files() == 0)
|
2008-04-07 03:22:26 +02:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
ec = errors::no_files_in_torrent;
|
2008-04-07 03:22:26 +02:00
|
|
|
return torrent_handle();
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2012-03-08 10:54:44 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
// add p.dht_nodes to the DHT, if enabled
|
|
|
|
if (m_dht && !p.dht_nodes.empty())
|
|
|
|
{
|
|
|
|
for (std::vector<std::pair<std::string, int> >::const_iterator i = p.dht_nodes.begin()
|
|
|
|
, end(p.dht_nodes.end()); i != end; ++i)
|
|
|
|
m_dht->add_node(*i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-22 18:27:29 +02:00
|
|
|
// INVARIANT_CHECK;
|
2007-08-21 20:33:28 +02:00
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
if (is_aborted())
|
2008-04-07 03:22:26 +02:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
ec = errors::session_is_closing;
|
2008-04-07 03:22:26 +02:00
|
|
|
return torrent_handle();
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
// figure out the info hash of the torrent
|
|
|
|
sha1_hash const* ih = 0;
|
2010-12-30 02:47:30 +01:00
|
|
|
sha1_hash tmp;
|
2008-04-24 05:28:48 +02:00
|
|
|
if (params.ti) ih = ¶ms.ti->info_hash();
|
2010-12-30 02:47:30 +01:00
|
|
|
else if (!params.url.empty())
|
|
|
|
{
|
|
|
|
// in order to avoid info-hash collisions, for
|
|
|
|
// torrents where we don't have an info-hash, but
|
|
|
|
// just a URL, set the temporary info-hash to the
|
|
|
|
// hash of the URL. This will be changed once we
|
|
|
|
// have the actual .torrent file
|
|
|
|
tmp = hasher(¶ms.url[0], params.url.size()).final();
|
|
|
|
ih = &tmp;
|
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
else ih = ¶ms.info_hash;
|
|
|
|
|
2012-11-08 03:07:10 +01:00
|
|
|
// we don't have a torrent file. If the user provided
|
|
|
|
// resume data, there may be some metadata in there
|
|
|
|
if ((!params.ti || !params.ti->is_valid())
|
2013-07-17 22:21:48 +02:00
|
|
|
&& !params.resume_data.empty())
|
2012-11-08 03:07:10 +01:00
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
error_code ec;
|
|
|
|
lazy_entry tmp;
|
|
|
|
lazy_entry const* info = 0;
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2012-11-09 18:11:42 +01:00
|
|
|
session_log("adding magnet link with resume data");
|
2012-11-08 03:07:10 +01:00
|
|
|
#endif
|
2013-07-17 22:21:48 +02:00
|
|
|
if (lazy_bdecode(¶ms.resume_data[0], ¶ms.resume_data[0]
|
|
|
|
+ params.resume_data.size(), tmp, ec, &pos) == 0
|
2012-11-08 03:07:10 +01:00
|
|
|
&& tmp.type() == lazy_entry::dict_t
|
|
|
|
&& (info = tmp.dict_find_dict("info")))
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2012-11-09 18:11:42 +01:00
|
|
|
session_log("found metadata in resume data");
|
2012-11-08 03:07:10 +01:00
|
|
|
#endif
|
|
|
|
// verify the info-hash of the metadata stored in the resume file matches
|
|
|
|
// the torrent we're loading
|
|
|
|
|
|
|
|
std::pair<char const*, int> buf = info->data_section();
|
|
|
|
sha1_hash resume_ih = hasher(buf.first, buf.second).final();
|
|
|
|
|
|
|
|
// if url is set, the info_hash is not actually the info-hash of the
|
|
|
|
// torrent, but the hash of the URL, until we have the full torrent
|
|
|
|
// only require the info-hash to match if we actually passed in one
|
|
|
|
if (resume_ih == params.info_hash
|
|
|
|
|| !params.url.empty()
|
|
|
|
|| params.info_hash.is_all_zeros())
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2012-11-09 18:11:42 +01:00
|
|
|
session_log("info-hash matched");
|
2012-11-08 03:07:10 +01:00
|
|
|
#endif
|
|
|
|
params.ti = new torrent_info(resume_ih);
|
|
|
|
|
|
|
|
if (params.ti->parse_info_section(*info, ec, 0))
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2012-11-09 18:11:42 +01:00
|
|
|
session_log("successfully loaded metadata from resume file");
|
2012-11-08 03:07:10 +01:00
|
|
|
#endif
|
|
|
|
// make the info-hash be the one in the resume file
|
|
|
|
params.info_hash = resume_ih;
|
|
|
|
ih = ¶ms.info_hash;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-11-09 18:11:42 +01:00
|
|
|
session_log("failed to load metadata from resume file: %s"
|
2012-11-08 03:07:10 +01:00
|
|
|
, ec.message().c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
else
|
|
|
|
{
|
2012-11-09 18:11:42 +01:00
|
|
|
session_log("metadata info-hash failed");
|
2012-11-08 03:07:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
else
|
|
|
|
{
|
2012-11-09 18:11:42 +01:00
|
|
|
session_log("no metadata found");
|
2012-11-08 03:07:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
// is the torrent already active?
|
2008-04-24 05:28:48 +02:00
|
|
|
boost::shared_ptr<torrent> torrent_ptr = find_torrent(*ih).lock();
|
2011-01-18 04:41:54 +01:00
|
|
|
if (!torrent_ptr && !params.uuid.empty()) torrent_ptr = find_torrent(params.uuid).lock();
|
2013-09-15 14:29:09 +02:00
|
|
|
// if we still can't find the torrent, look for it by url
|
|
|
|
if (!torrent_ptr && !params.url.empty())
|
|
|
|
{
|
|
|
|
std::map<sha1_hash, boost::shared_ptr<torrent> >::iterator i = std::find_if(m_torrents.begin()
|
|
|
|
, m_torrents.end(), boost::bind(&torrent::url, boost::bind(&std::pair<const sha1_hash
|
|
|
|
, boost::shared_ptr<torrent> >::second, _1)) == params.url);
|
|
|
|
if (i != m_torrents.end())
|
|
|
|
torrent_ptr = i->second;
|
|
|
|
}
|
2011-01-18 04:41:54 +01:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
if (torrent_ptr)
|
2008-04-07 03:22:26 +02:00
|
|
|
{
|
2011-11-08 06:36:22 +01:00
|
|
|
if ((params.flags & add_torrent_params::flag_duplicate_is_error) == 0)
|
2011-03-23 03:46:22 +01:00
|
|
|
{
|
|
|
|
if (!params.uuid.empty() && torrent_ptr->uuid().empty())
|
|
|
|
torrent_ptr->set_uuid(params.uuid);
|
|
|
|
if (!params.url.empty() && torrent_ptr->url().empty())
|
|
|
|
torrent_ptr->set_url(params.url);
|
|
|
|
if (!params.source_feed_url.empty() && torrent_ptr->source_feed_url().empty())
|
|
|
|
torrent_ptr->set_source_feed_url(params.source_feed_url);
|
2008-04-24 05:28:48 +02:00
|
|
|
return torrent_handle(torrent_ptr);
|
2011-03-23 03:46:22 +01:00
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2009-11-29 08:06:38 +01:00
|
|
|
ec = errors::duplicate_torrent;
|
2008-04-07 03:22:26 +02:00
|
|
|
return torrent_handle();
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2008-05-29 05:37:19 +02:00
|
|
|
int queue_pos = 0;
|
|
|
|
for (torrent_map::const_iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
int pos = i->second->queue_position();
|
|
|
|
if (pos >= queue_pos) queue_pos = pos + 1;
|
|
|
|
}
|
|
|
|
|
2009-02-03 08:46:24 +01:00
|
|
|
torrent_ptr.reset(new torrent(*this, m_listen_interface
|
2010-12-30 02:47:30 +01:00
|
|
|
, 16 * 1024, queue_pos, params, *ih));
|
2009-01-02 09:58:51 +01:00
|
|
|
torrent_ptr->start();
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2013-08-02 11:42:51 +02:00
|
|
|
typedef std::vector<boost::function<
|
|
|
|
boost::shared_ptr<torrent_plugin>(torrent*, void*)> >
|
|
|
|
torrent_plugins_t;
|
|
|
|
|
|
|
|
for (torrent_plugins_t::const_iterator i = params.extensions.begin()
|
|
|
|
, end(params.extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
torrent_ptr->add_extension((*i)(torrent_ptr.get(),
|
|
|
|
params.userdata));
|
|
|
|
}
|
|
|
|
|
2011-01-29 11:37:21 +01:00
|
|
|
for (ses_extension_list_t::iterator i = m_ses_extensions.begin()
|
|
|
|
, end(m_ses_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent_plugin> tp((*i)->new_torrent(torrent_ptr.get(), params.userdata));
|
|
|
|
if (tp) torrent_ptr->add_extension(tp);
|
|
|
|
}
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2008-04-24 05:28:48 +02:00
|
|
|
if (m_dht && params.ti)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2008-04-24 05:28:48 +02:00
|
|
|
torrent_info::nodes_t const& nodes = params.ti->nodes();
|
2009-11-23 00:55:54 +01:00
|
|
|
std::for_each(nodes.begin(), nodes.end(), boost::bind(
|
2006-10-11 22:57:54 +02:00
|
|
|
(void(dht::dht_tracker::*)(std::pair<std::string, int> const&))
|
|
|
|
&dht::dht_tracker::add_node
|
|
|
|
, boost::ref(m_dht), _1));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
m_torrents.insert(std::make_pair(*ih, torrent_ptr));
|
2011-01-18 04:41:54 +01:00
|
|
|
if (!params.uuid.empty() || !params.url.empty())
|
|
|
|
m_uuids.insert(std::make_pair(params.uuid.empty()
|
|
|
|
? params.url : params.uuid, torrent_ptr));
|
2008-03-08 07:06:31 +01:00
|
|
|
|
2011-02-01 04:25:40 +01:00
|
|
|
if (m_alerts.should_post<torrent_added_alert>())
|
|
|
|
m_alerts.post_alert(torrent_added_alert(torrent_ptr->get_handle()));
|
|
|
|
|
2011-12-21 22:21:19 +01:00
|
|
|
// recalculate auto-managed torrents sooner (or put it off)
|
|
|
|
// if another torrent will be added within one second from now
|
|
|
|
// we want to put it off again anyway. So that while we're adding
|
|
|
|
// a boat load of torrents, we postpone the recalculation until
|
|
|
|
// we're done adding them all (since it's kind of an expensive operation)
|
|
|
|
if (params.flags & add_torrent_params::flag_auto_managed)
|
2012-11-03 04:50:12 +01:00
|
|
|
trigger_auto_manage();
|
2011-03-04 07:03:45 +01:00
|
|
|
|
2008-04-09 22:09:36 +02:00
|
|
|
return torrent_handle(torrent_ptr);
|
2008-03-08 07:06:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-17 22:42:14 +01:00
|
|
|
void session_impl::queue_check_torrent(boost::shared_ptr<torrent> const& t)
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-06-24 14:17:42 +02:00
|
|
|
if (m_abort) return;
|
2008-11-19 01:46:48 +01:00
|
|
|
TORRENT_ASSERT(t->should_check_files());
|
|
|
|
TORRENT_ASSERT(t->state() != torrent_status::checking_files);
|
2008-03-08 07:06:31 +01:00
|
|
|
if (m_queued_for_checking.empty()) t->start_checking();
|
2008-11-19 01:46:48 +01:00
|
|
|
else t->set_state(torrent_status::queued_for_checking);
|
2008-09-20 19:42:25 +02:00
|
|
|
TORRENT_ASSERT(std::find(m_queued_for_checking.begin()
|
|
|
|
, m_queued_for_checking.end(), t) == m_queued_for_checking.end());
|
2008-03-08 07:06:31 +01:00
|
|
|
m_queued_for_checking.push_back(t);
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2010-01-17 22:42:14 +01:00
|
|
|
void session_impl::dequeue_check_torrent(boost::shared_ptr<torrent> const& t)
|
2008-03-08 07:06:31 +01:00
|
|
|
{
|
2008-11-19 01:46:48 +01:00
|
|
|
INVARIANT_CHECK;
|
2010-01-17 22:42:14 +01:00
|
|
|
TORRENT_ASSERT(t->state() == torrent_status::checking_files
|
|
|
|
|| t->state() == torrent_status::queued_for_checking);
|
2008-11-19 01:46:48 +01:00
|
|
|
|
2008-06-24 14:17:42 +02:00
|
|
|
if (m_queued_for_checking.empty()) return;
|
2010-01-17 22:42:14 +01:00
|
|
|
|
2009-02-03 18:35:41 +01:00
|
|
|
boost::shared_ptr<torrent> next_check = *m_queued_for_checking.begin();
|
2008-06-12 23:22:24 +02:00
|
|
|
check_queue_t::iterator done = m_queued_for_checking.end();
|
|
|
|
for (check_queue_t::iterator i = m_queued_for_checking.begin()
|
|
|
|
, end(m_queued_for_checking.end()); i != end; ++i)
|
|
|
|
{
|
2011-04-11 00:05:28 +02:00
|
|
|
// the reason m_paused is in there is because when the session
|
|
|
|
// is paused, all torrents that are queued ar all of a sudden
|
|
|
|
// not supposed to be queued anymore. The first torrent that gets
|
|
|
|
// removed from the queue will hence trigger this assert, without
|
|
|
|
// the m_paused exception
|
|
|
|
TORRENT_ASSERT(*i == t || (*i)->should_check_files() || m_paused);
|
2008-06-12 23:22:24 +02:00
|
|
|
if (*i == t) done = i;
|
2011-01-18 04:41:54 +01:00
|
|
|
else if (next_check == t || next_check->queue_position() > (*i)->queue_position())
|
2009-02-03 18:35:41 +01:00
|
|
|
next_check = *i;
|
2008-06-12 23:22:24 +02:00
|
|
|
}
|
2011-01-18 04:41:54 +01:00
|
|
|
TORRENT_ASSERT(next_check != t || m_queued_for_checking.size() == 1);
|
2008-11-19 01:46:48 +01:00
|
|
|
// only start a new one if we removed the one that is checking
|
2010-01-17 22:42:14 +01:00
|
|
|
TORRENT_ASSERT(done != m_queued_for_checking.end());
|
2009-02-03 18:35:41 +01:00
|
|
|
if (done == m_queued_for_checking.end()) return;
|
|
|
|
|
2011-04-28 09:54:57 +02:00
|
|
|
if (next_check != t
|
|
|
|
&& t->state() == torrent_status::checking_files
|
|
|
|
&& !m_paused)
|
|
|
|
{
|
2009-02-03 18:35:41 +01:00
|
|
|
next_check->start_checking();
|
2011-04-28 09:54:57 +02:00
|
|
|
}
|
2010-01-17 22:42:14 +01:00
|
|
|
|
2008-06-12 23:22:24 +02:00
|
|
|
m_queued_for_checking.erase(done);
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2007-10-13 05:33:33 +02:00
|
|
|
void session_impl::remove_torrent(const torrent_handle& h, int options)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2012-06-21 05:51:39 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-04-09 22:09:36 +02:00
|
|
|
boost::shared_ptr<torrent> tptr = h.m_torrent.lock();
|
2011-06-21 04:02:58 +02:00
|
|
|
if (!tptr) return;
|
|
|
|
|
2011-03-23 03:46:22 +01:00
|
|
|
remove_torrent_impl(tptr, options);
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2011-03-23 03:46:22 +01:00
|
|
|
if (m_alerts.should_post<torrent_removed_alert>())
|
|
|
|
m_alerts.post_alert(torrent_removed_alert(tptr->get_handle(), tptr->info_hash()));
|
|
|
|
|
|
|
|
tptr->abort();
|
2011-04-02 22:05:44 +02:00
|
|
|
tptr->set_queue_position(-1);
|
2011-03-23 03:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::remove_torrent_impl(boost::shared_ptr<torrent> tptr, int options)
|
|
|
|
{
|
2011-01-18 04:41:54 +01:00
|
|
|
// remove from uuid list
|
|
|
|
if (!tptr->uuid().empty())
|
|
|
|
{
|
|
|
|
std::map<std::string, boost::shared_ptr<torrent> >::iterator j
|
|
|
|
= m_uuids.find(tptr->uuid());
|
|
|
|
if (j != m_uuids.end()) m_uuids.erase(j);
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:21:19 +01:00
|
|
|
torrent_map::iterator i =
|
2008-04-09 22:09:36 +02:00
|
|
|
m_torrents.find(tptr->torrent_file().info_hash());
|
|
|
|
|
2010-12-30 07:46:28 +01:00
|
|
|
// this torrent might be filed under the URL-hash
|
|
|
|
if (i == m_torrents.end() && !tptr->url().empty())
|
|
|
|
{
|
|
|
|
std::string const& url = tptr->url();
|
|
|
|
sha1_hash urlhash = hasher(&url[0], url.size()).final();
|
|
|
|
i = m_torrents.find(urlhash);
|
|
|
|
}
|
|
|
|
|
2011-02-01 04:25:40 +01:00
|
|
|
if (i == m_torrents.end()) return;
|
|
|
|
|
|
|
|
torrent& t = *i->second;
|
|
|
|
if (options & session::delete_files)
|
2013-03-17 01:50:33 +01:00
|
|
|
{
|
|
|
|
if (!t.delete_files())
|
|
|
|
{
|
2013-03-17 23:16:57 +01:00
|
|
|
if (m_alerts.should_post<torrent_delete_failed_alert>())
|
2013-10-28 00:39:24 +01:00
|
|
|
m_alerts.post_alert(torrent_delete_failed_alert(t.get_handle()
|
|
|
|
, error_code(), t.torrent_file().info_hash()));
|
2013-03-17 01:50:33 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2013-02-09 06:56:31 +01:00
|
|
|
tptr->update_guage();
|
2012-06-21 05:51:39 +02:00
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2011-02-01 04:25:40 +01:00
|
|
|
sha1_hash i_hash = t.torrent_file().info_hash();
|
2010-02-14 02:39:55 +01:00
|
|
|
#endif
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2011-02-01 04:25:40 +01:00
|
|
|
if (i == m_next_dht_torrent)
|
|
|
|
++m_next_dht_torrent;
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
2011-02-01 04:25:40 +01:00
|
|
|
if (i == m_next_lsd_torrent)
|
|
|
|
++m_next_lsd_torrent;
|
|
|
|
if (i == m_next_connect_torrent)
|
|
|
|
++m_next_connect_torrent;
|
2010-02-05 09:23:17 +01:00
|
|
|
|
2011-02-01 04:25:40 +01:00
|
|
|
m_torrents.erase(i);
|
2010-02-05 09:23:17 +01:00
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2011-02-01 04:25:40 +01:00
|
|
|
if (m_next_dht_torrent == m_torrents.end())
|
|
|
|
m_next_dht_torrent = m_torrents.begin();
|
2010-02-14 02:39:55 +01:00
|
|
|
#endif
|
2011-02-01 04:25:40 +01:00
|
|
|
if (m_next_lsd_torrent == m_torrents.end())
|
|
|
|
m_next_lsd_torrent = m_torrents.begin();
|
|
|
|
if (m_next_connect_torrent == m_torrents.end())
|
|
|
|
m_next_connect_torrent = m_torrents.begin();
|
2010-02-05 09:23:17 +01:00
|
|
|
|
2011-02-01 04:25:40 +01:00
|
|
|
std::list<boost::shared_ptr<torrent> >::iterator k
|
|
|
|
= std::find(m_queued_for_checking.begin(), m_queued_for_checking.end(), tptr);
|
|
|
|
if (k != m_queued_for_checking.end()) m_queued_for_checking.erase(k);
|
|
|
|
TORRENT_ASSERT(m_torrents.find(i_hash) == m_torrents.end());
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2011-02-16 07:35:53 +01:00
|
|
|
void session_impl::listen_on(
|
2006-10-11 22:57:54 +02:00
|
|
|
std::pair<int, int> const& port_range
|
2011-02-16 07:35:53 +01:00
|
|
|
, error_code& ec
|
2010-06-17 19:14:56 +02:00
|
|
|
, const char* net_interface, int flags)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
tcp::endpoint new_interface;
|
|
|
|
if (net_interface && std::strlen(net_interface) > 0)
|
2008-11-05 06:39:18 +01:00
|
|
|
{
|
|
|
|
new_interface = tcp::endpoint(address::from_string(net_interface, ec), port_range.first);
|
|
|
|
if (ec)
|
|
|
|
{
|
2013-06-04 02:35:42 +02:00
|
|
|
if (m_alerts.should_post<listen_failed_alert>())
|
2013-10-06 08:32:33 +02:00
|
|
|
m_alerts.post_alert(listen_failed_alert(new_interface, listen_failed_alert::parse_addr, ec
|
|
|
|
, listen_failed_alert::tcp));
|
2013-06-04 02:35:42 +02:00
|
|
|
|
2008-11-05 06:39:18 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("listen_on: %s failed: %s"
|
|
|
|
, net_interface, ec.message().c_str());
|
2008-11-05 06:39:18 +01:00
|
|
|
#endif
|
2011-02-16 07:35:53 +01:00
|
|
|
return;
|
2008-11-05 06:39:18 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
else
|
2011-02-16 07:35:53 +01:00
|
|
|
{
|
2007-09-22 18:27:29 +02:00
|
|
|
new_interface = tcp::endpoint(address_v4::any(), port_range.first);
|
2011-02-16 07:35:53 +01:00
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2007-09-22 18:27:29 +02:00
|
|
|
m_listen_port_retries = port_range.second - port_range.first;
|
2006-10-11 22:57:54 +02:00
|
|
|
|
|
|
|
// if the interface is the same and the socket is open
|
|
|
|
// don't do anything
|
|
|
|
if (new_interface == m_listen_interface
|
2011-02-16 07:35:53 +01:00
|
|
|
&& !m_listen_sockets.empty())
|
|
|
|
return;
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2007-03-16 22:45:29 +01:00
|
|
|
m_listen_interface = new_interface;
|
|
|
|
|
2011-02-16 07:35:53 +01:00
|
|
|
open_listen_port(flags, ec);
|
2007-03-16 22:45:29 +01:00
|
|
|
|
2008-02-17 23:51:03 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2006-11-15 22:39:58 +01:00
|
|
|
m_logger = create_log("main_session", listen_port(), false);
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("log created");
|
2006-11-15 22:39:58 +01:00
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
address session_impl::listen_address() const
|
|
|
|
{
|
|
|
|
for (std::list<listen_socket_t>::const_iterator i = m_listen_sockets.begin()
|
|
|
|
, end(m_listen_sockets.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->external_address != address()) return i->external_address;
|
|
|
|
}
|
|
|
|
return address();
|
|
|
|
}
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
boost::uint16_t session_impl::listen_port() const
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-04-13 06:30:34 +02:00
|
|
|
// if peer connections are set up to be received over a socks
|
|
|
|
// proxy, and it's the same one as we're using for the tracker
|
|
|
|
// just tell the tracker the socks5 port we're listening on
|
2012-03-17 18:48:22 +01:00
|
|
|
if (m_socks_listen_socket && m_socks_listen_socket->is_open())
|
2010-04-13 06:30:34 +02:00
|
|
|
return m_socks_listen_port;
|
|
|
|
|
2013-02-19 07:48:53 +01:00
|
|
|
// if not, don't tell the tracker anything if we're in force_proxy
|
2010-04-13 06:30:34 +02:00
|
|
|
// mode. We don't want to leak our listen port since it can
|
|
|
|
// potentially identify us if it is leaked elsewere
|
2013-02-19 07:48:53 +01:00
|
|
|
if (m_settings.force_proxy) return 0;
|
2007-09-22 18:27:29 +02:00
|
|
|
if (m_listen_sockets.empty()) return 0;
|
2009-01-19 09:31:31 +01:00
|
|
|
return m_listen_sockets.front().external_port;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
boost::uint16_t session_impl::ssl_listen_port() const
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
// if peer connections are set up to be received over a socks
|
|
|
|
// proxy, and it's the same one as we're using for the tracker
|
|
|
|
// just tell the tracker the socks5 port we're listening on
|
2014-07-01 09:48:34 +02:00
|
|
|
if (m_socks_listen_socket && m_socks_listen_socket->is_open())
|
2012-01-14 17:04:25 +01:00
|
|
|
return m_socks_listen_port;
|
|
|
|
|
2013-02-19 07:48:53 +01:00
|
|
|
// if not, don't tell the tracker anything if we're in force_proxy
|
2012-01-14 17:04:25 +01:00
|
|
|
// mode. We don't want to leak our listen port since it can
|
|
|
|
// potentially identify us if it is leaked elsewere
|
2013-02-19 07:48:53 +01:00
|
|
|
if (m_settings.force_proxy) return 0;
|
2012-01-14 17:04:25 +01:00
|
|
|
if (m_listen_sockets.empty()) return 0;
|
|
|
|
for (std::list<listen_socket_t>::const_iterator i = m_listen_sockets.begin()
|
|
|
|
, end(m_listen_sockets.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->ssl) return i->external_port;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-12 05:51:49 +02:00
|
|
|
void session_impl::announce_lsd(sha1_hash const& ih, int port, bool broadcast)
|
2007-04-04 04:06:07 +02:00
|
|
|
{
|
2007-04-14 17:51:36 +02:00
|
|
|
// use internal listen port for local peers
|
2007-05-31 02:21:54 +02:00
|
|
|
if (m_lsd.get())
|
2011-09-12 05:51:49 +02:00
|
|
|
m_lsd->announce(ih, port, broadcast);
|
2007-04-04 04:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih)
|
|
|
|
{
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_num_messages[on_lsd_peer_counter];
|
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2007-04-04 04:06:07 +02:00
|
|
|
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-04-04 04:06:07 +02:00
|
|
|
boost::shared_ptr<torrent> t = find_torrent(ih).lock();
|
|
|
|
if (!t) return;
|
2007-07-23 02:38:31 +02:00
|
|
|
// don't add peers from lsd to private torrents
|
2009-08-20 05:19:12 +02:00
|
|
|
if (t->torrent_file().priv() || (t->torrent_file().is_i2p()
|
|
|
|
&& !m_settings.allow_i2p_mixed)) return;
|
2007-04-04 04:06:07 +02:00
|
|
|
|
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log("added peer from local discovery: %s", print_endpoint(peer).c_str());
|
2007-04-04 04:06:07 +02:00
|
|
|
#endif
|
2009-05-16 00:07:19 +02:00
|
|
|
t->get_policy().add_peer(peer, peer_id(0), peer_info::lsd, 0);
|
2010-07-15 07:56:29 +02:00
|
|
|
if (m_alerts.should_post<lsd_peer_alert>())
|
|
|
|
m_alerts.post_alert(lsd_peer_alert(t->get_handle(), peer));
|
2007-04-04 04:06:07 +02:00
|
|
|
}
|
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
void session_impl::on_port_map_log(
|
|
|
|
char const* msg, int map_transport)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-10-22 03:12:14 +02:00
|
|
|
TORRENT_ASSERT(map_transport >= 0 && map_transport <= 1);
|
|
|
|
// log message
|
|
|
|
#ifdef TORRENT_UPNP_LOGGING
|
2009-06-12 18:40:38 +02:00
|
|
|
char const* transport_names[] = {"NAT-PMP", "UPnP"};
|
|
|
|
m_upnp_log << time_now_string() << " "
|
|
|
|
<< transport_names[map_transport] << ": " << msg;
|
2008-10-22 03:12:14 +02:00
|
|
|
#endif
|
2009-06-12 18:40:38 +02:00
|
|
|
if (m_alerts.should_post<portmap_log_alert>())
|
|
|
|
m_alerts.post_alert(portmap_log_alert(map_transport, msg));
|
|
|
|
}
|
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
void session_impl::on_port_mapping(int mapping, address const& ip, int port
|
2009-06-12 18:40:38 +02:00
|
|
|
, error_code const& ec, int map_transport)
|
|
|
|
{
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
TORRENT_ASSERT(map_transport >= 0 && map_transport <= 1);
|
2008-10-22 03:12:14 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
if (mapping == m_udp_mapping[map_transport] && port != 0)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
m_external_udp_port = port;
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<portmap_alert>())
|
2008-04-06 21:17:58 +02:00
|
|
|
m_alerts.post_alert(portmap_alert(mapping, port
|
2008-07-06 14:22:56 +02:00
|
|
|
, map_transport));
|
2008-04-06 21:17:58 +02:00
|
|
|
return;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
if (mapping == m_tcp_mapping[map_transport] && port != 0)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2013-01-21 02:40:59 +01:00
|
|
|
if (ip != address())
|
|
|
|
{
|
|
|
|
// TODO: 1 report the proper address of the router as the source IP of
|
|
|
|
// this understanding of our external address, instead of the empty address
|
|
|
|
set_external_address(ip, source_router, address());
|
|
|
|
}
|
2010-12-05 21:40:28 +01:00
|
|
|
|
|
|
|
if (!m_listen_sockets.empty()) {
|
|
|
|
m_listen_sockets.front().external_address = ip;
|
2008-04-06 21:17:58 +02:00
|
|
|
m_listen_sockets.front().external_port = port;
|
2010-12-05 21:40:28 +01:00
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<portmap_alert>())
|
2008-04-06 21:17:58 +02:00
|
|
|
m_alerts.post_alert(portmap_alert(mapping, port
|
2008-07-06 14:22:56 +02:00
|
|
|
, map_transport));
|
2008-04-06 21:17:58 +02:00
|
|
|
return;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
if (ec)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<portmap_error_alert>())
|
2008-04-07 01:18:35 +02:00
|
|
|
m_alerts.post_alert(portmap_error_alert(mapping
|
2009-06-12 18:40:38 +02:00
|
|
|
, map_transport, ec));
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<portmap_alert>())
|
2008-04-07 01:18:35 +02:00
|
|
|
m_alerts.post_alert(portmap_alert(mapping, port
|
2008-07-06 14:22:56 +02:00
|
|
|
, map_transport));
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session_status session_impl::status() const
|
|
|
|
{
|
2007-09-22 18:27:29 +02:00
|
|
|
// INVARIANT_CHECK;
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2007-08-21 20:33:28 +02:00
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
session_status s;
|
2008-01-13 12:18:18 +01:00
|
|
|
|
2008-10-19 00:35:10 +02:00
|
|
|
s.optimistic_unchoke_counter = m_optimistic_unchoke_time_scaler;
|
|
|
|
s.unchoke_counter = m_unchoke_time_scaler;
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
s.num_peers = (int)m_connections.size();
|
2008-01-13 12:18:18 +01:00
|
|
|
s.num_unchoked = m_num_unchoked;
|
|
|
|
s.allowed_upload_slots = m_allowed_upload_slots;
|
|
|
|
|
2008-07-11 09:30:04 +02:00
|
|
|
s.total_redundant_bytes = m_total_redundant_bytes;
|
|
|
|
s.total_failed_bytes = m_total_failed_bytes;
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
s.up_bandwidth_queue = m_upload_rate.queue_size();
|
|
|
|
s.down_bandwidth_queue = m_download_rate.queue_size();
|
2008-01-17 18:40:46 +01:00
|
|
|
|
2014-05-10 05:23:05 +02:00
|
|
|
s.up_bandwidth_bytes_queue = int(m_upload_rate.queued_bytes());
|
|
|
|
s.down_bandwidth_bytes_queue = int(m_download_rate.queued_bytes());
|
2008-12-13 06:12:12 +01:00
|
|
|
|
2011-01-30 11:04:15 +01:00
|
|
|
s.disk_write_queue = m_disk_queues[peer_connection::download_channel];
|
|
|
|
s.disk_read_queue = m_disk_queues[peer_connection::upload_channel];
|
|
|
|
|
2008-01-13 12:18:18 +01:00
|
|
|
s.has_incoming_connections = m_incoming_connection;
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2008-09-20 19:42:25 +02:00
|
|
|
// total
|
2006-10-11 22:57:54 +02:00
|
|
|
s.download_rate = m_stat.download_rate();
|
2008-09-20 19:42:25 +02:00
|
|
|
s.total_upload = m_stat.total_upload();
|
2006-10-11 22:57:54 +02:00
|
|
|
s.upload_rate = m_stat.upload_rate();
|
2008-09-20 19:42:25 +02:00
|
|
|
s.total_download = m_stat.total_download();
|
|
|
|
|
|
|
|
// payload
|
|
|
|
s.payload_download_rate = m_stat.transfer_rate(stat::download_payload);
|
|
|
|
s.total_payload_download = m_stat.total_transfer(stat::download_payload);
|
|
|
|
s.payload_upload_rate = m_stat.transfer_rate(stat::upload_payload);
|
|
|
|
s.total_payload_upload = m_stat.total_transfer(stat::upload_payload);
|
|
|
|
|
2010-02-08 06:43:54 +01:00
|
|
|
#ifndef TORRENT_DISABLE_FULL_STATS
|
2008-09-20 19:42:25 +02:00
|
|
|
// IP-overhead
|
|
|
|
s.ip_overhead_download_rate = m_stat.transfer_rate(stat::download_ip_protocol);
|
|
|
|
s.total_ip_overhead_download = m_stat.total_transfer(stat::download_ip_protocol);
|
|
|
|
s.ip_overhead_upload_rate = m_stat.transfer_rate(stat::upload_ip_protocol);
|
|
|
|
s.total_ip_overhead_upload = m_stat.total_transfer(stat::upload_ip_protocol);
|
|
|
|
|
2010-12-29 03:17:44 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2008-09-20 19:42:25 +02:00
|
|
|
// DHT protocol
|
|
|
|
s.dht_download_rate = m_stat.transfer_rate(stat::download_dht_protocol);
|
|
|
|
s.total_dht_download = m_stat.total_transfer(stat::download_dht_protocol);
|
|
|
|
s.dht_upload_rate = m_stat.transfer_rate(stat::upload_dht_protocol);
|
|
|
|
s.total_dht_upload = m_stat.total_transfer(stat::upload_dht_protocol);
|
2013-04-09 03:04:43 +02:00
|
|
|
#else
|
|
|
|
s.dht_download_rate = 0;
|
|
|
|
s.total_dht_download = 0;
|
|
|
|
s.dht_upload_rate = 0;
|
|
|
|
s.total_dht_upload = 0;
|
|
|
|
#endif // TORRENT_DISABLE_DHT
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2008-09-22 02:15:05 +02:00
|
|
|
// tracker
|
|
|
|
s.tracker_download_rate = m_stat.transfer_rate(stat::download_tracker_protocol);
|
|
|
|
s.total_tracker_download = m_stat.total_transfer(stat::download_tracker_protocol);
|
|
|
|
s.tracker_upload_rate = m_stat.transfer_rate(stat::upload_tracker_protocol);
|
|
|
|
s.total_tracker_upload = m_stat.total_transfer(stat::upload_tracker_protocol);
|
2010-02-08 06:43:54 +01:00
|
|
|
#else
|
|
|
|
// IP-overhead
|
|
|
|
s.ip_overhead_download_rate = 0;
|
|
|
|
s.total_ip_overhead_download = 0;
|
|
|
|
s.ip_overhead_upload_rate = 0;
|
|
|
|
s.total_ip_overhead_upload = 0;
|
|
|
|
|
|
|
|
// DHT protocol
|
|
|
|
s.dht_download_rate = 0;
|
|
|
|
s.total_dht_download = 0;
|
|
|
|
s.dht_upload_rate = 0;
|
|
|
|
s.total_dht_upload = 0;
|
|
|
|
|
|
|
|
// tracker
|
|
|
|
s.tracker_download_rate = 0;
|
|
|
|
s.total_tracker_download = 0;
|
|
|
|
s.tracker_upload_rate = 0;
|
|
|
|
s.total_tracker_upload = 0;
|
|
|
|
#endif
|
2008-09-22 02:15:05 +02:00
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (m_dht)
|
|
|
|
{
|
|
|
|
m_dht->dht_status(s);
|
|
|
|
}
|
|
|
|
else
|
2013-04-09 03:04:43 +02:00
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2006-11-28 19:18:37 +01:00
|
|
|
s.dht_nodes = 0;
|
|
|
|
s.dht_node_cache = 0;
|
|
|
|
s.dht_torrents = 0;
|
2007-05-12 03:52:25 +02:00
|
|
|
s.dht_global_nodes = 0;
|
2010-11-06 08:12:57 +01:00
|
|
|
s.dht_total_allocations = 0;
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
m_utp_socket_manager.get_status(s.utp_stats);
|
|
|
|
|
2010-10-01 06:07:38 +02:00
|
|
|
int peerlist_size = 0;
|
|
|
|
for (torrent_map::const_iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
peerlist_size += i->second->get_policy().num_peers();
|
|
|
|
}
|
|
|
|
|
|
|
|
s.peerlist_size = peerlist_size;
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
|
2010-03-04 17:42:39 +01:00
|
|
|
void session_impl::start_dht()
|
|
|
|
{ start_dht(m_dht_state); }
|
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
void on_bootstrap(alert_manager& alerts)
|
|
|
|
{
|
|
|
|
if (alerts.should_post<dht_bootstrap_alert>())
|
|
|
|
alerts.post_alert(dht_bootstrap_alert());
|
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
void session_impl::start_dht(entry const& startup_state)
|
|
|
|
{
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2012-06-25 05:31:11 +02:00
|
|
|
stop_dht();
|
2010-05-30 03:33:03 +02:00
|
|
|
m_dht = new dht::dht_tracker(*this, m_udp_socket, m_dht_settings, &startup_state);
|
2008-09-02 08:37:40 +02:00
|
|
|
|
2010-02-14 08:46:57 +01:00
|
|
|
for (std::list<udp::endpoint>::iterator i = m_dht_router_nodes.begin()
|
2008-09-02 08:37:40 +02:00
|
|
|
, end(m_dht_router_nodes.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
m_dht->add_router_node(*i);
|
|
|
|
}
|
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
m_dht->start(startup_state, boost::bind(&on_bootstrap, boost::ref(m_alerts)));
|
2009-10-25 03:37:45 +01:00
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
m_udp_socket.subscribe(m_dht.get());
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::stop_dht()
|
|
|
|
{
|
2007-03-02 06:02:12 +01:00
|
|
|
if (!m_dht) return;
|
2012-06-22 06:21:20 +02:00
|
|
|
m_udp_socket.unsubscribe(m_dht.get());
|
2007-02-25 10:42:43 +01:00
|
|
|
m_dht->stop();
|
2007-03-02 02:16:59 +01:00
|
|
|
m_dht = 0;
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_dht_settings(dht_settings const& settings)
|
|
|
|
{
|
|
|
|
m_dht_settings = settings;
|
|
|
|
}
|
|
|
|
|
2010-03-04 17:42:39 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2010-07-14 06:16:38 +02:00
|
|
|
entry session_impl::dht_state() const
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2007-10-29 17:18:37 +01:00
|
|
|
if (!m_dht) return entry();
|
2010-07-14 06:16:38 +02:00
|
|
|
return m_dht->state();
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
2010-03-04 17:42:39 +01:00
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
void session_impl::add_dht_node_name(std::pair<std::string, int> const& node)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-11-07 20:18:16 +01:00
|
|
|
if (m_dht) m_dht->add_node(node);
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::add_dht_router(std::pair<std::string, int> const& node)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("session_impl::on_dht_router_name_lookup");
|
|
|
|
#endif
|
2010-02-14 08:46:57 +01:00
|
|
|
char port[7];
|
|
|
|
snprintf(port, sizeof(port), "%d", node.second);
|
|
|
|
tcp::resolver::query q(node.first, port);
|
|
|
|
m_host_resolver.async_resolve(q,
|
2010-07-22 18:49:40 +02:00
|
|
|
boost::bind(&session_impl::on_dht_router_name_lookup, this, _1, _2));
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2010-02-14 08:46:57 +01:00
|
|
|
void session_impl::on_dht_router_name_lookup(error_code const& e
|
|
|
|
, tcp::resolver::iterator host)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("session_impl::on_dht_router_name_lookup");
|
|
|
|
#endif
|
2014-01-20 10:20:47 +01:00
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
if (m_alerts.should_post<dht_error_alert>())
|
|
|
|
m_alerts.post_alert(dht_error_alert(
|
|
|
|
dht_error_alert::hostname_lookup, e));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-07 20:18:16 +01:00
|
|
|
while (host != tcp::resolver::iterator())
|
|
|
|
{
|
|
|
|
// router nodes should be added before the DHT is started (and bootstrapped)
|
|
|
|
udp::endpoint ep(host->endpoint().address(), host->endpoint().port());
|
|
|
|
if (m_dht) m_dht->add_router_node(ep);
|
|
|
|
m_dht_router_nodes.push_back(ep);
|
|
|
|
++host;
|
|
|
|
}
|
2010-02-14 08:46:57 +01:00
|
|
|
}
|
2014-02-24 01:31:13 +01:00
|
|
|
|
|
|
|
// callback for dht_immutable_get
|
|
|
|
void session_impl::get_immutable_callback(sha1_hash target
|
|
|
|
, dht::item const& i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!i.is_mutable());
|
|
|
|
m_alerts.post_alert(dht_immutable_item_alert(target, i.value()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::dht_get_immutable_item(sha1_hash const& target)
|
|
|
|
{
|
|
|
|
if (!m_dht) return;
|
|
|
|
m_dht->get_item(target, boost::bind(&session_impl::get_immutable_callback
|
|
|
|
, this, target, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback for dht_mutable_get
|
|
|
|
void session_impl::get_mutable_callback(dht::item const& i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(i.is_mutable());
|
|
|
|
m_alerts.post_alert(dht_mutable_item_alert(i.pk(), i.sig(), i.seq()
|
|
|
|
, i.salt(), i.value()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// key is a 32-byte binary string, the public key to look up.
|
|
|
|
// the salt is optional
|
|
|
|
void session_impl::dht_get_mutable_item(boost::array<char, 32> key
|
|
|
|
, std::string salt)
|
|
|
|
{
|
|
|
|
if (!m_dht) return;
|
|
|
|
m_dht->get_item(key.data(), boost::bind(&session_impl::get_mutable_callback
|
|
|
|
, this, _1), salt);
|
|
|
|
}
|
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
void on_dht_put(alert_manager& alerts, sha1_hash target)
|
|
|
|
{
|
|
|
|
if (alerts.should_post<dht_put_alert>())
|
|
|
|
alerts.post_alert(dht_put_alert(target));
|
|
|
|
}
|
2014-02-24 01:31:13 +01:00
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
void session_impl::dht_put_item(entry data, sha1_hash target)
|
2014-02-24 01:31:13 +01:00
|
|
|
{
|
|
|
|
if (!m_dht) return;
|
2014-02-28 05:02:48 +01:00
|
|
|
m_dht->put_item(data, boost::bind(&on_dht_put, boost::ref(m_alerts)
|
|
|
|
, target));
|
2014-02-24 01:31:13 +01:00
|
|
|
}
|
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
void put_mutable_callback(alert_manager& alerts, dht::item& i
|
2014-02-24 01:31:13 +01:00
|
|
|
, boost::function<void(entry&, boost::array<char,64>&
|
|
|
|
, boost::uint64_t&, std::string const&)> cb)
|
|
|
|
{
|
|
|
|
entry value = i.value();
|
|
|
|
boost::array<char, 64> sig = i.sig();
|
|
|
|
boost::array<char, 32> pk = i.pk();
|
|
|
|
boost::uint64_t seq = i.seq();
|
|
|
|
std::string salt = i.salt();
|
|
|
|
cb(value, sig, seq, salt);
|
|
|
|
i.assign(value, salt, seq, pk.data(), sig.data());
|
2014-02-28 05:02:48 +01:00
|
|
|
|
|
|
|
if (alerts.should_post<dht_put_alert>())
|
|
|
|
alerts.post_alert(dht_put_alert(pk, sig, salt, seq));
|
2014-02-24 01:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::dht_put_mutable_item(boost::array<char, 32> key
|
|
|
|
, boost::function<void(entry&, boost::array<char,64>&
|
|
|
|
, boost::uint64_t&, std::string const&)> cb
|
|
|
|
, std::string salt)
|
|
|
|
{
|
|
|
|
if (!m_dht) return;
|
2014-02-28 05:02:48 +01:00
|
|
|
m_dht->put_item(key.data(), boost::bind(&put_mutable_callback
|
|
|
|
, boost::ref(m_alerts), _1, cb), salt);
|
2014-02-24 01:31:13 +01:00
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
#endif
|
|
|
|
|
2010-12-29 03:17:44 +01:00
|
|
|
void session_impl::maybe_update_udp_mapping(int nat, int local_port, int external_port)
|
|
|
|
{
|
|
|
|
int local, external, protocol;
|
|
|
|
if (nat == 0 && m_natpmp.get())
|
|
|
|
{
|
|
|
|
if (m_udp_mapping[nat] != -1)
|
|
|
|
{
|
|
|
|
if (m_natpmp->get_mapping(m_udp_mapping[nat], local, external, protocol))
|
|
|
|
{
|
|
|
|
// we already have a mapping. If it's the same, don't do anything
|
|
|
|
if (local == local_port && external == external_port && protocol == natpmp::udp)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_natpmp->delete_mapping(m_udp_mapping[nat]);
|
|
|
|
}
|
|
|
|
m_udp_mapping[nat] = m_natpmp->add_mapping(natpmp::udp
|
|
|
|
, local_port, external_port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (nat == 1 && m_upnp.get())
|
|
|
|
{
|
|
|
|
if (m_udp_mapping[nat] != -1)
|
|
|
|
{
|
|
|
|
if (m_upnp->get_mapping(m_udp_mapping[nat], local, external, protocol))
|
|
|
|
{
|
|
|
|
// we already have a mapping. If it's the same, don't do anything
|
|
|
|
if (local == local_port && external == external_port && protocol == natpmp::udp)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_upnp->delete_mapping(m_udp_mapping[nat]);
|
|
|
|
}
|
|
|
|
m_udp_mapping[nat] = m_upnp->add_mapping(upnp::udp
|
|
|
|
, local_port, external_port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
void session_impl::set_pe_settings(pe_settings const& settings)
|
|
|
|
{
|
|
|
|
m_pe_settings = settings;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
bool session_impl::is_listening() const
|
|
|
|
{
|
2007-09-22 18:27:29 +02:00
|
|
|
return !m_listen_sockets.empty();
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session_impl::~session_impl()
|
|
|
|
{
|
2013-09-29 21:37:57 +02:00
|
|
|
TORRENT_ASSERT(is_not_network_thread());
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
m_io_service.post(boost::bind(&session_impl::abort, this));
|
2007-03-02 06:02:12 +01:00
|
|
|
|
2009-04-25 10:45:13 +02:00
|
|
|
// we need to wait for the disk-io thread to
|
|
|
|
// die first, to make sure it won't post any
|
|
|
|
// more messages to the io_service containing references
|
|
|
|
// to disk_io_pool inside the disk_io_thread. Once
|
|
|
|
// the main thread has handled all the outstanding requests
|
|
|
|
// we know it's safe to destruct the disk thread.
|
|
|
|
m_disk_thread.join();
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
int counter = 0;
|
|
|
|
while (log_async())
|
|
|
|
{
|
|
|
|
sleep(1000);
|
|
|
|
++counter;
|
2013-12-05 08:42:32 +01:00
|
|
|
printf("\x1b[2J\x1b[0;0H\x1b[33m==== Waiting to shut down: %d ==== conn-queue: %d connecting: %d timeout (next: %f max: %f)\x1b[0m\n\n"
|
2012-07-16 01:22:23 +02:00
|
|
|
, counter, m_half_open.size(), m_half_open.num_connecting(), m_half_open.next_timeout()
|
|
|
|
, m_half_open.max_timeout());
|
2010-11-28 02:47:30 +01:00
|
|
|
}
|
2010-12-24 19:15:01 +01:00
|
|
|
async_dec_threads();
|
2007-05-24 21:51:14 +02:00
|
|
|
#endif
|
2010-12-24 19:15:01 +01:00
|
|
|
|
2010-12-24 02:31:41 +01:00
|
|
|
if (m_thread) m_thread->join();
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
m_udp_socket.unsubscribe(this);
|
|
|
|
m_udp_socket.unsubscribe(&m_utp_socket_manager);
|
|
|
|
m_udp_socket.unsubscribe(&m_tracker_manager);
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_torrents.empty());
|
|
|
|
TORRENT_ASSERT(m_connections.empty());
|
2008-10-30 18:13:10 +01:00
|
|
|
TORRENT_ASSERT(m_connections.empty());
|
2011-02-11 18:39:22 +01:00
|
|
|
|
2011-12-29 13:15:29 +01:00
|
|
|
#ifdef TORRENT_REQUEST_LOGGING
|
|
|
|
if (m_request_log) fclose(m_request_log);
|
|
|
|
#endif
|
|
|
|
|
2011-02-11 18:39:22 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
if (m_stats_logger) fclose(m_stats_logger);
|
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
int session_impl::max_connections() const
|
|
|
|
{
|
|
|
|
return m_settings.connections_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_impl::max_uploads() const
|
|
|
|
{
|
|
|
|
return m_settings.unchoke_slots_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_impl::max_half_open_connections() const
|
|
|
|
{
|
|
|
|
return m_settings.half_open_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_local_download_rate_limit(int bytes_per_second)
|
|
|
|
{
|
|
|
|
session_settings s = m_settings;
|
|
|
|
s.local_download_rate_limit = bytes_per_second;
|
|
|
|
set_settings(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_local_upload_rate_limit(int bytes_per_second)
|
|
|
|
{
|
|
|
|
session_settings s = m_settings;
|
|
|
|
s.local_upload_rate_limit = bytes_per_second;
|
|
|
|
set_settings(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_download_rate_limit(int bytes_per_second)
|
|
|
|
{
|
|
|
|
session_settings s = m_settings;
|
|
|
|
s.download_rate_limit = bytes_per_second;
|
|
|
|
set_settings(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_upload_rate_limit(int bytes_per_second)
|
|
|
|
{
|
|
|
|
session_settings s = m_settings;
|
|
|
|
s.upload_rate_limit = bytes_per_second;
|
|
|
|
set_settings(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_max_half_open_connections(int limit)
|
|
|
|
{
|
|
|
|
session_settings s = m_settings;
|
|
|
|
s.half_open_limit = limit;
|
|
|
|
set_settings(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::set_max_connections(int limit)
|
|
|
|
{
|
|
|
|
session_settings s = m_settings;
|
|
|
|
s.connections_limit = limit;
|
|
|
|
set_settings(s);
|
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
void session_impl::set_max_uploads(int limit)
|
|
|
|
{
|
2010-10-09 21:09:38 +02:00
|
|
|
session_settings s = m_settings;
|
|
|
|
s.unchoke_slots_limit = limit;
|
|
|
|
set_settings(s);
|
|
|
|
}
|
2007-08-21 20:33:28 +02:00
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
int session_impl::local_upload_rate_limit() const
|
|
|
|
{
|
|
|
|
return m_local_upload_channel.throttle();
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_impl::local_download_rate_limit() const
|
|
|
|
{
|
|
|
|
return m_local_download_channel.throttle();
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_impl::upload_rate_limit() const
|
|
|
|
{
|
|
|
|
return m_upload_channel.throttle();
|
|
|
|
}
|
2007-08-21 20:33:28 +02:00
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
int session_impl::download_rate_limit() const
|
|
|
|
{
|
|
|
|
return m_download_channel.throttle();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void session_impl::update_unchoke_limit()
|
|
|
|
{
|
|
|
|
m_allowed_upload_slots = m_settings.unchoke_slots_limit;
|
2011-12-19 06:53:11 +01:00
|
|
|
if (m_allowed_upload_slots < 0)
|
|
|
|
m_allowed_upload_slots = (std::numeric_limits<int>::max)();
|
|
|
|
|
2010-08-27 16:52:42 +02:00
|
|
|
if (m_settings.num_optimistic_unchoke_slots >= m_allowed_upload_slots / 2)
|
2010-02-06 08:39:45 +01:00
|
|
|
{
|
|
|
|
if (m_alerts.should_post<performance_alert>())
|
|
|
|
m_alerts.post_alert(performance_alert(torrent_handle()
|
|
|
|
, performance_alert::too_many_optimistic_unchoke_slots));
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
void session_impl::update_rate_settings()
|
|
|
|
{
|
|
|
|
if (m_settings.half_open_limit <= 0) m_settings.half_open_limit
|
|
|
|
= (std::numeric_limits<int>::max)();
|
|
|
|
m_half_open.limit(m_settings.half_open_limit);
|
|
|
|
|
|
|
|
if (m_settings.local_download_rate_limit < 0)
|
|
|
|
m_settings.local_download_rate_limit = 0;
|
|
|
|
m_local_download_channel.throttle(m_settings.local_download_rate_limit);
|
|
|
|
|
|
|
|
if (m_settings.local_upload_rate_limit < 0)
|
|
|
|
m_settings.local_upload_rate_limit = 0;
|
|
|
|
m_local_upload_channel.throttle(m_settings.local_upload_rate_limit);
|
|
|
|
|
|
|
|
if (m_settings.download_rate_limit < 0)
|
|
|
|
m_settings.download_rate_limit = 0;
|
|
|
|
m_download_channel.throttle(m_settings.download_rate_limit);
|
|
|
|
|
|
|
|
if (m_settings.upload_rate_limit < 0)
|
|
|
|
m_settings.upload_rate_limit = 0;
|
|
|
|
m_upload_channel.throttle(m_settings.upload_rate_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::update_connections_limit()
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-10-09 21:09:38 +02:00
|
|
|
if (m_settings.connections_limit <= 0)
|
2008-05-16 17:19:38 +02:00
|
|
|
{
|
2010-10-09 21:09:38 +02:00
|
|
|
m_settings.connections_limit = (std::numeric_limits<int>::max)();
|
2010-02-14 05:05:18 +01:00
|
|
|
#if TORRENT_USE_RLIMIT
|
2008-05-16 17:19:38 +02:00
|
|
|
rlimit l;
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &l) == 0
|
|
|
|
&& l.rlim_cur != RLIM_INFINITY)
|
|
|
|
{
|
2010-10-09 21:09:38 +02:00
|
|
|
m_settings.connections_limit = l.rlim_cur - m_settings.file_pool_size;
|
|
|
|
if (m_settings.connections_limit < 5) m_settings.connections_limit = 5;
|
2008-05-16 17:19:38 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2010-10-03 12:07:38 +02:00
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
if (num_connections() > m_settings.connections_limit && !m_torrents.empty())
|
2010-10-03 12:07:38 +02:00
|
|
|
{
|
|
|
|
// if we have more connections that we're allowed, disconnect
|
|
|
|
// peers from the torrents so that they are all as even as possible
|
|
|
|
|
2010-10-09 21:09:38 +02:00
|
|
|
int to_disconnect = num_connections() - m_settings.connections_limit;
|
2010-10-03 12:07:38 +02:00
|
|
|
|
|
|
|
int last_average = 0;
|
2010-10-09 21:09:38 +02:00
|
|
|
int average = m_settings.connections_limit / m_torrents.size();
|
2010-10-03 12:07:38 +02:00
|
|
|
|
|
|
|
// the number of slots that are unused by torrents
|
2010-10-09 21:09:38 +02:00
|
|
|
int extra = m_settings.connections_limit % m_torrents.size();
|
2010-10-03 12:07:38 +02:00
|
|
|
|
|
|
|
// run 3 iterations of this, then we're probably close enough
|
|
|
|
for (int iter = 0; iter < 4; ++iter)
|
|
|
|
{
|
|
|
|
// the number of torrents that are above average
|
|
|
|
int num_above = 0;
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
int num = i->second->num_peers();
|
|
|
|
if (num <= last_average) continue;
|
|
|
|
if (num > average) ++num_above;
|
|
|
|
if (num < average) extra += average - num;
|
|
|
|
}
|
|
|
|
|
|
|
|
// distribute extra among the torrents that are above average
|
|
|
|
if (num_above == 0) num_above = 1;
|
|
|
|
last_average = average;
|
|
|
|
average += extra / num_above;
|
|
|
|
if (extra == 0) break;
|
|
|
|
// save the remainder for the next iteration
|
|
|
|
extra = extra % num_above;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
int num = i->second->num_peers();
|
|
|
|
if (num <= average) continue;
|
|
|
|
|
|
|
|
// distribute the remainder
|
|
|
|
int my_average = average;
|
|
|
|
if (extra > 0)
|
|
|
|
{
|
|
|
|
++my_average;
|
|
|
|
--extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
int disconnect = (std::min)(to_disconnect, num - my_average);
|
|
|
|
to_disconnect -= disconnect;
|
|
|
|
i->second->disconnect_peers(disconnect
|
|
|
|
, error_code(errors::too_many_connections, get_libtorrent_category()));
|
|
|
|
}
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
void session_impl::set_alert_dispatch(boost::function<void(std::auto_ptr<alert>)> const& fun)
|
2008-12-15 08:12:08 +01:00
|
|
|
{
|
|
|
|
m_alerts.set_dispatch_function(fun);
|
|
|
|
}
|
|
|
|
|
2006-10-11 22:57:54 +02:00
|
|
|
std::auto_ptr<alert> session_impl::pop_alert()
|
|
|
|
{
|
2010-07-14 06:16:38 +02:00
|
|
|
return m_alerts.get();
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
2007-11-25 09:18:57 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
void session_impl::pop_alerts(std::deque<alert*>* alerts)
|
|
|
|
{
|
|
|
|
m_alerts.get_all(alerts);
|
|
|
|
}
|
|
|
|
|
2007-11-25 09:18:57 +01:00
|
|
|
alert const* session_impl::wait_for_alert(time_duration max_wait)
|
|
|
|
{
|
|
|
|
return m_alerts.wait_for_alert(max_wait);
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2011-06-13 18:46:46 +02:00
|
|
|
void session_impl::set_alert_mask(boost::uint32_t m)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
m_alerts.set_alert_mask(m);
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
|
2011-01-23 19:00:52 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2008-10-07 07:46:42 +02:00
|
|
|
size_t session_impl::set_alert_queue_size_limit(size_t queue_size_limit_)
|
|
|
|
{
|
2011-01-23 19:00:52 +01:00
|
|
|
m_settings.alert_queue_size = queue_size_limit_;
|
2008-10-07 07:46:42 +02:00
|
|
|
return m_alerts.set_alert_queue_size_limit(queue_size_limit_);
|
|
|
|
}
|
2011-01-23 19:00:52 +01:00
|
|
|
#endif
|
2008-10-07 07:46:42 +02:00
|
|
|
|
2007-05-31 02:21:54 +02:00
|
|
|
void session_impl::start_lsd()
|
|
|
|
{
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-11-23 22:11:31 +01:00
|
|
|
if (m_lsd) return;
|
|
|
|
|
2007-09-29 23:31:51 +02:00
|
|
|
m_lsd = new lsd(m_io_service
|
2007-05-31 02:21:54 +02:00
|
|
|
, m_listen_interface.address()
|
2010-07-22 18:49:40 +02:00
|
|
|
, boost::bind(&session_impl::on_lsd_peer, this, _1, _2));
|
2007-05-31 02:21:54 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
natpmp* session_impl::start_natpmp()
|
2007-05-31 02:21:54 +02:00
|
|
|
{
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
if (m_natpmp) return m_natpmp.get();
|
|
|
|
|
|
|
|
// the natpmp constructor may fail and call the callbacks
|
|
|
|
// into the session_impl.
|
|
|
|
natpmp* n = new (std::nothrow) natpmp(m_io_service
|
|
|
|
, m_listen_interface.address()
|
|
|
|
, boost::bind(&session_impl::on_port_mapping
|
2010-12-05 21:40:28 +01:00
|
|
|
, this, _1, _2, _3, _4, 0)
|
2010-07-14 06:16:38 +02:00
|
|
|
, boost::bind(&session_impl::on_port_map_log
|
|
|
|
, this, _1, 0));
|
|
|
|
if (n == 0) return 0;
|
|
|
|
|
2009-06-13 12:45:07 +02:00
|
|
|
m_natpmp = n;
|
2007-05-31 02:21:54 +02:00
|
|
|
|
2008-04-13 21:19:22 +02:00
|
|
|
if (m_listen_interface.port() > 0)
|
|
|
|
{
|
2012-01-14 17:04:25 +01:00
|
|
|
remap_tcp_ports(1, m_listen_interface.port(), ssl_listen_port());
|
2008-04-13 21:19:22 +02:00
|
|
|
}
|
2010-05-30 03:33:03 +02:00
|
|
|
if (m_udp_socket.is_open())
|
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
m_udp_mapping[0] = m_natpmp->add_mapping(natpmp::udp
|
2010-05-30 03:33:03 +02:00
|
|
|
, m_listen_interface.port(), m_listen_interface.port());
|
|
|
|
}
|
2010-07-15 08:02:58 +02:00
|
|
|
return n;
|
2007-05-31 02:21:54 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
upnp* session_impl::start_upnp()
|
2007-05-31 02:21:54 +02:00
|
|
|
{
|
2007-08-21 20:33:28 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
if (m_upnp) return m_upnp.get();
|
|
|
|
|
|
|
|
// the upnp constructor may fail and call the callbacks
|
|
|
|
upnp* u = new (std::nothrow) upnp(m_io_service
|
|
|
|
, m_half_open
|
|
|
|
, m_listen_interface.address()
|
|
|
|
, m_settings.user_agent
|
|
|
|
, boost::bind(&session_impl::on_port_mapping
|
2010-12-05 21:40:28 +01:00
|
|
|
, this, _1, _2, _3, _4, 1)
|
2010-07-14 06:16:38 +02:00
|
|
|
, boost::bind(&session_impl::on_port_map_log
|
|
|
|
, this, _1, 1)
|
|
|
|
, m_settings.upnp_ignore_nonrouters);
|
|
|
|
|
|
|
|
if (u == 0) return 0;
|
|
|
|
|
2009-06-13 12:45:07 +02:00
|
|
|
m_upnp = u;
|
2007-05-31 02:21:54 +02:00
|
|
|
|
2007-12-24 09:18:53 +01:00
|
|
|
m_upnp->discover_device();
|
2012-01-14 17:04:25 +01:00
|
|
|
if (m_listen_interface.port() > 0 || ssl_listen_port() > 0)
|
2008-04-13 21:19:22 +02:00
|
|
|
{
|
2012-01-14 17:04:25 +01:00
|
|
|
remap_tcp_ports(2, m_listen_interface.port(), ssl_listen_port());
|
2008-04-13 21:19:22 +02:00
|
|
|
}
|
2010-05-30 03:33:03 +02:00
|
|
|
if (m_udp_socket.is_open())
|
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
m_udp_mapping[1] = m_upnp->add_mapping(upnp::udp
|
2010-05-30 03:33:03 +02:00
|
|
|
, m_listen_interface.port(), m_listen_interface.port());
|
|
|
|
}
|
2010-07-15 08:02:58 +02:00
|
|
|
return u;
|
2007-05-31 02:21:54 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 23:24:56 +01:00
|
|
|
int session_impl::add_port_mapping(int t, int external_port
|
2013-12-31 21:42:37 +01:00
|
|
|
, int local_port)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-12-31 23:24:56 +01:00
|
|
|
if (m_upnp) ret = m_upnp->add_mapping((upnp::protocol_type)t, external_port
|
|
|
|
, local_port);
|
|
|
|
if (m_natpmp) ret = m_natpmp->add_mapping((natpmp::protocol_type)t, external_port
|
|
|
|
, local_port);
|
2013-12-31 21:42:37 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::delete_port_mapping(int handle)
|
|
|
|
{
|
2013-12-31 23:24:56 +01:00
|
|
|
if (m_upnp) m_upnp->delete_mapping(handle);
|
|
|
|
if (m_natpmp) m_natpmp->delete_mapping(handle);
|
2013-12-31 21:42:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-31 02:21:54 +02:00
|
|
|
void session_impl::stop_lsd()
|
|
|
|
{
|
2007-11-19 03:24:07 +01:00
|
|
|
if (m_lsd.get())
|
|
|
|
m_lsd->close();
|
2007-09-29 23:31:51 +02:00
|
|
|
m_lsd = 0;
|
2007-05-31 02:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::stop_natpmp()
|
|
|
|
{
|
|
|
|
if (m_natpmp.get())
|
|
|
|
m_natpmp->close();
|
2007-09-29 23:31:51 +02:00
|
|
|
m_natpmp = 0;
|
2007-05-31 02:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_impl::stop_upnp()
|
|
|
|
{
|
|
|
|
if (m_upnp.get())
|
2008-04-06 21:17:58 +02:00
|
|
|
{
|
2007-05-31 02:21:54 +02:00
|
|
|
m_upnp->close();
|
2008-04-06 21:17:58 +02:00
|
|
|
m_udp_mapping[1] = -1;
|
|
|
|
m_tcp_mapping[1] = -1;
|
2012-01-14 17:04:25 +01:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
m_ssl_mapping[1] = -1;
|
|
|
|
#endif
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
2007-09-29 23:31:51 +02:00
|
|
|
m_upnp = 0;
|
2007-05-31 02:21:54 +02:00
|
|
|
}
|
2013-01-02 00:12:16 +01:00
|
|
|
|
|
|
|
external_ip const& session_impl::external_address() const
|
|
|
|
{ return m_external_ip; }
|
2010-12-24 02:31:41 +01:00
|
|
|
|
2013-02-05 05:18:44 +01:00
|
|
|
// this is the DHT observer version. DHT is the implied source
|
|
|
|
void session_impl::set_external_address(address const& ip
|
|
|
|
, address const& source)
|
|
|
|
{
|
|
|
|
set_external_address(ip, source_dht, source);
|
|
|
|
}
|
|
|
|
|
2010-12-24 02:31:41 +01:00
|
|
|
void session_impl::set_external_address(address const& ip
|
|
|
|
, int source_type, address const& source)
|
2008-03-29 23:45:55 +01:00
|
|
|
{
|
2010-12-24 02:31:41 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(": set_external_address(%s, %d, %s)", print_address(ip).c_str()
|
|
|
|
, source_type, print_address(source).c_str());
|
2010-12-24 02:31:41 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-02 00:43:52 +01:00
|
|
|
if (!m_external_ip.cast_vote(ip, source_type, source)) return;
|
2010-12-24 02:31:41 +01:00
|
|
|
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2012-10-18 09:32:16 +02:00
|
|
|
session_log(" external IP updated");
|
2010-12-24 02:31:41 +01:00
|
|
|
#endif
|
2010-12-12 10:15:54 +01:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (m_alerts.should_post<external_ip_alert>())
|
|
|
|
m_alerts.post_alert(external_ip_alert(ip));
|
2010-12-11 10:38:07 +01:00
|
|
|
|
2014-01-23 04:31:36 +01:00
|
|
|
for (torrent_map::iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
i->second->new_external_ip();
|
|
|
|
}
|
|
|
|
|
2010-12-11 10:38:07 +01:00
|
|
|
// since we have a new external IP now, we need to
|
|
|
|
// restart the DHT with a new node ID
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2013-01-21 00:21:53 +01:00
|
|
|
// TODO: 1 we only need to do this if our global IPv4 address has changed
|
|
|
|
// since the DHT (currently) only supports IPv4. Since restarting the DHT
|
|
|
|
// is kind of expensive, it would be nice to not do it unnecessarily
|
2010-12-12 10:15:54 +01:00
|
|
|
if (m_dht)
|
|
|
|
{
|
|
|
|
entry s = m_dht->state();
|
|
|
|
int cur_state = 0;
|
|
|
|
int prev_state = 0;
|
|
|
|
entry* nodes1 = s.find_key("nodes");
|
|
|
|
if (nodes1 && nodes1->type() == entry::list_t) cur_state = nodes1->list().size();
|
|
|
|
entry* nodes2 = m_dht_state.find_key("nodes");
|
|
|
|
if (nodes2 && nodes2->type() == entry::list_t) prev_state = nodes2->list().size();
|
|
|
|
if (cur_state > prev_state) m_dht_state = s;
|
|
|
|
start_dht(m_dht_state);
|
|
|
|
}
|
2010-12-11 10:38:07 +01:00
|
|
|
#endif
|
2008-03-29 23:45:55 +01:00
|
|
|
}
|
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
void session_impl::free_disk_buffer(char* buf)
|
|
|
|
{
|
|
|
|
m_disk_thread.free_buffer(buf);
|
|
|
|
}
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2009-01-23 10:13:31 +01:00
|
|
|
char* session_impl::allocate_disk_buffer(char const* category)
|
2008-04-10 12:03:23 +02:00
|
|
|
{
|
2009-01-23 10:13:31 +01:00
|
|
|
return m_disk_thread.allocate_buffer(category);
|
2008-04-10 12:03:23 +02:00
|
|
|
}
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2011-05-19 04:41:28 +02:00
|
|
|
char* session_impl::allocate_buffer()
|
2007-09-29 18:14:03 +02:00
|
|
|
{
|
2011-05-19 04:41:28 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2007-12-27 22:43:11 +01:00
|
|
|
|
2011-02-03 05:09:50 +01:00
|
|
|
#ifdef TORRENT_DISK_STATS
|
2008-02-14 04:48:20 +01:00
|
|
|
TORRENT_ASSERT(m_buffer_allocations >= 0);
|
2011-05-19 04:41:28 +02:00
|
|
|
m_buffer_allocations++;
|
2007-09-29 18:14:03 +02:00
|
|
|
m_buffer_usage_logger << log_time() << " protocol_buffer: "
|
|
|
|
<< (m_buffer_allocations * send_buffer_size) << std::endl;
|
|
|
|
#endif
|
2008-04-09 07:19:11 +02:00
|
|
|
#ifdef TORRENT_DISABLE_POOL_ALLOCATOR
|
2011-05-19 04:41:28 +02:00
|
|
|
int num_bytes = send_buffer_size;
|
|
|
|
return (char*)malloc(num_bytes);
|
2008-04-09 07:19:11 +02:00
|
|
|
#else
|
2011-05-19 04:41:28 +02:00
|
|
|
return (char*)m_send_buffers.malloc();
|
2008-04-09 07:19:11 +02:00
|
|
|
#endif
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
|
|
|
|
2011-02-03 05:09:50 +01:00
|
|
|
#ifdef TORRENT_DISK_STATS
|
2008-12-26 08:00:21 +01:00
|
|
|
void session_impl::log_buffer_usage()
|
|
|
|
{
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2008-12-26 08:00:21 +01:00
|
|
|
int send_buffer_capacity = 0;
|
|
|
|
int used_send_buffer = 0;
|
|
|
|
for (connection_map::const_iterator i = m_connections.begin()
|
|
|
|
, end(m_connections.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
send_buffer_capacity += (*i)->send_buffer_capacity();
|
|
|
|
used_send_buffer += (*i)->send_buffer_size();
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(send_buffer_capacity >= used_send_buffer);
|
|
|
|
m_buffer_usage_logger << log_time() << " send_buffer_size: " << send_buffer_capacity << std::endl;
|
|
|
|
m_buffer_usage_logger << log_time() << " used_send_buffer: " << used_send_buffer << std::endl;
|
|
|
|
m_buffer_usage_logger << log_time() << " send_buffer_utilization: "
|
2014-01-22 02:59:24 +01:00
|
|
|
<< (used_send_buffer * 100.f / (std::max)(send_buffer_capacity, 1)) << std::endl;
|
2008-12-26 08:00:21 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-19 04:41:28 +02:00
|
|
|
void session_impl::free_buffer(char* buf)
|
2007-09-29 18:14:03 +02:00
|
|
|
{
|
2011-05-19 04:41:28 +02:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
2007-12-27 22:43:11 +01:00
|
|
|
|
2011-02-03 05:09:50 +01:00
|
|
|
#ifdef TORRENT_DISK_STATS
|
2011-05-19 04:41:28 +02:00
|
|
|
m_buffer_allocations--;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_buffer_allocations >= 0);
|
2007-09-29 18:14:03 +02:00
|
|
|
m_buffer_usage_logger << log_time() << " protocol_buffer: "
|
|
|
|
<< (m_buffer_allocations * send_buffer_size) << std::endl;
|
|
|
|
#endif
|
2008-04-09 07:19:11 +02:00
|
|
|
#ifdef TORRENT_DISABLE_POOL_ALLOCATOR
|
|
|
|
free(buf);
|
|
|
|
#else
|
2011-05-19 04:41:28 +02:00
|
|
|
m_send_buffers.free(buf);
|
2008-04-09 07:19:11 +02:00
|
|
|
#endif
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
2007-01-02 00:51:24 +01:00
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2007-08-21 20:33:28 +02:00
|
|
|
void session_impl::check_invariant() const
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2010-12-04 23:20:31 +01:00
|
|
|
TORRENT_ASSERT(is_network_thread());
|
|
|
|
|
2012-08-29 03:58:06 +02:00
|
|
|
if (m_settings.unchoke_slots_limit < 0
|
|
|
|
&& m_settings.choking_algorithm == session_settings::fixed_slots_choker)
|
|
|
|
TORRENT_ASSERT(m_allowed_upload_slots == (std::numeric_limits<int>::max)());
|
|
|
|
|
2009-08-30 09:38:52 +02:00
|
|
|
int num_checking = 0;
|
2011-08-06 19:54:33 +02:00
|
|
|
int num_queued_for_checking = 0;
|
2009-08-30 09:38:52 +02:00
|
|
|
for (check_queue_t::const_iterator i = m_queued_for_checking.begin()
|
|
|
|
, end(m_queued_for_checking.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->state() == torrent_status::checking_files) ++num_checking;
|
2011-08-06 19:54:33 +02:00
|
|
|
else if ((*i)->state() == torrent_status::queued_for_checking)
|
|
|
|
{
|
|
|
|
++num_queued_for_checking;
|
|
|
|
}
|
2009-08-30 09:38:52 +02:00
|
|
|
}
|
2008-11-19 01:46:48 +01:00
|
|
|
|
|
|
|
// the queue is either empty, or it has exactly one checking torrent in it
|
2011-06-03 10:40:13 +02:00
|
|
|
TORRENT_ASSERT(m_queued_for_checking.empty() || num_checking == 1 || (m_paused && num_checking == 0));
|
2011-08-06 19:54:33 +02:00
|
|
|
// TORRENT_ASSERT(m_queued_for_checking.size() == num_queued_for_checking);
|
2008-11-19 01:46:48 +01:00
|
|
|
|
2008-05-29 05:37:19 +02:00
|
|
|
std::set<int> unique;
|
2012-06-21 05:51:39 +02:00
|
|
|
int num_active_downloading = 0;
|
|
|
|
int num_active_finished = 0;
|
2008-05-29 05:37:19 +02:00
|
|
|
int total_downloaders = 0;
|
|
|
|
for (torrent_map::const_iterator i = m_torrents.begin()
|
|
|
|
, end(m_torrents.end()); i != end; ++i)
|
|
|
|
{
|
2012-06-21 05:51:39 +02:00
|
|
|
boost::shared_ptr<torrent> t = i->second;
|
|
|
|
if (t->is_active_download()) ++num_active_downloading;
|
|
|
|
else if (t->is_active_finished()) ++num_active_finished;
|
|
|
|
|
|
|
|
int pos = t->queue_position();
|
2008-05-29 05:37:19 +02:00
|
|
|
if (pos < 0)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(pos == -1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++total_downloaders;
|
2012-06-21 05:51:39 +02:00
|
|
|
|
|
|
|
unique.insert(t->queue_position());
|
2008-05-29 05:37:19 +02:00
|
|
|
}
|
2008-09-25 22:12:53 +02:00
|
|
|
TORRENT_ASSERT(int(unique.size()) == total_downloaders);
|
2012-06-21 05:51:39 +02:00
|
|
|
TORRENT_ASSERT(num_active_downloading == m_num_active_downloading);
|
|
|
|
TORRENT_ASSERT(num_active_finished == m_num_active_finished);
|
2008-05-29 05:37:19 +02:00
|
|
|
|
2008-10-19 00:35:10 +02:00
|
|
|
std::set<peer_connection*> unique_peers;
|
2010-10-09 21:09:38 +02:00
|
|
|
TORRENT_ASSERT(m_settings.connections_limit > 0);
|
2010-02-09 04:04:41 +01:00
|
|
|
if (m_settings.choking_algorithm == session_settings::auto_expand_choker)
|
2010-10-09 21:09:38 +02:00
|
|
|
TORRENT_ASSERT(m_allowed_upload_slots >= m_settings.unchoke_slots_limit);
|
2007-08-16 14:41:46 +02:00
|
|
|
int unchokes = 0;
|
|
|
|
int num_optimistic = 0;
|
2011-01-30 11:04:15 +01:00
|
|
|
int disk_queue[2] = {0, 0};
|
2007-08-21 20:33:28 +02:00
|
|
|
for (connection_map::const_iterator i = m_connections.begin();
|
2006-10-11 22:57:54 +02:00
|
|
|
i != m_connections.end(); ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
TORRENT_ASSERT(*i);
|
|
|
|
boost::shared_ptr<torrent> t = (*i)->associated_torrent().lock();
|
2008-10-19 00:35:10 +02:00
|
|
|
TORRENT_ASSERT(unique_peers.find(i->get()) == unique_peers.end());
|
|
|
|
unique_peers.insert(i->get());
|
2006-10-11 22:57:54 +02:00
|
|
|
|
2011-10-29 01:08:52 +02:00
|
|
|
if ((*i)->m_channel_state[0] & peer_info::bw_disk) ++disk_queue[0];
|
|
|
|
if ((*i)->m_channel_state[1] & peer_info::bw_disk) ++disk_queue[1];
|
2011-01-30 11:04:15 +01:00
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_connection* p = i->get();
|
2008-01-19 20:00:54 +01:00
|
|
|
TORRENT_ASSERT(!p->is_disconnecting());
|
2009-01-28 07:14:21 +01:00
|
|
|
if (p->ignore_unchoke_slots()) continue;
|
2007-10-31 10:48:20 +01:00
|
|
|
if (!p->is_choked()) ++unchokes;
|
|
|
|
if (p->peer_info_struct()
|
|
|
|
&& p->peer_info_struct()->optimistically_unchoked)
|
2007-08-19 10:23:44 +02:00
|
|
|
{
|
2007-08-16 14:41:46 +02:00
|
|
|
++num_optimistic;
|
2007-10-31 10:48:20 +01:00
|
|
|
TORRENT_ASSERT(!p->is_choked());
|
2007-08-19 10:23:44 +02:00
|
|
|
}
|
2012-03-24 02:29:31 +01:00
|
|
|
if (t && p->peer_info_struct() && !p->peer_info_struct()->web_seed)
|
2006-10-11 22:57:54 +02:00
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
TORRENT_ASSERT(t->get_policy().has_connection(p));
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
}
|
2010-02-02 19:39:32 +01:00
|
|
|
|
2011-01-30 11:04:15 +01:00
|
|
|
TORRENT_ASSERT(disk_queue[0] == m_disk_queues[0]);
|
|
|
|
TORRENT_ASSERT(disk_queue[1] == m_disk_queues[1]);
|
|
|
|
|
2010-02-02 19:39:32 +01:00
|
|
|
if (m_settings.num_optimistic_unchoke_slots)
|
2010-08-27 16:52:42 +02:00
|
|
|
{
|
2010-02-02 19:39:32 +01:00
|
|
|
TORRENT_ASSERT(num_optimistic <= m_settings.num_optimistic_unchoke_slots);
|
2010-08-27 16:52:42 +02:00
|
|
|
}
|
2010-02-02 19:39:32 +01:00
|
|
|
|
2007-08-21 23:51:29 +02:00
|
|
|
if (m_num_unchoked != unchokes)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2007-08-21 23:51:29 +02:00
|
|
|
}
|
2011-12-21 22:21:19 +01:00
|
|
|
for (torrent_map::const_iterator j
|
2007-11-27 03:46:19 +01:00
|
|
|
= m_torrents.begin(); j != m_torrents.end(); ++j)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(boost::get_pointer(j->second));
|
|
|
|
}
|
2006-10-11 22:57:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-19 21:06:27 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
tracker_logger::tracker_logger(session_impl& ses): m_ses(ses) {}
|
|
|
|
void tracker_logger::tracker_warning(tracker_request const& req
|
|
|
|
, std::string const& str)
|
|
|
|
{
|
|
|
|
debug_log("*** tracker warning: %s", str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_logger::tracker_response(tracker_request const&
|
|
|
|
, libtorrent::address const& tracker_ip
|
|
|
|
, std::list<address> const& ip_list
|
|
|
|
, std::vector<peer_entry>& peers
|
|
|
|
, int interval
|
|
|
|
, int min_interval
|
|
|
|
, int complete
|
|
|
|
, int incomplete
|
|
|
|
, int downloaded
|
|
|
|
, address const& external_ip
|
|
|
|
, std::string const& tracker_id)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
s = "TRACKER RESPONSE:\n";
|
|
|
|
char tmp[200];
|
|
|
|
snprintf(tmp, 200, "interval: %d\nmin_interval: %d\npeers:\n", interval, min_interval);
|
|
|
|
s += tmp;
|
|
|
|
for (std::vector<peer_entry>::const_iterator i = peers.begin();
|
|
|
|
i != peers.end(); ++i)
|
|
|
|
{
|
|
|
|
char pid[41];
|
|
|
|
to_hex((const char*)&i->pid[0], 20, pid);
|
|
|
|
if (i->pid.is_all_zeros()) pid[0] = 0;
|
|
|
|
|
|
|
|
snprintf(tmp, 200, " %-16s %-5d %s\n", i->ip.c_str(), i->port, pid);
|
|
|
|
s += tmp;
|
|
|
|
}
|
|
|
|
snprintf(tmp, 200, "external ip: %s\n", print_address(external_ip).c_str());
|
|
|
|
s += tmp;
|
|
|
|
debug_log("%s", s.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_logger::tracker_request_timed_out(
|
|
|
|
tracker_request const&)
|
|
|
|
{
|
|
|
|
debug_log("*** tracker timed out");
|
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_logger::tracker_request_error(tracker_request const& r
|
|
|
|
, int response_code, error_code const& ec, const std::string& str
|
|
|
|
, int retry_interval)
|
|
|
|
{
|
|
|
|
debug_log("*** tracker error: %d: %s %s"
|
|
|
|
, response_code, ec.message().c_str(), str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tracker_logger::debug_log(const char* fmt, ...) const
|
|
|
|
{
|
|
|
|
if (!m_ses.m_logger) return;
|
|
|
|
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
|
|
|
|
|
|
|
char usr[1024];
|
|
|
|
vsnprintf(usr, sizeof(usr), fmt, v);
|
|
|
|
va_end(v);
|
|
|
|
char buf[1280];
|
|
|
|
snprintf(buf, sizeof(buf), "%s: %s\n", time_now_string(), usr);
|
|
|
|
(*m_ses.m_logger) << buf;
|
|
|
|
}
|
|
|
|
#endif
|
2006-10-11 22:57:54 +02:00
|
|
|
}}
|
|
|
|
|