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_POLICY_HPP_INCLUDED
|
|
|
|
#define TORRENT_POLICY_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/peer.hpp"
|
2003-10-24 04:18:11 +02:00
|
|
|
#include "libtorrent/piece_picker.hpp"
|
2004-01-12 04:05:10 +01:00
|
|
|
#include "libtorrent/socket.hpp"
|
2004-01-21 01:59:38 +01:00
|
|
|
#include "libtorrent/size_type.hpp"
|
2005-08-18 22:38:03 +02:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
class torrent;
|
|
|
|
class peer_connection;
|
|
|
|
|
2005-09-27 10:07:24 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// the limits of the download queue size
|
|
|
|
min_request_queue = 2,
|
|
|
|
|
|
|
|
// the amount of free upload allowed before
|
|
|
|
// the peer is choked
|
|
|
|
free_upload_amount = 4 * 16 * 1024
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT policy
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
policy(torrent* t);
|
2003-12-01 06:01:40 +01:00
|
|
|
|
|
|
|
// this is called every 10 seconds to allow
|
|
|
|
// for peer choking management
|
|
|
|
void pulse();
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// this is called once for every peer we get from
|
|
|
|
// the tracker
|
2006-04-25 23:04:48 +02:00
|
|
|
void peer_from_tracker(const tcp::endpoint& remote, const peer_id& pid);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-14 17:18:53 +01:00
|
|
|
// called when an incoming connection is accepted
|
|
|
|
// return false if the connection closed
|
|
|
|
void new_connection(peer_connection& c);
|
|
|
|
|
2004-03-21 03:03:37 +01:00
|
|
|
// this is called if a peer timed-out or
|
|
|
|
// forcefully closed the connection. This
|
|
|
|
// will mark the connection as non-reconnectale
|
|
|
|
void peer_failed(peer_connection const& c);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// the given connection was just closed
|
|
|
|
void connection_closed(const peer_connection& c);
|
|
|
|
|
2003-12-01 22:27:27 +01:00
|
|
|
// is called when a peer is believed to have
|
|
|
|
// sent invalid data
|
|
|
|
void ban_peer(const peer_connection& c);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// the peer has got at least one interesting piece
|
|
|
|
void peer_is_interesting(peer_connection& c);
|
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
void piece_finished(int index, bool successfully_verified);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
void block_finished(peer_connection& c, piece_block b);
|
|
|
|
|
|
|
|
// the peer choked us
|
|
|
|
void choked(peer_connection& c);
|
|
|
|
|
|
|
|
// the peer unchoked us
|
|
|
|
void unchoked(peer_connection& c);
|
|
|
|
|
|
|
|
// the peer is interested in our pieces
|
|
|
|
void interested(peer_connection& c);
|
|
|
|
|
|
|
|
// the peer is not interested in our pieces
|
|
|
|
void not_interested(peer_connection& c);
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
bool has_connection(const peer_connection* p);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2004-05-10 08:12:29 +02:00
|
|
|
void check_invariant() const;
|
2003-12-01 06:01:40 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct peer
|
|
|
|
{
|
2004-01-21 01:59:38 +01:00
|
|
|
enum connection_type { not_connectable,connectable };
|
2004-01-15 20:32:03 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
peer(const tcp::endpoint& ip, connection_type t);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2004-01-21 01:59:38 +01:00
|
|
|
size_type total_download() const;
|
|
|
|
size_type total_upload() const;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2004-01-12 04:05:10 +01:00
|
|
|
// the ip/port pair this peer is or was connected on
|
2004-01-15 17:45:34 +01:00
|
|
|
// if it was a remote (incoming) connection, type is
|
|
|
|
// set thereafter. If it was a peer we got from the
|
|
|
|
// tracker, type is set to local_connection.
|
2006-04-25 23:04:48 +02:00
|
|
|
tcp::endpoint ip;
|
2004-01-15 17:45:34 +01:00
|
|
|
connection_type type;
|
2004-01-12 04:05:10 +01:00
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// the time when this peer was optimistically unchoked
|
|
|
|
// the last time.
|
|
|
|
boost::posix_time::ptime last_optimistically_unchoked;
|
|
|
|
|
|
|
|
// the time when the peer connected to us
|
|
|
|
// or disconnected if it isn't connected right now
|
|
|
|
boost::posix_time::ptime connected;
|
|
|
|
|
|
|
|
// this is the accumulated amount of
|
|
|
|
// uploaded and downloaded data to this
|
|
|
|
// peer. It only accounts for what was
|
|
|
|
// shared during the last connection to
|
|
|
|
// this peer. i.e. These are only updated
|
|
|
|
// when the connection is closed. For the
|
|
|
|
// total amount of upload and download
|
|
|
|
// we'll have to add thes figures with the
|
|
|
|
// statistics from the peer_connection.
|
2004-01-21 01:59:38 +01:00
|
|
|
size_type prev_amount_upload;
|
|
|
|
size_type prev_amount_download;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2003-12-01 22:27:27 +01:00
|
|
|
// is set to true if this peer has been banned
|
|
|
|
bool banned;
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// if the peer is connected now, this
|
|
|
|
// will refer to a valid peer_connection
|
2003-12-14 06:56:12 +01:00
|
|
|
peer_connection* connection;
|
2003-12-01 06:01:40 +01:00
|
|
|
};
|
|
|
|
|
2004-03-21 03:03:37 +01:00
|
|
|
int num_peers() const
|
|
|
|
{
|
2006-07-08 21:41:39 +02:00
|
|
|
return m_peers.size();
|
2004-03-21 03:03:37 +01:00
|
|
|
}
|
|
|
|
|
2004-10-29 15:21:09 +02:00
|
|
|
int num_uploads() const
|
|
|
|
{
|
|
|
|
return m_num_unchoked;
|
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
typedef std::vector<peer>::iterator iterator;
|
|
|
|
iterator begin_peer() { return m_peers.begin(); }
|
|
|
|
iterator end_peer() { return m_peers.end(); }
|
2004-10-29 15:21:09 +02:00
|
|
|
|
2004-01-15 17:45:34 +01:00
|
|
|
private:
|
2004-01-15 02:29:43 +01:00
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
bool unchoke_one_peer();
|
2005-08-18 22:38:03 +02:00
|
|
|
void choke_one_peer();
|
2003-12-14 06:56:12 +01:00
|
|
|
peer* find_choke_candidate();
|
|
|
|
peer* find_unchoke_candidate();
|
|
|
|
|
2004-03-01 22:54:10 +01:00
|
|
|
// the seed prefix means that the
|
|
|
|
// function is used while seeding.
|
|
|
|
bool seed_unchoke_one_peer();
|
2005-08-18 22:38:03 +02:00
|
|
|
void seed_choke_one_peer();
|
2004-03-01 22:54:10 +01:00
|
|
|
peer* find_seed_choke_candidate();
|
|
|
|
peer* find_seed_unchoke_candidate();
|
2003-12-14 23:55:32 +01:00
|
|
|
|
2004-02-26 19:55:10 +01:00
|
|
|
bool connect_peer(peer *);
|
2004-01-14 17:18:53 +01:00
|
|
|
bool connect_one_peer();
|
2004-01-21 01:59:38 +01:00
|
|
|
bool disconnect_one_peer();
|
|
|
|
peer* find_disconnect_candidate();
|
2004-01-14 17:18:53 +01:00
|
|
|
peer* find_connect_candidate();
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// a functor that identifies peers that have disconnected and that
|
|
|
|
// are too old for still being saved.
|
|
|
|
struct old_disconnected_peer
|
|
|
|
{
|
|
|
|
bool operator()(const peer& p)
|
|
|
|
{
|
|
|
|
using namespace boost::posix_time;
|
|
|
|
|
2004-01-21 01:59:38 +01:00
|
|
|
ptime not_tried_yet(boost::gregorian::date(1970,boost::gregorian::Jan,1));
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
// this timeout has to be customizable!
|
2003-12-14 06:56:12 +01:00
|
|
|
return p.connection == 0
|
2004-09-16 03:14:16 +02:00
|
|
|
&& p.connected != not_tried_yet
|
2004-10-14 03:17:04 +02:00
|
|
|
&& second_clock::universal_time() - p.connected > minutes(30);
|
2003-12-01 06:01:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<peer> m_peers;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
torrent* m_torrent;
|
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
// the number of unchoked peers
|
|
|
|
// at any given time
|
|
|
|
int m_num_unchoked;
|
2003-12-14 23:55:32 +01:00
|
|
|
|
|
|
|
// free download we have got that hasn't
|
|
|
|
// been distributed yet.
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type m_available_free_upload;
|
2004-01-21 01:59:38 +01:00
|
|
|
|
|
|
|
// if there is a connection limit,
|
|
|
|
// we disconnect one peer every minute in hope of
|
|
|
|
// establishing a connection with a better peer
|
|
|
|
boost::posix_time::ptime m_last_optimistic_disconnect;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_POLICY_HPP_INCLUDED
|
2005-11-01 19:30:39 +01:00
|
|
|
|