2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
#define TORRENT_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2003-12-08 22:59:48 +01:00
|
|
|
#include <deque>
|
2004-01-07 01:48:02 +01:00
|
|
|
#include <string>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-01-11 02:32:26 +01:00
|
|
|
#include "libtorrent/debug.hpp"
|
|
|
|
|
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>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/array.hpp>
|
2003-11-20 20:58:29 +01:00
|
|
|
#include <boost/date_time/posix_time/posix_time.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>
|
2006-05-28 21:03:54 +02:00
|
|
|
#include <boost/detail/atomic_count.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/socket.hpp"
|
|
|
|
#include "libtorrent/peer_id.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
|
|
|
#include "libtorrent/stat.hpp"
|
2004-01-07 01:48:02 +01:00
|
|
|
#include "libtorrent/alert.hpp"
|
|
|
|
#include "libtorrent/torrent_handle.hpp"
|
2004-01-25 23:41:55 +01:00
|
|
|
#include "libtorrent/torrent.hpp"
|
2004-03-14 21:01:05 +01:00
|
|
|
#include "libtorrent/allocate_resources.hpp"
|
|
|
|
#include "libtorrent/peer_request.hpp"
|
|
|
|
#include "libtorrent/piece_block_progress.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
#include "libtorrent/session.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-12-08 22:59:48 +01:00
|
|
|
// TODO: each time a block is 'taken over'
|
|
|
|
// from another peer. That peer must be given
|
2004-01-28 12:37:46 +01:00
|
|
|
// a chance to become not-interested.
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
class torrent;
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
struct session_impl;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
TORRENT_EXPORT void intrusive_ptr_add_ref(peer_connection const*);
|
|
|
|
TORRENT_EXPORT void intrusive_ptr_release(peer_connection const*);
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_EXPORT protocol_error: std::runtime_error
|
2003-12-01 22:27:27 +01:00
|
|
|
{
|
|
|
|
protocol_error(const std::string& msg): std::runtime_error(msg) {};
|
|
|
|
};
|
|
|
|
|
2005-11-02 17:28:39 +01:00
|
|
|
class TORRENT_EXPORT peer_connection
|
|
|
|
: public boost::noncopyable
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
friend class invariant_access;
|
2006-04-25 23:04:48 +02:00
|
|
|
friend void intrusive_ptr_add_ref(peer_connection const*);
|
|
|
|
friend void intrusive_ptr_release(peer_connection const*);
|
2003-10-23 01:00:57 +02:00
|
|
|
public:
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// this is the constructor where the we are the active part.
|
2004-08-08 23:26:40 +02:00
|
|
|
// The peer_conenction should handshake and verify that the
|
|
|
|
// other end has the correct id
|
2003-11-05 00:27:06 +01:00
|
|
|
peer_connection(
|
2003-12-07 06:53:04 +01:00
|
|
|
detail::session_impl& ses
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::weak_ptr<torrent> t
|
|
|
|
, boost::shared_ptr<stream_socket> s
|
|
|
|
, tcp::endpoint const& remote);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-08-08 23:26:40 +02:00
|
|
|
// with this constructor we have been contacted and we still don't
|
|
|
|
// know which torrent the connection belongs to
|
2003-11-05 00:27:06 +01:00
|
|
|
peer_connection(
|
2003-12-07 06:53:04 +01:00
|
|
|
detail::session_impl& ses
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::shared_ptr<stream_socket> s);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
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-04-25 23:04:48 +02:00
|
|
|
void set_upload_limit(int limit);
|
|
|
|
void set_download_limit(int limit);
|
|
|
|
|
|
|
|
virtual ~peer_connection();
|
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);
|
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;
|
|
|
|
bool can_read() const;
|
2004-01-12 21:31:27 +01:00
|
|
|
|
|
|
|
bool is_seed() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-02-01 14:48:30 +01:00
|
|
|
bool has_timed_out() const;
|
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; }
|
|
|
|
void set_pid(const peer_id& pid) { m_peer_id = pid; }
|
2004-03-11 15:56:48 +01:00
|
|
|
bool has_piece(int i) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-03-11 15:56:48 +01:00
|
|
|
const std::deque<piece_block>& download_queue() const;
|
2005-09-27 10:07:24 +02:00
|
|
|
const std::deque<piece_block>& request_queue() const;
|
2004-03-11 15:56:48 +01:00
|
|
|
const std::deque<peer_request>& upload_queue() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
// 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
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
const stat& statistics() const { return m_statistics; }
|
2004-03-11 15:56:48 +01:00
|
|
|
void add_stat(size_type downloaded, size_type uploaded);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// is called once every second by the main loop
|
2006-04-25 23:04:48 +02:00
|
|
|
void second_tick(float tick_interval);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<stream_socket> get_socket() const { return m_socket; }
|
|
|
|
tcp::endpoint const& remote() const { return m_remote; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<bool> const& get_bitfield() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-20 12:01:50 +01:00
|
|
|
// this will cause this peer_connection to be disconnected.
|
|
|
|
// what it does is that it puts a reference to it in
|
|
|
|
// m_ses.m_disconnect_peer list, which will be scanned in the
|
|
|
|
// mainloop to disconnect peers.
|
|
|
|
void disconnect();
|
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
|
2006-04-25 23:04:48 +02:00
|
|
|
void on_connection_complete(asio::error 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; }
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
void connect();
|
|
|
|
|
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
|
|
|
// free upload.
|
|
|
|
size_type total_free_upload() const;
|
|
|
|
void add_free_upload(size_type free_upload);
|
2003-12-16 14:33:29 +01:00
|
|
|
|
2004-03-11 15:56:48 +01:00
|
|
|
// trust management.
|
|
|
|
void received_valid_data();
|
|
|
|
void received_invalid_data();
|
|
|
|
int trust_points() const;
|
2003-12-01 22:27:27 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type share_diff() const;
|
2003-12-14 23:55:32 +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
|
2004-03-11 15:56:48 +01:00
|
|
|
bool is_local() const { return m_active; }
|
2004-01-09 11:50:22 +01:00
|
|
|
|
2004-03-21 03:03:37 +01:00
|
|
|
void set_failed() { m_failed = true; }
|
|
|
|
bool failed() const { return m_failed; }
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
int desired_queue_size() const { return m_desired_queue_size; }
|
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2003-10-23 01:00:57 +02:00
|
|
|
boost::shared_ptr<logger> m_logger;
|
|
|
|
#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);
|
|
|
|
void incoming_bitfield(std::vector<bool> const& bitfield);
|
|
|
|
void incoming_request(peer_request const& r);
|
|
|
|
void incoming_piece(peer_request const& p, char const* data);
|
|
|
|
void incoming_piece_fragment();
|
|
|
|
void incoming_cancel(peer_request const& r);
|
|
|
|
void incoming_dht_port(int listen_port);
|
2004-01-05 00:51:54 +01:00
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
// the following functions appends messages
|
|
|
|
// to the send buffer
|
|
|
|
void send_choke();
|
|
|
|
void send_unchoke();
|
|
|
|
void send_interested();
|
|
|
|
void send_not_interested();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// adds a block to the request queue
|
|
|
|
void add_request(piece_block const& b);
|
|
|
|
void cancel_request(piece_block const& b);
|
2006-05-15 00:30:05 +02:00
|
|
|
void send_block_requests();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-03-22 15:56:32 +01:00
|
|
|
// how much bandwidth we're using, how much we want,
|
|
|
|
// and how much we are allowed to use.
|
2004-03-28 19:45:37 +02:00
|
|
|
resource_request m_ul_bandwidth_quota;
|
|
|
|
resource_request m_dl_bandwidth_quota;
|
2004-01-12 21:31:27 +01:00
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
#ifndef NDEBUG
|
2004-01-25 19:18:36 +01:00
|
|
|
void check_invariant() const;
|
2005-08-18 22:38:03 +02:00
|
|
|
boost::posix_time::ptime m_last_choke;
|
2004-06-14 01:30:42 +02:00
|
|
|
#endif
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
virtual void get_peer_info(peer_info& p) const = 0;
|
2005-08-18 22:38:03 +02:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << "downloading_piece_progress() dispatched to the base class!\n";
|
|
|
|
#endif
|
|
|
|
return boost::optional<piece_block_progress>();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
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;
|
|
|
|
virtual void write_keepalive() = 0;
|
|
|
|
virtual void write_piece(peer_request const& r) = 0;
|
|
|
|
|
|
|
|
virtual void on_connected() = 0;
|
|
|
|
virtual void on_tick() {}
|
|
|
|
|
|
|
|
virtual void on_receive(asio::error const& error
|
|
|
|
, std::size_t bytes_transferred) = 0;
|
|
|
|
virtual void on_sent(asio::error const& error
|
|
|
|
, std::size_t bytes_transferred) = 0;
|
|
|
|
|
|
|
|
void send_buffer(char const* begin, char const* end);
|
|
|
|
buffer::interval allocate_send_buffer(int size);
|
|
|
|
int send_buffer_size() const
|
|
|
|
{
|
|
|
|
return (int)m_send_buffer[0].size()
|
2006-04-30 02:39:18 +02:00
|
|
|
+ (int)m_send_buffer[1].size()
|
|
|
|
- m_write_pos;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2004-03-11 15:56:48 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
buffer::const_interval receive_buffer() const
|
|
|
|
{
|
|
|
|
return buffer::const_interval(&m_recv_buffer[0]
|
|
|
|
, &m_recv_buffer[0] + m_recv_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cut_receive_buffer(int size, int packet_size);
|
2004-03-11 15:56:48 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void reset_recv_buffer(int packet_size);
|
|
|
|
int packet_size() const { return m_packet_size; }
|
2004-01-12 21:31:27 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
bool packet_finished() const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
assert(m_recv_pos <= m_packet_size);
|
|
|
|
return m_packet_size == m_recv_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup_send();
|
|
|
|
void setup_receive();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void attach_to_torrent(sha1_hash const& ih);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
bool verify_piece(peer_request const& p) const;
|
|
|
|
|
|
|
|
// statistics about upload and download speeds
|
|
|
|
// and total amount of uploads and downloads for
|
|
|
|
// this peer
|
|
|
|
stat m_statistics;
|
|
|
|
|
|
|
|
// a back reference to the session
|
|
|
|
// the peer belongs to.
|
|
|
|
detail::session_impl& m_ses;
|
|
|
|
|
|
|
|
boost::intrusive_ptr<peer_connection> self()
|
|
|
|
{ return boost::intrusive_ptr<peer_connection>(this); }
|
|
|
|
|
|
|
|
// called from the main loop when this connection has any
|
|
|
|
// work to do.
|
|
|
|
void on_send_data(asio::error const& error
|
|
|
|
, std::size_t bytes_transferred);
|
|
|
|
void on_receive_data(asio::error const& error
|
|
|
|
, std::size_t bytes_transferred);
|
|
|
|
|
2006-05-15 00:30:05 +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.
|
2006-08-01 17:27:08 +02:00
|
|
|
// web seeds also has a limit on the queue size.
|
2006-05-15 00:30:05 +02:00
|
|
|
int m_max_out_request_queue;
|
|
|
|
|
2006-06-29 01:27:44 +02:00
|
|
|
void set_timeout(int s) { m_timeout = s; }
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
void fill_send_buffer();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// the timeout in seconds
|
|
|
|
int m_timeout;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// the time when we last got a part of a
|
|
|
|
// piece packet from this peer
|
|
|
|
boost::posix_time::ptime m_last_piece;
|
2004-01-05 00:51:54 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
int m_packet_size;
|
|
|
|
int m_recv_pos;
|
2003-10-23 01:00:57 +02:00
|
|
|
std::vector<char> m_recv_buffer;
|
|
|
|
|
|
|
|
// this is the buffer where data that is
|
2003-12-07 06:53:04 +01:00
|
|
|
// to be sent is stored until it gets
|
2006-04-25 23:04:48 +02:00
|
|
|
// consumed by send(). Since asio requires
|
|
|
|
// the memory buffer that is given to async.
|
|
|
|
// operations to remain valid until the operation
|
|
|
|
// finishes, there has to be two buffers. While
|
|
|
|
// waiting for a async_write operation on one
|
|
|
|
// buffer, the other is used to write data to
|
|
|
|
// be queued up.
|
2006-04-30 02:39:18 +02:00
|
|
|
std::vector<char> m_send_buffer[2];
|
2006-04-25 23:04:48 +02:00
|
|
|
// the current send buffer is the one to write to.
|
|
|
|
// (m_current_send_buffer + 1) % 2 is the
|
|
|
|
// buffer we're currently waiting for.
|
|
|
|
int m_current_send_buffer;
|
2006-04-30 02:39:18 +02:00
|
|
|
|
|
|
|
// if the sending buffer doesn't finish in one send
|
|
|
|
// operation, this is the position within that buffer
|
2006-05-15 00:30:05 +02:00
|
|
|
// where the next operation should continue
|
2006-04-30 02:39:18 +02:00
|
|
|
int m_write_pos;
|
2003-12-08 22:59:48 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// timeouts
|
|
|
|
boost::posix_time::ptime m_last_receive;
|
|
|
|
boost::posix_time::ptime m_last_sent;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<stream_socket> m_socket;
|
|
|
|
tcp::endpoint m_remote;
|
2006-09-27 19:20:18 +02:00
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// this is the torrent this connection is
|
|
|
|
// associated with. If the connection is an
|
|
|
|
// incoming conncetion, this is set to zero
|
|
|
|
// until the info_hash is received. Then it's
|
|
|
|
// set to the torrent it belongs to.
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::weak_ptr<torrent> m_torrent;
|
2003-10-23 01:00:57 +02:00
|
|
|
// is true if it was we that connected to the peer
|
|
|
|
// and false if we got an incomming connection
|
2004-01-09 11:50:22 +01:00
|
|
|
// could be considered: true = local, false = remote
|
2003-10-23 01:00:57 +02:00
|
|
|
bool m_active;
|
|
|
|
|
|
|
|
// remote peer's id
|
|
|
|
peer_id m_peer_id;
|
|
|
|
|
|
|
|
// other side says that it's interested in downloading
|
|
|
|
// from us.
|
|
|
|
bool m_peer_interested;
|
|
|
|
|
|
|
|
// the other side has told us that it won't send anymore
|
|
|
|
// data to us for a while
|
|
|
|
bool m_peer_choked;
|
|
|
|
|
|
|
|
// the peer has pieces we are interested in
|
|
|
|
bool m_interesting;
|
|
|
|
|
|
|
|
// we have choked the upload to the peer
|
|
|
|
bool m_choked;
|
|
|
|
|
2004-03-21 03:03:37 +01: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;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// the pieces the other end have
|
|
|
|
std::vector<bool> m_have_piece;
|
|
|
|
|
2004-01-28 12:37:46 +01: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;
|
|
|
|
|
2003-12-08 22:59:48 +01:00
|
|
|
// the queue of requests we have got
|
|
|
|
// from this peer
|
|
|
|
std::deque<peer_request> m_requests;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-09-27 10:07:24 +02:00
|
|
|
// the blocks we have reserved in the piece
|
|
|
|
// picker and will send to this peer.
|
|
|
|
std::deque<piece_block> m_request_queue;
|
|
|
|
|
2003-12-08 22:59:48 +01:00
|
|
|
// the queue of blocks we have requested
|
|
|
|
// from this peer
|
|
|
|
std::deque<piece_block> m_download_queue;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// the number of request we should queue up
|
|
|
|
// at the remote end.
|
|
|
|
int m_desired_queue_size;
|
2003-11-09 19:17:09 +01:00
|
|
|
|
2003-12-14 23:55:32 +01:00
|
|
|
// the amount of data this peer has been given
|
|
|
|
// as free upload. This is distributed from
|
|
|
|
// peers from which we get free download
|
|
|
|
// this will be negative on a peer from which
|
|
|
|
// we get free download, and positive on peers
|
|
|
|
// that we give the free upload, to keep the balance.
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type m_free_upload;
|
2003-12-14 23:55:32 +01:00
|
|
|
|
2003-12-01 22:27:27 +01:00
|
|
|
// for every valid piece we receive where this
|
|
|
|
// peer was one of the participants, we increase
|
|
|
|
// this value. For every invalid piece we receive
|
|
|
|
// where this peer was a participant, we decrease
|
|
|
|
// this value. If it sinks below a threshold, its
|
|
|
|
// considered a bad peer and will be banned.
|
|
|
|
int m_trust_points;
|
2004-01-04 05:29:13 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// if this is true, this peer is assumed to handle all piece
|
|
|
|
// requests in fifo order. All skipped blocks are re-requested
|
|
|
|
// immediately instead of having a looser requirement
|
|
|
|
// where blocks can be sent out of order. The default is to
|
|
|
|
// allow non-fifo order.
|
|
|
|
bool m_assume_fifo;
|
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;
|
|
|
|
|
2004-02-01 14:48:30 +01:00
|
|
|
// this is true if this connection has been added
|
|
|
|
// to the list of connections that will be closed.
|
2004-01-20 23:59:21 +01:00
|
|
|
bool m_disconnecting;
|
2004-02-01 14:48:30 +01:00
|
|
|
|
|
|
|
// the time when this peer sent us a not_interested message
|
|
|
|
// the last time.
|
|
|
|
boost::posix_time::ptime m_became_uninterested;
|
|
|
|
|
|
|
|
// the time when we sent a not_interested message to
|
|
|
|
// this peer the last time.
|
|
|
|
boost::posix_time::ptime m_became_uninteresting;
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2005-11-02 17:28:39 +01:00
|
|
|
// this is true until this socket has become
|
|
|
|
// writable for the first time (i.e. the
|
|
|
|
// connection completed). While connecting
|
2005-04-03 17:44:17 +02:00
|
|
|
// 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;
|
2005-06-15 14:54:35 +02:00
|
|
|
|
2005-11-02 17:28:39 +01:00
|
|
|
// This is true until connect is called on the
|
|
|
|
// peer_connection's socket. It is false on incoming
|
|
|
|
// connections.
|
|
|
|
bool m_queued;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// these are true when there's a asynchronous write
|
|
|
|
// or read operation running.
|
|
|
|
bool m_writing;
|
|
|
|
// this is the number of bytes sent to the socket last
|
|
|
|
// time it was invoked. This is compared against the
|
|
|
|
// bytes_transferred in the callback function that tells
|
|
|
|
// how much actually was sent. Then the quota can be
|
|
|
|
// corrected according to the actual number of bytes sent
|
|
|
|
int m_last_write_size;
|
|
|
|
bool m_reading;
|
|
|
|
int m_last_read_size;
|
|
|
|
// reference counter for intrusive_ptr
|
2006-05-28 21:03:54 +02:00
|
|
|
mutable boost::detail::atomic_count m_refs;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
public:
|
|
|
|
bool m_in_constructor;
|
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
|