2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2013-12-25 18:07:16 +01:00
|
|
|
Copyright (c) 2003-2013, Arvid Norberg
|
2003-10-23 01:00:57 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2004-11-30 12:17:32 +01:00
|
|
|
#include <vector>
|
2009-11-28 23:41:21 +01:00
|
|
|
#include <boost/limits.hpp>
|
2006-04-25 23:04:48 +02:00
|
|
|
#include <boost/bind.hpp>
|
2012-12-31 07:54:54 +01:00
|
|
|
#include <boost/cstdint.hpp>
|
2011-05-01 10:58:45 +02:00
|
|
|
#include <stdarg.h> // for va_start, va_end
|
2004-01-12 21:31:27 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/peer_connection.hpp"
|
2003-12-22 08:14:35 +01:00
|
|
|
#include "libtorrent/identify_client.hpp"
|
2004-01-04 05:29:13 +01:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
2004-01-18 20:12:18 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2004-01-25 19:18:36 +01:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
2004-01-26 18:39:44 +01:00
|
|
|
#include "libtorrent/io.hpp"
|
2006-07-08 21:41:39 +02:00
|
|
|
#include "libtorrent/file.hpp"
|
2004-04-17 14:29:35 +02:00
|
|
|
#include "libtorrent/version.hpp"
|
2006-11-14 01:08:16 +01:00
|
|
|
#include "libtorrent/extensions.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2007-04-10 23:23:13 +02:00
|
|
|
#include "libtorrent/policy.hpp"
|
2007-04-25 20:26:35 +02:00
|
|
|
#include "libtorrent/socket_type.hpp"
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2008-12-26 08:00:21 +01:00
|
|
|
#include "libtorrent/broadcast_socket.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/torrent.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2010-11-29 02:33:05 +01:00
|
|
|
#include "libtorrent/bt_peer_connection.hpp"
|
2011-02-04 05:31:20 +01:00
|
|
|
#include "libtorrent/error.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-05-04 02:08:00 +02:00
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
#include <set>
|
|
|
|
#endif
|
|
|
|
|
2008-06-23 17:37:24 +02:00
|
|
|
//#define TORRENT_CORRUPT_DATA
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
using boost::shared_ptr;
|
2006-10-11 16:02:21 +02:00
|
|
|
using libtorrent::aux::session_impl;
|
2004-10-14 03:17:04 +02:00
|
|
|
|
2004-01-04 05:29:13 +01:00
|
|
|
namespace libtorrent
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2010-12-24 02:22:49 +01:00
|
|
|
int round_up8(int v)
|
|
|
|
{
|
|
|
|
return ((v & 7) == 0) ? v : v + (8 - (v & 7));
|
|
|
|
}
|
|
|
|
|
2011-12-29 13:15:29 +01:00
|
|
|
#if defined TORRENT_REQUEST_LOGGING
|
|
|
|
void write_request_log(FILE* f, sha1_hash const& ih
|
|
|
|
, peer_connection* p, peer_request const& r)
|
|
|
|
{
|
|
|
|
// the event format in the log is:
|
|
|
|
// uint64_t timestamp (microseconds)
|
|
|
|
// uint64_t info-hash prefix
|
|
|
|
// uint32_t peer identifier
|
|
|
|
// uint32_t piece
|
|
|
|
// uint32_t start offset
|
|
|
|
// uint32_t length
|
|
|
|
char event[32];
|
|
|
|
char* ptr = event;
|
|
|
|
detail::write_uint64(total_microseconds((time_now_hires() - min_time())), ptr);
|
|
|
|
memcpy(ptr, &ih[0], 8);
|
|
|
|
ptr += 8;
|
2013-07-30 17:59:41 +02:00
|
|
|
detail::write_uint32(uintptr_t(p) & 0xffffffff, ptr);
|
2011-12-29 13:15:29 +01:00
|
|
|
detail::write_uint32(r.piece, ptr);
|
|
|
|
detail::write_uint32(r.start, ptr);
|
|
|
|
detail::write_uint32(r.length, ptr);
|
|
|
|
|
|
|
|
int ret = fwrite(event, 1, sizeof(event), f);
|
|
|
|
if (ret != sizeof(event))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR writing to request log: (%d) %s\n"
|
|
|
|
, errno, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-10 03:50:11 +02:00
|
|
|
// outbound connection
|
2004-01-04 05:29:13 +01:00
|
|
|
peer_connection::peer_connection(
|
2006-10-11 16:02:21 +02:00
|
|
|
session_impl& ses
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::weak_ptr<torrent> tor
|
2007-04-23 23:36:21 +02:00
|
|
|
, shared_ptr<socket_type> s
|
2008-05-12 12:10:39 +02:00
|
|
|
, tcp::endpoint const& endp
|
2011-09-12 05:51:49 +02:00
|
|
|
, policy::peer* peerinfo
|
|
|
|
, bool outgoing)
|
2013-12-02 05:24:10 +01:00
|
|
|
: m_received_listen_port(false)
|
|
|
|
, m_have_all(false)
|
|
|
|
, m_peer_interested(false)
|
|
|
|
, m_peer_choked(true)
|
|
|
|
, m_interesting(false)
|
|
|
|
, m_choked(true)
|
|
|
|
, m_failed(false)
|
|
|
|
, m_disconnecting(false)
|
|
|
|
, m_bitfield_received(false)
|
|
|
|
, m_endgame_mode(false)
|
|
|
|
, m_sent_suggests(false)
|
|
|
|
, m_holepunch_mode(false)
|
|
|
|
, m_ignore_stats(false)
|
|
|
|
, m_corked(false)
|
|
|
|
, m_has_metadata(true)
|
|
|
|
, m_exceeded_limit(false)
|
|
|
|
, m_ses(ses)
|
2009-03-31 10:02:25 +02:00
|
|
|
, m_work(ses.m_io_service)
|
2007-04-05 00:27:36 +02:00
|
|
|
, m_last_piece(time_now())
|
2007-05-25 21:42:10 +02:00
|
|
|
, m_last_request(time_now())
|
2007-09-01 09:38:10 +02:00
|
|
|
, m_last_incoming_request(min_time())
|
2008-12-11 00:07:44 +01:00
|
|
|
, m_last_unchoke(time_now())
|
2010-02-09 04:04:41 +01:00
|
|
|
, m_last_unchoked(time_now())
|
2013-03-26 05:45:38 +01:00
|
|
|
, m_last_choke(min_time())
|
2007-04-05 00:27:36 +02:00
|
|
|
, m_last_receive(time_now())
|
|
|
|
, m_last_sent(time_now())
|
2008-06-29 11:50:42 +02:00
|
|
|
, m_requested(min_time())
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_remote_dl_update(time_now())
|
2008-07-10 12:58:30 +02:00
|
|
|
, m_connect(time_now())
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_became_uninterested(time_now())
|
|
|
|
, m_became_uninteresting(time_now())
|
|
|
|
, m_free_upload(0)
|
2013-06-02 00:33:11 +02:00
|
|
|
, m_downloaded_at_last_round(0)
|
|
|
|
, m_uploaded_at_last_round(0)
|
2008-12-11 00:07:44 +01:00
|
|
|
, m_uploaded_at_last_unchoke(0)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_disk_recv_buffer(ses, 0)
|
2004-01-04 05:29:13 +01:00
|
|
|
, m_socket(s)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_torrent(tor)
|
2013-11-26 08:47:48 +01:00
|
|
|
, m_peer_info(peerinfo)
|
2010-03-19 19:39:51 +01:00
|
|
|
, m_last_seen_complete(0)
|
2013-11-26 08:47:48 +01:00
|
|
|
, m_receiving_block(piece_block::invalid)
|
|
|
|
, m_remote(endp)
|
2010-02-09 04:04:41 +01:00
|
|
|
, m_timeout_extend(0)
|
2009-03-17 10:34:44 +01:00
|
|
|
, m_outstanding_bytes(0)
|
2010-02-20 19:56:53 +01:00
|
|
|
, m_extension_outstanding_bytes(0)
|
2009-03-17 10:34:44 +01:00
|
|
|
, m_queued_time_critical(0)
|
2004-01-28 12:37:46 +01:00
|
|
|
, m_num_pieces(0)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_timeout(m_ses.settings().peer_timeout)
|
|
|
|
, m_packet_size(0)
|
2009-03-13 07:09:39 +01:00
|
|
|
, m_soft_packet_size(0)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_recv_pos(0)
|
|
|
|
, m_disk_recv_buffer_size(0)
|
|
|
|
, m_reading_bytes(0)
|
2004-01-12 21:31:27 +01:00
|
|
|
, m_num_invalid_requests(0)
|
2008-01-17 18:40:46 +01:00
|
|
|
, m_priority(1)
|
2009-04-26 02:21:59 +02:00
|
|
|
, m_upload_limit(0)
|
|
|
|
, m_download_limit(0)
|
2007-04-27 02:27:37 +02:00
|
|
|
, m_speed(slow)
|
2007-05-05 02:29:33 +02:00
|
|
|
, m_connection_ticket(-1)
|
2007-06-07 02:05:18 +02:00
|
|
|
, m_remote_bytes_dled(0)
|
|
|
|
, m_remote_dl_rate(0)
|
2007-08-01 07:22:34 +02:00
|
|
|
, m_outstanding_writing_bytes(0)
|
2008-04-03 08:11:21 +02:00
|
|
|
, m_download_rate_peak(0)
|
|
|
|
, m_upload_rate_peak(0)
|
2013-11-26 08:47:48 +01:00
|
|
|
, m_max_out_request_queue(m_ses.settings().max_out_request_queue)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_rtt(0)
|
|
|
|
, m_prefer_whole_pieces(0)
|
|
|
|
, m_desired_queue_size(2)
|
|
|
|
, m_fast_reconnect(false)
|
2012-02-07 04:46:21 +01:00
|
|
|
, m_outgoing(outgoing)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_ignore_bandwidth_limits(false)
|
2009-01-28 07:14:21 +01:00
|
|
|
, m_ignore_unchoke_slots(false)
|
2011-09-12 05:51:49 +02:00
|
|
|
, m_connecting(outgoing)
|
|
|
|
, m_queued(outgoing)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_request_large_blocks(false)
|
2010-09-05 18:01:36 +02:00
|
|
|
, m_share_mode(false)
|
2008-05-19 04:52:32 +02:00
|
|
|
, m_upload_only(false)
|
2008-06-29 11:50:42 +02:00
|
|
|
, m_snubbed(false)
|
2008-11-06 09:34:56 +01:00
|
|
|
, m_no_download(false)
|
2011-05-09 01:52:06 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_in_constructor(true)
|
2008-07-18 12:03:42 +02:00
|
|
|
, m_disconnect_started(false)
|
2008-08-29 19:21:56 +02:00
|
|
|
, m_initialized(false)
|
2012-02-09 07:24:58 +01:00
|
|
|
, m_in_use(1337)
|
2009-03-17 10:34:44 +01:00
|
|
|
, m_received_in_piece(0)
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
2012-04-23 07:48:46 +02:00
|
|
|
m_superseed_piece[0] = -1;
|
|
|
|
m_superseed_piece[1] = -1;
|
2012-01-21 10:05:34 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
// if t is NULL, we better not be connecting, since
|
|
|
|
// we can't decrement the connecting counter
|
|
|
|
TORRENT_ASSERT(t || !m_connecting);
|
2012-01-23 06:14:12 +01:00
|
|
|
if (m_connecting && t) t->inc_num_connecting();
|
2010-02-09 04:04:41 +01:00
|
|
|
m_est_reciprocation_rate = m_ses.m_settings.default_est_reciprocation_rate;
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
if (peerinfo && peerinfo->is_i2p_addr)
|
|
|
|
{
|
|
|
|
// quadruple the timeout for i2p peers
|
|
|
|
m_timeout *= 4;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-14 00:46:43 +01:00
|
|
|
m_channel_state[upload_channel] = peer_info::bw_idle;
|
|
|
|
m_channel_state[download_channel] = peer_info::bw_idle;
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
m_quota[0] = 0;
|
|
|
|
m_quota[1] = 0;
|
|
|
|
|
2007-12-21 00:53:03 +01:00
|
|
|
TORRENT_ASSERT(peerinfo == 0 || peerinfo->banned == false);
|
2007-05-02 21:47:38 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2007-01-29 08:39:33 +01:00
|
|
|
std::fill(m_country, m_country + 2, 0);
|
2008-04-11 10:46:43 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
if (m_ses.has_country_db())
|
|
|
|
{
|
|
|
|
char const *country = m_ses.country_for_ip(m_remote.address());
|
|
|
|
if (country != 0)
|
|
|
|
{
|
|
|
|
m_country[0] = country[0];
|
|
|
|
m_country[1] = country[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-05-02 21:47:38 +02:00
|
|
|
#endif
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2008-10-23 06:10:23 +02:00
|
|
|
error_code ec;
|
2012-05-14 07:43:45 +02:00
|
|
|
TORRENT_ASSERT(m_socket->remote_endpoint(ec) == m_remote || ec);
|
2012-10-07 01:45:36 +02:00
|
|
|
std::string log_name = m_remote.address().to_string(ec) + "_"
|
|
|
|
+ to_string(m_remote.port()).elems;
|
2012-10-06 02:33:10 +02:00
|
|
|
|
2012-10-07 01:45:36 +02:00
|
|
|
if (t) log_name = combine_path(to_hex(t->info_hash().to_string())
|
|
|
|
, log_name);
|
|
|
|
|
|
|
|
m_logger = m_ses.create_log(log_name, m_ses.listen_port());
|
2012-01-16 00:34:43 +01:00
|
|
|
peer_log("%s [ ep: %s type: %s seed: %d p: %p local: %s]"
|
2012-02-07 04:46:21 +01:00
|
|
|
, m_outgoing ? ">>> OUTGOING_CONNECTION" : "<<< INCOMING CONNECTION"
|
2010-12-19 20:40:32 +01:00
|
|
|
, print_endpoint(m_remote).c_str()
|
2012-01-16 00:34:43 +01:00
|
|
|
, m_socket->type_name()
|
|
|
|
, m_peer_info ? m_peer_info->seed : 0, m_peer_info
|
|
|
|
, print_endpoint(m_socket->local_endpoint(ec)).c_str());
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
m_inet_as_name = m_ses.as_name_for_ip(m_remote.address());
|
|
|
|
#endif
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-18 07:04:54 +01:00
|
|
|
piece_failed = false;
|
2007-12-14 19:02:06 +01:00
|
|
|
#endif
|
2004-01-15 17:45:34 +01:00
|
|
|
std::fill(m_peer_id.begin(), m_peer_id.end(), 0);
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2011-02-03 05:09:50 +01:00
|
|
|
#ifdef TORRENT_DISK_STATS
|
2009-10-28 20:56:18 +01:00
|
|
|
void peer_connection::log_buffer_usage(char* buffer, int size, char const* label)
|
|
|
|
{
|
|
|
|
if (m_ses.m_disk_thread.is_disk_buffer(buffer))
|
|
|
|
m_ses.m_disk_thread.rename_buffer(buffer, label);
|
|
|
|
|
|
|
|
m_ses.m_buffer_usage_logger << log_time() << " append_send_buffer: " << size << std::endl;
|
|
|
|
m_ses.log_buffer_usage();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
void peer_connection::increase_est_reciprocation_rate()
|
|
|
|
{
|
|
|
|
m_est_reciprocation_rate += m_est_reciprocation_rate
|
|
|
|
* m_ses.m_settings.increase_est_reciprocation_rate / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::decrease_est_reciprocation_rate()
|
|
|
|
{
|
|
|
|
m_est_reciprocation_rate -= m_est_reciprocation_rate
|
|
|
|
* m_ses.m_settings.decrease_est_reciprocation_rate / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection::bittyrant_unchoke_compare(
|
|
|
|
boost::intrusive_ptr<peer_connection const> const& p) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(p);
|
|
|
|
peer_connection const& rhs = *p;
|
|
|
|
|
|
|
|
size_type d1, d2, u1, u2;
|
|
|
|
|
|
|
|
// first compare how many bytes they've sent us
|
2013-06-02 00:33:11 +02:00
|
|
|
d1 = downloaded_in_last_round();
|
|
|
|
d2 = rhs.downloaded_in_last_round();
|
2010-02-09 04:04:41 +01:00
|
|
|
// divided by the number of bytes we've sent them
|
2013-06-02 00:33:11 +02:00
|
|
|
u1 = uploaded_in_last_round();
|
|
|
|
u2 = rhs.uploaded_in_last_round();
|
2010-02-09 04:04:41 +01:00
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t1 = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t1);
|
|
|
|
boost::shared_ptr<torrent> t2 = rhs.associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t2);
|
|
|
|
|
|
|
|
// take torrent priority into account
|
|
|
|
d1 *= 1 + t1->priority();
|
|
|
|
d2 *= 1 + t2->priority();
|
|
|
|
|
|
|
|
d1 = d1 * 1000 / (std::max)(size_type(1), u1);
|
|
|
|
d2 = d2 * 1000 / (std::max)(size_type(1), u2);
|
|
|
|
if (d1 > d2) return true;
|
|
|
|
if (d1 < d2) return false;
|
|
|
|
|
|
|
|
// if both peers are still in their send quota or not in their send quota
|
|
|
|
// prioritize the one that has waited the longest to be unchoked
|
|
|
|
return m_last_unchoke < rhs.m_last_unchoke;
|
|
|
|
}
|
|
|
|
|
2013-05-29 04:11:15 +02:00
|
|
|
// return true if 'this' peer should be preferred to be unchoke over p
|
2008-03-10 04:30:01 +01:00
|
|
|
bool peer_connection::unchoke_compare(boost::intrusive_ptr<peer_connection const> const& p) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(p);
|
|
|
|
peer_connection const& rhs = *p;
|
|
|
|
|
2011-04-30 22:33:35 +02:00
|
|
|
// if one peer belongs to a higher priority torrent than the other one
|
|
|
|
// that one should be unchoked.
|
2010-02-09 04:04:41 +01:00
|
|
|
boost::shared_ptr<torrent> t1 = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t1);
|
|
|
|
boost::shared_ptr<torrent> t2 = rhs.associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t2);
|
|
|
|
|
2011-04-30 22:33:35 +02:00
|
|
|
if (t1->priority() != t2->priority())
|
|
|
|
return t1->priority() > t2->priority();
|
|
|
|
|
|
|
|
// compare how many bytes they've sent us
|
|
|
|
size_type c1;
|
|
|
|
size_type c2;
|
2013-06-02 00:33:11 +02:00
|
|
|
c1 = downloaded_in_last_round();
|
|
|
|
c2 = rhs.downloaded_in_last_round();
|
2010-02-09 04:04:41 +01:00
|
|
|
|
2011-04-30 22:33:35 +02:00
|
|
|
if (c1 != c2) return c1 > c2;
|
2008-03-10 04:30:01 +01:00
|
|
|
|
2010-10-09 23:11:03 +02:00
|
|
|
if (m_ses.settings().seed_choking_algorithm == session_settings::round_robin)
|
|
|
|
{
|
2013-06-02 00:33:11 +02:00
|
|
|
// the amount uploaded since unchoked (not just in the last round)
|
|
|
|
c1 = uploaded_since_unchoked();
|
|
|
|
c2 = rhs.uploaded_since_unchoked();
|
2010-10-09 23:11:03 +02:00
|
|
|
|
2013-06-02 00:33:11 +02:00
|
|
|
// the way the round-robin unchoker works is that it,
|
|
|
|
// by default, prioritizes any peer that is already unchoked.
|
|
|
|
// this maintain the status quo across unchoke rounds. However,
|
|
|
|
// peers that are unchoked, but have sent more than one quota
|
|
|
|
// since they were unchoked, they get de-prioritized.
|
2010-10-09 23:11:03 +02:00
|
|
|
|
2013-06-02 00:33:11 +02:00
|
|
|
int pieces = m_ses.settings().seeding_piece_quota;
|
|
|
|
// if a peer is already unchoked, and the number of bytes sent since it was unchoked
|
|
|
|
// is greater than the send quanta, then it's done with it' upload slot, and we
|
|
|
|
// can de-prioritize it
|
|
|
|
bool c1_quota_complete = !is_choked() && c1 > (std::max)(t1->torrent_file().piece_length() * pieces, 256 * 1024);
|
|
|
|
bool c2_quota_complete = !rhs.is_choked() && c2 > (std::max)(t2->torrent_file().piece_length() * pieces, 256 * 1024);
|
|
|
|
|
|
|
|
// if c2 has completed a quanta, it shuold be de-prioritized
|
|
|
|
// and vice versa
|
|
|
|
if (c1_quota_complete < c2_quota_complete) return true;
|
|
|
|
if (c1_quota_complete > c2_quota_complete) return false;
|
|
|
|
|
|
|
|
// if both peers have either completed a quanta, or not.
|
|
|
|
// keep unchoked peers prioritized over choked ones, to let
|
|
|
|
// peers keep working on uploading a full quanta
|
|
|
|
if (is_choked() < rhs.is_choked()) return true;
|
|
|
|
if (is_choked() > rhs.is_choked()) return false;
|
|
|
|
|
|
|
|
// if the peers are still identical (say, they're both waiting to be unchoked)
|
|
|
|
// fall through and rely on the logic to prioritize peers who have waited
|
|
|
|
// the longest to be unchoked
|
2010-10-09 23:11:03 +02:00
|
|
|
}
|
|
|
|
else if (m_ses.settings().seed_choking_algorithm == session_settings::fastest_upload)
|
|
|
|
{
|
2013-06-02 00:33:11 +02:00
|
|
|
c1 = uploaded_in_last_round();
|
|
|
|
c2 = rhs.uploaded_in_last_round();
|
2008-10-14 02:57:58 +02:00
|
|
|
|
2010-10-09 23:11:03 +02:00
|
|
|
// take torrent priority into account
|
|
|
|
c1 *= 1 + t1->priority();
|
|
|
|
c2 *= 1 + t2->priority();
|
|
|
|
|
|
|
|
if (c1 > c2) return true;
|
|
|
|
if (c2 > c1) return false;
|
|
|
|
}
|
2011-03-02 08:26:09 +01:00
|
|
|
else if (m_ses.settings().seed_choking_algorithm == session_settings::anti_leech)
|
|
|
|
{
|
2013-07-26 18:59:10 +02:00
|
|
|
// the anti-leech seeding algorithm is based on the paper "Improving
|
|
|
|
// BitTorrent: A Simple Approach" from Chow et. al. and ranks peers based
|
|
|
|
// on how many pieces they have, prefering to unchoke peers that just
|
|
|
|
// started and peers that are close to completing. Like this:
|
2011-03-02 08:26:09 +01:00
|
|
|
// ^
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// s | \ / |
|
|
|
|
// c | \ / |
|
|
|
|
// o | \ / |
|
|
|
|
// r | \ / |
|
|
|
|
// e | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// | V |
|
|
|
|
// +---------------------------+
|
|
|
|
// 0% num have pieces 100%
|
|
|
|
int t1_total = t1->torrent_file().num_pieces();
|
|
|
|
int t2_total = t2->torrent_file().num_pieces();
|
|
|
|
int score1 = (num_have_pieces() < t1_total / 2
|
|
|
|
? t1_total - num_have_pieces() : num_have_pieces()) * 1000 / t1_total;
|
|
|
|
int score2 = (rhs.num_have_pieces() < t2_total / 2
|
|
|
|
? t2_total - rhs.num_have_pieces() : rhs.num_have_pieces()) * 1000 / t2_total;
|
|
|
|
if (score1 > score2) return true;
|
|
|
|
if (score2 > score1) return false;
|
|
|
|
}
|
|
|
|
|
2008-12-11 00:07:44 +01:00
|
|
|
// prioritize the one that has waited the longest to be unchoked
|
2013-06-02 00:33:11 +02:00
|
|
|
// the round-robin unchoker relies on this logic. Don't change it
|
|
|
|
// without moving this into that unchoker logic
|
2008-12-11 00:07:44 +01:00
|
|
|
return m_last_unchoke < rhs.m_last_unchoke;
|
2008-03-10 04:30:01 +01:00
|
|
|
}
|
|
|
|
|
2009-04-04 09:55:34 +02:00
|
|
|
bool peer_connection::upload_rate_compare(peer_connection const* p) const
|
|
|
|
{
|
|
|
|
size_type c1;
|
|
|
|
size_type c2;
|
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
boost::shared_ptr<torrent> t1 = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t1);
|
|
|
|
boost::shared_ptr<torrent> t2 = p->associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t2);
|
|
|
|
|
2013-06-02 00:33:11 +02:00
|
|
|
c1 = uploaded_in_last_round();
|
|
|
|
c2 = p->uploaded_in_last_round();
|
2009-04-04 09:55:34 +02:00
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
// take torrent priority into account
|
|
|
|
c1 *= 1 + t1->priority();
|
|
|
|
c2 *= 1 + t2->priority();
|
|
|
|
|
2009-04-04 09:55:34 +02:00
|
|
|
return c1 > c2;
|
|
|
|
}
|
|
|
|
|
2008-03-10 04:30:01 +01:00
|
|
|
void peer_connection::reset_choke_counters()
|
|
|
|
{
|
2013-06-02 00:33:11 +02:00
|
|
|
m_downloaded_at_last_round= m_statistics.total_payload_download();
|
|
|
|
m_uploaded_at_last_round = m_statistics.total_payload_upload();
|
2008-03-10 04:30:01 +01:00
|
|
|
}
|
|
|
|
|
2008-03-31 06:46:24 +02:00
|
|
|
void peer_connection::start()
|
|
|
|
{
|
2008-05-15 04:29:26 +02:00
|
|
|
TORRENT_ASSERT(m_peer_info == 0 || m_peer_info->connection == this);
|
2008-03-31 06:46:24 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
|
2012-02-07 04:46:21 +01:00
|
|
|
if (!m_outgoing)
|
2008-04-09 06:09:40 +02:00
|
|
|
{
|
|
|
|
tcp::socket::non_blocking_io ioc(true);
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-04-09 06:09:40 +02:00
|
|
|
m_socket->io_control(ioc, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
disconnect(ec);
|
2008-04-09 06:09:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_remote = m_socket->remote_endpoint(ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
disconnect(ec);
|
2008-04-09 06:09:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_remote.address().is_v4())
|
2011-11-10 10:18:55 +01:00
|
|
|
{
|
2008-04-09 06:09:40 +02:00
|
|
|
m_socket->set_option(type_of_service(m_ses.settings().peer_tos), ec);
|
2011-11-10 10:18:55 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log(">>> SET_TOS[ tos: %d e: %s ]", m_ses.settings().peer_tos, ec.message().c_str());
|
|
|
|
#endif
|
|
|
|
}
|
2008-04-09 06:09:40 +02:00
|
|
|
}
|
2011-09-12 05:51:49 +02:00
|
|
|
|
|
|
|
if (t && t->ready_for_connections())
|
2008-04-09 06:09:40 +02:00
|
|
|
{
|
2008-03-31 06:46:24 +02:00
|
|
|
init();
|
2008-04-09 06:09:40 +02:00
|
|
|
}
|
2008-03-31 06:46:24 +02:00
|
|
|
}
|
|
|
|
|
2007-03-21 03:09:50 +01:00
|
|
|
void peer_connection::update_interest()
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2010-09-05 18:01:36 +02:00
|
|
|
if (!t) return;
|
2007-03-21 03:09:50 +01:00
|
|
|
|
2008-09-18 22:17:49 +02:00
|
|
|
// if m_have_piece is 0, it means the connections
|
|
|
|
// have not been initialized yet. The interested
|
|
|
|
// flag will be updated once they are.
|
|
|
|
if (m_have_piece.size() == 0) return;
|
2008-09-19 09:14:24 +02:00
|
|
|
if (!t->ready_for_connections()) return;
|
2008-09-18 22:17:49 +02:00
|
|
|
|
2007-03-21 03:09:50 +01:00
|
|
|
bool interested = false;
|
2011-04-10 01:57:56 +02:00
|
|
|
if (!t->is_upload_only())
|
2007-03-21 03:09:50 +01:00
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
piece_picker const& p = t->picker();
|
|
|
|
int num_pieces = p.num_pieces();
|
|
|
|
for (int j = 0; j != num_pieces; ++j)
|
2007-03-21 03:09:50 +01:00
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
if (!p.have_piece(j)
|
|
|
|
&& t->piece_priority(j) > 0
|
|
|
|
&& m_have_piece[j])
|
|
|
|
{
|
|
|
|
interested = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-21 03:09:50 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-21 19:10:11 +02:00
|
|
|
if (!interested) send_not_interested();
|
|
|
|
else t->get_policy().peer_is_interesting(*this);
|
2007-03-21 03:09:50 +01:00
|
|
|
|
2008-09-19 04:37:05 +02:00
|
|
|
TORRENT_ASSERT(in_handshake() || is_interesting() == interested);
|
2007-03-21 03:09:50 +01:00
|
|
|
}
|
|
|
|
|
2010-10-31 23:12:26 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-01 05:22:03 +01:00
|
|
|
void peer_connection::peer_log(char const* fmt, ...) const
|
2010-10-31 23:12:26 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
void peer_connection::add_extension(boost::shared_ptr<peer_plugin> ext)
|
|
|
|
{
|
|
|
|
m_extensions.push_back(ext);
|
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
peer_plugin const* peer_connection::find_plugin(char const* type)
|
|
|
|
{
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (strcmp((*i)->type(), type) == 0) return (*i).get();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
void peer_connection::send_allowed_set()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2008-12-08 07:36:22 +01:00
|
|
|
if (t->super_seeding())
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** SKIPPING ALLOWED SET BECAUSE OF SUPER SEEDING");
|
2008-12-08 07:36:22 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-21 10:07:09 +01:00
|
|
|
if (upload_only())
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** SKIPPING ALLOWED SET BECAUSE PEER IS UPLOAD ONLY");
|
2010-02-21 10:07:09 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
int num_allowed_pieces = m_ses.settings().allowed_fast_set_size;
|
2009-06-28 02:47:49 +02:00
|
|
|
if (num_allowed_pieces == 0) return;
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
int num_pieces = t->torrent_file().num_pieces();
|
|
|
|
|
|
|
|
if (num_allowed_pieces >= num_pieces)
|
|
|
|
{
|
2009-05-04 02:08:00 +02:00
|
|
|
// this is a special case where we have more allowed
|
|
|
|
// fast pieces than pieces in the torrent. Just send
|
|
|
|
// an allowed fast message for every single piece
|
2007-08-14 19:47:48 +02:00
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
{
|
2010-02-21 10:07:09 +01:00
|
|
|
// there's no point in offering fast pieces
|
|
|
|
// that the peer already has
|
|
|
|
if (has_piece(i)) continue;
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> ALLOWED_FAST [ %d ]", i);
|
2007-08-14 19:47:48 +02:00
|
|
|
#endif
|
|
|
|
write_allow_fast(i);
|
2009-05-04 02:08:00 +02:00
|
|
|
TORRENT_ASSERT(std::find(m_accept_fast.begin()
|
|
|
|
, m_accept_fast.end(), i)
|
|
|
|
== m_accept_fast.end());
|
2013-07-26 18:59:10 +02:00
|
|
|
if (m_accept_fast.empty())
|
|
|
|
{
|
|
|
|
m_accept_fast.reserve(10);
|
|
|
|
m_accept_fast_piece_cnt.reserve(10);
|
|
|
|
}
|
2009-05-04 02:08:00 +02:00
|
|
|
m_accept_fast.push_back(i);
|
2013-07-26 18:59:10 +02:00
|
|
|
m_accept_fast_piece_cnt.push_back(0);
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string x;
|
|
|
|
address const& addr = m_remote.address();
|
|
|
|
if (addr.is_v4())
|
|
|
|
{
|
|
|
|
address_v4::bytes_type bytes = addr.to_v4().to_bytes();
|
|
|
|
x.assign((char*)&bytes[0], bytes.size());
|
|
|
|
}
|
2009-11-27 19:46:29 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
2007-08-14 19:47:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
address_v6::bytes_type bytes = addr.to_v6().to_bytes();
|
|
|
|
x.assign((char*)&bytes[0], bytes.size());
|
|
|
|
}
|
2009-11-27 19:46:29 +01:00
|
|
|
#endif
|
2007-08-14 19:47:48 +02:00
|
|
|
x.append((char*)&t->torrent_file().info_hash()[0], 20);
|
|
|
|
|
2010-06-28 03:12:54 +02:00
|
|
|
sha1_hash hash = hasher(x.c_str(), x.size()).final();
|
2007-08-14 19:47:48 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char* p = (char*)&hash[0];
|
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
{
|
|
|
|
int piece = detail::read_uint32(p) % num_pieces;
|
2009-05-04 02:08:00 +02:00
|
|
|
if (std::find(m_accept_fast.begin(), m_accept_fast.end(), piece)
|
|
|
|
== m_accept_fast.end())
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> ALLOWED_FAST [ %d ]", piece);
|
2007-08-14 19:47:48 +02:00
|
|
|
#endif
|
|
|
|
write_allow_fast(piece);
|
2013-07-26 18:59:10 +02:00
|
|
|
if (m_accept_fast.empty())
|
|
|
|
{
|
|
|
|
m_accept_fast.reserve(10);
|
|
|
|
m_accept_fast_piece_cnt.reserve(10);
|
|
|
|
}
|
2009-05-04 02:08:00 +02:00
|
|
|
m_accept_fast.push_back(piece);
|
2013-07-26 18:59:10 +02:00
|
|
|
m_accept_fast_piece_cnt.push_back(0);
|
2007-08-14 19:47:48 +02:00
|
|
|
if (int(m_accept_fast.size()) >= num_allowed_pieces
|
|
|
|
|| int(m_accept_fast.size()) == num_pieces) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hash = hasher((char*)&hash[0], 20).final();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
void peer_connection::on_metadata_impl()
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
m_have_piece.resize(t->torrent_file().num_pieces(), m_have_all);
|
|
|
|
m_num_pieces = m_have_piece.count();
|
2009-09-01 06:41:50 +02:00
|
|
|
|
|
|
|
// now that we know how many pieces there are
|
|
|
|
// remove any invalid allowed_fast and suggest pieces
|
|
|
|
// now that we know what the number of pieces are
|
|
|
|
for (std::vector<int>::iterator i = m_allowed_fast.begin();
|
|
|
|
i != m_allowed_fast.end();)
|
|
|
|
{
|
|
|
|
if (*i < m_num_pieces)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i = m_allowed_fast.erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<int>::iterator i = m_suggested_pieces.begin();
|
|
|
|
i != m_suggested_pieces.end();)
|
|
|
|
{
|
|
|
|
if (*i < m_num_pieces)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i = m_suggested_pieces.erase(i);
|
|
|
|
}
|
2008-08-29 19:21:56 +02:00
|
|
|
on_metadata();
|
|
|
|
if (m_disconnecting) return;
|
|
|
|
}
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
void peer_connection::init()
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
|
|
|
TORRENT_ASSERT(t->ready_for_connections());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
m_have_piece.resize(t->torrent_file().num_pieces(), m_have_all);
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
if (m_have_all) m_num_pieces = t->torrent_file().num_pieces();
|
2011-05-09 01:52:06 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2012-04-30 22:15:11 +02:00
|
|
|
TORRENT_ASSERT(!m_initialized);
|
2008-08-29 19:21:56 +02:00
|
|
|
m_initialized = true;
|
|
|
|
#endif
|
2004-06-14 01:30:42 +02:00
|
|
|
// now that we have a piece_picker,
|
2008-01-31 18:52:29 +01:00
|
|
|
// update it with this peer's pieces
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
TORRENT_ASSERT(m_num_pieces == m_have_piece.count());
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
if (m_num_pieces == int(m_have_piece.size()))
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-24 01:37:01 +01:00
|
|
|
peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info);
|
2004-06-14 01:30:42 +02:00
|
|
|
#endif
|
2007-04-15 04:14:02 +02:00
|
|
|
// if this is a web seed. we don't have a peer_info struct
|
2009-06-13 06:14:41 +02:00
|
|
|
t->get_policy().set_seed(m_peer_info, true);
|
2008-09-20 01:02:45 +02:00
|
|
|
m_upload_only = true;
|
2008-07-18 12:03:42 +02:00
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_has_all(this);
|
2011-04-10 01:57:56 +02:00
|
|
|
if (t->is_upload_only()) send_not_interested();
|
2008-07-18 12:03:42 +02:00
|
|
|
else t->get_policy().peer_is_interesting(*this);
|
2007-04-15 04:14:02 +02:00
|
|
|
return;
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
|
2007-04-15 04:14:02 +02:00
|
|
|
// if we're a seed, we don't keep track of piece availability
|
|
|
|
if (!t->is_seed())
|
|
|
|
{
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_has(m_have_piece, this);
|
2007-04-15 04:14:02 +02:00
|
|
|
bool interesting = false;
|
|
|
|
for (int i = 0; i < int(m_have_piece.size()); ++i)
|
|
|
|
{
|
|
|
|
if (m_have_piece[i])
|
|
|
|
{
|
|
|
|
// if the peer has a piece and we don't, the peer is interesting
|
|
|
|
if (!t->have_piece(i)
|
|
|
|
&& t->picker().piece_priority(i) != 0)
|
|
|
|
interesting = true;
|
|
|
|
}
|
|
|
|
}
|
2008-07-18 12:03:42 +02:00
|
|
|
if (interesting) t->get_policy().peer_is_interesting(*this);
|
|
|
|
else send_not_interested();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
update_interest();
|
2007-04-15 04:14:02 +02:00
|
|
|
}
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
|
2004-01-04 05:29:13 +01:00
|
|
|
peer_connection::~peer_connection()
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2006-06-12 01:24:36 +02:00
|
|
|
// INVARIANT_CHECK;
|
2008-01-19 20:00:54 +01:00
|
|
|
TORRENT_ASSERT(!m_in_constructor);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_disconnecting);
|
2008-07-18 12:03:42 +02:00
|
|
|
TORRENT_ASSERT(m_disconnect_started);
|
2012-03-30 06:29:54 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2012-02-09 07:24:58 +01:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
|
|
|
m_in_use = 0;
|
|
|
|
#endif
|
|
|
|
|
2012-01-21 10:05:34 +01:00
|
|
|
// defensive
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
// if t is NULL, we better not be connecting, since
|
|
|
|
// we can't decrement the connecting counter
|
|
|
|
TORRENT_ASSERT(t || !m_connecting);
|
|
|
|
|
|
|
|
// we should really have dealt with this already
|
|
|
|
TORRENT_ASSERT(!m_connecting);
|
|
|
|
if (m_connecting && t)
|
|
|
|
{
|
2012-01-23 06:14:12 +01:00
|
|
|
t->dec_num_connecting();
|
2012-01-21 10:05:34 +01:00
|
|
|
m_connecting = false;
|
|
|
|
}
|
|
|
|
|
2008-05-05 08:25:22 +02:00
|
|
|
m_disk_recv_buffer_size = 0;
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2009-09-14 04:08:34 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
m_extensions.clear();
|
|
|
|
#endif
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** CONNECTION CLOSED");
|
2005-01-10 12:14:22 +01:00
|
|
|
#endif
|
2011-03-01 22:57:03 +01:00
|
|
|
// TORRENT_ASSERT(!m_ses.has_peer(this));
|
2010-10-02 23:01:11 +02:00
|
|
|
TORRENT_ASSERT(m_request_queue.empty());
|
|
|
|
TORRENT_ASSERT(m_download_queue.empty());
|
2012-03-30 06:29:54 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-01-07 02:10:46 +01:00
|
|
|
for (aux::session_impl::torrent_map::const_iterator i = m_ses.m_torrents.begin()
|
|
|
|
, end(m_ses.m_torrents.end()); i != end; ++i)
|
|
|
|
TORRENT_ASSERT(!i->second->has_peer(this));
|
2007-04-12 12:21:55 +02:00
|
|
|
if (m_peer_info)
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_peer_info->connection == 0);
|
2005-10-01 17:12:10 +02:00
|
|
|
#endif
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int peer_connection::picker_options() const
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
if (!t) return 0;
|
|
|
|
|
|
|
|
if (t->is_sequential_download())
|
|
|
|
{
|
2013-10-20 04:40:43 +02:00
|
|
|
ret |= piece_picker::sequential;
|
2008-09-06 23:04:57 +02:00
|
|
|
}
|
|
|
|
else if (t->num_have() < t->settings().initial_picker_threshold)
|
|
|
|
{
|
|
|
|
// if we have fewer pieces than a certain threshols
|
|
|
|
// don't pick rare pieces, just pick random ones,
|
|
|
|
// and prioritize finishing them
|
|
|
|
ret |= piece_picker::prioritize_partials;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-14 10:07:27 +01:00
|
|
|
ret |= piece_picker::rarest_first | piece_picker::speed_affinity;
|
2008-09-06 23:04:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_snubbed)
|
|
|
|
{
|
|
|
|
// snubbed peers should request
|
|
|
|
// the common pieces first, just to make
|
|
|
|
// it more likely for all snubbed peers to
|
|
|
|
// request blocks from the same piece
|
|
|
|
ret |= piece_picker::reverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t->settings().prioritize_partial_pieces)
|
|
|
|
ret |= piece_picker::prioritize_partials;
|
|
|
|
|
|
|
|
if (on_parole()) ret |= piece_picker::on_parole
|
|
|
|
| piece_picker::prioritize_partials;
|
|
|
|
|
|
|
|
// only one of rarest_first, common_first and sequential can be set.
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT((ret & piece_picker::rarest_first) ? 1 : 0
|
|
|
|
+ (ret & piece_picker::sequential) ? 1 : 0 <= 1);
|
2008-09-06 23:04:57 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-15 07:03:29 +02:00
|
|
|
void peer_connection::fast_reconnect(bool r)
|
|
|
|
{
|
2008-04-01 19:38:19 +02:00
|
|
|
if (!peer_info_struct() || peer_info_struct()->fast_reconnects > 1)
|
|
|
|
return;
|
2007-10-15 07:03:29 +02:00
|
|
|
m_fast_reconnect = r;
|
2009-11-18 19:43:54 +01:00
|
|
|
peer_info_struct()->last_connected = m_ses.session_time();
|
|
|
|
int rewind = m_ses.settings().min_reconnect_time * m_ses.settings().max_failcount;
|
|
|
|
if (peer_info_struct()->last_connected < rewind) peer_info_struct()->last_connected = 0;
|
|
|
|
else peer_info_struct()->last_connected -= rewind;
|
|
|
|
|
|
|
|
if (peer_info_struct()->fast_reconnects < 15)
|
|
|
|
++peer_info_struct()->fast_reconnects;
|
2007-10-15 07:03:29 +02:00
|
|
|
}
|
|
|
|
|
2004-03-11 15:56:48 +01:00
|
|
|
void peer_connection::announce_piece(int index)
|
|
|
|
{
|
2007-06-06 02:41:20 +02:00
|
|
|
// dont announce during handshake
|
|
|
|
if (in_handshake()) return;
|
2007-09-01 09:38:10 +02:00
|
|
|
|
2010-01-15 17:45:42 +01:00
|
|
|
// remove suggested pieces once we have them
|
2007-09-01 09:38:10 +02:00
|
|
|
std::vector<int>::iterator i = std::find(
|
|
|
|
m_suggested_pieces.begin(), m_suggested_pieces.end(), index);
|
|
|
|
if (i != m_suggested_pieces.end()) m_suggested_pieces.erase(i);
|
|
|
|
|
2009-09-01 06:41:50 +02:00
|
|
|
// remove allowed fast pieces
|
|
|
|
i = std::find(m_allowed_fast.begin(), m_allowed_fast.end(), index);
|
|
|
|
if (i != m_allowed_fast.end()) m_allowed_fast.erase(i);
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
if (has_piece(index))
|
2008-04-20 19:17:58 +02:00
|
|
|
{
|
2008-07-18 12:03:42 +02:00
|
|
|
// if we got a piece that this peer has
|
|
|
|
// it might have been the last interesting
|
|
|
|
// piece this peer had. We might not be
|
|
|
|
// interested anymore
|
|
|
|
update_interest();
|
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
|
|
|
// optimization, don't send have messages
|
|
|
|
// to peers that already have the piece
|
|
|
|
if (!m_ses.settings().send_redundant_have)
|
|
|
|
{
|
2008-04-20 19:17:58 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> HAVE [ piece: %d ] SUPRESSED", index);
|
2008-04-20 19:17:58 +02:00
|
|
|
#endif
|
2008-07-18 12:03:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-04-20 19:17:58 +02:00
|
|
|
}
|
2006-11-19 16:29:58 +01:00
|
|
|
|
2013-11-02 04:26:53 +01:00
|
|
|
disconnect_if_redundant();
|
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2006-11-19 16:29:58 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2011-08-13 20:46:52 +02:00
|
|
|
peer_log("==> HAVE [ piece: %d ]", index);
|
2006-11-19 16:29:58 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
write_have(index);
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2006-11-19 16:29:58 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-11-19 16:29:58 +01:00
|
|
|
#endif
|
2004-03-11 15:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection::has_piece(int i) const
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
|
|
|
TORRENT_ASSERT(i >= 0);
|
|
|
|
TORRENT_ASSERT(i < t->torrent_file().num_pieces());
|
2004-03-11 15:56:48 +01:00
|
|
|
return m_have_piece[i];
|
|
|
|
}
|
|
|
|
|
2009-12-25 17:52:57 +01:00
|
|
|
std::vector<pending_block> const& peer_connection::request_queue() const
|
2005-09-27 10:07:24 +02:00
|
|
|
{
|
|
|
|
return m_request_queue;
|
|
|
|
}
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
std::vector<pending_block> const& peer_connection::download_queue() const
|
2004-03-11 15:56:48 +01:00
|
|
|
{
|
|
|
|
return m_download_queue;
|
|
|
|
}
|
|
|
|
|
2009-05-03 22:21:24 +02:00
|
|
|
std::vector<peer_request> const& peer_connection::upload_queue() const
|
2004-03-11 15:56:48 +01:00
|
|
|
{
|
|
|
|
return m_requests;
|
|
|
|
}
|
|
|
|
|
2009-02-17 03:33:45 +01:00
|
|
|
time_duration peer_connection::download_queue_time(int extra_bytes) const
|
|
|
|
{
|
|
|
|
int rate = m_statistics.transfer_rate(stat::download_payload)
|
|
|
|
+ m_statistics.transfer_rate(stat::download_protocol);
|
|
|
|
// avoid division by zero
|
|
|
|
if (rate < 50) rate = 50;
|
2009-03-17 10:34:44 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
return seconds((m_outstanding_bytes + m_queued_time_critical * t->block_size()) / rate);
|
2009-02-17 03:33:45 +01:00
|
|
|
}
|
|
|
|
|
2004-03-11 15:56:48 +01:00
|
|
|
void peer_connection::add_stat(size_type downloaded, size_type uploaded)
|
|
|
|
{
|
|
|
|
m_statistics.add_stat(downloaded, uploaded);
|
|
|
|
}
|
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
bitfield const& peer_connection::get_bitfield() const
|
2004-03-11 15:56:48 +01:00
|
|
|
{
|
|
|
|
return m_have_piece;
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void peer_connection::received_valid_data(int index)
|
2004-03-11 15:56:48 +01:00
|
|
|
{
|
2012-02-07 10:18:07 +01:00
|
|
|
// this fails because we haven't had time to disconnect
|
|
|
|
// seeds yet, and we might have just become one
|
|
|
|
// INVARIANT_CHECK;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
(*i)->on_piece_pass(index);
|
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-03-11 15:56:48 +01:00
|
|
|
}
|
|
|
|
|
2012-09-29 19:46:41 +02:00
|
|
|
bool peer_connection::received_invalid_data(int index, bool single_peer)
|
2004-03-11 15:56:48 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
(*i)->on_piece_failed(index);
|
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
2012-09-29 19:46:41 +02:00
|
|
|
return true;
|
2004-03-11 15:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type peer_connection::total_free_upload() const
|
|
|
|
{
|
|
|
|
return m_free_upload;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::add_free_upload(size_type free_upload)
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2004-03-11 15:56:48 +01:00
|
|
|
m_free_upload += free_upload;
|
|
|
|
}
|
|
|
|
|
2004-01-08 14:03:38 +01:00
|
|
|
// verifies a piece to see if it is valid (is within a valid range)
|
|
|
|
// and if it can correspond to a request generated by libtorrent.
|
|
|
|
bool peer_connection::verify_piece(const peer_request& p) const
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
2007-09-03 23:16:24 +02:00
|
|
|
torrent_info const& ti = t->torrent_file();
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2004-01-08 14:03:38 +01:00
|
|
|
return p.piece >= 0
|
2010-02-23 17:26:24 +01:00
|
|
|
&& p.piece < ti.num_pieces()
|
2004-01-08 14:03:38 +01:00
|
|
|
&& p.start >= 0
|
2010-02-23 17:26:24 +01:00
|
|
|
&& p.start < ti.piece_length()
|
|
|
|
&& t->to_req(piece_block(p.piece, p.start / t->block_size())) == p;
|
2004-01-08 14:03:38 +01:00
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2011-09-05 07:50:41 +02:00
|
|
|
void peer_connection::attach_to_torrent(sha1_hash const& ih, bool allow_encrypted)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_disconnecting);
|
|
|
|
TORRENT_ASSERT(m_torrent.expired());
|
2007-08-16 14:41:46 +02:00
|
|
|
boost::weak_ptr<torrent> wpt = m_ses.find_torrent(ih);
|
2007-08-17 00:13:35 +02:00
|
|
|
boost::shared_ptr<torrent> t = wpt.lock();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2012-10-07 01:45:36 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
// now that we know which torrent this peer belongs
|
|
|
|
// to. Move the log file into its directory
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
std::string log_name = combine_path(to_hex(ih.to_string())
|
|
|
|
, m_remote.address().to_string(ec) + "_"
|
|
|
|
+ to_string(m_remote.port()).elems);
|
|
|
|
m_logger->move_log_file(m_ses.get_log_path(), log_name, m_ses.listen_port());
|
|
|
|
#endif
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
if (t && t->is_aborted())
|
2007-08-17 00:13:35 +02:00
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** the torrent has been aborted");
|
2007-08-17 00:13:35 +02:00
|
|
|
#endif
|
2006-05-28 21:03:54 +02:00
|
|
|
t.reset();
|
2007-08-17 00:13:35 +02:00
|
|
|
}
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
// we couldn't find the torrent!
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** couldn't find a torrent with the given info_hash: %s torrents:", to_hex(ih.to_string()).c_str());
|
2007-08-17 00:13:35 +02:00
|
|
|
session_impl::torrent_map const& torrents = m_ses.m_torrents;
|
|
|
|
for (session_impl::torrent_map::const_iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
|
|
|
{
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log(" %s", to_hex(i->second->torrent_file().info_hash().to_string()).c_str());
|
2007-08-17 00:13:35 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2011-02-04 07:55:39 +01:00
|
|
|
disconnect(errors::invalid_info_hash, 1);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 09:47:21 +02:00
|
|
|
if (t->is_paused() && (!t->is_auto_managed()
|
2010-04-09 07:54:36 +02:00
|
|
|
|| !m_ses.m_settings.incoming_starts_queued_torrents))
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
// paused torrents will not accept
|
2010-02-11 05:39:04 +01:00
|
|
|
// incoming connections unless they are auto managed
|
|
|
|
// and inconing_starts_queued_torrents is true
|
|
|
|
// torrents that have errors should always reject
|
|
|
|
// incoming peers
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("rejected connection to paused torrent");
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::torrent_paused, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
#if TORRENT_USE_I2P
|
2009-08-20 05:19:12 +02:00
|
|
|
i2p_stream* i2ps = m_socket->get<i2p_stream>();
|
|
|
|
if (!i2ps && t->torrent_file().is_i2p() && !m_ses.m_settings.allow_i2p_mixed)
|
|
|
|
{
|
|
|
|
// the torrent is an i2p torrent, the peer is a regular peer
|
|
|
|
// and we don't allow mixed mode. Disconnect the peer.
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("rejected regular connection to i2p torrent");
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::peer_banned, 2);
|
2009-08-20 05:19:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-10-20 04:49:56 +02:00
|
|
|
#endif // TORRENT_USE_I2P
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_torrent.expired());
|
2010-02-11 05:39:04 +01:00
|
|
|
|
2010-05-09 02:55:22 +02:00
|
|
|
if (t->is_paused()
|
|
|
|
&& m_ses.m_settings.incoming_starts_queued_torrents
|
2010-05-09 08:00:11 +02:00
|
|
|
&& !m_ses.is_paused()
|
2010-05-09 02:55:22 +02:00
|
|
|
&& !t->is_aborted()
|
|
|
|
&& !m_ses.is_aborted())
|
2010-02-11 05:39:04 +01:00
|
|
|
{
|
|
|
|
t->resume();
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// check to make sure we don't have another connection with the same
|
|
|
|
// info_hash and peer_id. If we do. close this connection.
|
|
|
|
t->attach_peer(this);
|
2008-01-19 20:00:54 +01:00
|
|
|
if (m_disconnecting) return;
|
2007-08-16 14:41:46 +02:00
|
|
|
m_torrent = wpt;
|
|
|
|
|
2012-12-31 07:54:54 +01:00
|
|
|
if (m_exceeded_limit)
|
|
|
|
{
|
|
|
|
// find a peer in some torrent (presumably the one with most peers)
|
|
|
|
// and disconnect the lowest ranking peer
|
2013-01-02 08:39:02 +01:00
|
|
|
boost::weak_ptr<torrent> torr = m_ses.find_disconnect_candidate_torrent();
|
|
|
|
boost::shared_ptr<torrent> other_t = torr.lock();
|
2012-12-31 07:54:54 +01:00
|
|
|
|
2013-01-02 08:39:02 +01:00
|
|
|
if (other_t)
|
|
|
|
{
|
|
|
|
if (other_t->num_peers() <= t->num_peers())
|
|
|
|
{
|
|
|
|
disconnect(errors::too_many_connections);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// find the lowest ranking peer and disconnect that
|
|
|
|
peer_connection* p = other_t->find_lowest_ranking_peer();
|
|
|
|
p->disconnect(errors::too_many_connections);
|
|
|
|
peer_disconnected_other();
|
|
|
|
}
|
|
|
|
else
|
2012-12-31 07:54:54 +01:00
|
|
|
{
|
|
|
|
disconnect(errors::too_many_connections);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_torrent.expired());
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// if the torrent isn't ready to accept
|
|
|
|
// connections yet, we'll have to wait with
|
|
|
|
// our initialization
|
|
|
|
if (t->ready_for_connections()) init();
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_torrent.expired());
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// assume the other end has no pieces
|
|
|
|
// if we don't have valid metadata yet,
|
|
|
|
// leave the vector unallocated
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_num_pieces == 0);
|
2008-05-28 04:35:02 +02:00
|
|
|
m_have_piece.clear_all();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_torrent.expired());
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2012-12-31 07:54:54 +01:00
|
|
|
boost::uint32_t peer_connection::peer_rank() const
|
|
|
|
{
|
2013-10-23 08:45:23 +02:00
|
|
|
return m_peer_info == NULL ? 0
|
|
|
|
: m_peer_info->rank(m_ses.external_address(), m_ses.listen_port());
|
2012-12-31 07:54:54 +01:00
|
|
|
}
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// message handlers
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// -----------------------------
|
|
|
|
// --------- KEEPALIVE ---------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void peer_connection::incoming_keepalive()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== KEEPALIVE");
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// ----------- CHOKE -----------
|
|
|
|
// -----------------------------
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_choke()
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_choke()) return;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-04-02 22:00:24 +02:00
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== CHOKE");
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
|
|
|
m_peer_choked = true;
|
2011-06-01 11:14:21 +02:00
|
|
|
set_endgame(false);
|
2010-05-01 18:17:37 +02:00
|
|
|
|
2010-10-30 10:36:18 +02:00
|
|
|
clear_request_queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::clear_request_queue()
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
2010-05-01 18:17:37 +02:00
|
|
|
// clear the requests that haven't been sent yet
|
2007-10-10 04:27:55 +02:00
|
|
|
if (peer_info_struct() == 0 || !peer_info_struct()->on_parole)
|
2004-01-04 06:28:24 +01:00
|
|
|
{
|
2007-10-10 04:27:55 +02:00
|
|
|
// if the peer is not in parole mode, clear the queued
|
|
|
|
// up block requests
|
|
|
|
if (!t->is_seed())
|
2006-12-04 13:20:34 +01:00
|
|
|
{
|
2007-10-10 04:27:55 +02:00
|
|
|
piece_picker& p = t->picker();
|
2009-12-25 17:52:57 +01:00
|
|
|
for (std::vector<pending_block>::const_iterator i = m_request_queue.begin()
|
2007-10-10 04:27:55 +02:00
|
|
|
, end(m_request_queue.end()); i != end; ++i)
|
|
|
|
{
|
2010-10-04 00:06:53 +02:00
|
|
|
p.abort_download(i->block, peer_info_struct());
|
2007-10-10 04:27:55 +02:00
|
|
|
}
|
2006-12-04 13:20:34 +01:00
|
|
|
}
|
2007-10-10 04:27:55 +02:00
|
|
|
m_request_queue.clear();
|
2009-11-02 02:01:07 +01:00
|
|
|
m_queued_time_critical = 0;
|
2005-09-27 10:07:24 +02:00
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2004-01-04 06:28:24 +01:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
bool match_request(peer_request const& r, piece_block const& b, int block_size)
|
|
|
|
{
|
2011-02-21 06:24:41 +01:00
|
|
|
if (int(b.piece_index) != r.piece) return false;
|
|
|
|
if (int(b.block_index) != r.start / block_size) return false;
|
2007-08-14 19:47:48 +02:00
|
|
|
if (r.start % block_size != 0) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// -------- REJECT PIECE -------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void peer_connection::incoming_reject_request(peer_request const& r)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2012-05-12 23:22:06 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("<== REJECT_PIECE [ piece: %d | s: %d | l: %d ]"
|
|
|
|
, r.piece, r.start, r.length);
|
|
|
|
#endif
|
|
|
|
|
2007-09-14 05:38:38 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_reject(r)) return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
std::vector<pending_block>::iterator i = std::find_if(
|
2007-08-14 19:47:48 +02:00
|
|
|
m_download_queue.begin(), m_download_queue.end()
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(match_request, boost::cref(r), boost::bind(&pending_block::block, _1)
|
2008-07-07 14:04:06 +02:00
|
|
|
, t->block_size()));
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
if (i != m_download_queue.end())
|
|
|
|
{
|
2009-12-25 17:52:57 +01:00
|
|
|
pending_block b = *i;
|
2009-03-17 10:34:44 +01:00
|
|
|
bool remove_from_picker = !i->timed_out && !i->not_wanted;
|
2007-08-14 19:47:48 +02:00
|
|
|
m_download_queue.erase(i);
|
2009-06-28 09:39:06 +02:00
|
|
|
TORRENT_ASSERT(m_outstanding_bytes >= r.length);
|
2009-03-17 10:34:44 +01:00
|
|
|
m_outstanding_bytes -= r.length;
|
2009-06-28 23:50:18 +02:00
|
|
|
if (m_outstanding_bytes < 0) m_outstanding_bytes = 0;
|
2007-10-10 04:27:55 +02:00
|
|
|
|
|
|
|
// if the peer is in parole mode, keep the request
|
|
|
|
if (peer_info_struct() && peer_info_struct()->on_parole)
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
2009-07-02 08:43:05 +02:00
|
|
|
// we should only add it if the block is marked as
|
|
|
|
// busy in the piece-picker
|
|
|
|
if (remove_from_picker)
|
|
|
|
m_request_queue.insert(m_request_queue.begin(), b);
|
2007-10-10 04:27:55 +02:00
|
|
|
}
|
2009-03-17 10:34:44 +01:00
|
|
|
else if (!t->is_seed() && remove_from_picker)
|
2007-10-10 04:27:55 +02:00
|
|
|
{
|
|
|
|
piece_picker& p = t->picker();
|
2010-10-04 00:06:53 +02:00
|
|
|
p.abort_download(b.block, peer_info_struct());
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
2009-03-17 10:34:44 +01:00
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_DEBUG
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-08-14 19:47:48 +02:00
|
|
|
else
|
|
|
|
{
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** PIECE NOT IN REQUEST QUEUE");
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
2007-10-03 18:57:20 +02:00
|
|
|
if (has_peer_choked())
|
|
|
|
{
|
|
|
|
// if we're choked and we got a rejection of
|
|
|
|
// a piece in the allowed fast set, remove it
|
|
|
|
// from the allow fast set.
|
|
|
|
std::vector<int>::iterator i = std::find(
|
|
|
|
m_allowed_fast.begin(), m_allowed_fast.end(), r.piece);
|
|
|
|
if (i != m_allowed_fast.end()) m_allowed_fast.erase(i);
|
|
|
|
}
|
2007-10-04 11:32:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<int>::iterator i = std::find(m_suggested_pieces.begin()
|
|
|
|
, m_suggested_pieces.end(), r.piece);
|
|
|
|
if (i != m_suggested_pieces.end())
|
|
|
|
m_suggested_pieces.erase(i);
|
|
|
|
}
|
|
|
|
|
2008-07-08 23:33:36 +02:00
|
|
|
if (m_request_queue.empty() && m_download_queue.size() < 2)
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
2011-02-07 01:51:20 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_reject_piece_picks;
|
|
|
|
#endif
|
2008-07-08 23:33:36 +02:00
|
|
|
request_a_block(*t, *this);
|
2007-08-14 19:47:48 +02:00
|
|
|
send_block_requests();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-01 09:38:10 +02:00
|
|
|
// -----------------------------
|
2008-01-02 04:18:29 +01:00
|
|
|
// ------- SUGGEST PIECE -------
|
2007-09-01 09:38:10 +02:00
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void peer_connection::incoming_suggest(int index)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-09-14 05:38:38 +02:00
|
|
|
|
2007-09-01 09:38:10 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== SUGGEST_PIECE [ piece: %d ]", index);
|
2007-09-01 09:38:10 +02:00
|
|
|
#endif
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
if (!t) return;
|
|
|
|
|
2007-09-14 05:38:38 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_suggest(index)) return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2009-09-01 06:41:50 +02:00
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== INVALID_SUGGEST_PIECE [ %d ]", index);
|
2009-09-01 06:41:50 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-09-01 09:38:10 +02:00
|
|
|
|
2009-09-01 06:41:50 +02:00
|
|
|
if (t->valid_metadata())
|
|
|
|
{
|
|
|
|
if (index >= int(m_have_piece.size()))
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== INVALID_ALLOWED_FAST [ %d | s: %d ]"
|
|
|
|
, index, int(m_have_piece.size()));
|
2009-09-01 06:41:50 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we already have the piece, we can
|
|
|
|
// ignore this message
|
|
|
|
if (t->have_piece(index))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
if (int(m_suggested_pieces.size()) > m_ses.m_settings.max_suggest_pieces)
|
2007-09-01 09:38:10 +02:00
|
|
|
m_suggested_pieces.erase(m_suggested_pieces.begin());
|
2009-09-01 06:41:50 +02:00
|
|
|
|
2007-09-01 09:38:10 +02:00
|
|
|
m_suggested_pieces.push_back(index);
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("** SUGGEST_PIECE [ piece: %d added to set: %d ]", index, int(m_suggested_pieces.size()));
|
2007-09-01 09:38:10 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// ---------- UNCHOKE ----------
|
|
|
|
// -----------------------------
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_unchoke()
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_unchoke()) return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== UNCHOKE");
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
|
|
|
m_peer_choked = false;
|
2010-02-09 04:04:41 +01:00
|
|
|
m_last_unchoked = time_now();
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2009-07-23 06:38:52 +02:00
|
|
|
if (is_interesting())
|
|
|
|
{
|
2011-02-07 01:51:20 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_unchoke_piece_picks;
|
|
|
|
#endif
|
2009-07-23 06:38:52 +02:00
|
|
|
request_a_block(*t, *this);
|
|
|
|
send_block_requests();
|
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// -------- INTERESTED ---------
|
|
|
|
// -----------------------------
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_interested()
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_interested()) return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== INTERESTED");
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
|
|
|
m_peer_interested = true;
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2009-07-23 06:38:52 +02:00
|
|
|
|
2012-02-07 10:18:07 +01:00
|
|
|
// if the peer is ready to download stuff, it must have metadata
|
|
|
|
m_has_metadata = true;
|
|
|
|
|
|
|
|
disconnect_if_redundant();
|
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2013-04-07 03:20:09 +02:00
|
|
|
if (is_choked() && !t->graceful_pause())
|
2009-07-23 06:38:52 +02:00
|
|
|
{
|
|
|
|
if (ignore_unchoke_slots())
|
|
|
|
{
|
|
|
|
// if this peer is expempted from the choker
|
|
|
|
// just unchoke it immediately
|
|
|
|
send_unchoke();
|
|
|
|
}
|
2011-12-19 06:53:11 +01:00
|
|
|
else if ((m_ses.num_uploads() < m_ses.settings().unchoke_slots_limit
|
|
|
|
|| m_ses.settings().unchoke_slots_limit < 0)
|
2009-07-23 06:38:52 +02:00
|
|
|
&& (t->ratio() == 0
|
|
|
|
|| share_diff() >= size_type(-free_upload_amount)
|
|
|
|
|| t->is_finished()))
|
|
|
|
{
|
|
|
|
// if the peer is choked and we have upload slots left,
|
|
|
|
// then unchoke it. Another condition that has to be met
|
|
|
|
// is that the torrent doesn't keep track of the individual
|
|
|
|
// up/down ratio for each peer (ratio == 0) or (if it does
|
|
|
|
// keep track) this particular connection isn't a leecher.
|
|
|
|
// If the peer was choked because it was leeching, don't
|
|
|
|
// unchoke it again.
|
|
|
|
// The exception to this last condition is if we're a seed.
|
|
|
|
// In that case we don't care if people are leeching, they
|
|
|
|
// can't pay for their downloads anyway.
|
|
|
|
m_ses.unchoke_peer(*this);
|
|
|
|
}
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string reason;
|
2011-12-19 06:53:11 +01:00
|
|
|
if (m_ses.num_uploads() >= m_ses.settings().unchoke_slots_limit
|
|
|
|
&& m_ses.settings().unchoke_slots_limit >= 0)
|
2009-07-23 06:38:52 +02:00
|
|
|
{
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("DID NOT UNCHOKE [ the number of uploads (%d)"
|
|
|
|
"is more than or equal to the limit (%d) ]"
|
|
|
|
, m_ses.num_uploads(), m_ses.settings().unchoke_slots_limit);
|
2009-07-23 06:38:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("DID NOT UNCHOKE [ the share ratio (%d) is <= "
|
|
|
|
"free_upload_amount (%d) and we are not seeding and the ratio (%d) is non-zero"
|
|
|
|
, share_diff(), int(free_upload_amount), t->ratio());
|
2009-07-23 06:38:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-04-07 03:20:09 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
else if (t->graceful_pause())
|
|
|
|
{
|
|
|
|
peer_log("DID NOT UNCHOKE [ graceful pause mode ]");
|
|
|
|
}
|
|
|
|
#endif
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// ------ NOT INTERESTED -------
|
|
|
|
// -----------------------------
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_not_interested()
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_not_interested()) return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
m_became_uninterested = time_now();
|
2004-02-01 14:48:30 +01:00
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== NOT_INTERESTED");
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
m_peer_interested = false;
|
|
|
|
if (is_disconnecting()) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-07-23 06:38:52 +02:00
|
|
|
if (!is_choked())
|
2008-10-19 00:12:05 +02:00
|
|
|
{
|
2009-07-23 06:38:52 +02:00
|
|
|
if (ignore_unchoke_slots())
|
|
|
|
{
|
|
|
|
send_choke();
|
|
|
|
}
|
|
|
|
else
|
2008-10-19 00:12:05 +02:00
|
|
|
{
|
2009-07-23 06:38:52 +02:00
|
|
|
if (m_peer_info && m_peer_info->optimistically_unchoked)
|
|
|
|
{
|
|
|
|
m_peer_info->optimistically_unchoked = false;
|
|
|
|
m_ses.m_optimistic_unchoke_time_scaler = 0;
|
|
|
|
}
|
|
|
|
m_ses.choke_peer(*this);
|
|
|
|
m_ses.m_unchoke_time_scaler = 0;
|
2008-10-19 00:12:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-23 06:38:52 +02:00
|
|
|
if (t->ratio() != 0.f)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(share_diff() < (std::numeric_limits<size_type>::max)());
|
|
|
|
size_type diff = share_diff();
|
|
|
|
if (diff > 0 && is_seed())
|
|
|
|
{
|
|
|
|
// the peer is a seed and has sent
|
|
|
|
// us more than we have sent it back.
|
|
|
|
// consider the download as free download
|
|
|
|
t->add_free_upload(diff);
|
|
|
|
add_free_upload(-diff);
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 07:36:22 +01:00
|
|
|
|
2012-04-23 07:48:46 +02:00
|
|
|
if (t->super_seeding())
|
2008-12-08 07:36:22 +01:00
|
|
|
{
|
2012-04-23 07:48:46 +02:00
|
|
|
// maybe we need to try another piece, to see if the peer
|
|
|
|
// is interested in us then
|
|
|
|
superseed_piece(-1, t->get_piece_to_super_seed(m_have_piece));
|
2008-12-08 07:36:22 +01:00
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// ----------- HAVE ------------
|
|
|
|
// -----------------------------
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_have(int index)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_have(index)) return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
// if we haven't received a bitfield, it was
|
|
|
|
// probably omitted, which is the same as 'have_none'
|
|
|
|
if (!m_bitfield_received) incoming_have_none();
|
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== HAVE [ piece: %d ]", index);
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
|
|
|
|
2008-09-07 12:00:58 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2011-04-29 04:45:02 +02:00
|
|
|
if (!t->valid_metadata() && index >= int(m_have_piece.size()))
|
2007-12-21 00:58:58 +01:00
|
|
|
{
|
2012-02-07 10:18:07 +01:00
|
|
|
if (index < 131072)
|
2007-12-21 00:58:58 +01:00
|
|
|
{
|
|
|
|
// if we don't have metadata
|
|
|
|
// and we might not have received a bitfield
|
|
|
|
// extend the bitmask to fit the new
|
|
|
|
// have message
|
|
|
|
m_have_piece.resize(index + 1, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// unless the index > 64k, in which case
|
|
|
|
// we just ignore it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// if we got an invalid message, abort
|
2007-12-21 00:58:58 +01:00
|
|
|
if (index >= int(m_have_piece.size()) || index < 0)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_have, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-12-09 08:56:37 +01:00
|
|
|
if (t->super_seeding() && !m_ses.settings().strict_super_seeding)
|
2008-12-08 07:36:22 +01:00
|
|
|
{
|
|
|
|
// if we're superseeding and the peer just told
|
|
|
|
// us that it completed the piece we're superseeding
|
|
|
|
// to it, change the superseeding piece for this peer
|
|
|
|
// if the peer optimizes out redundant have messages
|
|
|
|
// this will be handled when the peer sends not-interested
|
|
|
|
// instead.
|
2012-04-23 07:48:46 +02:00
|
|
|
if (super_seeded_piece(index))
|
2008-12-08 07:36:22 +01:00
|
|
|
{
|
2012-04-23 07:48:46 +02:00
|
|
|
superseed_piece(index, t->get_piece_to_super_seed(m_have_piece));
|
2008-12-08 07:36:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
if (m_have_piece[index])
|
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log(" got redundant HAVE message for index: %d", index);
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
2009-09-12 23:27:52 +02:00
|
|
|
return;
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2009-09-12 23:27:52 +02:00
|
|
|
|
|
|
|
m_have_piece.set_bit(index);
|
|
|
|
++m_num_pieces;
|
|
|
|
|
2012-08-10 00:30:45 +02:00
|
|
|
// if the peer is downloading stuff, it must have metadata
|
|
|
|
m_has_metadata = true;
|
|
|
|
|
2009-09-12 23:27:52 +02:00
|
|
|
// only update the piece_picker if
|
|
|
|
// we have the metadata and if
|
|
|
|
// we're not a seed (in which case
|
|
|
|
// we won't have a piece picker)
|
|
|
|
if (!t->valid_metadata()) return;
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_has(index, this);
|
2009-09-12 23:27:52 +02:00
|
|
|
|
|
|
|
// this will disregard all have messages we get within
|
|
|
|
// the first two seconds. Since some clients implements
|
|
|
|
// lazy bitfields, these will not be reliable to use
|
|
|
|
// for an estimated peer download rate.
|
|
|
|
if (!peer_info_struct()
|
|
|
|
|| m_ses.session_time() - peer_info_struct()->last_connected > 2)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2009-09-12 23:27:52 +02:00
|
|
|
// update bytes downloaded since last timer
|
|
|
|
m_remote_bytes_dled += t->torrent_file().piece_size(index);
|
|
|
|
}
|
2012-02-07 10:18:07 +01:00
|
|
|
|
2009-09-12 23:27:52 +02:00
|
|
|
// it's important to not disconnect before we have
|
|
|
|
// updated the piece picker, otherwise we will incorrectly
|
|
|
|
// decrement the piece count without first incrementing it
|
|
|
|
if (is_seed())
|
|
|
|
{
|
2010-12-24 01:37:01 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info);
|
|
|
|
#endif
|
2010-03-19 19:39:51 +01:00
|
|
|
t->seen_complete();
|
2009-09-12 23:27:52 +02:00
|
|
|
t->get_policy().set_seed(m_peer_info, true);
|
|
|
|
m_upload_only = true;
|
|
|
|
disconnect_if_redundant();
|
|
|
|
if (is_disconnecting()) return;
|
|
|
|
}
|
2009-06-12 18:12:19 +02:00
|
|
|
|
2009-09-12 23:27:52 +02:00
|
|
|
if (!t->have_piece(index)
|
|
|
|
&& !t->is_seed()
|
|
|
|
&& !is_interesting()
|
|
|
|
&& t->picker().piece_priority(index) != 0)
|
|
|
|
t->get_policy().peer_is_interesting(*this);
|
|
|
|
|
|
|
|
// if we're super seeding, this might mean that somebody
|
|
|
|
// forwarded this piece. In which case we need to give
|
|
|
|
// a new piece to that peer
|
|
|
|
if (t->super_seeding()
|
|
|
|
&& m_ses.settings().strict_super_seeding
|
2012-04-23 07:48:46 +02:00
|
|
|
&& (!super_seeded_piece(index) || t->num_peers() == 1))
|
2009-09-12 23:27:52 +02:00
|
|
|
{
|
|
|
|
for (torrent::peer_iterator i = t->begin()
|
|
|
|
, end(t->end()); i != end; ++i)
|
2008-12-09 08:56:37 +01:00
|
|
|
{
|
2009-09-12 23:27:52 +02:00
|
|
|
peer_connection* p = *i;
|
2012-04-23 07:48:46 +02:00
|
|
|
if (!p->super_seeded_piece(index)) continue;
|
2009-09-12 23:27:52 +02:00
|
|
|
if (!p->has_piece(index)) continue;
|
2012-04-23 07:48:46 +02:00
|
|
|
p->superseed_piece(index, t->get_piece_to_super_seed(p->get_bitfield()));
|
2008-12-09 08:56:37 +01:00
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
|
|
|
}
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2011-08-07 02:41:13 +02:00
|
|
|
// -----------------------------
|
|
|
|
// -------- DONT HAVE ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void peer_connection::incoming_dont_have(int index)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_dont_have(index)) return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("<== DONT_HAVE [ piece: %d ]", index);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// if we got an invalid message, abort
|
|
|
|
if (index >= int(m_have_piece.size()) || index < 0)
|
|
|
|
{
|
|
|
|
disconnect(errors::invalid_dont_have, 2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_have_piece[index])
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
peer_log(" got redundant DONT_HAVE message for index: %d", index);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool was_seed = is_seed();
|
|
|
|
m_have_piece.clear_bit(index);
|
|
|
|
TORRENT_ASSERT(m_num_pieces > 0);
|
|
|
|
--m_num_pieces;
|
|
|
|
|
|
|
|
// only update the piece_picker if
|
|
|
|
// we have the metadata and if
|
|
|
|
// we're not a seed (in which case
|
|
|
|
// we won't have a piece picker)
|
|
|
|
if (!t->valid_metadata()) return;
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_lost(index, this);
|
2011-08-07 02:41:13 +02:00
|
|
|
|
|
|
|
if (was_seed)
|
|
|
|
t->get_policy().set_seed(m_peer_info, false);
|
|
|
|
}
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// --------- BITFIELD ----------
|
|
|
|
// -----------------------------
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
void peer_connection::incoming_bitfield(bitfield const& bits)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2008-05-28 04:35:02 +02:00
|
|
|
if ((*i)->on_bitfield(bits)) return;
|
2007-04-02 22:00:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
std::string bitfield_str;
|
|
|
|
bitfield_str.resize(bits.size());
|
2008-05-28 04:35:02 +02:00
|
|
|
for (int i = 0; i < int(bits.size()); ++i)
|
2010-10-31 23:12:26 +01:00
|
|
|
bitfield_str[i] = bits[i] ? '1' : '0';
|
|
|
|
peer_log("<== BITFIELD [ %s ]", bitfield_str.c_str());
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// if we don't have the metedata, we cannot
|
|
|
|
// verify the bitfield size
|
|
|
|
if (t->valid_metadata()
|
2008-08-29 19:21:56 +02:00
|
|
|
&& (bits.size() + 7) / 8 != (m_have_piece.size() + 7) / 8)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_bitfield_size, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2012-05-02 03:08:25 +02:00
|
|
|
if (m_bitfield_received)
|
|
|
|
{
|
|
|
|
// if we've already received a bitfield message
|
|
|
|
// we first need to count down all the pieces
|
|
|
|
// we believe the peer has first
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_lost(m_have_piece, this);
|
2012-05-02 03:08:25 +02:00
|
|
|
}
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
m_bitfield_received = true;
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
// if we don't have metadata yet
|
|
|
|
// just remember the bitmask
|
|
|
|
// don't update the piecepicker
|
|
|
|
// (since it doesn't exist yet)
|
2007-03-17 00:28:26 +01:00
|
|
|
if (!t->ready_for_connections())
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2010-12-24 01:37:01 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
if (m_num_pieces == int(bits.size()))
|
|
|
|
peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info);
|
|
|
|
#endif
|
2008-05-28 04:35:02 +02:00
|
|
|
m_have_piece = bits;
|
|
|
|
m_num_pieces = bits.count();
|
2009-06-13 06:14:41 +02:00
|
|
|
t->get_policy().set_seed(m_peer_info, m_num_pieces == int(bits.size()));
|
2004-06-14 01:30:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
int num_pieces = bits.count();
|
2007-04-13 03:53:25 +02:00
|
|
|
if (num_pieces == int(m_have_piece.size()))
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-24 01:37:01 +01:00
|
|
|
peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info);
|
2007-04-13 03:53:25 +02:00
|
|
|
#endif
|
2007-04-13 03:57:16 +02:00
|
|
|
// if this is a web seed. we don't have a peer_info struct
|
2009-06-13 06:14:41 +02:00
|
|
|
t->get_policy().set_seed(m_peer_info, true);
|
2008-07-18 12:03:42 +02:00
|
|
|
m_upload_only = true;
|
2007-04-15 04:14:02 +02:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
m_have_piece.set_all();
|
2007-04-15 04:14:02 +02:00
|
|
|
m_num_pieces = num_pieces;
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_has_all(this);
|
2011-04-10 01:57:56 +02:00
|
|
|
if (!t->is_upload_only())
|
2007-04-15 04:14:02 +02:00
|
|
|
t->get_policy().peer_is_interesting(*this);
|
2008-08-12 10:18:05 +02:00
|
|
|
|
|
|
|
disconnect_if_redundant();
|
|
|
|
|
2007-04-15 04:14:02 +02:00
|
|
|
return;
|
2007-04-13 03:53:25 +02:00
|
|
|
}
|
|
|
|
|
2007-02-12 06:46:29 +01:00
|
|
|
// let the torrent know which pieces the
|
|
|
|
// peer has
|
2007-04-15 04:14:02 +02:00
|
|
|
// if we're a seed, we don't keep track of piece availability
|
2008-01-31 18:52:29 +01:00
|
|
|
bool interesting = false;
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_has(bits, this);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
m_have_piece = bits;
|
2008-01-31 18:52:29 +01:00
|
|
|
m_num_pieces = num_pieces;
|
|
|
|
|
|
|
|
if (interesting) t->get_policy().peer_is_interesting(*this);
|
2013-08-09 09:55:12 +02:00
|
|
|
else if (upload_only()
|
|
|
|
&& can_disconnect(error_code(errors::upload_upload_connection, get_libtorrent_category())))
|
|
|
|
disconnect(errors::upload_upload_connection);
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2003-12-17 20:03:23 +01:00
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
void peer_connection::disconnect_if_redundant()
|
|
|
|
{
|
2009-08-08 17:27:07 +02:00
|
|
|
// we cannot disconnect in a constructor
|
|
|
|
TORRENT_ASSERT(m_in_constructor == false);
|
2008-07-18 12:03:42 +02:00
|
|
|
if (!m_ses.settings().close_redundant_connections) return;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2009-08-02 02:05:41 +02:00
|
|
|
if (!t) return;
|
|
|
|
|
2012-02-07 10:18:07 +01:00
|
|
|
// if we don't have the metadata yet, don't disconnect
|
|
|
|
// also, if the peer doesn't have metadata we shouldn't
|
|
|
|
// disconnect it, since it may want to request the
|
|
|
|
// metadata from us
|
|
|
|
if (!t->valid_metadata() || !has_metadata()) return;
|
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
// don't close connections in share mode, we don't know if we need them
|
|
|
|
if (t->share_mode()) return;
|
|
|
|
|
2011-03-25 06:14:14 +01:00
|
|
|
if (m_upload_only && t->is_upload_only())
|
2008-11-01 11:14:11 +01:00
|
|
|
{
|
2013-08-09 09:55:12 +02:00
|
|
|
if (!can_disconnect(error_code(errors::upload_upload_connection, get_libtorrent_category()))) return;
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::upload_upload_connection);
|
2008-11-01 11:14:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-07-18 12:03:42 +02:00
|
|
|
|
|
|
|
if (m_upload_only
|
|
|
|
&& !m_interesting
|
|
|
|
&& m_bitfield_received
|
|
|
|
&& t->are_files_checked())
|
2008-11-01 11:14:11 +01:00
|
|
|
{
|
2013-08-09 09:55:12 +02:00
|
|
|
if (!can_disconnect(error_code(errors::uninteresting_upload_peer, get_libtorrent_category()))) return;
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::uninteresting_upload_peer);
|
2008-11-01 11:14:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-07-18 12:03:42 +02:00
|
|
|
}
|
|
|
|
|
2013-11-02 04:26:53 +01:00
|
|
|
bool peer_connection::can_disconnect(error_code const& ec) const
|
2013-08-09 09:55:12 +02:00
|
|
|
{
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2013-11-02 04:26:53 +01:00
|
|
|
for (extension_list_t::const_iterator i = m_extensions.begin()
|
2013-08-09 09:55:12 +02:00
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (!(*i)->can_disconnect(ec)) return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// ---------- REQUEST ----------
|
|
|
|
// -----------------------------
|
2003-12-17 20:03:23 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_request(peer_request const& r)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2004-01-05 00:51:54 +01:00
|
|
|
|
2012-03-09 07:24:01 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_piece_requests;
|
|
|
|
#endif
|
|
|
|
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2012-01-14 17:04:25 +01:00
|
|
|
peer_log("<== REQUEST [ piece: %d s: %d l: %d ]"
|
2010-12-19 20:40:32 +01:00
|
|
|
, r.piece, r.start, r.length);
|
|
|
|
#endif
|
|
|
|
|
2012-04-23 07:48:46 +02:00
|
|
|
if (t->super_seeding()
|
|
|
|
&& !super_seeded_piece(r.piece))
|
2008-12-08 07:36:22 +01:00
|
|
|
{
|
2012-03-09 07:24:01 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_invalid_piece_requests;
|
|
|
|
#endif
|
2008-12-08 07:36:22 +01:00
|
|
|
++m_num_invalid_requests;
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
peer_log("*** INVALID_REQUEST [ piece not superseeded "
|
2012-04-23 07:48:46 +02:00
|
|
|
"i: %d t: %d n: %d h: %d ss1: %d ss2: %d ]"
|
2010-12-19 20:40:32 +01:00
|
|
|
, m_peer_interested
|
2010-10-31 23:12:26 +01:00
|
|
|
, int(t->torrent_file().piece_size(r.piece))
|
|
|
|
, t->torrent_file().num_pieces()
|
|
|
|
, t->have_piece(r.piece)
|
2012-04-23 07:48:46 +02:00
|
|
|
, m_superseed_piece[0]
|
|
|
|
, m_superseed_piece[1]);
|
2008-12-08 07:36:22 +01:00
|
|
|
#endif
|
|
|
|
|
2012-04-23 07:48:46 +02:00
|
|
|
write_reject_request(r);
|
|
|
|
|
2008-12-08 07:36:22 +01:00
|
|
|
if (t->alerts().should_post<invalid_request_alert>())
|
|
|
|
{
|
|
|
|
t->alerts().post_alert(invalid_request_alert(
|
|
|
|
t->get_handle(), m_remote, m_peer_id, r));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
// if we haven't received a bitfield, it was
|
|
|
|
// probably omitted, which is the same as 'have_none'
|
|
|
|
if (!m_bitfield_received) incoming_have_none();
|
2008-09-07 12:00:58 +02:00
|
|
|
if (is_disconnecting()) return;
|
2008-07-18 12:03:42 +02:00
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_request(r)) return;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-04-02 22:00:24 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!t->valid_metadata())
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2012-03-09 07:24:01 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_invalid_piece_requests;
|
|
|
|
#endif
|
2004-06-14 01:30:42 +02:00
|
|
|
// if we don't have valid metadata yet,
|
|
|
|
// we shouldn't get a request
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** INVALID_REQUEST [ we don't have metadata yet ]");
|
|
|
|
peer_log("==> REJECT_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece , r.start , r.length);
|
2004-06-14 01:30:42 +02:00
|
|
|
#endif
|
2007-08-14 19:47:48 +02:00
|
|
|
write_reject_request(r);
|
2004-06-14 01:30:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
if (int(m_requests.size()) > m_ses.settings().max_allowed_in_request_queue)
|
2004-02-20 16:22:23 +01:00
|
|
|
{
|
2012-03-09 07:24:01 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_max_piece_requests;
|
|
|
|
#endif
|
2004-02-20 16:22:23 +01:00
|
|
|
// don't allow clients to abuse our
|
|
|
|
// memory consumption.
|
|
|
|
// ignore requests if the client
|
|
|
|
// is making too many of them.
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** INVALID_REQUEST [ incoming request queue full %d ]"
|
|
|
|
, int(m_requests.size()));
|
|
|
|
peer_log("==> REJECT_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece , r.start , r.length);
|
2004-06-14 01:30:42 +02:00
|
|
|
#endif
|
2007-08-14 19:47:48 +02:00
|
|
|
write_reject_request(r);
|
2004-02-20 16:22:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-26 18:59:10 +02:00
|
|
|
int fast_idx = -1;
|
|
|
|
std::vector<int>::iterator fast_iter = std::find(m_accept_fast.begin()
|
|
|
|
, m_accept_fast.end(), r.piece);
|
|
|
|
if (fast_iter != m_accept_fast.end()) fast_idx = fast_iter - m_accept_fast.begin();
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// make sure this request
|
2004-11-30 12:17:32 +01:00
|
|
|
// is legal and that the peer
|
2004-01-05 00:51:54 +01:00
|
|
|
// is not choked
|
|
|
|
if (r.piece >= 0
|
2006-04-25 23:04:48 +02:00
|
|
|
&& r.piece < t->torrent_file().num_pieces()
|
|
|
|
&& t->have_piece(r.piece)
|
2004-01-05 00:51:54 +01:00
|
|
|
&& r.start >= 0
|
2006-04-25 23:04:48 +02:00
|
|
|
&& r.start < t->torrent_file().piece_size(r.piece)
|
2004-01-05 00:51:54 +01:00
|
|
|
&& r.length > 0
|
2006-04-25 23:04:48 +02:00
|
|
|
&& r.length + r.start <= t->torrent_file().piece_size(r.piece)
|
2007-08-03 18:12:32 +02:00
|
|
|
&& m_peer_interested
|
|
|
|
&& r.length <= t->block_size())
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
|
|
|
// if we have choked the client
|
|
|
|
// ignore the request
|
2013-07-26 18:59:10 +02:00
|
|
|
const int blocks_per_piece = static_cast<int>(
|
|
|
|
(t->torrent_file().piece_length() + t->block_size() - 1) / t->block_size());
|
|
|
|
|
|
|
|
// disconnect peers that downloads more than foo times an allowed
|
|
|
|
// fast piece
|
2013-08-09 09:55:12 +02:00
|
|
|
if (m_choked && fast_idx != -1
|
|
|
|
&& m_accept_fast_piece_cnt[fast_idx] >= 3 * blocks_per_piece
|
|
|
|
&& can_disconnect(error_code(errors::too_many_requests_when_choked, get_libtorrent_category())))
|
2013-07-26 18:59:10 +02:00
|
|
|
{
|
|
|
|
disconnect(errors::too_many_requests_when_choked);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_choked && fast_idx == -1)
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** REJECTING REQUEST [ peer choked and piece not in allowed fast set ]");
|
|
|
|
peer_log(" ==> REJECT_PIECE [ piece: %d | s: %d | l: %d ]"
|
|
|
|
, r.piece, r.start, r.length);
|
2012-03-09 07:24:01 +01:00
|
|
|
#endif
|
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_choked_piece_requests;
|
2007-08-14 19:47:48 +02:00
|
|
|
#endif
|
2010-12-19 20:40:32 +01:00
|
|
|
write_reject_request(r);
|
2009-03-12 18:06:41 +01:00
|
|
|
|
2013-03-26 05:45:38 +01:00
|
|
|
time_duration since_choked = time_now() - m_last_choke;
|
|
|
|
|
|
|
|
// allow peers to send request up to 2 seconds after getting choked,
|
|
|
|
// the disconnect them
|
2013-08-09 09:55:12 +02:00
|
|
|
if (total_milliseconds(since_choked) > 2000
|
|
|
|
&& can_disconnect(error_code(errors::too_many_requests_when_choked, get_libtorrent_category())))
|
2009-03-12 18:06:41 +01:00
|
|
|
{
|
2011-02-14 05:48:02 +01:00
|
|
|
disconnect(errors::too_many_requests_when_choked, 2);
|
2009-03-12 18:06:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-26 18:59:10 +02:00
|
|
|
// increase the allowed fast set counter
|
|
|
|
if (fast_idx != -1)
|
|
|
|
++m_accept_fast_piece_cnt[fast_idx];
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
m_requests.push_back(r);
|
2011-12-29 13:15:29 +01:00
|
|
|
#ifdef TORRENT_REQUEST_LOGGING
|
|
|
|
if (m_ses.m_request_log)
|
|
|
|
write_request_log(m_ses.m_request_log, t->info_hash(), this, r);
|
|
|
|
#endif
|
2007-09-01 09:38:10 +02:00
|
|
|
m_last_incoming_request = time_now();
|
2007-08-14 19:47:48 +02:00
|
|
|
fill_send_buffer();
|
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-09 07:24:01 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_invalid_piece_requests;
|
|
|
|
#endif
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** INVALID_REQUEST [ "
|
|
|
|
"i: %d t: %d n: %d h: %d block_limit: %d ]"
|
|
|
|
, m_peer_interested
|
|
|
|
, int(t->torrent_file().piece_size(r.piece))
|
|
|
|
, t->torrent_file().num_pieces()
|
|
|
|
, t->have_piece(r.piece)
|
|
|
|
, t->block_size());
|
2008-01-02 04:18:29 +01:00
|
|
|
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("==> REJECT_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece , r.start , r.length);
|
2004-01-05 02:30:34 +01:00
|
|
|
#endif
|
2004-01-12 21:31:27 +01:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
write_reject_request(r);
|
2004-01-12 21:31:27 +01:00
|
|
|
++m_num_invalid_requests;
|
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (t->alerts().should_post<invalid_request_alert>())
|
2004-01-08 14:03:38 +01:00
|
|
|
{
|
2008-07-08 11:30:10 +02:00
|
|
|
t->alerts().post_alert(invalid_request_alert(
|
|
|
|
t->get_handle(), m_remote, m_peer_id, r));
|
2004-01-08 14:03:38 +01:00
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
void peer_connection::incoming_piece_fragment(int bytes)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_piece = time_now();
|
2009-06-28 09:39:06 +02:00
|
|
|
TORRENT_ASSERT(m_outstanding_bytes >= bytes);
|
2009-03-17 10:34:44 +01:00
|
|
|
m_outstanding_bytes -= bytes;
|
2009-06-28 23:50:18 +02:00
|
|
|
if (m_outstanding_bytes < 0) m_outstanding_bytes = 0;
|
2009-04-11 22:45:14 +02:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2011-11-15 03:34:00 +01:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2009-04-11 22:45:14 +02:00
|
|
|
TORRENT_ASSERT(m_received_in_piece + bytes <= t->block_size());
|
2009-03-17 10:34:44 +01:00
|
|
|
m_received_in_piece += bytes;
|
|
|
|
#endif
|
2011-11-15 03:34:00 +01:00
|
|
|
|
|
|
|
// progress of this torrent increased
|
|
|
|
t->state_updated();
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_DEBUG
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::start_receive_piece(peer_request const& r)
|
|
|
|
{
|
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_DEBUG
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2009-03-17 10:34:44 +01:00
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
int recv_pos = recv_buffer.end - recv_buffer.begin;
|
|
|
|
TORRENT_ASSERT(recv_pos >= 9);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t);
|
2013-03-23 04:01:13 +01:00
|
|
|
|
|
|
|
// piece_block can't necessarily hold large piece numbers
|
|
|
|
// so check that first
|
|
|
|
if (r.piece < 0
|
|
|
|
|| r.piece >= t->torrent_file().num_pieces()
|
|
|
|
|| r.start < 0
|
|
|
|
|| r.start > t->torrent_file().piece_length())
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
peer_log("*** INVALID_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece, r.start, r.length);
|
|
|
|
#endif
|
|
|
|
disconnect(errors::invalid_piece, 2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
piece_block b(r.piece, r.start / t->block_size());
|
2009-07-01 06:14:43 +02:00
|
|
|
m_receiving_block = b;
|
2009-03-17 10:34:44 +01:00
|
|
|
|
|
|
|
if (!verify_piece(r))
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** INVALID_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece, r.start, r.length);
|
2009-03-17 10:34:44 +01:00
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_piece, 2);
|
2009-03-17 10:34:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool in_req_queue = false;
|
|
|
|
for (std::vector<pending_block>::const_iterator i = m_download_queue.begin()
|
|
|
|
, end(m_download_queue.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->block != b) continue;
|
|
|
|
in_req_queue = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-28 09:48:31 +02:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
// if this is not in the request queue, we have to
|
|
|
|
// assume our outstanding bytes includes this piece too
|
2010-10-03 03:22:11 +02:00
|
|
|
// if we're disconnecting, we shouldn't add pieces
|
|
|
|
if (!in_req_queue && !m_disconnecting)
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
2009-12-25 17:52:57 +01:00
|
|
|
for (std::vector<pending_block>::iterator i = m_request_queue.begin()
|
2009-08-28 09:48:31 +02:00
|
|
|
, end(m_request_queue.end()); i != end; ++i)
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
2009-12-25 17:52:57 +01:00
|
|
|
if (i->block != b) continue;
|
2009-08-28 09:48:31 +02:00
|
|
|
in_req_queue = true;
|
2012-05-12 09:14:07 +02:00
|
|
|
if (i - m_request_queue.begin() < m_queued_time_critical)
|
|
|
|
--m_queued_time_critical;
|
2009-08-28 09:48:31 +02:00
|
|
|
m_request_queue.erase(i);
|
|
|
|
break;
|
2009-03-17 10:34:44 +01:00
|
|
|
}
|
2009-08-28 09:48:31 +02:00
|
|
|
|
2010-05-13 08:29:33 +02:00
|
|
|
m_download_queue.insert(m_download_queue.begin(), b);
|
2009-08-28 09:48:31 +02:00
|
|
|
if (!in_req_queue)
|
|
|
|
{
|
|
|
|
if (t->alerts().should_post<unwanted_block_alert>())
|
|
|
|
{
|
|
|
|
t->alerts().post_alert(unwanted_block_alert(t->get_handle(), m_remote
|
|
|
|
, m_peer_id, b.block_index, b.piece_index));
|
|
|
|
}
|
2009-03-17 10:34:44 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** The block we just got was not in the request queue ***");
|
2009-03-17 10:34:44 +01:00
|
|
|
#endif
|
2010-05-13 08:29:33 +02:00
|
|
|
TORRENT_ASSERT(m_download_queue.front().block == b);
|
|
|
|
m_download_queue.front().not_wanted = true;
|
2009-08-28 09:48:31 +02:00
|
|
|
}
|
2009-03-17 10:34:44 +01:00
|
|
|
m_outstanding_bytes += r.length;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2006-12-22 01:45:43 +01:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-12-22 01:45:43 +01:00
|
|
|
struct check_postcondition
|
|
|
|
{
|
|
|
|
check_postcondition(boost::shared_ptr<torrent> const& t_
|
|
|
|
, bool init_check = true): t(t_) { if (init_check) check(); }
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-12-22 01:45:43 +01:00
|
|
|
~check_postcondition() { check(); }
|
|
|
|
|
|
|
|
void check()
|
|
|
|
{
|
|
|
|
if (!t->is_seed())
|
|
|
|
{
|
|
|
|
const int blocks_per_piece = static_cast<int>(
|
2010-02-23 17:26:24 +01:00
|
|
|
(t->torrent_file().piece_length() + t->block_size() - 1) / t->block_size());
|
2006-12-22 01:45:43 +01:00
|
|
|
|
|
|
|
std::vector<piece_picker::downloading_piece> const& dl_queue
|
|
|
|
= t->picker().get_download_queue();
|
|
|
|
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i =
|
|
|
|
dl_queue.begin(); i != dl_queue.end(); ++i)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->finished <= blocks_per_piece);
|
2006-12-22 01:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<torrent> t;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// ----------- PIECE -----------
|
|
|
|
// -----------------------------
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_piece(peer_request const& p, char const* data)
|
2008-04-10 12:03:23 +02:00
|
|
|
{
|
2009-01-23 10:13:31 +01:00
|
|
|
char* buffer = m_ses.allocate_disk_buffer("receive buffer");
|
2008-04-10 12:03:23 +02:00
|
|
|
if (buffer == 0)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2008-04-10 12:03:23 +02:00
|
|
|
return;
|
2008-06-29 11:50:42 +02:00
|
|
|
}
|
2008-04-10 12:03:23 +02:00
|
|
|
disk_buffer_holder holder(m_ses, buffer);
|
2008-04-29 05:12:14 +02:00
|
|
|
std::memcpy(buffer, data, p.length);
|
2008-04-10 12:03:23 +02:00
|
|
|
incoming_piece(p, holder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::incoming_piece(peer_request const& p, disk_buffer_holder& data)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-04-02 22:00:24 +02:00
|
|
|
|
2008-05-05 08:25:22 +02:00
|
|
|
TORRENT_ASSERT(!m_disk_recv_buffer);
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(m_disk_recv_buffer_size == 0);
|
|
|
|
|
2009-07-01 06:14:43 +02:00
|
|
|
// we're not receiving any block right now
|
2011-03-13 09:07:33 +01:00
|
|
|
m_receiving_block = piece_block::invalid;
|
2009-07-01 06:14:43 +02:00
|
|
|
|
2008-06-23 17:37:24 +02:00
|
|
|
#ifdef TORRENT_CORRUPT_DATA
|
|
|
|
// corrupt all pieces from certain peers
|
|
|
|
if (m_remote.address().is_v4()
|
|
|
|
&& (m_remote.address().to_v4().to_ulong() & 0xf) == 0)
|
|
|
|
{
|
|
|
|
data.get()[0] = ~data.get()[0];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
// if we haven't received a bitfield, it was
|
|
|
|
// probably omitted, which is the same as 'have_none'
|
|
|
|
if (!m_bitfield_received) incoming_have_none();
|
2008-09-07 12:00:58 +02:00
|
|
|
if (is_disconnecting()) return;
|
2008-07-18 12:03:42 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
update_desired_queue_size();
|
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2009-03-17 10:34:44 +01:00
|
|
|
if ((*i)->on_piece(p, data))
|
|
|
|
{
|
2011-05-09 01:52:06 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2009-04-11 22:45:14 +02:00
|
|
|
TORRENT_ASSERT(m_received_in_piece == p.length);
|
2009-03-17 10:34:44 +01:00
|
|
|
m_received_in_piece = 0;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-04-02 22:00:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-04-02 22:00:24 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-12-22 01:45:43 +01:00
|
|
|
check_postcondition post_checker_(t);
|
2008-10-10 07:25:55 +02:00
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2006-12-11 13:48:33 +01:00
|
|
|
t->check_invariant();
|
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2012-03-26 17:24:14 +02:00
|
|
|
hasher h;
|
|
|
|
h.update(data.get(), p.length);
|
|
|
|
peer_log("<== PIECE [ piece: %d | s: %d | l: %d | ds: %d | qs: %d | q: %d | hash: %s ]"
|
2010-10-31 23:12:26 +01:00
|
|
|
, p.piece, p.start, p.length, statistics().download_rate()
|
2012-03-26 17:24:14 +02:00
|
|
|
, int(m_desired_queue_size), int(m_download_queue.size())
|
|
|
|
, to_hex(h.final().to_string()).c_str());
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2008-07-09 19:37:38 +02:00
|
|
|
if (p.length == 0)
|
|
|
|
{
|
|
|
|
if (t->alerts().should_post<peer_error_alert>())
|
|
|
|
{
|
|
|
|
t->alerts().post_alert(peer_error_alert(t->get_handle(), m_remote
|
2009-11-29 08:06:38 +01:00
|
|
|
, m_peer_id, errors::peer_sent_empty_piece));
|
2008-07-09 19:37:38 +02:00
|
|
|
}
|
2008-11-17 20:29:14 +01:00
|
|
|
// This is used as a reject-request by bitcomet
|
|
|
|
incoming_reject_request(p);
|
2008-07-09 19:37:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-12-04 21:42:47 +01:00
|
|
|
// if we're already seeding, don't bother,
|
|
|
|
// just ignore it
|
2006-12-16 03:19:53 +01:00
|
|
|
if (t->is_seed())
|
|
|
|
{
|
2011-05-09 01:52:06 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2009-04-11 22:45:14 +02:00
|
|
|
TORRENT_ASSERT(m_received_in_piece == p.length);
|
2009-03-17 10:34:44 +01:00
|
|
|
m_received_in_piece = 0;
|
|
|
|
#endif
|
|
|
|
if (!m_download_queue.empty()) m_download_queue.erase(m_download_queue.begin());
|
2011-11-16 03:29:59 +01:00
|
|
|
t->add_redundant_bytes(p.length, torrent::piece_seed);
|
2006-12-16 03:19:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-12-04 21:42:47 +01:00
|
|
|
|
2008-07-10 21:31:22 +02:00
|
|
|
ptime now = time_now();
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
piece_picker& picker = t->picker();
|
2006-12-11 13:48:33 +01:00
|
|
|
piece_manager& fs = t->filesystem();
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2006-12-11 16:43:27 +01:00
|
|
|
std::vector<piece_block> finished_blocks;
|
2006-04-25 23:04:48 +02:00
|
|
|
piece_block block_finished(p.piece, p.start / t->block_size());
|
2010-02-23 17:26:24 +01:00
|
|
|
TORRENT_ASSERT(verify_piece(p));
|
2006-12-21 12:37:03 +01:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
std::vector<pending_block>::iterator b
|
2008-07-07 14:04:06 +02:00
|
|
|
= std::find_if(
|
2006-12-21 12:37:03 +01:00
|
|
|
m_download_queue.begin()
|
|
|
|
, m_download_queue.end()
|
2008-07-07 14:04:06 +02:00
|
|
|
, has_block(block_finished));
|
2003-12-08 22:59:48 +01:00
|
|
|
|
2008-03-14 11:17:27 +01:00
|
|
|
if (b == m_download_queue.end())
|
2006-12-21 12:37:03 +01:00
|
|
|
{
|
2008-07-09 19:40:03 +02:00
|
|
|
if (t->alerts().should_post<unwanted_block_alert>())
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
2008-07-08 20:41:04 +02:00
|
|
|
t->alerts().post_alert(unwanted_block_alert(t->get_handle(), m_remote
|
2010-03-08 09:03:53 +01:00
|
|
|
, m_peer_id, block_finished.block_index, block_finished.piece_index));
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** The block we just got was not in the request queue ***");
|
2009-03-17 10:34:44 +01:00
|
|
|
#endif
|
2011-05-09 01:52:06 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2011-03-24 17:46:26 +01:00
|
|
|
TORRENT_ASSERT_VAL(m_received_in_piece == p.length, m_received_in_piece);
|
2009-03-17 10:34:44 +01:00
|
|
|
m_received_in_piece = 0;
|
2006-12-21 12:37:03 +01:00
|
|
|
#endif
|
2011-11-16 03:29:59 +01:00
|
|
|
t->add_redundant_bytes(p.length, torrent::piece_unknown);
|
2007-07-06 19:15:35 +02:00
|
|
|
return;
|
2006-12-21 12:37:03 +01:00
|
|
|
}
|
2009-03-17 10:34:44 +01:00
|
|
|
|
2011-05-08 11:04:59 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-09-25 22:12:53 +02:00
|
|
|
pending_block pending_b = *b;
|
|
|
|
#endif
|
2003-12-08 22:59:48 +01:00
|
|
|
|
2009-05-31 07:50:07 +02:00
|
|
|
int block_index = b - m_download_queue.begin();
|
|
|
|
TORRENT_ASSERT(m_download_queue[block_index] == pending_b);
|
2008-08-25 17:25:46 +02:00
|
|
|
for (int i = 0; i < block_index; ++i)
|
2008-03-14 11:17:27 +01:00
|
|
|
{
|
2008-08-25 17:25:46 +02:00
|
|
|
pending_block& qe = m_download_queue[i];
|
2009-05-31 07:50:07 +02:00
|
|
|
TORRENT_ASSERT(m_download_queue[block_index] == pending_b);
|
|
|
|
TORRENT_ASSERT(i < block_index);
|
2008-07-07 14:04:06 +02:00
|
|
|
|
2008-03-14 11:17:27 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** SKIPPED_PIECE [ piece: %d b: %d dqs: %d ]"
|
|
|
|
, qe.block.piece_index, qe.block.block_index, int(m_desired_queue_size));
|
2008-03-14 11:17:27 +01:00
|
|
|
#endif
|
2008-07-07 14:04:06 +02:00
|
|
|
|
2008-08-25 17:25:46 +02:00
|
|
|
++qe.skipped;
|
2008-07-07 14:04:06 +02:00
|
|
|
// if the number of times a block is skipped by out of order
|
|
|
|
// blocks exceeds the size of the outstanding queue, assume that
|
|
|
|
// the other end dropped the request.
|
2010-01-14 03:16:23 +01:00
|
|
|
if (m_ses.m_settings.drop_skipped_requests
|
|
|
|
&& qe.skipped > m_desired_queue_size)
|
2008-07-07 14:04:06 +02:00
|
|
|
{
|
|
|
|
if (m_ses.m_alerts.should_post<request_dropped_alert>())
|
|
|
|
m_ses.m_alerts.post_alert(request_dropped_alert(t->get_handle()
|
2008-08-25 17:25:46 +02:00
|
|
|
, remote(), pid(), qe.block.block_index, qe.block.piece_index));
|
2009-03-17 10:34:44 +01:00
|
|
|
|
2010-01-14 03:16:23 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** DROPPED_PIECE [ piece: %d b: %d dqs: %d skip: %d ]"
|
|
|
|
, qe.block.piece_index, qe.block.block_index
|
|
|
|
, int(m_desired_queue_size), qe.skipped);
|
2010-01-14 03:16:23 +01:00
|
|
|
#endif
|
2009-03-17 10:34:44 +01:00
|
|
|
if (!qe.timed_out && !qe.not_wanted)
|
2010-10-04 00:06:53 +02:00
|
|
|
picker.abort_download(qe.block, peer_info_struct());
|
2009-03-17 10:34:44 +01:00
|
|
|
|
2010-05-01 18:17:37 +02:00
|
|
|
TORRENT_ASSERT(m_outstanding_bytes >= t->to_req(qe.block).length);
|
|
|
|
m_outstanding_bytes -= t->to_req(qe.block).length;
|
|
|
|
if (m_outstanding_bytes < 0) m_outstanding_bytes = 0;
|
2009-04-25 10:19:21 +02:00
|
|
|
TORRENT_ASSERT(m_download_queue[block_index] == pending_b);
|
2008-08-25 17:25:46 +02:00
|
|
|
m_download_queue.erase(m_download_queue.begin() + i);
|
|
|
|
--i;
|
|
|
|
--block_index;
|
2009-05-31 07:50:07 +02:00
|
|
|
TORRENT_ASSERT(m_download_queue[block_index] == pending_b);
|
2009-03-17 10:34:44 +01:00
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_DEBUG
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
2008-03-14 11:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
2009-05-31 12:43:29 +02:00
|
|
|
TORRENT_ASSERT(int(m_download_queue.size()) > block_index);
|
2009-05-31 07:50:07 +02:00
|
|
|
b = m_download_queue.begin() + block_index;
|
|
|
|
TORRENT_ASSERT(*b == pending_b);
|
2008-07-07 14:04:06 +02:00
|
|
|
|
2011-05-09 01:52:06 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2011-03-24 17:46:26 +01:00
|
|
|
TORRENT_ASSERT_VAL(m_received_in_piece == p.length, m_received_in_piece);
|
2009-03-17 10:34:44 +01:00
|
|
|
m_received_in_piece = 0;
|
|
|
|
#endif
|
2006-12-21 12:37:03 +01:00
|
|
|
// if the block we got is already finished, then ignore it
|
2007-06-10 22:46:09 +02:00
|
|
|
if (picker.is_downloaded(block_finished))
|
2006-12-21 12:37:03 +01:00
|
|
|
{
|
2011-11-16 03:29:59 +01:00
|
|
|
torrent::wasted_reason_t reason;
|
|
|
|
if (b->timed_out) reason = torrent::piece_timed_out;
|
|
|
|
else if (b->not_wanted) reason = torrent::piece_cancelled;
|
|
|
|
else if (b->busy) reason = torrent::piece_end_game;
|
|
|
|
else reason = torrent::piece_unknown;
|
|
|
|
|
|
|
|
t->add_redundant_bytes(p.length, reason);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-03-14 11:17:27 +01:00
|
|
|
m_download_queue.erase(b);
|
2008-07-07 14:04:06 +02:00
|
|
|
m_timeout_extend = 0;
|
2008-06-29 11:50:42 +02:00
|
|
|
|
|
|
|
if (!m_download_queue.empty())
|
2008-07-10 21:31:22 +02:00
|
|
|
m_requested = now;
|
2008-06-29 11:50:42 +02:00
|
|
|
|
2011-02-07 01:51:20 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_incoming_redundant_piece_picks;
|
|
|
|
#endif
|
2007-08-14 19:47:48 +02:00
|
|
|
request_a_block(*t, *this);
|
|
|
|
send_block_requests();
|
2006-12-21 12:37:03 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2006-12-21 12:37:03 +01:00
|
|
|
|
2008-07-10 21:31:22 +02:00
|
|
|
if (total_seconds(now - m_requested)
|
2008-07-07 14:04:06 +02:00
|
|
|
< m_ses.settings().request_timeout
|
|
|
|
&& m_snubbed)
|
|
|
|
{
|
|
|
|
m_snubbed = false;
|
|
|
|
if (m_ses.m_alerts.should_post<peer_unsnubbed_alert>())
|
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(peer_unsnubbed_alert(t->get_handle()
|
|
|
|
, m_remote, m_peer_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 07:27:45 +02:00
|
|
|
if (t->is_deleted()) return;
|
|
|
|
|
2011-03-16 08:45:51 +01:00
|
|
|
int write_queue_size = fs.async_write(p, data, boost::bind(&peer_connection::on_disk_write_complete
|
2007-06-10 22:46:09 +02:00
|
|
|
, self(), _1, _2, p, t));
|
2007-08-01 07:22:34 +02:00
|
|
|
m_outstanding_writing_bytes += p.length;
|
2008-03-14 11:17:27 +01:00
|
|
|
m_download_queue.erase(b);
|
2008-03-14 18:43:38 +01:00
|
|
|
|
2011-03-17 05:56:02 +01:00
|
|
|
if (write_queue_size / 16 / 1024 > m_ses.m_settings.cache_size / 2
|
|
|
|
&& m_ses.m_settings.cache_size > 5
|
2011-03-16 08:45:51 +01:00
|
|
|
&& (now - m_ses.m_last_disk_queue_performance_warning) > seconds(10)
|
|
|
|
&& m_ses.m_alerts.should_post<performance_alert>())
|
|
|
|
{
|
|
|
|
m_ses.m_last_disk_queue_performance_warning = now;
|
|
|
|
t->alerts().post_alert(performance_alert(t->get_handle()
|
|
|
|
, performance_alert::too_high_disk_queue_limit));
|
|
|
|
}
|
|
|
|
|
2011-02-13 23:27:02 +01:00
|
|
|
if (!m_ses.can_write_to_disk()
|
2009-06-10 10:30:55 +02:00
|
|
|
&& m_ses.settings().max_queued_disk_bytes
|
2011-01-23 03:09:54 +01:00
|
|
|
&& t->alerts().should_post<performance_alert>()
|
|
|
|
&& (now - m_ses.m_last_disk_performance_warning) > seconds(10))
|
2008-08-19 17:04:14 +02:00
|
|
|
{
|
2011-01-23 03:09:54 +01:00
|
|
|
m_ses.m_last_disk_performance_warning = now;
|
2008-08-19 17:04:14 +02:00
|
|
|
t->alerts().post_alert(performance_alert(t->get_handle()
|
|
|
|
, performance_alert::outstanding_disk_buffer_limit_reached));
|
|
|
|
}
|
|
|
|
|
2008-06-29 11:50:42 +02:00
|
|
|
if (!m_download_queue.empty())
|
2008-07-10 21:31:22 +02:00
|
|
|
{
|
|
|
|
m_timeout_extend = (std::max)(m_timeout_extend
|
|
|
|
- m_ses.settings().request_timeout, 0);
|
|
|
|
m_requested += seconds(m_ses.settings().request_timeout);
|
|
|
|
if (m_requested > now) m_requested = now;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_timeout_extend = 0;
|
|
|
|
}
|
2008-06-29 11:50:42 +02:00
|
|
|
|
2010-04-22 03:53:09 +02:00
|
|
|
bool was_finished = picker.is_piece_finished(p.piece);
|
2008-03-14 18:43:38 +01:00
|
|
|
// did we request this block from any other peers?
|
|
|
|
bool multi = picker.num_peers(block_finished) > 1;
|
2007-07-04 04:16:49 +02:00
|
|
|
picker.mark_as_writing(block_finished, peer_info_struct());
|
2008-03-14 18:43:38 +01:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(picker.num_peers(block_finished) == 0);
|
2008-03-14 18:43:38 +01:00
|
|
|
// if we requested this block from other peers, cancel it now
|
|
|
|
if (multi) t->cancel_block(block_finished);
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(picker.num_peers(block_finished) == 0);
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS \
|
2008-10-10 07:25:55 +02:00
|
|
|
&& defined TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-02 04:18:29 +01:00
|
|
|
t->check_invariant();
|
|
|
|
#endif
|
2009-06-10 10:30:55 +02:00
|
|
|
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2011-02-09 03:56:00 +01:00
|
|
|
piece_picker::downloading_piece pi;
|
|
|
|
picker.piece_info(p.piece, pi);
|
|
|
|
int num_blocks = picker.blocks_in_piece(p.piece);
|
|
|
|
TORRENT_ASSERT(pi.writing + pi.finished + pi.requested <= num_blocks);
|
|
|
|
TORRENT_ASSERT(picker.is_piece_finished(p.piece) == (pi.writing + pi.finished == num_blocks));
|
|
|
|
#endif
|
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
// did we just finish the piece?
|
|
|
|
// this means all blocks are either written
|
|
|
|
// to disk or are in the disk write cache
|
2010-04-22 03:53:09 +02:00
|
|
|
if (picker.is_piece_finished(p.piece) && !was_finished)
|
2009-06-10 10:30:55 +02:00
|
|
|
{
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
check_postcondition post_checker2_(t, false);
|
|
|
|
#endif
|
2010-04-30 21:08:16 +02:00
|
|
|
t->async_verify_piece(p.piece, boost::bind(&torrent::piece_finished, t
|
2009-06-10 10:30:55 +02:00
|
|
|
, p.piece, _1));
|
|
|
|
}
|
|
|
|
|
2012-07-04 18:13:20 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2011-02-07 01:51:20 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_incoming_piece_picks;
|
|
|
|
#endif
|
2008-02-27 21:37:41 +01:00
|
|
|
request_a_block(*t, *this);
|
|
|
|
send_block_requests();
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void peer_connection::on_disk_write_complete(int ret, disk_io_job const& j
|
|
|
|
, peer_request p, boost::shared_ptr<torrent> t)
|
|
|
|
{
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_num_messages[aux::session_impl::on_disk_write_counter];
|
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2011-10-24 06:22:53 +02:00
|
|
|
// flush send buffer at the end of this scope
|
2013-01-21 00:21:53 +01:00
|
|
|
// TODO: 1 peers should really be corked/uncorked outside of
|
2011-10-24 06:22:53 +02:00
|
|
|
// all completed disk operations
|
|
|
|
cork _c(*this);
|
|
|
|
|
2007-10-18 02:32:16 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-08-01 07:22:34 +02:00
|
|
|
m_outstanding_writing_bytes -= p.length;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_outstanding_writing_bytes >= 0);
|
2007-08-01 07:22:34 +02:00
|
|
|
|
2007-10-18 02:32:16 +02:00
|
|
|
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
|
2008-01-07 02:10:46 +01:00
|
|
|
// (*m_ses.m_logger) << time_now_string() << " *** DISK_WRITE_COMPLETE [ p: "
|
|
|
|
// << p.piece << " o: " << p.start << " ]\n";
|
2007-08-03 10:19:10 +02:00
|
|
|
#endif
|
2011-04-28 05:23:14 +02:00
|
|
|
|
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
disconnect(j.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-01 07:22:34 +02:00
|
|
|
// in case the outstanding bytes just dropped down
|
|
|
|
// to allow to receive more data
|
2011-04-21 05:13:53 +02:00
|
|
|
setup_receive(read_async);
|
2007-08-01 07:22:34 +02:00
|
|
|
|
2007-10-18 02:32:16 +02:00
|
|
|
piece_block block_finished(p.piece, p.start / t->block_size());
|
|
|
|
|
2011-04-28 05:23:14 +02:00
|
|
|
if (ret == -1)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2009-06-01 00:41:53 +02:00
|
|
|
// handle_disk_error may disconnect us
|
|
|
|
t->handle_disk_error(j, this);
|
2007-06-10 22:46:09 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-12-21 23:20:28 +01:00
|
|
|
|
2012-04-27 06:36:12 +02:00
|
|
|
if (!t->has_picker()) return;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
piece_picker& picker = t->picker();
|
2004-01-18 11:22:18 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p.piece == j.piece);
|
|
|
|
TORRENT_ASSERT(p.start == j.offset);
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(picker.num_peers(block_finished) == 0);
|
2007-07-04 04:16:49 +02:00
|
|
|
picker.mark_as_finished(block_finished, peer_info_struct());
|
2008-07-06 14:22:56 +02:00
|
|
|
if (t->alerts().should_post<block_finished_alert>())
|
2007-08-01 08:14:16 +02:00
|
|
|
{
|
|
|
|
t->alerts().post_alert(block_finished_alert(t->get_handle(),
|
2008-07-08 11:30:10 +02:00
|
|
|
remote(), pid(), block_finished.block_index, block_finished.piece_index));
|
2007-08-01 08:14:16 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2009-05-31 21:25:26 +02:00
|
|
|
if (t->is_aborted()) return;
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// -----------------------------
|
|
|
|
// ---------- CANCEL -----------
|
|
|
|
// -----------------------------
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_cancel(peer_request const& r)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_cancel(r)) return;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-04-02 22:00:24 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== CANCEL [ piece: %d | s: %d | l: %d ]", r.piece, r.start, r.length);
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2009-05-03 22:21:24 +02:00
|
|
|
std::vector<peer_request>::iterator i
|
2004-01-05 00:51:54 +01:00
|
|
|
= std::find(m_requests.begin(), m_requests.end(), r);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
if (i != m_requests.end())
|
|
|
|
{
|
2012-03-09 07:24:01 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_cancelled_piece_requests;
|
|
|
|
#endif
|
2004-01-05 00:51:54 +01:00
|
|
|
m_requests.erase(i);
|
2008-01-02 04:18:29 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("==> REJECT_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece , r.start , r.length);
|
2008-01-02 04:18:29 +01:00
|
|
|
#endif
|
|
|
|
write_reject_request(r);
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
else
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** GOT CANCEL NOT IN THE QUEUE");
|
2004-01-05 00:51:54 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2003-11-05 00:27:06 +01:00
|
|
|
|
2005-09-15 00:45:22 +02:00
|
|
|
// -----------------------------
|
|
|
|
// --------- DHT PORT ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::incoming_dht_port(int listen_port)
|
2005-09-15 00:45:22 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== DHT_PORT [ p: %d ]", listen_port);
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2006-10-11 16:02:21 +02:00
|
|
|
m_ses.add_dht_node(udp::endpoint(
|
2006-08-01 17:27:08 +02:00
|
|
|
m_remote.address(), listen_port));
|
2005-09-15 00:45:22 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// -----------------------------
|
|
|
|
// --------- HAVE ALL ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void peer_connection::incoming_have_all()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2009-08-08 17:27:07 +02:00
|
|
|
// we cannot disconnect in a constructor, and
|
|
|
|
// this function may end up doing that
|
|
|
|
TORRENT_ASSERT(m_in_constructor == false);
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== HAVE_ALL");
|
2007-08-14 19:47:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_have_all()) return;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
if (m_bitfield_received)
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_lost(m_have_piece, this);
|
2012-05-02 20:03:12 +02:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
m_have_all = true;
|
|
|
|
|
2010-12-24 01:37:01 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("*** THIS IS A SEED [ p: %p ]", m_peer_info);
|
|
|
|
#endif
|
|
|
|
|
2009-06-13 06:14:41 +02:00
|
|
|
t->get_policy().set_seed(m_peer_info, true);
|
2008-07-18 12:03:42 +02:00
|
|
|
m_upload_only = true;
|
|
|
|
m_bitfield_received = true;
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// if we don't have metadata yet
|
|
|
|
// just remember the bitmask
|
|
|
|
// don't update the piecepicker
|
|
|
|
// (since it doesn't exist yet)
|
|
|
|
if (!t->ready_for_connections())
|
|
|
|
{
|
2008-08-12 10:18:05 +02:00
|
|
|
// assume seeds are interesting when we
|
|
|
|
// don't even have the metadata
|
|
|
|
t->get_policy().peer_is_interesting(*this);
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
disconnect_if_redundant();
|
2007-08-14 19:47:48 +02:00
|
|
|
// TODO: this might need something more
|
|
|
|
// so that once we have the metadata
|
|
|
|
// we can construct a full bitfield
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_have_piece.empty());
|
2008-05-28 04:35:02 +02:00
|
|
|
m_have_piece.set_all();
|
2007-08-21 10:16:41 +02:00
|
|
|
m_num_pieces = m_have_piece.size();
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_has_all(this);
|
2008-07-18 12:03:42 +02:00
|
|
|
|
|
|
|
// if we're finished, we're not interested
|
2011-04-10 01:57:56 +02:00
|
|
|
if (t->is_upload_only()) send_not_interested();
|
2008-07-18 12:03:42 +02:00
|
|
|
else t->get_policy().peer_is_interesting(*this);
|
|
|
|
|
|
|
|
disconnect_if_redundant();
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// --------- HAVE NONE ---------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void peer_connection::incoming_have_none()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== HAVE_NONE");
|
2007-08-14 19:47:48 +02:00
|
|
|
#endif
|
|
|
|
|
2008-05-12 05:05:27 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_have_none()) return;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2012-05-02 20:03:12 +02:00
|
|
|
|
|
|
|
if (m_bitfield_received)
|
2013-01-06 05:02:29 +01:00
|
|
|
t->peer_lost(m_have_piece, this);
|
2012-05-02 20:03:12 +02:00
|
|
|
|
2009-06-13 06:14:41 +02:00
|
|
|
t->get_policy().set_seed(m_peer_info, false);
|
2008-07-18 12:03:42 +02:00
|
|
|
m_bitfield_received = true;
|
|
|
|
|
2012-05-02 20:03:12 +02:00
|
|
|
m_have_piece.clear_all();
|
|
|
|
m_num_pieces = 0;
|
|
|
|
|
2012-08-10 00:30:45 +02:00
|
|
|
// if the peer is ready to download stuff, it must have metadata
|
|
|
|
m_has_metadata = true;
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
// we're never interested in a peer that doesn't have anything
|
|
|
|
send_not_interested();
|
2008-05-12 05:05:27 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_have_piece.empty() || !t->ready_for_connections());
|
2008-07-18 12:03:42 +02:00
|
|
|
disconnect_if_redundant();
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ------- ALLOWED FAST --------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void peer_connection::incoming_allowed_fast(int index)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== ALLOWED_FAST [ %d ]", index);
|
2007-08-14 19:47:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_allowed_fast(index)) return;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2009-09-01 06:41:50 +02:00
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== INVALID_ALLOWED_FAST [ %d ]", index);
|
2009-09-01 06:41:50 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
if (t->valid_metadata())
|
2007-10-01 03:12:00 +02:00
|
|
|
{
|
2009-09-01 06:41:50 +02:00
|
|
|
if (index >= int(m_have_piece.size()))
|
2008-08-29 19:21:56 +02:00
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<== INVALID_ALLOWED_FAST [ %d | s: %d ]"
|
|
|
|
, index, int(m_have_piece.size()));
|
2007-10-01 03:12:00 +02:00
|
|
|
#endif
|
2008-08-29 19:21:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-10-01 03:12:00 +02:00
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
// if we already have the piece, we can
|
|
|
|
// ignore this message
|
|
|
|
if (t->have_piece(index))
|
|
|
|
return;
|
|
|
|
}
|
2007-12-18 05:40:19 +01:00
|
|
|
|
2009-09-01 06:41:50 +02:00
|
|
|
// if we don't have the metadata, we'll verify
|
|
|
|
// this piece index later
|
2007-08-14 19:47:48 +02:00
|
|
|
m_allowed_fast.push_back(index);
|
|
|
|
|
|
|
|
// if the peer has the piece and we want
|
|
|
|
// to download it, request it
|
2007-08-16 14:41:46 +02:00
|
|
|
if (int(m_have_piece.size()) > index
|
2007-08-14 19:47:48 +02:00
|
|
|
&& m_have_piece[index]
|
2008-08-29 19:21:56 +02:00
|
|
|
&& t->valid_metadata()
|
2007-08-14 19:47:48 +02:00
|
|
|
&& t->has_picker()
|
|
|
|
&& t->picker().piece_priority(index) > 0)
|
|
|
|
{
|
|
|
|
t->get_policy().peer_is_interesting(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> const& peer_connection::allowed_fast()
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
// TODO: sort the allowed fast set in priority order
|
|
|
|
return m_allowed_fast;
|
|
|
|
}
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
bool peer_connection::can_request_time_critical() const
|
|
|
|
{
|
|
|
|
if (has_peer_choked() || !is_interesting()) return false;
|
2009-11-02 02:01:07 +01:00
|
|
|
if ((int)m_download_queue.size() + (int)m_request_queue.size()
|
|
|
|
> m_desired_queue_size * 2) return false;
|
2009-03-17 10:34:44 +01:00
|
|
|
if (on_parole()) return false;
|
2011-08-05 19:35:09 +02:00
|
|
|
if (m_disconnecting) return false;
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
if (t->upload_mode()) return false;
|
2013-07-27 19:43:34 +02:00
|
|
|
|
|
|
|
// ignore snubbed peers, since they're not likely to return pieces in a timely
|
|
|
|
// manner anyway
|
|
|
|
if (m_snubbed) return false;
|
2009-03-17 10:34:44 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::make_time_critical(piece_block const& block)
|
|
|
|
{
|
2009-12-25 17:52:57 +01:00
|
|
|
std::vector<pending_block>::iterator rit = std::find_if(m_request_queue.begin()
|
|
|
|
, m_request_queue.end(), has_block(block));
|
2009-03-17 10:34:44 +01:00
|
|
|
if (rit == m_request_queue.end()) return;
|
2012-05-13 06:37:31 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
TORRENT_ASSERT(t->has_picker());
|
|
|
|
TORRENT_ASSERT(t->picker().is_requested(block));
|
|
|
|
#endif
|
2009-03-17 10:34:44 +01:00
|
|
|
// ignore it if it's already time critical
|
|
|
|
if (rit - m_request_queue.begin() < m_queued_time_critical) return;
|
2009-12-25 17:52:57 +01:00
|
|
|
pending_block b = *rit;
|
2009-03-17 10:34:44 +01:00
|
|
|
m_request_queue.erase(rit);
|
|
|
|
m_request_queue.insert(m_request_queue.begin() + m_queued_time_critical, b);
|
|
|
|
++m_queued_time_critical;
|
|
|
|
}
|
|
|
|
|
2009-12-25 17:52:57 +01:00
|
|
|
bool peer_connection::add_request(piece_block const& block, int flags)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2009-11-01 01:47:22 +01:00
|
|
|
INVARIANT_CHECK;
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2003-12-01 22:27:27 +01:00
|
|
|
|
2010-10-02 23:01:11 +02:00
|
|
|
TORRENT_ASSERT(!m_disconnecting);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
2012-05-03 03:51:56 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(int(block.piece_index) < t->torrent_file().num_pieces());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < t->torrent_file().piece_size(block.piece_index));
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!t->picker().is_requested(block) || (t->picker().num_peers(block) > 0));
|
|
|
|
TORRENT_ASSERT(!t->have_piece(block.piece_index));
|
2008-07-07 14:04:06 +02:00
|
|
|
TORRENT_ASSERT(std::find_if(m_download_queue.begin(), m_download_queue.end()
|
|
|
|
, has_block(block)) == m_download_queue.end());
|
|
|
|
TORRENT_ASSERT(std::find(m_request_queue.begin(), m_request_queue.end()
|
|
|
|
, block) == m_request_queue.end());
|
2004-01-05 00:51:54 +01:00
|
|
|
|
2009-06-19 00:32:55 +02:00
|
|
|
if (t->upload_mode()) return false;
|
2010-10-02 23:01:11 +02:00
|
|
|
if (m_disconnecting) return false;
|
2009-06-19 00:32:55 +02:00
|
|
|
|
2007-04-27 02:27:37 +02:00
|
|
|
piece_picker::piece_state_t state;
|
|
|
|
peer_speed_t speed = peer_speed();
|
2007-09-06 20:33:15 +02:00
|
|
|
char const* speedmsg = 0;
|
2007-08-01 08:14:16 +02:00
|
|
|
if (speed == fast)
|
|
|
|
{
|
|
|
|
speedmsg = "fast";
|
|
|
|
state = piece_picker::fast;
|
|
|
|
}
|
|
|
|
else if (speed == medium)
|
|
|
|
{
|
|
|
|
speedmsg = "medium";
|
|
|
|
state = piece_picker::medium;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
speedmsg = "slow";
|
|
|
|
state = piece_picker::slow;
|
|
|
|
}
|
2007-04-27 02:27:37 +02:00
|
|
|
|
2009-12-25 17:52:57 +01:00
|
|
|
if (flags & req_busy)
|
|
|
|
{
|
|
|
|
// this block is busy (i.e. it has been requested
|
|
|
|
// from another peer already). Only allow one busy
|
|
|
|
// request in the pipeline at the time
|
|
|
|
for (std::vector<pending_block>::const_iterator i = m_download_queue.begin()
|
|
|
|
, end(m_download_queue.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->busy) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<pending_block>::const_iterator i = m_request_queue.begin()
|
|
|
|
, end(m_request_queue.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->busy) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-15 22:20:07 +02:00
|
|
|
if (!t->picker().mark_as_downloading(block, peer_info_struct(), state))
|
2009-06-19 00:32:55 +02:00
|
|
|
return false;
|
2007-09-15 22:20:07 +02:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (t->alerts().should_post<block_downloading_alert>())
|
2007-08-01 08:14:16 +02:00
|
|
|
{
|
|
|
|
t->alerts().post_alert(block_downloading_alert(t->get_handle(),
|
2008-07-08 11:30:10 +02:00
|
|
|
remote(), pid(), speedmsg, block.block_index, block.piece_index));
|
2007-08-01 08:14:16 +02:00
|
|
|
}
|
2007-04-27 02:27:37 +02:00
|
|
|
|
2009-12-25 17:52:57 +01:00
|
|
|
pending_block pb(block);
|
2011-02-21 06:24:41 +01:00
|
|
|
pb.busy = (flags & req_busy) ? true : false;
|
2009-12-25 17:52:57 +01:00
|
|
|
if (flags & req_time_critical)
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
|
|
|
m_request_queue.insert(m_request_queue.begin() + m_queued_time_critical
|
2009-12-25 17:52:57 +01:00
|
|
|
, pb);
|
2009-03-17 10:34:44 +01:00
|
|
|
++m_queued_time_critical;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-25 17:52:57 +01:00
|
|
|
m_request_queue.push_back(pb);
|
2009-03-17 10:34:44 +01:00
|
|
|
}
|
2009-06-19 00:32:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::cancel_all_requests()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
// this peer might be disconnecting
|
|
|
|
if (!t) return;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** CANCEL ALL REQUESTS");
|
2009-06-19 00:32:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
while (!m_request_queue.empty())
|
|
|
|
{
|
2010-10-04 00:06:53 +02:00
|
|
|
t->picker().abort_download(m_request_queue.back().block, peer_info_struct());
|
2009-06-19 00:32:55 +02:00
|
|
|
m_request_queue.pop_back();
|
|
|
|
}
|
2009-11-01 01:47:22 +01:00
|
|
|
m_queued_time_critical = 0;
|
2009-06-19 00:32:55 +02:00
|
|
|
|
2009-07-18 18:34:19 +02:00
|
|
|
// make a local temporary copy of the download queue, since it
|
|
|
|
// may be modified when we call write_cancel (for peers that don't
|
|
|
|
// support the FAST extensions).
|
|
|
|
std::vector<pending_block> temp_copy = m_download_queue;
|
|
|
|
|
|
|
|
for (std::vector<pending_block>::iterator i = temp_copy.begin()
|
|
|
|
, end(temp_copy.end()); i != end; ++i)
|
2009-06-19 00:32:55 +02:00
|
|
|
{
|
|
|
|
piece_block b = i->block;
|
|
|
|
|
|
|
|
int block_offset = b.block_index * t->block_size();
|
|
|
|
int block_size
|
|
|
|
= (std::min)(t->torrent_file().piece_size(b.piece_index)-block_offset,
|
|
|
|
t->block_size());
|
|
|
|
TORRENT_ASSERT(block_size > 0);
|
|
|
|
TORRENT_ASSERT(block_size <= t->block_size());
|
|
|
|
|
2009-06-28 23:09:07 +02:00
|
|
|
// we can't cancel the piece if we've started receiving it
|
2009-07-01 06:14:43 +02:00
|
|
|
if (m_receiving_block == b) continue;
|
2009-06-28 23:09:07 +02:00
|
|
|
|
2009-06-19 00:32:55 +02:00
|
|
|
peer_request r;
|
|
|
|
r.piece = b.piece_index;
|
|
|
|
r.start = block_offset;
|
|
|
|
r.length = block_size;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("==> CANCEL [ piece: %d s: %d l: %d b: %d ]"
|
|
|
|
, b.piece_index, block_offset, block_size, b.block_index);
|
2009-06-19 00:32:55 +02:00
|
|
|
#endif
|
|
|
|
write_cancel(r);
|
|
|
|
}
|
2004-01-05 00:51:54 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2012-06-06 18:05:25 +02:00
|
|
|
void peer_connection::cancel_request(piece_block const& block, bool force)
|
2004-01-05 00:51:54 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2008-01-16 22:07:04 +01:00
|
|
|
// this peer might be disconnecting
|
|
|
|
if (!t) return;
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(int(block.piece_index) < t->torrent_file().num_pieces());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < t->torrent_file().piece_size(block.piece_index));
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
// if all the peers that requested this block has been
|
|
|
|
// cancelled, then just ignore the cancel.
|
|
|
|
if (!t->picker().is_requested(block)) return;
|
2004-01-07 01:48:02 +01:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
std::vector<pending_block>::iterator it
|
2008-07-07 14:04:06 +02:00
|
|
|
= std::find_if(m_download_queue.begin(), m_download_queue.end(), has_block(block));
|
2006-04-25 23:04:48 +02:00
|
|
|
if (it == m_download_queue.end())
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2009-12-25 17:52:57 +01:00
|
|
|
std::vector<pending_block>::iterator rit = std::find_if(m_request_queue.begin()
|
|
|
|
, m_request_queue.end(), has_block(block));
|
2008-07-07 14:04:06 +02:00
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
// when a multi block is received, it is cancelled
|
|
|
|
// from all peers, so if this one hasn't requested
|
|
|
|
// the block, just ignore to cancel it.
|
2008-07-07 14:04:06 +02:00
|
|
|
if (rit == m_request_queue.end()) return;
|
2007-07-06 19:15:35 +02:00
|
|
|
|
2012-05-12 09:14:07 +02:00
|
|
|
if (rit - m_request_queue.begin() < m_queued_time_critical)
|
|
|
|
--m_queued_time_critical;
|
|
|
|
|
2010-10-04 00:06:53 +02:00
|
|
|
t->picker().abort_download(block, peer_info_struct());
|
2008-07-07 14:04:06 +02:00
|
|
|
m_request_queue.erase(rit);
|
2006-04-25 23:04:48 +02:00
|
|
|
// since we found it in the request queue, it means it hasn't been
|
|
|
|
// sent yet, so we don't have to send a cancel.
|
|
|
|
return;
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
int block_offset = block.block_index * t->block_size();
|
|
|
|
int block_size
|
2008-04-05 23:18:27 +02:00
|
|
|
= (std::min)(t->torrent_file().piece_size(block.piece_index)-block_offset,
|
2006-04-25 23:04:48 +02:00
|
|
|
t->block_size());
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(block_size > 0);
|
|
|
|
TORRENT_ASSERT(block_size <= t->block_size());
|
2004-01-08 14:03:38 +01:00
|
|
|
|
2012-06-06 18:05:25 +02:00
|
|
|
if (force) t->picker().abort_download(block, peer_info_struct());
|
|
|
|
|
2009-06-29 00:58:21 +02:00
|
|
|
if (m_outstanding_bytes < block_size) return;
|
2009-06-28 23:50:18 +02:00
|
|
|
|
2004-01-08 14:03:38 +01:00
|
|
|
peer_request r;
|
|
|
|
r.piece = block.piece_index;
|
|
|
|
r.start = block_offset;
|
|
|
|
r.length = block_size;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("==> CANCEL [ piece: %d s: %d l: %d b: %d ]"
|
|
|
|
, block.piece_index, block_offset, block_size, block.block_index);
|
2005-03-19 13:22:40 +01:00
|
|
|
#endif
|
2008-01-02 04:18:29 +01:00
|
|
|
write_cancel(r);
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
|
|
|
|
2009-07-23 06:38:52 +02:00
|
|
|
bool peer_connection::send_choke()
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2010-11-23 21:36:27 +01:00
|
|
|
if (m_peer_info && m_peer_info->optimistically_unchoked)
|
|
|
|
m_peer_info->optimistically_unchoked = false;
|
2007-08-21 19:45:28 +02:00
|
|
|
|
2009-07-23 06:38:52 +02:00
|
|
|
if (m_choked) return false;
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> CHOKE");
|
2005-08-19 01:55:32 +02:00
|
|
|
#endif
|
2010-12-19 20:40:32 +01:00
|
|
|
write_choke();
|
|
|
|
m_choked = true;
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_choke = time_now();
|
2004-01-12 21:31:27 +01:00
|
|
|
m_num_invalid_requests = 0;
|
2007-10-06 00:45:24 +02:00
|
|
|
|
|
|
|
// reject the requests we have in the queue
|
2008-07-09 19:45:37 +02:00
|
|
|
// except the allowed fast pieces
|
2009-05-03 22:21:24 +02:00
|
|
|
for (std::vector<peer_request>::iterator i = m_requests.begin();
|
2008-07-09 19:45:37 +02:00
|
|
|
i != m_requests.end();)
|
2008-01-02 04:18:29 +01:00
|
|
|
{
|
2009-05-04 02:08:00 +02:00
|
|
|
if (std::find(m_accept_fast.begin(), m_accept_fast.end(), i->piece)
|
|
|
|
!= m_accept_fast.end())
|
2008-07-09 19:45:37 +02:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2008-01-02 04:18:29 +01:00
|
|
|
peer_request const& r = *i;
|
2012-03-09 07:24:01 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_choked_piece_requests;
|
|
|
|
#endif
|
2008-07-09 19:45:37 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("==> REJECT_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece , r.start , r.length);
|
2008-01-02 04:18:29 +01:00
|
|
|
#endif
|
2010-12-19 20:40:32 +01:00
|
|
|
write_reject_request(r);
|
2008-07-09 19:45:37 +02:00
|
|
|
i = m_requests.erase(i);
|
|
|
|
}
|
2009-07-23 06:38:52 +02:00
|
|
|
return true;
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
bool peer_connection::send_unchoke()
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
if (!m_choked) return false;
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
if (!t->ready_for_connections()) return false;
|
2010-01-15 17:45:42 +01:00
|
|
|
|
|
|
|
if (!m_sent_suggests)
|
|
|
|
{
|
|
|
|
std::vector<int> ret;
|
|
|
|
t->get_suggested_pieces(ret);
|
|
|
|
for (std::vector<int>::iterator i = ret.begin()
|
|
|
|
, end(ret.end()); i != end; ++i)
|
2011-03-15 02:58:48 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(*i >= 0);
|
2010-01-15 17:45:42 +01:00
|
|
|
send_suggest(*i);
|
2011-03-15 02:58:48 +01:00
|
|
|
}
|
2010-01-15 17:45:42 +01:00
|
|
|
|
|
|
|
m_sent_suggests = true;
|
|
|
|
}
|
|
|
|
|
2007-09-01 09:38:10 +02:00
|
|
|
m_last_unchoke = time_now();
|
2006-04-25 23:04:48 +02:00
|
|
|
write_unchoke();
|
2004-01-04 05:29:13 +01:00
|
|
|
m_choked = false;
|
2005-03-19 13:22:40 +01:00
|
|
|
|
2013-06-02 00:33:11 +02:00
|
|
|
m_uploaded_at_last_unchoke = m_statistics.total_payload_upload();
|
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> UNCHOKE");
|
2005-03-19 13:22:40 +01:00
|
|
|
#endif
|
2008-08-29 19:21:56 +02:00
|
|
|
return true;
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
void peer_connection::send_interested()
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
|
|
|
if (m_interesting) return;
|
2008-08-17 00:07:15 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2008-08-29 19:21:56 +02:00
|
|
|
if (!t->ready_for_connections()) return;
|
|
|
|
m_interesting = true;
|
2008-07-18 12:03:42 +02:00
|
|
|
write_interested();
|
2005-03-19 13:22:40 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> INTERESTED");
|
2005-03-19 13:22:40 +01:00
|
|
|
#endif
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
void peer_connection::send_not_interested()
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
2009-08-08 17:27:07 +02:00
|
|
|
// we cannot disconnect in a constructor, and
|
|
|
|
// this function may end up doing that
|
|
|
|
TORRENT_ASSERT(m_in_constructor == false);
|
|
|
|
|
2009-08-02 00:48:43 +02:00
|
|
|
if (!m_interesting)
|
|
|
|
{
|
|
|
|
disconnect_if_redundant();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-17 00:07:15 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2008-08-29 19:21:56 +02:00
|
|
|
if (!t->ready_for_connections()) return;
|
|
|
|
m_interesting = false;
|
2008-07-18 12:03:42 +02:00
|
|
|
write_not_interested();
|
2004-02-01 14:48:30 +01:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
m_became_uninteresting = time_now();
|
2004-02-01 14:48:30 +01:00
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> NOT_INTERESTED");
|
2005-03-19 13:22:40 +01:00
|
|
|
#endif
|
2008-07-18 12:03:42 +02:00
|
|
|
disconnect_if_redundant();
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:45:42 +01:00
|
|
|
void peer_connection::send_suggest(int piece)
|
|
|
|
{
|
|
|
|
if (m_connecting) return;
|
|
|
|
if (in_handshake()) return;
|
|
|
|
|
|
|
|
// don't suggest a piece that the peer already has
|
|
|
|
// don't suggest anything to a peer that isn't interested
|
|
|
|
if (has_piece(piece)
|
|
|
|
|| !m_peer_interested)
|
|
|
|
return;
|
|
|
|
|
2010-05-17 00:30:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> SUGGEST [ %d ]", piece);
|
2010-05-17 00:30:48 +02:00
|
|
|
#endif
|
2010-01-15 17:45:42 +01:00
|
|
|
write_suggest(piece);
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::send_block_requests()
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
2007-01-01 12:54:31 +01:00
|
|
|
INVARIANT_CHECK;
|
2006-12-27 16:14:17 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-12-27 16:14:17 +01:00
|
|
|
|
2010-10-02 23:01:11 +02:00
|
|
|
if (m_disconnecting) return;
|
|
|
|
|
2010-10-30 10:36:18 +02:00
|
|
|
if (t->graceful_pause() && m_outstanding_bytes == 0)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** GRACEFUL PAUSE [ NO MORE DOWNLOAD ]");
|
2010-10-30 10:36:18 +02:00
|
|
|
#endif
|
|
|
|
disconnect(errors::torrent_paused);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:43:56 +02:00
|
|
|
// we can't download pieces in these states
|
|
|
|
if (t->state() == torrent_status::checking_files
|
|
|
|
|| t->state() == torrent_status::checking_resume_data
|
|
|
|
|| t->state() == torrent_status::downloading_metadata
|
|
|
|
|| t->state() == torrent_status::allocating)
|
|
|
|
return;
|
|
|
|
|
2009-06-19 00:32:55 +02:00
|
|
|
if ((int)m_download_queue.size() >= m_desired_queue_size
|
|
|
|
|| t->upload_mode()) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-06-29 11:50:42 +02:00
|
|
|
bool empty_download_queue = m_download_queue.empty();
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
while (!m_request_queue.empty()
|
2009-03-17 10:34:44 +01:00
|
|
|
&& ((int)m_download_queue.size() < m_desired_queue_size
|
|
|
|
|| m_queued_time_critical > 0))
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2009-12-25 17:52:57 +01:00
|
|
|
pending_block block = m_request_queue.front();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-05-13 08:29:33 +02:00
|
|
|
m_request_queue.erase(m_request_queue.begin());
|
|
|
|
if (m_queued_time_critical) --m_queued_time_critical;
|
|
|
|
|
|
|
|
// if we're a seed, we don't have a piece picker
|
|
|
|
// so we don't have to worry about invariants getting
|
|
|
|
// out of sync with it
|
2012-04-25 21:16:22 +02:00
|
|
|
if (!t->has_picker()) continue;
|
2010-05-13 08:29:33 +02:00
|
|
|
|
|
|
|
// this can happen if a block times out, is re-requested and
|
|
|
|
// then arrives "unexpectedly"
|
|
|
|
if (t->picker().is_finished(block.block)
|
|
|
|
|| t->picker().is_downloaded(block.block))
|
|
|
|
{
|
2010-10-04 00:06:53 +02:00
|
|
|
t->picker().abort_download(block.block, peer_info_struct());
|
2010-05-13 08:29:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-25 17:52:57 +01:00
|
|
|
int block_offset = block.block.block_index * t->block_size();
|
2008-04-05 23:18:27 +02:00
|
|
|
int block_size = (std::min)(t->torrent_file().piece_size(
|
2009-12-25 17:52:57 +01:00
|
|
|
block.block.piece_index) - block_offset, t->block_size());
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(block_size > 0);
|
|
|
|
TORRENT_ASSERT(block_size <= t->block_size());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
peer_request r;
|
2009-12-25 17:52:57 +01:00
|
|
|
r.piece = block.block.piece_index;
|
2006-04-25 23:04:48 +02:00
|
|
|
r.start = block_offset;
|
|
|
|
r.length = block_size;
|
|
|
|
|
2009-12-25 17:52:57 +01:00
|
|
|
TORRENT_ASSERT(verify_piece(t->to_req(block.block)));
|
2006-11-14 01:08:16 +01:00
|
|
|
m_download_queue.push_back(block);
|
2009-03-17 10:34:44 +01:00
|
|
|
m_outstanding_bytes += block_size;
|
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_DEBUG
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
2010-05-13 08:29:33 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
/*
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-04-10 09:52:58 +02:00
|
|
|
(*m_logger) << time_now_string()
|
2006-11-14 01:08:16 +01:00
|
|
|
<< " *** REQUEST-QUEUE** [ "
|
|
|
|
"piece: " << block.piece_index << " | "
|
|
|
|
"block: " << block.block_index << " ]\n";
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
// if we are requesting large blocks, merge the smaller
|
|
|
|
// blocks that are in the same piece into larger requests
|
|
|
|
if (m_request_large_blocks)
|
|
|
|
{
|
2007-09-03 23:16:24 +02:00
|
|
|
int blocks_per_piece = t->torrent_file().piece_length() / t->block_size();
|
|
|
|
|
|
|
|
while (!m_request_queue.empty())
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
2007-09-03 23:16:24 +02:00
|
|
|
// check to see if this block is connected to the previous one
|
|
|
|
// if it is, merge them, otherwise, break this merge loop
|
2009-12-25 17:52:57 +01:00
|
|
|
pending_block const& front = m_request_queue.front();
|
|
|
|
if (front.block.piece_index * blocks_per_piece + front.block.block_index
|
|
|
|
!= block.block.piece_index * blocks_per_piece + block.block.block_index + 1)
|
2007-09-03 23:16:24 +02:00
|
|
|
break;
|
2006-11-14 01:08:16 +01:00
|
|
|
block = m_request_queue.front();
|
2009-03-17 10:34:44 +01:00
|
|
|
m_request_queue.erase(m_request_queue.begin());
|
2009-12-25 17:52:57 +01:00
|
|
|
TORRENT_ASSERT(verify_piece(t->to_req(block.block)));
|
2006-11-14 01:08:16 +01:00
|
|
|
m_download_queue.push_back(block);
|
2009-03-17 10:34:44 +01:00
|
|
|
if (m_queued_time_critical) --m_queued_time_critical;
|
2007-09-03 23:16:24 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** MERGING REQUEST [ piece: %d block: %d ]"
|
|
|
|
, block.block.piece_index, block.block.block_index);
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2007-09-03 23:16:24 +02:00
|
|
|
|
2009-12-25 17:52:57 +01:00
|
|
|
block_offset = block.block.block_index * t->block_size();
|
2008-04-05 23:18:27 +02:00
|
|
|
block_size = (std::min)(t->torrent_file().piece_size(
|
2009-12-25 17:52:57 +01:00
|
|
|
block.block.piece_index) - block_offset, t->block_size());
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(block_size > 0);
|
|
|
|
TORRENT_ASSERT(block_size <= t->block_size());
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
r.length += block_size;
|
2009-03-17 10:34:44 +01:00
|
|
|
m_outstanding_bytes += block_size;
|
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_DEBUG
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-02 08:08:29 +01:00
|
|
|
// the verification will fail for coalesced blocks
|
|
|
|
TORRENT_ASSERT(verify_piece(r) || m_request_large_blocks);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
bool handled = false;
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
2009-04-04 18:19:19 +02:00
|
|
|
if ((handled = (*i)->write_request(r))) break;
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-05-25 21:42:10 +02:00
|
|
|
if (!handled)
|
2009-04-04 18:19:19 +02:00
|
|
|
#endif
|
2007-05-25 21:42:10 +02:00
|
|
|
{
|
|
|
|
write_request(r);
|
|
|
|
m_last_request = time_now();
|
|
|
|
}
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> REQUEST [ piece: %d | s: %d | l: %d | ds: %d B/s | "
|
|
|
|
"dqs: %d rqs: %d blk: %s ]"
|
|
|
|
, r.piece, r.start, r.length, statistics().download_rate()
|
|
|
|
, int(m_desired_queue_size), int(m_download_queue.size())
|
|
|
|
, m_request_large_blocks?"large":"single");
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
}
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_piece = time_now();
|
2008-06-29 11:50:42 +02:00
|
|
|
|
|
|
|
if (!m_download_queue.empty()
|
|
|
|
&& empty_download_queue)
|
|
|
|
{
|
|
|
|
// This means we just added a request to this connection
|
|
|
|
m_requested = time_now();
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2009-05-23 23:36:09 +02:00
|
|
|
void peer_connection::on_timeout()
|
2007-05-05 02:29:33 +02:00
|
|
|
{
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2009-05-23 23:36:09 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
connect_failed(errors::timed_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::connect_failed(error_code const& e)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(e);
|
|
|
|
|
2010-10-31 23:26:22 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-11-29 02:33:05 +01:00
|
|
|
peer_log("CONNECTION FAILED: %s", print_endpoint(m_remote).c_str());
|
2010-10-31 23:26:22 +01:00
|
|
|
#endif
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2011-09-12 05:51:49 +02:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " CONNECTION FAILED: " << print_endpoint(m_remote) << "\n";
|
2007-05-05 02:29:33 +02:00
|
|
|
#endif
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2011-02-14 05:48:02 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_connect_timeouts;
|
|
|
|
#endif
|
|
|
|
|
2012-01-21 10:05:34 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2012-01-25 08:58:39 +01:00
|
|
|
TORRENT_ASSERT(!m_connecting || t);
|
|
|
|
if (m_connecting && t)
|
2012-01-21 10:05:34 +01:00
|
|
|
{
|
2012-01-23 06:14:12 +01:00
|
|
|
t->dec_num_connecting();
|
2012-01-21 10:05:34 +01:00
|
|
|
m_connecting = false;
|
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
if (m_connection_ticket != -1)
|
|
|
|
{
|
2013-08-19 05:54:45 +02:00
|
|
|
if (m_ses.m_half_open.done(m_connection_ticket))
|
|
|
|
m_connection_ticket = -1;
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// a connection attempt using uTP just failed
|
|
|
|
// mark this peer as not supporting uTP
|
|
|
|
// we'll never try it again (unless we're trying holepunch)
|
2012-10-05 05:20:40 +02:00
|
|
|
if (is_utp(*m_socket)
|
2010-11-29 02:33:05 +01:00
|
|
|
&& m_peer_info
|
|
|
|
&& m_peer_info->supports_utp
|
|
|
|
&& !m_holepunch_mode)
|
|
|
|
{
|
|
|
|
m_peer_info->supports_utp = false;
|
|
|
|
// reconnect immediately using TCP
|
|
|
|
policy::peer* pi = peer_info_struct();
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
fast_reconnect(true);
|
|
|
|
disconnect(e, 0);
|
|
|
|
if (t && pi) t->connect_to_peer(pi, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_holepunch_mode)
|
|
|
|
fast_reconnect(true);
|
|
|
|
|
2011-01-29 13:13:49 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2012-10-05 05:20:40 +02:00
|
|
|
if ((!is_utp(*m_socket)
|
|
|
|
|| !m_ses.m_settings.enable_outgoing_tcp)
|
2010-11-29 02:33:05 +01:00
|
|
|
&& m_peer_info
|
|
|
|
&& m_peer_info->supports_holepunch
|
|
|
|
&& !m_holepunch_mode)
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
// see if we can try a holepunch
|
|
|
|
bt_peer_connection* p = t->find_introducer(remote());
|
|
|
|
if (p)
|
|
|
|
p->write_holepunch_msg(bt_peer_connection::hp_rendezvous, remote(), 0);
|
|
|
|
}
|
2011-01-29 13:13:49 +01:00
|
|
|
#endif
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
disconnect(e, 1);
|
|
|
|
return;
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
|
|
|
|
2008-05-12 08:03:31 +02:00
|
|
|
// the error argument defaults to 0, which means deliberate disconnect
|
|
|
|
// 1 means unexpected disconnect/error
|
|
|
|
// 2 protocol error (client sent something invalid)
|
2009-06-12 18:40:38 +02:00
|
|
|
void peer_connection::disconnect(error_code const& ec, int error)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2011-05-09 01:52:06 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-07-18 12:03:42 +02:00
|
|
|
m_disconnect_started = true;
|
|
|
|
#endif
|
|
|
|
|
2012-03-24 02:29:31 +01:00
|
|
|
if (m_disconnecting) return;
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2008-05-12 08:03:31 +02:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case 0:
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** CONNECTION CLOSED %s", ec.message().c_str());
|
2008-05-12 08:03:31 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** CONNECTION FAILED %s", ec.message().c_str());
|
2008-05-12 08:03:31 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** PEER ERROR %s", ec.message().c_str());
|
2008-05-12 08:03:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-01-07 02:10:46 +01:00
|
|
|
#endif
|
2011-02-03 07:22:22 +01:00
|
|
|
|
2013-08-04 01:31:43 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
(*i)->on_disconnect(ec);
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-06 08:35:56 +01:00
|
|
|
// for incoming connections, we get invalid argument errors
|
|
|
|
// when asking for the remote endpoint and the socket already
|
|
|
|
// closed, which is an edge case, but possible to happen when
|
|
|
|
// a peer makes a TCP and uTP connection in parallel.
|
|
|
|
// for outgoing connections however, why would we get this?
|
|
|
|
TORRENT_ASSERT(ec != error::invalid_argument || !m_outgoing);
|
|
|
|
|
2011-02-03 07:22:22 +01:00
|
|
|
#ifdef TORRENT_STATS
|
2011-02-04 08:14:00 +01:00
|
|
|
++m_ses.m_disconnected_peers;
|
2011-02-03 07:22:22 +01:00
|
|
|
if (error == 2) ++m_ses.m_error_peers;
|
2011-02-04 05:31:20 +01:00
|
|
|
if (ec == error::connection_reset) ++m_ses.m_connreset_peers;
|
2012-03-02 09:52:54 +01:00
|
|
|
else if (ec == error::eof) ++m_ses.m_eof_peers;
|
|
|
|
else if (ec == error::connection_refused) ++m_ses.m_connrefused_peers;
|
|
|
|
else if (ec == error::connection_aborted) ++m_ses.m_connaborted_peers;
|
|
|
|
else if (ec == error::no_permission) ++m_ses.m_perm_peers;
|
|
|
|
else if (ec == error::no_buffer_space) ++m_ses.m_buffer_peers;
|
|
|
|
else if (ec == error::host_unreachable) ++m_ses.m_unreachable_peers;
|
|
|
|
else if (ec == error::broken_pipe) ++m_ses.m_broken_pipe_peers;
|
|
|
|
else if (ec == error::address_in_use) ++m_ses.m_addrinuse_peers;
|
|
|
|
else if (ec == error::access_denied) ++m_ses.m_no_access_peers;
|
|
|
|
else if (ec == error::invalid_argument) ++m_ses.m_invalid_arg_peers;
|
|
|
|
else if (ec == error::operation_aborted) ++m_ses.m_aborted_peers;
|
|
|
|
else if (ec == error_code(errors::upload_upload_connection)
|
2011-02-14 05:48:02 +01:00
|
|
|
|| ec == error_code(errors::uninteresting_upload_peer)
|
|
|
|
|| ec == error_code(errors::torrent_aborted)
|
|
|
|
|| ec == error_code(errors::self_connection)
|
|
|
|
|| ec == error_code(errors::torrent_paused))
|
|
|
|
++m_ses.m_uninteresting_peers;
|
2011-11-24 18:50:57 +01:00
|
|
|
|
2012-03-02 09:52:54 +01:00
|
|
|
if (ec == error_code(errors::timed_out)
|
|
|
|
|| ec == error::timed_out)
|
2011-11-24 18:50:57 +01:00
|
|
|
++m_ses.m_transport_timeout_peers;
|
2011-02-14 06:38:59 +01:00
|
|
|
|
|
|
|
if (ec == error_code(errors::timed_out_inactivity)
|
|
|
|
|| ec == error_code(errors::timed_out_no_request)
|
|
|
|
|| ec == error_code(errors::timed_out_no_interest))
|
|
|
|
++m_ses.m_timeout_peers;
|
2011-02-14 07:37:22 +01:00
|
|
|
|
2011-11-24 18:50:57 +01:00
|
|
|
if (ec == error_code(errors::no_memory))
|
|
|
|
++m_ses.m_no_memory_peers;
|
|
|
|
|
|
|
|
if (ec == error_code(errors::too_many_connections))
|
|
|
|
++m_ses.m_too_many_peers;
|
|
|
|
|
2011-02-14 07:37:22 +01:00
|
|
|
if (ec == error_code(errors::timed_out_no_handshake))
|
|
|
|
++m_ses.m_connect_timeouts;
|
2012-03-02 09:52:54 +01:00
|
|
|
|
2012-10-05 05:20:40 +02:00
|
|
|
if (is_utp(*m_socket)) ++m_ses.m_error_utp_peers;
|
2012-03-02 09:52:54 +01:00
|
|
|
else ++m_ses.m_error_tcp_peers;
|
|
|
|
|
|
|
|
if (m_outgoing) ++m_ses.m_error_outgoing_peers;
|
|
|
|
else ++m_ses.m_error_incoming_peers;
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
if (type() == bittorrent_connection)
|
|
|
|
{
|
|
|
|
bt_peer_connection* bt = static_cast<bt_peer_connection*>(this);
|
|
|
|
if (bt->supports_encryption()) ++m_ses.m_error_encrypted_peers;
|
|
|
|
if (bt->rc4_encrypted() && bt->supports_encryption()) ++m_ses.m_error_rc4_peers;
|
|
|
|
}
|
|
|
|
#endif // TORRENT_DISABLE_ENCRYPTION
|
2011-02-03 07:22:22 +01:00
|
|
|
#endif
|
|
|
|
|
2008-04-09 06:09:40 +02:00
|
|
|
// we cannot do this in a constructor
|
|
|
|
TORRENT_ASSERT(m_in_constructor == false);
|
2008-05-12 08:03:31 +02:00
|
|
|
if (error > 0) m_failed = true;
|
2008-04-09 06:09:40 +02:00
|
|
|
boost::intrusive_ptr<peer_connection> me(this);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2011-10-25 19:34:42 +02:00
|
|
|
if (m_channel_state[upload_channel] & peer_info::bw_disk)
|
2011-02-01 17:55:32 +01:00
|
|
|
{
|
|
|
|
m_ses.dec_disk_queue(upload_channel);
|
2011-10-25 19:34:42 +02:00
|
|
|
m_channel_state[upload_channel] &= ~peer_info::bw_disk;
|
2011-02-01 17:55:32 +01:00
|
|
|
}
|
2011-10-25 19:34:42 +02:00
|
|
|
if (m_channel_state[download_channel] & peer_info::bw_disk)
|
2011-02-01 17:55:32 +01:00
|
|
|
{
|
|
|
|
m_ses.dec_disk_queue(download_channel);
|
2011-10-25 19:34:42 +02:00
|
|
|
m_channel_state[download_channel] &= ~peer_info::bw_disk;
|
2011-02-01 17:55:32 +01:00
|
|
|
}
|
|
|
|
|
2012-01-21 10:05:34 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
if (m_connecting)
|
|
|
|
{
|
2012-01-23 06:14:12 +01:00
|
|
|
t->dec_num_connecting();
|
2012-01-21 10:05:34 +01:00
|
|
|
m_connecting = false;
|
|
|
|
}
|
|
|
|
if (m_connection_ticket >= 0)
|
2008-01-19 20:00:54 +01:00
|
|
|
{
|
2013-08-19 05:54:45 +02:00
|
|
|
if (m_ses.m_half_open.done(m_connection_ticket))
|
|
|
|
m_connection_ticket = -1;
|
2008-01-19 20:00:54 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
torrent_handle handle;
|
|
|
|
if (t) handle = t->get_handle();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2012-03-03 03:29:37 +01:00
|
|
|
if (ec == error::address_in_use
|
|
|
|
&& m_ses.m_settings.outgoing_ports.first != 0)
|
|
|
|
{
|
|
|
|
if (m_ses.m_alerts.should_post<performance_alert>())
|
|
|
|
m_ses.m_alerts.post_alert(performance_alert(
|
|
|
|
handle, performance_alert::too_few_outgoing_ports));
|
|
|
|
}
|
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
if (ec)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2011-02-04 04:49:20 +01:00
|
|
|
if ((error > 1 || ec.category() == socks_category)
|
2010-08-25 08:22:49 +02:00
|
|
|
&& m_ses.m_alerts.should_post<peer_error_alert>())
|
2008-05-12 08:03:31 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
2009-06-12 18:40:38 +02:00
|
|
|
peer_error_alert(handle, remote(), pid(), ec));
|
2008-05-12 08:03:31 +02:00
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
else if (error <= 1 && m_ses.m_alerts.should_post<peer_disconnected_alert>())
|
2008-05-12 08:03:31 +02:00
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(
|
2009-06-12 18:40:38 +02:00
|
|
|
peer_disconnected_alert(handle, remote(), pid(), ec));
|
2008-05-12 08:03:31 +02:00
|
|
|
}
|
2008-01-07 02:10:46 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (t)
|
|
|
|
{
|
2008-06-03 07:21:00 +02:00
|
|
|
// make sure we keep all the stats!
|
2010-10-18 02:10:33 +02:00
|
|
|
if (!m_ignore_stats)
|
|
|
|
{
|
|
|
|
t->add_stats(statistics());
|
|
|
|
|
|
|
|
// report any partially received payload as redundant
|
|
|
|
boost::optional<piece_block_progress> pbp = downloading_piece_progress();
|
|
|
|
if (pbp
|
|
|
|
&& pbp->bytes_downloaded > 0
|
|
|
|
&& pbp->bytes_downloaded < pbp->full_block_bytes)
|
|
|
|
{
|
2011-11-16 03:29:59 +01:00
|
|
|
t->add_redundant_bytes(pbp->bytes_downloaded, torrent::piece_closing);
|
2010-10-18 02:10:33 +02:00
|
|
|
}
|
|
|
|
}
|
2008-06-03 07:21:00 +02:00
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
if (t->has_picker())
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
piece_picker& picker = t->picker();
|
2007-04-12 12:21:55 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
while (!m_download_queue.empty())
|
|
|
|
{
|
2009-03-17 10:34:44 +01:00
|
|
|
pending_block& qe = m_download_queue.back();
|
2010-10-04 00:06:53 +02:00
|
|
|
if (!qe.timed_out && !qe.not_wanted)
|
|
|
|
picker.abort_download(qe.block, peer_info_struct());
|
2010-05-01 18:17:37 +02:00
|
|
|
m_outstanding_bytes -= t->to_req(qe.block).length;
|
|
|
|
if (m_outstanding_bytes < 0) m_outstanding_bytes = 0;
|
2006-04-25 23:04:48 +02:00
|
|
|
m_download_queue.pop_back();
|
|
|
|
}
|
|
|
|
while (!m_request_queue.empty())
|
|
|
|
{
|
2010-10-04 00:06:53 +02:00
|
|
|
picker.abort_download(m_request_queue.back().block, peer_info_struct());
|
2006-04-25 23:04:48 +02:00
|
|
|
m_request_queue.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2010-10-04 05:06:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_download_queue.clear();
|
|
|
|
m_request_queue.clear();
|
2010-10-04 17:49:30 +02:00
|
|
|
m_outstanding_bytes = 0;
|
2010-10-04 05:06:51 +02:00
|
|
|
}
|
2009-11-02 02:01:07 +01:00
|
|
|
m_queued_time_critical = 0;
|
2006-12-11 16:43:27 +01:00
|
|
|
|
2010-05-01 18:17:37 +02:00
|
|
|
#if !defined TORRENT_DISABLE_INVARIANT_CHECKS && defined TORRENT_DEBUG
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
2006-12-11 16:43:27 +01:00
|
|
|
t->remove_peer(this);
|
2006-04-25 23:04:48 +02:00
|
|
|
m_torrent.reset();
|
|
|
|
}
|
2010-10-03 03:22:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_download_queue.empty());
|
|
|
|
TORRENT_ASSERT(m_request_queue.empty());
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#if defined TORRENT_DEBUG && defined TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
// since this connection doesn't have a torrent reference
|
|
|
|
// no torrent should have a reference to this connection either
|
|
|
|
for (aux::session_impl::torrent_map::const_iterator i = m_ses.m_torrents.begin()
|
|
|
|
, end(m_ses.m_torrents.end()); i != end; ++i)
|
|
|
|
TORRENT_ASSERT(!i->second->has_peer(this));
|
|
|
|
#endif
|
|
|
|
|
2008-01-19 20:00:54 +01:00
|
|
|
m_disconnecting = true;
|
2009-06-12 18:40:38 +02:00
|
|
|
error_code e;
|
2012-01-14 17:04:25 +01:00
|
|
|
|
2012-04-05 06:17:19 +02:00
|
|
|
async_shutdown(*m_socket, m_socket);
|
2012-01-14 17:04:25 +01:00
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
m_ses.close_connection(this, ec);
|
2009-01-25 03:45:31 +01:00
|
|
|
|
|
|
|
// we should only disconnect while we still have
|
|
|
|
// at least one reference left to the connection
|
|
|
|
TORRENT_ASSERT(refcount() > 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 18:09:22 +02:00
|
|
|
int peer_connection::get_upload_limit() const
|
|
|
|
{
|
|
|
|
return m_upload_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
int peer_connection::get_download_limit() const
|
|
|
|
{
|
|
|
|
return m_download_limit;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::set_upload_limit(int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2009-04-26 02:21:59 +02:00
|
|
|
if (limit < 0) limit = 0;
|
|
|
|
if (limit < 10 && limit > 0) limit = 10;
|
2007-04-03 01:10:11 +02:00
|
|
|
m_upload_limit = limit;
|
2009-04-26 02:21:59 +02:00
|
|
|
m_bandwidth_channel[upload_channel].throttle(m_upload_limit);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection::set_download_limit(int limit)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(limit >= -1);
|
2009-04-26 02:21:59 +02:00
|
|
|
if (limit < 0) limit = 0;
|
|
|
|
if (limit < 10 && limit > 0) limit = 10;
|
2007-04-03 01:10:11 +02:00
|
|
|
m_download_limit = limit;
|
2009-04-26 02:21:59 +02:00
|
|
|
m_bandwidth_channel[download_channel].throttle(m_download_limit);
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type peer_connection::share_diff() const
|
2004-01-12 04:05:10 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
float ratio = t->ratio();
|
2004-01-12 04:05:10 +01:00
|
|
|
|
|
|
|
// if we have an infinite ratio, just say we have downloaded
|
|
|
|
// much more than we have uploaded. And we'll keep uploading.
|
2004-11-18 23:33:50 +01:00
|
|
|
if (ratio == 0.f)
|
2007-08-21 06:46:17 +02:00
|
|
|
return (std::numeric_limits<size_type>::max)();
|
2004-01-12 04:05:10 +01:00
|
|
|
|
|
|
|
return m_free_upload
|
2004-01-25 19:18:36 +01:00
|
|
|
+ static_cast<size_type>(m_statistics.total_payload_download() * ratio)
|
2004-01-12 04:05:10 +01:00
|
|
|
- m_statistics.total_payload_upload();
|
|
|
|
}
|
|
|
|
|
2009-03-01 08:20:08 +01:00
|
|
|
bool peer_connection::ignore_unchoke_slots() const
|
|
|
|
{
|
|
|
|
return m_ignore_unchoke_slots
|
2009-11-14 22:25:13 +01:00
|
|
|
|| (m_ses.settings().ignore_limits_on_local_network
|
|
|
|
&& on_local_network()
|
|
|
|
&& m_ses.m_local_upload_channel.throttle() == 0);
|
2009-03-01 08:20:08 +01:00
|
|
|
}
|
|
|
|
|
2007-05-25 19:06:30 +02:00
|
|
|
// defined in upnp.cpp
|
|
|
|
bool is_local(address const& a);
|
|
|
|
|
|
|
|
bool peer_connection::on_local_network() const
|
|
|
|
{
|
2008-01-11 06:40:19 +01:00
|
|
|
if (libtorrent::is_local(m_remote.address())
|
|
|
|
|| is_loopback(m_remote.address())) return true;
|
2007-05-25 19:06:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-05-25 21:42:10 +02:00
|
|
|
void peer_connection::get_peer_info(peer_info& p) const
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!associated_torrent().expired());
|
2007-05-25 21:42:10 +02:00
|
|
|
|
2008-06-29 11:50:42 +02:00
|
|
|
ptime now = time_now();
|
|
|
|
|
2008-04-03 08:11:21 +02:00
|
|
|
p.download_rate_peak = m_download_rate_peak;
|
|
|
|
p.upload_rate_peak = m_upload_rate_peak;
|
2008-02-09 23:42:56 +01:00
|
|
|
p.rtt = m_rtt;
|
2007-05-25 21:42:10 +02:00
|
|
|
p.down_speed = statistics().download_rate();
|
|
|
|
p.up_speed = statistics().upload_rate();
|
|
|
|
p.payload_down_speed = statistics().download_payload_rate();
|
|
|
|
p.payload_up_speed = statistics().upload_payload_rate();
|
|
|
|
p.pid = pid();
|
|
|
|
p.ip = remote();
|
2007-08-01 08:11:11 +02:00
|
|
|
p.pending_disk_bytes = m_outstanding_writing_bytes;
|
2009-04-26 02:21:59 +02:00
|
|
|
p.send_quota = m_quota[upload_channel];
|
|
|
|
p.receive_quota = m_quota[download_channel];
|
2008-10-24 02:15:39 +02:00
|
|
|
p.num_pieces = m_num_pieces;
|
2008-06-29 11:50:42 +02:00
|
|
|
if (m_download_queue.empty()) p.request_timeout = -1;
|
2008-07-07 14:04:06 +02:00
|
|
|
else p.request_timeout = total_seconds(m_requested - now) + m_ses.settings().request_timeout
|
|
|
|
+ m_timeout_extend;
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
p.inet_as_name = m_inet_as_name;
|
|
|
|
#endif
|
2009-02-17 03:33:45 +01:00
|
|
|
|
|
|
|
p.download_queue_time = download_queue_time();
|
2009-03-17 10:34:44 +01:00
|
|
|
p.queue_bytes = m_outstanding_bytes;
|
2007-05-25 21:42:10 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
|
|
|
p.country[0] = m_country[0];
|
|
|
|
p.country[1] = m_country[1];
|
2013-04-09 02:37:53 +02:00
|
|
|
#else
|
|
|
|
std::fill(p.country, p.country + 2, 0);
|
2007-05-25 21:42:10 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
p.total_download = statistics().total_payload_download();
|
|
|
|
p.total_upload = statistics().total_payload_upload();
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
if (m_bandwidth_channel[upload_channel].throttle() == 0)
|
2007-05-25 21:42:10 +02:00
|
|
|
p.upload_limit = -1;
|
|
|
|
else
|
2009-04-26 02:21:59 +02:00
|
|
|
p.upload_limit = m_bandwidth_channel[upload_channel].throttle();
|
2007-05-25 21:42:10 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
if (m_bandwidth_channel[download_channel].throttle() == 0)
|
2007-05-25 21:42:10 +02:00
|
|
|
p.download_limit = -1;
|
|
|
|
else
|
2009-04-26 02:21:59 +02:00
|
|
|
p.download_limit = m_bandwidth_channel[download_channel].throttle();
|
2007-05-25 21:42:10 +02:00
|
|
|
|
|
|
|
p.load_balancing = total_free_upload();
|
|
|
|
|
2007-09-10 10:07:18 +02:00
|
|
|
p.download_queue_length = int(download_queue().size() + m_request_queue.size());
|
2011-06-06 06:24:12 +02:00
|
|
|
p.requests_in_buffer = int(m_requests_in_buffer.size() + m_request_queue.size());
|
2007-09-10 10:07:18 +02:00
|
|
|
p.target_dl_queue_length = int(desired_queue_size());
|
|
|
|
p.upload_queue_length = int(upload_queue().size());
|
2011-06-06 06:24:12 +02:00
|
|
|
p.timed_out_requests = 0;
|
|
|
|
p.busy_requests = 0;
|
|
|
|
for (std::vector<pending_block>::const_iterator i = m_download_queue.begin()
|
|
|
|
, end(m_download_queue.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->timed_out) ++p.timed_out_requests;
|
|
|
|
if (i->busy) ++p.busy_requests;
|
|
|
|
}
|
2007-05-25 21:42:10 +02:00
|
|
|
|
|
|
|
if (boost::optional<piece_block_progress> ret = downloading_piece_progress())
|
|
|
|
{
|
|
|
|
p.downloading_piece_index = ret->piece_index;
|
|
|
|
p.downloading_block_index = ret->block_index;
|
|
|
|
p.downloading_progress = ret->bytes_downloaded;
|
|
|
|
p.downloading_total = ret->full_block_bytes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.downloading_piece_index = -1;
|
|
|
|
p.downloading_block_index = -1;
|
|
|
|
p.downloading_progress = 0;
|
|
|
|
p.downloading_total = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.pieces = get_bitfield();
|
|
|
|
p.last_request = now - m_last_request;
|
2007-08-17 00:13:35 +02:00
|
|
|
p.last_active = now - (std::max)(m_last_sent, m_last_receive);
|
2007-05-25 21:42:10 +02:00
|
|
|
|
|
|
|
// this will set the flags so that we can update them later
|
|
|
|
p.flags = 0;
|
|
|
|
get_specific_peer_info(p);
|
|
|
|
|
|
|
|
p.flags |= is_seed() ? peer_info::seed : 0;
|
2008-06-29 11:50:42 +02:00
|
|
|
p.flags |= m_snubbed ? peer_info::snubbed : 0;
|
2008-07-19 09:57:43 +02:00
|
|
|
p.flags |= m_upload_only ? peer_info::upload_only : 0;
|
2010-12-18 11:19:34 +01:00
|
|
|
p.flags |= m_endgame_mode ? peer_info::endgame_mode : 0;
|
2010-11-29 02:33:05 +01:00
|
|
|
p.flags |= m_holepunch_mode ? peer_info::holepunched : 0;
|
2007-05-25 21:42:10 +02:00
|
|
|
if (peer_info_struct())
|
|
|
|
{
|
2008-04-05 06:53:22 +02:00
|
|
|
policy::peer* pi = peer_info_struct();
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(pi->in_use);
|
2008-04-05 06:53:22 +02:00
|
|
|
p.source = pi->source;
|
|
|
|
p.failcount = pi->failcount;
|
|
|
|
p.num_hashfails = pi->hashfails;
|
|
|
|
p.flags |= pi->on_parole ? peer_info::on_parole : 0;
|
|
|
|
p.flags |= pi->optimistically_unchoked ? peer_info::optimistic_unchoke : 0;
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
2012-04-12 07:00:20 +02:00
|
|
|
p.inet_as = pi->inet_as ? pi->inet_as->first : 0xffff;
|
2013-11-17 23:38:37 +01:00
|
|
|
#else
|
|
|
|
p.inet_as = 0xffff;
|
2008-04-05 06:53:22 +02:00
|
|
|
#endif
|
2007-05-25 21:42:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.source = 0;
|
|
|
|
p.failcount = 0;
|
|
|
|
p.num_hashfails = 0;
|
2008-04-05 06:53:22 +02:00
|
|
|
p.inet_as = 0xffff;
|
2007-05-25 21:42:10 +02:00
|
|
|
}
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
p.remote_dl_rate = m_remote_dl_rate;
|
2007-09-29 18:14:03 +02:00
|
|
|
p.send_buffer_size = m_send_buffer.capacity();
|
2008-01-10 23:13:23 +01:00
|
|
|
p.used_send_buffer = m_send_buffer.size();
|
2008-04-10 12:03:23 +02:00
|
|
|
p.receive_buffer_size = m_recv_buffer.capacity() + m_disk_recv_buffer_size;
|
|
|
|
p.used_receive_buffer = m_recv_pos;
|
2008-01-14 00:46:43 +01:00
|
|
|
p.write_state = m_channel_state[upload_channel];
|
|
|
|
p.read_state = m_channel_state[download_channel];
|
2008-09-25 22:39:06 +02:00
|
|
|
|
2009-10-03 17:24:59 +02:00
|
|
|
// pieces may be empty if we don't have metadata yet
|
|
|
|
if (p.pieces.size() == 0)
|
|
|
|
{
|
|
|
|
p.progress = 0.f;
|
|
|
|
p.progress_ppm = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-19 06:59:27 +02:00
|
|
|
#if TORRENT_NO_FPU
|
2009-10-03 17:24:59 +02:00
|
|
|
p.progress = 0.f;
|
2009-07-19 06:59:27 +02:00
|
|
|
#else
|
2009-10-03 17:24:59 +02:00
|
|
|
p.progress = (float)p.pieces.count() / (float)p.pieces.size();
|
2009-07-19 06:59:27 +02:00
|
|
|
#endif
|
2012-01-09 15:13:24 +01:00
|
|
|
p.progress_ppm = boost::uint64_t(p.pieces.count()) * 1000000 / p.pieces.size();
|
2009-10-03 17:24:59 +02:00
|
|
|
}
|
2010-02-09 04:04:41 +01:00
|
|
|
|
|
|
|
p.estimated_reciprocation_rate = m_est_reciprocation_rate;
|
2010-07-17 09:13:14 +02:00
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
p.local_endpoint = get_socket()->local_endpoint(ec);
|
2007-05-25 21:42:10 +02:00
|
|
|
}
|
2007-05-25 19:06:30 +02:00
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
// allocates a disk buffer of size 'disk_buffer_size' and replaces the
|
|
|
|
// end of the current receive buffer with it. i.e. the receive pos
|
|
|
|
// must be <= packet_size - disk_buffer_size
|
|
|
|
// the disk buffer can be accessed through release_disk_receive_buffer()
|
|
|
|
// when it is queried, the responsibility to free it is transferred
|
|
|
|
// to the caller
|
|
|
|
bool peer_connection::allocate_disk_receive_buffer(int disk_buffer_size)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(m_packet_size > 0);
|
|
|
|
TORRENT_ASSERT(m_recv_pos <= m_packet_size - disk_buffer_size);
|
2008-05-05 08:25:22 +02:00
|
|
|
TORRENT_ASSERT(!m_disk_recv_buffer);
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(disk_buffer_size <= 16 * 1024);
|
|
|
|
|
2008-10-06 05:23:47 +02:00
|
|
|
if (disk_buffer_size == 0) return true;
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
if (disk_buffer_size > 16 * 1024)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_piece_size, 2);
|
2008-04-10 12:03:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-11 06:29:34 +01:00
|
|
|
// first free the old buffer
|
|
|
|
m_disk_recv_buffer.reset();
|
|
|
|
// then allocate a new one
|
|
|
|
|
2009-01-23 10:13:31 +01:00
|
|
|
m_disk_recv_buffer.reset(m_ses.allocate_disk_buffer("receive buffer"));
|
2008-05-05 08:25:22 +02:00
|
|
|
if (!m_disk_recv_buffer)
|
2008-04-10 12:03:23 +02:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2008-04-10 12:03:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_disk_recv_buffer_size = disk_buffer_size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* peer_connection::release_disk_receive_buffer()
|
|
|
|
{
|
|
|
|
m_disk_recv_buffer_size = 0;
|
2008-05-05 08:25:22 +02:00
|
|
|
return m_disk_recv_buffer.release();
|
2008-04-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
|
2009-04-21 21:27:52 +02:00
|
|
|
// size = the packet size to remove from the receive buffer
|
|
|
|
// packet_size = the next packet size to receive in the buffer
|
2010-10-27 08:39:18 +02:00
|
|
|
void peer_connection::cut_receive_buffer(int size, int packet_size, int offset)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(packet_size > 0);
|
|
|
|
TORRENT_ASSERT(int(m_recv_buffer.size()) >= size);
|
|
|
|
TORRENT_ASSERT(int(m_recv_buffer.size()) >= m_recv_pos);
|
2010-10-27 08:39:18 +02:00
|
|
|
TORRENT_ASSERT(m_recv_pos >= size + offset);
|
|
|
|
TORRENT_ASSERT(offset >= 0);
|
2013-12-18 00:26:44 +01:00
|
|
|
TORRENT_ASSERT(size >= 0);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2013-11-04 11:06:14 +01:00
|
|
|
if (size > 0)
|
2013-12-16 07:17:05 +01:00
|
|
|
{
|
2013-12-18 00:26:44 +01:00
|
|
|
if (m_recv_pos - size - offset > 0)
|
|
|
|
std::memmove(&m_recv_buffer[0] + offset, &m_recv_buffer[0] + offset + size, m_recv_pos - size - offset);
|
2013-12-16 07:17:05 +01:00
|
|
|
m_recv_pos -= size;
|
2013-12-18 00:26:44 +01:00
|
|
|
|
|
|
|
// defensive. If this actually happens, we would have triggered
|
|
|
|
// an assert already (if asserts are enabled).
|
|
|
|
if (m_recv_pos < 0) m_recv_pos = 0;
|
2013-12-16 07:17:05 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-04-25 23:04:48 +02:00
|
|
|
std::fill(m_recv_buffer.begin() + m_recv_pos, m_recv_buffer.end(), 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_packet_size = packet_size;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:48:46 +02:00
|
|
|
void peer_connection::superseed_piece(int replace_piece, int new_piece)
|
2008-12-08 07:36:22 +01:00
|
|
|
{
|
2012-04-23 07:48:46 +02:00
|
|
|
if (new_piece == -1)
|
2008-12-08 07:36:22 +01:00
|
|
|
{
|
2012-04-23 07:48:46 +02:00
|
|
|
if (m_superseed_piece[0] == -1) return;
|
|
|
|
m_superseed_piece[0] = -1;
|
|
|
|
m_superseed_piece[1] = -1;
|
2008-12-08 07:36:22 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** ending super seed mode");
|
2008-12-08 07:36:22 +01:00
|
|
|
#endif
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
assert(t);
|
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
for (int i = 0; i < int(m_have_piece.size()); ++i)
|
2008-12-08 07:36:22 +01:00
|
|
|
{
|
|
|
|
if (m_have_piece[i] || !t->have_piece(i)) continue;
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("==> HAVE [ piece: %d] (ending super seed)", i);
|
2008-12-08 07:36:22 +01:00
|
|
|
#endif
|
|
|
|
write_have(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:48:46 +02:00
|
|
|
assert(!has_piece(new_piece));
|
2008-12-08 07:36:22 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2012-04-23 07:48:46 +02:00
|
|
|
peer_log("==> HAVE [ piece: %d ] (super seed)", new_piece);
|
2008-12-08 07:36:22 +01:00
|
|
|
#endif
|
2012-04-23 07:48:46 +02:00
|
|
|
write_have(new_piece);
|
|
|
|
|
|
|
|
if (replace_piece >= 0)
|
|
|
|
{
|
|
|
|
// move the piece we're replacing to the tail
|
|
|
|
if (m_superseed_piece[0] == replace_piece)
|
|
|
|
std::swap(m_superseed_piece[0], m_superseed_piece[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_superseed_piece[1] = m_superseed_piece[0];
|
|
|
|
m_superseed_piece[0] = new_piece;
|
2008-12-08 07:36:22 +01:00
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
void peer_connection::update_desired_queue_size()
|
|
|
|
{
|
|
|
|
if (m_snubbed)
|
|
|
|
{
|
|
|
|
m_desired_queue_size = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int download_rate = statistics().download_rate();
|
|
|
|
|
|
|
|
// calculate the desired download queue size
|
|
|
|
const int queue_time = m_ses.settings().request_queue_time;
|
|
|
|
// (if the latency is more than this, the download will stall)
|
|
|
|
// so, the queue size is queue_time * down_rate / 16 kiB
|
|
|
|
// (16 kB is the size of each request)
|
|
|
|
// the minimum number of requests is 2 and the maximum is 48
|
|
|
|
// the block size doesn't have to be 16. So we first query the
|
|
|
|
// torrent for it
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
const int block_size = t->block_size();
|
|
|
|
|
|
|
|
TORRENT_ASSERT(block_size > 0);
|
|
|
|
|
|
|
|
m_desired_queue_size = queue_time * download_rate / block_size;
|
|
|
|
|
|
|
|
if (m_desired_queue_size > m_max_out_request_queue)
|
|
|
|
m_desired_queue_size = m_max_out_request_queue;
|
|
|
|
if (m_desired_queue_size < min_request_queue)
|
|
|
|
m_desired_queue_size = min_request_queue;
|
|
|
|
}
|
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
void peer_connection::second_tick(int tick_interval_ms)
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
2009-05-25 04:45:51 +02:00
|
|
|
ptime now = time_now();
|
2008-05-12 05:05:27 +02:00
|
|
|
boost::intrusive_ptr<peer_connection> me(self());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-06-30 09:58:50 +02:00
|
|
|
// the invariant check must be run before me is destructed
|
|
|
|
// in case the peer got disconnected
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2009-04-26 02:21:59 +02:00
|
|
|
|
|
|
|
// drain the IP overhead from the bandwidth limiters
|
|
|
|
if (m_ses.m_settings.rate_limit_ip_overhead)
|
|
|
|
{
|
|
|
|
int download_overhead = m_statistics.download_ip_overhead();
|
|
|
|
int upload_overhead = m_statistics.upload_ip_overhead();
|
|
|
|
m_bandwidth_channel[download_channel].use_quota(download_overhead);
|
|
|
|
m_bandwidth_channel[upload_channel].use_quota(upload_overhead);
|
|
|
|
|
2009-11-14 22:25:13 +01:00
|
|
|
bandwidth_channel* upc = 0;
|
|
|
|
bandwidth_channel* downc = 0;
|
2009-07-19 11:17:40 +02:00
|
|
|
if (m_ignore_bandwidth_limits)
|
|
|
|
{
|
|
|
|
upc = &m_ses.m_local_upload_channel;
|
|
|
|
downc = &m_ses.m_local_download_channel;
|
|
|
|
}
|
2009-11-14 22:25:13 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
upc = &m_ses.m_upload_channel;
|
|
|
|
downc = &m_ses.m_download_channel;
|
|
|
|
}
|
2009-07-19 11:17:40 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
int up_limit = m_bandwidth_channel[upload_channel].throttle();
|
|
|
|
int down_limit = m_bandwidth_channel[download_channel].throttle();
|
|
|
|
|
|
|
|
if (t)
|
|
|
|
{
|
2009-07-19 11:17:40 +02:00
|
|
|
if (!m_ignore_bandwidth_limits)
|
|
|
|
{
|
|
|
|
t->m_bandwidth_channel[download_channel].use_quota(download_overhead);
|
|
|
|
t->m_bandwidth_channel[upload_channel].use_quota(upload_overhead);
|
|
|
|
}
|
2009-04-26 02:21:59 +02:00
|
|
|
|
|
|
|
if (down_limit > 0
|
|
|
|
&& download_overhead >= down_limit
|
|
|
|
&& t->alerts().should_post<performance_alert>())
|
|
|
|
{
|
|
|
|
t->alerts().post_alert(performance_alert(t->get_handle()
|
|
|
|
, performance_alert::download_limit_too_low));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up_limit > 0
|
|
|
|
&& upload_overhead >= up_limit
|
|
|
|
&& t->alerts().should_post<performance_alert>())
|
|
|
|
{
|
|
|
|
t->alerts().post_alert(performance_alert(t->get_handle()
|
|
|
|
, performance_alert::upload_limit_too_low));
|
|
|
|
}
|
|
|
|
}
|
2009-07-19 11:17:40 +02:00
|
|
|
downc->use_quota(download_overhead);
|
|
|
|
upc->use_quota(upload_overhead);
|
2009-04-26 02:21:59 +02:00
|
|
|
}
|
|
|
|
|
2008-04-09 22:12:52 +02:00
|
|
|
if (!t || m_disconnecting)
|
|
|
|
{
|
2013-08-19 05:54:45 +02:00
|
|
|
if (m_connection_ticket != -1)
|
|
|
|
{
|
|
|
|
if (m_ses.m_half_open.done(m_connection_ticket))
|
|
|
|
m_connection_ticket = -1;
|
|
|
|
}
|
2012-01-21 10:05:34 +01:00
|
|
|
TORRENT_ASSERT(t || !m_connecting);
|
|
|
|
if (m_connecting && t)
|
|
|
|
{
|
2012-01-23 06:14:12 +01:00
|
|
|
t->dec_num_connecting();
|
2012-01-21 10:05:34 +01:00
|
|
|
m_connecting = false;
|
|
|
|
}
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::torrent_aborted);
|
2008-04-09 22:12:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-12-18 11:19:34 +01:00
|
|
|
if (m_endgame_mode
|
|
|
|
&& m_interesting
|
|
|
|
&& m_download_queue.empty()
|
|
|
|
&& m_request_queue.empty()
|
2011-02-09 03:56:00 +01:00
|
|
|
&& total_seconds(now - m_last_request) >= 5)
|
2010-12-18 11:19:34 +01:00
|
|
|
{
|
|
|
|
// this happens when we're in strict end-game
|
|
|
|
// mode and the peer could not request any blocks
|
|
|
|
// because they were all taken but there were still
|
|
|
|
// unrequested blocks. Now, 5 seconds later, there
|
|
|
|
// might not be any unrequested blocks anymore, so
|
|
|
|
// we should try to pick another block to see
|
|
|
|
// if we can pick a busy one
|
2011-02-07 01:51:20 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_end_game_piece_picks;
|
|
|
|
#endif
|
2011-02-09 03:56:00 +01:00
|
|
|
m_last_request = now;
|
2010-12-18 11:19:34 +01:00
|
|
|
request_a_block(*t, *this);
|
|
|
|
if (m_disconnecting) return;
|
2013-04-08 04:55:07 +02:00
|
|
|
send_block_requests();
|
2010-12-18 11:19:34 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
on_tick();
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
(*i)->tick();
|
|
|
|
}
|
2008-06-29 07:35:48 +02:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// if the peer hasn't said a thing for a certain
|
|
|
|
// time, it is considered to have timed out
|
|
|
|
time_duration d;
|
2011-04-27 10:05:37 +02:00
|
|
|
d = (std::min)(now - m_last_receive, now - m_last_sent);
|
2011-01-23 03:09:54 +01:00
|
|
|
|
2010-11-02 07:44:25 +01:00
|
|
|
// if we can't read, it means we're blocked on the rate-limiter
|
|
|
|
// or the disk, not the peer itself. In this case, don't blame
|
|
|
|
// the peer and disconnect it
|
2011-10-25 19:34:42 +02:00
|
|
|
bool may_timeout = (m_channel_state[download_channel] & peer_info::bw_network);
|
2011-01-23 03:09:54 +01:00
|
|
|
|
2013-08-09 09:55:12 +02:00
|
|
|
if (may_timeout && d > seconds(m_timeout) && !m_connecting
|
|
|
|
&& can_disconnect(error_code(errors::timed_out_inactivity, get_libtorrent_category())))
|
2008-06-29 07:35:48 +02:00
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** LAST ACTIVITY [ %d seconds ago ] ***", int(total_seconds(d)));
|
2008-06-29 07:35:48 +02:00
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::timed_out_inactivity);
|
2008-06-29 07:35:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not stall waiting for a handshake
|
2010-11-02 07:44:25 +01:00
|
|
|
if (may_timeout
|
|
|
|
&& !m_connecting
|
2008-06-29 07:35:48 +02:00
|
|
|
&& in_handshake()
|
|
|
|
&& d > seconds(m_ses.settings().handshake_timeout))
|
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("*** NO HANDSHAKE [ waited %d seconds ] ***", int(total_seconds(d)));
|
2008-06-29 07:35:48 +02:00
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::timed_out_no_handshake);
|
2008-06-29 07:35:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disconnect peers that we unchoked, but
|
|
|
|
// they didn't send a request within 20 seconds.
|
|
|
|
// but only if we're a seed
|
|
|
|
d = now - (std::max)(m_last_unchoke, m_last_incoming_request);
|
2010-11-02 07:44:25 +01:00
|
|
|
if (may_timeout
|
|
|
|
&& !m_connecting
|
2008-06-29 07:35:48 +02:00
|
|
|
&& m_requests.empty()
|
2010-02-06 22:40:55 +01:00
|
|
|
&& m_reading_bytes == 0
|
2008-06-29 07:35:48 +02:00
|
|
|
&& !m_choked
|
|
|
|
&& m_peer_interested
|
2011-04-10 01:57:56 +02:00
|
|
|
&& t && t->is_upload_only()
|
2013-08-09 09:55:12 +02:00
|
|
|
&& d > seconds(20)
|
|
|
|
&& can_disconnect(error_code(errors::timed_out_no_request, get_libtorrent_category())))
|
2008-06-29 07:35:48 +02:00
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-11-02 07:44:25 +01:00
|
|
|
peer_log("*** NO REQUEST [ waited %d seconds ] ***", int(total_seconds(d)));
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::timed_out_no_request);
|
2008-06-29 07:35:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the peer hasn't become interested and we haven't
|
|
|
|
// become interested in the peer for 10 minutes, it
|
|
|
|
// has also timed out.
|
|
|
|
time_duration d1;
|
|
|
|
time_duration d2;
|
|
|
|
d1 = now - m_became_uninterested;
|
|
|
|
d2 = now - m_became_uninteresting;
|
|
|
|
time_duration time_limit = seconds(
|
|
|
|
m_ses.settings().inactivity_timeout);
|
|
|
|
|
|
|
|
// don't bother disconnect peers we haven't been interested
|
|
|
|
// in (and that hasn't been interested in us) for a while
|
|
|
|
// unless we have used up all our connection slots
|
2010-11-02 07:44:25 +01:00
|
|
|
if (may_timeout
|
|
|
|
&& !m_interesting
|
2008-06-29 07:35:48 +02:00
|
|
|
&& !m_peer_interested
|
|
|
|
&& d1 > time_limit
|
|
|
|
&& d2 > time_limit
|
2010-10-09 21:09:38 +02:00
|
|
|
&& (m_ses.num_connections() >= m_ses.settings().connections_limit
|
2013-08-09 09:55:12 +02:00
|
|
|
|| (t && t->num_peers() >= t->max_connections()))
|
|
|
|
&& can_disconnect(error_code(errors::timed_out_no_interest, get_libtorrent_category())))
|
2008-06-29 07:35:48 +02:00
|
|
|
{
|
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** MUTUAL NO INTEREST [ t1: %d t2: %d ]"
|
|
|
|
, total_seconds(d1), total_seconds(d2));
|
2008-06-29 07:35:48 +02:00
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::timed_out_no_interest);
|
2008-06-29 07:35:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-02 07:44:25 +01:00
|
|
|
if (may_timeout
|
|
|
|
&& !m_download_queue.empty()
|
2010-06-18 06:43:20 +02:00
|
|
|
&& m_quota[download_channel] > 0
|
2008-07-07 14:04:06 +02:00
|
|
|
&& now > m_requested + seconds(m_ses.settings().request_timeout
|
|
|
|
+ m_timeout_extend))
|
2008-06-29 11:50:42 +02:00
|
|
|
{
|
2008-07-07 14:04:06 +02:00
|
|
|
snub_peer();
|
2008-06-29 11:50:42 +02:00
|
|
|
}
|
2008-07-07 14:04:06 +02:00
|
|
|
|
2008-06-29 07:35:48 +02:00
|
|
|
// if we haven't sent something in too long, send a keep-alive
|
|
|
|
keep_alive();
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2007-05-25 19:06:30 +02:00
|
|
|
m_ignore_bandwidth_limits = m_ses.settings().ignore_limits_on_local_network
|
|
|
|
&& on_local_network();
|
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
m_statistics.second_tick(tick_interval_ms);
|
2007-02-25 06:20:14 +01:00
|
|
|
|
2008-04-03 08:11:21 +02:00
|
|
|
if (m_statistics.upload_payload_rate() > m_upload_rate_peak)
|
|
|
|
{
|
|
|
|
m_upload_rate_peak = m_statistics.upload_payload_rate();
|
|
|
|
}
|
|
|
|
if (m_statistics.download_payload_rate() > m_download_rate_peak)
|
|
|
|
{
|
|
|
|
m_download_rate_peak = m_statistics.download_payload_rate();
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
if (peer_info_struct())
|
|
|
|
{
|
|
|
|
std::pair<const int, int>* as_stats = peer_info_struct()->inet_as;
|
|
|
|
if (as_stats && as_stats->second < m_download_rate_peak)
|
|
|
|
as_stats->second = m_download_rate_peak;
|
|
|
|
}
|
|
|
|
#endif
|
2008-04-03 08:11:21 +02:00
|
|
|
}
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2008-04-03 08:11:21 +02:00
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
if (!t->ready_for_connections()) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
update_desired_queue_size();
|
2008-08-19 17:04:14 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
if (m_desired_queue_size == m_max_out_request_queue
|
2008-08-19 17:04:14 +02:00
|
|
|
&& t->alerts().should_post<performance_alert>())
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
|
|
|
t->alerts().post_alert(performance_alert(t->get_handle()
|
|
|
|
, performance_alert::outstanding_request_limit_reached));
|
2008-06-29 11:50:42 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-06-23 07:28:36 +02:00
|
|
|
int piece_timeout = m_ses.settings().piece_timeout;
|
|
|
|
int rate_limit = INT_MAX;
|
|
|
|
if (m_bandwidth_channel[download_channel].throttle() > 0)
|
|
|
|
rate_limit = (std::min)(m_bandwidth_channel[download_channel].throttle(), rate_limit);
|
|
|
|
if (t->bandwidth_throttle(download_channel) > 0)
|
|
|
|
rate_limit = (std::min)(t->bandwidth_throttle(download_channel) / t->num_peers(), rate_limit);
|
|
|
|
if (m_ses.m_download_channel.throttle() > 0)
|
|
|
|
rate_limit = (std::min)(m_ses.m_download_channel.throttle()
|
|
|
|
/ m_ses.num_connections(), rate_limit);
|
|
|
|
|
|
|
|
// rate_limit is an approximation of what this connection is
|
|
|
|
// allowed to download. If it is impossible to beat the piece
|
|
|
|
// timeout at this rate, adjust it to be realistic
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
const int block_size = t->block_size();
|
2009-06-23 07:28:36 +02:00
|
|
|
int rate_limit_timeout = rate_limit / block_size;
|
|
|
|
if (piece_timeout < rate_limit_timeout) piece_timeout = rate_limit_timeout;
|
|
|
|
|
2005-09-21 23:44:38 +02:00
|
|
|
if (!m_download_queue.empty()
|
2010-06-18 06:43:20 +02:00
|
|
|
&& m_quota[download_channel] > 0
|
2009-06-23 07:28:36 +02:00
|
|
|
&& now - m_last_piece > seconds(piece_timeout + m_timeout_extend))
|
2005-09-21 23:44:38 +02:00
|
|
|
{
|
|
|
|
// this peer isn't sending the pieces we've
|
|
|
|
// requested (this has been observed by BitComet)
|
|
|
|
// in this case we'll clear our download queue and
|
|
|
|
// re-request the blocks.
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** PIECE_REQUEST TIMED OUT [ %d time: %d to: %d extend: %d ]"
|
|
|
|
, (int)m_download_queue.size(), total_seconds(now - m_last_piece)
|
|
|
|
, piece_timeout, m_timeout_extend);
|
2005-09-21 23:44:38 +02:00
|
|
|
#endif
|
|
|
|
|
2008-07-07 14:04:06 +02:00
|
|
|
snub_peer();
|
2004-08-08 23:26:40 +02:00
|
|
|
}
|
|
|
|
|
2004-01-04 05:29:13 +01:00
|
|
|
// If the client sends more data
|
|
|
|
// we send it data faster, otherwise, slower.
|
|
|
|
// It will also depend on how much data the
|
|
|
|
// client has sent us. This is the mean to
|
2004-01-14 13:53:17 +01:00
|
|
|
// maintain the share ratio given by m_ratio
|
|
|
|
// with all peers.
|
2004-03-07 21:50:56 +01:00
|
|
|
|
2011-04-10 01:57:56 +02:00
|
|
|
if (t->is_upload_only() || is_choked() || t->ratio() == 0.0f)
|
2004-02-23 23:54:54 +01:00
|
|
|
{
|
|
|
|
// if we have downloaded more than one piece more
|
|
|
|
// than we have uploaded OR if we are a seed
|
|
|
|
// have an unlimited upload rate
|
2009-04-26 02:21:59 +02:00
|
|
|
m_bandwidth_channel[upload_channel].throttle(m_upload_limit);
|
2004-02-23 23:54:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-20 22:03:58 +01:00
|
|
|
size_type bias = 0x10000 + 2 * t->block_size() + m_free_upload;
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
const int break_even_time = 15; // seconds.
|
2004-03-07 21:50:56 +01:00
|
|
|
size_type have_uploaded = m_statistics.total_payload_upload();
|
|
|
|
size_type have_downloaded = m_statistics.total_payload_download();
|
2009-07-19 06:59:27 +02:00
|
|
|
int download_speed = m_statistics.download_rate();
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2004-03-07 21:50:56 +01:00
|
|
|
size_type soon_downloaded =
|
2009-07-19 06:59:27 +02:00
|
|
|
have_downloaded + (size_type)(download_speed * (break_even_time + break_even_time / 2));
|
2004-03-07 21:50:56 +01:00
|
|
|
|
2006-12-04 21:42:47 +01:00
|
|
|
if (t->ratio() != 1.f)
|
2009-07-19 06:59:27 +02:00
|
|
|
soon_downloaded = size_type(soon_downloaded * t->ratio());
|
2004-03-07 21:50:56 +01:00
|
|
|
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT((soon_downloaded - have_uploaded + bias) / break_even_time < INT_MAX);
|
|
|
|
int upload_speed_limit = int((soon_downloaded - have_uploaded
|
|
|
|
+ bias) / break_even_time);
|
2009-04-26 02:21:59 +02:00
|
|
|
|
|
|
|
if (m_upload_limit > 0 && m_upload_limit < upload_speed_limit)
|
|
|
|
upload_speed_limit = m_upload_limit;
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
upload_speed_limit = (std::min)(upload_speed_limit, (std::numeric_limits<int>::max)());
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
m_bandwidth_channel[upload_channel].throttle(
|
2009-07-19 06:59:27 +02:00
|
|
|
(std::min)((std::max)(upload_speed_limit, 10), m_upload_limit));
|
2004-02-23 23:54:54 +01:00
|
|
|
}
|
2006-12-04 21:42:47 +01:00
|
|
|
|
2007-06-14 23:47:00 +02:00
|
|
|
// update once every minute
|
|
|
|
if (now - m_remote_dl_update >= seconds(60))
|
|
|
|
{
|
2009-07-19 06:59:27 +02:00
|
|
|
if (m_remote_dl_rate > 0)
|
|
|
|
m_remote_dl_rate = (m_remote_dl_rate * 2 / 3) +
|
|
|
|
((m_remote_bytes_dled / 3) / 60);
|
|
|
|
else
|
2009-11-14 21:55:42 +01:00
|
|
|
m_remote_dl_rate = m_remote_bytes_dled / 60;
|
2007-06-07 02:05:18 +02:00
|
|
|
|
2007-06-14 23:47:00 +02:00
|
|
|
m_remote_bytes_dled = 0;
|
|
|
|
m_remote_dl_update = now;
|
|
|
|
}
|
2007-06-07 02:05:18 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
fill_send_buffer();
|
2003-12-08 02:37:30 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-07-07 14:04:06 +02:00
|
|
|
void peer_connection::snub_peer()
|
|
|
|
{
|
2008-07-08 10:13:45 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-07-07 14:04:06 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
|
|
|
if (!m_snubbed)
|
|
|
|
{
|
|
|
|
m_snubbed = true;
|
|
|
|
if (m_ses.m_alerts.should_post<peer_snubbed_alert>())
|
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(peer_snubbed_alert(t->get_handle()
|
|
|
|
, m_remote, m_peer_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_desired_queue_size = 1;
|
|
|
|
|
2008-07-11 11:25:03 +02:00
|
|
|
if (on_parole())
|
|
|
|
{
|
|
|
|
m_timeout_extend += m_ses.settings().request_timeout;
|
|
|
|
return;
|
|
|
|
}
|
2008-07-07 14:04:06 +02:00
|
|
|
if (!t->has_picker()) return;
|
|
|
|
piece_picker& picker = t->picker();
|
|
|
|
|
2011-11-16 03:29:59 +01:00
|
|
|
// first, if we have any unsent requests, just
|
|
|
|
// wipe those out
|
|
|
|
while (!m_request_queue.empty())
|
|
|
|
{
|
|
|
|
t->picker().abort_download(m_request_queue.back().block, peer_info_struct());
|
|
|
|
m_request_queue.pop_back();
|
|
|
|
}
|
|
|
|
m_queued_time_critical = 0;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(!m_download_queue.empty());
|
2009-11-01 01:47:22 +01:00
|
|
|
|
|
|
|
// request a new block before removing the previous
|
|
|
|
// one, in order to prevent it from
|
|
|
|
// picking the same block again, stalling the
|
|
|
|
// same piece indefinitely.
|
|
|
|
m_desired_queue_size = 2;
|
2011-02-07 01:51:20 +01:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_snubbed_piece_picks;
|
|
|
|
#endif
|
2009-11-01 01:47:22 +01:00
|
|
|
request_a_block(*t, *this);
|
2011-11-16 03:29:59 +01:00
|
|
|
|
|
|
|
// the block we just picked (potentially)
|
|
|
|
// hasn't been put in m_download_queue yet.
|
|
|
|
// it's in m_request_queue and will be sent
|
|
|
|
// once send_block_requests() is called.
|
|
|
|
|
2009-11-01 01:47:22 +01:00
|
|
|
m_desired_queue_size = 1;
|
|
|
|
|
2011-11-16 03:29:59 +01:00
|
|
|
// time out the last request eligible
|
|
|
|
// block in the queue
|
|
|
|
int i = m_download_queue.size() - 1;
|
|
|
|
for (; i >= 0; --i)
|
2008-07-07 14:04:06 +02:00
|
|
|
{
|
2011-11-16 03:29:59 +01:00
|
|
|
if (!m_download_queue[i].timed_out
|
|
|
|
&& !m_download_queue[i].not_wanted)
|
|
|
|
break;
|
2008-07-07 14:04:06 +02:00
|
|
|
}
|
2011-11-16 03:29:59 +01:00
|
|
|
|
|
|
|
if (i >= 0)
|
2008-07-07 14:04:06 +02:00
|
|
|
{
|
2011-11-16 03:29:59 +01:00
|
|
|
pending_block& qe = m_download_queue[i];
|
|
|
|
piece_block r = qe.block;
|
2008-07-07 14:04:06 +02:00
|
|
|
|
|
|
|
// only time out a request if it blocks the piece
|
|
|
|
// from being completed (i.e. no free blocks to
|
|
|
|
// request from it)
|
|
|
|
piece_picker::downloading_piece p;
|
2009-03-17 10:34:44 +01:00
|
|
|
picker.piece_info(qe.block.piece_index, p);
|
|
|
|
int free_blocks = picker.blocks_in_piece(qe.block.piece_index)
|
2008-07-07 14:04:06 +02:00
|
|
|
- p.finished - p.writing - p.requested;
|
|
|
|
if (free_blocks > 0)
|
|
|
|
{
|
|
|
|
m_timeout_extend += m_ses.settings().request_timeout;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ses.m_alerts.should_post<block_timeout_alert>())
|
|
|
|
{
|
|
|
|
m_ses.m_alerts.post_alert(block_timeout_alert(t->get_handle()
|
2009-03-17 10:34:44 +01:00
|
|
|
, remote(), pid(), qe.block.block_index, qe.block.piece_index));
|
2008-07-07 14:04:06 +02:00
|
|
|
}
|
2009-03-17 10:34:44 +01:00
|
|
|
qe.timed_out = true;
|
2010-10-04 00:06:53 +02:00
|
|
|
picker.abort_download(r, peer_info_struct());
|
2011-11-16 03:29:59 +01:00
|
|
|
}
|
2008-07-08 10:13:45 +02:00
|
|
|
|
|
|
|
send_block_requests();
|
2008-07-07 14:04:06 +02:00
|
|
|
}
|
|
|
|
|
2010-01-31 20:14:00 +01:00
|
|
|
std::pair<int, int> peer_connection::preferred_caching() const
|
|
|
|
{
|
|
|
|
int line_size = 0;
|
|
|
|
int expiry = 0;
|
|
|
|
if (m_ses.m_settings.guided_read_cache)
|
|
|
|
{
|
2010-01-31 21:22:35 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2010-01-31 20:14:00 +01:00
|
|
|
int upload_rate = m_statistics.upload_payload_rate();
|
|
|
|
if (upload_rate == 0) upload_rate = 1;
|
|
|
|
|
|
|
|
int num_uploads = m_ses.num_uploads();
|
|
|
|
if (num_uploads == 0) num_uploads = 1;
|
2010-02-01 01:38:32 +01:00
|
|
|
|
2010-01-31 20:14:00 +01:00
|
|
|
// assume half of the cache is write cache if we're downloading
|
|
|
|
// this torrent as well
|
|
|
|
int cache_size = m_ses.m_settings.cache_size / num_uploads;
|
2011-04-10 01:57:56 +02:00
|
|
|
if (!t->is_upload_only()) cache_size /= 2;
|
2010-01-31 20:14:00 +01:00
|
|
|
// cache_size is the amount of cache we have per peer. The
|
|
|
|
// cache line should not be greater than this
|
|
|
|
|
|
|
|
// try to avoid locking caches for more than a couple of seconds
|
|
|
|
expiry = cache_size * 16 * 1024 / upload_rate;
|
|
|
|
if (expiry < 1) expiry = 1;
|
|
|
|
else if (expiry > 10) expiry = 10;
|
|
|
|
|
|
|
|
line_size = cache_size;
|
|
|
|
}
|
2010-01-31 21:22:35 +01:00
|
|
|
return std::make_pair(line_size, expiry);
|
2010-01-31 20:14:00 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::fill_send_buffer()
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-10-10 09:08:46 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
2008-10-10 09:08:46 +02:00
|
|
|
#endif
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
bool sent_a_piece = false;
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2013-10-13 20:50:07 +02:00
|
|
|
if (!t || t->is_aborted()) return;
|
2005-03-12 13:18:07 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// only add new piece-chunks if the send buffer is small enough
|
|
|
|
// otherwise there will be no end to how large it will be!
|
2007-05-10 08:20:29 +02:00
|
|
|
|
2011-10-18 18:22:36 +02:00
|
|
|
boost::uint64_t upload_rate = int(m_statistics.upload_rate());
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
int buffer_size_watermark = upload_rate
|
2011-09-23 23:12:28 +02:00
|
|
|
* m_ses.settings().send_buffer_watermark_factor / 100;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2012-03-07 03:59:50 +01:00
|
|
|
if (buffer_size_watermark < m_ses.settings().send_buffer_low_watermark)
|
|
|
|
{
|
|
|
|
buffer_size_watermark = m_ses.settings().send_buffer_low_watermark;
|
|
|
|
}
|
2008-01-08 02:16:30 +01:00
|
|
|
else if (buffer_size_watermark > m_ses.settings().send_buffer_watermark)
|
2009-08-17 22:29:09 +02:00
|
|
|
{
|
2008-01-08 02:16:30 +01:00
|
|
|
buffer_size_watermark = m_ses.settings().send_buffer_watermark;
|
2009-08-17 22:29:09 +02:00
|
|
|
}
|
2007-05-10 08:20:29 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
while (!m_requests.empty()
|
2008-01-13 10:33:00 +01:00
|
|
|
&& (send_buffer_size() + m_reading_bytes < buffer_size_watermark))
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2008-08-29 19:21:56 +02:00
|
|
|
TORRENT_ASSERT(t->ready_for_connections());
|
2006-04-25 23:04:48 +02:00
|
|
|
peer_request& r = m_requests.front();
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(r.piece >= 0);
|
|
|
|
TORRENT_ASSERT(r.piece < (int)m_have_piece.size());
|
|
|
|
TORRENT_ASSERT(t->have_piece(r.piece));
|
|
|
|
TORRENT_ASSERT(r.start + r.length <= t->torrent_file().piece_size(r.piece));
|
|
|
|
TORRENT_ASSERT(r.length > 0 && r.start >= 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-01-31 20:14:00 +01:00
|
|
|
std::pair<int, int> cache = preferred_caching();
|
|
|
|
|
2009-02-03 08:46:24 +01:00
|
|
|
if (!t->seed_mode() || t->verified_piece(r.piece))
|
|
|
|
{
|
2010-04-30 21:08:16 +02:00
|
|
|
t->filesystem().async_read(r, boost::bind(&peer_connection::on_disk_read_complete
|
2010-01-31 20:14:00 +01:00
|
|
|
, self(), _1, _2, r), cache.first, cache.second);
|
2009-02-03 08:46:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this means we're in seed mode and we haven't yet
|
|
|
|
// verified this piece (r.piece)
|
2010-04-30 21:08:16 +02:00
|
|
|
t->filesystem().async_read_and_hash(r, boost::bind(&peer_connection::on_disk_read_complete
|
2010-01-31 20:14:00 +01:00
|
|
|
, self(), _1, _2, r), cache.second);
|
2009-02-03 08:46:24 +01:00
|
|
|
t->verified(r.piece);
|
|
|
|
}
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
m_reading_bytes += r.length;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
m_requests.erase(m_requests.begin());
|
2010-09-05 18:01:36 +02:00
|
|
|
sent_a_piece = true;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2010-09-05 18:01:36 +02:00
|
|
|
|
|
|
|
if (t->share_mode() && sent_a_piece)
|
|
|
|
t->recalc_share_mode();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void peer_connection::on_disk_read_complete(int ret, disk_io_job const& j, peer_request r)
|
|
|
|
{
|
2011-10-24 06:22:53 +02:00
|
|
|
// flush send buffer at the end of this scope
|
|
|
|
// TODO: peers should really be corked/uncorked outside of
|
|
|
|
// all completed disk operations
|
|
|
|
cork _c(*this);
|
|
|
|
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_num_messages[aux::session_impl::on_disk_read_counter];
|
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2007-06-10 22:46:09 +02:00
|
|
|
|
|
|
|
m_reading_bytes -= r.length;
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
disk_buffer_holder buffer(m_ses, j.buffer);
|
2009-06-01 00:38:49 +02:00
|
|
|
#if TORRENT_DISK_STATS
|
2009-06-23 10:10:59 +02:00
|
|
|
if (j.buffer) m_ses.m_disk_thread.rename_buffer(j.buffer, "received send buffer");
|
2009-06-01 00:38:49 +02:00
|
|
|
#endif
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2009-02-03 08:46:24 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2011-04-28 05:23:14 +02:00
|
|
|
if (!t)
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
2011-04-28 05:23:14 +02:00
|
|
|
disconnect(j.error);
|
|
|
|
return;
|
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2011-04-28 05:23:14 +02:00
|
|
|
if (ret != r.length)
|
|
|
|
{
|
2009-02-03 08:46:24 +01:00
|
|
|
if (ret == -3)
|
|
|
|
{
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("==> REJECT_PIECE [ piece: %d s: %d l: %d ]"
|
|
|
|
, r.piece , r.start , r.length);
|
|
|
|
#endif
|
2009-02-03 08:46:24 +01:00
|
|
|
write_reject_request(r);
|
2011-04-28 08:36:44 +02:00
|
|
|
if (t->seed_mode()) t->leave_seed_mode(false);
|
2009-02-03 08:46:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-01 00:41:53 +02:00
|
|
|
// handle_disk_error may disconnect us
|
|
|
|
t->handle_disk_error(j, this);
|
2009-02-03 08:46:24 +01:00
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-03 08:46:24 +01:00
|
|
|
if (t)
|
|
|
|
{
|
|
|
|
if (t->seed_mode() && t->all_verified())
|
|
|
|
t->leave_seed_mode(true);
|
|
|
|
}
|
|
|
|
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2012-01-14 17:04:25 +01:00
|
|
|
peer_log("==> PIECE [ piece: %d s: %d l: %d ]"
|
2010-12-19 20:40:32 +01:00
|
|
|
, r.piece, r.start, r.length);
|
2007-06-10 22:46:09 +02:00
|
|
|
#endif
|
|
|
|
|
2009-06-01 00:38:49 +02:00
|
|
|
#if TORRENT_DISK_STATS
|
2009-06-23 10:10:59 +02:00
|
|
|
if (j.buffer) m_ses.m_disk_thread.rename_buffer(j.buffer, "dispatched send buffer");
|
2009-06-01 00:38:49 +02:00
|
|
|
#endif
|
2008-04-10 12:03:23 +02:00
|
|
|
write_piece(r, buffer);
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
void peer_connection::assign_bandwidth(int channel, int amount)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("%s ASSIGN BANDWIDHT [ bytes: %d ]"
|
2010-12-19 20:40:32 +01:00
|
|
|
, channel == upload_channel ? ">>>" : "<<<", amount);
|
2007-01-10 16:02:25 +01:00
|
|
|
#endif
|
|
|
|
|
2013-11-02 04:26:53 +01:00
|
|
|
TORRENT_ASSERT(amount > 0 || is_disconnecting());
|
2009-04-26 02:21:59 +02:00
|
|
|
m_quota[channel] += amount;
|
2011-10-25 19:34:42 +02:00
|
|
|
TORRENT_ASSERT(m_channel_state[channel] & peer_info::bw_limit);
|
|
|
|
m_channel_state[channel] &= ~peer_info::bw_limit;
|
2013-11-02 04:26:53 +01:00
|
|
|
|
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
|
|
|
|
check_invariant();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (is_disconnecting()) return;
|
2007-01-10 16:02:25 +01:00
|
|
|
if (channel == upload_channel)
|
|
|
|
{
|
|
|
|
setup_send();
|
|
|
|
}
|
|
|
|
else if (channel == download_channel)
|
|
|
|
{
|
|
|
|
setup_receive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 05:13:53 +02:00
|
|
|
int peer_connection::request_upload_bandwidth(
|
2009-05-14 19:21:19 +02:00
|
|
|
bandwidth_channel* bwc1
|
|
|
|
, bandwidth_channel* bwc2
|
|
|
|
, bandwidth_channel* bwc3
|
|
|
|
, bandwidth_channel* bwc4)
|
|
|
|
{
|
2013-03-24 19:00:58 +01:00
|
|
|
// we can only have one outstanding bandwidth request at a time
|
|
|
|
if (m_channel_state[upload_channel] & peer_info::bw_limit) return 0;
|
|
|
|
|
2014-01-13 07:49:47 +01:00
|
|
|
int bytes = (std::max)(m_send_buffer.size()
|
|
|
|
, int(boost::int64_t(m_statistics.upload_rate()) * 2
|
|
|
|
* m_ses.m_settings.tick_interval / 1000));
|
2013-03-24 19:00:58 +01:00
|
|
|
|
|
|
|
// we already have quota for the bytes we want to send
|
|
|
|
if (m_quota[upload_channel] >= bytes) return 0;
|
|
|
|
|
|
|
|
// deduct the bytes we already have quota for
|
|
|
|
bytes -= m_quota[upload_channel];
|
|
|
|
|
2009-05-14 19:21:19 +02:00
|
|
|
shared_ptr<torrent> t = m_torrent.lock();
|
2010-02-09 04:04:41 +01:00
|
|
|
int priority;
|
2012-06-08 06:28:37 +02:00
|
|
|
if (t && m_ses.m_settings.choking_algorithm == session_settings::bittyrant_choker
|
2011-04-10 01:57:56 +02:00
|
|
|
&& !t->upload_mode() && !t->is_upload_only())
|
2010-02-09 04:04:41 +01:00
|
|
|
{
|
|
|
|
// when we use the bittyrant choker, the priority of a peer
|
|
|
|
// is decided based on the estimated reciprocation rate and
|
|
|
|
// the share it represents of the total upload rate capacity
|
|
|
|
// the torrent priority is taken into account when unchoking peers
|
2010-10-09 21:09:38 +02:00
|
|
|
int upload_capacity = m_ses.settings().upload_rate_limit;
|
2010-02-09 04:04:41 +01:00
|
|
|
if (upload_capacity == 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 = (std::max)(20000, m_ses.m_peak_up_rate + 10000);
|
|
|
|
}
|
2012-04-28 07:05:39 +02:00
|
|
|
int estimated_reciprocation_rate = m_est_reciprocation_rate;
|
|
|
|
// we cannot send faster than our upload rate anyway
|
|
|
|
if (estimated_reciprocation_rate < upload_capacity)
|
|
|
|
estimated_reciprocation_rate = upload_capacity;
|
|
|
|
|
|
|
|
priority = (boost::uint64_t(estimated_reciprocation_rate) << 14) / upload_capacity;
|
|
|
|
if (priority > 0xffff) priority = 0xffff;
|
2010-02-09 04:04:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priority = 1 + is_interesting() * 2 + m_requests_in_buffer.size();
|
|
|
|
if (priority > 255) priority = 255;
|
2012-06-08 06:28:37 +02:00
|
|
|
priority += t ? t->priority() << 8 : 0;
|
2010-02-09 04:04:41 +01:00
|
|
|
}
|
2012-04-28 07:05:39 +02:00
|
|
|
TORRENT_ASSERT(priority <= 0xffff);
|
2009-09-13 04:24:25 +02:00
|
|
|
|
2009-05-14 19:21:19 +02:00
|
|
|
// peers that we are not interested in are non-prioritized
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log(">>> REQUEST_BANDWIDTH [ upload: %d prio: %d "
|
2010-10-31 23:12:26 +01:00
|
|
|
"channels: %p %p %p %p limits: %d %d %d %d ignore: %d ]"
|
|
|
|
, int(m_send_buffer.size()), priority
|
|
|
|
, bwc1, bwc2, bwc3, bwc4
|
|
|
|
, (bwc1?bwc1->throttle():0)
|
2012-06-08 06:28:37 +02:00
|
|
|
, (bwc2?bwc2->throttle():0)
|
|
|
|
, (bwc3?bwc3->throttle():0)
|
|
|
|
, (bwc4?bwc4->throttle():0)
|
2010-10-31 23:12:26 +01:00
|
|
|
, m_ignore_bandwidth_limits);
|
2009-05-14 19:21:19 +02:00
|
|
|
#endif
|
2013-03-24 19:00:58 +01:00
|
|
|
|
|
|
|
int ret = m_ses.m_upload_rate.request_bandwidth(self()
|
|
|
|
, bytes
|
2010-12-01 05:22:03 +01:00
|
|
|
, priority
|
|
|
|
, bwc1, bwc2, bwc3, bwc4);
|
2013-03-24 19:00:58 +01:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
m_channel_state[upload_channel] |= peer_info::bw_limit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_quota[upload_channel] += ret;
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log(">>> ASSIGN BANDWIDTH [ bytes: %d ]", ret);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return ret;
|
2009-05-14 19:21:19 +02:00
|
|
|
}
|
|
|
|
|
2011-04-21 05:13:53 +02:00
|
|
|
int peer_connection::request_download_bandwidth(
|
2009-05-14 19:21:19 +02:00
|
|
|
bandwidth_channel* bwc1
|
|
|
|
, bandwidth_channel* bwc2
|
|
|
|
, bandwidth_channel* bwc3
|
|
|
|
, bandwidth_channel* bwc4)
|
|
|
|
{
|
2013-11-02 04:26:53 +01:00
|
|
|
INVARIANT_CHECK;
|
2013-03-24 19:00:58 +01:00
|
|
|
// we can only have one outstanding bandwidth request at a time
|
|
|
|
if (m_channel_state[download_channel] & peer_info::bw_limit) return 0;
|
|
|
|
|
|
|
|
int bytes = (std::max)((std::max)(m_outstanding_bytes, m_packet_size - m_recv_pos) + 30
|
2014-01-13 07:49:47 +01:00
|
|
|
, int(boost::int64_t(m_statistics.download_rate()) * 2
|
|
|
|
* m_ses.m_settings.tick_interval / 1000));
|
2013-03-24 19:00:58 +01:00
|
|
|
|
|
|
|
// we already have enough quota
|
|
|
|
if (m_quota[download_channel] >= bytes) return 0;
|
|
|
|
|
|
|
|
// deduct the bytes we already have quota for
|
|
|
|
bytes -= m_quota[download_channel];
|
|
|
|
|
2009-05-14 19:21:19 +02:00
|
|
|
shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("<<< REQUEST_BANDWIDTH [ download: %d prio: %d "
|
2010-10-31 23:12:26 +01:00
|
|
|
"channels: %p %p %p %p limits: %d %d %d %d ignore: %d ]"
|
|
|
|
, int(m_download_queue.size() * 16 * 1024 + 30), m_priority
|
|
|
|
, bwc1, bwc2, bwc3, bwc4
|
|
|
|
, (bwc1?bwc1->throttle():0)
|
2012-06-08 06:28:37 +02:00
|
|
|
, (bwc2?bwc2->throttle():0)
|
|
|
|
, (bwc3?bwc3->throttle():0)
|
|
|
|
, (bwc4?bwc4->throttle():0)
|
2010-10-31 23:12:26 +01:00
|
|
|
, m_ignore_bandwidth_limits);
|
2009-05-14 19:21:19 +02:00
|
|
|
#endif
|
2010-10-31 23:12:26 +01:00
|
|
|
|
2009-09-13 04:24:25 +02:00
|
|
|
TORRENT_ASSERT(m_priority <= 255);
|
2012-06-08 06:28:37 +02:00
|
|
|
int priority = m_priority + (t ? (t->priority() << 8) : 0);
|
2009-09-13 04:24:25 +02:00
|
|
|
|
2009-05-14 19:21:19 +02:00
|
|
|
TORRENT_ASSERT(m_outstanding_bytes >= 0);
|
2011-10-25 19:34:42 +02:00
|
|
|
TORRENT_ASSERT((m_channel_state[download_channel] & peer_info::bw_limit) == 0);
|
2013-03-24 19:00:58 +01:00
|
|
|
|
|
|
|
int ret = m_ses.m_download_rate.request_bandwidth(self()
|
|
|
|
, bytes, priority, bwc1, bwc2, bwc3, bwc4);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
m_channel_state[download_channel] |= peer_info::bw_limit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("<<< ASSIGN BANDWIDTH [ bytes: %d ]", ret);
|
|
|
|
#endif
|
|
|
|
m_quota[download_channel] += ret;
|
|
|
|
}
|
|
|
|
return ret;
|
2009-05-14 19:21:19 +02:00
|
|
|
}
|
|
|
|
|
2011-10-24 06:22:53 +02:00
|
|
|
void peer_connection::uncork_socket()
|
|
|
|
{
|
|
|
|
if (!m_corked) return;
|
|
|
|
m_corked = false;
|
|
|
|
setup_send();
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::setup_send()
|
|
|
|
{
|
2011-10-29 01:34:55 +02:00
|
|
|
if (m_disconnecting) return;
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
shared_ptr<torrent> t = m_torrent.lock();
|
2007-05-25 19:06:30 +02:00
|
|
|
|
2013-03-24 19:00:58 +01:00
|
|
|
// we may want to request more quota at this point
|
|
|
|
int ret = 0;
|
|
|
|
bool utp = m_socket->get<utp_stream>() != 0;
|
|
|
|
bool ignore_limits = m_ignore_bandwidth_limits
|
|
|
|
|| (!m_ses.m_settings.rate_limit_utp && utp);
|
|
|
|
if (!ignore_limits)
|
|
|
|
{
|
|
|
|
// in this case, we have data to send, but no
|
|
|
|
// bandwidth. So, we simply request bandwidth
|
|
|
|
// from the bandwidth manager
|
|
|
|
ret = request_upload_bandwidth(
|
|
|
|
&m_ses.m_upload_channel
|
|
|
|
, t ? &t->m_bandwidth_channel[upload_channel] : 0
|
|
|
|
, &m_bandwidth_channel[upload_channel]
|
|
|
|
, !utp ? &m_ses.m_tcp_upload_channel : 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// in this case, we're a local peer, and the settings
|
|
|
|
// are set to ignore rate limits for local peers. So,
|
|
|
|
// instead we rate limit ourself against the special
|
|
|
|
// global bandwidth channel for local peers, which defaults
|
|
|
|
// to unthrottled
|
|
|
|
ret = request_upload_bandwidth(&m_ses.m_local_upload_channel
|
|
|
|
, &m_bandwidth_channel[upload_channel]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_channel_state[upload_channel] & peer_info::bw_network) return;
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
if (m_quota[upload_channel] == 0
|
2007-09-29 18:14:03 +02:00
|
|
|
&& !m_send_buffer.empty()
|
2012-06-08 06:28:37 +02:00
|
|
|
&& !m_connecting)
|
2009-05-14 19:21:19 +02:00
|
|
|
{
|
2013-03-24 19:00:58 +01:00
|
|
|
return;
|
2007-01-10 16:02:25 +01:00
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2010-04-17 04:21:48 +02:00
|
|
|
int quota_left = m_quota[upload_channel];
|
|
|
|
|
|
|
|
if (m_send_buffer.empty()
|
|
|
|
&& m_reading_bytes > 0
|
|
|
|
&& quota_left > 0)
|
|
|
|
{
|
2011-10-25 19:34:42 +02:00
|
|
|
if ((m_channel_state[upload_channel] & peer_info::bw_disk) == 0)
|
2011-01-30 11:04:15 +01:00
|
|
|
m_ses.inc_disk_queue(upload_channel);
|
2011-10-25 19:34:42 +02:00
|
|
|
m_channel_state[upload_channel] |= peer_info::bw_disk;
|
2010-04-17 04:21:48 +02:00
|
|
|
|
|
|
|
if (!m_connecting
|
|
|
|
&& !m_requests.empty()
|
|
|
|
&& m_reading_bytes > m_ses.settings().send_buffer_watermark - 0x4000)
|
|
|
|
{
|
|
|
|
// we're stalled on the disk. We want to write and we can write
|
|
|
|
// but our send buffer is empty, waiting to be refilled from the disk
|
|
|
|
// this either means the disk is slower than the network connection
|
|
|
|
// or that our send buffer watermark is too small, because we can
|
|
|
|
// send it all before the disk gets back to us. That's why we only
|
|
|
|
// trigger this if we've also filled the allowed send buffer. The
|
|
|
|
// first request would not fill it all the way up because of the
|
|
|
|
// upload rate being virtually 0. If m_requests is empty, it doesn't
|
|
|
|
// matter anyway, because we don't have any more requests from the
|
|
|
|
// peer to hang on to the disk
|
2011-10-29 01:34:55 +02:00
|
|
|
if (m_ses.m_alerts.should_post<performance_alert>())
|
2010-04-17 04:21:48 +02:00
|
|
|
{
|
2011-10-29 01:34:55 +02:00
|
|
|
m_ses.m_alerts.post_alert(performance_alert(t->get_handle()
|
2010-04-17 04:21:48 +02:00
|
|
|
, performance_alert::send_buffer_watermark_too_low));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-09 02:19:30 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_channel_state[upload_channel] & peer_info::bw_disk)
|
|
|
|
m_ses.dec_disk_queue(upload_channel);
|
|
|
|
m_channel_state[upload_channel] &= ~peer_info::bw_disk;
|
|
|
|
}
|
2010-04-17 04:21:48 +02:00
|
|
|
|
2008-02-27 21:37:41 +01:00
|
|
|
if (!can_write())
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2008-11-12 00:45:43 +01:00
|
|
|
if (m_send_buffer.empty())
|
|
|
|
{
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log(">>> SEND BUFFER DEPLETED ["
|
2010-10-31 23:12:26 +01:00
|
|
|
" quota: %d ignore: %s buf: %d connecting: %s disconnecting: %s pending_disk: %d ]"
|
|
|
|
, m_quota[upload_channel], m_ignore_bandwidth_limits?"yes":"no"
|
|
|
|
, int(m_send_buffer.size()), m_connecting?"yes":"no"
|
|
|
|
, m_disconnecting?"yes":"no", m_reading_bytes);
|
2008-11-12 00:45:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log(">>> CANNOT WRITE ["
|
2010-10-31 23:12:26 +01:00
|
|
|
" quota: %d ignore: %s buf: %d connecting: %s disconnecting: %s pending_disk: %d ]"
|
|
|
|
, m_quota[upload_channel], m_ignore_bandwidth_limits?"yes":"no"
|
|
|
|
, int(m_send_buffer.size()), m_connecting?"yes":"no"
|
|
|
|
, m_disconnecting?"yes":"no", m_reading_bytes);
|
2008-11-12 00:45:43 +01:00
|
|
|
}
|
2008-02-27 21:37:41 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// send the actual buffer
|
2010-04-17 04:21:48 +02:00
|
|
|
int amount_to_send = m_send_buffer.size();
|
|
|
|
if (amount_to_send > quota_left)
|
|
|
|
amount_to_send = quota_left;
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2010-04-17 04:21:48 +02:00
|
|
|
TORRENT_ASSERT(amount_to_send > 0);
|
2006-04-30 02:39:18 +02:00
|
|
|
|
2011-10-24 06:22:53 +02:00
|
|
|
if (m_corked)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log(">>> CORKED WRITE [ bytes: %d ]", amount_to_send);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-25 19:34:42 +02:00
|
|
|
TORRENT_ASSERT((m_channel_state[upload_channel] & peer_info::bw_network) == 0);
|
2007-05-25 19:06:30 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log(">>> ASYNC_WRITE [ bytes: %d ]", amount_to_send);
|
2007-05-25 19:06:30 +02:00
|
|
|
#endif
|
2010-04-17 04:21:48 +02:00
|
|
|
std::list<asio::const_buffer> const& vec = m_send_buffer.build_iovec(amount_to_send);
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("peer_connection::on_send_data");
|
|
|
|
#endif
|
2010-04-17 04:21:48 +02:00
|
|
|
m_socket->async_write_some(
|
2010-04-30 21:08:16 +02:00
|
|
|
vec, make_write_handler(boost::bind(
|
2010-04-17 04:21:48 +02:00
|
|
|
&peer_connection::on_send_data, self(), _1, _2)));
|
2010-04-16 21:14:30 +02:00
|
|
|
|
2011-10-25 19:34:42 +02:00
|
|
|
m_channel_state[upload_channel] |= peer_info::bw_network;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
|
2011-01-25 09:03:35 +01:00
|
|
|
void peer_connection::on_disk()
|
|
|
|
{
|
2011-10-25 19:34:42 +02:00
|
|
|
if ((m_channel_state[download_channel] & peer_info::bw_disk) == 0) return;
|
2011-01-25 09:03:35 +01:00
|
|
|
boost::intrusive_ptr<peer_connection> me(this);
|
|
|
|
|
2011-01-30 11:04:15 +01:00
|
|
|
m_ses.dec_disk_queue(download_channel);
|
2011-10-25 19:34:42 +02:00
|
|
|
m_channel_state[download_channel] &= ~peer_info::bw_disk;
|
2011-04-21 05:13:53 +02:00
|
|
|
setup_receive(read_async);
|
2011-01-25 09:03:35 +01:00
|
|
|
}
|
|
|
|
|
2010-08-28 21:44:50 +02:00
|
|
|
void peer_connection::setup_receive(sync_t sync)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2011-10-29 01:34:55 +02:00
|
|
|
if (m_disconnecting) return;
|
2007-01-10 16:02:25 +01:00
|
|
|
|
|
|
|
shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
|
2013-03-24 19:00:58 +01:00
|
|
|
// we may want to request more quota at this point
|
|
|
|
int ret = 0;
|
|
|
|
bool utp = m_socket->get<utp_stream>() != 0;
|
|
|
|
bool ignore_limits = m_ignore_bandwidth_limits
|
|
|
|
|| (!m_ses.m_settings.rate_limit_utp && utp);
|
|
|
|
if (!ignore_limits)
|
|
|
|
{
|
|
|
|
// in this case, we have outstanding data to
|
|
|
|
// receive, but no bandwidth quota. So, we simply
|
|
|
|
// request bandwidth from the bandwidth manager
|
|
|
|
ret = request_download_bandwidth(
|
|
|
|
&m_ses.m_download_channel
|
|
|
|
, t ? &t->m_bandwidth_channel[download_channel] : 0
|
|
|
|
, &m_bandwidth_channel[download_channel]
|
|
|
|
, !utp ? &m_ses.m_tcp_download_channel : 0);
|
|
|
|
}
|
|
|
|
else
|
2007-01-10 16:02:25 +01:00
|
|
|
{
|
2013-03-24 19:00:58 +01:00
|
|
|
// in this case, we're a local peer, and the settings
|
|
|
|
// are set to ignore rate limits for local peers. So,
|
|
|
|
// instead we rate limit ourself against the special
|
|
|
|
// global bandwidth channel for local peers, which defaults
|
|
|
|
// to unthrottled
|
|
|
|
ret = request_download_bandwidth(&m_ses.m_local_download_channel
|
|
|
|
, &m_bandwidth_channel[download_channel]);
|
|
|
|
}
|
2011-04-21 05:13:53 +02:00
|
|
|
|
2013-03-24 19:00:58 +01:00
|
|
|
if (m_channel_state[download_channel] & peer_info::bw_network) return;
|
2011-10-28 23:38:45 +02:00
|
|
|
|
2013-03-24 19:00:58 +01:00
|
|
|
if (m_quota[download_channel] == 0
|
|
|
|
&& !m_connecting)
|
|
|
|
{
|
|
|
|
return;
|
2007-01-10 16:02:25 +01:00
|
|
|
}
|
|
|
|
|
2009-07-21 06:29:29 +02:00
|
|
|
if (!can_read(&m_channel_state[download_channel]))
|
2008-02-27 21:37:41 +01:00
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("<<< CANNOT READ [ quota: %d ignore: %s "
|
2011-05-01 10:58:45 +02:00
|
|
|
"can-write-to-disk: %s queue-limit: %d disconnecting: %s ]"
|
2010-12-19 20:40:32 +01:00
|
|
|
, m_quota[download_channel]
|
|
|
|
, (m_ignore_bandwidth_limits?"yes":"no")
|
2011-02-15 08:39:25 +01:00
|
|
|
, (m_ses.can_write_to_disk()?"yes":"no")
|
2010-12-19 20:40:32 +01:00
|
|
|
, m_ses.settings().max_queued_disk_bytes
|
|
|
|
, (m_disconnecting?"yes":"no"));
|
2008-02-27 21:37:41 +01:00
|
|
|
#endif
|
2009-06-10 10:30:55 +02:00
|
|
|
// if we block reading, waiting for the disk, we will wake up
|
|
|
|
// by the disk_io_thread posting a message every time it drops
|
|
|
|
// from being at or exceeding the limit down to below the limit
|
2008-02-27 21:37:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-06-18 06:43:20 +02:00
|
|
|
error_code ec;
|
2010-08-28 21:44:50 +02:00
|
|
|
try_read(read_async, ec);
|
2010-06-18 06:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t peer_connection::try_read(sync_t s, error_code& ec)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_packet_size > 0);
|
2007-05-25 19:06:30 +02:00
|
|
|
int max_receive = m_packet_size - m_recv_pos;
|
2010-05-23 23:39:03 +02:00
|
|
|
TORRENT_ASSERT(max_receive >= 0);
|
|
|
|
|
2009-03-13 07:09:39 +01:00
|
|
|
if (m_recv_pos >= m_soft_packet_size) m_soft_packet_size = 0;
|
|
|
|
if (m_soft_packet_size && max_receive > m_soft_packet_size - m_recv_pos)
|
|
|
|
max_receive = m_soft_packet_size - m_recv_pos;
|
2009-04-26 02:21:59 +02:00
|
|
|
int quota_left = m_quota[download_channel];
|
2009-11-14 22:25:13 +01:00
|
|
|
if (max_receive > quota_left)
|
2007-05-25 19:06:30 +02:00
|
|
|
max_receive = quota_left;
|
|
|
|
|
2010-06-18 06:43:20 +02:00
|
|
|
if (max_receive == 0)
|
|
|
|
{
|
|
|
|
ec = asio::error::would_block;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_recv_pos >= 0);
|
|
|
|
TORRENT_ASSERT(m_packet_size > 0);
|
2010-06-18 06:43:20 +02:00
|
|
|
|
|
|
|
if (!can_read())
|
|
|
|
{
|
|
|
|
ec = asio::error::would_block;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-04-10 12:03:23 +02:00
|
|
|
|
|
|
|
int regular_buffer_size = m_packet_size - m_disk_recv_buffer_size;
|
|
|
|
|
|
|
|
if (int(m_recv_buffer.size()) < regular_buffer_size)
|
2010-12-24 02:22:49 +01:00
|
|
|
m_recv_buffer.resize(round_up8(regular_buffer_size));
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2010-06-18 06:43:20 +02:00
|
|
|
boost::array<asio::mutable_buffer, 2> vec;
|
|
|
|
int num_bufs = 0;
|
2008-05-05 08:25:22 +02:00
|
|
|
if (!m_disk_recv_buffer || regular_buffer_size >= m_recv_pos + max_receive)
|
2008-04-10 12:03:23 +02:00
|
|
|
{
|
|
|
|
// only receive into regular buffer
|
|
|
|
TORRENT_ASSERT(m_recv_pos + max_receive <= int(m_recv_buffer.size()));
|
2010-06-18 06:43:20 +02:00
|
|
|
vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos], max_receive);
|
|
|
|
num_bufs = 1;
|
2008-04-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
else if (m_recv_pos >= regular_buffer_size)
|
|
|
|
{
|
|
|
|
// only receive into disk buffer
|
|
|
|
TORRENT_ASSERT(m_recv_pos - regular_buffer_size >= 0);
|
|
|
|
TORRENT_ASSERT(m_recv_pos - regular_buffer_size + max_receive <= m_disk_recv_buffer_size);
|
2010-06-18 06:43:20 +02:00
|
|
|
vec[0] = asio::buffer(m_disk_recv_buffer.get() + m_recv_pos - regular_buffer_size, max_receive);
|
|
|
|
num_bufs = 1;
|
2008-04-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// receive into both regular and disk buffer
|
|
|
|
TORRENT_ASSERT(max_receive + m_recv_pos > regular_buffer_size);
|
|
|
|
TORRENT_ASSERT(m_recv_pos < regular_buffer_size);
|
|
|
|
TORRENT_ASSERT(max_receive - regular_buffer_size
|
|
|
|
+ m_recv_pos <= m_disk_recv_buffer_size);
|
|
|
|
|
|
|
|
vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos]
|
|
|
|
, regular_buffer_size - m_recv_pos);
|
2008-05-05 08:25:22 +02:00
|
|
|
vec[1] = asio::buffer(m_disk_recv_buffer.get()
|
2008-04-10 12:03:23 +02:00
|
|
|
, max_receive - regular_buffer_size + m_recv_pos);
|
2010-06-18 06:43:20 +02:00
|
|
|
num_bufs = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s == read_async)
|
|
|
|
{
|
2011-10-25 19:34:42 +02:00
|
|
|
TORRENT_ASSERT((m_channel_state[download_channel] & peer_info::bw_network) == 0);
|
|
|
|
m_channel_state[download_channel] |= peer_info::bw_network;
|
2010-06-18 06:43:20 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("<<< ASYNC_READ [ max: %d bytes ]", max_receive);
|
2010-06-18 06:43:20 +02:00
|
|
|
#endif
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("peer_connection::on_receive_data");
|
|
|
|
#endif
|
2010-06-18 06:43:20 +02:00
|
|
|
if (num_bufs == 1)
|
|
|
|
{
|
|
|
|
m_socket->async_read_some(
|
|
|
|
asio::mutable_buffers_1(vec[0]), make_read_handler(
|
|
|
|
boost::bind(&peer_connection::on_receive_data, self(), _1, _2)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_socket->async_read_some(
|
|
|
|
vec, make_read_handler(
|
|
|
|
boost::bind(&peer_connection::on_receive_data, self(), _1, _2)));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ret = 0;
|
|
|
|
if (num_bufs == 1)
|
|
|
|
{
|
|
|
|
ret = m_socket->read_some(asio::mutable_buffers_1(vec[0]), ec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = m_socket->read_some(vec, ec);
|
2008-04-10 12:03:23 +02:00
|
|
|
}
|
2010-06-18 06:43:20 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("<<< SYNC_READ [ max: %d ret: %d e: %s ]", max_receive, ret, ec ? ec.message().c_str() : "");
|
2010-06-18 06:43:20 +02:00
|
|
|
#endif
|
|
|
|
return ret;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|
|
|
|
// returns the last 'bytes' from the receive buffer
|
|
|
|
std::pair<buffer::interval, buffer::interval> peer_connection::wr_recv_buffers(int bytes)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(bytes <= m_recv_pos);
|
|
|
|
|
|
|
|
std::pair<buffer::interval, buffer::interval> vec;
|
|
|
|
int regular_buffer_size = m_packet_size - m_disk_recv_buffer_size;
|
|
|
|
TORRENT_ASSERT(regular_buffer_size >= 0);
|
2008-05-05 08:25:22 +02:00
|
|
|
if (!m_disk_recv_buffer || regular_buffer_size >= m_recv_pos)
|
2008-04-10 12:03:23 +02:00
|
|
|
{
|
|
|
|
vec.first = buffer::interval(&m_recv_buffer[0]
|
|
|
|
+ m_recv_pos - bytes, &m_recv_buffer[0] + m_recv_pos);
|
|
|
|
vec.second = buffer::interval(0,0);
|
|
|
|
}
|
|
|
|
else if (m_recv_pos - bytes >= regular_buffer_size)
|
|
|
|
{
|
2008-05-05 08:25:22 +02:00
|
|
|
vec.first = buffer::interval(m_disk_recv_buffer.get() + m_recv_pos
|
|
|
|
- regular_buffer_size - bytes, m_disk_recv_buffer.get() + m_recv_pos
|
2008-04-10 12:03:23 +02:00
|
|
|
- regular_buffer_size);
|
|
|
|
vec.second = buffer::interval(0,0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_recv_pos - bytes < regular_buffer_size);
|
|
|
|
TORRENT_ASSERT(m_recv_pos > regular_buffer_size);
|
|
|
|
vec.first = buffer::interval(&m_recv_buffer[0] + m_recv_pos - bytes
|
|
|
|
, &m_recv_buffer[0] + regular_buffer_size);
|
2008-05-05 08:25:22 +02:00
|
|
|
vec.second = buffer::interval(m_disk_recv_buffer.get()
|
|
|
|
, m_disk_recv_buffer.get() + m_recv_pos - regular_buffer_size);
|
2008-04-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
TORRENT_ASSERT(vec.first.left() + vec.second.left() == bytes);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::reset_recv_buffer(int packet_size)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(packet_size > 0);
|
2007-02-12 06:46:29 +01:00
|
|
|
if (m_recv_pos > m_packet_size)
|
|
|
|
{
|
|
|
|
cut_receive_buffer(m_packet_size, packet_size);
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
m_recv_pos = 0;
|
|
|
|
m_packet_size = packet_size;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2010-06-18 07:44:08 +02:00
|
|
|
void nop(char*) {}
|
|
|
|
|
|
|
|
void peer_connection::append_const_send_buffer(char const* buffer, int size)
|
|
|
|
{
|
|
|
|
m_send_buffer.append_buffer((char*)buffer, size, size, &nop);
|
2011-02-01 18:25:06 +01:00
|
|
|
#if defined TORRENT_STATS && defined TORRENT_DISK_STATS
|
2010-06-18 07:44:08 +02:00
|
|
|
m_ses.m_buffer_usage_logger << log_time() << " append_const_send_buffer: " << size << std::endl;
|
|
|
|
m_ses.log_buffer_usage();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-19 04:41:28 +02:00
|
|
|
void peer_connection::send_buffer(char const* buf, int size, int flags
|
|
|
|
, void (*fun)(char*, int, void*), void* userdata)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-07-08 02:03:08 +02:00
|
|
|
if (flags == message_type_request)
|
|
|
|
m_requests_in_buffer.push_back(m_send_buffer.size() + size);
|
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
int free_space = m_send_buffer.space_in_last_buffer();
|
|
|
|
if (free_space > size) free_space = size;
|
|
|
|
if (free_space > 0)
|
|
|
|
{
|
2011-05-19 04:41:28 +02:00
|
|
|
char* dst = m_send_buffer.append(buf, free_space);
|
|
|
|
TORRENT_ASSERT(dst != 0);
|
|
|
|
if (fun) fun(dst, free_space, userdata);
|
2007-09-29 18:14:03 +02:00
|
|
|
size -= free_space;
|
|
|
|
buf += free_space;
|
2010-01-31 21:32:12 +01:00
|
|
|
#if defined TORRENT_STATS && defined TORRENT_DISK_STATS
|
2007-09-29 18:14:03 +02:00
|
|
|
m_ses.m_buffer_usage_logger << log_time() << " send_buffer: "
|
|
|
|
<< free_space << std::endl;
|
|
|
|
m_ses.log_buffer_usage();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (size <= 0) return;
|
|
|
|
|
2010-01-31 21:32:12 +01:00
|
|
|
#if defined TORRENT_STATS && defined TORRENT_DISK_STATS
|
2007-09-29 18:14:03 +02:00
|
|
|
m_ses.m_buffer_usage_logger << log_time() << " send_buffer_alloc: " << size << std::endl;
|
|
|
|
m_ses.log_buffer_usage();
|
|
|
|
#endif
|
2011-05-19 04:41:28 +02:00
|
|
|
int i = 0;
|
|
|
|
while (size > 0)
|
2007-09-29 18:14:03 +02:00
|
|
|
{
|
2011-05-19 04:41:28 +02:00
|
|
|
char* chain_buf = m_ses.allocate_buffer();
|
|
|
|
if (chain_buf == 0)
|
2008-02-05 06:51:05 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2011-05-19 04:41:28 +02:00
|
|
|
return;
|
2008-02-05 06:51:05 +01:00
|
|
|
}
|
2011-05-19 04:41:28 +02:00
|
|
|
|
|
|
|
int buf_size = (std::min)(int(aux::session_impl::send_buffer_size), size);
|
|
|
|
memcpy(chain_buf, buf, buf_size);
|
|
|
|
if (fun) fun(chain_buf, buf_size, userdata);
|
|
|
|
buf += buf_size;
|
|
|
|
size -= buf_size;
|
|
|
|
m_send_buffer.append_buffer(chain_buf, aux::session_impl::send_buffer_size, buf_size
|
|
|
|
, boost::bind(&session_impl::free_buffer, boost::ref(m_ses), _1));
|
|
|
|
++i;
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
2011-10-24 06:22:53 +02:00
|
|
|
setup_send();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct set_to_zero
|
|
|
|
{
|
|
|
|
set_to_zero(T& v, bool cond): m_val(v), m_cond(cond) {}
|
|
|
|
void fire() { if (!m_cond) return; m_cond = false; m_val = 0; }
|
|
|
|
~set_to_zero() { if (m_cond) m_val = 0; }
|
|
|
|
private:
|
|
|
|
T& m_val;
|
|
|
|
bool m_cond;
|
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// RECEIVE DATA
|
|
|
|
// --------------------------
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void peer_connection::on_receive_data(const error_code& error
|
2008-04-07 10:39:01 +02:00
|
|
|
, std::size_t bytes_transferred)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_num_messages[aux::session_impl::on_read_counter];
|
2011-10-17 08:54:02 +02:00
|
|
|
int size = 8;
|
|
|
|
int index = 0;
|
2011-10-19 04:40:37 +02:00
|
|
|
while (bytes_transferred > size + 13) { size <<= 1; ++index; }
|
2011-10-17 08:54:02 +02:00
|
|
|
int num_max = sizeof(m_ses.m_recv_buffer_sizes)/sizeof(m_ses.m_recv_buffer_sizes[0]);
|
|
|
|
if (index >= num_max) index = num_max - 1;
|
|
|
|
++m_ses.m_recv_buffer_sizes[index];
|
2011-10-17 07:17:21 +02:00
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2011-01-25 09:03:35 +01:00
|
|
|
|
|
|
|
// keep ourselves alive in until this function exits in
|
|
|
|
// case we disconnect
|
|
|
|
// this needs to be created before the invariant check,
|
|
|
|
// to keep the object alive through the exit check
|
|
|
|
boost::intrusive_ptr<peer_connection> me(self());
|
|
|
|
|
2011-10-24 06:22:53 +02:00
|
|
|
// flush the send buffer at the end of this function
|
|
|
|
cork _c(*this);
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2013-08-15 07:29:05 +02:00
|
|
|
peer_log("<<< ON_RECEIVE_DATA [ bytes: %d error: %s ]"
|
|
|
|
, bytes_transferred, error.message().c_str());
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("peer_connection::on_receive_data");
|
|
|
|
#endif
|
2011-11-30 21:07:18 +01:00
|
|
|
|
|
|
|
// leave this bit set until we're done looping, reading from the socket.
|
|
|
|
// that way we don't trigger any async read calls until the end of this
|
|
|
|
// function.
|
2011-10-25 19:34:42 +02:00
|
|
|
TORRENT_ASSERT(m_channel_state[download_channel] & peer_info::bw_network);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-12-28 21:06:59 +01:00
|
|
|
int bytes_in_loop = bytes_transferred;
|
2008-11-14 21:51:49 +01:00
|
|
|
|
2010-02-20 19:56:53 +01:00
|
|
|
if (m_extension_outstanding_bytes > 0)
|
|
|
|
m_extension_outstanding_bytes -= (std::min)(m_extension_outstanding_bytes, int(bytes_transferred));
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("*** ERROR [ in peer_connection::on_receive_data error: %s ]"
|
|
|
|
, error.message().c_str());
|
2005-03-19 13:22:40 +01:00
|
|
|
#endif
|
2008-12-28 21:06:59 +01:00
|
|
|
m_statistics.trancieve_ip_packet(bytes_in_loop, m_remote.address().is_v6());
|
2006-04-25 23:04:48 +02:00
|
|
|
on_receive(error, bytes_transferred);
|
2009-06-12 18:40:38 +02:00
|
|
|
disconnect(error);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-03-28 19:45:37 +02:00
|
|
|
|
2010-08-28 21:44:50 +02:00
|
|
|
int num_loops = 0;
|
2007-05-10 03:50:11 +02:00
|
|
|
do
|
|
|
|
{
|
2011-02-15 11:05:25 +01:00
|
|
|
TORRENT_ASSERT(int(m_recv_pos + bytes_transferred) <= m_packet_size);
|
2007-05-25 21:42:10 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("<<< read %d bytes", int(bytes_transferred));
|
2007-05-25 21:42:10 +02:00
|
|
|
#endif
|
2007-05-10 03:50:11 +02:00
|
|
|
// correct the dl quota usage, if not all of the buffer was actually read
|
2010-02-14 02:39:55 +01:00
|
|
|
TORRENT_ASSERT(int(bytes_transferred) <= m_quota[download_channel]);
|
2009-11-14 22:25:13 +01:00
|
|
|
m_quota[download_channel] -= bytes_transferred;
|
2007-05-10 03:50:11 +02:00
|
|
|
|
2008-12-28 21:06:59 +01:00
|
|
|
if (m_disconnecting)
|
|
|
|
{
|
|
|
|
m_statistics.trancieve_ip_packet(bytes_in_loop, m_remote.address().is_v6());
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_packet_size > 0);
|
|
|
|
TORRENT_ASSERT(bytes_transferred > 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-05-10 03:50:11 +02:00
|
|
|
m_last_receive = time_now();
|
|
|
|
m_recv_pos += bytes_transferred;
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(m_recv_pos <= int(m_recv_buffer.size()
|
|
|
|
+ m_disk_recv_buffer_size));
|
2007-12-19 22:36:54 +01:00
|
|
|
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-09-21 19:12:26 +02:00
|
|
|
size_type cur_payload_dl = m_statistics.last_payload_downloaded();
|
|
|
|
size_type cur_protocol_dl = m_statistics.last_protocol_downloaded();
|
|
|
|
#endif
|
2009-11-28 09:58:07 +01:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
on_receive(error, bytes_transferred);
|
|
|
|
}
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-09-21 19:12:26 +02:00
|
|
|
TORRENT_ASSERT(m_statistics.last_payload_downloaded() - cur_payload_dl >= 0);
|
|
|
|
TORRENT_ASSERT(m_statistics.last_protocol_downloaded() - cur_protocol_dl >= 0);
|
|
|
|
size_type stats_diff = m_statistics.last_payload_downloaded() - cur_payload_dl +
|
|
|
|
m_statistics.last_protocol_downloaded() - cur_protocol_dl;
|
2010-02-14 02:39:55 +01:00
|
|
|
TORRENT_ASSERT(stats_diff == int(bytes_transferred));
|
2008-09-21 19:12:26 +02:00
|
|
|
#endif
|
2010-05-23 23:39:03 +02:00
|
|
|
if (m_disconnecting) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_packet_size > 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-05-10 03:50:11 +02:00
|
|
|
if (m_peer_choked
|
|
|
|
&& m_recv_pos == 0
|
|
|
|
&& (m_recv_buffer.capacity() - m_packet_size) > 128)
|
|
|
|
{
|
2010-12-24 02:22:49 +01:00
|
|
|
// round up to an even 8 bytes since that's the RC4 blocksize
|
|
|
|
buffer(round_up8(m_packet_size)).swap(m_recv_buffer);
|
2007-05-10 03:50:11 +02:00
|
|
|
}
|
|
|
|
|
2009-03-13 07:09:39 +01:00
|
|
|
if (m_recv_pos >= m_soft_packet_size) m_soft_packet_size = 0;
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2010-08-28 21:44:50 +02:00
|
|
|
if (num_loops > 20) break;
|
|
|
|
|
2010-06-18 06:43:20 +02:00
|
|
|
error_code ec;
|
|
|
|
bytes_transferred = try_read(read_sync, ec);
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_ASSERT(bytes_transferred > 0 || ec);
|
2007-05-10 03:50:11 +02:00
|
|
|
if (ec && ec != asio::error::would_block)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2008-12-28 21:06:59 +01:00
|
|
|
m_statistics.trancieve_ip_packet(bytes_in_loop, m_remote.address().is_v6());
|
2009-06-12 18:40:38 +02:00
|
|
|
disconnect(ec);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-04-10 12:03:23 +02:00
|
|
|
if (ec == asio::error::would_block) break;
|
2008-12-28 21:06:59 +01:00
|
|
|
bytes_in_loop += bytes_transferred;
|
2010-08-28 21:44:50 +02:00
|
|
|
++num_loops;
|
2007-04-25 19:36:12 +02:00
|
|
|
}
|
2007-05-10 03:50:11 +02:00
|
|
|
while (bytes_transferred > 0);
|
2007-04-25 19:36:12 +02:00
|
|
|
|
2010-03-19 19:39:51 +01:00
|
|
|
if (is_seed())
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
if (t) t->seen_complete();
|
|
|
|
}
|
|
|
|
|
2008-12-28 21:06:59 +01:00
|
|
|
m_statistics.trancieve_ip_packet(bytes_in_loop, m_remote.address().is_v6());
|
2011-11-30 21:07:18 +01:00
|
|
|
|
|
|
|
// allow reading from the socket again
|
|
|
|
TORRENT_ASSERT(m_channel_state[download_channel] & peer_info::bw_network);
|
|
|
|
m_channel_state[download_channel] &= ~peer_info::bw_network;
|
|
|
|
|
2010-08-28 21:44:50 +02:00
|
|
|
setup_receive(read_async);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
bool peer_connection::can_write() const
|
2004-01-04 05:29:13 +01:00
|
|
|
{
|
|
|
|
// if we have requests or pending data to be sent or announcements to be made
|
|
|
|
// we want to send data
|
2007-09-29 18:14:03 +02:00
|
|
|
return !m_send_buffer.empty()
|
2009-11-14 22:25:13 +01:00
|
|
|
&& m_quota[upload_channel] > 0
|
2006-04-25 23:04:48 +02:00
|
|
|
&& !m_connecting;
|
2004-03-28 19:45:37 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 05:24:10 +01:00
|
|
|
bool peer_connection::can_read(boost::uint8_t* state) const
|
2004-03-28 19:45:37 +02:00
|
|
|
{
|
2009-06-10 10:30:55 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
|
2009-11-14 22:25:13 +01:00
|
|
|
bool bw_limit = m_quota[download_channel] > 0;
|
2009-07-21 06:29:29 +02:00
|
|
|
|
|
|
|
if (!bw_limit) return false;
|
|
|
|
|
|
|
|
bool disk = m_ses.settings().max_queued_disk_bytes == 0
|
2011-02-13 23:27:02 +01:00
|
|
|
|| m_ses.can_write_to_disk()
|
2011-01-23 03:09:54 +01:00
|
|
|
// don't block this peer because of disk saturation
|
|
|
|
// if we're not downloading any pieces from it
|
|
|
|
|| m_outstanding_bytes == 0;
|
2009-07-21 06:29:29 +02:00
|
|
|
|
|
|
|
if (!disk)
|
|
|
|
{
|
2011-02-01 17:55:32 +01:00
|
|
|
if (state)
|
|
|
|
{
|
2011-10-25 19:34:42 +02:00
|
|
|
if ((*state & peer_info::bw_disk) == 0)
|
|
|
|
m_ses.inc_disk_queue(download_channel);
|
|
|
|
*state |= peer_info::bw_disk;
|
2011-02-01 17:55:32 +01:00
|
|
|
}
|
2009-07-21 06:29:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !m_connecting && !m_disconnecting;
|
2004-01-04 05:29:13 +01:00
|
|
|
}
|
|
|
|
|
2009-05-23 23:36:09 +02:00
|
|
|
void peer_connection::on_connect(int ticket)
|
2005-11-02 17:28:39 +01:00
|
|
|
{
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-10-06 01:36:50 +02:00
|
|
|
// in case we disconnect here, we need to
|
|
|
|
// keep the connection alive until the
|
|
|
|
// exit invariant check is run
|
|
|
|
boost::intrusive_ptr<peer_connection> me(self());
|
|
|
|
#endif
|
2005-11-02 17:28:39 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-08-03 11:08:37 +02:00
|
|
|
(*m_ses.m_logger) << time_now_string() << " ON_CONNECT: " << print_endpoint(m_remote) << "\n";
|
2005-11-02 17:28:39 +01:00
|
|
|
#endif
|
|
|
|
|
2012-07-16 01:22:23 +02:00
|
|
|
if (ticket == -1)
|
|
|
|
{
|
|
|
|
disconnect(asio::error::operation_aborted);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
m_connection_ticket = ticket;
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
|
2005-11-02 17:28:39 +01:00
|
|
|
m_queued = false;
|
2008-03-16 11:49:47 +01:00
|
|
|
|
|
|
|
if (!t)
|
|
|
|
{
|
2012-01-21 22:29:22 +01:00
|
|
|
TORRENT_ASSERT(!m_connecting);
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::torrent_aborted);
|
2008-03-16 11:49:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-21 22:29:22 +01:00
|
|
|
TORRENT_ASSERT(m_connecting);
|
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log(">>> OPEN [ protocol: %s ]", (m_remote.address().is_v4()?"IPv4":"IPv6"));
|
2010-08-03 11:08:37 +02:00
|
|
|
#endif
|
2009-03-22 21:34:57 +01:00
|
|
|
m_socket->open(m_remote.protocol(), ec);
|
2008-01-07 02:10:46 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
disconnect(ec);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-05-10 03:50:11 +02:00
|
|
|
|
2008-09-25 09:40:55 +02:00
|
|
|
tcp::endpoint bind_interface = t->get_interface();
|
|
|
|
|
|
|
|
std::pair<int, int> const& out_ports = m_ses.settings().outgoing_ports;
|
|
|
|
if (out_ports.first > 0 && out_ports.second >= out_ports.first)
|
|
|
|
{
|
2010-08-03 11:08:37 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log(">>> SET_REUSE_ADDRESS");
|
2010-08-03 11:08:37 +02:00
|
|
|
#endif
|
2008-09-25 09:40:55 +02:00
|
|
|
m_socket->set_option(socket_acceptor::reuse_address(true), ec);
|
2010-08-03 11:08:37 +02:00
|
|
|
// ignore errors because the underlying socket may not
|
|
|
|
// be opened yet. This happens when we're routing through
|
|
|
|
// a proxy. In that case, we don't yet know the address of
|
|
|
|
// the proxy server, and more importantly, we don't know
|
|
|
|
// the address family of its address. This means we can't
|
|
|
|
// open the socket yet. The socks abstraction layer defers
|
|
|
|
// opening it.
|
|
|
|
ec.clear();
|
2008-09-25 09:40:55 +02:00
|
|
|
bind_interface.port(m_ses.next_port());
|
|
|
|
}
|
|
|
|
|
2009-04-04 12:08:07 +02:00
|
|
|
// if we're not binding to a specific interface, bind
|
|
|
|
// to the same protocol family as the target endpoint
|
|
|
|
if (is_any(bind_interface.address()))
|
|
|
|
{
|
2009-11-27 19:46:29 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (m_remote.address().is_v6())
|
2009-04-04 12:08:07 +02:00
|
|
|
bind_interface.address(address_v6::any());
|
2009-11-27 19:46:29 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
bind_interface.address(address_v4::any());
|
2009-04-04 12:08:07 +02:00
|
|
|
}
|
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log(">>> BIND [ ep: %s ]", print_endpoint(bind_interface).c_str());
|
2010-08-03 11:08:37 +02:00
|
|
|
#endif
|
2008-09-25 09:40:55 +02:00
|
|
|
m_socket->bind(bind_interface, ec);
|
2008-01-07 02:10:46 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
disconnect(ec);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2011-09-12 05:51:49 +02:00
|
|
|
peer_log(">>> ASYNC_CONNECT [ dst: %s ]", print_endpoint(m_remote).c_str());
|
2010-11-28 02:47:30 +01:00
|
|
|
#endif
|
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("peer_connection::on_connection_complete");
|
2010-08-03 11:08:37 +02:00
|
|
|
#endif
|
2007-04-25 20:26:35 +02:00
|
|
|
m_socket->async_connect(m_remote
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&peer_connection::on_connection_complete, self(), _1));
|
2012-01-16 00:34:43 +01:00
|
|
|
m_connect = time_now_hires();
|
2008-11-14 21:51:49 +01:00
|
|
|
m_statistics.sent_syn(m_remote.address().is_v6());
|
2005-11-02 17:28:39 +01:00
|
|
|
|
2008-07-06 14:22:56 +02:00
|
|
|
if (t->alerts().should_post<peer_connect_alert>())
|
2005-11-02 17:28:39 +01:00
|
|
|
{
|
2008-07-06 14:22:56 +02:00
|
|
|
t->alerts().post_alert(peer_connect_alert(
|
2013-08-01 02:44:14 +02:00
|
|
|
t->get_handle(), remote(), pid(), m_socket->type()));
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
2012-01-16 00:34:43 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("*** LOCAL ENDPOINT[ e: %s ]", print_endpoint(m_socket->local_endpoint(ec)).c_str());
|
|
|
|
#endif
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void peer_connection::on_connection_complete(error_code const& e)
|
2005-11-02 17:28:39 +01:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("peer_connection::on_connection_complete");
|
|
|
|
#endif
|
2012-01-16 00:34:43 +01:00
|
|
|
ptime completed = time_now_hires();
|
2008-02-09 23:42:56 +01:00
|
|
|
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2005-11-02 17:28:39 +01:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-02-09 23:42:56 +01:00
|
|
|
m_rtt = total_milliseconds(completed - m_connect);
|
2012-01-16 00:34:43 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
// add this RTT to the PRNG seed, to add more unpredictability
|
|
|
|
boost::uint64_t now = total_microseconds(completed - m_connect);
|
|
|
|
// assume 12 bits of entropy (i.e. about 8 milliseconds)
|
|
|
|
RAND_add(&now, 8, 1.5);
|
|
|
|
#endif
|
2008-02-09 23:42:56 +01:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
if (m_disconnecting) return;
|
|
|
|
|
2008-10-23 06:10:23 +02:00
|
|
|
error_code ec;
|
2006-04-25 23:04:48 +02:00
|
|
|
if (e)
|
|
|
|
{
|
2010-11-29 02:33:05 +01:00
|
|
|
connect_failed(e);
|
2006-04-25 23:04:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2005-11-02 17:28:39 +01:00
|
|
|
|
2012-01-21 10:05:34 +01:00
|
|
|
// if t is NULL, we better not be connecting, since
|
|
|
|
// we can't decrement the connecting counter
|
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
TORRENT_ASSERT(t || !m_connecting);
|
|
|
|
if (m_connecting && t)
|
|
|
|
{
|
2012-01-23 06:14:12 +01:00
|
|
|
t->dec_num_connecting();
|
2012-01-21 10:05:34 +01:00
|
|
|
m_connecting = false;
|
|
|
|
}
|
2013-08-19 05:54:45 +02:00
|
|
|
if (m_connection_ticket != -1)
|
|
|
|
{
|
|
|
|
if (m_ses.m_half_open.done(m_connection_ticket))
|
|
|
|
m_connection_ticket = -1;
|
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2006-05-29 19:50:57 +02:00
|
|
|
if (m_disconnecting) return;
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_receive = time_now();
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2012-10-05 05:20:40 +02:00
|
|
|
if (is_utp(*m_socket) && m_peer_info)
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
|
|
|
m_peer_info->confirmed_supports_utp = true;
|
|
|
|
m_peer_info->supports_utp = false;
|
|
|
|
}
|
|
|
|
|
2005-11-02 17:28:39 +01:00
|
|
|
// this means the connection just succeeded
|
|
|
|
|
2008-11-14 21:51:49 +01:00
|
|
|
m_statistics.received_synack(m_remote.address().is_v6());
|
2008-09-26 08:10:21 +02:00
|
|
|
|
2008-06-16 15:54:14 +02:00
|
|
|
TORRENT_ASSERT(m_socket);
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log(">>> COMPLETED [ ep: %s rtt: %d ]", print_endpoint(m_remote).c_str(), m_rtt);
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
|
2010-08-03 11:08:37 +02:00
|
|
|
// set the socket to non-blocking, so that we can
|
|
|
|
// read the entire buffer on each read event we get
|
|
|
|
tcp::socket::non_blocking_io ioc(true);
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("*** SET NON-BLOCKING");
|
2010-08-03 11:08:37 +02:00
|
|
|
#endif
|
|
|
|
m_socket->io_control(ioc, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
disconnect(ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-16 15:54:14 +02:00
|
|
|
if (m_remote == m_socket->local_endpoint(ec))
|
|
|
|
{
|
|
|
|
// if the remote endpoint is the same as the local endpoint, we're connected
|
|
|
|
// to ourselves
|
2009-06-18 18:16:41 +02:00
|
|
|
if (m_peer_info && t) t->get_policy().ban_peer(m_peer_info);
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::self_connection, 1);
|
2008-06-16 15:54:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-12 17:58:23 +01:00
|
|
|
if (m_remote.address().is_v4())
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-03-12 17:58:23 +01:00
|
|
|
m_socket->set_option(type_of_service(m_ses.settings().peer_tos), ec);
|
2011-11-10 10:18:55 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log(">>> SET_TOS[ tos: %d e: %s ]", m_ses.settings().peer_tos, ec.message().c_str());
|
|
|
|
#endif
|
2008-03-12 17:58:23 +01:00
|
|
|
}
|
|
|
|
|
2013-08-04 01:31:43 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
(*i)->on_connected();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
on_connected();
|
2006-05-02 01:34:37 +02:00
|
|
|
setup_send();
|
2007-01-10 16:02:25 +01:00
|
|
|
setup_receive();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2005-11-02 17:28:39 +01:00
|
|
|
|
2004-01-04 05:29:13 +01:00
|
|
|
// --------------------------
|
|
|
|
// SEND DATA
|
|
|
|
// --------------------------
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void peer_connection::on_send_data(error_code const& error
|
2008-04-07 10:39:01 +02:00
|
|
|
, std::size_t bytes_transferred)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2011-10-17 07:17:21 +02:00
|
|
|
#ifdef TORRENT_STATS
|
|
|
|
++m_ses.m_num_messages[aux::session_impl::on_write_counter];
|
2011-10-17 08:54:02 +02:00
|
|
|
int size = 8;
|
|
|
|
int index = 0;
|
2011-10-19 04:40:37 +02:00
|
|
|
while (bytes_transferred > size + 13) { size <<= 1; ++index; }
|
2011-10-17 08:54:02 +02:00
|
|
|
int num_max = sizeof(m_ses.m_send_buffer_sizes)/sizeof(m_ses.m_send_buffer_sizes[0]);
|
|
|
|
if (index >= num_max) index = num_max - 1;
|
|
|
|
++m_ses.m_send_buffer_sizes[index];
|
2011-10-17 07:17:21 +02:00
|
|
|
#endif
|
2010-07-14 06:16:38 +02:00
|
|
|
TORRENT_ASSERT(m_ses.is_network_thread());
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2010-12-19 20:40:32 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
|
|
|
peer_log("*** ON_SEND_DATA [ bytes: %d error: %s ]"
|
|
|
|
, int(bytes_transferred), error.message().c_str());
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
INVARIANT_CHECK;
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("peer_connection::on_send_data");
|
|
|
|
#endif
|
2009-01-25 03:45:31 +01:00
|
|
|
// keep ourselves alive in until this function exits in
|
|
|
|
// case we disconnect
|
|
|
|
boost::intrusive_ptr<peer_connection> me(self());
|
|
|
|
|
2011-10-25 19:34:42 +02:00
|
|
|
TORRENT_ASSERT(m_channel_state[upload_channel] & peer_info::bw_network);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
m_send_buffer.pop_front(bytes_transferred);
|
2008-07-08 02:03:08 +02:00
|
|
|
|
|
|
|
for (std::vector<int>::iterator i = m_requests_in_buffer.begin()
|
|
|
|
, end(m_requests_in_buffer.end()); i != end; ++i)
|
|
|
|
*i -= bytes_transferred;
|
|
|
|
|
|
|
|
while (!m_requests_in_buffer.empty()
|
|
|
|
&& m_requests_in_buffer.front() <= 0)
|
|
|
|
m_requests_in_buffer.erase(m_requests_in_buffer.begin());
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2011-10-25 19:34:42 +02:00
|
|
|
m_channel_state[upload_channel] &= ~peer_info::bw_network;
|
2007-01-10 16:02:25 +01:00
|
|
|
|
2010-02-14 02:39:55 +01:00
|
|
|
TORRENT_ASSERT(int(bytes_transferred) <= m_quota[upload_channel]);
|
2009-11-14 22:25:13 +01:00
|
|
|
m_quota[upload_channel] -= bytes_transferred;
|
2007-05-25 19:06:30 +02:00
|
|
|
|
2008-11-14 21:51:49 +01:00
|
|
|
m_statistics.trancieve_ip_packet(bytes_transferred, m_remote.address().is_v6());
|
|
|
|
|
2007-05-25 19:06:30 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log(">>> wrote %d bytes", int(bytes_transferred));
|
2007-05-25 19:06:30 +02:00
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (error)
|
2006-05-29 19:50:57 +02:00
|
|
|
{
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2010-10-31 23:12:26 +01:00
|
|
|
peer_log("**ERROR**: %s [in peer_connection::on_send_data]", error.message().c_str());
|
2006-05-29 19:50:57 +02:00
|
|
|
#endif
|
2009-06-12 18:40:38 +02:00
|
|
|
disconnect(error);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-05-29 19:50:57 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_disconnecting) return;
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_connecting);
|
|
|
|
TORRENT_ASSERT(bytes_transferred > 0);
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_sent = time_now();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-09-21 19:12:26 +02:00
|
|
|
size_type cur_payload_ul = m_statistics.last_payload_uploaded();
|
|
|
|
size_type cur_protocol_ul = m_statistics.last_protocol_uploaded();
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
on_sent(error, bytes_transferred);
|
2011-08-22 03:37:56 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-09-21 19:12:26 +02:00
|
|
|
TORRENT_ASSERT(m_statistics.last_payload_uploaded() - cur_payload_ul >= 0);
|
|
|
|
TORRENT_ASSERT(m_statistics.last_protocol_uploaded() - cur_protocol_ul >= 0);
|
|
|
|
size_type stats_diff = m_statistics.last_payload_uploaded() - cur_payload_ul
|
|
|
|
+ m_statistics.last_protocol_uploaded() - cur_protocol_ul;
|
2010-02-14 02:39:55 +01:00
|
|
|
TORRENT_ASSERT(stats_diff == int(bytes_transferred));
|
2008-09-21 19:12:26 +02:00
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
fill_send_buffer();
|
2007-04-25 19:36:12 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
setup_send();
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-02-25 05:13:46 +01:00
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
|
2010-10-02 23:01:11 +02:00
|
|
|
struct peer_count_t
|
|
|
|
{
|
|
|
|
peer_count_t(): num_peers(0), num_peers_with_timeouts(0), num_peers_with_nowant(0), num_not_requested(0) {}
|
|
|
|
int num_peers;
|
|
|
|
int num_peers_with_timeouts;
|
|
|
|
int num_peers_with_nowant;
|
|
|
|
int num_not_requested;
|
2010-10-04 05:26:42 +02:00
|
|
|
// std::vector<peer_connection const*> peers;
|
2010-10-02 23:01:11 +02:00
|
|
|
};
|
2010-05-01 18:17:37 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_connection::check_invariant() const
|
|
|
|
{
|
2012-02-09 07:24:58 +01:00
|
|
|
TORRENT_ASSERT(m_in_use == 1337);
|
2011-02-15 11:05:25 +01:00
|
|
|
TORRENT_ASSERT(m_queued_time_critical <= int(m_request_queue.size()));
|
2013-07-26 18:59:10 +02:00
|
|
|
TORRENT_ASSERT(m_accept_fast.size() == m_accept_fast_piece_cnt.size());
|
2009-11-01 01:47:22 +01:00
|
|
|
|
2008-05-05 08:25:22 +02:00
|
|
|
TORRENT_ASSERT(bool(m_disk_recv_buffer) == (m_disk_recv_buffer_size > 0));
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
TORRENT_ASSERT(m_upload_limit >= 0);
|
|
|
|
TORRENT_ASSERT(m_download_limit >= 0);
|
|
|
|
|
2013-11-10 11:49:57 +01:00
|
|
|
// It's not obvious why this invariant breaks when the peer disconnects
|
|
|
|
if (!m_disconnecting)
|
|
|
|
{
|
|
|
|
if (m_channel_state[upload_channel] & peer_info::bw_limit)
|
|
|
|
TORRENT_ASSERT(m_ses.m_upload_rate.is_queued(this));
|
|
|
|
if (m_channel_state[download_channel] & peer_info::bw_limit)
|
|
|
|
TORRENT_ASSERT(m_ses.m_download_rate.is_queued(this));
|
|
|
|
}
|
2013-11-02 04:26:53 +01:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
2011-11-03 10:20:16 +01:00
|
|
|
|
|
|
|
if (!m_disconnect_started && m_initialized)
|
|
|
|
{
|
|
|
|
// none of this matters if we're disconnecting anyway
|
|
|
|
if (t->is_finished())
|
|
|
|
TORRENT_ASSERT(!is_interesting());
|
|
|
|
if (is_seed())
|
|
|
|
TORRENT_ASSERT(upload_only());
|
|
|
|
}
|
|
|
|
|
2008-01-19 20:00:54 +01:00
|
|
|
if (m_disconnecting)
|
|
|
|
{
|
2010-10-04 00:06:53 +02:00
|
|
|
TORRENT_ASSERT(m_download_queue.empty());
|
|
|
|
TORRENT_ASSERT(m_request_queue.empty());
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(!t);
|
2008-07-18 12:03:42 +02:00
|
|
|
TORRENT_ASSERT(m_disconnect_started);
|
2008-01-19 20:00:54 +01:00
|
|
|
}
|
|
|
|
else if (!m_in_constructor)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_ses.has_peer((peer_connection*)this));
|
|
|
|
}
|
|
|
|
|
2009-04-26 21:44:08 +02:00
|
|
|
TORRENT_ASSERT(m_outstanding_bytes >= 0);
|
2009-03-31 20:01:12 +02:00
|
|
|
if (t && t->valid_metadata() && !m_disconnecting)
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
|
|
|
torrent_info const& ti = t->torrent_file();
|
|
|
|
// if the piece is fully downloaded, we might have popped it from the
|
|
|
|
// download queue already
|
|
|
|
int outstanding_bytes = 0;
|
|
|
|
bool in_download_queue = false;
|
|
|
|
int block_size = t->block_size();
|
2009-05-26 18:12:09 +02:00
|
|
|
piece_block last_block(ti.num_pieces()-1
|
2010-02-24 00:47:40 +01:00
|
|
|
, (ti.piece_size(ti.num_pieces()-1) + block_size - 1) / block_size);
|
2009-03-17 10:34:44 +01:00
|
|
|
for (std::vector<pending_block>::const_iterator i = m_download_queue.begin()
|
|
|
|
, end(m_download_queue.end()); i != end; ++i)
|
|
|
|
{
|
2009-05-26 18:12:09 +02:00
|
|
|
TORRENT_ASSERT(i->block.piece_index <= last_block.piece_index);
|
|
|
|
TORRENT_ASSERT(i->block.piece_index < last_block.piece_index
|
|
|
|
|| i->block.block_index <= last_block.block_index);
|
2010-06-22 20:09:04 +02:00
|
|
|
if (m_received_in_piece && i == m_download_queue.begin())
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
|
|
|
in_download_queue = true;
|
2010-08-13 17:55:52 +02:00
|
|
|
// this assert is not correct since block may have different sizes
|
|
|
|
// and may not be returned in the order they were requested
|
|
|
|
// TORRENT_ASSERT(t->to_req(i->block).length >= m_received_in_piece);
|
2010-06-22 20:09:04 +02:00
|
|
|
outstanding_bytes += t->to_req(i->block).length - m_received_in_piece;
|
2009-03-17 10:34:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-23 17:26:24 +01:00
|
|
|
outstanding_bytes += t->to_req(i->block).length;
|
2009-03-17 10:34:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//if (p && p->bytes_downloaded < p->full_block_bytes) TORRENT_ASSERT(in_download_queue);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(m_outstanding_bytes == outstanding_bytes);
|
|
|
|
}
|
2013-03-24 19:00:58 +01:00
|
|
|
|
2008-01-02 04:18:29 +01:00
|
|
|
std::set<piece_block> unique;
|
2008-07-07 14:04:06 +02:00
|
|
|
std::transform(m_download_queue.begin(), m_download_queue.end()
|
|
|
|
, std::inserter(unique, unique.begin()), boost::bind(&pending_block::block, _1));
|
2009-12-25 17:52:57 +01:00
|
|
|
std::transform(m_request_queue.begin(), m_request_queue.end()
|
|
|
|
, std::inserter(unique, unique.begin()), boost::bind(&pending_block::block, _1));
|
2008-01-02 04:18:29 +01:00
|
|
|
TORRENT_ASSERT(unique.size() == m_download_queue.size() + m_request_queue.size());
|
2007-04-12 12:21:55 +02:00
|
|
|
if (m_peer_info)
|
2007-08-21 19:45:28 +02:00
|
|
|
{
|
2008-04-07 10:15:31 +02:00
|
|
|
TORRENT_ASSERT(m_peer_info->prev_amount_upload == 0);
|
|
|
|
TORRENT_ASSERT(m_peer_info->prev_amount_download == 0);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_peer_info->connection == this
|
2007-04-12 12:21:55 +02:00
|
|
|
|| m_peer_info->connection == 0);
|
2007-08-21 19:45:28 +02:00
|
|
|
|
|
|
|
if (m_peer_info->optimistically_unchoked)
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!is_choked());
|
2007-08-21 19:45:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
TORRENT_ASSERT(m_have_piece.count() == m_num_pieces);
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (!t)
|
|
|
|
{
|
2008-07-12 19:51:59 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
// since this connection doesn't have a torrent reference
|
|
|
|
// no torrent should have a reference to this connection either
|
|
|
|
for (aux::session_impl::torrent_map::const_iterator i = m_ses.m_torrents.begin()
|
|
|
|
, end(m_ses.m_torrents.end()); i != end; ++i)
|
|
|
|
TORRENT_ASSERT(!i->second->has_peer((peer_connection*)this));
|
2008-07-12 19:51:59 +02:00
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-10-31 10:48:20 +01:00
|
|
|
|
2008-08-29 19:21:56 +02:00
|
|
|
if (t->ready_for_connections() && m_initialized)
|
2008-09-25 22:12:53 +02:00
|
|
|
TORRENT_ASSERT(t->torrent_file().num_pieces() == int(m_have_piece.size()));
|
2008-08-29 19:21:56 +02:00
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
// in share mode we don't close redundant connections
|
|
|
|
if (m_ses.settings().close_redundant_connections && !t->share_mode())
|
2008-07-18 12:03:42 +02:00
|
|
|
{
|
2013-11-02 04:26:53 +01:00
|
|
|
bool ok_to_disconnect =
|
|
|
|
can_disconnect(error_code(errors::upload_upload_connection, get_libtorrent_category()))
|
|
|
|
|| can_disconnect(error_code(errors::uninteresting_upload_peer, get_libtorrent_category()))
|
|
|
|
|| can_disconnect(error_code(errors::too_many_requests_when_choked, get_libtorrent_category()))
|
|
|
|
|| can_disconnect(error_code(errors::timed_out_no_interest, get_libtorrent_category()))
|
|
|
|
|| can_disconnect(error_code(errors::timed_out_no_request, get_libtorrent_category()))
|
|
|
|
|| can_disconnect(error_code(errors::timed_out_inactivity, get_libtorrent_category()));
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
// make sure upload only peers are disconnected
|
2012-02-09 08:46:23 +01:00
|
|
|
if (t->is_upload_only()
|
|
|
|
&& m_upload_only
|
|
|
|
&& t->valid_metadata()
|
2013-11-02 04:26:53 +01:00
|
|
|
&& has_metadata()
|
|
|
|
&& ok_to_disconnect)
|
2011-08-13 20:46:52 +02:00
|
|
|
TORRENT_ASSERT(m_disconnect_started || t->graceful_pause() || t->has_error());
|
2012-02-09 08:46:23 +01:00
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
if (m_upload_only
|
|
|
|
&& !m_interesting
|
|
|
|
&& m_bitfield_received
|
2012-02-09 08:46:23 +01:00
|
|
|
&& t->are_files_checked()
|
|
|
|
&& t->valid_metadata()
|
2013-11-02 04:26:53 +01:00
|
|
|
&& has_metadata()
|
|
|
|
&& ok_to_disconnect)
|
2008-07-18 12:03:42 +02:00
|
|
|
TORRENT_ASSERT(m_disconnect_started);
|
|
|
|
}
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
if (!m_disconnect_started && m_initialized && m_ses.settings().close_redundant_connections)
|
2008-08-29 19:21:56 +02:00
|
|
|
{
|
|
|
|
// none of this matters if we're disconnecting anyway
|
2011-04-10 01:57:56 +02:00
|
|
|
if (t->is_upload_only())
|
2011-08-13 20:46:52 +02:00
|
|
|
TORRENT_ASSERT(!m_interesting || t->graceful_pause() || t->has_error());
|
2008-08-29 19:21:56 +02:00
|
|
|
if (is_seed())
|
|
|
|
TORRENT_ASSERT(m_upload_only);
|
|
|
|
}
|
2008-07-18 12:03:42 +02:00
|
|
|
|
2013-12-05 08:42:32 +01:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-03-12 09:36:22 +01:00
|
|
|
if (t->has_picker())
|
|
|
|
{
|
2010-05-01 18:17:37 +02:00
|
|
|
std::map<piece_block, peer_count_t> num_requests;
|
2008-03-12 09:36:22 +01:00
|
|
|
for (torrent::const_peer_iterator i = t->begin(); i != t->end(); ++i)
|
|
|
|
{
|
|
|
|
// make sure this peer is not a dangling pointer
|
2008-07-12 19:51:59 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-03-12 09:36:22 +01:00
|
|
|
TORRENT_ASSERT(m_ses.has_peer(*i));
|
2008-07-12 19:51:59 +02:00
|
|
|
#endif
|
2008-03-12 09:36:22 +01:00
|
|
|
peer_connection const& p = *(*i);
|
2009-12-25 17:52:57 +01:00
|
|
|
for (std::vector<pending_block>::const_iterator i = p.request_queue().begin()
|
2008-03-12 09:36:22 +01:00
|
|
|
, end(p.request_queue().end()); i != end; ++i)
|
2010-05-01 18:17:37 +02:00
|
|
|
{
|
|
|
|
++num_requests[i->block].num_peers;
|
|
|
|
++num_requests[i->block].num_peers_with_timeouts;
|
2010-05-13 08:29:33 +02:00
|
|
|
++num_requests[i->block].num_peers_with_nowant;
|
2010-10-02 23:01:11 +02:00
|
|
|
++num_requests[i->block].num_not_requested;
|
2010-10-04 05:26:42 +02:00
|
|
|
// num_requests[i->block].peers.push_back(&p);
|
2010-05-01 18:17:37 +02:00
|
|
|
}
|
2009-03-17 10:34:44 +01:00
|
|
|
for (std::vector<pending_block>::const_iterator i = p.download_queue().begin()
|
2008-03-12 09:36:22 +01:00
|
|
|
, end(p.download_queue().end()); i != end; ++i)
|
2010-05-01 18:17:37 +02:00
|
|
|
{
|
|
|
|
if (!i->not_wanted && !i->timed_out) ++num_requests[i->block].num_peers;
|
2010-05-13 08:29:33 +02:00
|
|
|
if (i->timed_out) ++num_requests[i->block].num_peers_with_timeouts;
|
|
|
|
if (i->not_wanted) ++num_requests[i->block].num_peers_with_nowant;
|
2010-10-04 05:26:42 +02:00
|
|
|
// num_requests[i->block].peers.push_back(&p);
|
2010-05-01 18:17:37 +02:00
|
|
|
}
|
2008-03-12 09:36:22 +01:00
|
|
|
}
|
2010-05-01 18:17:37 +02:00
|
|
|
for (std::map<piece_block, peer_count_t>::iterator i = num_requests.begin()
|
2008-03-12 09:36:22 +01:00
|
|
|
, end(num_requests.end()); i != end; ++i)
|
|
|
|
{
|
2009-03-17 10:34:44 +01:00
|
|
|
piece_block b = i->first;
|
2010-10-02 23:01:11 +02:00
|
|
|
peer_count_t const& pc = i->second;
|
|
|
|
int count = pc.num_peers;
|
|
|
|
int count_with_timeouts = pc.num_peers_with_timeouts;
|
|
|
|
int count_with_nowant = pc.num_peers_with_nowant;
|
2011-02-21 06:24:41 +01:00
|
|
|
(void)count_with_timeouts;
|
|
|
|
(void)count_with_nowant;
|
2013-10-20 09:10:09 +02:00
|
|
|
int picker_count = t->picker().num_peers(b);
|
|
|
|
if (!t->picker().is_downloaded(b))
|
2009-03-17 10:34:44 +01:00
|
|
|
TORRENT_ASSERT(picker_count == count);
|
2008-03-12 09:36:22 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-05 08:42:32 +01:00
|
|
|
|
2012-03-24 02:29:31 +01:00
|
|
|
if (m_peer_info && type() == bittorrent_connection)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
2008-07-30 08:42:08 +02:00
|
|
|
policy::const_iterator i = t->get_policy().begin_peer();
|
|
|
|
policy::const_iterator end = t->get_policy().end_peer();
|
|
|
|
for (; i != end; ++i)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
2009-05-07 00:36:24 +02:00
|
|
|
if (*i == m_peer_info) break;
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
2008-07-30 08:42:08 +02:00
|
|
|
TORRENT_ASSERT(i != end);
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
2013-10-20 09:10:09 +02:00
|
|
|
#endif
|
2007-10-18 06:18:09 +02:00
|
|
|
if (t->has_picker() && !t->is_aborted())
|
2007-10-18 02:32:16 +02:00
|
|
|
{
|
|
|
|
// make sure that pieces that have completed the download
|
|
|
|
// of all their blocks are in the disk io thread's queue
|
|
|
|
// to be checked.
|
|
|
|
const std::vector<piece_picker::downloading_piece>& dl_queue
|
|
|
|
= t->picker().get_download_queue();
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i =
|
|
|
|
dl_queue.begin(); i != dl_queue.end(); ++i)
|
|
|
|
{
|
|
|
|
const int blocks_per_piece = t->picker().blocks_in_piece(i->index);
|
|
|
|
|
|
|
|
bool complete = true;
|
|
|
|
for (int j = 0; j < blocks_per_piece; ++j)
|
|
|
|
{
|
|
|
|
if (i->info[j].state == piece_picker::block_info::state_finished)
|
|
|
|
continue;
|
|
|
|
complete = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-16 04:34:56 +01:00
|
|
|
|
2006-12-18 02:24:35 +01:00
|
|
|
// extremely expensive invariant check
|
|
|
|
/*
|
2006-12-16 04:34:56 +01:00
|
|
|
if (!t->is_seed())
|
|
|
|
{
|
|
|
|
piece_picker& p = t->picker();
|
|
|
|
const std::vector<piece_picker::downloading_piece>& dlq = p.get_download_queue();
|
|
|
|
const int blocks_per_piece = static_cast<int>(
|
|
|
|
t->torrent_file().piece_length() / t->block_size());
|
|
|
|
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::const_iterator i =
|
|
|
|
dlq.begin(); i != dlq.end(); ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < blocks_per_piece; ++j)
|
|
|
|
{
|
|
|
|
if (std::find(m_request_queue.begin(), m_request_queue.end()
|
|
|
|
, piece_block(i->index, j)) != m_request_queue.end()
|
|
|
|
||
|
|
|
|
std::find(m_download_queue.begin(), m_download_queue.end()
|
|
|
|
, piece_block(i->index, j)) != m_download_queue.end())
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->info[j].peer == m_remote);
|
2006-12-16 04:34:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->info[j].peer != m_remote || i->info[j].finished);
|
2006-12-16 04:34:56 +01:00
|
|
|
}
|
2006-12-16 04:36:33 +01:00
|
|
|
}
|
2006-12-16 04:34:56 +01:00
|
|
|
}
|
|
|
|
}
|
2006-12-18 02:24:35 +01:00
|
|
|
*/
|
2003-11-05 00:27:06 +01:00
|
|
|
}
|
2004-01-25 19:18:36 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-04-27 02:27:37 +02:00
|
|
|
peer_connection::peer_speed_t peer_connection::peer_speed()
|
|
|
|
{
|
|
|
|
shared_ptr<torrent> t = m_torrent.lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-04-27 02:27:37 +02:00
|
|
|
|
2007-04-27 18:26:30 +02:00
|
|
|
int download_rate = int(statistics().download_payload_rate());
|
|
|
|
int torrent_download_rate = int(t->statistics().download_payload_rate());
|
2007-04-27 02:27:37 +02:00
|
|
|
|
|
|
|
if (download_rate > 512 && download_rate > torrent_download_rate / 16)
|
|
|
|
m_speed = fast;
|
|
|
|
else if (download_rate > 4096 && download_rate > torrent_download_rate / 64)
|
|
|
|
m_speed = medium;
|
|
|
|
else if (download_rate < torrent_download_rate / 15 && m_speed == fast)
|
|
|
|
m_speed = medium;
|
2008-09-06 23:04:57 +02:00
|
|
|
else
|
2007-04-27 02:27:37 +02:00
|
|
|
m_speed = slow;
|
|
|
|
|
|
|
|
return m_speed;
|
|
|
|
}
|
2004-02-01 14:48:30 +01:00
|
|
|
|
2004-01-04 05:29:13 +01:00
|
|
|
void peer_connection::keep_alive()
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-10-10 09:08:46 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2004-01-25 19:18:36 +01:00
|
|
|
INVARIANT_CHECK;
|
2008-10-10 09:08:46 +02:00
|
|
|
#endif
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
time_duration d;
|
|
|
|
d = time_now() - m_last_sent;
|
|
|
|
if (total_seconds(d) < m_timeout / 2) return;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_connecting) return;
|
2007-06-06 02:41:20 +02:00
|
|
|
if (in_handshake()) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// if the last send has not completed yet, do not send a keep
|
|
|
|
// alive
|
2011-10-27 10:42:51 +02:00
|
|
|
if (m_channel_state[upload_channel] & peer_info::bw_network) return;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-19 20:40:32 +01:00
|
|
|
peer_log("==> KEEPALIVE");
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
2007-10-22 06:56:09 +02:00
|
|
|
|
2007-08-03 10:19:10 +02:00
|
|
|
m_last_sent = time_now();
|
2006-04-25 23:04:48 +02:00
|
|
|
write_keepalive();
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2004-01-12 21:31:27 +01:00
|
|
|
|
|
|
|
bool peer_connection::is_seed() const
|
|
|
|
{
|
2007-05-27 00:27:40 +02:00
|
|
|
// if m_num_pieces == 0, we probably don't have the
|
2004-10-18 00:23:08 +02:00
|
|
|
// metadata yet.
|
2008-08-29 19:21:56 +02:00
|
|
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
|
|
|
return m_num_pieces == (int)m_have_piece.size() && m_num_pieces > 0 && t && t->valid_metadata();
|
2004-01-12 21:31:27 +01:00
|
|
|
}
|
2009-11-24 19:49:59 +01:00
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
void peer_connection::set_share_mode(bool u)
|
|
|
|
{
|
|
|
|
// if the peer is a seed, ignore share mode messages
|
|
|
|
if (is_seed()) return;
|
|
|
|
|
|
|
|
m_share_mode = u;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
void peer_connection::set_upload_only(bool u)
|
|
|
|
{
|
2009-12-05 17:46:10 +01:00
|
|
|
// if the peer is a seed, don't allow setting
|
|
|
|
// upload_only to false
|
2010-12-24 01:37:01 +01:00
|
|
|
if (m_upload_only || is_seed()) return;
|
2009-12-05 17:46:10 +01:00
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
m_upload_only = u;
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
t->get_policy().set_seed(m_peer_info, u);
|
|
|
|
disconnect_if_redundant();
|
|
|
|
}
|
|
|
|
|
2004-01-04 06:53:01 +01:00
|
|
|
}
|