2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2003-2014, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
#define TORRENT_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2004-01-07 01:48:02 +01:00
|
|
|
#include <string>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2006-01-11 02:32:26 +01:00
|
|
|
#include "libtorrent/debug.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#endif
|
2006-01-11 02:32:26 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/smart_ptr.hpp>
|
2006-04-25 23:04:48 +02:00
|
|
|
#include <boost/weak_ptr.hpp>
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/intrusive_ptr.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/array.hpp>
|
2003-12-09 19:09:34 +01:00
|
|
|
#include <boost/optional.hpp>
|
2004-01-20 23:59:21 +01:00
|
|
|
#include <boost/cstdint.hpp>
|
2007-09-29 18:14:03 +02:00
|
|
|
#include <boost/pool/pool.hpp>
|
2010-01-20 04:01:39 +01:00
|
|
|
#include <boost/aligned_storage.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2005-09-14 21:33:16 +02:00
|
|
|
#include "libtorrent/buffer.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
|
|
|
#include "libtorrent/stat.hpp"
|
2004-01-07 01:48:02 +01:00
|
|
|
#include "libtorrent/alert.hpp"
|
2004-03-14 21:01:05 +01:00
|
|
|
#include "libtorrent/peer_request.hpp"
|
|
|
|
#include "libtorrent/piece_block_progress.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-12-31 10:41:50 +01:00
|
|
|
#include "libtorrent/bandwidth_limit.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/socket_type_fwd.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2007-09-29 18:14:03 +02:00
|
|
|
#include "libtorrent/chained_buffer.hpp"
|
2008-04-10 12:03:23 +02:00
|
|
|
#include "libtorrent/disk_buffer_holder.hpp"
|
2008-05-28 04:35:02 +02:00
|
|
|
#include "libtorrent/bitfield.hpp"
|
2009-09-16 06:41:35 +02:00
|
|
|
#include "libtorrent/bandwidth_socket.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/socket_type_fwd.hpp"
|
2009-12-09 10:55:19 +01:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2010-11-29 02:33:05 +01:00
|
|
|
#include "libtorrent/sliding_average.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/peer_class.hpp"
|
|
|
|
#include "libtorrent/peer_class_set.hpp"
|
|
|
|
#include "libtorrent/aux_/session_settings.hpp"
|
|
|
|
#include "libtorrent/disk_observer.hpp"
|
|
|
|
#include "libtorrent/connection_interface.hpp"
|
|
|
|
#include "libtorrent/peer_connection_interface.hpp"
|
|
|
|
#include "libtorrent/piece_picker.hpp" // for piece_block
|
|
|
|
#include "libtorrent/socket.hpp" // for tcp::endpoint
|
2013-04-09 04:38:11 +02:00
|
|
|
#include "libtorrent/io_service_fwd.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
class torrent;
|
2009-11-26 06:45:43 +01:00
|
|
|
struct peer_info;
|
|
|
|
struct disk_io_job;
|
2014-07-06 21:18:00 +02:00
|
|
|
struct disk_interface;
|
|
|
|
struct torrent_peer;
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
struct peer_plugin;
|
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace aux
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
struct session_interface;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-07-07 14:04:06 +02:00
|
|
|
struct pending_block
|
2003-12-01 22:27:27 +01:00
|
|
|
{
|
2009-03-17 10:34:44 +01:00
|
|
|
pending_block(piece_block const& b)
|
2014-07-28 09:52:59 +02:00
|
|
|
: block(b), send_buffer_offset(-1), not_wanted(false)
|
|
|
|
, timed_out(false), busy(false), receiving(false)
|
2014-07-06 21:18:00 +02:00
|
|
|
{}
|
2010-05-13 08:29:33 +02:00
|
|
|
|
|
|
|
piece_block block;
|
2009-03-17 10:34:44 +01:00
|
|
|
|
2014-07-28 09:52:59 +02:00
|
|
|
// the time we sent this request. This is used to track the round-trip
|
|
|
|
// time of receiving the piece. This is not initialized until this
|
|
|
|
// pending_block is inserted in the download queue (i.e. not the
|
|
|
|
// requst_queue)
|
|
|
|
ptime request_time;
|
|
|
|
|
|
|
|
// the number of bytes into the send buffer this request is. Every time
|
|
|
|
// some portion of the send buffer is transmitted, this offset is
|
|
|
|
// decremented by the number of bytes sent. once this drops below 0, the
|
|
|
|
// request_time field is set to the current time.
|
|
|
|
// if the request has not been written to the send buffer, this field
|
|
|
|
// remoains -1.
|
|
|
|
int send_buffer_offset;
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
// if any of these are set to true, this block
|
|
|
|
// is not allocated
|
|
|
|
// in the piece picker anymore, and open for
|
|
|
|
// other peers to pick. This may be caused by
|
|
|
|
// it either timing out or being received
|
|
|
|
// unexpectedly from the peer
|
|
|
|
bool not_wanted:1;
|
|
|
|
bool timed_out:1;
|
2009-12-25 17:52:57 +01:00
|
|
|
|
|
|
|
// the busy flag is set if the block was
|
|
|
|
// requested from another peer when this
|
|
|
|
// request was queued. We only allow a single
|
|
|
|
// busy request at a time in each peer's queue
|
|
|
|
bool busy:1;
|
2009-03-17 10:34:44 +01:00
|
|
|
|
2014-07-28 09:52:59 +02:00
|
|
|
// this is true when we first start to receive the resopnse for this
|
|
|
|
// request. The first time we read the message header for the piece
|
|
|
|
// response is when we calculate the RTT for this request.
|
|
|
|
bool receiving:1;
|
|
|
|
|
2009-04-25 10:19:21 +02:00
|
|
|
bool operator==(pending_block const& b)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
return b.block == block
|
|
|
|
&& b.not_wanted == not_wanted
|
|
|
|
&& b.timed_out == timed_out;
|
2009-04-25 10:19:21 +02:00
|
|
|
}
|
2008-07-07 14:04:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct has_block
|
|
|
|
{
|
|
|
|
has_block(piece_block const& b): block(b) {}
|
|
|
|
piece_block const& block;
|
|
|
|
bool operator()(pending_block const& pb) const
|
|
|
|
{ return pb.block == block; }
|
2003-12-01 22:27:27 +01:00
|
|
|
};
|
|
|
|
|
2014-07-14 06:32:41 +02:00
|
|
|
// argument pack passed to peer_connection constructor
|
|
|
|
struct peer_connection_args
|
|
|
|
{
|
|
|
|
aux::session_interface* ses;
|
|
|
|
aux::session_settings const* sett;
|
|
|
|
counters* stats_counters;
|
|
|
|
buffer_allocator_interface* allocator;
|
|
|
|
disk_interface* disk_thread;
|
|
|
|
io_service* ios;
|
|
|
|
boost::weak_ptr<torrent> tor;
|
|
|
|
boost::shared_ptr<socket_type> s;
|
2014-09-28 06:05:44 +02:00
|
|
|
tcp::endpoint endp;
|
2014-07-14 06:32:41 +02:00
|
|
|
torrent_peer* peerinfo;
|
|
|
|
};
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// internal
|
|
|
|
inline void nop(char*, void*, block_cache_reference) {}
|
|
|
|
|
|
|
|
struct peer_connection_hot_members
|
|
|
|
{
|
2014-07-13 06:56:53 +02:00
|
|
|
// if tor is set, this is an outgoing connection
|
2014-07-06 21:18:00 +02:00
|
|
|
peer_connection_hot_members(
|
|
|
|
boost::weak_ptr<torrent> t
|
|
|
|
, aux::session_interface& ses
|
2014-07-13 06:56:53 +02:00
|
|
|
, aux::session_settings const& sett)
|
2014-07-06 21:18:00 +02:00
|
|
|
: m_torrent(t)
|
|
|
|
, m_ses(ses)
|
|
|
|
, m_settings(sett)
|
|
|
|
, m_disconnecting(false)
|
2014-07-13 06:56:53 +02:00
|
|
|
, m_connecting(!t.expired())
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_endgame_mode(false)
|
|
|
|
, m_snubbed(false)
|
|
|
|
, m_interesting(false)
|
|
|
|
, m_choked(true)
|
|
|
|
, m_corked(false)
|
|
|
|
, m_ignore_stats(false)
|
|
|
|
, m_recv_pos(0)
|
|
|
|
, m_packet_size(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// the pieces the other end have
|
|
|
|
bitfield m_have_piece;
|
|
|
|
|
|
|
|
// this is the torrent this connection is
|
|
|
|
// associated with. If the connection is an
|
|
|
|
// incoming connection, this is set to zero
|
|
|
|
// until the info_hash is received. Then it's
|
|
|
|
// set to the torrent it belongs to.
|
|
|
|
|
|
|
|
// TODO: make this a raw pointer (to save size in
|
|
|
|
// the first cache line) and make the constructor
|
|
|
|
// take a raw pointer. torrent objects should always
|
|
|
|
// outlive their peers
|
|
|
|
boost::weak_ptr<torrent> m_torrent;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// a back reference to the session
|
|
|
|
// the peer belongs to.
|
|
|
|
aux::session_interface& m_ses;
|
|
|
|
|
|
|
|
// settings that apply to this peer
|
|
|
|
aux::session_settings const& m_settings;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// this is true if this connection has been added
|
|
|
|
// to the list of connections that will be closed.
|
|
|
|
bool m_disconnecting:1;
|
|
|
|
|
|
|
|
// this is true until this socket has become
|
|
|
|
// writable for the first time (i.e. the
|
|
|
|
// connection completed). While connecting
|
|
|
|
// the timeout will not be triggered. This is
|
|
|
|
// because windows XP SP2 may delay connection
|
|
|
|
// attempts, which means that the connection
|
|
|
|
// may not even have been attempted when the
|
|
|
|
// time out is reached.
|
|
|
|
bool m_connecting:1;
|
|
|
|
|
|
|
|
// this is set to true if the last time we tried to
|
|
|
|
// pick a piece to download, we could only find
|
|
|
|
// blocks that were already requested from other
|
|
|
|
// peers. In this case, we should not try to pick
|
|
|
|
// another piece until the last one we requested is done
|
|
|
|
bool m_endgame_mode:1;
|
|
|
|
|
|
|
|
// set to true when a piece request times out. The
|
|
|
|
// result is that the desired pending queue size
|
|
|
|
// is set to 1
|
|
|
|
bool m_snubbed:1;
|
|
|
|
|
|
|
|
// the peer has pieces we are interested in
|
|
|
|
bool m_interesting:1;
|
|
|
|
|
|
|
|
// we have choked the upload to the peer
|
|
|
|
bool m_choked:1;
|
|
|
|
|
|
|
|
// when this is set, the peer_connection socket is
|
|
|
|
// corked, similar to the linux TCP feature TCP_CORK.
|
|
|
|
// we won't send anything to the actual socket, just
|
|
|
|
// buffer messages up in the application layer send
|
|
|
|
// buffer, and send it once we're uncorked.
|
|
|
|
bool m_corked:1;
|
|
|
|
|
|
|
|
// when this is set, the transfer stats for this connection
|
|
|
|
// is not included in the torrent or session stats
|
|
|
|
bool m_ignore_stats:1;
|
|
|
|
|
|
|
|
// the byte offset in m_recv_buffer that we have
|
|
|
|
// are passing on to the upper layer. This is
|
|
|
|
// always <= m_recv_end
|
|
|
|
int m_recv_pos:24;
|
|
|
|
|
|
|
|
// the size (in bytes) of the bittorrent message
|
|
|
|
// we're currently receiving
|
|
|
|
int m_packet_size;
|
|
|
|
};
|
|
|
|
|
2012-03-20 04:53:07 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT peer_connection
|
2014-07-06 21:18:00 +02:00
|
|
|
: public peer_connection_hot_members
|
|
|
|
, public bandwidth_socket
|
|
|
|
, public peer_class_set
|
|
|
|
, public disk_observer
|
|
|
|
, public connection_interface
|
|
|
|
, public peer_connection_interface
|
|
|
|
, public boost::enable_shared_from_this<peer_connection>
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
friend class invariant_access;
|
2014-07-06 21:18:00 +02:00
|
|
|
friend struct network_thread_pool;
|
|
|
|
friend class torrent;
|
2003-10-23 01:00:57 +02:00
|
|
|
public:
|
|
|
|
|
2009-11-02 21:43:38 +01:00
|
|
|
enum connection_type
|
|
|
|
{
|
|
|
|
bittorrent_connection = 0,
|
|
|
|
url_seed_connection = 1,
|
|
|
|
http_seed_connection = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual int type() const = 0;
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
enum channels
|
|
|
|
{
|
|
|
|
upload_channel,
|
|
|
|
download_channel,
|
|
|
|
num_channels
|
|
|
|
};
|
|
|
|
|
2014-07-14 06:32:41 +02:00
|
|
|
peer_connection(peer_connection_args const& pack);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-03-31 06:46:24 +02:00
|
|
|
// this function is called after it has been constructed and properly
|
|
|
|
// reference counted. It is safe to call self() in this function
|
|
|
|
// and schedule events with references to itself (that is not safe to
|
|
|
|
// do in the constructor).
|
|
|
|
virtual void start();
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
virtual ~peer_connection();
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void set_peer_info(torrent_peer* pi)
|
2009-09-09 18:42:52 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_peer_info == 0 || pi == 0 );
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(pi != NULL || m_disconnect_started);
|
2009-09-09 18:42:52 +02:00
|
|
|
m_peer_info = pi;
|
|
|
|
}
|
2007-04-10 23:23:13 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
torrent_peer* peer_info_struct() const
|
|
|
|
{ return m_peer_info; }
|
|
|
|
|
2012-12-31 07:54:54 +01:00
|
|
|
// this is called when the peer object is created, in case
|
|
|
|
// it was let in by the connections limit slack. This means
|
|
|
|
// the peer needs to, as soon as the handshake is done, either
|
|
|
|
// disconnect itself or another peer.
|
|
|
|
void peer_exceeds_limit()
|
|
|
|
{ m_exceeded_limit = true; }
|
|
|
|
|
|
|
|
// this is called if this peer causes another peer
|
|
|
|
// to be disconnected, in which case it has fulfilled
|
|
|
|
// its requirement.
|
|
|
|
void peer_disconnected_other()
|
|
|
|
{ m_exceeded_limit = false; }
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
enum peer_speed_t { slow = 1, medium, fast };
|
2007-04-27 02:27:37 +02:00
|
|
|
peer_speed_t peer_speed();
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
void send_allowed_set();
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
void add_extension(boost::shared_ptr<peer_plugin>);
|
2010-11-29 02:33:05 +01:00
|
|
|
peer_plugin const* find_plugin(char const* type);
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
// this function is called once the torrent associated
|
|
|
|
// with this peer connection has retrieved the meta-
|
|
|
|
// data. If the torrent was spawned with metadata
|
|
|
|
// this is called from the constructor.
|
|
|
|
void init();
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
// this is called when the metadata is retrieved
|
|
|
|
// and the files has been checked
|
2008-08-29 19:21:56 +02:00
|
|
|
virtual void on_metadata() {};
|
|
|
|
|
|
|
|
void on_metadata_impl();
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void picker_options(int o)
|
|
|
|
{ m_picker_options = o; }
|
2007-04-02 23:06:00 +02:00
|
|
|
|
2007-09-03 23:16:24 +02:00
|
|
|
int prefer_whole_pieces() const
|
|
|
|
{
|
2007-09-10 10:07:18 +02:00
|
|
|
if (on_parole()) return 1;
|
2007-09-03 23:16:24 +02:00
|
|
|
return m_prefer_whole_pieces;
|
|
|
|
}
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool on_parole() const;
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int picker_options() const;
|
|
|
|
|
2007-09-03 23:16:24 +02:00
|
|
|
void prefer_whole_pieces(int num)
|
|
|
|
{ m_prefer_whole_pieces = num; }
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
bool request_large_blocks() const
|
|
|
|
{ return m_request_large_blocks; }
|
|
|
|
|
|
|
|
void request_large_blocks(bool b)
|
|
|
|
{ m_request_large_blocks = b; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void set_endgame(bool b);
|
2010-12-18 11:19:34 +01:00
|
|
|
bool endgame() const { return m_endgame_mode; }
|
|
|
|
|
2008-11-06 09:34:56 +01:00
|
|
|
bool no_download() const { return m_no_download; }
|
|
|
|
void no_download(bool b) { m_no_download = b; }
|
|
|
|
|
2010-07-15 08:27:44 +02:00
|
|
|
bool ignore_stats() const { return m_ignore_stats; }
|
|
|
|
void ignore_stats(bool b) { m_ignore_stats = b; }
|
|
|
|
|
2012-12-31 07:54:54 +01:00
|
|
|
boost::uint32_t peer_rank() const;
|
|
|
|
|
2007-10-15 07:03:29 +02:00
|
|
|
void fast_reconnect(bool r);
|
2007-10-04 23:26:50 +02:00
|
|
|
bool fast_reconnect() const { return m_fast_reconnect; }
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is called when we receive a new piece
|
|
|
|
// (and it has passed the hash check)
|
|
|
|
void received_piece(int index);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// this adds an announcement in the announcement queue
|
|
|
|
// it will let the peer know that we have the given piece
|
2004-03-11 15:56:48 +01:00
|
|
|
void announce_piece(int index);
|
2008-12-08 07:36:22 +01:00
|
|
|
|
|
|
|
// this will tell the peer to announce the given piece
|
|
|
|
// and only allow it to request that piece
|
2012-04-23 07:48:46 +02:00
|
|
|
void superseed_piece(int replace_piece, int new_piece);
|
|
|
|
bool super_seeded_piece(int index) const
|
|
|
|
{
|
|
|
|
return m_superseed_piece[0] == index
|
|
|
|
|| m_superseed_piece[1] == index;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// tells if this connection has data it want to send
|
2004-03-28 19:45:37 +02:00
|
|
|
// and has enough upload bandwidth quota left to send it.
|
|
|
|
bool can_write() const;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool can_read();
|
2004-01-12 21:31:27 +01:00
|
|
|
|
|
|
|
bool is_seed() const;
|
2010-09-05 18:01:36 +02:00
|
|
|
int num_have_pieces() const { return m_num_pieces; }
|
|
|
|
|
|
|
|
void set_share_mode(bool m);
|
|
|
|
bool share_mode() const { return m_share_mode; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
void set_upload_only(bool u);
|
2008-05-18 07:59:47 +02:00
|
|
|
bool upload_only() const { return m_upload_only; }
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
void set_holepunch_mode()
|
|
|
|
{
|
|
|
|
m_holepunch_mode = true;
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-01 05:22:03 +01:00
|
|
|
peer_log("*** HOLEPUNCH MODE ***");
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// will send a keep-alive message to the peer
|
|
|
|
void keep_alive();
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
peer_id const& pid() const { return m_peer_id; }
|
2010-03-06 08:16:39 +01:00
|
|
|
void set_pid(const peer_id& peer_id) { m_peer_id = peer_id; }
|
2004-03-11 15:56:48 +01:00
|
|
|
bool has_piece(int i) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
std::vector<pending_block> const& download_queue() const;
|
2009-12-25 17:52:57 +01:00
|
|
|
std::vector<pending_block> const& request_queue() const;
|
2009-05-03 22:21:24 +02:00
|
|
|
std::vector<peer_request> const& upload_queue() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2010-10-30 10:36:18 +02:00
|
|
|
void clear_request_queue();
|
|
|
|
|
2009-02-17 03:33:45 +01:00
|
|
|
// estimate of how long it will take until we have
|
|
|
|
// received all piece requests that we have sent
|
|
|
|
// if extra_bytes is specified, it will include those
|
|
|
|
// bytes as if they've been requested
|
|
|
|
time_duration download_queue_time(int extra_bytes = 0) const;
|
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
bool is_interesting() const { return m_interesting; }
|
|
|
|
bool is_choked() const { return m_choked; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
bool is_peer_interested() const { return m_peer_interested; }
|
|
|
|
bool has_peer_choked() const { return m_peer_choked; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-09-22 05:47:43 +02:00
|
|
|
void choke_this_peer();
|
|
|
|
void maybe_unchoke_this_peer();
|
|
|
|
|
2007-03-21 03:09:50 +01:00
|
|
|
void update_interest();
|
|
|
|
|
2007-05-25 21:42:10 +02:00
|
|
|
virtual void get_peer_info(peer_info& p) const;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// returns the torrent this connection is a part of
|
|
|
|
// may be zero if the connection is an incoming connection
|
|
|
|
// and it hasn't received enough information to determine
|
|
|
|
// which torrent it should be associated with
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::weak_ptr<torrent> associated_torrent() const
|
|
|
|
{ return m_torrent; }
|
2004-01-08 14:03:38 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
stat const& statistics() const { return m_statistics; }
|
2004-03-11 15:56:48 +01:00
|
|
|
void add_stat(size_type downloaded, size_type uploaded);
|
2014-07-06 21:18:00 +02:00
|
|
|
void sent_bytes(int bytes_payload, int bytes_protocol);
|
|
|
|
void received_bytes(int bytes_payload, int bytes_protocol);
|
|
|
|
void trancieve_ip_packet(int bytes, bool ipv6);
|
|
|
|
void sent_syn(bool ipv6);
|
|
|
|
void received_synack(bool ipv6);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// is called once every second by the main loop
|
2009-07-19 06:59:27 +02:00
|
|
|
void second_tick(int tick_interval_ms);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-07-07 14:04:06 +02:00
|
|
|
void timeout_requests();
|
|
|
|
|
2007-04-23 23:36:21 +02:00
|
|
|
boost::shared_ptr<socket_type> get_socket() const { return m_socket; }
|
2006-04-25 23:04:48 +02:00
|
|
|
tcp::endpoint const& remote() const { return m_remote; }
|
2014-07-06 21:18:00 +02:00
|
|
|
tcp::endpoint local_endpoint() const { return m_local; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
bitfield const& get_bitfield() const;
|
2007-08-14 19:47:48 +02:00
|
|
|
std::vector<int> const& allowed_fast();
|
2007-09-01 09:38:10 +02:00
|
|
|
std::vector<int> const& suggested_pieces() const { return m_suggested_pieces; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-05-12 07:17:11 +02:00
|
|
|
ptime connected_time() const { return m_connect; }
|
|
|
|
ptime last_received() const { return m_last_receive; }
|
|
|
|
|
2004-01-20 12:01:50 +01:00
|
|
|
// this will cause this peer_connection to be disconnected.
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void disconnect(error_code const& ec
|
|
|
|
, peer_connection_interface::operation_t op, int error = 0);
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// called when a connect attempt fails (not when an
|
|
|
|
// established connection fails)
|
|
|
|
void connect_failed(error_code const& e);
|
2004-03-11 15:56:48 +01:00
|
|
|
bool is_disconnecting() const { return m_disconnecting; }
|
2003-11-09 19:17:09 +01:00
|
|
|
|
2005-11-02 17:28:39 +01:00
|
|
|
// this is called when the connection attempt has succeeded
|
|
|
|
// and the peer_connection is supposed to set m_connecting
|
|
|
|
// to false, and stop monitor writability
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_connection_complete(error_code const& e);
|
2005-11-02 17:28:39 +01:00
|
|
|
|
|
|
|
// returns true if this connection is still waiting to
|
|
|
|
// finish the connection attempt
|
|
|
|
bool is_connecting() const { return m_connecting; }
|
|
|
|
|
|
|
|
// returns true if the socket of this peer hasn't been
|
|
|
|
// attempted to connect yet (i.e. it's queued for
|
|
|
|
// connection attempt).
|
|
|
|
bool is_queued() const { return m_queued; }
|
2014-09-21 01:57:59 +02:00
|
|
|
|
|
|
|
// returns true if this peer has successfully completed its connection
|
|
|
|
// attempt to the remote end.
|
|
|
|
bool is_connected() const { return m_connected; }
|
2005-11-02 17:28:39 +01:00
|
|
|
|
|
|
|
// called when it's time for this peer_conncetion to actually
|
|
|
|
// initiate the tcp connection. This may be postponed until
|
|
|
|
// the library isn't using up the limitation of half-open
|
|
|
|
// tcp connections.
|
2014-07-06 21:18:00 +02:00
|
|
|
// implements connection_interface
|
|
|
|
void on_allow_connect(int ticket);
|
2005-11-02 17:28:39 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// implements connection_interface. Called by the connection_queue
|
|
|
|
void on_connect_timeout();
|
|
|
|
|
2004-03-22 15:56:32 +01:00
|
|
|
// This is called for every peer right after the upload
|
|
|
|
// bandwidth has been distributed among them
|
2006-04-25 23:04:48 +02:00
|
|
|
// It will reset the used bandwidth to 0.
|
2004-03-22 15:56:32 +01:00
|
|
|
void reset_upload_quota();
|
2004-02-23 23:54:54 +01:00
|
|
|
|
2004-03-11 15:56:48 +01:00
|
|
|
// trust management.
|
2012-09-29 19:46:41 +02:00
|
|
|
virtual void received_valid_data(int index);
|
|
|
|
// returns false if the peer should not be
|
|
|
|
// disconnected
|
|
|
|
virtual bool received_invalid_data(int index, bool single_peer);
|
2003-12-01 22:27:27 +01:00
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
// a connection is local if it was initiated by us.
|
|
|
|
// if it was an incoming connection, it is remote
|
2012-02-07 04:46:21 +01:00
|
|
|
bool is_outgoing() const { return m_outgoing; }
|
2004-01-09 11:50:22 +01:00
|
|
|
|
2012-01-04 21:49:54 +01:00
|
|
|
bool received_listen_port() const { return m_received_listen_port; }
|
|
|
|
void received_listen_port()
|
|
|
|
{ m_received_listen_port = true; }
|
|
|
|
|
2007-05-25 19:06:30 +02:00
|
|
|
bool on_local_network() const;
|
2009-03-01 08:20:08 +01:00
|
|
|
bool ignore_unchoke_slots() const;
|
2009-01-28 07:14:21 +01:00
|
|
|
|
2004-03-21 03:03:37 +01:00
|
|
|
bool failed() const { return m_failed; }
|
|
|
|
|
2010-12-18 11:19:34 +01:00
|
|
|
int desired_queue_size() const
|
|
|
|
{
|
|
|
|
// this peer is in end-game mode we only want
|
|
|
|
// one outstanding request
|
2014-08-26 05:14:32 +02:00
|
|
|
return (m_endgame_mode || m_snubbed) ? 1 : m_desired_queue_size;
|
2010-12-18 11:19:34 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-02-09 05:58:16 +01:00
|
|
|
bool bittyrant_unchoke_compare(
|
2014-07-06 21:18:00 +02:00
|
|
|
peer_connection const* p) const;
|
2008-03-10 04:30:01 +01:00
|
|
|
// compares this connection against the given connection
|
|
|
|
// for which one is more eligible for an unchoke.
|
|
|
|
// returns true if this is more eligible
|
2014-07-06 21:18:00 +02:00
|
|
|
bool unchoke_compare(peer_connection const* p) const;
|
2009-04-04 09:55:34 +02:00
|
|
|
bool upload_rate_compare(peer_connection const* p) const;
|
2014-07-06 21:18:00 +02:00
|
|
|
int download_payload_rate() const { return m_statistics.download_payload_rate(); }
|
2008-03-10 04:30:01 +01:00
|
|
|
|
|
|
|
// resets the byte counters that are used to measure
|
|
|
|
// the number of bytes transferred within unchoke cycles
|
|
|
|
void reset_choke_counters();
|
|
|
|
|
2008-07-18 12:03:42 +02:00
|
|
|
// if this peer connection is useless (neither party is
|
|
|
|
// interested in the other), disconnect it
|
2014-07-06 21:18:00 +02:00
|
|
|
// returns true if the connection was disconnected
|
|
|
|
bool disconnect_if_redundant();
|
2008-07-18 12:03:42 +02:00
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
void increase_est_reciprocation_rate();
|
|
|
|
void decrease_est_reciprocation_rate();
|
|
|
|
int est_reciprocation_rate() const { return m_est_reciprocation_rate; }
|
|
|
|
|
2008-02-07 08:09:52 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void peer_log(char const* fmt, ...) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
boost::shared_ptr<logger> m_logger;
|
|
|
|
#endif
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#if defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
|
|
|
|
ptime m_connect_time;
|
|
|
|
ptime m_bitfield_time;
|
|
|
|
ptime m_unchoke_time;
|
|
|
|
#endif
|
|
|
|
|
2004-01-05 00:51:54 +01:00
|
|
|
// the message handlers are called
|
|
|
|
// each time a recv() returns some new
|
|
|
|
// data, the last time it will be called
|
|
|
|
// is when the entire packet has been
|
|
|
|
// received, then it will no longer
|
|
|
|
// be called. i.e. most handlers need
|
|
|
|
// to check how much of the packet they
|
|
|
|
// have received before any processing
|
2006-04-25 23:04:48 +02:00
|
|
|
void incoming_keepalive();
|
|
|
|
void incoming_choke();
|
|
|
|
void incoming_unchoke();
|
|
|
|
void incoming_interested();
|
|
|
|
void incoming_not_interested();
|
|
|
|
void incoming_have(int piece_index);
|
2011-08-07 02:41:13 +02:00
|
|
|
void incoming_dont_have(int piece_index);
|
2008-05-28 04:35:02 +02:00
|
|
|
void incoming_bitfield(bitfield const& bits);
|
2006-04-25 23:04:48 +02:00
|
|
|
void incoming_request(peer_request const& r);
|
2008-04-10 12:03:23 +02:00
|
|
|
void incoming_piece(peer_request const& p, disk_buffer_holder& data);
|
2006-04-25 23:04:48 +02:00
|
|
|
void incoming_piece(peer_request const& p, char const* data);
|
2009-03-17 10:34:44 +01:00
|
|
|
void incoming_piece_fragment(int bytes);
|
|
|
|
void start_receive_piece(peer_request const& r);
|
2006-04-25 23:04:48 +02:00
|
|
|
void incoming_cancel(peer_request const& r);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2013-11-02 04:26:53 +01:00
|
|
|
bool can_disconnect(error_code const& ec) const;
|
2006-04-25 23:04:48 +02:00
|
|
|
void incoming_dht_port(int listen_port);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
void incoming_reject_request(peer_request const& r);
|
|
|
|
void incoming_have_all();
|
|
|
|
void incoming_have_none();
|
|
|
|
void incoming_allowed_fast(int index);
|
2007-09-01 09:38:10 +02:00
|
|
|
void incoming_suggest(int index);
|
2004-01-05 00:51:54 +01:00
|
|
|
|
2012-02-07 10:18:07 +01:00
|
|
|
void set_has_metadata(bool m) { m_has_metadata = m; }
|
|
|
|
bool has_metadata() const { return m_has_metadata; }
|
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
// the following functions appends messages
|
|
|
|
// to the send buffer
|
2009-07-23 06:38:52 +02:00
|
|
|
bool send_choke();
|
2008-08-29 19:21:56 +02:00
|
|
|
bool send_unchoke();
|
2004-01-12 21:31:27 +01:00
|
|
|
void send_interested();
|
|
|
|
void send_not_interested();
|
2010-01-15 17:45:42 +01:00
|
|
|
void send_suggest(int piece);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-07-07 14:04:06 +02:00
|
|
|
void snub_peer();
|
2014-07-06 21:18:00 +02:00
|
|
|
// reject any request in the request
|
|
|
|
// queue from this piece
|
|
|
|
void reject_piece(int index);
|
2008-07-07 14:04:06 +02:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
bool can_request_time_critical() const;
|
|
|
|
|
2014-04-22 06:21:14 +02:00
|
|
|
// returns true if the specified block was actually made time-critical.
|
|
|
|
// if the block was already time-critical, it returns false.
|
|
|
|
bool make_time_critical(piece_block const& block);
|
2009-03-17 10:34:44 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// adds a block to the request queue
|
2009-06-19 00:32:55 +02:00
|
|
|
// returns true if successful, false otherwise
|
2009-12-25 17:52:57 +01:00
|
|
|
enum flags_t { req_time_critical = 1, req_busy = 2 };
|
|
|
|
bool add_request(piece_block const& b, int flags = 0);
|
2009-06-19 00:32:55 +02:00
|
|
|
|
|
|
|
// clears the request queue and sends cancels for all messages
|
|
|
|
// in the download queue
|
|
|
|
void cancel_all_requests();
|
|
|
|
|
2007-02-12 10:20:49 +01:00
|
|
|
// removes a block from the request queue or download queue
|
|
|
|
// sends a cancel message if appropriate
|
|
|
|
// refills the request queue, and possibly ignoring pieces requested
|
|
|
|
// by peers in the ignore list (to avoid recursion)
|
2012-06-06 18:05:25 +02:00
|
|
|
// if force is true, the blocks is also freed from the piece
|
|
|
|
// picker, allowing another peer to request it immediately
|
|
|
|
void cancel_request(piece_block const& b, bool force = false);
|
2006-05-15 00:30:05 +02:00
|
|
|
void send_block_requests();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
void assign_bandwidth(int channel, int amount);
|
2004-01-12 21:31:27 +01:00
|
|
|
|
2014-01-19 21:19:15 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2004-01-25 19:18:36 +01:00
|
|
|
void check_invariant() const;
|
2013-02-25 05:13:46 +01:00
|
|
|
#endif
|
2005-08-18 22:38:03 +02:00
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
// is true until we can be sure that the other end
|
|
|
|
// speaks our protocol (be it bittorrent or http).
|
|
|
|
virtual bool in_handshake() const = 0;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// returns the block currently being
|
|
|
|
// downloaded. And the progress of that
|
|
|
|
// block. If the peer isn't downloading
|
|
|
|
// a piece for the moment, the boost::optional
|
|
|
|
// will be invalid.
|
|
|
|
virtual boost::optional<piece_block_progress>
|
|
|
|
downloading_piece_progress() const
|
|
|
|
{
|
2007-09-29 18:14:03 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2010-12-01 05:22:03 +01:00
|
|
|
peer_log("*** downloading_piece_progress() dispatched to the base class!");
|
2007-09-29 18:14:03 +02:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
return boost::optional<piece_block_progress>();
|
|
|
|
}
|
|
|
|
|
2007-10-31 11:20:05 +01:00
|
|
|
// these functions are virtual to let bt_peer_connection hook into them
|
|
|
|
// and encrypt the content
|
2011-10-24 06:22:53 +02:00
|
|
|
enum message_type_flags { message_type_request = 1 };
|
2011-05-19 04:41:28 +02:00
|
|
|
virtual void send_buffer(char const* begin, int size, int flags = 0
|
|
|
|
, void (*fun)(char*, int, void*) = 0, void* userdata = 0);
|
2007-10-31 11:20:05 +01:00
|
|
|
virtual void setup_send();
|
|
|
|
|
2011-10-24 06:22:53 +02:00
|
|
|
void cork_socket() { TORRENT_ASSERT(!m_corked); m_corked = true; }
|
2014-07-06 21:18:00 +02:00
|
|
|
bool is_corked() const { return m_corked; }
|
2011-10-24 06:22:53 +02:00
|
|
|
void uncork_socket();
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void append_send_buffer(char* buffer, int size
|
|
|
|
, chained_buffer::free_buffer_fun destructor = &nop
|
|
|
|
, void* userdata = NULL, block_cache_reference ref
|
|
|
|
= block_cache_reference(), bool encrypted = false);
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void append_const_send_buffer(char const* buffer, int size
|
|
|
|
, chained_buffer::free_buffer_fun destructor = &nop
|
|
|
|
, void* userdata = NULL, block_cache_reference ref
|
|
|
|
= block_cache_reference());
|
2010-06-18 07:44:08 +02:00
|
|
|
|
2007-05-02 21:47:38 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2007-01-29 08:39:33 +01:00
|
|
|
void set_country(char const* c)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(strlen(c) == 2);
|
2007-01-29 08:39:33 +01:00
|
|
|
m_country[0] = c[0];
|
|
|
|
m_country[1] = c[1];
|
|
|
|
}
|
|
|
|
bool has_country() const { return m_country[0] != 0; }
|
2007-05-02 21:47:38 +02:00
|
|
|
#endif
|
2007-01-29 08:39:33 +01:00
|
|
|
|
2010-10-30 10:36:18 +02:00
|
|
|
int outstanding_bytes() const { return m_outstanding_bytes; }
|
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
int send_buffer_size() const
|
|
|
|
{ return m_send_buffer.size(); }
|
|
|
|
|
|
|
|
int send_buffer_capacity() const
|
|
|
|
{ return m_send_buffer.capacity(); }
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
int packet_size() const { return m_packet_size; }
|
|
|
|
|
|
|
|
bool packet_finished() const
|
|
|
|
{ return m_packet_size <= m_recv_pos; }
|
|
|
|
|
2011-11-27 02:08:28 +01:00
|
|
|
int receive_pos() const { return m_recv_pos; }
|
|
|
|
|
2013-11-26 08:47:48 +01:00
|
|
|
void max_out_request_queue(int s)
|
|
|
|
{ m_max_out_request_queue = s; }
|
|
|
|
int max_out_request_queue() const
|
|
|
|
{ return m_max_out_request_queue; }
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-18 07:04:54 +01:00
|
|
|
bool piece_failed;
|
|
|
|
#endif
|
|
|
|
|
2010-03-19 19:39:51 +01:00
|
|
|
time_t last_seen_complete() const { return m_last_seen_complete; }
|
|
|
|
void set_last_seen_complete(int ago) { m_last_seen_complete = time(0) - ago; }
|
|
|
|
|
2013-06-02 00:33:11 +02:00
|
|
|
size_type uploaded_in_last_round() const
|
|
|
|
{ return m_statistics.total_payload_upload() - m_uploaded_at_last_round; }
|
|
|
|
|
|
|
|
size_type downloaded_in_last_round() const
|
|
|
|
{ return m_statistics.total_payload_download() - m_downloaded_at_last_round; }
|
2009-04-04 09:55:34 +02:00
|
|
|
|
2013-06-02 00:33:11 +02:00
|
|
|
size_type uploaded_since_unchoked() const
|
|
|
|
{ return m_statistics.total_payload_upload() - m_uploaded_at_last_unchoke; }
|
2009-04-04 09:55:34 +02:00
|
|
|
|
2011-01-25 09:03:35 +01:00
|
|
|
// called when the disk write buffer is drained again, and we can
|
|
|
|
// start downloading payload again
|
|
|
|
void on_disk();
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void on_allocate_disk_buffer(char* buffer, int buffer_size);
|
|
|
|
|
2011-09-23 22:57:42 +02:00
|
|
|
int num_reading_bytes() const { return m_reading_bytes; }
|
|
|
|
|
2010-08-28 21:44:50 +02:00
|
|
|
enum sync_t { read_async, read_sync };
|
|
|
|
void setup_receive(sync_t sync = read_sync);
|
2009-06-10 10:30:55 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::shared_ptr<peer_connection> self()
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!m_in_constructor);
|
|
|
|
return shared_from_this();
|
|
|
|
}
|
|
|
|
|
2014-07-13 06:56:53 +02:00
|
|
|
counters& stats_counters() const { return m_counters; }
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
protected:
|
|
|
|
|
2010-06-18 06:43:20 +02:00
|
|
|
size_t try_read(sync_t s, error_code& ec);
|
|
|
|
|
2007-05-25 21:42:10 +02:00
|
|
|
virtual void get_specific_peer_info(peer_info& p) const = 0;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
virtual void write_choke() = 0;
|
|
|
|
virtual void write_unchoke() = 0;
|
|
|
|
virtual void write_interested() = 0;
|
|
|
|
virtual void write_not_interested() = 0;
|
|
|
|
virtual void write_request(peer_request const& r) = 0;
|
|
|
|
virtual void write_cancel(peer_request const& r) = 0;
|
|
|
|
virtual void write_have(int index) = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void write_dont_have(int index) = 0;
|
2006-04-25 23:04:48 +02:00
|
|
|
virtual void write_keepalive() = 0;
|
2008-04-10 12:03:23 +02:00
|
|
|
virtual void write_piece(peer_request const& r, disk_buffer_holder& buffer) = 0;
|
2010-01-15 17:45:42 +01:00
|
|
|
virtual void write_suggest(int piece) = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual void write_bitfield() = 0;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
virtual void write_reject_request(peer_request const& r) = 0;
|
|
|
|
virtual void write_allow_fast(int piece) = 0;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
virtual void on_connected() = 0;
|
|
|
|
virtual void on_tick() {}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
virtual void on_receive(error_code const& error
|
2006-04-25 23:04:48 +02:00
|
|
|
, std::size_t bytes_transferred) = 0;
|
2008-05-03 18:05:42 +02:00
|
|
|
virtual void on_sent(error_code const& error
|
2006-04-25 23:04:48 +02:00
|
|
|
, std::size_t bytes_transferred) = 0;
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
buffer::interval wr_recv_buffer()
|
|
|
|
{
|
2011-11-27 06:46:52 +01:00
|
|
|
if (m_recv_buffer.empty())
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_recv_pos == 0);
|
|
|
|
return buffer::interval(0,0);
|
|
|
|
}
|
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);
|
2011-11-27 02:08:28 +01:00
|
|
|
int rcv_pos = (std::min)(m_recv_pos, int(m_recv_buffer.size()));
|
2014-07-06 21:18:00 +02:00
|
|
|
return buffer::interval(&m_recv_buffer[0] + m_recv_start
|
|
|
|
, &m_recv_buffer[0] + m_recv_start + rcv_pos);
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
2008-04-10 12:03:23 +02:00
|
|
|
|
|
|
|
std::pair<buffer::interval, buffer::interval> wr_recv_buffers(int bytes);
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
buffer::const_interval receive_buffer() const
|
|
|
|
{
|
2011-11-27 06:46:52 +01:00
|
|
|
if (m_recv_buffer.empty())
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_recv_pos == 0);
|
|
|
|
return buffer::interval(0,0);
|
|
|
|
}
|
2011-11-27 02:08:28 +01:00
|
|
|
int rcv_pos = (std::min)(m_recv_pos, int(m_recv_buffer.size()));
|
2014-07-06 21:18:00 +02:00
|
|
|
return buffer::const_interval(&m_recv_buffer[0] + m_recv_start
|
|
|
|
, &m_recv_buffer[0] + m_recv_start + rcv_pos);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
bool allocate_disk_receive_buffer(int disk_buffer_size);
|
|
|
|
char* release_disk_receive_buffer();
|
|
|
|
bool has_disk_receive_buffer() const { return m_disk_recv_buffer; }
|
2010-10-27 08:39:18 +02:00
|
|
|
void cut_receive_buffer(int size, int packet_size, int offset = 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
void reset_recv_buffer(int packet_size);
|
2014-07-06 21:18:00 +02:00
|
|
|
void normalize_receive_buffer();
|
2009-03-13 07:09:39 +01:00
|
|
|
void set_soft_packet_size(int size) { m_soft_packet_size = size; }
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2011-09-05 07:50:41 +02:00
|
|
|
// if allow_encrypted is false, and the torrent 'ih' turns out
|
|
|
|
// to be an encrypted torrent (AES-256 encrypted) the peer will
|
|
|
|
// be disconnected. This is to prevent non-encrypted peers to
|
|
|
|
// attach to an encrypted torrent
|
|
|
|
void attach_to_torrent(sha1_hash const& ih, bool allow_encrypted);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
bool verify_piece(peer_request const& p) const;
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
void update_desired_queue_size();
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// called from the main loop when this connection has any
|
|
|
|
// work to do.
|
|
|
|
void on_send_data(error_code const& error
|
|
|
|
, std::size_t bytes_transferred);
|
|
|
|
void on_receive_data(error_code const& error
|
|
|
|
, std::size_t bytes_transferred);
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// _nb means null_buffers. i.e. we just know the socket is
|
|
|
|
// readable at this point, we don't know how much has been received
|
|
|
|
void on_receive_data_nb(error_code const& error
|
|
|
|
, std::size_t bytes_transferred);
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void receive_data_impl(error_code const& error
|
|
|
|
, std::size_t bytes_transferred, int read_loops);
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
virtual int timeout() const;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void do_update_interest();
|
|
|
|
int preferred_caching() const;
|
|
|
|
void fill_send_buffer();
|
2014-07-29 00:18:06 +02:00
|
|
|
void on_disk_read_complete(disk_io_job const* j, peer_request r
|
|
|
|
, ptime issue_time);
|
2014-07-06 21:18:00 +02:00
|
|
|
void on_disk_write_complete(disk_io_job const* j
|
|
|
|
, peer_request r, boost::shared_ptr<torrent> t);
|
|
|
|
void on_seed_mode_hashed(disk_io_job const* j);
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int wanted_transfer(int channel);
|
|
|
|
int request_bandwidth(int channel, int bytes = 0);
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int get_priority(int channel) const;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::shared_ptr<socket_type> m_socket;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the queue of blocks we have requested
|
|
|
|
// from this peer
|
|
|
|
std::vector<pending_block> m_download_queue;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the queue of requests we have got
|
|
|
|
// from this peer that haven't been issued
|
|
|
|
// to the disk thread yet
|
|
|
|
std::vector<peer_request> m_requests;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this peer's peer info struct. This may
|
|
|
|
// be 0, in case the connection is incoming
|
|
|
|
// and hasn't been added to a torrent yet.
|
|
|
|
torrent_peer* m_peer_info;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-13 06:56:53 +02:00
|
|
|
// stats counters
|
|
|
|
counters& m_counters;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of pieces this peer
|
|
|
|
// has. Must be the same as
|
|
|
|
// std::count(m_have_piece.begin(),
|
|
|
|
// m_have_piece.end(), true)
|
|
|
|
int m_num_pieces;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
public:
|
|
|
|
// upload and download channel state
|
|
|
|
// enum from peer_info::bw_state
|
|
|
|
char m_channel_state[2];
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
protected:
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
buffer m_recv_buffer;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// number of bytes this peer can send and receive
|
|
|
|
int m_quota[2];
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the blocks we have reserved in the piece
|
|
|
|
// picker and will request from this peer.
|
|
|
|
std::vector<pending_block> m_request_queue;
|
|
|
|
|
|
|
|
// the start of the logical receive buffer
|
|
|
|
int m_recv_start:24;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is the limit on the number of outstanding requests
|
|
|
|
// we have to this peer. This is initialized to the settings
|
|
|
|
// in the session_settings structure. But it may be lowered
|
|
|
|
// if the peer is known to require a smaller limit (like BitComet).
|
|
|
|
// or if the extended handshake sets a limit.
|
|
|
|
// web seeds also has a limit on the queue size.
|
|
|
|
int m_max_out_request_queue;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is the peer we're actually talking to
|
|
|
|
// it may not necessarily be the peer we're
|
|
|
|
// connected to, in case we use a proxy
|
|
|
|
tcp::endpoint m_remote;
|
2007-01-10 16:02:25 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
public:
|
|
|
|
chained_buffer m_send_buffer;
|
|
|
|
private:
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the disk thread to use to issue disk jobs to
|
|
|
|
disk_interface& m_disk_thread;
|
|
|
|
|
|
|
|
public:
|
|
|
|
buffer_allocator_interface& m_allocator;
|
|
|
|
private:
|
|
|
|
|
|
|
|
// io service
|
|
|
|
io_service& m_ios;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
public:
|
2013-11-26 08:47:48 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
typedef std::list<boost::shared_ptr<peer_plugin> > extension_list_t;
|
|
|
|
extension_list_t m_extensions;
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
private:
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// the average rate of receiving complete piece messages
|
|
|
|
sliding_average<20> m_piece_rate;
|
|
|
|
sliding_average<20> m_send_rate;
|
|
|
|
|
2014-07-28 09:52:59 +02:00
|
|
|
// the round-trip time of piece requests and the corresponding piece
|
|
|
|
// message
|
|
|
|
sliding_average<50> m_rtt;
|
|
|
|
|
2009-03-31 10:02:25 +02:00
|
|
|
// keep the io_service running as long as we
|
|
|
|
// have peer connections
|
|
|
|
io_service::work m_work;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// the time when we last got a part of a
|
|
|
|
// piece packet from this peer
|
2007-05-23 10:45:12 +02:00
|
|
|
ptime m_last_piece;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2007-05-25 21:42:10 +02:00
|
|
|
// the time we sent a request to
|
|
|
|
// this peer the last time
|
|
|
|
ptime m_last_request;
|
2007-09-01 09:38:10 +02:00
|
|
|
// the time we received the last
|
|
|
|
// piece request from the peer
|
|
|
|
ptime m_last_incoming_request;
|
|
|
|
// the time when we unchoked this peer
|
|
|
|
ptime m_last_unchoke;
|
2004-01-05 00:51:54 +01:00
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
// if we're unchoked by this peer, this
|
|
|
|
// was the time
|
|
|
|
ptime m_last_unchoked;
|
|
|
|
|
2013-03-26 05:45:38 +01:00
|
|
|
// the time we last choked this peer. min_time() in
|
|
|
|
// case we never unchoked it
|
|
|
|
ptime m_last_choke;
|
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// timeouts
|
|
|
|
ptime m_last_receive;
|
|
|
|
ptime m_last_sent;
|
|
|
|
|
2008-06-29 11:50:42 +02:00
|
|
|
// the time when the first entry in the
|
|
|
|
// request queue was requested, increased
|
|
|
|
// for each entry that is popped from the
|
|
|
|
// download queue. Used for request timeout
|
|
|
|
ptime m_requested;
|
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// a timestamp when the remote download rate
|
|
|
|
// was last updated
|
|
|
|
ptime m_remote_dl_update;
|
|
|
|
|
|
|
|
// the time when async_connect was called
|
2008-07-10 12:58:30 +02:00
|
|
|
// or when the incoming connection was established
|
2008-05-19 04:52:32 +02:00
|
|
|
ptime m_connect;
|
|
|
|
|
|
|
|
// the time when this peer sent us a not_interested message
|
|
|
|
// the last time.
|
|
|
|
ptime m_became_uninterested;
|
|
|
|
|
|
|
|
// the time when we sent a not_interested message to
|
|
|
|
// this peer the last time.
|
|
|
|
ptime m_became_uninteresting;
|
|
|
|
|
|
|
|
// the total payload download bytes
|
2013-06-02 00:33:11 +02:00
|
|
|
// at the last unchoke round. This is used to
|
2008-05-19 04:52:32 +02:00
|
|
|
// measure the number of bytes transferred during
|
|
|
|
// an unchoke cycle, to unchoke peers the more bytes
|
|
|
|
// they sent us
|
2013-06-02 00:33:11 +02:00
|
|
|
size_type m_downloaded_at_last_round;
|
|
|
|
size_type m_uploaded_at_last_round;
|
|
|
|
|
|
|
|
// this is the number of bytes we had uploaded the
|
|
|
|
// last time this peer was unchoked. This does not
|
|
|
|
// reset each unchoke interval/round. This is used to
|
|
|
|
// track upload across rounds, for the full duration of
|
|
|
|
// the peer being unchoked. Specifically, it's used
|
|
|
|
// for the round-robin unchoke algorithm.
|
2008-12-11 00:07:44 +01:00
|
|
|
size_type m_uploaded_at_last_unchoke;
|
2008-05-19 04:52:32 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// some messages needs to be read from the socket
|
|
|
|
// buffer in multiple stages. This soft packet
|
|
|
|
// size limits the read size between message handler
|
|
|
|
// dispatch. Ignored when set to 0
|
|
|
|
int m_soft_packet_size;
|
|
|
|
|
|
|
|
// the number of bytes that the other
|
|
|
|
// end has to send us in order to respond
|
|
|
|
// to all outstanding piece requests we
|
|
|
|
// have sent to it
|
|
|
|
int m_outstanding_bytes;
|
|
|
|
|
2013-11-26 05:39:33 +01:00
|
|
|
template <std::size_t Size>
|
|
|
|
struct handler_storage
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
handler_storage()
|
|
|
|
: used(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool used;
|
|
|
|
#else
|
|
|
|
handler_storage() {}
|
|
|
|
#endif
|
|
|
|
boost::aligned_storage<Size> bytes;
|
2014-07-06 21:18:00 +02:00
|
|
|
private:
|
|
|
|
handler_storage(handler_storage const&);
|
2013-11-26 05:39:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
handler_storage<TORRENT_READ_HANDLER_MAX_SIZE> m_read_handler_storage;
|
|
|
|
handler_storage<TORRENT_WRITE_HANDLER_MAX_SIZE> m_write_handler_storage;
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
// if this peer is receiving a piece, this
|
|
|
|
// points to a disk buffer that the data is
|
|
|
|
// read into. This eliminates a memcopy from
|
|
|
|
// the receive buffer into the disk buffer
|
2008-05-05 08:25:22 +02:00
|
|
|
disk_buffer_holder m_disk_recv_buffer;
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// we have suggested these pieces to the peer
|
|
|
|
// don't suggest it again
|
|
|
|
bitfield m_sent_suggested_pieces;
|
2013-11-26 08:47:48 +01:00
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// the pieces we will send to the peer
|
|
|
|
// if requested (regardless of choke state)
|
2009-05-04 02:08:00 +02:00
|
|
|
std::vector<int> m_accept_fast;
|
2003-11-09 19:17:09 +01:00
|
|
|
|
2013-07-26 18:59:10 +02:00
|
|
|
// a sent-piece counter for the allowed fast set
|
|
|
|
// to avoid exploitation. Each slot is a counter
|
|
|
|
// for one of the pieces from the allowed-fast set
|
|
|
|
std::vector<boost::uint16_t> m_accept_fast_piece_cnt;
|
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// the pieces the peer will send us if
|
|
|
|
// requested (regardless of choke state)
|
|
|
|
std::vector<int> m_allowed_fast;
|
2003-12-14 23:55:32 +01:00
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// pieces that has been suggested to be
|
|
|
|
// downloaded from this peer
|
|
|
|
std::vector<int> m_suggested_pieces;
|
|
|
|
|
2010-03-19 19:39:51 +01:00
|
|
|
// the time when this peer last saw a complete copy
|
|
|
|
// of this torrent
|
|
|
|
time_t m_last_seen_complete;
|
|
|
|
|
2013-11-26 08:47:48 +01:00
|
|
|
// the block we're currently receiving. Or
|
|
|
|
// (-1, -1) if we're not receiving one
|
|
|
|
piece_block m_receiving_block;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the local endpoint for this peer, i.e. our address
|
|
|
|
// and our port. If this is set for outgoing connections
|
|
|
|
// before the connection completes, it means we want to
|
|
|
|
// force the connection to be bound to the specified interface.
|
|
|
|
// if it ends up being bound to a different local IP, the connection
|
|
|
|
// is closed.
|
|
|
|
tcp::endpoint m_local;
|
2013-11-26 08:47:48 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// remote peer's id
|
|
|
|
peer_id m_peer_id;
|
|
|
|
|
2013-11-26 08:47:48 +01:00
|
|
|
// the bandwidth channels, upload and download
|
|
|
|
// keeps track of the current quotas
|
|
|
|
bandwidth_channel m_bandwidth_channel[num_channels];
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
private:
|
|
|
|
// statistics about upload and download speeds
|
|
|
|
// and total amount of uploads and downloads for
|
|
|
|
// this peer
|
|
|
|
// TODO: factor this out into its own class with a virtual interface
|
|
|
|
// torrent and session should implement this interface
|
|
|
|
stat m_statistics;
|
|
|
|
protected:
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2010-02-09 04:04:41 +01:00
|
|
|
// if the timeout is extended for the outstanding
|
|
|
|
// requests, this is the number of seconds it was
|
|
|
|
// extended.
|
|
|
|
int m_timeout_extend;
|
|
|
|
|
2010-02-20 19:56:53 +01:00
|
|
|
// the number of outstanding bytes expected
|
|
|
|
// to be received by extensions
|
|
|
|
int m_extension_outstanding_bytes;
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
// the number of time critical requests
|
|
|
|
// queued up in the m_request_queue that
|
|
|
|
// soon will be committed to the download
|
|
|
|
// queue. This is included in download_queue_time()
|
|
|
|
// so that it can be used while adding more
|
|
|
|
// requests and take the previous requests
|
|
|
|
// into account without submitting it all
|
|
|
|
// immediately
|
|
|
|
int m_queued_time_critical;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of valid, received bytes in m_recv_buffer
|
|
|
|
int m_recv_end:24;
|
|
|
|
|
|
|
|
//#error 1 byte
|
|
|
|
|
|
|
|
// recv_buf.begin (start of actual receive buffer)
|
|
|
|
// |
|
|
|
|
// | m_recv_start (logical start of current
|
|
|
|
// | | receive buffer, as perceived by upper layers)
|
|
|
|
// | |
|
|
|
|
// | | m_recv_pos (number of bytes consumed
|
|
|
|
// | | | by upper layer, from logical receive buffer)
|
|
|
|
// | | |
|
|
|
|
// | x---------x
|
|
|
|
// | | | recv_buf.end (end of actual receive buffer)
|
|
|
|
// | | | |
|
|
|
|
// v v v v
|
|
|
|
// *------==========---------
|
|
|
|
// ^
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// ------------------->x m_recv_end (end of received data,
|
|
|
|
// beyond this point is garbage)
|
|
|
|
// m_recv_buffer
|
|
|
|
|
|
|
|
// when not using contiguous receive buffers, there
|
|
|
|
// may be a disk_recv_buffer in the mix as well. Whenever
|
|
|
|
// m_disk_recv_buffer_size > 0 (and presumably also
|
|
|
|
// m_disk_recv_buffer != NULL) the disk buffer is imagined
|
|
|
|
// to be appended to the receive buffer right after m_recv_end.
|
2008-05-19 04:52:32 +02:00
|
|
|
|
|
|
|
int m_disk_recv_buffer_size;
|
|
|
|
|
|
|
|
// the number of bytes we are currently reading
|
|
|
|
// from disk, that will be added to the send
|
|
|
|
// buffer as soon as they complete
|
|
|
|
int m_reading_bytes;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// options used for the piece picker. These flags will
|
|
|
|
// be augmented with flags controlled by other settings
|
|
|
|
// like sequential download etc. These are here to
|
|
|
|
// let plugins control flags that should always be set
|
|
|
|
int m_picker_options;
|
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
// the number of invalid piece-requests
|
|
|
|
// we have got from this peer. If the request
|
|
|
|
// queue gets empty, and there have been
|
|
|
|
// invalid requests, we can assume the
|
|
|
|
// peer is waiting for those pieces.
|
|
|
|
// we can then clear its download queue
|
|
|
|
// by sending choke, unchoke.
|
|
|
|
int m_num_invalid_requests;
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
// the ticket id from the connection queue.
|
|
|
|
// This is used to identify the connection
|
|
|
|
// so that it can be removed from the queue
|
|
|
|
// once the connection completes
|
|
|
|
int m_connection_ticket;
|
2008-12-08 07:36:22 +01:00
|
|
|
|
2012-04-23 07:48:46 +02:00
|
|
|
// if [0] is -1, superseeding is not active. If it is >= 0
|
2008-12-08 07:36:22 +01:00
|
|
|
// this is the piece that is available to this peer. Only
|
2012-04-23 07:48:46 +02:00
|
|
|
// these two pieces can be downloaded from us by this peer.
|
2008-12-08 07:36:22 +01:00
|
|
|
// This will remain the current piece for this peer until
|
|
|
|
// another peer sends us a have message for this piece
|
2012-04-23 07:48:46 +02:00
|
|
|
int m_superseed_piece[2];
|
2008-12-08 07:36:22 +01:00
|
|
|
|
2014-04-16 10:25:14 +02:00
|
|
|
// pieces downloaded since last second
|
2007-06-07 02:05:18 +02:00
|
|
|
// timer timeout; used for determining
|
|
|
|
// approx download rate
|
2014-04-16 10:25:14 +02:00
|
|
|
int m_remote_pieces_dled;
|
2007-06-07 02:05:18 +02:00
|
|
|
|
|
|
|
// approximate peer download rate
|
|
|
|
int m_remote_dl_rate;
|
2007-06-14 23:47:00 +02:00
|
|
|
|
2007-08-01 07:22:34 +02:00
|
|
|
// the number of bytes send to the disk-io
|
|
|
|
// thread that hasn't yet been completely written.
|
|
|
|
int m_outstanding_writing_bytes;
|
2007-10-04 23:26:50 +02:00
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// max transfer rates seen on this peer
|
|
|
|
int m_download_rate_peak;
|
|
|
|
int m_upload_rate_peak;
|
|
|
|
|
2013-12-02 05:24:10 +01:00
|
|
|
// when using the BitTyrant choker, this is our
|
|
|
|
// estimated reciprocation rate. i.e. the rate
|
|
|
|
// we need to send to this peer for it to unchoke
|
|
|
|
// us
|
|
|
|
int m_est_reciprocation_rate;
|
|
|
|
|
2014-08-26 05:14:32 +02:00
|
|
|
// the number of request we should queue up
|
|
|
|
// at the remote end.
|
|
|
|
boost::uint16_t m_desired_queue_size;
|
|
|
|
|
2013-11-26 08:47:48 +01:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
|
|
|
// in case the session settings is set
|
|
|
|
// to resolve countries, this is set to
|
|
|
|
// the two character country code this
|
|
|
|
// peer resides in.
|
|
|
|
char m_country[2];
|
|
|
|
#endif
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is a measurement of how fast the peer
|
|
|
|
// it allows some variance without changing
|
|
|
|
// back and forth between states. values are enums
|
|
|
|
// from peer_speed_t.
|
|
|
|
boost::uint8_t m_speed;
|
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// if set to non-zero, this peer will always prefer
|
|
|
|
// to request entire n pieces, rather than blocks.
|
|
|
|
// where n is the value of this variable.
|
|
|
|
// if it is 0, the download rate limit setting
|
|
|
|
// will be used to determine if whole pieces
|
|
|
|
// are preferred.
|
|
|
|
boost::uint8_t m_prefer_whole_pieces;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is the number of times this peer has had
|
|
|
|
// a request rejected because of a disk I/O failure.
|
|
|
|
// once this reaches a certain threshold, the
|
|
|
|
// peer is disconnected in order to avoid infinite
|
|
|
|
// loops of consistent failures
|
|
|
|
boost::uint8_t m_disk_read_failures;
|
|
|
|
|
|
|
|
// this is used in seed mode whenever we trigger a hash check
|
|
|
|
// for a piece, before we read it. It's used to throttle
|
|
|
|
// the hash checks to just a few per peer at a time.
|
|
|
|
boost::uint8_t m_outstanding_piece_verification:3;
|
|
|
|
|
|
|
|
// is true if it was we that connected to the peer
|
|
|
|
// and false if we got an incoming connection
|
|
|
|
// could be considered: true = local, false = remote
|
|
|
|
bool m_outgoing:1;
|
|
|
|
|
|
|
|
// is true if we learn the incoming connections listening
|
|
|
|
// during the extended handshake
|
|
|
|
bool m_received_listen_port:1;
|
2008-05-19 04:52:32 +02:00
|
|
|
|
2007-10-04 23:26:50 +02:00
|
|
|
// if this is true, the disconnection
|
|
|
|
// timestamp is not updated when the connection
|
|
|
|
// is closed. This means the time until we can
|
|
|
|
// reconnect to this peer is shorter, and likely
|
|
|
|
// immediate.
|
2008-05-19 04:52:32 +02:00
|
|
|
bool m_fast_reconnect:1;
|
2007-06-07 02:05:18 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is set to true if the connection timed
|
|
|
|
// out or closed the connection. In that
|
|
|
|
// case we will not try to reconnect to
|
|
|
|
// this peer
|
|
|
|
bool m_failed:1;
|
2009-01-28 07:14:21 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is set to true if the connection attempt
|
|
|
|
// succeeded. i.e. the TCP 3-way handshake
|
|
|
|
bool m_connected:1;
|
2008-05-19 04:52:32 +02:00
|
|
|
|
|
|
|
// This is true until connect is called on the
|
|
|
|
// peer_connection's socket. It is false on incoming
|
|
|
|
// connections.
|
|
|
|
bool m_queued:1;
|
|
|
|
|
|
|
|
// if this is true, the blocks picked by the piece
|
|
|
|
// picker will be merged before passed to the
|
|
|
|
// request function. i.e. subsequent blocks are
|
|
|
|
// merged into larger blocks. This is used by
|
|
|
|
// the http-downloader, to request whole pieces
|
|
|
|
// at a time.
|
|
|
|
bool m_request_large_blocks:1;
|
2010-09-05 18:01:36 +02:00
|
|
|
|
|
|
|
// set to true if this peer is in share mode
|
|
|
|
bool m_share_mode:1;
|
|
|
|
|
2008-05-19 04:52:32 +02:00
|
|
|
// set to true when this peer is only uploading
|
|
|
|
bool m_upload_only:1;
|
2008-04-03 08:11:21 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is set to true once the bitfield is received
|
|
|
|
bool m_bitfield_received:1;
|
2008-06-29 11:50:42 +02:00
|
|
|
|
2008-11-06 09:34:56 +01:00
|
|
|
// if this is set to true, the client will not
|
|
|
|
// pick any pieces from this peer
|
|
|
|
bool m_no_download:1;
|
2010-01-15 17:45:42 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// set to true when we've sent the first round of suggests
|
|
|
|
bool m_sent_suggests:1;
|
|
|
|
|
|
|
|
// set to true while we're trying to holepunch
|
|
|
|
bool m_holepunch_mode:1;
|
|
|
|
|
|
|
|
// the other side has told us that it won't send anymore
|
|
|
|
// data to us for a while
|
|
|
|
bool m_peer_choked:1;
|
|
|
|
|
|
|
|
// this is set to true when a have_all
|
|
|
|
// message is received. This information
|
|
|
|
// is used to fill the bitmask in init()
|
|
|
|
bool m_have_all:1;
|
|
|
|
|
|
|
|
// other side says that it's interested in downloading
|
|
|
|
// from us.
|
|
|
|
bool m_peer_interested:1;
|
|
|
|
|
|
|
|
// set to true when we should recalculate interest
|
|
|
|
// for this peer. Since this is a fairly expensive
|
|
|
|
// operation, it's delayed until the second_tick is
|
|
|
|
// fired, so that multiple events that wants to recalc
|
|
|
|
// interest are coalesced into only triggering it once
|
|
|
|
// the actual computation is done in do_update_interest().
|
|
|
|
bool m_need_interest_update:1;
|
|
|
|
|
|
|
|
// set to true if this peer has metadata, and false
|
|
|
|
// otherwise.
|
|
|
|
bool m_has_metadata:1;
|
|
|
|
|
|
|
|
// this is true while this connection is queued
|
|
|
|
// in the connection_queue. We may not destruct
|
|
|
|
// the connection while it is, since it's not
|
|
|
|
// held by an owning pointer, just a plain one
|
|
|
|
bool m_queued_for_connection:1;
|
|
|
|
|
|
|
|
// this is set to true if this peer was accepted exceeding
|
|
|
|
// the connection limit. It means it has to disconnect
|
|
|
|
// itself, or some other peer, as soon as it's completed
|
|
|
|
// the handshake. We need to wait for the handshake in
|
|
|
|
// order to know which torrent it belongs to, to know which
|
|
|
|
// other peers to compare it to.
|
|
|
|
bool m_exceeded_limit:1;
|
|
|
|
|
2009-05-20 10:57:08 +02:00
|
|
|
template <class Handler, std::size_t Size>
|
|
|
|
struct allocating_handler
|
|
|
|
{
|
|
|
|
allocating_handler(
|
2010-03-06 08:16:39 +01:00
|
|
|
Handler const& h, handler_storage<Size>& s
|
2009-05-20 10:57:08 +02:00
|
|
|
)
|
2010-03-06 08:16:39 +01:00
|
|
|
: handler(h)
|
|
|
|
, storage(s)
|
2009-05-20 10:57:08 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
template <class A0>
|
|
|
|
void operator()(A0 const& a0) const
|
|
|
|
{
|
|
|
|
handler(a0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class A0, class A1>
|
|
|
|
void operator()(A0 const& a0, A1 const& a1) const
|
|
|
|
{
|
|
|
|
handler(a0, a1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class A0, class A1, class A2>
|
|
|
|
void operator()(A0 const& a0, A1 const& a1, A2 const& a2) const
|
|
|
|
{
|
|
|
|
handler(a0, a1, a2);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend void* asio_handler_allocate(
|
|
|
|
std::size_t size, allocating_handler<Handler, Size>* ctx)
|
|
|
|
{
|
2010-07-15 08:31:39 +02:00
|
|
|
TORRENT_ASSERT(size <= Size);
|
2009-05-20 10:57:08 +02:00
|
|
|
#ifdef TORRENT_DEBUG
|
2010-07-15 08:31:39 +02:00
|
|
|
TORRENT_ASSERT(!ctx->storage.used);
|
2009-05-20 10:57:08 +02:00
|
|
|
ctx->storage.used = true;
|
|
|
|
#endif
|
|
|
|
return &ctx->storage.bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend void asio_handler_deallocate(
|
|
|
|
void*, std::size_t, allocating_handler<Handler, Size>* ctx)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
ctx->storage.used = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Handler handler;
|
|
|
|
handler_storage<Size>& storage;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Handler>
|
|
|
|
allocating_handler<Handler, TORRENT_READ_HANDLER_MAX_SIZE>
|
|
|
|
make_read_handler(Handler const& handler)
|
|
|
|
{
|
|
|
|
return allocating_handler<Handler, TORRENT_READ_HANDLER_MAX_SIZE>(
|
|
|
|
handler, m_read_handler_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Handler>
|
|
|
|
allocating_handler<Handler, TORRENT_WRITE_HANDLER_MAX_SIZE>
|
|
|
|
make_write_handler(Handler const& handler)
|
|
|
|
{
|
|
|
|
return allocating_handler<Handler, TORRENT_WRITE_HANDLER_MAX_SIZE>(
|
|
|
|
handler, m_write_handler_storage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2006-04-25 23:04:48 +02:00
|
|
|
public:
|
2014-07-06 21:18:00 +02:00
|
|
|
bool m_in_constructor;
|
|
|
|
bool m_disconnect_started;
|
|
|
|
bool m_initialized;
|
2012-02-09 07:24:58 +01:00
|
|
|
int m_in_use;
|
2009-03-17 10:34:44 +01:00
|
|
|
int m_received_in_piece;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool m_destructed;
|
|
|
|
// this is true while there is an outstanding
|
|
|
|
// async write job on the socket
|
|
|
|
bool m_socket_is_writing;
|
2014-09-22 19:41:06 +02:00
|
|
|
bool is_single_thread() const;
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
2011-10-24 06:22:53 +02:00
|
|
|
|
|
|
|
struct cork
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
cork(peer_connection& p): m_pc(p), m_need_uncork(false)
|
|
|
|
{
|
|
|
|
if (m_pc.is_corked()) return;
|
|
|
|
m_pc.cork_socket();
|
|
|
|
m_need_uncork = true;
|
|
|
|
}
|
|
|
|
~cork() { if (m_need_uncork) m_pc.uncork_socket(); }
|
2011-10-24 06:22:53 +02:00
|
|
|
peer_connection& m_pc;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool m_need_uncork;
|
2011-10-24 06:22:53 +02:00
|
|
|
};
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_PEER_CONNECTION_HPP_INCLUDED
|