fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / include / libtorrent / policy.hpp
blob500d59e4dc8b44f9b3bef3803c032c49527adac5
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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
36 #include <algorithm>
37 #include <vector>
39 #ifdef _MSC_VER
40 #pragma warning(push, 1)
41 #endif
43 #ifdef _MSC_VER
44 #pragma warning(pop)
45 #endif
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"
55 namespace libtorrent
58 class torrent;
59 class peer_connection;
61 enum
63 // the limits of the download queue size
64 min_request_queue = 2,
66 // the amount of free upload allowed before
67 // the peer is choked
68 free_upload_amount = 4 * 16 * 1024
71 void request_a_block(torrent& t, peer_connection& c);
73 class TORRENT_EXPORT policy
75 public:
77 policy(torrent* t);
79 // this is called every 10 seconds to allow
80 // for peer choking management
81 void pulse();
83 struct peer;
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);
89 // false means duplicate connection
90 bool update_peer_port(int port, policy::peer* p, int src);
92 // called when an incoming connection is accepted
93 // false means the connection was refused or failed
94 bool new_connection(peer_connection& c);
96 // the given connection was just closed
97 void connection_closed(const peer_connection& c);
99 // the peer has got at least one interesting piece
100 void peer_is_interesting(peer_connection& c);
102 // the peer unchoked us
103 void unchoked(peer_connection& c);
105 // the peer is interested in our pieces
106 void interested(peer_connection& c);
108 // the peer is not interested in our pieces
109 void not_interested(peer_connection& c);
111 void ip_filter_updated();
113 #ifndef NDEBUG
114 bool has_connection(const peer_connection* p);
116 void check_invariant() const;
117 #endif
119 struct peer
121 enum connection_type { not_connectable, connectable };
122 peer(tcp::endpoint const& ip, connection_type t, int src);
124 size_type total_download() const;
125 size_type total_upload() const;
127 tcp::endpoint ip() const { return tcp::endpoint(addr, port); }
128 void set_ip(tcp::endpoint const& endp)
129 { addr = endp.address(); port = endp.port(); }
131 // this is the accumulated amount of
132 // uploaded and downloaded data to this
133 // peer. It only accounts for what was
134 // shared during the last connection to
135 // this peer. i.e. These are only updated
136 // when the connection is closed. For the
137 // total amount of upload and download
138 // we'll have to add thes figures with the
139 // statistics from the peer_connection.
140 size_type prev_amount_upload;
141 size_type prev_amount_download;
143 // the ip address this peer is or was connected on
144 address addr;
146 // the time when this peer was optimistically unchoked
147 // the last time.
148 libtorrent::ptime last_optimistically_unchoked;
150 // the time when the peer connected to us
151 // or disconnected if it isn't connected right now
152 libtorrent::ptime connected;
154 // if the peer is connected now, this
155 // will refer to a valid peer_connection
156 peer_connection* connection;
158 #ifndef TORRENT_DISABLE_GEO_IP
159 #ifndef NDEBUG
160 // only used in debug mode to assert that
161 // the first entry in the AS pair keeps the same
162 boost::uint16_t inet_as_num;
163 #endif
164 // The AS this peer belongs to
165 std::pair<const int, int>* inet_as;
166 #endif
168 // the port this peer is or was connected on
169 boost::uint16_t port;
171 // the number of failed connection attempts
172 // this peer has
173 boost::uint8_t failcount;
175 // for every valid piece we receive where this
176 // peer was one of the participants, we increase
177 // this value. For every invalid piece we receive
178 // where this peer was a participant, we decrease
179 // this value. If it sinks below a threshold, its
180 // considered a bad peer and will be banned.
181 boost::int8_t trust_points;
183 // a bitmap combining the peer_source flags
184 // from peer_info.
185 boost::uint8_t source;
187 // the number of times this peer has been
188 // part of a piece that failed the hash check
189 boost::uint8_t hashfails;
191 // type specifies if the connection was incoming
192 // or outgoing. If we ever saw this peer as connectable
193 // it will remain as connectable
194 unsigned type:4;
196 // the number of times we have allowed a fast
197 // reconnect for this peer.
198 unsigned fast_reconnects:4;
200 #ifndef TORRENT_DISABLE_ENCRYPTION
201 // Hints encryption support of peer. Only effective
202 // for and when the outgoing encryption policy
203 // allows both encrypted and non encrypted
204 // connections (pe_settings::out_enc_policy
205 // == enabled). The initial state of this flag
206 // determines the initial connection attempt
207 // type (true = encrypted, false = standard).
208 // This will be toggled everytime either an
209 // encrypted or non-encrypted handshake fails.
210 bool pe_support:1;
211 #endif
212 // true if this peer currently is unchoked
213 // because of an optimistic unchoke.
214 // when the optimistic unchoke is moved to
215 // another peer, this peer will be choked
216 // if this is true
217 bool optimistically_unchoked:1;
219 // this is true if the peer is a seed
220 bool seed:1;
222 // if this is true, the peer has previously
223 // participated in a piece that failed the piece
224 // hash check. This will put the peer on parole
225 // and only request entire pieces. If a piece pass
226 // that was partially requested from this peer it
227 // will leave parole mode and continue download
228 // pieces as normal peers.
229 bool on_parole:1;
231 // is set to true if this peer has been banned
232 bool banned:1;
234 #ifndef TORRENT_DISABLE_DHT
235 // this is set to true when this peer as been
236 // pinged by the DHT
237 bool added_to_dht:1;
238 #endif
241 int num_peers() const { return m_peers.size(); }
243 typedef std::multimap<address, peer>::iterator iterator;
244 typedef std::multimap<address, peer>::const_iterator const_iterator;
245 iterator begin_peer() { return m_peers.begin(); }
246 iterator end_peer() { return m_peers.end(); }
247 const_iterator begin_peer() const { return m_peers.begin(); }
248 const_iterator end_peer() const { return m_peers.end(); }
250 bool connect_one_peer();
252 bool has_peer(policy::peer const* p) const;
254 int num_seeds() const { return m_num_seeds; }
255 int num_connect_candidates() const { return m_num_connect_candidates; }
256 void recalculate_connect_candidates()
258 if (m_num_connect_candidates == 0)
259 m_num_connect_candidates = 1;
262 void erase_peer(iterator i);
264 private:
266 bool compare_peer(policy::peer const& lhs, policy::peer const& rhs
267 , address const& external_ip) const;
269 iterator find_connect_candidate();
271 bool is_connect_candidate(peer const& p, bool finished);
273 std::multimap<address, peer> m_peers;
275 // since the peer list can grow too large
276 // to scan all of it, start at this iterator
277 iterator m_round_robin;
279 torrent* m_torrent;
281 // free download we have got that hasn't
282 // been distributed yet.
283 size_type m_available_free_upload;
285 // The number of peers in our peer list
286 // that are connect candidates. i.e. they're
287 // not already connected and they have not
288 // yet reached their max try count and they
289 // have the connectable state (we have a listen
290 // port for them).
291 int m_num_connect_candidates;
293 // the number of seeds in the peer list
294 int m_num_seeds;
299 #endif // TORRENT_POLICY_HPP_INCLUDED