3
Copyright (c) 2003, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#ifndef TORRENT_POLICY_HPP_INCLUDED
34
#define TORRENT_POLICY_HPP_INCLUDED
40
#pragma warning(push, 1)
47
#include "libtorrent/peer.hpp"
48
#include "libtorrent/piece_picker.hpp"
49
#include "libtorrent/socket.hpp"
50
#include "libtorrent/size_type.hpp"
51
#include "libtorrent/invariant_check.hpp"
52
#include "libtorrent/config.hpp"
53
#include "libtorrent/time.hpp"
59
class peer_connection;
63
// the limits of the download queue size
64
min_request_queue = 2,
66
// the amount of free upload allowed before
68
free_upload_amount = 4 * 16 * 1024
71
void request_a_block(torrent& t, peer_connection& c);
73
class TORRENT_EXPORT policy
79
// this is called every 10 seconds to allow
80
// for peer choking management
84
// this is called once for every peer we get from
85
// the tracker, pex, lsd or dht.
86
policy::peer* peer_from_tracker(const tcp::endpoint& remote, const peer_id& pid
87
, int source, char flags);
88
void update_peer_port(int port, policy::peer* p, int src);
90
// called when an incoming connection is accepted
91
void new_connection(peer_connection& c);
93
// the given connection was just closed
94
void connection_closed(const peer_connection& c) throw();
96
// the peer has got at least one interesting piece
97
void peer_is_interesting(peer_connection& c);
99
void piece_finished(int index, bool successfully_verified);
101
// the peer choked us
102
void choked(peer_connection& c);
104
int count_choked() const;
106
// the peer unchoked us
107
void unchoked(peer_connection& c);
109
// the peer is interested in our pieces
110
void interested(peer_connection& c);
112
// the peer is not interested in our pieces
113
void not_interested(peer_connection& c);
115
void ip_filter_updated();
118
bool has_connection(const peer_connection* p);
120
void check_invariant() const;
125
enum connection_type { not_connectable, connectable };
127
peer(tcp::endpoint const& ip, connection_type t, int src);
129
size_type total_download() const;
130
size_type total_upload() const;
132
// the ip/port pair this peer is or was connected on
133
// if it was a remote (incoming) connection, type is
134
// set thereafter. If it was a peer we got from the
135
// tracker, type is set to local_connection.
137
connection_type type;
139
#ifndef TORRENT_DISABLE_ENCRYPTION
140
// Hints encryption support of peer. Only effective for
141
// and when the outgoing encryption policy allows both
142
// encrypted and non encrypted connections
143
// (pe_settings::out_enc_policy == enabled). The initial
144
// state of this flag determines the initial connection
145
// attempt type (true = encrypted, false = standard).
146
// This will be toggled everytime either an encrypted or
147
// non-encrypted handshake fails.
150
// the number of failed connection attempts this peer has
153
// the number of times this peer has been
154
// part of a piece that failed the hash check
157
// this is true if the peer is a seed
162
// true if this peer currently is unchoked
163
// because of an optimistic unchoke.
164
// when the optimistic unchoke is moved to
165
// another peer, this peer will be choked
167
bool optimistically_unchoked;
169
// the time when this peer was optimistically unchoked
171
libtorrent::ptime last_optimistically_unchoked;
173
// the time when the peer connected to us
174
// or disconnected if it isn't connected right now
175
libtorrent::ptime connected;
177
// for every valid piece we receive where this
178
// peer was one of the participants, we increase
179
// this value. For every invalid piece we receive
180
// where this peer was a participant, we decrease
181
// this value. If it sinks below a threshold, its
182
// considered a bad peer and will be banned.
185
// if this is true, the peer has previously participated
186
// in a piece that failed the piece hash check. This will
187
// put the peer on parole and only request entire pieces.
188
// if a piece pass that was partially requested from this
189
// peer it will leave parole mode and continue download
190
// pieces as normal peers.
193
// this is the accumulated amount of
194
// uploaded and downloaded data to this
195
// peer. It only accounts for what was
196
// shared during the last connection to
197
// this peer. i.e. These are only updated
198
// when the connection is closed. For the
199
// total amount of upload and download
200
// we'll have to add thes figures with the
201
// statistics from the peer_connection.
202
size_type prev_amount_upload;
203
size_type prev_amount_download;
205
// is set to true if this peer has been banned
208
// a bitmap combining the peer_source flags
212
// if the peer is connected now, this
213
// will refer to a valid peer_connection
214
peer_connection* connection;
217
int num_peers() const { return m_peers.size(); }
219
typedef std::multimap<address, peer>::iterator iterator;
220
typedef std::multimap<address, peer>::const_iterator const_iterator;
221
iterator begin_peer() { return m_peers.begin(); }
222
iterator end_peer() { return m_peers.end(); }
223
const_iterator begin_peer() const { return m_peers.begin(); }
224
const_iterator end_peer() const { return m_peers.end(); }
226
bool connect_one_peer();
227
bool disconnect_one_peer();
231
bool unchoke_one_peer();
232
void choke_one_peer();
233
iterator find_choke_candidate();
234
iterator find_unchoke_candidate();
236
// the seed prefix means that the
237
// function is used while seeding.
238
bool seed_unchoke_one_peer();
239
void seed_choke_one_peer();
240
iterator find_seed_choke_candidate();
241
iterator find_seed_unchoke_candidate();
243
iterator find_disconnect_candidate();
244
iterator find_connect_candidate();
246
std::multimap<address, peer> m_peers;
250
// free download we have got that hasn't
251
// been distributed yet.
252
size_type m_available_free_upload;
254
// if there is a connection limit,
255
// we disconnect one peer every minute in hope of
256
// establishing a connection with a better peer
257
// ptime m_last_optimistic_disconnect;
262
#endif // TORRENT_POLICY_HPP_INCLUDED