~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to docs/manual.html

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
Import upstream version 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="utf-8" ?>
 
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
4
<head>
 
5
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
6
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
 
7
<title>libtorrent API Documentation</title>
 
8
<meta name="author" content="Arvid Norberg, arvid&#64;rasterbar.com" />
 
9
<link rel="stylesheet" href="style.css" type="text/css" />
 
10
</head>
 
11
<body>
 
12
<div class="document" id="libtorrent-api-documentation">
 
13
<h1 class="title">libtorrent API Documentation</h1>
 
14
<table class="docinfo" frame="void" rules="none">
 
15
<col class="docinfo-name" />
 
16
<col class="docinfo-content" />
 
17
<tbody valign="top">
 
18
<tr><th class="docinfo-name">Author:</th>
 
19
<td>Arvid Norberg, <a class="last reference external" href="mailto:arvid&#64;rasterbar.com">arvid&#64;rasterbar.com</a></td></tr>
 
20
<tr><th class="docinfo-name">Version:</th>
 
21
<td>0.13</td></tr>
 
22
</tbody>
 
23
</table>
 
24
<div class="contents topic" id="table-of-contents">
 
25
<p class="topic-title first">Table of contents</p>
 
26
<ul class="simple">
 
27
<li><a class="reference internal" href="#overview" id="id18">overview</a></li>
 
28
<li><a class="reference internal" href="#network-primitives" id="id19">network primitives</a></li>
 
29
<li><a class="reference internal" href="#session" id="id20">session</a><ul>
 
30
<li><a class="reference internal" href="#id1" id="id21">session()</a></li>
 
31
<li><a class="reference internal" href="#id2" id="id22">~session()</a></li>
 
32
<li><a class="reference internal" href="#abort" id="id23">abort()</a></li>
 
33
<li><a class="reference internal" href="#add-torrent" id="id24">add_torrent()</a></li>
 
34
<li><a class="reference internal" href="#remove-torrent" id="id25">remove_torrent()</a></li>
 
35
<li><a class="reference internal" href="#find-torrent-get-torrents" id="id26">find_torrent() get_torrents()</a></li>
 
36
<li><a class="reference internal" href="#set-upload-rate-limit-set-download-rate-limit-upload-rate-limit-download-rate-limit" id="id27">set_upload_rate_limit() set_download_rate_limit() upload_rate_limit() download_rate_limit()</a></li>
 
37
<li><a class="reference internal" href="#set-max-uploads-set-max-connections" id="id28">set_max_uploads() set_max_connections()</a></li>
 
38
<li><a class="reference internal" href="#num-uploads-num-connections" id="id29">num_uploads() num_connections()</a></li>
 
39
<li><a class="reference internal" href="#set-max-half-open-connections-max-half-open-connections" id="id30">set_max_half_open_connections() max_half_open_connections()</a></li>
 
40
<li><a class="reference internal" href="#set-ip-filter" id="id31">set_ip_filter()</a></li>
 
41
<li><a class="reference internal" href="#status" id="id32">status()</a></li>
 
42
<li><a class="reference internal" href="#is-listening-listen-port-listen-on" id="id33">is_listening() listen_port() listen_on()</a></li>
 
43
<li><a class="reference internal" href="#pop-alert-set-severity-level-wait-for-alert" id="id34">pop_alert() set_severity_level() wait_for_alert()</a></li>
 
44
<li><a class="reference internal" href="#add-extension" id="id35">add_extension()</a></li>
 
45
<li><a class="reference internal" href="#set-settings-set-pe-settings" id="id36">set_settings() set_pe_settings()</a></li>
 
46
<li><a class="reference internal" href="#set-peer-proxy-set-web-seed-proxy-set-tracker-proxy-set-dht-proxy" id="id37">set_peer_proxy() set_web_seed_proxy() set_tracker_proxy() set_dht_proxy()</a></li>
 
47
<li><a class="reference internal" href="#peer-proxy-web-seed-proxy-tracker-proxy-dht-proxy" id="id38">peer_proxy() web_seed_proxy() tracker_proxy() dht_proxy()</a></li>
 
48
<li><a class="reference internal" href="#start-dht-stop-dht-set-dht-settings-dht-state" id="id39">start_dht() stop_dht() set_dht_settings() dht_state()</a></li>
 
49
<li><a class="reference internal" href="#add-dht-node-add-dht-router" id="id40">add_dht_node() add_dht_router()</a></li>
 
50
<li><a class="reference internal" href="#start-lsd-stop-lsd" id="id41">start_lsd() stop_lsd()</a></li>
 
51
<li><a class="reference internal" href="#start-upnp-stop-upnp" id="id42">start_upnp() stop_upnp()</a></li>
 
52
<li><a class="reference internal" href="#start-natpmp-stop-natpmp" id="id43">start_natpmp() stop_natpmp()</a></li>
 
53
</ul>
 
54
</li>
 
55
<li><a class="reference internal" href="#entry" id="id44">entry</a><ul>
 
56
<li><a class="reference internal" href="#integer-string-list-dict-type" id="id45">integer() string() list() dict() type()</a></li>
 
57
<li><a class="reference internal" href="#operator" id="id46">operator[]</a></li>
 
58
<li><a class="reference internal" href="#find-key" id="id47">find_key()</a></li>
 
59
</ul>
 
60
</li>
 
61
<li><a class="reference internal" href="#torrent-info" id="id48">torrent_info</a><ul>
 
62
<li><a class="reference internal" href="#id3" id="id49">torrent_info()</a></li>
 
63
<li><a class="reference internal" href="#set-comment-set-piece-size-set-creator-set-hash-add-tracker-add-file" id="id50">set_comment() set_piece_size() set_creator() set_hash() add_tracker() add_file()</a></li>
 
64
<li><a class="reference internal" href="#create-torrent" id="id51">create_torrent()</a></li>
 
65
<li><a class="reference internal" href="#remap-files" id="id52">remap_files()</a></li>
 
66
<li><a class="reference internal" href="#begin-files-end-files-rbegin-files-rend-files" id="id53">begin_files() end_files() rbegin_files() rend_files()</a></li>
 
67
<li><a class="reference internal" href="#num-files-file-at" id="id54">num_files() file_at()</a></li>
 
68
<li><a class="reference internal" href="#map-block" id="id55">map_block()</a></li>
 
69
<li><a class="reference internal" href="#map-file" id="id56">map_file()</a></li>
 
70
<li><a class="reference internal" href="#url-seeds-add-url-seed" id="id57">url_seeds() add_url_seed()</a></li>
 
71
<li><a class="reference internal" href="#print" id="id58">print()</a></li>
 
72
<li><a class="reference internal" href="#trackers" id="id59">trackers()</a></li>
 
73
<li><a class="reference internal" href="#total-size-piece-length-piece-size-num-pieces" id="id60">total_size() piece_length() piece_size() num_pieces()</a></li>
 
74
<li><a class="reference internal" href="#hash-for-piece-info-hash" id="id61">hash_for_piece() info_hash()</a></li>
 
75
<li><a class="reference internal" href="#name-comment-creation-date-creator" id="id62">name() comment() creation_date() creator()</a></li>
 
76
<li><a class="reference internal" href="#priv-set-priv" id="id63">priv() set_priv()</a></li>
 
77
<li><a class="reference internal" href="#nodes" id="id64">nodes()</a></li>
 
78
<li><a class="reference internal" href="#add-node" id="id65">add_node()</a></li>
 
79
</ul>
 
80
</li>
 
81
<li><a class="reference internal" href="#torrent-handle" id="id66">torrent_handle</a><ul>
 
82
<li><a class="reference internal" href="#piece-priority-prioritize-pieces-piece-priorities-prioritize-files" id="id67">piece_priority() prioritize_pieces() piece_priorities() prioritize_files()</a></li>
 
83
<li><a class="reference internal" href="#file-progress" id="id68">file_progress()</a></li>
 
84
<li><a class="reference internal" href="#save-path" id="id69">save_path()</a></li>
 
85
<li><a class="reference internal" href="#move-storage" id="id70">move_storage()</a></li>
 
86
<li><a class="reference internal" href="#force-reannounce" id="id71">force_reannounce()</a></li>
 
87
<li><a class="reference internal" href="#scrape-tracker" id="id72">scrape_tracker()</a></li>
 
88
<li><a class="reference internal" href="#connect-peer" id="id73">connect_peer()</a></li>
 
89
<li><a class="reference internal" href="#name" id="id74">name()</a></li>
 
90
<li><a class="reference internal" href="#set-ratio" id="id75">set_ratio()</a></li>
 
91
<li><a class="reference internal" href="#set-upload-limit-set-download-limit-upload-limit-download-limit" id="id76">set_upload_limit() set_download_limit() upload_limit() download_limit()</a></li>
 
92
<li><a class="reference internal" href="#set-sequenced-download-threshold" id="id77">set_sequenced_download_threshold()</a></li>
 
93
<li><a class="reference internal" href="#set-peer-upload-limit-set-peer-download-limit" id="id78">set_peer_upload_limit() set_peer_download_limit()</a></li>
 
94
<li><a class="reference internal" href="#pause-resume-is-paused" id="id79">pause() resume() is_paused()</a></li>
 
95
<li><a class="reference internal" href="#resolve-countries" id="id80">resolve_countries()</a></li>
 
96
<li><a class="reference internal" href="#is-seed" id="id81">is_seed()</a></li>
 
97
<li><a class="reference internal" href="#has-metadata" id="id82">has_metadata()</a></li>
 
98
<li><a class="reference internal" href="#set-tracker-login" id="id83">set_tracker_login()</a></li>
 
99
<li><a class="reference internal" href="#trackers-replace-trackers" id="id84">trackers() replace_trackers()</a></li>
 
100
<li><a class="reference internal" href="#add-url-seed-remove-url-seed-url-seeds" id="id85">add_url_seed() remove_url_seed() url_seeds()</a></li>
 
101
<li><a class="reference internal" href="#use-interface" id="id86">use_interface()</a></li>
 
102
<li><a class="reference internal" href="#info-hash" id="id87">info_hash()</a></li>
 
103
<li><a class="reference internal" href="#id5" id="id88">set_max_uploads() set_max_connections()</a></li>
 
104
<li><a class="reference internal" href="#write-resume-data" id="id89">write_resume_data()</a></li>
 
105
<li><a class="reference internal" href="#id6" id="id90">status()</a></li>
 
106
<li><a class="reference internal" href="#get-download-queue" id="id91">get_download_queue()</a></li>
 
107
<li><a class="reference internal" href="#get-peer-info" id="id92">get_peer_info()</a></li>
 
108
<li><a class="reference internal" href="#get-torrent-info" id="id93">get_torrent_info()</a></li>
 
109
<li><a class="reference internal" href="#is-valid" id="id94">is_valid()</a></li>
 
110
</ul>
 
111
</li>
 
112
<li><a class="reference internal" href="#torrent-status" id="id95">torrent_status</a></li>
 
113
<li><a class="reference internal" href="#peer-info" id="id96">peer_info</a></li>
 
114
<li><a class="reference internal" href="#session-settings" id="id97">session_settings</a></li>
 
115
<li><a class="reference internal" href="#pe-settings" id="id98">pe_settings</a></li>
 
116
<li><a class="reference internal" href="#proxy-settings" id="id99">proxy_settings</a></li>
 
117
<li><a class="reference internal" href="#ip-filter" id="id100">ip_filter</a><ul>
 
118
<li><a class="reference internal" href="#id9" id="id101">ip_filter()</a></li>
 
119
<li><a class="reference internal" href="#add-rule" id="id102">add_rule()</a></li>
 
120
<li><a class="reference internal" href="#access" id="id103">access()</a></li>
 
121
<li><a class="reference internal" href="#export-filter" id="id104">export_filter()</a></li>
 
122
</ul>
 
123
</li>
 
124
<li><a class="reference internal" href="#big-number" id="id105">big_number</a></li>
 
125
<li><a class="reference internal" href="#hasher" id="id106">hasher</a></li>
 
126
<li><a class="reference internal" href="#fingerprint" id="id107">fingerprint</a></li>
 
127
<li><a class="reference internal" href="#free-functions" id="id108">free functions</a><ul>
 
128
<li><a class="reference internal" href="#identify-client" id="id109">identify_client()</a></li>
 
129
<li><a class="reference internal" href="#client-fingerprint" id="id110">client_fingerprint()</a></li>
 
130
<li><a class="reference internal" href="#bdecode-bencode" id="id111">bdecode() bencode()</a></li>
 
131
</ul>
 
132
</li>
 
133
<li><a class="reference internal" href="#alerts" id="id112">alerts</a><ul>
 
134
<li><a class="reference internal" href="#listen-failed-alert" id="id113">listen_failed_alert</a></li>
 
135
<li><a class="reference internal" href="#portmap-error-alert" id="id114">portmap_error_alert</a></li>
 
136
<li><a class="reference internal" href="#portmap-alert" id="id115">portmap_alert</a></li>
 
137
<li><a class="reference internal" href="#file-error-alert" id="id116">file_error_alert</a></li>
 
138
<li><a class="reference internal" href="#tracker-announce-alert" id="id117">tracker_announce_alert</a></li>
 
139
<li><a class="reference internal" href="#tracker-alert" id="id118">tracker_alert</a></li>
 
140
<li><a class="reference internal" href="#tracker-reply-alert" id="id119">tracker_reply_alert</a></li>
 
141
<li><a class="reference internal" href="#tracker-warning-alert" id="id120">tracker_warning_alert</a></li>
 
142
<li><a class="reference internal" href="#scrape-reply-alert" id="id121">scrape_reply_alert</a></li>
 
143
<li><a class="reference internal" href="#scrape-failed-alert" id="id122">scrape_failed_alert</a></li>
 
144
<li><a class="reference internal" href="#url-seed-alert" id="id123">url_seed_alert</a></li>
 
145
<li><a class="reference internal" href="#hash-failed-alert" id="id124">hash_failed_alert</a></li>
 
146
<li><a class="reference internal" href="#peer-ban-alert" id="id125">peer_ban_alert</a></li>
 
147
<li><a class="reference internal" href="#peer-error-alert" id="id126">peer_error_alert</a></li>
 
148
<li><a class="reference internal" href="#invalid-request-alert" id="id127">invalid_request_alert</a></li>
 
149
<li><a class="reference internal" href="#torrent-finished-alert" id="id128">torrent_finished_alert</a></li>
 
150
<li><a class="reference internal" href="#metadata-failed-alert" id="id129">metadata_failed_alert</a></li>
 
151
<li><a class="reference internal" href="#metadata-received-alert" id="id130">metadata_received_alert</a></li>
 
152
<li><a class="reference internal" href="#fastresume-rejected-alert" id="id131">fastresume_rejected_alert</a></li>
 
153
<li><a class="reference internal" href="#peer-blocked-alert" id="id132">peer_blocked_alert</a></li>
 
154
<li><a class="reference internal" href="#storage-moved-alert" id="id133">storage_moved_alert</a></li>
 
155
<li><a class="reference internal" href="#torrent-paused-alert" id="id134">torrent_paused_alert</a></li>
 
156
<li><a class="reference internal" href="#dispatcher" id="id135">dispatcher</a></li>
 
157
</ul>
 
158
</li>
 
159
<li><a class="reference internal" href="#exceptions" id="id136">exceptions</a><ul>
 
160
<li><a class="reference internal" href="#invalid-handle" id="id137">invalid_handle</a></li>
 
161
<li><a class="reference internal" href="#duplicate-torrent" id="id138">duplicate_torrent</a></li>
 
162
<li><a class="reference internal" href="#invalid-encoding" id="id139">invalid_encoding</a></li>
 
163
<li><a class="reference internal" href="#type-error" id="id140">type_error</a></li>
 
164
<li><a class="reference internal" href="#invalid-torrent-file" id="id141">invalid_torrent_file</a></li>
 
165
</ul>
 
166
</li>
 
167
<li><a class="reference internal" href="#storage-interface" id="id142">storage_interface</a><ul>
 
168
<li><a class="reference internal" href="#initialize" id="id143">initialize()</a></li>
 
169
<li><a class="reference internal" href="#read" id="id144">read()</a></li>
 
170
<li><a class="reference internal" href="#write" id="id145">write()</a></li>
 
171
<li><a class="reference internal" href="#id11" id="id146">move_storage()</a></li>
 
172
<li><a class="reference internal" href="#verify-resume-data" id="id147">verify_resume_data()</a></li>
 
173
<li><a class="reference internal" href="#id12" id="id148">write_resume_data( )</a></li>
 
174
<li><a class="reference internal" href="#move-slot" id="id149">move_slot()</a></li>
 
175
<li><a class="reference internal" href="#swap-slots" id="id150">swap_slots()</a></li>
 
176
<li><a class="reference internal" href="#swap-slots3" id="id151">swap_slots3()</a></li>
 
177
<li><a class="reference internal" href="#hash-for-slot" id="id152">hash_for_slot()</a></li>
 
178
<li><a class="reference internal" href="#release-files" id="id153">release_files()</a></li>
 
179
<li><a class="reference internal" href="#delete-files" id="id154">delete_files()</a></li>
 
180
</ul>
 
181
</li>
 
182
<li><a class="reference internal" href="#fast-resume" id="id155">fast resume</a><ul>
 
183
<li><a class="reference internal" href="#file-format" id="id156">file format</a></li>
 
184
</ul>
 
185
</li>
 
186
<li><a class="reference internal" href="#threads" id="id157">threads</a></li>
 
187
<li><a class="reference internal" href="#storage-allocation" id="id158">storage allocation</a><ul>
 
188
<li><a class="reference internal" href="#sparse-allocation" id="id159">sparse allocation</a></li>
 
189
<li><a class="reference internal" href="#full-allocation" id="id160">full allocation</a></li>
 
190
<li><a class="reference internal" href="#compact-allocation" id="id161">compact allocation</a></li>
 
191
</ul>
 
192
</li>
 
193
<li><a class="reference internal" href="#extensions" id="id162">extensions</a><ul>
 
194
<li><a class="reference internal" href="#metadata-from-peers" id="id163">metadata from peers</a></li>
 
195
<li><a class="reference internal" href="#http-seeding" id="id164">HTTP seeding</a></li>
 
196
</ul>
 
197
</li>
 
198
<li><a class="reference internal" href="#filename-checks" id="id165">filename checks</a></li>
 
199
<li><a class="reference internal" href="#acknowledgments" id="id166">acknowledgments</a></li>
 
200
</ul>
 
201
</div>
 
202
<div class="section" id="overview">
 
203
<h1>overview</h1>
 
204
<p>The interface of libtorrent consists of a few classes. The main class is
 
205
the <tt class="docutils literal"><span class="pre">session</span></tt>, it contains the main loop that serves all torrents.</p>
 
206
<p>The basic usage is as follows:</p>
 
207
<ul>
 
208
<li><p class="first">construct a session</p>
 
209
</li>
 
210
<li><p class="first">parse .torrent-files and add them to the session (see <a class="reference internal" href="#bdecode-bencode">bdecode() bencode()</a> and <a class="reference internal" href="#add-torrent">add_torrent()</a>)</p>
 
211
</li>
 
212
<li><p class="first">main loop (see <a class="reference internal" href="#session">session</a>)</p>
 
213
<blockquote>
 
214
<ul class="simple">
 
215
<li>query the torrent_handles for progress (see <a class="reference internal" href="#torrent-handle">torrent_handle</a>)</li>
 
216
<li>query the session for information</li>
 
217
<li>add and remove torrents from the session at run-time</li>
 
218
</ul>
 
219
</blockquote>
 
220
</li>
 
221
<li><p class="first">save resume data for all torrent_handles (optional, see
 
222
<a class="reference internal" href="#write-resume-data">write_resume_data()</a>)</p>
 
223
</li>
 
224
<li><p class="first">destruct session object</p>
 
225
</li>
 
226
</ul>
 
227
<p>Each class and function is described in this manual.</p>
 
228
</div>
 
229
<div class="section" id="network-primitives">
 
230
<h1>network primitives</h1>
 
231
<p>There are a few typedefs in the <tt class="docutils literal"><span class="pre">libtorrent</span></tt> namespace which pulls
 
232
in network types from the <tt class="docutils literal"><span class="pre">asio</span></tt> namespace. These are:</p>
 
233
<pre class="literal-block">
 
234
typedef asio::ip::address address;
 
235
typedef asio::ip::address_v4 address_v4;
 
236
typedef asio::ip::address_v6 address_v6;
 
237
using asio::ip::tcp;
 
238
using asio::ip::udp;
 
239
</pre>
 
240
<p>These are declared in the <tt class="docutils literal"><span class="pre">&lt;libtorrent/socket.hpp&gt;</span></tt> header.</p>
 
241
<p>The <tt class="docutils literal"><span class="pre">using</span></tt> statements will give easy access to:</p>
 
242
<pre class="literal-block">
 
243
tcp::endpoint
 
244
udp::endpoint
 
245
</pre>
 
246
<p>Which are the endpoint types used in libtorrent. An endpoint is an address
 
247
with an associated port.</p>
 
248
<p>For documentation on these types, please refer to the <a class="reference external" href="http://asio.sourceforge.net/asio-0.3.8/doc/asio/reference.html">asio documentation</a>.</p>
 
249
</div>
 
250
<div class="section" id="session">
 
251
<h1>session</h1>
 
252
<p>The <tt class="docutils literal"><span class="pre">session</span></tt> class has the following synopsis:</p>
 
253
<pre class="literal-block">
 
254
class session: public boost::noncopyable
 
255
{
 
256
 
 
257
        session(fingerprint const&amp; print
 
258
                = libtorrent::fingerprint(
 
259
                &quot;LT&quot;, 0, 1, 0, 0));
 
260
 
 
261
        session(
 
262
                fingerprint const&amp; print
 
263
                , std::pair&lt;int, int&gt; listen_port_range
 
264
                , char const* listen_interface = 0);
 
265
 
 
266
        torrent_handle add_torrent(
 
267
                boost::intrusive_ptr&lt;torrent_info&gt; const&amp; ti
 
268
                , boost::filesystem::path const&amp; save_path
 
269
                , entry const&amp; resume_data = entry()
 
270
                , storage_mode_t storage_mode = storage_mode_sparse
 
271
                , bool paused = false
 
272
                , storage_constructor_type sc = default_storage_constructor
 
273
                , void* userdata = 0);
 
274
 
 
275
        torrent_handle add_torrent(
 
276
                char const* tracker_url
 
277
                , sha1_hash const&amp; info_hash
 
278
                , char const* name
 
279
                , boost::filesystem::path const&amp; save_path
 
280
                , entry const&amp; resume_data = entry()
 
281
                , storage_mode_t storage_mode = storage_mode_sparse
 
282
                , bool paused = false
 
283
                , storage_constructor_type sc = default_storage_constructor
 
284
                , void* userdata = 0);
 
285
 
 
286
        session_proxy abort();
 
287
 
 
288
        enum options_t
 
289
        {
 
290
                none = 0,
 
291
                delete_files = 1
 
292
        };
 
293
 
 
294
        void remove_torrent(torrent_handle const&amp; h, int options = none);
 
295
        torrent_handle find_torrent(sha_hash const&amp; ih);
 
296
        std::vector&lt;torrent_handle&gt; get_torrents() const;
 
297
 
 
298
        void set_settings(session_settings const&amp; settings);
 
299
        void set_pe_settings(pe_settings const&amp; settings);
 
300
 
 
301
        void set_upload_rate_limit(int bytes_per_second);
 
302
        int upload_rate_limit() const;
 
303
        void set_download_rate_limit(int bytes_per_second);
 
304
        int download_rate_limit() const;
 
305
        void set_max_uploads(int limit);
 
306
        void set_max_connections(int limit);
 
307
        void set_max_half_open_connections(int limit);
 
308
        int max_half_open_connections() const;
 
309
 
 
310
        void set_peer_proxy(proxy_settings const&amp; s);
 
311
        void set_web_seed_proxy(proxy_settings const&amp; s);
 
312
        void set_tracker_proxy(proxy_settings const&amp; s);
 
313
 
 
314
        proxy_settings const&amp; peer_proxy() const;
 
315
        proxy_settings const&amp; web_seed_proxy() const;
 
316
        proxy_settings const&amp; tracker_proxy() const;
 
317
 
 
318
        int num_uploads() const;
 
319
        int num_connections() const;
 
320
 
 
321
        void set_ip_filter(ip_filter const&amp; f);
 
322
 
 
323
        session_status status() const;
 
324
 
 
325
        bool is_listening() const;
 
326
        unsigned short listen_port() const;
 
327
        bool listen_on(
 
328
                std::pair&lt;int, int&gt; const&amp; port_range
 
329
                , char const* interface = 0);
 
330
 
 
331
        std::auto_ptr&lt;alert&gt; pop_alert();
 
332
        alert const* wait_for_alert(time_duration max_wait);
 
333
        void set_severity_level(alert::severity_t s);
 
334
 
 
335
        void add_extension(boost::function&lt;
 
336
                boost::shared_ptr&lt;torrent_plugin&gt;(torrent*)&gt; ext);
 
337
 
 
338
        void start_dht();
 
339
        void stop_dht();
 
340
        void set_dht_settings(
 
341
                dht_settings const&amp; settings);
 
342
        entry dht_state() const;
 
343
        void add_dht_node(std::pair&lt;std::string
 
344
                , int&gt; const&amp; node);
 
345
        void add_dht_router(std::pair&lt;std::string
 
346
                , int&gt; const&amp; node);
 
347
 
 
348
        void start_lsd();
 
349
        void stop_lsd();
 
350
 
 
351
        void start_upnp();
 
352
        void stop_upnp();
 
353
 
 
354
        void start_natpmp();
 
355
        void stop_natpmp();
 
356
};
 
357
</pre>
 
358
<p>Once it's created, the session object will spawn the main thread that will do all the work.
 
359
The main thread will be idle as long it doesn't have any torrents to participate in.</p>
 
360
<div class="section" id="id1">
 
361
<h2>session()</h2>
 
362
<blockquote>
 
363
<pre class="literal-block">
 
364
session(fingerprint const&amp; print
 
365
        = libtorrent::fingerprint(&quot;LT&quot;, 0, 1, 0, 0));
 
366
session(fingerprint const&amp; print
 
367
        , std::pair&lt;int, int&gt; listen_port_range
 
368
        , char const* listen_interface = 0);
 
369
</pre>
 
370
</blockquote>
 
371
<p>If the fingerprint in the first overload is omited, the client will get a default
 
372
fingerprint stating the version of libtorrent. The fingerprint is a short string that will be
 
373
used in the peer-id to identify the client and the client's version. For more details see the
 
374
<a class="reference internal" href="#fingerprint">fingerprint</a> class. The constructor that only takes a fingerprint will not open a
 
375
listen port for the session, to get it running you'll have to call <tt class="docutils literal"><span class="pre">session::listen_on()</span></tt>.
 
376
The other constructor, that takes a port range and an interface as well as the fingerprint
 
377
will automatically try to listen on a port on the given interface. For more information about
 
378
the parameters, see <tt class="docutils literal"><span class="pre">listen_on()</span></tt> function.</p>
 
379
</div>
 
380
<div class="section" id="id2">
 
381
<h2>~session()</h2>
 
382
<p>The destructor of session will notify all trackers that our torrents have been shut down.
 
383
If some trackers are down, they will time out. All this before the destructor of session
 
384
returns. So, it's advised that any kind of interface (such as windows) are closed before
 
385
destructing the session object. Because it can take a few second for it to finish. The
 
386
timeout can be set with <tt class="docutils literal"><span class="pre">set_settings()</span></tt>.</p>
 
387
</div>
 
388
<div class="section" id="abort">
 
389
<h2>abort()</h2>
 
390
<pre class="literal-block">
 
391
session_proxy abort();
 
392
</pre>
 
393
<p>In case you want to destruct the session asynchrounously, you can request a session
 
394
destruction proxy. If you don't do this, the destructor of the session object will
 
395
block while the trackers are contacted. If you keep one <tt class="docutils literal"><span class="pre">session_proxy</span></tt> to the
 
396
session when destructing it, the destructor will not block, but start to close down
 
397
the session, the destructor of the proxy will then synchronize the threads. So, the
 
398
destruction of the session is performed from the <tt class="docutils literal"><span class="pre">session</span></tt> destructor call until the
 
399
<tt class="docutils literal"><span class="pre">session_proxy</span></tt> destructor call. The <tt class="docutils literal"><span class="pre">session_proxy</span></tt> does not have any operations
 
400
on it (since the session is being closed down, no operations are allowed on it). The
 
401
only valid operation is calling the destructor:</p>
 
402
<pre class="literal-block">
 
403
class session_proxy
 
404
{
 
405
public:
 
406
        session_proxy();
 
407
        ~session_proxy()
 
408
};
 
409
</pre>
 
410
</div>
 
411
<div class="section" id="add-torrent">
 
412
<h2>add_torrent()</h2>
 
413
<blockquote>
 
414
<pre class="literal-block">
 
415
typedef storage_interface* (&amp;storage_constructor_type)(
 
416
        boost::intrusive_ptr&lt;torrent_info const&gt;, fs::path const&amp;
 
417
        , file_pool&amp;);
 
418
 
 
419
torrent_handle add_torrent(
 
420
        boost::intrusive_ptr&lt;torrent_info&gt; const&amp; ti
 
421
        , boost::filesystem::path const&amp; save_path
 
422
        , entry const&amp; resume_data = entry()
 
423
        , storage_mode_t storage_mode = storage_mode_sparse
 
424
        , bool paused = false
 
425
        , storage_constructor_type sc = default_storage_constructor
 
426
        , void* userdata = 0);
 
427
 
 
428
torrent_handle add_torrent(
 
429
        char const* tracker_url
 
430
        , sha1_hash const&amp; info_hash
 
431
        , char const* name
 
432
        , boost::filesystem::path const&amp; save_path
 
433
        , entry const&amp; resume_data = entry()
 
434
        , storage_mode_t storage_mode = storage_mode_sparse
 
435
        , bool paused = false
 
436
        , storage_constructor_type sc = default_storage_constructor
 
437
        , void* userdata = 0);
 
438
</pre>
 
439
</blockquote>
 
440
<p>You add torrents through the <tt class="docutils literal"><span class="pre">add_torrent()</span></tt> function where you give an
 
441
object representing the information found in the torrent file and the path where you
 
442
want to save the files. The <tt class="docutils literal"><span class="pre">save_path</span></tt> will be prepended to the directory
 
443
structure in the torrent-file.</p>
 
444
<p>If the torrent you are trying to add already exists in the session (is either queued
 
445
for checking, being checked or downloading) <tt class="docutils literal"><span class="pre">add_torrent()</span></tt> will throw
 
446
<a class="reference internal" href="#duplicate-torrent">duplicate_torrent</a> which derives from <tt class="docutils literal"><span class="pre">std::exception</span></tt>.</p>
 
447
<p>The optional parameter, <tt class="docutils literal"><span class="pre">resume_data</span></tt> can be given if up to date fast-resume data
 
448
is available. The fast-resume data can be acquired from a running torrent by calling
 
449
<tt class="docutils literal"><span class="pre">torrent_handle::write_resume_data()</span></tt>. See <a class="reference internal" href="#fast-resume">fast resume</a>.</p>
 
450
<p>The <tt class="docutils literal"><span class="pre">storage_mode</span></tt> parameter refers to the layout of the storage for this torrent.
 
451
There are 3 different modes:</p>
 
452
<dl class="docutils">
 
453
<dt>storage_mode_sparse</dt>
 
454
<dd>All pieces will be written to the place where they belong and sparse files
 
455
will be used. This is the recommended, and default mode.</dd>
 
456
<dt>storage_mode_allocate</dt>
 
457
<dd>All pieces will be allocated, zeroes will be written to the files, before
 
458
the data is downloaded and written to the file. This might be useful for
 
459
filesystems that don't support sparse files.</dd>
 
460
<dt>storage_mode_compact</dt>
 
461
<dd>The storage will grow as more pieces are downloaded, and pieces
 
462
are rearranged to finally be in their correct places once the entire torrent has been
 
463
downloaded.</dd>
 
464
</dl>
 
465
<p>For more information, see <a class="reference internal" href="#storage-allocation">storage allocation</a>.</p>
 
466
<p><tt class="docutils literal"><span class="pre">paused</span></tt> is a boolean that specifies whether or not the torrent is to be started in
 
467
a paused state. I.e. it won't connect to the tracker or any of the peers until it's
 
468
resumed. This is typically a good way of avoiding race conditions when setting
 
469
configuration options on torrents before starting them.</p>
 
470
<p><tt class="docutils literal"><span class="pre">storage_constructor</span></tt> can be used to customize how the data is stored. The default
 
471
storage will simply write the data to the files it belongs to, but it could be
 
472
overridden to save everything to a single file at a specific location or encrypt the
 
473
content on disk for instance. For more information about the <tt class="docutils literal"><span class="pre">storage_interface</span></tt>
 
474
that needs to be implemented for a custom storage, see <a class="reference internal" href="#storage-interface">storage_interface</a>.</p>
 
475
<p>The <a class="reference internal" href="#torrent-handle">torrent_handle</a> returned by <tt class="docutils literal"><span class="pre">add_torrent()</span></tt> can be used to retrieve information
 
476
about the torrent's progress, its peers etc. It is also used to abort a torrent.</p>
 
477
<p>The <tt class="docutils literal"><span class="pre">userdata</span></tt> parameter is optional and will be passed on to the extension
 
478
constructor functions, if any (see <a class="reference internal" href="#add-extension">add_extension()</a>).</p>
 
479
<p>The second overload that takes a tracker url and an info-hash instead of metadata
 
480
(<tt class="docutils literal"><span class="pre">torrent_info</span></tt>) can be used with torrents where (at least some) peers support
 
481
the metadata extension. For the overload to be available, libtorrent must be built
 
482
with extensions enabled (<tt class="docutils literal"><span class="pre">TORRENT_DISABLE_EXTENSIONS</span></tt> must not be defined). It also
 
483
takes an optional <tt class="docutils literal"><span class="pre">name</span></tt> argument. This may be 0 in case no name should be assigned
 
484
to the torrent. In case it's not 0, the name is used for the torrent as long as it doesn't
 
485
have metadata. See <tt class="docutils literal"><span class="pre">torrent_handle::name</span></tt>.</p>
 
486
<p>If the torrent doesn't have a tracker, but relies on the DHT to find peers, the
 
487
<tt class="docutils literal"><span class="pre">tracker_url</span></tt> can be 0.</p>
 
488
</div>
 
489
<div class="section" id="remove-torrent">
 
490
<h2>remove_torrent()</h2>
 
491
<blockquote>
 
492
<pre class="literal-block">
 
493
void remove_torrent(torrent_handle const&amp; h, int options = none);
 
494
</pre>
 
495
</blockquote>
 
496
<p><tt class="docutils literal"><span class="pre">remove_torrent()</span></tt> will close all peer connections associated with the torrent and tell
 
497
the tracker that we've stopped participating in the swarm. The optional second argument
 
498
<tt class="docutils literal"><span class="pre">options</span></tt> can be used to delete all the files downloaded by this torrent. To do this, pass
 
499
in the value <tt class="docutils literal"><span class="pre">session::delete_files</span></tt>. The removal of the torrent is asyncronous, there is
 
500
no guarantee that adding the same torrent immediately after it was removed will not throw
 
501
a <a class="reference internal" href="#duplicate-torrent">duplicate_torrent</a> exception.</p>
 
502
</div>
 
503
<div class="section" id="find-torrent-get-torrents">
 
504
<h2>find_torrent() get_torrents()</h2>
 
505
<blockquote>
 
506
<pre class="literal-block">
 
507
torrent_handle find_torrent(sha_hash const&amp; ih);
 
508
std::vector&lt;torrent_handle&gt; get_torrents() const;
 
509
</pre>
 
510
</blockquote>
 
511
<p><tt class="docutils literal"><span class="pre">find_torrent()</span></tt> looks for a torrent with the given info-hash. In case there
 
512
is such a torrent in the session, a torrent_handle to that torrent is returned.
 
513
In case the torrent cannot be found, an invalid torrent_handle is returned.</p>
 
514
<p>See <tt class="docutils literal"><span class="pre">torrent_handle::is_valid()</span></tt> to know if the torrent was found or not.</p>
 
515
<p><tt class="docutils literal"><span class="pre">get_torrents()</span></tt> returns a vector of torrent_handles to all the torrents
 
516
currently in the session.</p>
 
517
</div>
 
518
<div class="section" id="set-upload-rate-limit-set-download-rate-limit-upload-rate-limit-download-rate-limit">
 
519
<h2>set_upload_rate_limit() set_download_rate_limit() upload_rate_limit() download_rate_limit()</h2>
 
520
<blockquote>
 
521
<pre class="literal-block">
 
522
void set_upload_rate_limit(int bytes_per_second);
 
523
void set_download_rate_limit(int bytes_per_second);
 
524
int upload_rate_limit() const;
 
525
int download_rate_limit() const;
 
526
</pre>
 
527
</blockquote>
 
528
<p><tt class="docutils literal"><span class="pre">set_upload_rate_limit()</span></tt> set the maximum number of bytes allowed to be
 
529
sent to peers per second. This bandwidth is distributed among all the peers. If
 
530
you don't want to limit upload rate, you can set this to -1 (the default).
 
531
<tt class="docutils literal"><span class="pre">set_download_rate_limit()</span></tt> works the same way but for download rate instead
 
532
of upload rate.
 
533
<tt class="docutils literal"><span class="pre">download_rate_limit()</span></tt> and <tt class="docutils literal"><span class="pre">upload_rate_limit()</span></tt> returns the previously
 
534
set limits.</p>
 
535
</div>
 
536
<div class="section" id="set-max-uploads-set-max-connections">
 
537
<h2>set_max_uploads() set_max_connections()</h2>
 
538
<blockquote>
 
539
<pre class="literal-block">
 
540
void set_max_uploads(int limit);
 
541
void set_max_connections(int limit);
 
542
</pre>
 
543
</blockquote>
 
544
<p>These functions will set a global limit on the number of unchoked peers (uploads)
 
545
and the number of connections opened. The number of connections is set to a hard
 
546
minimum of at least two connections per torrent, so if you set a too low
 
547
connections limit, and open too many torrents, the limit will not be met. The
 
548
number of uploads is at least one per torrent.</p>
 
549
</div>
 
550
<div class="section" id="num-uploads-num-connections">
 
551
<h2>num_uploads() num_connections()</h2>
 
552
<blockquote>
 
553
<pre class="literal-block">
 
554
int num_uploads() const;
 
555
int num_connections() const;
 
556
</pre>
 
557
</blockquote>
 
558
<p>Returns the number of currently unchoked peers and the number of connections
 
559
(including half-open ones) respectively.</p>
 
560
</div>
 
561
<div class="section" id="set-max-half-open-connections-max-half-open-connections">
 
562
<h2>set_max_half_open_connections() max_half_open_connections()</h2>
 
563
<blockquote>
 
564
<pre class="literal-block">
 
565
void set_max_half_open_connections(int limit);
 
566
int max_half_open_connections() const;
 
567
</pre>
 
568
</blockquote>
 
569
<p>Sets the maximum number of half-open connections libtorrent will have when
 
570
connecting to peers. A half-open connection is one where connect() has been
 
571
called, but the connection still hasn't been established (nor failed). Windows
 
572
XP Service Pack 2 sets a default, system wide, limit of the number of half-open
 
573
connections to 10. So, this limit can be used to work nicer together with
 
574
other network applications on that system. The default is to have no limit,
 
575
and passing -1 as the limit, means to have no limit. When limiting the number
 
576
of simultaneous connection attempts, peers will be put in a queue waiting for
 
577
their turn to get connected.</p>
 
578
<p><tt class="docutils literal"><span class="pre">max_half_open_connections()</span></tt> returns the set limit. This limit defaults
 
579
to 8 on windows.</p>
 
580
</div>
 
581
<div class="section" id="set-ip-filter">
 
582
<h2>set_ip_filter()</h2>
 
583
<blockquote>
 
584
<pre class="literal-block">
 
585
void set_ip_filter(ip_filter const&amp; filter);
 
586
</pre>
 
587
</blockquote>
 
588
<p>Sets a filter that will be used to reject and accept incoming as well as outgoing
 
589
connections based on their originating ip address. The default filter will allow
 
590
connections to any ip address. To build a set of rules for which addresses are
 
591
accepted and not, see <a class="reference internal" href="#ip-filter">ip_filter</a>.</p>
 
592
<p>Each time a peer is blocked because of the IP filter, a <a class="reference internal" href="#peer-blocked-alert">peer_blocked_alert</a> is
 
593
generated.</p>
 
594
</div>
 
595
<div class="section" id="status">
 
596
<h2>status()</h2>
 
597
<blockquote>
 
598
<pre class="literal-block">
 
599
session_status status() const;
 
600
</pre>
 
601
</blockquote>
 
602
<p><tt class="docutils literal"><span class="pre">status()</span></tt> returns session wide-statistics and status. The <tt class="docutils literal"><span class="pre">session_status</span></tt>
 
603
struct has the following members:</p>
 
604
<pre class="literal-block">
 
605
struct session_status
 
606
{
 
607
        bool has_incoming_connections;
 
608
 
 
609
        float upload_rate;
 
610
        float download_rate;
 
611
 
 
612
        float payload_upload_rate;
 
613
        float payload_download_rate;
 
614
 
 
615
        size_type total_download;
 
616
        size_type total_upload;
 
617
 
 
618
        size_type total_payload_download;
 
619
        size_type total_payload_upload;
 
620
 
 
621
        int num_peers;
 
622
 
 
623
        int dht_nodes;
 
624
        int dht_cache_nodes;
 
625
        int dht_torrents;
 
626
        int dht_global_nodes;
 
627
};
 
628
</pre>
 
629
<p><tt class="docutils literal"><span class="pre">has_incoming_connections</span></tt> is false as long as no incoming connections have been
 
630
established on the listening socket. Every time you change the listen port, this will
 
631
be reset to false.</p>
 
632
<p><tt class="docutils literal"><span class="pre">upload_rate</span></tt>, <tt class="docutils literal"><span class="pre">download_rate</span></tt>, <tt class="docutils literal"><span class="pre">payload_download_rate</span></tt> and <tt class="docutils literal"><span class="pre">payload_upload_rate</span></tt>
 
633
are the total download and upload rates accumulated from all torrents. The payload
 
634
versions is the payload download only.</p>
 
635
<p><tt class="docutils literal"><span class="pre">total_download</span></tt> and <tt class="docutils literal"><span class="pre">total_upload</span></tt> are the total number of bytes downloaded and
 
636
uploaded to and from all torrents. <tt class="docutils literal"><span class="pre">total_payload_download</span></tt> and <tt class="docutils literal"><span class="pre">total_payload_upload</span></tt>
 
637
are the same thing but where only the payload is considered.</p>
 
638
<p><tt class="docutils literal"><span class="pre">num_peers</span></tt> is the total number of peer connections this session has. This includes
 
639
incoming connections that still hasn't sent their handshake or outgoing connections
 
640
that still hasn't completed the TCP connection. This number may be slightly higher
 
641
than the sum of all peers of all torrents because the incoming connections may not
 
642
be assigned a torrent yet.</p>
 
643
<p><tt class="docutils literal"><span class="pre">dht_nodes</span></tt>, <tt class="docutils literal"><span class="pre">dht_cache_nodes</span></tt> and <tt class="docutils literal"><span class="pre">dht_torrents</span></tt> are only available when
 
644
built with DHT support. They are all set to 0 if the DHT isn't running. When
 
645
the DHT is running, <tt class="docutils literal"><span class="pre">dht_nodes</span></tt> is set to the number of nodes in the routing
 
646
table. This number only includes <em>active</em> nodes, not cache nodes. The
 
647
<tt class="docutils literal"><span class="pre">dht_cache_nodes</span></tt> is set to the number of nodes in the node cache. These nodes
 
648
are used to replace the regular nodes in the routing table in case any of them
 
649
becomes unresponsive.</p>
 
650
<p><tt class="docutils literal"><span class="pre">dht_torrents</span></tt> are the number of torrents tracked by the DHT at the moment.</p>
 
651
<p><tt class="docutils literal"><span class="pre">dht_global_nodes</span></tt> is an estimation of the total number of nodes in the DHT
 
652
network.</p>
 
653
</div>
 
654
<div class="section" id="is-listening-listen-port-listen-on">
 
655
<h2>is_listening() listen_port() listen_on()</h2>
 
656
<blockquote>
 
657
<pre class="literal-block">
 
658
bool is_listening() const;
 
659
unsigned short listen_port() const;
 
660
bool listen_on(
 
661
        std::pair&lt;int, int&gt; const&amp; port_range
 
662
        , char const* interface = 0);
 
663
</pre>
 
664
</blockquote>
 
665
<p><tt class="docutils literal"><span class="pre">is_listening()</span></tt> will tell you whether or not the session has successfully
 
666
opened a listening port. If it hasn't, this function will return false, and
 
667
then you can use <tt class="docutils literal"><span class="pre">listen_on()</span></tt> to make another try.</p>
 
668
<p><tt class="docutils literal"><span class="pre">listen_port()</span></tt> returns the port we ended up listening on. Since you just pass
 
669
a port-range to the constructor and to <tt class="docutils literal"><span class="pre">listen_on()</span></tt>, to know which port it
 
670
ended up using, you have to ask the session using this function.</p>
 
671
<p><tt class="docutils literal"><span class="pre">listen_on()</span></tt> will change the listen port and/or the listen interface. If the
 
672
session is already listening on a port, this socket will be closed and a new socket
 
673
will be opened with these new settings. The port range is the ports it will try
 
674
to listen on, if the first port fails, it will continue trying the next port within
 
675
the range and so on. The interface parameter can be left as 0, in that case the
 
676
os will decide which interface to listen on, otherwise it should be the ip-address
 
677
of the interface you want the listener socket bound to. <tt class="docutils literal"><span class="pre">listen_on()</span></tt> returns true
 
678
if it managed to open the socket, and false if it failed. If it fails, it will also
 
679
generate an appropriate alert (<a class="reference internal" href="#listen-failed-alert">listen_failed_alert</a>).</p>
 
680
<p>The interface parameter can also be a hostname that will resolve to the device you
 
681
want to listen on.</p>
 
682
<p>If you're also starting the DHT, it is a good idea to do that after you've called
 
683
<tt class="docutils literal"><span class="pre">listen_on()</span></tt>, since the default listen port for the DHT is the same as the tcp
 
684
listen socket. If you start the DHT first, it will assume the tcp port is free and
 
685
open the udp socket on that port, then later, when <tt class="docutils literal"><span class="pre">listen_on()</span></tt> is called, it
 
686
may turn out that the tcp port is in use. That results in the DHT and the bittorrent
 
687
socket listening on different ports. If the DHT is active when <tt class="docutils literal"><span class="pre">listen_on</span></tt> is
 
688
called, the udp port will be rebound to the new port, if it was configured to use
 
689
the same port as the tcp socket, and if the listen_on call failed to bind to the
 
690
same port that the udp uses.</p>
 
691
<p>The reason why it's a good idea to run the DHT and the bittorrent socket on the same
 
692
port is because that is an assumption that may be used to increase performance. One
 
693
way to accelerate the connecting of peers on windows may be to first ping all peers
 
694
with a DHT ping packet, and connect to those that responds first. On windows one
 
695
can only connect to a few peers at a time because of a built in limitation (in XP
 
696
Service pack 2).</p>
 
697
</div>
 
698
<div class="section" id="pop-alert-set-severity-level-wait-for-alert">
 
699
<h2>pop_alert() set_severity_level() wait_for_alert()</h2>
 
700
<blockquote>
 
701
<pre class="literal-block">
 
702
std::auto_ptr&lt;alert&gt; pop_alert();
 
703
alert const* wait_for_alert(time_duration max_wait);
 
704
void set_severity_level(alert::severity_t s);
 
705
</pre>
 
706
</blockquote>
 
707
<p><tt class="docutils literal"><span class="pre">pop_alert()</span></tt> is used to ask the session if any errors or events has occurred. With
 
708
<tt class="docutils literal"><span class="pre">set_severity_level()</span></tt> you can filter how serious the event has to be for you to
 
709
receive it through <tt class="docutils literal"><span class="pre">pop_alert()</span></tt>. For information, see <a class="reference internal" href="#alerts">alerts</a>.</p>
 
710
<p><tt class="docutils literal"><span class="pre">wait_for_alert</span></tt> blocks until an alert is available, or for no more than <tt class="docutils literal"><span class="pre">max_wait</span></tt>
 
711
time. If <tt class="docutils literal"><span class="pre">wait_for_alert</span></tt> returns because of the time-out, and no alerts are available,
 
712
it returns 0. If at least one alert was generated, a pointer to that alert is returned.
 
713
The alert is not popped, any subsequent calls to <tt class="docutils literal"><span class="pre">wait_for_alert</span></tt> will return the
 
714
same pointer until the alert is popped by calling <tt class="docutils literal"><span class="pre">pop_alert</span></tt>. This is useful for
 
715
leaving any alert dispatching mechanism independent of this blocking call, the dispatcher
 
716
can be called and it can pop the alert independently.</p>
 
717
</div>
 
718
<div class="section" id="add-extension">
 
719
<h2>add_extension()</h2>
 
720
<blockquote>
 
721
<pre class="literal-block">
 
722
void add_extension(boost::function&lt;
 
723
        boost::shared_ptr&lt;torrent_plugin&gt;(torrent*, void*)&gt; ext);
 
724
</pre>
 
725
</blockquote>
 
726
<p>This function adds an extension to this session. The argument is a function
 
727
object that is called with a <tt class="docutils literal"><span class="pre">torrent*</span></tt> and which should return a
 
728
<tt class="docutils literal"><span class="pre">boost::shared_ptr&lt;torrent_plugin&gt;</span></tt>. To write custom plugins, see
 
729
<a class="reference external" href="libtorrent_plugins.html">libtorrent plugins</a>. The main plugins implemented in libtorrent are:</p>
 
730
<dl class="docutils">
 
731
<dt>metadata extension</dt>
 
732
<dd>Allows peers to download the metadata (.torren files) from the swarm
 
733
directly. Makes it possible to join a swarm with just a tracker and
 
734
info-hash.</dd>
 
735
<dt>uTorrent peer exchange</dt>
 
736
<dd>Exchanges peers between clients.</dd>
 
737
</dl>
 
738
<p>To use these, imclude <tt class="docutils literal"><span class="pre">&lt;libtorrent/extensions/metadata_transfer.hpp&gt;</span></tt>
 
739
or <tt class="docutils literal"><span class="pre">&lt;libtorrent/extensions/ut_pex.hpp&gt;</span></tt>. The functions to pass in to
 
740
<tt class="docutils literal"><span class="pre">add_extension()</span></tt> are <tt class="docutils literal"><span class="pre">libtorrent::create_metadata_plugin</span></tt> and
 
741
<tt class="docutils literal"><span class="pre">libtorrent::create_ut_pex_plugin</span></tt> respectively.</p>
 
742
<p>e.g.</p>
 
743
<pre class="literal-block">
 
744
ses.add_extension(&amp;libtorrent::create_metadata_plugin);
 
745
ses.add_extension(&amp;libtorrent::create_ut_pex_plugin);
 
746
</pre>
 
747
</div>
 
748
<div class="section" id="set-settings-set-pe-settings">
 
749
<h2>set_settings() set_pe_settings()</h2>
 
750
<blockquote>
 
751
<pre class="literal-block">
 
752
void set_settings(session_settings const&amp; settings);
 
753
void set_pe_settings(pe_settings const&amp; settings);
 
754
</pre>
 
755
</blockquote>
 
756
<p>Sets the session settings and the packet encryption settings respectively.
 
757
See <a class="reference internal" href="#session-settings">session_settings</a> and <a class="reference internal" href="#pe-settings">pe_settings</a> for more information on available
 
758
options.</p>
 
759
</div>
 
760
<div class="section" id="set-peer-proxy-set-web-seed-proxy-set-tracker-proxy-set-dht-proxy">
 
761
<h2>set_peer_proxy() set_web_seed_proxy() set_tracker_proxy() set_dht_proxy()</h2>
 
762
<blockquote>
 
763
<pre class="literal-block">
 
764
void set_peer_proxy(proxy_settings const&amp; s);
 
765
void set_web_seed_proxy(proxy_settings const&amp; s);
 
766
void set_tracker_proxy(proxy_settings const&amp; s);
 
767
void set_dht_proxy(proxy_settings const&amp; s);
 
768
</pre>
 
769
</blockquote>
 
770
<p>The <tt class="docutils literal"><span class="pre">set_dht_proxy</span></tt> is not available when DHT is disabled. These functions
 
771
sets the proxy settings for different kinds of connections, bittorrent peers,
 
772
web seeds, trackers and the DHT traffic.</p>
 
773
<p><tt class="docutils literal"><span class="pre">set_peer_proxy</span></tt> affects regular bittorrent peers. <tt class="docutils literal"><span class="pre">set_web_seed_proxy</span></tt>
 
774
affects only web seeds. see <a class="reference internal" href="#http-seeding">HTTP seeding</a>.</p>
 
775
<p><tt class="docutils literal"><span class="pre">set_tracker_proxy</span></tt> only affects HTTP tracker connections (UDP tracker
 
776
connections are affected if the given proxy supports UDP, e.g. SOCKS5).</p>
 
777
<p><tt class="docutils literal"><span class="pre">set_dht_proxy</span></tt> affects the DHT messages. Since they are sent over UDP,
 
778
it only has any effect if the proxy supports UDP.</p>
 
779
<p>For more information on what settings are available for proxies, see
 
780
<a class="reference internal" href="#proxy-settings">proxy_settings</a>.</p>
 
781
</div>
 
782
<div class="section" id="peer-proxy-web-seed-proxy-tracker-proxy-dht-proxy">
 
783
<h2>peer_proxy() web_seed_proxy() tracker_proxy() dht_proxy()</h2>
 
784
<blockquote>
 
785
<pre class="literal-block">
 
786
proxy_settings const&amp; peer_proxy() const;
 
787
proxy_settings const&amp; web_seed_proxy() const;
 
788
proxy_settings const&amp; tracker_proxy() const;
 
789
proxy_settings const&amp; dht_proxy() const;
 
790
</pre>
 
791
</blockquote>
 
792
<p>These functions returns references to their respective current settings.</p>
 
793
<p>The <tt class="docutils literal"><span class="pre">dht_proxy</span></tt> is not available when DHT is disabled.</p>
 
794
</div>
 
795
<div class="section" id="start-dht-stop-dht-set-dht-settings-dht-state">
 
796
<h2>start_dht() stop_dht() set_dht_settings() dht_state()</h2>
 
797
<blockquote>
 
798
<pre class="literal-block">
 
799
void start_dht(entry const&amp; startup_state);
 
800
void stop_dht();
 
801
void set_dht_settings(dht_settings const&amp; settings);
 
802
entry dht_state() const;
 
803
</pre>
 
804
</blockquote>
 
805
<p>These functions are not available in case <tt class="docutils literal"><span class="pre">TORRENT_DISABLE_DHT</span></tt> is
 
806
defined. <tt class="docutils literal"><span class="pre">start_dht</span></tt> starts the dht node and makes the trackerless service
 
807
available to torrents. The startup state is optional and can contain nodes
 
808
and the node id from the previous session. The dht node state is a bencoded
 
809
dictionary with the following entries:</p>
 
810
<dl class="docutils">
 
811
<dt><tt class="docutils literal"><span class="pre">nodes</span></tt></dt>
 
812
<dd>A list of strings, where each string is a node endpoint encoded in binary. If
 
813
the string is 6 bytes long, it is an IPv4 address of 4 bytes, encoded in
 
814
network byte order (big endian), followed by a 2 byte port number (also
 
815
network byte order). If the string is 18 bytes long, it is 16 bytes of IPv6
 
816
address followed by a 2 bytes port number (also network byte order).</dd>
 
817
<dt><tt class="docutils literal"><span class="pre">node-id</span></tt></dt>
 
818
<dd>The node id written as a readable string as a hexadecimal number.</dd>
 
819
</dl>
 
820
<p><tt class="docutils literal"><span class="pre">dht_state</span></tt> will return the current state of the dht node, this can be used
 
821
to start up the node again, passing this entry to <tt class="docutils literal"><span class="pre">start_dht</span></tt>. It is a good
 
822
idea to save this to disk when the session is closed, and read it up again
 
823
when starting.</p>
 
824
<p>If the port the DHT is supposed to listen on is already in use, and exception
 
825
is thrown, <tt class="docutils literal"><span class="pre">asio::error</span></tt>.</p>
 
826
<p><tt class="docutils literal"><span class="pre">stop_dht</span></tt> stops the dht node.</p>
 
827
<p><tt class="docutils literal"><span class="pre">add_dht_node</span></tt> adds a node to the routing table. This can be used if your
 
828
client has its own source of bootstrapping nodes.</p>
 
829
<p><tt class="docutils literal"><span class="pre">set_dht_settings</span></tt> sets some parameters availavle to the dht node. The
 
830
struct has the following members:</p>
 
831
<pre class="literal-block">
 
832
struct dht_settings
 
833
{
 
834
        int max_peers_reply;
 
835
        int search_branching;
 
836
        int service_port;
 
837
        int max_fail_count;
 
838
};
 
839
</pre>
 
840
<p><tt class="docutils literal"><span class="pre">max_peers_reply</span></tt> is the maximum number of peers the node will send in
 
841
response to a <tt class="docutils literal"><span class="pre">get_peers</span></tt> message from another node.</p>
 
842
<p><tt class="docutils literal"><span class="pre">search_branching</span></tt> is the number of concurrent search request the node will
 
843
send when announcing and refreshing the routing table. This parameter is
 
844
called alpha in the kademlia paper.</p>
 
845
<p><tt class="docutils literal"><span class="pre">service_port</span></tt> is the udp port the node will listen to. This will default
 
846
to 0, which means the udp listen port will be the same as the tcp listen
 
847
port. This is in general a good idea, since some NAT implementations
 
848
reserves the udp port for any mapped tcp port, and vice versa. NAT-PMP
 
849
guarantees this for example.</p>
 
850
<p><tt class="docutils literal"><span class="pre">max_fail_count</span></tt> is the maximum number of failed tries to contact a node
 
851
before it is removed from the routing table. If there are known working nodes
 
852
that are ready to replace a failing node, it will be replaced immediately,
 
853
this limit is only used to clear out nodes that don't have any node that can
 
854
replace them.</p>
 
855
</div>
 
856
<div class="section" id="add-dht-node-add-dht-router">
 
857
<h2>add_dht_node() add_dht_router()</h2>
 
858
<blockquote>
 
859
<pre class="literal-block">
 
860
void add_dht_node(std::pair&lt;std::string, int&gt; const&amp; node);
 
861
void add_dht_router(std::pair&lt;std::string, int&gt; const&amp; node);
 
862
</pre>
 
863
</blockquote>
 
864
<p><tt class="docutils literal"><span class="pre">add_dht_node</span></tt> takes a host name and port pair. That endpoint will be
 
865
pinged, and if a valid DHT reply is received, the node will be added to
 
866
the routing table.</p>
 
867
<p><tt class="docutils literal"><span class="pre">add_dht_router</span></tt> adds the given endpoint to a list of DHT router nodes.
 
868
If a search is ever made while the routing table is empty, those nodes will
 
869
be used as backups. Nodes in the router node list will also never be added
 
870
to the regular routing table, which effectively means they are only used
 
871
for bootstrapping, to keep the load off them.</p>
 
872
<p>An example routing node that you could typically add is
 
873
<tt class="docutils literal"><span class="pre">router.bittorrent.com</span></tt>.</p>
 
874
</div>
 
875
<div class="section" id="start-lsd-stop-lsd">
 
876
<h2>start_lsd() stop_lsd()</h2>
 
877
<blockquote>
 
878
<pre class="literal-block">
 
879
void start_lsd();
 
880
void stop_lsd();
 
881
</pre>
 
882
</blockquote>
 
883
<p>Starts and stops Local Service Discovery. This service will broadcast
 
884
the infohashes of all the non-private torrents on the local network to
 
885
look for peers on the same swarm within multicast reach.</p>
 
886
<p>It is turned off by default.</p>
 
887
</div>
 
888
<div class="section" id="start-upnp-stop-upnp">
 
889
<h2>start_upnp() stop_upnp()</h2>
 
890
<blockquote>
 
891
<pre class="literal-block">
 
892
void start_upnp();
 
893
void stop_upnp();
 
894
</pre>
 
895
</blockquote>
 
896
<p>Starts and stops the UPnP service. When started, the listen port and the DHT
 
897
port are attempted to be forwarded on local UPnP router devices.</p>
 
898
<p>It is off by default.</p>
 
899
</div>
 
900
<div class="section" id="start-natpmp-stop-natpmp">
 
901
<h2>start_natpmp() stop_natpmp()</h2>
 
902
<blockquote>
 
903
<pre class="literal-block">
 
904
void start_natpmp();
 
905
void stop_natpmp();
 
906
</pre>
 
907
</blockquote>
 
908
<p>Starts and stops the NAT-PMP service. When started, the listen port and the DHT
 
909
port are attempted to be forwarded on the router through NAT-PMP.</p>
 
910
<p>It is off by default.</p>
 
911
</div>
 
912
</div>
 
913
<div class="section" id="entry">
 
914
<h1>entry</h1>
 
915
<p>The <tt class="docutils literal"><span class="pre">entry</span></tt> class represents one node in a bencoded hierarchy. It works as a
 
916
variant type, it can be either a list, a dictionary (<tt class="docutils literal"><span class="pre">std::map</span></tt>), an integer
 
917
or a string. This is its synopsis:</p>
 
918
<pre class="literal-block">
 
919
class entry
 
920
{
 
921
public:
 
922
 
 
923
        typedef std::map&lt;std::string, entry&gt; dictionary_type;
 
924
        typedef std::string string_type;
 
925
        typedef std::list&lt;entry&gt; list_type;
 
926
        typedef size_type integer_type;
 
927
 
 
928
        enum data_type
 
929
        {
 
930
                int_t,
 
931
                string_t,
 
932
                list_t,
 
933
                dictionary_t,
 
934
                undefined_t
 
935
        };
 
936
 
 
937
        data_type type() const;
 
938
 
 
939
        entry(dictionary_type const&amp;);
 
940
        entry(string_type const&amp;);
 
941
        entry(list_type const&amp;);
 
942
        entry(integer_type const&amp;);
 
943
 
 
944
        entry();
 
945
        entry(data_type t);
 
946
        entry(entry const&amp; e);
 
947
        ~entry();
 
948
 
 
949
        void operator=(entry const&amp; e);
 
950
        void operator=(dictionary_type const&amp;);
 
951
        void operator=(string_type const&amp;);
 
952
        void operator=(list_type const&amp;);
 
953
        void operator=(integer_type const&amp;);
 
954
 
 
955
        integer_type&amp; integer();
 
956
        integer_type const&amp; integer() const;
 
957
        string_type&amp; string();
 
958
        string_type const&amp; string() const;
 
959
        list_type&amp; list();
 
960
        list_type const&amp; list() const;
 
961
        dictionary_type&amp; dict();
 
962
        dictionary_type const&amp; dict() const;
 
963
 
 
964
        // these functions requires that the entry
 
965
        // is a dictionary, otherwise they will throw
 
966
        entry&amp; operator[](char const* key);
 
967
        entry&amp; operator[](std::string const&amp; key);
 
968
        entry const&amp; operator[](char const* key) const;
 
969
        entry const&amp; operator[](std::string const&amp; key) const;
 
970
        entry* find_key(char const* key);
 
971
        entry const* find_key(char const* key) const;
 
972
 
 
973
        void print(std::ostream&amp; os, int indent = 0) const;
 
974
};
 
975
</pre>
 
976
<p><em>TODO: finish documentation of entry.</em></p>
 
977
<div class="section" id="integer-string-list-dict-type">
 
978
<h2>integer() string() list() dict() type()</h2>
 
979
<blockquote>
 
980
<pre class="literal-block">
 
981
integer_type&amp; integer();
 
982
integer_type const&amp; integer() const;
 
983
string_type&amp; string();
 
984
string_type const&amp; string() const;
 
985
list_type&amp; list();
 
986
list_type const&amp; list() const;
 
987
dictionary_type&amp; dict();
 
988
dictionary_type const&amp; dict() const;
 
989
</pre>
 
990
</blockquote>
 
991
<p>The <tt class="docutils literal"><span class="pre">integer()</span></tt>, <tt class="docutils literal"><span class="pre">string()</span></tt>, <tt class="docutils literal"><span class="pre">list()</span></tt> and <tt class="docutils literal"><span class="pre">dict()</span></tt> functions
 
992
are accessors that return the respective type. If the <tt class="docutils literal"><span class="pre">entry</span></tt> object isn't of the
 
993
type you request, the accessor will throw <a class="reference internal" href="#type-error">type_error</a> (which derives from
 
994
<tt class="docutils literal"><span class="pre">std::runtime_error</span></tt>). You can ask an <tt class="docutils literal"><span class="pre">entry</span></tt> for its type through the
 
995
<tt class="docutils literal"><span class="pre">type()</span></tt> function.</p>
 
996
<p>The <tt class="docutils literal"><span class="pre">print()</span></tt> function is there for debug purposes only.</p>
 
997
<p>If you want to create an <tt class="docutils literal"><span class="pre">entry</span></tt> you give it the type you want it to have in its
 
998
constructor, and then use one of the non-const accessors to get a reference which you then
 
999
can assign the value you want it to have.</p>
 
1000
<p>The typical code to get info from a torrent file will then look like this:</p>
 
1001
<pre class="literal-block">
 
1002
entry torrent_file;
 
1003
// ...
 
1004
 
 
1005
// throws if this is not a dictionary
 
1006
entry::dictionary_type const&amp; dict = torrent_file.dict();
 
1007
entry::dictionary_type::const_iterator i;
 
1008
i = dict.find(&quot;announce&quot;);
 
1009
if (i != dict.end())
 
1010
{
 
1011
        std::string tracker_url = i-&gt;second.string();
 
1012
        std::cout &lt;&lt; tracker_url &lt;&lt; &quot;\n&quot;;
 
1013
}
 
1014
</pre>
 
1015
<p>The following code is equivalent, but a little bit shorter:</p>
 
1016
<pre class="literal-block">
 
1017
entry torrent_file;
 
1018
// ...
 
1019
 
 
1020
// throws if this is not a dictionary
 
1021
if (entry* i = torrent_file.find_key(&quot;announce&quot;))
 
1022
{
 
1023
        std::string tracker_url = i-&gt;string();
 
1024
        std::cout &lt;&lt; tracker_url &lt;&lt; &quot;\n&quot;;
 
1025
}
 
1026
</pre>
 
1027
<p>To make it easier to extract information from a torrent file, the class <a class="reference internal" href="#torrent-info">torrent_info</a>
 
1028
exists.</p>
 
1029
</div>
 
1030
<div class="section" id="operator">
 
1031
<h2>operator[]</h2>
 
1032
<blockquote>
 
1033
<pre class="literal-block">
 
1034
entry&amp; operator[](char const* key);
 
1035
entry&amp; operator[](std::string const&amp; key);
 
1036
entry const&amp; operator[](char const* key) const;
 
1037
entry const&amp; operator[](std::string const&amp; key) const;
 
1038
</pre>
 
1039
</blockquote>
 
1040
<p>All of these functions requires the entry to be a dictionary, if it isn't they
 
1041
will throw <tt class="docutils literal"><span class="pre">libtorrent::type_error</span></tt>.</p>
 
1042
<p>The non-const versions of the <tt class="docutils literal"><span class="pre">operator[]</span></tt> will return a reference to either
 
1043
the existing element at the given key or, if there is no element with the
 
1044
given key, a reference to a newly inserted element at that key.</p>
 
1045
<p>The const version of <tt class="docutils literal"><span class="pre">operator[]</span></tt> will only return a reference to an
 
1046
existing element at the given key. If the key is not found, it will throw
 
1047
<tt class="docutils literal"><span class="pre">libtorrent::type_error</span></tt>.</p>
 
1048
</div>
 
1049
<div class="section" id="find-key">
 
1050
<h2>find_key()</h2>
 
1051
<blockquote>
 
1052
<pre class="literal-block">
 
1053
entry* find_key(char const* key);
 
1054
entry const* find_key(char const* key) const;
 
1055
</pre>
 
1056
</blockquote>
 
1057
<p>These functions requires the entry to be a dictionary, if it isn't they
 
1058
will throw <tt class="docutils literal"><span class="pre">libtorrent::type_error</span></tt>.</p>
 
1059
<p>They will look for an element at the given key in the dictionary, if the
 
1060
element cannot be found, they will return 0. If an element with the given
 
1061
key is found, the return a pointer to it.</p>
 
1062
</div>
 
1063
</div>
 
1064
<div class="section" id="torrent-info">
 
1065
<h1>torrent_info</h1>
 
1066
<p>The <tt class="docutils literal"><span class="pre">torrent_info</span></tt> has the following synopsis:</p>
 
1067
<pre class="literal-block">
 
1068
class torrent_info
 
1069
{
 
1070
public:
 
1071
 
 
1072
        torrent_info();
 
1073
        torrent_info(sha1_hash const&amp; info_hash);
 
1074
        torrent_info(entry const&amp; torrent_file);
 
1075
 
 
1076
        entry create_torrent() const;
 
1077
        void set_comment(char const* str);
 
1078
        void set_piece_size(int size);
 
1079
        void set_creator(char const* str);
 
1080
        void set_hash(int index, sha1_hash const&amp; h);
 
1081
        void add_tracker(std::string const&amp; url, int tier = 0);
 
1082
        void add_file(boost::filesystem::path file, size_type size);
 
1083
        void add_url_seed(std::string const&amp; url);
 
1084
 
 
1085
        typedef std::vector&lt;file_entry&gt;::const_iterator file_iterator;
 
1086
        typedef std::vector&lt;file_entry&gt;::const_reverse_iterator
 
1087
                reverse_file_iterator;
 
1088
 
 
1089
        bool remap_files(std::vector&lt;file_entry&gt; const&amp; map);
 
1090
 
 
1091
        file_iterator begin_files(bool storage = false) const;
 
1092
        file_iterator end_files(bool storage = false) const;
 
1093
        reverse_file_iterator rbegin_files(bool storage = false) const;
 
1094
        reverse_file_iterator rend_files(bool storage = false) const;
 
1095
 
 
1096
        int num_files(bool storage = false) const;
 
1097
        file_entry const&amp; file_at(int index, bool storage = false) const;
 
1098
 
 
1099
        std::vector&lt;file_slice&gt; map_block(int piece, size_type offset
 
1100
                , int size, bool storage = false) const;
 
1101
        peer_request map_file(int file_index, size_type file_offset
 
1102
                , int size, bool storage = false) const;
 
1103
 
 
1104
        std::vector&lt;announce_entry&gt; const&amp; trackers() const;
 
1105
 
 
1106
        bool priv() const;
 
1107
        void set_priv(bool v);
 
1108
 
 
1109
        std::vector&lt;std::string&gt; const&amp; url_seeds() const;
 
1110
 
 
1111
        size_type total_size() const;
 
1112
        size_type piece_length() const;
 
1113
        int num_pieces() const;
 
1114
        sha1_hash const&amp; info_hash() const;
 
1115
        std::string const&amp; name() const;
 
1116
        std::string const&amp; comment() const;
 
1117
        std::string const&amp; creator() const;
 
1118
 
 
1119
        std::vector&lt;std::pair&lt;std::string, int&gt; &gt; const&amp; nodes() const;
 
1120
        void add_node(std::pair&lt;std::string, int&gt; const&amp; node);
 
1121
 
 
1122
        boost::optional&lt;boost::posix_time::ptime&gt;
 
1123
        creation_date() const;
 
1124
 
 
1125
        void print(std::ostream&amp; os) const;
 
1126
 
 
1127
        size_type piece_size(unsigned int index) const;
 
1128
        sha1_hash const&amp; hash_for_piece(unsigned int index) const;
 
1129
};
 
1130
</pre>
 
1131
<div class="section" id="id3">
 
1132
<h2>torrent_info()</h2>
 
1133
<blockquote>
 
1134
<pre class="literal-block">
 
1135
torrent_info();
 
1136
torrent_info(sha1_hash const&amp; info_hash);
 
1137
torrent_info(entry const&amp; torrent_file);
 
1138
</pre>
 
1139
</blockquote>
 
1140
<p>The default constructor of <tt class="docutils literal"><span class="pre">torrent_info</span></tt> is used when creating torrent files. It will
 
1141
initialize the object to an empty torrent, containing no files. The info hash will be set
 
1142
to 0 when this constructor is used. To use the empty <tt class="docutils literal"><span class="pre">torrent_info</span></tt> object, add files
 
1143
and piece hashes, announce URLs and optionally a creator tag and comment. To do this you
 
1144
use the members <tt class="docutils literal"><span class="pre">set_comment()</span></tt>, <tt class="docutils literal"><span class="pre">set_piece_size()</span></tt>, <tt class="docutils literal"><span class="pre">set_creator()</span></tt>, <tt class="docutils literal"><span class="pre">set_hash()</span></tt>
 
1145
etc.</p>
 
1146
<p>The constructor that takes an info-hash is identical to the default constructor with the
 
1147
exception that it will initialize the info-hash to the given value. This is used internally
 
1148
when downloading torrents without the metadata. The metadata will be created by libtorrent
 
1149
as soon as it has been downloaded from the swarm.</p>
 
1150
<p>The last constructor is the one that is used in most cases. It will create a <tt class="docutils literal"><span class="pre">torrent_info</span></tt>
 
1151
object from the information found in the given torrent_file. The <tt class="docutils literal"><span class="pre">entry</span></tt> represents a tree
 
1152
node in an bencoded file. To load an ordinary .torrent file into an <tt class="docutils literal"><span class="pre">entry</span></tt>, use bdecode(),
 
1153
see <a class="reference internal" href="#bdecode-bencode">bdecode() bencode()</a>.</p>
 
1154
</div>
 
1155
<div class="section" id="set-comment-set-piece-size-set-creator-set-hash-add-tracker-add-file">
 
1156
<h2>set_comment() set_piece_size() set_creator() set_hash() add_tracker() add_file()</h2>
 
1157
<blockquote>
 
1158
<pre class="literal-block">
 
1159
void set_comment(char const* str);
 
1160
void set_piece_size(int size);
 
1161
void set_creator(char const* str);
 
1162
void set_hash(int index, sha1_hash const&amp; h);
 
1163
void add_tracker(std::string const&amp; url, int tier = 0);
 
1164
void add_file(boost::filesystem::path file, size_type size);
 
1165
</pre>
 
1166
</blockquote>
 
1167
<p>These files are used when creating a torrent file. <tt class="docutils literal"><span class="pre">set_comment()</span></tt> will simply set
 
1168
the comment that belongs to this torrent. The comment can be retrieved with the
 
1169
<tt class="docutils literal"><span class="pre">comment()</span></tt> member. The string should be UTF-8 encoded.</p>
 
1170
<p><tt class="docutils literal"><span class="pre">set_piece_size()</span></tt> will set the size of each piece in this torrent. The piece size must
 
1171
be an even multiple of 2. i.e. usually something like 256 kiB, 512 kiB, 1024 kiB etc. The
 
1172
size is given in number of bytes.</p>
 
1173
<p><tt class="docutils literal"><span class="pre">set_creator()</span></tt> is an optional attribute that can be used to identify your application
 
1174
that was used to create the torrent file. The string should be UTF-8 encoded.</p>
 
1175
<p><tt class="docutils literal"><span class="pre">set_hash()</span></tt> writes the hash for the piece with the given piece-index. You have to call
 
1176
this function for every piece in the torrent. Usually the <a class="reference internal" href="#hasher">hasher</a> is used to calculate
 
1177
the sha1-hash for a piece.</p>
 
1178
<p><tt class="docutils literal"><span class="pre">add_tracker()</span></tt> adds a tracker to the announce-list. The <tt class="docutils literal"><span class="pre">tier</span></tt> determines the order in
 
1179
which the trackers are to be tried. For more information see <a class="reference internal" href="#trackers">trackers()</a>.</p>
 
1180
<p><tt class="docutils literal"><span class="pre">add_file()</span></tt> adds a file to the torrent. The order in which you add files will determine
 
1181
the order in which they are placed in the torrent file. You have to add at least one file
 
1182
to the torrent. The <tt class="docutils literal"><span class="pre">path</span></tt> you give has to be a relative path from the root directory
 
1183
of the torrent. The <tt class="docutils literal"><span class="pre">size</span></tt> is given in bytes.</p>
 
1184
<p>When you have added all the files and hashes to your torrent, you can generate an <tt class="docutils literal"><span class="pre">entry</span></tt>
 
1185
which then can be encoded as a .torrent file. You do this by calling <a class="reference internal" href="#create-torrent">create_torrent()</a>.</p>
 
1186
<p>For a complete example of how to create a torrent from a file structure, see <a class="reference external" href="examples.html#make_torrent">make_torrent</a>.</p>
 
1187
</div>
 
1188
<div class="section" id="create-torrent">
 
1189
<h2>create_torrent()</h2>
 
1190
<blockquote>
 
1191
<pre class="literal-block">
 
1192
entry create_torrent();
 
1193
</pre>
 
1194
</blockquote>
 
1195
<p>Returns an <tt class="docutils literal"><span class="pre">entry</span></tt> representing the bencoded tree of data that makes up a .torrent file.
 
1196
You can save this data as a torrent file with bencode() (see <a class="reference internal" href="#bdecode-bencode">bdecode() bencode()</a>), for a
 
1197
complete example, see <a class="reference external" href="examples.html#make_torrent">make_torrent</a>.</p>
 
1198
<p>This function is not const because it will also set the info-hash of the <tt class="docutils literal"><span class="pre">torrent_info</span></tt>
 
1199
object.</p>
 
1200
<p>Note that a torrent file must include at least one file, and it must have at
 
1201
least one tracker url or at least one DHT node.</p>
 
1202
</div>
 
1203
<div class="section" id="remap-files">
 
1204
<h2>remap_files()</h2>
 
1205
<blockquote>
 
1206
<pre class="literal-block">
 
1207
bool remap_files(std::vector&lt;file_entry&gt; const&amp; map);
 
1208
</pre>
 
1209
</blockquote>
 
1210
<p>This call will create a new mapping of the data in this torrent to other files. The
 
1211
<tt class="docutils literal"><span class="pre">torrent_info</span></tt> maintains 2 views of the file storage. One that is true to the torrent
 
1212
file, and one that represents what is actually saved on disk. This call will change
 
1213
what the files on disk are called.</p>
 
1214
<p>The each entry in the vector <tt class="docutils literal"><span class="pre">map</span></tt> is a <tt class="docutils literal"><span class="pre">file_entry</span></tt>. The only fields in this struct
 
1215
that are used in this case are <tt class="docutils literal"><span class="pre">path</span></tt>, <tt class="docutils literal"><span class="pre">size</span></tt> and <tt class="docutils literal"><span class="pre">file_base</span></tt>.</p>
 
1216
<p>The return value indicates if the remap was successful or not. True means success and
 
1217
false means failure. The sum of all the files passed in through <tt class="docutils literal"><span class="pre">map</span></tt> has to be exactly
 
1218
the same as the total_size of the torrent. If the number of bytes that are mapped do not
 
1219
match, false will be returned (this is the only case this function may fail).</p>
 
1220
<p>Changing this mapping for an existing torrent will not move or rename files. If some files
 
1221
should be renamed, this can be done before the torrent is added.</p>
 
1222
</div>
 
1223
<div class="section" id="begin-files-end-files-rbegin-files-rend-files">
 
1224
<h2>begin_files() end_files() rbegin_files() rend_files()</h2>
 
1225
<blockquote>
 
1226
<pre class="literal-block">
 
1227
file_iterator begin_files(bool storage = false) const;
 
1228
file_iterator end_files(bool storage = false) const;
 
1229
reverse_file_iterator rbegin_files(bool storage = false) const;
 
1230
reverse_file_iterator rend_files(bool storage = false) const;
 
1231
</pre>
 
1232
</blockquote>
 
1233
<p>This class will need some explanation. First of all, to get a list of all files
 
1234
in the torrent, you can use <tt class="docutils literal"><span class="pre">begin_files()</span></tt>, <tt class="docutils literal"><span class="pre">end_files()</span></tt>,
 
1235
<tt class="docutils literal"><span class="pre">rbegin_files()</span></tt> and <tt class="docutils literal"><span class="pre">rend_files()</span></tt>. These will give you standard vector
 
1236
iterators with the type <tt class="docutils literal"><span class="pre">file_entry</span></tt>.</p>
 
1237
<p>The <tt class="docutils literal"><span class="pre">storage</span></tt> parameter specifies which view of the files you want. The default
 
1238
is false, which means you will see the content of the torrent file. If set to
 
1239
true, you will see the file that the storage class uses to save the files to
 
1240
disk. Typically these views are the same, but in case the files have been
 
1241
remapped, they may differ. For more info, see <a class="reference internal" href="#remap-files">remap_files()</a>.</p>
 
1242
<pre class="literal-block">
 
1243
struct file_entry
 
1244
{
 
1245
        boost::filesystem::path path;
 
1246
        size_type offset;
 
1247
        size_type size;
 
1248
        size_type file_base;
 
1249
        boost::shared_ptr&lt;const boost::filesystem::path&gt; orig_path;
 
1250
};
 
1251
</pre>
 
1252
<p>The <tt class="docutils literal"><span class="pre">path</span></tt> is the full (relative) path of each file. i.e. if it is a multi-file
 
1253
torrent, all the files starts with a directory with the same name as <tt class="docutils literal"><span class="pre">torrent_info::name()</span></tt>.
 
1254
The filenames are encoded with UTF-8.</p>
 
1255
<p><tt class="docutils literal"><span class="pre">size</span></tt> is the size of the file (in bytes) and <tt class="docutils literal"><span class="pre">offset</span></tt> is the byte offset
 
1256
of the file within the torrent. i.e. the sum of all the sizes of the files
 
1257
before it in the list.</p>
 
1258
<p><tt class="docutils literal"><span class="pre">file_base</span></tt> is the offset in the file where the storage should start. The normal
 
1259
case is to have this set to 0, so that the storage starts saving data at the start
 
1260
if the file. In cases where multiple files are mapped into the same file though,
 
1261
the <tt class="docutils literal"><span class="pre">file_base</span></tt> should be set to an offset so that the different regions do
 
1262
not overlap. This is used when mapping &quot;unselected&quot; files into a so-called part
 
1263
file.</p>
 
1264
<p><tt class="docutils literal"><span class="pre">orig_path</span></tt> is set to 0 in case the path element is an exact copy of that
 
1265
found in the metadata. In case the path in the original metadata was
 
1266
incorrectly encoded, and had to be fixed in order to be acceptable utf-8,
 
1267
the original string is preserved in <tt class="docutils literal"><span class="pre">orig_path</span></tt>. The reason to keep it
 
1268
is to be able to reproduce the info-section exactly, with the correct
 
1269
info-hash.</p>
 
1270
</div>
 
1271
<div class="section" id="num-files-file-at">
 
1272
<h2>num_files() file_at()</h2>
 
1273
<blockquote>
 
1274
<pre class="literal-block">
 
1275
int num_files(bool storage = false) const;
 
1276
file_entry const&amp; file_at(int index, bool storage = false) const;
 
1277
</pre>
 
1278
</blockquote>
 
1279
<p>If you need index-access to files you can use the <tt class="docutils literal"><span class="pre">num_files()</span></tt> and <tt class="docutils literal"><span class="pre">file_at()</span></tt>
 
1280
to access files using indices.</p>
 
1281
<p>The <tt class="docutils literal"><span class="pre">storage</span></tt> parameter specifies which view of the files you want. The default
 
1282
is false, which means you will see the content of the torrent file. If set to
 
1283
true, you will see the file that the storage class uses to save the files to
 
1284
disk. Typically these views are the same, but in case the files have been
 
1285
remapped, they may differ. For more info, see <a class="reference internal" href="#remap-files">remap_files()</a>.</p>
 
1286
</div>
 
1287
<div class="section" id="map-block">
 
1288
<h2>map_block()</h2>
 
1289
<blockquote>
 
1290
<pre class="literal-block">
 
1291
std::vector&lt;file_slice&gt; map_block(int piece, size_type offset
 
1292
        , int size, bool storage = false) const;
 
1293
</pre>
 
1294
</blockquote>
 
1295
<p>This function will map a piece index, a byte offset within that piece and
 
1296
a size (in bytes) into the corresponding files with offsets where that data
 
1297
for that piece is supposed to be stored.</p>
 
1298
<p>The file slice struct looks like this:</p>
 
1299
<pre class="literal-block">
 
1300
struct file_slice
 
1301
{
 
1302
        int file_index;
 
1303
        size_type offset;
 
1304
        size_type size;
 
1305
};
 
1306
</pre>
 
1307
<p>The <tt class="docutils literal"><span class="pre">file_index</span></tt> refers to the index of the file (in the torrent_info).
 
1308
To get the path and filename, use <tt class="docutils literal"><span class="pre">file_at()</span></tt> and give the <tt class="docutils literal"><span class="pre">file_index</span></tt>
 
1309
as argument. The <tt class="docutils literal"><span class="pre">offset</span></tt> is the byte offset in the file where the range
 
1310
starts, and <tt class="docutils literal"><span class="pre">size</span></tt> is the number of bytes this range is. The size + offset
 
1311
will never be greater than the file size.</p>
 
1312
<p>The <tt class="docutils literal"><span class="pre">storage</span></tt> parameter specifies which view of the files you want. The default
 
1313
is false, which means you will see the content of the torrent file. If set to
 
1314
true, you will see the file that the storage class uses to save the files to
 
1315
disk. Typically these views are the same, but in case the files have been
 
1316
remapped, they may differ. For more info, see <a class="reference internal" href="#remap-files">remap_files()</a>.</p>
 
1317
</div>
 
1318
<div class="section" id="map-file">
 
1319
<h2>map_file()</h2>
 
1320
<blockquote>
 
1321
<pre class="literal-block">
 
1322
peer_request map_file(int file_index, size_type file_offset
 
1323
        , int size, bool storage = false) const;
 
1324
</pre>
 
1325
</blockquote>
 
1326
<p>This function will map a range in a specific file into a range in the torrent.
 
1327
The <tt class="docutils literal"><span class="pre">file_offset</span></tt> parameter is the offset in the file, given in bytes, where
 
1328
0 is the start of the file.
 
1329
The <tt class="docutils literal"><span class="pre">peer_request</span></tt> structure looks like this:</p>
 
1330
<pre class="literal-block">
 
1331
struct peer_request
 
1332
{
 
1333
        int piece;
 
1334
        int start;
 
1335
        int length;
 
1336
        bool operator==(peer_request const&amp; r) const;
 
1337
};
 
1338
</pre>
 
1339
<p><tt class="docutils literal"><span class="pre">piece</span></tt> is the index of the piece in which the range starts.
 
1340
<tt class="docutils literal"><span class="pre">start</span></tt> is the offset within that piece where the range starts.
 
1341
<tt class="docutils literal"><span class="pre">length</span></tt> is the size of the range, in bytes.</p>
 
1342
<p>The input range is assumed to be valid within the torrent. <tt class="docutils literal"><span class="pre">file_offset</span></tt>
 
1343
+ <tt class="docutils literal"><span class="pre">size</span></tt> is not allowed to be greater than the file size. <tt class="docutils literal"><span class="pre">file_index</span></tt>
 
1344
must refer to a valid file, i.e. it cannot be &gt;= <tt class="docutils literal"><span class="pre">num_files()</span></tt>.</p>
 
1345
</div>
 
1346
<div class="section" id="url-seeds-add-url-seed">
 
1347
<h2>url_seeds() add_url_seed()</h2>
 
1348
<blockquote>
 
1349
<pre class="literal-block">
 
1350
std::vector&lt;std::string&gt; const&amp; url_seeds() const;
 
1351
void add_url_seed(std::string const&amp; url);
 
1352
</pre>
 
1353
</blockquote>
 
1354
<p>If there are any url-seeds in this torrent, <tt class="docutils literal"><span class="pre">url_seeds()</span></tt> will return a
 
1355
vector of those urls. If you're creating a torrent file, <tt class="docutils literal"><span class="pre">add_url_seed()</span></tt>
 
1356
adds one url to the list of url-seeds. Currently, the only transport protocol
 
1357
supported for the url is http.</p>
 
1358
<p>The <tt class="docutils literal"><span class="pre">storage</span></tt> parameter specifies which view of the files you want. The default
 
1359
is false, which means you will see the content of the torrent file. If set to
 
1360
true, you will see the file that the storage class uses to save the files to
 
1361
disk. Typically these views are the same, but in case the files have been
 
1362
remapped, they may differ. For more info, see <a class="reference internal" href="#remap-files">remap_files()</a>.</p>
 
1363
<p>See <a class="reference internal" href="#http-seeding">HTTP seeding</a> for more information.</p>
 
1364
</div>
 
1365
<div class="section" id="print">
 
1366
<h2>print()</h2>
 
1367
<blockquote>
 
1368
<pre class="literal-block">
 
1369
void print(std::ostream&amp; os) const;
 
1370
</pre>
 
1371
</blockquote>
 
1372
<p>The <tt class="docutils literal"><span class="pre">print()</span></tt> function is there for debug purposes only. It will print the info from
 
1373
the torrent file to the given outstream. This function has been deprecated and will
 
1374
be removed from future releases.</p>
 
1375
</div>
 
1376
<div class="section" id="trackers">
 
1377
<h2>trackers()</h2>
 
1378
<blockquote>
 
1379
<pre class="literal-block">
 
1380
std::vector&lt;announce_entry&gt; const&amp; trackers() const;
 
1381
</pre>
 
1382
</blockquote>
 
1383
<p>The <tt class="docutils literal"><span class="pre">trackers()</span></tt> function will return a sorted vector of <tt class="docutils literal"><span class="pre">announce_entry</span></tt>.
 
1384
Each announce entry contains a string, which is the tracker url, and a tier index. The
 
1385
tier index is the high-level priority. No matter which trackers that works or not, the
 
1386
ones with lower tier will always be tried before the one with higher tier number.</p>
 
1387
<pre class="literal-block">
 
1388
struct announce_entry
 
1389
{
 
1390
        announce_entry(std::string const&amp; url);
 
1391
        std::string url;
 
1392
        int tier;
 
1393
};
 
1394
</pre>
 
1395
</div>
 
1396
<div class="section" id="total-size-piece-length-piece-size-num-pieces">
 
1397
<h2>total_size() piece_length() piece_size() num_pieces()</h2>
 
1398
<blockquote>
 
1399
<pre class="literal-block">
 
1400
size_type total_size() const;
 
1401
size_type piece_length() const;
 
1402
size_type piece_size(unsigned int index) const;
 
1403
int num_pieces() const;
 
1404
</pre>
 
1405
</blockquote>
 
1406
<p><tt class="docutils literal"><span class="pre">total_size()</span></tt>, <tt class="docutils literal"><span class="pre">piece_length()</span></tt> and <tt class="docutils literal"><span class="pre">num_pieces()</span></tt> returns the total
 
1407
number of bytes the torrent-file represents (all the files in it), the number of byte for
 
1408
each piece and the total number of pieces, respectively. The difference between
 
1409
<tt class="docutils literal"><span class="pre">piece_size()</span></tt> and <tt class="docutils literal"><span class="pre">piece_length()</span></tt> is that <tt class="docutils literal"><span class="pre">piece_size()</span></tt> takes
 
1410
the piece index as argument and gives you the exact size of that piece. It will always
 
1411
be the same as <tt class="docutils literal"><span class="pre">piece_length()</span></tt> except in the case of the last piece, which may
 
1412
be smaller.</p>
 
1413
</div>
 
1414
<div class="section" id="hash-for-piece-info-hash">
 
1415
<h2>hash_for_piece() info_hash()</h2>
 
1416
<blockquote>
 
1417
<pre class="literal-block">
 
1418
size_type piece_size(unsigned int index) const;
 
1419
sha1_hash const&amp; hash_for_piece(unsigned int index) const;
 
1420
</pre>
 
1421
</blockquote>
 
1422
<p><tt class="docutils literal"><span class="pre">hash_for_piece()</span></tt> takes a piece-index and returns the 20-bytes sha1-hash for that
 
1423
piece and <tt class="docutils literal"><span class="pre">info_hash()</span></tt> returns the 20-bytes sha1-hash for the info-section of the
 
1424
torrent file. For more information on the <tt class="docutils literal"><span class="pre">sha1_hash</span></tt>, see the <a class="reference internal" href="#big-number">big_number</a> class.
 
1425
<tt class="docutils literal"><span class="pre">info_hash()</span></tt> will only return a valid hash if the torrent_info was read from a
 
1426
<tt class="docutils literal"><span class="pre">.torrent</span></tt> file or if an <tt class="docutils literal"><span class="pre">entry</span></tt> was created from it (through <tt class="docutils literal"><span class="pre">create_torrent</span></tt>).</p>
 
1427
</div>
 
1428
<div class="section" id="name-comment-creation-date-creator">
 
1429
<h2>name() comment() creation_date() creator()</h2>
 
1430
<blockquote>
 
1431
<pre class="literal-block">
 
1432
std::string const&amp; name() const;
 
1433
std::string const&amp; comment() const;
 
1434
boost::optional&lt;boost::posix_time::ptime&gt; creation_date() const;
 
1435
</pre>
 
1436
</blockquote>
 
1437
<p><tt class="docutils literal"><span class="pre">name()</span></tt> returns the name of the torrent.</p>
 
1438
<p><tt class="docutils literal"><span class="pre">comment()</span></tt> returns the comment associated with the torrent. If there's no comment,
 
1439
it will return an empty string. <tt class="docutils literal"><span class="pre">creation_date()</span></tt> returns a <a class="reference external" href="http://www.boost.org/doc/html/date_time/posix_time.html#date_time.posix_time.ptime_class">boost::posix_time::ptime</a>
 
1440
object, representing the time when this torrent file was created. If there's no time stamp
 
1441
in the torrent file, this will return a date of January 1:st 1970.</p>
 
1442
<p>Both the name and the comment is UTF-8 encoded strings.</p>
 
1443
<p><tt class="docutils literal"><span class="pre">creator()</span></tt> returns the creator string in the torrent. If there is no creator string
 
1444
it will return an empty string.</p>
 
1445
</div>
 
1446
<div class="section" id="priv-set-priv">
 
1447
<h2>priv() set_priv()</h2>
 
1448
<blockquote>
 
1449
<pre class="literal-block">
 
1450
bool priv() const;
 
1451
void set_priv(bool v);
 
1452
</pre>
 
1453
</blockquote>
 
1454
<p><tt class="docutils literal"><span class="pre">priv()</span></tt> returns true if this torrent is private. i.e., it should not be
 
1455
distributed on the trackerless network (the kademlia DHT).</p>
 
1456
<p><tt class="docutils literal"><span class="pre">set_priv()</span></tt> sets or clears the private flag on this torrent.</p>
 
1457
</div>
 
1458
<div class="section" id="nodes">
 
1459
<h2>nodes()</h2>
 
1460
<blockquote>
 
1461
<pre class="literal-block">
 
1462
std::vector&lt;std::pair&lt;std::string, int&gt; &gt; const&amp; nodes() const;
 
1463
</pre>
 
1464
</blockquote>
 
1465
<p>If this torrent contains any DHT nodes, they are put in this vector in their original
 
1466
form (host name and port number).</p>
 
1467
</div>
 
1468
<div class="section" id="add-node">
 
1469
<h2>add_node()</h2>
 
1470
<blockquote>
 
1471
<pre class="literal-block">
 
1472
void add_node(std::pair&lt;std::string, int&gt; const&amp; node);
 
1473
</pre>
 
1474
</blockquote>
 
1475
<p>This is used when creating torrent. Use this to add a known DHT node. It may
 
1476
be used, by the client, to bootstrap into the DHT network.</p>
 
1477
</div>
 
1478
</div>
 
1479
<div class="section" id="torrent-handle">
 
1480
<h1>torrent_handle</h1>
 
1481
<p>You will usually have to store your torrent handles somewhere, since it's the
 
1482
object through which you retrieve information about the torrent and aborts the torrent.
 
1483
Its declaration looks like this:</p>
 
1484
<pre class="literal-block">
 
1485
struct torrent_handle
 
1486
{
 
1487
        torrent_handle();
 
1488
 
 
1489
        torrent_status status();
 
1490
        void file_progress(std::vector&lt;float&gt;&amp; fp);
 
1491
        void get_download_queue(std::vector&lt;partial_piece_info&gt;&amp; queue) const;
 
1492
        void get_peer_info(std::vector&lt;peer_info&gt;&amp; v) const;
 
1493
        torrent_info const&amp; get_torrent_info() const;
 
1494
        bool is_valid() const;
 
1495
 
 
1496
        std::string name() const;
 
1497
 
 
1498
        entry write_resume_data() const;
 
1499
        void force_reannounce() const;
 
1500
        void force_reannounce(boost::posix_time::time_duration) const;
 
1501
        void scrape_tracker() const;
 
1502
        void connect_peer(asio::ip::tcp::endpoint const&amp; adr, int source = 0) const;
 
1503
 
 
1504
        void set_tracker_login(std::string const&amp; username
 
1505
                , std::string const&amp; password) const;
 
1506
 
 
1507
        std::vector&lt;announce_entry&gt; const&amp; trackers() const;
 
1508
        void replace_trackers(std::vector&lt;announce_entry&gt; const&amp;);
 
1509
 
 
1510
        void add_url_seed(std::string const&amp; url);
 
1511
        void remove_url_seed(std::string const&amp; url);
 
1512
        std::set&lt;std::string&gt; url_seeds() const;
 
1513
 
 
1514
        void set_ratio(float ratio) const;
 
1515
        void set_max_uploads(int max_uploads) const;
 
1516
        void set_max_connections(int max_connections) const;
 
1517
        void set_upload_limit(int limit) const;
 
1518
        int upload_limit() const;
 
1519
        void set_download_limit(int limit) const;
 
1520
        int download_limit() const;
 
1521
        void set_sequenced_download_threshold(int threshold) const;
 
1522
 
 
1523
        void set_peer_upload_limit(asio::ip::tcp::endpoint ip, int limit) const;
 
1524
        void set_peer_download_limit(asio::ip::tcp::endpoint ip, int limit) const;
 
1525
 
 
1526
        void use_interface(char const* net_interface) const;
 
1527
 
 
1528
        void pause() const;
 
1529
        void resume() const;
 
1530
        bool is_paused() const;
 
1531
        bool is_seed() const;
 
1532
 
 
1533
        void resolve_countries(bool r);
 
1534
        bool resolve_countries() const;
 
1535
 
 
1536
        void piece_priority(int index, int priority) const;
 
1537
        int piece_priority(int index) const;
 
1538
 
 
1539
        void prioritize_pieces(std::vector&lt;int&gt; const&amp; pieces) const;
 
1540
        std::vector&lt;int&gt; piece_priorities() const;
 
1541
 
 
1542
        void prioritize_files(std::vector&lt;int&gt; const&amp; files) const;
 
1543
 
 
1544
        // these functions are deprecated
 
1545
        void filter_piece(int index, bool filter) const;
 
1546
        void filter_pieces(std::vector&lt;bool&gt; const&amp; bitmask) const;
 
1547
        bool is_piece_filtered(int index) const;
 
1548
        std::vector&lt;bool&gt; filtered_pieces() const;
 
1549
        void filter_files(std::vector&lt;bool&gt; const&amp; files) const;
 
1550
 
 
1551
        bool has_metadata() const;
 
1552
 
 
1553
        boost::filesystem::path save_path() const;
 
1554
        void move_storage(boost::filesystem::path const&amp; save_path) const;
 
1555
 
 
1556
        sha1_hash info_hash() const;
 
1557
 
 
1558
        bool operator==(torrent_handle const&amp;) const;
 
1559
        bool operator!=(torrent_handle const&amp;) const;
 
1560
        bool operator&lt;(torrent_handle const&amp;) const;
 
1561
};
 
1562
</pre>
 
1563
<p>The default constructor will initialize the handle to an invalid state. Which
 
1564
means you cannot perform any operation on it, unless you first assign it a
 
1565
valid handle. If you try to perform any operation on an uninitialized handle,
 
1566
it will throw <tt class="docutils literal"><span class="pre">invalid_handle</span></tt>.</p>
 
1567
<div class="warning">
 
1568
<p class="first admonition-title">Warning</p>
 
1569
<p class="last">All operations on a <tt class="docutils literal"><span class="pre">torrent_handle</span></tt> may throw <a class="reference internal" href="#invalid-handle">invalid_handle</a>
 
1570
exception, in case the handle is no longer refering to a torrent. There are
 
1571
two exceptions, <tt class="docutils literal"><span class="pre">info_hash()</span></tt> and <tt class="docutils literal"><span class="pre">is_valid()</span></tt> will never throw.
 
1572
Since the torrents are processed by a background thread, there is no
 
1573
guarantee that a handle will remain valid between two calls.</p>
 
1574
</div>
 
1575
<div class="section" id="piece-priority-prioritize-pieces-piece-priorities-prioritize-files">
 
1576
<h2>piece_priority() prioritize_pieces() piece_priorities() prioritize_files()</h2>
 
1577
<blockquote>
 
1578
<pre class="literal-block">
 
1579
void piece_priority(int index, int priority) const;
 
1580
int piece_priority(int index) const;
 
1581
void prioritize_pieces(std::vector&lt;int&gt; const&amp; pieces) const;
 
1582
std::vector&lt;int&gt; piece_priorities() const;
 
1583
void prioritize_files(std::vector&lt;int&gt; const&amp; files) const;
 
1584
</pre>
 
1585
</blockquote>
 
1586
<p>These functions are used to set and get the prioritiy of individual pieces.
 
1587
By default all pieces have priority 1. That means that the random rarest
 
1588
first algorithm is effectively active for all pieces. You may however
 
1589
change the priority of individual pieces. There are 8 different priority
 
1590
levels:</p>
 
1591
<blockquote>
 
1592
<ol class="arabic simple" start="0">
 
1593
<li>piece is not downloaded at all</li>
 
1594
<li>normal priority. Download order is dependent on availability</li>
 
1595
<li>higher than normal priority. Pieces are preferred over pieces with
 
1596
the same availability, but not over pieces with lower availability</li>
 
1597
<li>pieces are as likely to be picked as partial pieces.</li>
 
1598
<li>pieces are preferred over partial pieces, but not over pieces with
 
1599
lower availability</li>
 
1600
<li><em>currently the same as 4</em></li>
 
1601
<li>piece is as likely to be picked as any piece with availability 1</li>
 
1602
<li>maximum priority, availability is disregarded, the piece is preferred
 
1603
over any other piece with lower priority</li>
 
1604
</ol>
 
1605
</blockquote>
 
1606
<p>The exact definitions of these priorities are implementation details, and
 
1607
subject to change. The interface guarantees that higher number means higher
 
1608
priority, and that 0 means do not download.</p>
 
1609
<p><tt class="docutils literal"><span class="pre">piece_priority</span></tt> sets or gets the priority for an individual piece,
 
1610
specified by <tt class="docutils literal"><span class="pre">index</span></tt>.</p>
 
1611
<p><tt class="docutils literal"><span class="pre">prioritize_pieces</span></tt> takes a vector of integers, one integer per piece in
 
1612
the torrent. All the piece priorities will be updated with the priorities
 
1613
in the vector.</p>
 
1614
<p><tt class="docutils literal"><span class="pre">piece_priorities</span></tt> returns a vector with one element for each piece in the
 
1615
torrent. Each element is the current priority of that piece.</p>
 
1616
<p><tt class="docutils literal"><span class="pre">prioritize_files</span></tt> takes a vector that has at as many elements as there are
 
1617
files in the torrent. Each entry is the priority of that file. The function
 
1618
sets the priorities of all the pieces in the torrent based on the vector.</p>
 
1619
</div>
 
1620
<div class="section" id="file-progress">
 
1621
<h2>file_progress()</h2>
 
1622
<blockquote>
 
1623
<pre class="literal-block">
 
1624
void file_progress(std::vector&lt;float&gt;&amp; fp);
 
1625
</pre>
 
1626
</blockquote>
 
1627
<p>This function fills in the supplied vector with the progress (a value in the
 
1628
range [0, 1]) describing the download progress of each file in this torrent.
 
1629
The progress values are ordered the same as the files in the <a class="reference internal" href="#torrent-info">torrent_info</a>.
 
1630
This operation is not very cheap.</p>
 
1631
</div>
 
1632
<div class="section" id="save-path">
 
1633
<h2>save_path()</h2>
 
1634
<blockquote>
 
1635
<pre class="literal-block">
 
1636
boost::filesystem::path save_path() const;
 
1637
</pre>
 
1638
</blockquote>
 
1639
<p><tt class="docutils literal"><span class="pre">save_path()</span></tt> returns the path that was given to <a class="reference internal" href="#add-torrent">add_torrent()</a> when this torrent
 
1640
was started.</p>
 
1641
</div>
 
1642
<div class="section" id="move-storage">
 
1643
<h2>move_storage()</h2>
 
1644
<blockquote>
 
1645
<pre class="literal-block">
 
1646
void move_storage(boost::filesystem::path const&amp; save_path) const;
 
1647
</pre>
 
1648
</blockquote>
 
1649
<p>Moves the file(s) that this torrent are currently seeding from or downloading to. This
 
1650
operation will only have the desired effect if the given <tt class="docutils literal"><span class="pre">save_path</span></tt> is located on
 
1651
the same drive as the original save path. Since disk IO is performed in a separate
 
1652
thread, this operation is also asynchronous. Once the operation completes, the
 
1653
<tt class="docutils literal"><span class="pre">storage_moved_alert</span></tt> is generated, with the new path as the message.</p>
 
1654
</div>
 
1655
<div class="section" id="force-reannounce">
 
1656
<h2>force_reannounce()</h2>
 
1657
<blockquote>
 
1658
<pre class="literal-block">
 
1659
void force_reannounce() const;
 
1660
void force_reannounce(boost::posix_time::time_duration) const;
 
1661
</pre>
 
1662
</blockquote>
 
1663
<p><tt class="docutils literal"><span class="pre">force_reannounce()</span></tt> will force this torrent to do another tracker request, to receive new
 
1664
peers. The second overload of <tt class="docutils literal"><span class="pre">force_reannounce</span></tt> that takes a <tt class="docutils literal"><span class="pre">time_duration</span></tt> as
 
1665
argument will schedule a reannounce in that amount of time from now.</p>
 
1666
</div>
 
1667
<div class="section" id="scrape-tracker">
 
1668
<h2>scrape_tracker()</h2>
 
1669
<blockquote>
 
1670
<pre class="literal-block">
 
1671
void scrape_tracker() const;
 
1672
</pre>
 
1673
</blockquote>
 
1674
<p><tt class="docutils literal"><span class="pre">scrape_tracker()</span></tt> will send a scrape request to the tracker. A scrape request queries the
 
1675
tracker for statistics such as total number of incomplete peers, complete peers, number of
 
1676
downloads etc.</p>
 
1677
<p>This request will specifically update the <tt class="docutils literal"><span class="pre">num_complete</span></tt> and <tt class="docutils literal"><span class="pre">num_incomplete</span></tt> fields in
 
1678
the <a class="reference internal" href="#torrent-status">torrent_status</a> struct once it completes. When it completes, it will generate a
 
1679
<a class="reference internal" href="#scrape-reply-alert">scrape_reply_alert</a>. If it fails, it will generate a <a class="reference internal" href="#scrape-failed-alert">scrape_failed_alert</a>.</p>
 
1680
</div>
 
1681
<div class="section" id="connect-peer">
 
1682
<h2>connect_peer()</h2>
 
1683
<blockquote>
 
1684
<pre class="literal-block">
 
1685
void connect_peer(asio::ip::tcp::endpoint const&amp; adr, int source = 0) const;
 
1686
</pre>
 
1687
</blockquote>
 
1688
<p><tt class="docutils literal"><span class="pre">connect_peer()</span></tt> is a way to manually connect to peers that one believe is a part of the
 
1689
torrent. If the peer does not respond, or is not a member of this torrent, it will simply
 
1690
be disconnected. No harm can be done by using this other than an unnecessary connection
 
1691
attempt is made. If the torrent is uninitialized or in queued or checking mode, this
 
1692
will throw <a class="reference internal" href="#invalid-handle">invalid_handle</a>. The second (optional) argument will be bitwised ORed into
 
1693
the source mask of this peer. Typically this is one of the source flags in <a class="reference internal" href="#peer-info">peer_info</a>.
 
1694
i.e. <tt class="docutils literal"><span class="pre">tracker</span></tt>, <tt class="docutils literal"><span class="pre">pex</span></tt>, <tt class="docutils literal"><span class="pre">dht</span></tt> etc.</p>
 
1695
</div>
 
1696
<div class="section" id="name">
 
1697
<h2>name()</h2>
 
1698
<blockquote>
 
1699
<pre class="literal-block">
 
1700
std::string name() const;
 
1701
</pre>
 
1702
</blockquote>
 
1703
<p>Returns the name of the torrent. i.e. the name from the metadata associated with it. In
 
1704
case the torrent was started without metadata, and hasn't completely received it yet,
 
1705
it returns the name given to it when added to the session. See <tt class="docutils literal"><span class="pre">session::add_torrent</span></tt>.</p>
 
1706
</div>
 
1707
<div class="section" id="set-ratio">
 
1708
<h2>set_ratio()</h2>
 
1709
<blockquote>
 
1710
<pre class="literal-block">
 
1711
void set_ratio(float ratio) const;
 
1712
</pre>
 
1713
</blockquote>
 
1714
<p><tt class="docutils literal"><span class="pre">set_ratio()</span></tt> sets the desired download / upload ratio. If set to 0, it is considered being
 
1715
infinite. i.e. the client will always upload as much as it can, no matter how much it gets back
 
1716
in return. With this setting it will work much like the standard clients.</p>
 
1717
<p>Besides 0, the ratio can be set to any number greater than or equal to 1. It means how much to
 
1718
attempt to upload in return for each download. e.g. if set to 2, the client will try to upload
 
1719
2 bytes for every byte received. The default setting for this is 0, which will make it work
 
1720
as a standard client.</p>
 
1721
</div>
 
1722
<div class="section" id="set-upload-limit-set-download-limit-upload-limit-download-limit">
 
1723
<h2>set_upload_limit() set_download_limit() upload_limit() download_limit()</h2>
 
1724
<blockquote>
 
1725
<pre class="literal-block">
 
1726
void set_upload_limit(int limit) const;
 
1727
void set_download_limit(int limit) const;
 
1728
int upload_limit() const;
 
1729
int download_limit() const;
 
1730
</pre>
 
1731
</blockquote>
 
1732
<p><tt class="docutils literal"><span class="pre">set_upload_limit</span></tt> will limit the upload bandwidth used by this particular torrent to the
 
1733
limit you set. It is given as the number of bytes per second the torrent is allowed to upload.
 
1734
<tt class="docutils literal"><span class="pre">set_download_limit</span></tt> works the same way but for download bandwidth instead of upload bandwidth.
 
1735
Note that setting a higher limit on a torrent then the global limit (<tt class="docutils literal"><span class="pre">session::set_upload_rate_limit</span></tt>)
 
1736
will not override the global rate limit. The torrent can never upload more than the global rate
 
1737
limit.</p>
 
1738
<p><tt class="docutils literal"><span class="pre">upload_limit</span></tt> and <tt class="docutils literal"><span class="pre">download_limit</span></tt> will return the current limit setting, for upload and
 
1739
download, respectively.</p>
 
1740
</div>
 
1741
<div class="section" id="set-sequenced-download-threshold">
 
1742
<h2>set_sequenced_download_threshold()</h2>
 
1743
<blockquote>
 
1744
<pre class="literal-block">
 
1745
void set_sequenced_download_threshold(int threshold);
 
1746
</pre>
 
1747
</blockquote>
 
1748
<p>sequenced-download threshold is the limit on how popular a piece has to be
 
1749
(popular == inverse of rarity) to be downloaded in sequence instead of in
 
1750
random (rarest first) order. It can be used to tweak disk performance in
 
1751
settings where the random download property is less necessary. For example, if
 
1752
the threshold is 10, all pieces which 10 or more peers have, will be downloaded
 
1753
in index order. This setting defaults to 100, which means that it is disabled
 
1754
in practice.</p>
 
1755
<p>Setting this threshold to a very small value will affect the piece distribution
 
1756
negatively in the swarm. It should basically only be used in situations where
 
1757
the random seeks on the disk is the download bottleneck.</p>
 
1758
</div>
 
1759
<div class="section" id="set-peer-upload-limit-set-peer-download-limit">
 
1760
<h2>set_peer_upload_limit() set_peer_download_limit()</h2>
 
1761
<blockquote>
 
1762
<pre class="literal-block">
 
1763
void set_peer_upload_limit(asio::ip::tcp::endpoint ip, int limit) const;
 
1764
void set_peer_download_limit(asio::ip::tcp::endpoint ip, int limit) const;
 
1765
</pre>
 
1766
</blockquote>
 
1767
<p>Works like <tt class="docutils literal"><span class="pre">set_upload_limit</span></tt> and <tt class="docutils literal"><span class="pre">set_download_limit</span></tt> respectively, but controls individual
 
1768
peer instead of the whole torrent.</p>
 
1769
</div>
 
1770
<div class="section" id="pause-resume-is-paused">
 
1771
<h2>pause() resume() is_paused()</h2>
 
1772
<blockquote>
 
1773
<pre class="literal-block">
 
1774
void pause() const;
 
1775
void resume() const;
 
1776
bool is_paused() const;
 
1777
</pre>
 
1778
</blockquote>
 
1779
<p><tt class="docutils literal"><span class="pre">pause()</span></tt>, and <tt class="docutils literal"><span class="pre">resume()</span></tt> will disconnect all peers and reconnect all peers respectively.
 
1780
When a torrent is paused, it will however remember all share ratios to all peers and remember
 
1781
all potential (not connected) peers. You can use <tt class="docutils literal"><span class="pre">is_paused()</span></tt> to determine if a torrent
 
1782
is currently paused. Torrents may be paused automatically if there is a file error (e.g. disk full)
 
1783
or something similar. See <a class="reference internal" href="#file-error-alert">file_error_alert</a>.</p>
 
1784
</div>
 
1785
<div class="section" id="resolve-countries">
 
1786
<h2>resolve_countries()</h2>
 
1787
<blockquote>
 
1788
<pre class="literal-block">
 
1789
void resolve_countries(bool r);
 
1790
bool resolve_countries() const;
 
1791
</pre>
 
1792
</blockquote>
 
1793
<p>Sets or gets the flag that derermines if countries should be resolved for the peers of this
 
1794
torrent. It defaults to false. If it is set to true, the <a class="reference internal" href="#peer-info">peer_info</a> structure for the peers
 
1795
in this torrent will have their <tt class="docutils literal"><span class="pre">country</span></tt> member set. See <a class="reference internal" href="#peer-info">peer_info</a> for more information
 
1796
on how to interpret this field.</p>
 
1797
</div>
 
1798
<div class="section" id="is-seed">
 
1799
<h2>is_seed()</h2>
 
1800
<blockquote>
 
1801
<pre class="literal-block">
 
1802
bool is_seed() const;
 
1803
</pre>
 
1804
</blockquote>
 
1805
<p>Returns true if the torrent is in seed mode (i.e. if it has finished downloading).</p>
 
1806
</div>
 
1807
<div class="section" id="has-metadata">
 
1808
<h2>has_metadata()</h2>
 
1809
<blockquote>
 
1810
<pre class="literal-block">
 
1811
bool has_metadata() const;
 
1812
</pre>
 
1813
</blockquote>
 
1814
<p>Returns true if this torrent has metadata (either it was started from a .torrent file or the
 
1815
metadata has been downloaded). The only scenario where this can return false is when the torrent
 
1816
was started torrent-less (i.e. with just an info-hash and tracker ip). Note that if the torrent
 
1817
doesn't have metadata, the member <a class="reference internal" href="#get-torrent-info">get_torrent_info()</a> will throw.</p>
 
1818
</div>
 
1819
<div class="section" id="set-tracker-login">
 
1820
<h2>set_tracker_login()</h2>
 
1821
<blockquote>
 
1822
<pre class="literal-block">
 
1823
void set_tracker_login(std::string const&amp; username
 
1824
        , std::string const&amp; password) const;
 
1825
</pre>
 
1826
</blockquote>
 
1827
<p><tt class="docutils literal"><span class="pre">set_tracker_login()</span></tt> sets a username and password that will be sent along in the HTTP-request
 
1828
of the tracker announce. Set this if the tracker requires authorization.</p>
 
1829
</div>
 
1830
<div class="section" id="trackers-replace-trackers">
 
1831
<h2>trackers() replace_trackers()</h2>
 
1832
<blockquote>
 
1833
<pre class="literal-block">
 
1834
std::vector&lt;announce_entry&gt; const&amp; trackers() const;
 
1835
void replace_trackers(std::vector&lt;announce_entry&gt; const&amp;) const;
 
1836
</pre>
 
1837
</blockquote>
 
1838
<p><tt class="docutils literal"><span class="pre">trackers()</span></tt> will return the list of trackers for this torrent. The
 
1839
announce entry contains both a string <tt class="docutils literal"><span class="pre">url</span></tt> which specify the announce url
 
1840
for the tracker as well as an int <tt class="docutils literal"><span class="pre">tier</span></tt>, which is specifies the order in
 
1841
which this tracker is tried. If you want libtorrent to use another list of
 
1842
trackers for this torrent, you can use <tt class="docutils literal"><span class="pre">replace_trackers()</span></tt> which takes
 
1843
a list of the same form as the one returned from <tt class="docutils literal"><span class="pre">trackers()</span></tt> and will
 
1844
replace it. If you want an immediate effect, you have to call
 
1845
<a class="reference internal" href="#force-reannounce">force_reannounce()</a>.</p>
 
1846
</div>
 
1847
<div class="section" id="add-url-seed-remove-url-seed-url-seeds">
 
1848
<h2>add_url_seed() remove_url_seed() url_seeds()</h2>
 
1849
<blockquote>
 
1850
<pre class="literal-block">
 
1851
void add_url_seed(std::string const&amp; url);
 
1852
void remove_url_seed(std::string const&amp; url);
 
1853
std::set&lt;std::string&gt; url_seeds() const;
 
1854
</pre>
 
1855
</blockquote>
 
1856
<p><tt class="docutils literal"><span class="pre">add_url_seed()</span></tt> adds another url to the torrent's list of url seeds. If the
 
1857
given url already exists in that list, the call has no effect. The torrent
 
1858
will connect to the server and try to download pieces from it, unless it's
 
1859
paused, queued, checking or seeding. <tt class="docutils literal"><span class="pre">remove_url_seed()</span></tt> removes the given
 
1860
url if it exists already. <tt class="docutils literal"><span class="pre">url_seeds()</span></tt> return a set of the url seeds
 
1861
currently in this torrent. Note that urls that fails may be removed
 
1862
automatically from the list.</p>
 
1863
<p>See <a class="reference internal" href="#http-seeding">HTTP seeding</a> for more information.</p>
 
1864
</div>
 
1865
<div class="section" id="use-interface">
 
1866
<h2>use_interface()</h2>
 
1867
<blockquote>
 
1868
<pre class="literal-block">
 
1869
void use_interface(char const* net_interface) const;
 
1870
</pre>
 
1871
</blockquote>
 
1872
<p><tt class="docutils literal"><span class="pre">use_interface()</span></tt> sets the network interface this torrent will use when it opens outgoing
 
1873
connections. By default, it uses the same interface as the <a class="reference internal" href="#session">session</a> uses to listen on. The
 
1874
parameter must be a string containing an ip-address (either an IPv4 or IPv6 address). If
 
1875
the string does not conform to this format and exception is thrown.</p>
 
1876
</div>
 
1877
<div class="section" id="info-hash">
 
1878
<h2>info_hash()</h2>
 
1879
<blockquote>
 
1880
<pre class="literal-block">
 
1881
sha1_hash info_hash() const;
 
1882
</pre>
 
1883
</blockquote>
 
1884
<p><tt class="docutils literal"><span class="pre">info_hash()</span></tt> returns the info-hash for the torrent.</p>
 
1885
</div>
 
1886
<div class="section" id="id5">
 
1887
<h2>set_max_uploads() set_max_connections()</h2>
 
1888
<blockquote>
 
1889
<pre class="literal-block">
 
1890
void set_max_uploads(int max_uploads) const;
 
1891
void set_max_connections(int max_connections) const;
 
1892
</pre>
 
1893
</blockquote>
 
1894
<p><tt class="docutils literal"><span class="pre">set_max_uploads()</span></tt> sets the maximum number of peers that's unchoked at the same time on this
 
1895
torrent. If you set this to -1, there will be no limit.</p>
 
1896
<p><tt class="docutils literal"><span class="pre">set_max_connections()</span></tt> sets the maximum number of connection this torrent will open. If all
 
1897
connections are used up, incoming connections may be refused or poor connections may be closed.
 
1898
This must be at least 2. The default is unlimited number of connections. If -1 is given to the
 
1899
function, it means unlimited.</p>
 
1900
</div>
 
1901
<div class="section" id="write-resume-data">
 
1902
<h2>write_resume_data()</h2>
 
1903
<blockquote>
 
1904
<pre class="literal-block">
 
1905
entry write_resume_data() const;
 
1906
</pre>
 
1907
</blockquote>
 
1908
<p><tt class="docutils literal"><span class="pre">write_resume_data()</span></tt> generates fast-resume data and returns it as an <a class="reference internal" href="#entry">entry</a>. This <a class="reference internal" href="#entry">entry</a>
 
1909
is suitable for being bencoded. For more information about how fast-resume works, see <a class="reference internal" href="#fast-resume">fast resume</a>.</p>
 
1910
<p>There are three cases where this function will just return an empty <tt class="docutils literal"><span class="pre">entry</span></tt>:</p>
 
1911
<blockquote>
 
1912
<ol class="arabic simple">
 
1913
<li>The torrent handle is invalid.</li>
 
1914
<li>The torrent is checking (or is queued for checking) its storage, it will obviously
 
1915
not be ready to write resume data.</li>
 
1916
<li>The torrent hasn't received valid metadata and was started without metadata
 
1917
(see libtorrent's <a class="reference internal" href="#metadata-from-peers">metadata from peers</a> extension)</li>
 
1918
</ol>
 
1919
</blockquote>
 
1920
<p>Note that by the time this function returns, the resume data may already be invalid if the torrent
 
1921
is still downloading! The recommended practice is to first pause the torrent, then generate the
 
1922
fast resume data, and then close it down. Since the disk IO is done in a separate thread, in order
 
1923
to synchronize, you shoule to wait for the <tt class="docutils literal"><span class="pre">torrent_paused_alert</span></tt> before you write the resume
 
1924
data.</p>
 
1925
<p>In full allocation mode the reume data is never invalidated by subsequent
 
1926
writes to the files, since pieces won't move around. This means that you don't need to
 
1927
pause before writing resume data in full or sparse mode. If you don't, however, any data written to
 
1928
disk after you saved resume data and before the session closed is lost.</p>
 
1929
<p>It also means that if the resume data is out dated, libtorrent will not re-check the files, but assume
 
1930
that it is fairly recent. The assumption is that it's better to loose a little bit than to re-check
 
1931
the entire file.</p>
 
1932
<p>It is still a good idea to save resume data periodically during download as well as when
 
1933
closing down.</p>
 
1934
</div>
 
1935
<div class="section" id="id6">
 
1936
<h2>status()</h2>
 
1937
<blockquote>
 
1938
<pre class="literal-block">
 
1939
torrent_status status() const;
 
1940
</pre>
 
1941
</blockquote>
 
1942
<p><tt class="docutils literal"><span class="pre">status()</span></tt> will return a structure with information about the status of this
 
1943
torrent. If the <a class="reference internal" href="#torrent-handle">torrent_handle</a> is invalid, it will throw <a class="reference internal" href="#invalid-handle">invalid_handle</a> exception.
 
1944
See <a class="reference internal" href="#torrent-status">torrent_status</a>.</p>
 
1945
</div>
 
1946
<div class="section" id="get-download-queue">
 
1947
<h2>get_download_queue()</h2>
 
1948
<blockquote>
 
1949
<pre class="literal-block">
 
1950
void get_download_queue(std::vector&lt;partial_piece_info&gt;&amp; queue) const;
 
1951
</pre>
 
1952
</blockquote>
 
1953
<p><tt class="docutils literal"><span class="pre">get_download_queue()</span></tt> takes a non-const reference to a vector which it will fill with
 
1954
information about pieces that are partially downloaded or not downloaded at all but partially
 
1955
requested. The entry in the vector (<tt class="docutils literal"><span class="pre">partial_piece_info</span></tt>) looks like this:</p>
 
1956
<pre class="literal-block">
 
1957
struct partial_piece_info
 
1958
{
 
1959
        int piece_index;
 
1960
        int blocks_in_piece;
 
1961
        block_info blocks[256];
 
1962
        enum state_t { none, slow, medium, fast };
 
1963
        state_t piece_state;
 
1964
};
 
1965
</pre>
 
1966
<p><tt class="docutils literal"><span class="pre">piece_index</span></tt> is the index of the piece in question. <tt class="docutils literal"><span class="pre">blocks_in_piece</span></tt> is the
 
1967
number of blocks in this particular piece. This number will be the same for most pieces, but
 
1968
the last piece may have fewer blocks than the standard pieces.</p>
 
1969
<p><tt class="docutils literal"><span class="pre">piece_state</span></tt> is set to either <tt class="docutils literal"><span class="pre">fast</span></tt>, <tt class="docutils literal"><span class="pre">medium</span></tt>, <tt class="docutils literal"><span class="pre">slow</span></tt> or <tt class="docutils literal"><span class="pre">none</span></tt>. It tells which
 
1970
download rate category the peers downloading this piece falls into. <tt class="docutils literal"><span class="pre">none</span></tt> means that no
 
1971
peer is currently downloading any part of the piece. Peers prefer picking pieces from
 
1972
the same category as themselves. The reason for this is to keep the number of partially
 
1973
downloaded pieces down. Pieces set to <tt class="docutils literal"><span class="pre">none</span></tt> can be converted into any of <tt class="docutils literal"><span class="pre">fast</span></tt>,
 
1974
<tt class="docutils literal"><span class="pre">medium</span></tt> or <tt class="docutils literal"><span class="pre">slow</span></tt> as soon as a peer want to download from it.</p>
 
1975
<pre class="literal-block">
 
1976
struct block_info
 
1977
{
 
1978
        enum block_state_t
 
1979
        { none, requested, writing, finished };
 
1980
 
 
1981
        tcp::endpoint peer;
 
1982
        unsigned state:2;
 
1983
        unsigned num_peers:14;
 
1984
};
 
1985
</pre>
 
1986
<p>The <tt class="docutils literal"><span class="pre">block_info</span></tt> array contains data for each individual block in the piece. Each block has
 
1987
a state (<tt class="docutils literal"><span class="pre">state</span></tt>) which is any of:</p>
 
1988
<ul class="simple">
 
1989
<li><tt class="docutils literal"><span class="pre">none</span></tt> - This block has not been downloaded or requested form any peer.</li>
 
1990
<li><tt class="docutils literal"><span class="pre">requested</span></tt> - The block has been requested, but not completely downloaded yet.</li>
 
1991
<li><tt class="docutils literal"><span class="pre">writing</span></tt> - The block has been downloaded and is currently queued for being written to disk.</li>
 
1992
<li><tt class="docutils literal"><span class="pre">finished</span></tt> - The block has been written to disk.</li>
 
1993
</ul>
 
1994
<p>The <tt class="docutils literal"><span class="pre">peer</span></tt> field is the ip address of the peer this block was downloaded from.
 
1995
<tt class="docutils literal"><span class="pre">num_peers</span></tt> is the number of peers that is currently requesting this block. Typically this
 
1996
is 0 or 1, but at the end of the torrent blocks may be requested by more peers in parallel to
 
1997
speed things up.</p>
 
1998
</div>
 
1999
<div class="section" id="get-peer-info">
 
2000
<h2>get_peer_info()</h2>
 
2001
<blockquote>
 
2002
<pre class="literal-block">
 
2003
void get_peer_info(std::vector&lt;peer_info&gt;&amp;) const;
 
2004
</pre>
 
2005
</blockquote>
 
2006
<p><tt class="docutils literal"><span class="pre">get_peer_info()</span></tt> takes a reference to a vector that will be cleared and filled
 
2007
with one entry for each peer connected to this torrent, given the handle is valid. If the
 
2008
<a class="reference internal" href="#torrent-handle">torrent_handle</a> is invalid, it will throw <a class="reference internal" href="#invalid-handle">invalid_handle</a> exception. Each entry in
 
2009
the vector contains information about that particular peer. See <a class="reference internal" href="#peer-info">peer_info</a>.</p>
 
2010
</div>
 
2011
<div class="section" id="get-torrent-info">
 
2012
<h2>get_torrent_info()</h2>
 
2013
<blockquote>
 
2014
<pre class="literal-block">
 
2015
torrent_info const&amp; get_torrent_info() const;
 
2016
</pre>
 
2017
</blockquote>
 
2018
<p>Returns a const reference to the <a class="reference internal" href="#torrent-info">torrent_info</a> object associated with this torrent.
 
2019
This reference is valid as long as the <a class="reference internal" href="#torrent-handle">torrent_handle</a> is valid, no longer. If the
 
2020
<a class="reference internal" href="#torrent-handle">torrent_handle</a> is invalid or if it doesn't have any metadata, <a class="reference internal" href="#invalid-handle">invalid_handle</a>
 
2021
exception will be thrown. The torrent may be in a state without metadata only if
 
2022
it was started without a .torrent file, i.e. by using the libtorrent extension of
 
2023
just supplying a tracker and info-hash.</p>
 
2024
</div>
 
2025
<div class="section" id="is-valid">
 
2026
<h2>is_valid()</h2>
 
2027
<blockquote>
 
2028
<pre class="literal-block">
 
2029
bool is_valid() const;
 
2030
</pre>
 
2031
</blockquote>
 
2032
<p>Returns true if this handle refers to a valid torrent and false if it hasn't been initialized
 
2033
or if the torrent it refers to has been aborted. Note that a handle may become invalid after
 
2034
it has been added to the session. Usually this is because the storage for the torrent is
 
2035
somehow invalid or if the filenames are not allowed (and hence cannot be opened/created) on
 
2036
your filesystem. If such an error occurs, a <a class="reference internal" href="#file-error-alert">file_error_alert</a> is generated and all handles
 
2037
that refers to that torrent will become invalid.</p>
 
2038
<p><em>TODO: document storage</em></p>
 
2039
</div>
 
2040
</div>
 
2041
<div class="section" id="torrent-status">
 
2042
<h1>torrent_status</h1>
 
2043
<p>It contains the following fields:</p>
 
2044
<pre class="literal-block">
 
2045
struct torrent_status
 
2046
{
 
2047
        enum state_t
 
2048
        {
 
2049
                queued_for_checking,
 
2050
                checking_files,
 
2051
                connecting_to_tracker,
 
2052
                downloading_metadata,
 
2053
                downloading,
 
2054
                finished,
 
2055
                seeding,
 
2056
                allocating
 
2057
        };
 
2058
 
 
2059
        state_t state;
 
2060
        bool paused;
 
2061
        float progress;
 
2062
        boost::posix_time::time_duration next_announce;
 
2063
        boost::posix_time::time_duration announce_interval;
 
2064
 
 
2065
        std::string current_tracker;
 
2066
 
 
2067
        size_type total_download;
 
2068
        size_type total_upload;
 
2069
 
 
2070
        size_type total_payload_download;
 
2071
        size_type total_payload_upload;
 
2072
 
 
2073
        size_type total_failed_bytes;
 
2074
        size_type total_redundant_bytes;
 
2075
 
 
2076
        float download_rate;
 
2077
        float upload_rate;
 
2078
 
 
2079
        float download_payload_rate;
 
2080
        float upload_payload_rate;
 
2081
 
 
2082
        int num_peers;
 
2083
 
 
2084
        int num_complete;
 
2085
        int num_incomplete;
 
2086
 
 
2087
        int list_seeds;
 
2088
        int list_peers;
 
2089
 
 
2090
        const std::vector&lt;bool&gt;* pieces;
 
2091
        int num_pieces;
 
2092
 
 
2093
        size_type total_done;
 
2094
        size_type total_wanted_done;
 
2095
        size_type total_wanted;
 
2096
 
 
2097
        int num_seeds;
 
2098
        float distributed_copies;
 
2099
 
 
2100
        int block_size;
 
2101
 
 
2102
        int num_uploads;
 
2103
        int num_connections;
 
2104
        int uploads_limit;
 
2105
        int connections_limit;
 
2106
 
 
2107
        storage_mode_t storage_mode;
 
2108
 
 
2109
        int up_bandwidth_queue;
 
2110
        int down_bandwidth_queue;
 
2111
};
 
2112
</pre>
 
2113
<p><tt class="docutils literal"><span class="pre">progress</span></tt> is a value in the range [0, 1], that represents the progress of the
 
2114
torrent's current task. It may be checking files or downloading. The torrent's
 
2115
current task is in the <tt class="docutils literal"><span class="pre">state</span></tt> member, it will be one of the following:</p>
 
2116
<table border="1" class="docutils">
 
2117
<colgroup>
 
2118
<col width="31%" />
 
2119
<col width="69%" />
 
2120
</colgroup>
 
2121
<tbody valign="top">
 
2122
<tr><td><tt class="docutils literal"><span class="pre">queued_for_checking</span></tt></td>
 
2123
<td>The torrent is in the queue for being checked. But there
 
2124
currently is another torrent that are being checked.
 
2125
This torrent will wait for its turn.</td>
 
2126
</tr>
 
2127
<tr><td><tt class="docutils literal"><span class="pre">checking_files</span></tt></td>
 
2128
<td>The torrent has not started its download yet, and is
 
2129
currently checking existing files.</td>
 
2130
</tr>
 
2131
<tr><td><tt class="docutils literal"><span class="pre">connecting_to_tracker</span></tt></td>
 
2132
<td>The torrent has sent a request to the tracker and is
 
2133
currently waiting for a response</td>
 
2134
</tr>
 
2135
<tr><td><tt class="docutils literal"><span class="pre">downloading_metadata</span></tt></td>
 
2136
<td>The torrent is trying to download metadata from peers.
 
2137
This assumes the metadata_transfer extension is in use.</td>
 
2138
</tr>
 
2139
<tr><td><tt class="docutils literal"><span class="pre">downloading</span></tt></td>
 
2140
<td>The torrent is being downloaded. This is the state
 
2141
most torrents will be in most of the time. The progress
 
2142
meter will tell how much of the files that has been
 
2143
downloaded.</td>
 
2144
</tr>
 
2145
<tr><td><tt class="docutils literal"><span class="pre">finished</span></tt></td>
 
2146
<td>In this state the torrent has finished downloading but
 
2147
still doesn't have the entire torrent. i.e. some pieces
 
2148
are filtered and won't get downloaded.</td>
 
2149
</tr>
 
2150
<tr><td><tt class="docutils literal"><span class="pre">seeding</span></tt></td>
 
2151
<td>In this state the torrent has finished downloading and
 
2152
is a pure seeder.</td>
 
2153
</tr>
 
2154
<tr><td><tt class="docutils literal"><span class="pre">allocating</span></tt></td>
 
2155
<td>If the torrent was started in full allocation mode, this
 
2156
indicates that the (disk) storage for the torrent is
 
2157
allocated.</td>
 
2158
</tr>
 
2159
</tbody>
 
2160
</table>
 
2161
<p>When downloading, the progress is <tt class="docutils literal"><span class="pre">total_wanted_done</span></tt> / <tt class="docutils literal"><span class="pre">total_wanted</span></tt>.</p>
 
2162
<p><tt class="docutils literal"><span class="pre">paused</span></tt> is set to true if the torrent is paused and false otherwise.</p>
 
2163
<p><tt class="docutils literal"><span class="pre">next_announce</span></tt> is the time until the torrent will announce itself to the tracker. And
 
2164
<tt class="docutils literal"><span class="pre">announce_interval</span></tt> is the time the tracker want us to wait until we announce ourself
 
2165
again the next time.</p>
 
2166
<p><tt class="docutils literal"><span class="pre">current_tracker</span></tt> is the URL of the last working tracker. If no tracker request has
 
2167
been successful yet, it's set to an empty string.</p>
 
2168
<p><tt class="docutils literal"><span class="pre">total_download</span></tt> and <tt class="docutils literal"><span class="pre">total_upload</span></tt> is the number of bytes downloaded and
 
2169
uploaded to all peers, accumulated, <em>this session</em> only.</p>
 
2170
<p><tt class="docutils literal"><span class="pre">total_payload_download</span></tt> and <tt class="docutils literal"><span class="pre">total_payload_upload</span></tt> counts the amount of bytes
 
2171
send and received this session, but only the actual payload data (i.e the interesting
 
2172
data), these counters ignore any protocol overhead.</p>
 
2173
<p><tt class="docutils literal"><span class="pre">total_failed_bytes</span></tt> is the number of bytes that has been downloaded and that
 
2174
has failed the piece hash test. In other words, this is just how much crap that
 
2175
has been downloaded.</p>
 
2176
<p><tt class="docutils literal"><span class="pre">total_redundant_bytes</span></tt> is the number of bytes that has been downloaded even
 
2177
though that data already was downloaded. The reason for this is that in some
 
2178
situations the same data can be downloaded by mistake. When libtorrent sends
 
2179
requests to a peer, and the peer doesn't send a response within a certain
 
2180
timeout, libtorrent will re-request that block. Another situation when
 
2181
libtorrent may re-request blocks is when the requests it sends out are not
 
2182
replied in FIFO-order (it will re-request blocks that are skipped by an out of
 
2183
order block). This is supposed to be as low as possible.</p>
 
2184
<p><tt class="docutils literal"><span class="pre">pieces</span></tt> is the bitmask that represents which pieces we have (set to true) and
 
2185
the pieces we don't have. It's a pointer and may be set to 0 if the torrent isn't
 
2186
downloading or seeding.</p>
 
2187
<p><tt class="docutils literal"><span class="pre">num_pieces</span></tt> is the number of pieces that has been downloaded. It is equivalent
 
2188
to: <tt class="docutils literal"><span class="pre">std::accumulate(pieces-&gt;begin(),</span> <span class="pre">pieces-&gt;end())</span></tt>. So you don't have to
 
2189
count yourself. This can be used to see if anything has updated since last time
 
2190
if you want to keep a graph of the pieces up to date.</p>
 
2191
<p><tt class="docutils literal"><span class="pre">download_rate</span></tt> and <tt class="docutils literal"><span class="pre">upload_rate</span></tt> are the total rates for all peers for this
 
2192
torrent. These will usually have better precision than summing the rates from
 
2193
all peers. The rates are given as the number of bytes per second. The
 
2194
<tt class="docutils literal"><span class="pre">download_payload_rate</span></tt> and <tt class="docutils literal"><span class="pre">upload_payload_rate</span></tt> respectively is the
 
2195
total transfer rate of payload only, not counting protocol chatter. This might
 
2196
be slightly smaller than the other rates, but if projected over a long time
 
2197
(e.g. when calculating ETA:s) the difference may be noticeable.</p>
 
2198
<p><tt class="docutils literal"><span class="pre">num_peers</span></tt> is the number of peers this torrent currently is connected to.
 
2199
Peer connections that are in the half-open state (is attempting to connect)
 
2200
or are queued for later connection attempt do not count. Although they are
 
2201
visible in the peer list when you call <a class="reference internal" href="#get-peer-info">get_peer_info()</a>.</p>
 
2202
<p><tt class="docutils literal"><span class="pre">num_complete</span></tt> and <tt class="docutils literal"><span class="pre">num_incomplete</span></tt> are set to -1 if the tracker did not
 
2203
send any scrape data in its announce reply. This data is optional and may
 
2204
not be available from all trackers. If these are not -1, they are the total
 
2205
number of peers that are seeding (complete) and the total number of peers
 
2206
that are still downloading (incomplete) this torrent.</p>
 
2207
<p><tt class="docutils literal"><span class="pre">list_seeds</span></tt> and <tt class="docutils literal"><span class="pre">list_peers</span></tt> are the number of seeds in our peer list
 
2208
and the total number of peers (including seeds) respectively. We are not
 
2209
necessarily connected to all the peers in our peer list. This is the number
 
2210
of peers we know of in total, including banned peers and peers that we have
 
2211
failed to connect to.</p>
 
2212
<p><tt class="docutils literal"><span class="pre">total_done</span></tt> is the total number of bytes of the file(s) that we have. All
 
2213
this does not necessarily has to be downloaded during this session (that's
 
2214
<tt class="docutils literal"><span class="pre">total_payload_download</span></tt>).</p>
 
2215
<p><tt class="docutils literal"><span class="pre">total_wanted_done</span></tt> is the number of bytes we have downloaded, only counting the
 
2216
pieces that we actually want to download. i.e. excluding any pieces that we have but
 
2217
are filtered as not wanted.</p>
 
2218
<p><tt class="docutils literal"><span class="pre">total_wanted</span></tt> is the total number of bytes we want to download. This is also
 
2219
excluding pieces that have been filtered.</p>
 
2220
<p><tt class="docutils literal"><span class="pre">num_seeds</span></tt> is the number of peers that are seeding that this client is
 
2221
currently connected to.</p>
 
2222
<p><tt class="docutils literal"><span class="pre">distributed_copies</span></tt> is the number of distributed copies of the torrent.
 
2223
Note that one copy may be spread out among many peers. The integer part
 
2224
tells how many copies there are currently of the rarest piece(s) among the
 
2225
peers this client is connected to. The fractional part tells the share of
 
2226
pieces that have more copies than the rarest piece(s). For example: 2.5 would
 
2227
mean that the rarest pieces have only 2 copies among the peers this torrent is
 
2228
connected to, and that 50% of all the pieces have more than two copies.</p>
 
2229
<p>If we are a seed, the piece picker is deallocated as an optimization, and
 
2230
piece availability is no longer tracked. In this case the distributed
 
2231
copies is set to -1.</p>
 
2232
<p><tt class="docutils literal"><span class="pre">block_size</span></tt> is the size of a block, in bytes. A block is a sub piece, it
 
2233
is the number of bytes that each piece request asks for and the number of
 
2234
bytes that each bit in the <tt class="docutils literal"><span class="pre">partial_piece_info</span></tt>'s bitset represents
 
2235
(see <a class="reference internal" href="#get-download-queue">get_download_queue()</a>). This is typically 16 kB, but it may be
 
2236
larger if the pieces are larger.</p>
 
2237
<p><tt class="docutils literal"><span class="pre">num_uploads</span></tt> is the number of unchoked peers in this torrent.</p>
 
2238
<p><tt class="docutils literal"><span class="pre">num_connections</span></tt> is the number of peer connections this torrent has, including
 
2239
half-open connections that hasn't completed the bittorrent handshake yet. This is
 
2240
always &lt;= <tt class="docutils literal"><span class="pre">num_peers</span></tt>.</p>
 
2241
<p><tt class="docutils literal"><span class="pre">uploads_limit</span></tt> is the set limit of upload slots (unchoked peers) for this torrent.</p>
 
2242
<p><tt class="docutils literal"><span class="pre">connections_limit</span></tt> is the set limit of number of connections for this torrent.</p>
 
2243
<p><tt class="docutils literal"><span class="pre">storage_mode</span></tt> is one of <tt class="docutils literal"><span class="pre">storage_mode_allocate</span></tt>, <tt class="docutils literal"><span class="pre">storage_mode_sparse</span></tt> or
 
2244
<tt class="docutils literal"><span class="pre">storage_mode_compact</span></tt>. Identifies which storage mode this torrent is being saved
 
2245
with. See <a class="reference internal" href="#storage-allocation">Storage allocation</a>.</p>
 
2246
<p><tt class="docutils literal"><span class="pre">up_bandwidth_queue</span></tt> and <tt class="docutils literal"><span class="pre">down_bandwidth_queue</span></tt> are the number of peers in this
 
2247
torrent that are waiting for more bandwidth quota from the torrent rate limiter.
 
2248
This can determine if the rate you get from this torrent is bound by the torrents
 
2249
limit or not. If there is no limit set on this torrent, the peers might still be
 
2250
waiting for bandwidth quota from the global limiter, but then they are counted in
 
2251
the <tt class="docutils literal"><span class="pre">session_status</span></tt> object.</p>
 
2252
</div>
 
2253
<div class="section" id="peer-info">
 
2254
<h1>peer_info</h1>
 
2255
<p>It contains the following fields:</p>
 
2256
<pre class="literal-block">
 
2257
struct peer_info
 
2258
{
 
2259
        enum
 
2260
        {
 
2261
                interesting = 0x1,
 
2262
                choked = 0x2,
 
2263
                remote_interested = 0x4,
 
2264
                remote_choked = 0x8,
 
2265
                supports_extensions = 0x10,
 
2266
                local_connection = 0x20,
 
2267
                handshake = 0x40,
 
2268
                connecting = 0x80,
 
2269
                queued = 0x100,
 
2270
                on_parole = 0x200,
 
2271
                seed = 0x400,
 
2272
                optimistic_unchoke = 0x800,
 
2273
                rc4_encrypted = 0x100000,
 
2274
                plaintext_encrypted = 0x200000
 
2275
        };
 
2276
 
 
2277
        unsigned int flags;
 
2278
 
 
2279
        enum peer_source_flags
 
2280
        {
 
2281
                tracker = 0x1,
 
2282
                dht = 0x2,
 
2283
                pex = 0x4,
 
2284
                lsd = 0x8
 
2285
        };
 
2286
 
 
2287
        int source;
 
2288
 
 
2289
        asio::ip::tcp::endpoint ip;
 
2290
        float up_speed;
 
2291
        float down_speed;
 
2292
        float payload_up_speed;
 
2293
        float payload_down_speed;
 
2294
        size_type total_download;
 
2295
        size_type total_upload;
 
2296
        peer_id pid;
 
2297
        std::vector&lt;bool&gt; pieces;
 
2298
        int upload_limit;
 
2299
        int download_limit;
 
2300
 
 
2301
        time_duration last_request;
 
2302
        time_duration last_active;
 
2303
 
 
2304
        int send_buffer_size;
 
2305
        int used_send_buffer;
 
2306
 
 
2307
        int num_hashfails;
 
2308
 
 
2309
        char country[2];
 
2310
 
 
2311
        size_type load_balancing;
 
2312
 
 
2313
        int download_queue_length;
 
2314
        int upload_queue_length;
 
2315
 
 
2316
        int failcount;
 
2317
 
 
2318
        int downloading_piece_index;
 
2319
        int downloading_block_index;
 
2320
        int downloading_progress;
 
2321
        int downloading_total;
 
2322
 
 
2323
        std::string client;
 
2324
 
 
2325
        enum
 
2326
        {
 
2327
                standard_bittorrent = 0,
 
2328
                web_seed = 1
 
2329
        };
 
2330
        int connection_type;
 
2331
};
 
2332
</pre>
 
2333
<p>The <tt class="docutils literal"><span class="pre">flags</span></tt> attribute tells you in which state the peer is. It is set to
 
2334
any combination of the enums above. The following table describes each flag:</p>
 
2335
<table border="1" class="docutils">
 
2336
<colgroup>
 
2337
<col width="31%" />
 
2338
<col width="69%" />
 
2339
</colgroup>
 
2340
<tbody valign="top">
 
2341
<tr><td><tt class="docutils literal"><span class="pre">interesting</span></tt></td>
 
2342
<td><strong>we</strong> are interested in pieces from this peer.</td>
 
2343
</tr>
 
2344
<tr><td><tt class="docutils literal"><span class="pre">choked</span></tt></td>
 
2345
<td><strong>we</strong> have choked this peer.</td>
 
2346
</tr>
 
2347
<tr><td><tt class="docutils literal"><span class="pre">remote_interested</span></tt></td>
 
2348
<td>the peer is interested in <strong>us</strong></td>
 
2349
</tr>
 
2350
<tr><td><tt class="docutils literal"><span class="pre">remote_choked</span></tt></td>
 
2351
<td>the peer has choked <strong>us</strong>.</td>
 
2352
</tr>
 
2353
<tr><td><tt class="docutils literal"><span class="pre">support_extensions</span></tt></td>
 
2354
<td>means that this peer supports the
 
2355
<a class="reference external" href="extension_protocol.html">extension protocol</a>.</td>
 
2356
</tr>
 
2357
<tr><td><tt class="docutils literal"><span class="pre">local_connection</span></tt></td>
 
2358
<td>The connection was initiated by us, the peer has a
 
2359
listen port open, and that port is the same as in the
 
2360
address of this peer. If this flag is not set, this
 
2361
peer connection was opened by this peer connecting to
 
2362
us.</td>
 
2363
</tr>
 
2364
<tr><td><tt class="docutils literal"><span class="pre">handshake</span></tt></td>
 
2365
<td>The connection is opened, and waiting for the
 
2366
handshake. Until the handshake is done, the peer
 
2367
cannot be identified.</td>
 
2368
</tr>
 
2369
<tr><td><tt class="docutils literal"><span class="pre">connecting</span></tt></td>
 
2370
<td>The connection is in a half-open state (i.e. it is
 
2371
being connected).</td>
 
2372
</tr>
 
2373
<tr><td><tt class="docutils literal"><span class="pre">queued</span></tt></td>
 
2374
<td>The connection is currently queued for a connection
 
2375
attempt. This may happen if there is a limit set on
 
2376
the number of half-open TCP connections.</td>
 
2377
</tr>
 
2378
<tr><td><tt class="docutils literal"><span class="pre">on_parole</span></tt></td>
 
2379
<td>The peer has participated in a piece that failed the
 
2380
hash check, and is now &quot;on parole&quot;, which means we're
 
2381
only requesting whole pieces from this peer until
 
2382
it either fails that piece or proves that it doesn't
 
2383
send bad data.</td>
 
2384
</tr>
 
2385
<tr><td><tt class="docutils literal"><span class="pre">seed</span></tt></td>
 
2386
<td>This peer is a seed (it has all the pieces).</td>
 
2387
</tr>
 
2388
<tr><td><tt class="docutils literal"><span class="pre">optimistic_unchoke</span></tt></td>
 
2389
<td>This peer is subject to an optimistic unchoke. It has
 
2390
been unchoked for a while to see if it might unchoke
 
2391
us in return an earn an upload/unchoke slot. If it
 
2392
doesn't within some period of time, it will be choked
 
2393
and another peer will be optimistically unchoked.</td>
 
2394
</tr>
 
2395
</tbody>
 
2396
</table>
 
2397
<p><tt class="docutils literal"><span class="pre">source</span></tt> is a combination of flags describing from which sources this peer
 
2398
was received. The flags are:</p>
 
2399
<table border="1" class="docutils">
 
2400
<colgroup>
 
2401
<col width="30%" />
 
2402
<col width="70%" />
 
2403
</colgroup>
 
2404
<tbody valign="top">
 
2405
<tr><td><tt class="docutils literal"><span class="pre">tracker</span></tt></td>
 
2406
<td>The peer was received from the tracker.</td>
 
2407
</tr>
 
2408
<tr><td><tt class="docutils literal"><span class="pre">dht</span></tt></td>
 
2409
<td>The peer was received from the kademlia DHT.</td>
 
2410
</tr>
 
2411
<tr><td><tt class="docutils literal"><span class="pre">pex</span></tt></td>
 
2412
<td>The peer was received from the peer exchange
 
2413
extension.</td>
 
2414
</tr>
 
2415
<tr><td><tt class="docutils literal"><span class="pre">lsd</span></tt></td>
 
2416
<td>The peer was received from the local service
 
2417
discovery (The peer is on the local network).</td>
 
2418
</tr>
 
2419
<tr><td><tt class="docutils literal"><span class="pre">resume_data</span></tt></td>
 
2420
<td>The peer was added from the fast resume data.</td>
 
2421
</tr>
 
2422
</tbody>
 
2423
</table>
 
2424
<p>The <tt class="docutils literal"><span class="pre">ip</span></tt> field is the IP-address to this peer. The type is an asio endpoint. For
 
2425
more info, see the <a class="reference external" href="http://asio.sf.net">asio</a> documentation.</p>
 
2426
<p><tt class="docutils literal"><span class="pre">up_speed</span></tt> and <tt class="docutils literal"><span class="pre">down_speed</span></tt> contains the current upload and download speed
 
2427
we have to and from this peer (including any protocol messages). The transfer rates
 
2428
of payload data only are found in <tt class="docutils literal"><span class="pre">payload_up_speed</span></tt> and <tt class="docutils literal"><span class="pre">payload_down_speed</span></tt>.
 
2429
These figures are updated approximately once every second.</p>
 
2430
<p><tt class="docutils literal"><span class="pre">total_download</span></tt> and <tt class="docutils literal"><span class="pre">total_upload</span></tt> are the total number of bytes downloaded
 
2431
from and uploaded to this peer. These numbers do not include the protocol chatter, but only
 
2432
the payload data.</p>
 
2433
<p><tt class="docutils literal"><span class="pre">pid</span></tt> is the peer's id as used in the bit torrent protocol. This id can be used to
 
2434
extract 'fingerprints' from the peer. Sometimes it can tell you which client the peer
 
2435
is using. See identify_client()_</p>
 
2436
<p><tt class="docutils literal"><span class="pre">pieces</span></tt> is a vector of booleans that has as many entries as there are pieces
 
2437
in the torrent. Each boolean tells you if the peer has that piece (if it's set to true)
 
2438
or if the peer miss that piece (set to false).</p>
 
2439
<p><tt class="docutils literal"><span class="pre">seed</span></tt> is true if this peer is a seed.</p>
 
2440
<p><tt class="docutils literal"><span class="pre">upload_limit</span></tt> is the number of bytes per second we are allowed to send to this
 
2441
peer every second. It may be -1 if there's no local limit on the peer. The global
 
2442
limit and the torrent limit is always enforced anyway.</p>
 
2443
<p><tt class="docutils literal"><span class="pre">download_limit</span></tt> is the number of bytes per second this peer is allowed to
 
2444
receive. -1 means it's unlimited.</p>
 
2445
<p><tt class="docutils literal"><span class="pre">last_request</span></tt> and <tt class="docutils literal"><span class="pre">last_active</span></tt> is the time since we last sent a request
 
2446
to this peer and since any transfer occurred with this peer, respectively.</p>
 
2447
<p><tt class="docutils literal"><span class="pre">send_buffer_size</span></tt> and <tt class="docutils literal"><span class="pre">used_send_buffer</span></tt> is the number of bytes allocated
 
2448
and used for the peer's send buffer, respectively.</p>
 
2449
<p><tt class="docutils literal"><span class="pre">num_hashfails</span></tt> is the number of pieces this peer has participated in
 
2450
sending us that turned out to fail the hash check.</p>
 
2451
<p><tt class="docutils literal"><span class="pre">country</span></tt> is the two letter <a class="reference external" href="http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">ISO 3166 country code</a> for the country the peer
 
2452
is connected from. If the country hasn't been resolved yet, both chars are set
 
2453
to 0. If the resolution failed for some reason, the field is set to &quot;--&quot;. If the
 
2454
resolution service returns an invalid country code, it is set to &quot;!!&quot;.
 
2455
The <tt class="docutils literal"><span class="pre">countries.nerd.dk</span></tt> service is used to look up countries. This field will
 
2456
remain set to 0 unless the torrent is set to resolve countries, see <a class="reference internal" href="#resolve-countries">resolve_countries()</a>.</p>
 
2457
<p><tt class="docutils literal"><span class="pre">load_balancing</span></tt> is a measurement of the balancing of free download (that we get)
 
2458
and free upload that we give. Every peer gets a certain amount of free upload, but
 
2459
this member says how much <em>extra</em> free upload this peer has got. If it is a negative
 
2460
number it means that this was a peer from which we have got this amount of free
 
2461
download.</p>
 
2462
<p><tt class="docutils literal"><span class="pre">download_queue_length</span></tt> is the number of piece-requests we have sent to this peer
 
2463
that hasn't been answered with a piece yet.</p>
 
2464
<p><tt class="docutils literal"><span class="pre">upload_queue_length</span></tt> is the number of piece-requests we have received from this peer
 
2465
that we haven't answered with a piece yet.</p>
 
2466
<p><tt class="docutils literal"><span class="pre">failcount</span></tt> is the number of times this peer has &quot;failed&quot;. i.e. failed to connect
 
2467
or disconnected us. The failcount is decremented when we see this peer in a tracker
 
2468
response or peer exchange message.</p>
 
2469
<p>You can know which piece, and which part of that piece, that is currently being
 
2470
downloaded from a specific peer by looking at the next four members.
 
2471
<tt class="docutils literal"><span class="pre">downloading_piece_index</span></tt> is the index of the piece that is currently being downloaded.
 
2472
This may be set to -1 if there's currently no piece downloading from this peer. If it is
 
2473
&gt;= 0, the other three members are valid. <tt class="docutils literal"><span class="pre">downloading_block_index</span></tt> is the index of the
 
2474
block (or sub-piece) that is being downloaded. <tt class="docutils literal"><span class="pre">downloading_progress</span></tt> is the number
 
2475
of bytes of this block we have received from the peer, and <tt class="docutils literal"><span class="pre">downloading_total</span></tt> is
 
2476
the total number of bytes in this block.</p>
 
2477
<p><tt class="docutils literal"><span class="pre">client</span></tt> is a string describing the software at the other end of the connection.
 
2478
In some cases this information is not available, then it will contain a string
 
2479
that may give away something about which software is running in the other end.
 
2480
In the case of a web seed, the server type and version will be a part of this
 
2481
string.</p>
 
2482
<p><tt class="docutils literal"><span class="pre">connection_type</span></tt> can currently be one of <tt class="docutils literal"><span class="pre">standard_bittorrent</span></tt> or
 
2483
<tt class="docutils literal"><span class="pre">web_seed</span></tt>. These are currently the only implemented protocols.</p>
 
2484
</div>
 
2485
<div class="section" id="session-settings">
 
2486
<h1>session_settings</h1>
 
2487
<p>You have some control over tracker requests through the <tt class="docutils literal"><span class="pre">session_settings</span></tt> object. You
 
2488
create it and fill it with your settings and then use <tt class="docutils literal"><span class="pre">session::set_settings()</span></tt>
 
2489
to apply them. You have control over proxy and authorization settings and also the user-agent
 
2490
that will be sent to the tracker. The user-agent is a good way to identify your client.</p>
 
2491
<pre class="literal-block">
 
2492
struct session_settings
 
2493
{
 
2494
        session_settings();
 
2495
        std::string user_agent;
 
2496
        int tracker_completion_timeout;
 
2497
        int tracker_receive_timeout;
 
2498
        int stop_tracker_timeout;
 
2499
        int tracker_maximum_response_length;
 
2500
 
 
2501
        int piece_timeout;
 
2502
        float request_queue_time;
 
2503
        int max_allowed_in_request_queue;
 
2504
        int max_out_request_queue;
 
2505
        int whole_pieces_threshold;
 
2506
        int peer_timeout;
 
2507
        int urlseed_timeout;
 
2508
        int urlseed_pipeline_size;
 
2509
        int file_pool_size;
 
2510
        bool allow_multiple_connections_per_ip;
 
2511
        int max_failcount;
 
2512
        int min_reconnect_time;
 
2513
        int peer_connect_timeout;
 
2514
        bool ignore_limits_on_local_network;
 
2515
        int connection_speed;
 
2516
        bool send_redundant_have;
 
2517
        bool lazy_bitfields;
 
2518
        int inactivity_timeout;
 
2519
        int unchoke_interval;
 
2520
        int optimistic_unchoke_multiplier;
 
2521
        address announce_ip;
 
2522
        int num_want;
 
2523
        int initial_picker_threshold;
 
2524
        int allowed_fast_set_size;
 
2525
        int max_outstanding_disk_bytes_per_connection;
 
2526
        int handshake_timeout;
 
2527
        bool use_dht_as_fallback;
 
2528
        bool free_torrent_hashes;
 
2529
        bool upnp_ignore_nonrouters;
 
2530
};
 
2531
</pre>
 
2532
<p><tt class="docutils literal"><span class="pre">user_agent</span></tt> this is the client identification to the tracker.
 
2533
The recommended format of this string is:
 
2534
&quot;ClientName/ClientVersion libtorrent/libtorrentVersion&quot;.
 
2535
This name will not only be used when making HTTP requests, but also when
 
2536
sending extended headers to peers that support that extension.</p>
 
2537
<p><tt class="docutils literal"><span class="pre">tracker_completion_timeout</span></tt> is the number of seconds the tracker
 
2538
connection will wait from when it sent the request until it considers the
 
2539
tracker to have timed-out. Default value is 60 seconds.</p>
 
2540
<p><tt class="docutils literal"><span class="pre">tracker_receive_timeout</span></tt> is the number of seconds to wait to receive
 
2541
any data from the tracker. If no data is received for this number of
 
2542
seconds, the tracker will be considered as having timed out. If a tracker
 
2543
is down, this is the kind of timeout that will occur. The default value
 
2544
is 20 seconds.</p>
 
2545
<p><tt class="docutils literal"><span class="pre">stop_tracker_timeout</span></tt> is the time to wait for tracker responses when
 
2546
shutting down the session object. This is given in seconds. Default is
 
2547
10 seconds.</p>
 
2548
<p><tt class="docutils literal"><span class="pre">tracker_maximum_response_length</span></tt> is the maximum number of bytes in a
 
2549
tracker response. If a response size passes this number it will be rejected
 
2550
and the connection will be closed. On gzipped responses this size is measured
 
2551
on the uncompressed data. So, if you get 20 bytes of gzip response that'll
 
2552
expand to 2 megs, it will be interrupted before the entire response has been
 
2553
uncompressed (given your limit is lower than 2 megs). Default limit is
 
2554
1 megabyte.</p>
 
2555
<p><tt class="docutils literal"><span class="pre">piece_timeout</span></tt> controls the number of seconds from a request is sent until
 
2556
it times out if no piece response is returned.</p>
 
2557
<p><tt class="docutils literal"><span class="pre">request_queue_time</span></tt> is the length of the request queue given in the number
 
2558
of seconds it should take for the other end to send all the pieces. i.e. the
 
2559
actual number of requests depends on the download rate and this number.</p>
 
2560
<p><tt class="docutils literal"><span class="pre">max_allowed_in_request_queue</span></tt> is the number of outstanding block requests
 
2561
a peer is allowed to queue up in the client. If a peer sends more requests
 
2562
than this (before the first one has been handled) the last request will be
 
2563
dropped. The higher this is, the faster upload speeds the client can get to a
 
2564
single peer.</p>
 
2565
<p><tt class="docutils literal"><span class="pre">max_out_request_queue</span></tt> is the maximum number of outstanding requests to
 
2566
send to a peer. This limit takes precedence over <tt class="docutils literal"><span class="pre">request_queue_time</span></tt>. i.e.
 
2567
no matter the download speed, the number of outstanding requests will never
 
2568
exceed this limit.</p>
 
2569
<p><tt class="docutils literal"><span class="pre">whole_pieces_threshold</span></tt> is a limit in seconds. if a whole piece can be
 
2570
downloaded in at least this number of seconds from a specific peer, the
 
2571
peer_connection will prefer requesting whole pieces at a time from this peer.
 
2572
The benefit of this is to better utilize disk caches by doing localized
 
2573
accesses and also to make it easier to identify bad peers if a piece fails
 
2574
the hash check.</p>
 
2575
<p><tt class="docutils literal"><span class="pre">peer_timeout</span></tt> is the number of seconds the peer connection should
 
2576
wait (for any activity on the peer connection) before closing it due
 
2577
to time out. This defaults to 120 seconds, since that's what's specified
 
2578
in the protocol specification. After half the time out, a keep alive message
 
2579
is sent.</p>
 
2580
<p><tt class="docutils literal"><span class="pre">urlseed_timeout</span></tt> is the same as <tt class="docutils literal"><span class="pre">peer_timeout</span></tt> but applies only to
 
2581
url seeds. This value defaults to 20 seconds.</p>
 
2582
<p><tt class="docutils literal"><span class="pre">urlseed_pipeline_size</span></tt> controls the pipelining with the web server. When
 
2583
using persistent connections to HTTP 1.1 servers, the client is allowed to
 
2584
send more requests before the first response is received. This number controls
 
2585
the number of outstanding requests to use with url-seeds. Default is 5.</p>
 
2586
<p><tt class="docutils literal"><span class="pre">file_pool_size</span></tt> is the the upper limit on the total number of files this
 
2587
session will keep open. The reason why files are left open at all is that
 
2588
some anti virus software hooks on every file close, and scans the file for
 
2589
viruses. deferring the closing of the files will be the difference between
 
2590
a usable system and a completely hogged down system. Most operating systems
 
2591
also has a limit on the total number of file descriptors a process may have
 
2592
open. It is usually a good idea to find this limit and set the number of
 
2593
connections and the number of files limits so their sum is slightly below it.</p>
 
2594
<p><tt class="docutils literal"><span class="pre">allow_multiple_connections_per_ip</span></tt> determines if connections from the
 
2595
same IP address as existing connections should be rejected or not. Multiple
 
2596
connections from the same IP address is not allowed by default, to prevent
 
2597
abusive behavior by peers. It may be useful to allow such connections in
 
2598
cases where simulations are run on the same machie, and all peers in a
 
2599
swarm has the same IP address.</p>
 
2600
<p><tt class="docutils literal"><span class="pre">max_failcount</span></tt> is the maximum times we try to connect to a peer before
 
2601
stop connecting again. If a peer succeeds, the failcounter is reset. If
 
2602
a peer is retrieved from a peer source (other than DHT) the failcount is
 
2603
decremented by one, allowing another try.</p>
 
2604
<p><tt class="docutils literal"><span class="pre">min_reconnect_time</span></tt> is the time to wait between connection attempts. If
 
2605
the peer fails, the time is multiplied by fail counter.</p>
 
2606
<p><tt class="docutils literal"><span class="pre">peer_connect_timeout</span></tt> the number of seconds to wait after a connection
 
2607
attempt is initiated to a peer until it is considered as having timed out.
 
2608
The default is 10 seconds. This setting is especially important in case
 
2609
the number of half-open connections are limited, since stale half-open
 
2610
connection may delay the connection of other peers considerably.</p>
 
2611
<p><tt class="docutils literal"><span class="pre">ignore_limits_on_local_network</span></tt>, if set to true, upload, download and
 
2612
unchoke limits are ignored for peers on the local network.</p>
 
2613
<p><tt class="docutils literal"><span class="pre">connection_speed</span></tt> is the number of connection attempts that
 
2614
are made per second. If a number &lt;= 0 is specified, it will default to
 
2615
200 connections per second.</p>
 
2616
<p><tt class="docutils literal"><span class="pre">send_redundant_have</span></tt> controls if have messages will be sent
 
2617
to peers that already have the piece. This is typically not necessary,
 
2618
but it might be necessary for collecting statistics in some cases.
 
2619
Default is false.</p>
 
2620
<p><tt class="docutils literal"><span class="pre">lazy_bitfields</span></tt> prevents outgoing bitfields from being full. If the
 
2621
client is seed, a few bits will be set to 0, and later filled in with
 
2622
have-messages. This is to prevent certain ISPs from stopping people
 
2623
from seeding.</p>
 
2624
<p><tt class="docutils literal"><span class="pre">inactivity_timeout</span></tt>, if a peer is uninteresting and uninterested
 
2625
for longer than this number of seconds, it will be disconnected.
 
2626
Default is 10 minutes</p>
 
2627
<p><tt class="docutils literal"><span class="pre">unchoke_interval</span></tt> is the number of seconds between chokes/unchokes.
 
2628
On this interval, peers are re-evaluated for being choked/unchoked. This
 
2629
is defined as 30 seconds in the protocol, and it should be significantly
 
2630
longer than what it takes for TCP to ramp up to it's max rate.</p>
 
2631
<p><tt class="docutils literal"><span class="pre">optimistic_unchoke_multiplier</span></tt> is the number of unchoke intervals between
 
2632
each <em>optimistic</em> unchoke interval. On this timer, the currently optimistically
 
2633
unchoked peer will change.</p>
 
2634
<p><tt class="docutils literal"><span class="pre">announce_ip</span></tt> is the ip address passed along to trackers as the <tt class="docutils literal"><span class="pre">&amp;ip=</span></tt> parameter.
 
2635
If left as the default (default constructed), that parameter is ommited.</p>
 
2636
<p><tt class="docutils literal"><span class="pre">num_want</span></tt> is the number of peers we want from each tracker request. It defines
 
2637
what is sent as the <tt class="docutils literal"><span class="pre">&amp;num_want=</span></tt> parameter to the tracker.</p>
 
2638
<p><tt class="docutils literal"><span class="pre">initial_picker_threshold</span></tt> specifies the number of pieces we need before we
 
2639
switch to rarest first picking. This defaults to 4, which means the 4 first
 
2640
pieces in any torrent are picked at random, the following pieces are picked
 
2641
in rarest first order.</p>
 
2642
<p><tt class="docutils literal"><span class="pre">allowed_fast_set_size</span></tt> is the number of pieces we allow peers to download
 
2643
from us without being unchoked.</p>
 
2644
<p><tt class="docutils literal"><span class="pre">max_outstanding_disk_bytes_per_connection</span></tt> is the number of bytes each
 
2645
connection is allowed to have waiting in the disk I/O queue before it is
 
2646
throttled back. This limit is meant to stop fast internet connections to
 
2647
queue up bufferes indefinitely on slow hard-drives or storage.</p>
 
2648
<p><tt class="docutils literal"><span class="pre">handshake_timeout</span></tt> specifies the number of seconds we allow a peer to
 
2649
delay responding to a protocol handshake. If no response is received within
 
2650
this time, the connection is closed.</p>
 
2651
<p><tt class="docutils literal"><span class="pre">use_dht_as_fallback</span></tt> determines how the DHT is used. If this is true
 
2652
(which it is by default), the DHT will only be used for torrents where
 
2653
all trackers in its tracker list has failed. Either by an explicit error
 
2654
message or a time out.</p>
 
2655
<p><tt class="docutils literal"><span class="pre">free_torrent_hashes</span></tt> determines whether or not the torrent's piece hashes
 
2656
are kept in memory after the torrent becomes a seed or not. If it is set to
 
2657
<tt class="docutils literal"><span class="pre">true</span></tt> the hashes are freed once the torrent is a seed (they're not
 
2658
needed anymore since the torrent won't download anything more). If it's set
 
2659
to false they are not freed. If they are freed, the <a class="reference internal" href="#torrent-info">torrent_info</a> returned
 
2660
by get_torrent_info() will return an object that may be incomplete, that
 
2661
cannot be passed back to <a class="reference internal" href="#add-torrent">add_torrent()</a> for instance.</p>
 
2662
<p><tt class="docutils literal"><span class="pre">upnp_ignore_nonrouters</span></tt> indicates whether or not the UPnP implementation
 
2663
should ignore any broadcast response from a device whose address is not the
 
2664
configured router for this machine. i.e. it's a way to not talk to other
 
2665
people's routers by mistake.</p>
 
2666
</div>
 
2667
<div class="section" id="pe-settings">
 
2668
<h1>pe_settings</h1>
 
2669
<p>The <tt class="docutils literal"><span class="pre">pe_settings</span></tt> structure is used to control the settings related
 
2670
to peer protocol encryption:</p>
 
2671
<pre class="literal-block">
 
2672
struct pe_settings
 
2673
{
 
2674
        pe_settings();
 
2675
 
 
2676
        enum enc_policy
 
2677
        {
 
2678
                forced,
 
2679
                enabled,
 
2680
                disabled
 
2681
        };
 
2682
 
 
2683
        enum enc_level
 
2684
        {
 
2685
                plaintext,
 
2686
                rc4,
 
2687
                both
 
2688
        };
 
2689
 
 
2690
        enc_policy out_enc_policy;
 
2691
        enc_policy in_enc_policy;
 
2692
        enc_level allowed_enc_level;
 
2693
        bool prefer_rc4;
 
2694
};
 
2695
</pre>
 
2696
<p><tt class="docutils literal"><span class="pre">in_enc_policy</span></tt> and <tt class="docutils literal"><span class="pre">out_enc_policy</span></tt> control the settings for incoming
 
2697
and outgoing connections respectively. The settings for these are:</p>
 
2698
<blockquote>
 
2699
<ul class="simple">
 
2700
<li><tt class="docutils literal"><span class="pre">forced</span></tt> - Only encrypted connections are allowed. Incoming connections
 
2701
that are not encrypted are closed and if the encrypted outgoing connection
 
2702
fails, a non-encrypted retry will not be made.</li>
 
2703
<li><tt class="docutils literal"><span class="pre">enabled</span></tt> - encrypted connections are enabled, but non-encrypted
 
2704
connections are allowed. An incoming non-encrypted connection will
 
2705
be accepted, and if an outgoing encrypted connection fails, a non-
 
2706
encrypted connection will be tried.</li>
 
2707
<li><tt class="docutils literal"><span class="pre">disabled</span></tt> - only non-encrypted connections are allowed.</li>
 
2708
</ul>
 
2709
</blockquote>
 
2710
<p><tt class="docutils literal"><span class="pre">allowed_enc_level</span></tt> determines the encryption level of the
 
2711
connections.  This setting will adjust which encryption scheme is
 
2712
offered to the other peer, as well as which encryption scheme is
 
2713
selected by the client. The settings are:</p>
 
2714
<blockquote>
 
2715
<ul class="simple">
 
2716
<li><tt class="docutils literal"><span class="pre">plaintext</span></tt> - only the handshake is encrypted, the bulk of the traffic
 
2717
remains unchanged.</li>
 
2718
<li><tt class="docutils literal"><span class="pre">rc4</span></tt> - the entire stream is encrypted with RC4</li>
 
2719
<li><tt class="docutils literal"><span class="pre">both</span></tt> - both RC4 and plaintext connections are allowed.</li>
 
2720
</ul>
 
2721
</blockquote>
 
2722
<p><tt class="docutils literal"><span class="pre">prefer_rc4</span></tt> can be set to true if you want to prefer the RC4 encrypted stream.</p>
 
2723
</div>
 
2724
<div class="section" id="proxy-settings">
 
2725
<h1>proxy_settings</h1>
 
2726
<p>The <tt class="docutils literal"><span class="pre">proxy_settings</span></tt> structs contains the information needed to
 
2727
direct certain traffic to a proxy.</p>
 
2728
<blockquote>
 
2729
<pre class="literal-block">
 
2730
struct proxy_settings
 
2731
{
 
2732
        proxy_settings();
 
2733
 
 
2734
        std::string hostname;
 
2735
        int port;
 
2736
 
 
2737
        std::string username;
 
2738
        std::string password;
 
2739
 
 
2740
        enum proxy_type
 
2741
        {
 
2742
                none,
 
2743
                socks4,
 
2744
                socks5,
 
2745
                socks5_pw,
 
2746
                http,
 
2747
                http_pw
 
2748
        };
 
2749
 
 
2750
        proxy_type type;
 
2751
};
 
2752
</pre>
 
2753
</blockquote>
 
2754
<p><tt class="docutils literal"><span class="pre">hostname</span></tt> is the name or IP of the proxy server. <tt class="docutils literal"><span class="pre">port</span></tt> is the
 
2755
port number the proxy listens to. If required, <tt class="docutils literal"><span class="pre">username</span></tt> and <tt class="docutils literal"><span class="pre">password</span></tt>
 
2756
can be set to authenticate with the proxy.</p>
 
2757
<p>The <tt class="docutils literal"><span class="pre">type</span></tt> tells libtorrent what kind of proxy server it is. The following
 
2758
options are available:</p>
 
2759
<blockquote>
 
2760
<ul class="simple">
 
2761
<li><tt class="docutils literal"><span class="pre">none</span></tt> - This is the default, no proxy server is used, all other fields
 
2762
are ignored.</li>
 
2763
<li><tt class="docutils literal"><span class="pre">socks4</span></tt> - The server is assumed to be a <a class="reference external" href="http://www.ufasoft.com/doc/socks4_protocol.htm">SOCKS4 server</a> that
 
2764
requires a username.</li>
 
2765
<li><tt class="docutils literal"><span class="pre">socks5</span></tt> - The server is assumed to be a SOCKS5 server (<a class="reference external" href="http://www.faqs.org/rfcs/rfc1928.html">RFC 1928</a>) that
 
2766
does not require any authentication. The username and password are ignored.</li>
 
2767
<li><tt class="docutils literal"><span class="pre">socks5_pw</span></tt> - The server is assumed to be a SOCKS5 server that supports
 
2768
plain text username and password authentication (<a class="reference external" href="http://www.faqs.org/rfcs/rfc1929.html">RFC 1929</a>). The username
 
2769
and password specified may be sent to the proxy if it requires.</li>
 
2770
<li><tt class="docutils literal"><span class="pre">http</span></tt> - The server is assumed to be an HTTP proxy. If the transport used
 
2771
for the connection is non-HTTP, the server is assumed to support the
 
2772
<a class="reference external" href="draft-luotonen-web-proxy-tunneling-01.txt">CONNECT</a> method. i.e. for web seeds and HTTP trackers, a plain proxy will
 
2773
suffice. The proxy is assumed to not require authorization. The username
 
2774
and password will not be used.</li>
 
2775
<li><tt class="docutils literal"><span class="pre">http_pw</span></tt> - The server is assumed to be an HTTP proxy that requires
 
2776
user authorization. The username and password will be sent to the proxy.</li>
 
2777
</ul>
 
2778
</blockquote>
 
2779
</div>
 
2780
<div class="section" id="ip-filter">
 
2781
<h1>ip_filter</h1>
 
2782
<p>The <tt class="docutils literal"><span class="pre">ip_filter</span></tt> class is a set of rules that uniquely categorizes all
 
2783
ip addresses as allowed or disallowed. The default constructor creates
 
2784
a single rule that allows all addresses (0.0.0.0 - 255.255.255.255 for
 
2785
the IPv4 range, and the equivalent range covering all addresses for the
 
2786
IPv6 range).</p>
 
2787
<blockquote>
 
2788
<pre class="literal-block">
 
2789
template &lt;class Addr&gt;
 
2790
struct ip_range
 
2791
{
 
2792
        Addr first;
 
2793
        Addr last;
 
2794
        int flags;
 
2795
};
 
2796
 
 
2797
class ip_filter
 
2798
{
 
2799
public:
 
2800
        enum access_flags { blocked = 1 };
 
2801
 
 
2802
        ip_filter();
 
2803
        void add_rule(address first, address last, int flags);
 
2804
        int access(address const&amp; addr) const;
 
2805
 
 
2806
        typedef boost::tuple&lt;std::vector&lt;ip_range&lt;address_v4&gt; &gt;
 
2807
                , std::vector&lt;ip_range&lt;address_v6&gt; &gt; &gt; filter_tuple_t;
 
2808
 
 
2809
        filter_tuple_t export_filter() const;
 
2810
};
 
2811
</pre>
 
2812
</blockquote>
 
2813
<div class="section" id="id9">
 
2814
<h2>ip_filter()</h2>
 
2815
<blockquote>
 
2816
<pre class="literal-block">
 
2817
ip_filter()
 
2818
</pre>
 
2819
</blockquote>
 
2820
<p>Creates a default filter that doesn't filter any address.</p>
 
2821
<p>postcondition:
 
2822
<tt class="docutils literal"><span class="pre">access(x)</span> <span class="pre">==</span> <span class="pre">0</span></tt> for every <tt class="docutils literal"><span class="pre">x</span></tt></p>
 
2823
</div>
 
2824
<div class="section" id="add-rule">
 
2825
<h2>add_rule()</h2>
 
2826
<blockquote>
 
2827
<pre class="literal-block">
 
2828
void add_rule(address first, address last, int flags);
 
2829
</pre>
 
2830
</blockquote>
 
2831
<p>Adds a rule to the filter. <tt class="docutils literal"><span class="pre">first</span></tt> and <tt class="docutils literal"><span class="pre">last</span></tt> defines a range of
 
2832
ip addresses that will be marked with the given flags. The <tt class="docutils literal"><span class="pre">flags</span></tt>
 
2833
can currently be 0, which means allowed, or <tt class="docutils literal"><span class="pre">ip_filter::blocked</span></tt>, which
 
2834
means disallowed.</p>
 
2835
<p>precondition:
 
2836
<tt class="docutils literal"><span class="pre">first.is_v4()</span> <span class="pre">==</span> <span class="pre">last.is_v4()</span> <span class="pre">&amp;&amp;</span> <span class="pre">first.is_v6()</span> <span class="pre">==</span> <span class="pre">last.is_v6()</span></tt></p>
 
2837
<p>postcondition:
 
2838
<tt class="docutils literal"><span class="pre">access(x)</span> <span class="pre">==</span> <span class="pre">flags</span></tt> for every <tt class="docutils literal"><span class="pre">x</span></tt> in the range [<tt class="docutils literal"><span class="pre">first</span></tt>, <tt class="docutils literal"><span class="pre">last</span></tt>]</p>
 
2839
<p>This means that in a case of overlapping ranges, the last one applied takes
 
2840
precedence.</p>
 
2841
</div>
 
2842
<div class="section" id="access">
 
2843
<h2>access()</h2>
 
2844
<blockquote>
 
2845
<pre class="literal-block">
 
2846
int access(address const&amp; addr) const;
 
2847
</pre>
 
2848
</blockquote>
 
2849
<p>Returns the access permissions for the given address (<tt class="docutils literal"><span class="pre">addr</span></tt>). The permission
 
2850
can currently be 0 or <tt class="docutils literal"><span class="pre">ip_filter::blocked</span></tt>. The complexity of this operation
 
2851
is O(<tt class="docutils literal"><span class="pre">log</span></tt> n), where n is the minimum number of non-overlapping ranges to describe
 
2852
the current filter.</p>
 
2853
</div>
 
2854
<div class="section" id="export-filter">
 
2855
<h2>export_filter()</h2>
 
2856
<blockquote>
 
2857
<pre class="literal-block">
 
2858
boost::tuple&lt;std::vector&lt;ip_range&lt;address_v4&gt; &gt;
 
2859
        , std::vector&lt;ip_range&lt;address_v6&gt; &gt; &gt; export_filter() const;
 
2860
</pre>
 
2861
</blockquote>
 
2862
<p>This function will return the current state of the filter in the minimum number of
 
2863
ranges possible. They are sorted from ranges in low addresses to high addresses. Each
 
2864
entry in the returned vector is a range with the access control specified in its
 
2865
<tt class="docutils literal"><span class="pre">flags</span></tt> field.</p>
 
2866
<p>The return value is a tuple containing two range-lists. One for IPv4 addresses
 
2867
and one for IPv6 addresses.</p>
 
2868
</div>
 
2869
</div>
 
2870
<div class="section" id="big-number">
 
2871
<h1>big_number</h1>
 
2872
<p>Both the <tt class="docutils literal"><span class="pre">peer_id</span></tt> and <tt class="docutils literal"><span class="pre">sha1_hash</span></tt> types are typedefs of the class
 
2873
<tt class="docutils literal"><span class="pre">big_number</span></tt>. It represents 20 bytes of data. Its synopsis follows:</p>
 
2874
<pre class="literal-block">
 
2875
class big_number
 
2876
{
 
2877
public:
 
2878
        bool operator==(const big_number&amp; n) const;
 
2879
        bool operator!=(const big_number&amp; n) const;
 
2880
        bool operator&lt;(const big_number&amp; n) const;
 
2881
 
 
2882
        const unsigned char* begin() const;
 
2883
        const unsigned char* end() const;
 
2884
 
 
2885
        unsigned char* begin();
 
2886
        unsigned char* end();
 
2887
};
 
2888
</pre>
 
2889
<p>The iterators gives you access to individual bytes.</p>
 
2890
</div>
 
2891
<div class="section" id="hasher">
 
2892
<h1>hasher</h1>
 
2893
<p>This class creates sha1-hashes. Its declaration looks like this:</p>
 
2894
<pre class="literal-block">
 
2895
class hasher
 
2896
{
 
2897
public:
 
2898
        hasher();
 
2899
        hasher(char const* data, unsigned int len);
 
2900
 
 
2901
        void update(char const* data, unsigned int len);
 
2902
        sha1_hash final();
 
2903
        void reset();
 
2904
};
 
2905
</pre>
 
2906
<p>You use it by first instantiating it, then call <tt class="docutils literal"><span class="pre">update()</span></tt> to feed it
 
2907
with data. i.e. you don't have to keep the entire buffer of which you want to
 
2908
create the hash in memory. You can feed the hasher parts of it at a time. When
 
2909
You have fed the hasher with all the data, you call <tt class="docutils literal"><span class="pre">final()</span></tt> and it
 
2910
will return the sha1-hash of the data.</p>
 
2911
<p>The constructor that takes a <tt class="docutils literal"><span class="pre">char</span> <span class="pre">const*</span></tt> and an integer will construct the
 
2912
sha1 context and feed it the data passed in.</p>
 
2913
<p>If you want to reuse the hasher object once you have created a hash, you have to
 
2914
call <tt class="docutils literal"><span class="pre">reset()</span></tt> to reinitialize it.</p>
 
2915
<p>The sha1-algorithm used was implemented by Steve Reid and released as public domain.
 
2916
For more info, see <tt class="docutils literal"><span class="pre">src/sha1.cpp</span></tt>.</p>
 
2917
</div>
 
2918
<div class="section" id="fingerprint">
 
2919
<h1>fingerprint</h1>
 
2920
<p>The fingerprint class represents information about a client and its version. It is used
 
2921
to encode this information into the client's peer id.</p>
 
2922
<p>This is the class declaration:</p>
 
2923
<pre class="literal-block">
 
2924
struct fingerprint
 
2925
{
 
2926
        fingerprint(const char* id_string, int major, int minor
 
2927
                , int revision, int tag);
 
2928
 
 
2929
        std::string to_string() const;
 
2930
 
 
2931
        char name[2];
 
2932
        char major_version;
 
2933
        char minor_version;
 
2934
        char revision_version;
 
2935
        char tag_version;
 
2936
 
 
2937
};
 
2938
</pre>
 
2939
<p>The constructor takes a <tt class="docutils literal"><span class="pre">char</span> <span class="pre">const*</span></tt> that should point to a string constant containing
 
2940
exactly two characters. These are the characters that should be unique for your client. Make
 
2941
sure not to clash with anybody else. Here are some taken id's:</p>
 
2942
<table border="1" class="docutils">
 
2943
<colgroup>
 
2944
<col width="30%" />
 
2945
<col width="70%" />
 
2946
</colgroup>
 
2947
<thead valign="bottom">
 
2948
<tr><th class="head">id chars</th>
 
2949
<th class="head">client</th>
 
2950
</tr>
 
2951
</thead>
 
2952
<tbody valign="top">
 
2953
<tr><td>'AZ'</td>
 
2954
<td>Azureus</td>
 
2955
</tr>
 
2956
<tr><td>'LT'</td>
 
2957
<td>libtorrent (default)</td>
 
2958
</tr>
 
2959
<tr><td>'BX'</td>
 
2960
<td>BittorrentX</td>
 
2961
</tr>
 
2962
<tr><td>'MT'</td>
 
2963
<td>Moonlight Torrent</td>
 
2964
</tr>
 
2965
<tr><td>'TS'</td>
 
2966
<td>Torrent Storm</td>
 
2967
</tr>
 
2968
<tr><td>'SS'</td>
 
2969
<td>Swarm Scope</td>
 
2970
</tr>
 
2971
<tr><td>'XT'</td>
 
2972
<td>Xan Torrent</td>
 
2973
</tr>
 
2974
</tbody>
 
2975
</table>
 
2976
<p>There's currently an informal directory of client id's <a class="reference external" href="http://wiki.theory.org/BitTorrentSpecification#peer_id">here</a>.</p>
 
2977
<p>The <tt class="docutils literal"><span class="pre">major</span></tt>, <tt class="docutils literal"><span class="pre">minor</span></tt>, <tt class="docutils literal"><span class="pre">revision</span></tt> and <tt class="docutils literal"><span class="pre">tag</span></tt> parameters are used to identify the
 
2978
version of your client. All these numbers must be within the range [0, 9].</p>
 
2979
<p><tt class="docutils literal"><span class="pre">to_string()</span></tt> will generate the actual string put in the peer-id, and return it.</p>
 
2980
</div>
 
2981
<div class="section" id="free-functions">
 
2982
<h1>free functions</h1>
 
2983
<div class="section" id="identify-client">
 
2984
<h2>identify_client()</h2>
 
2985
<blockquote>
 
2986
<pre class="literal-block">
 
2987
std::string identify_client(peer_id const&amp; id);
 
2988
</pre>
 
2989
</blockquote>
 
2990
<p>This function is declared in the header <tt class="docutils literal"><span class="pre">&lt;libtorrent/identify_client.hpp&gt;</span></tt>. It can can be used
 
2991
to extract a string describing a client version from its peer-id. It will recognize most clients
 
2992
that have this kind of identification in the peer-id.</p>
 
2993
</div>
 
2994
<div class="section" id="client-fingerprint">
 
2995
<h2>client_fingerprint()</h2>
 
2996
<blockquote>
 
2997
<pre class="literal-block">
 
2998
boost::optional&lt;fingerprint&gt; client_fingerprint(peer_id const&amp; p);
 
2999
</pre>
 
3000
</blockquote>
 
3001
<p>Returns an optional fingerprint if any can be identified from the peer id. This can be used
 
3002
to automate the identification of clients. It will not be able to identify peers with non-
 
3003
standard encodings. Only Azureus style, Shadow's style and Mainline style. This function is
 
3004
declared in the header <tt class="docutils literal"><span class="pre">&lt;libtorrent/identify_client.hpp&gt;</span></tt>.</p>
 
3005
</div>
 
3006
<div class="section" id="bdecode-bencode">
 
3007
<h2>bdecode() bencode()</h2>
 
3008
<blockquote>
 
3009
<pre class="literal-block">
 
3010
template&lt;class InIt&gt; entry bdecode(InIt start, InIt end);
 
3011
template&lt;class OutIt&gt; void bencode(OutIt out, const entry&amp; e);
 
3012
</pre>
 
3013
</blockquote>
 
3014
<p>These functions will encode data to <a class="reference external" href="http://wiki.theory.org/index.php/BitTorrentSpecification">bencoded</a> or decode <a class="reference external" href="http://wiki.theory.org/index.php/BitTorrentSpecification">bencoded</a> data.</p>
 
3015
<p>The <a class="reference internal" href="#entry">entry</a> class is the internal representation of the bencoded data
 
3016
and it can be used to retrieve information, an <a class="reference internal" href="#entry">entry</a> can also be build by
 
3017
the program and given to <tt class="docutils literal"><span class="pre">bencode()</span></tt> to encode it into the <tt class="docutils literal"><span class="pre">OutIt</span></tt>
 
3018
iterator.</p>
 
3019
<p>The <tt class="docutils literal"><span class="pre">OutIt</span></tt> and <tt class="docutils literal"><span class="pre">InIt</span></tt> are iterators
 
3020
(<a class="reference external" href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a> and <a class="reference external" href="http://www.sgi.com/tech/stl/OutputIterator.html">OutputIterator</a> respectively). They
 
3021
are templates and are usually instantiated as <a class="reference external" href="http://www.sgi.com/tech/stl/ostream_iterator.html">ostream_iterator</a>,
 
3022
<a class="reference external" href="http://www.sgi.com/tech/stl/back_insert_iterator.html">back_insert_iterator</a> or <a class="reference external" href="http://www.sgi.com/tech/stl/istream_iterator.html">istream_iterator</a>. These
 
3023
functions will assume that the iterator refers to a character
 
3024
(<tt class="docutils literal"><span class="pre">char</span></tt>). So, if you want to encode entry <tt class="docutils literal"><span class="pre">e</span></tt> into a buffer
 
3025
in memory, you can do it like this:</p>
 
3026
<pre class="literal-block">
 
3027
std::vector&lt;char&gt; buffer;
 
3028
bencode(std::back_inserter(buf), e);
 
3029
</pre>
 
3030
<p>If you want to decode a torrent file from a buffer in memory, you can do it like this:</p>
 
3031
<pre class="literal-block">
 
3032
std::vector&lt;char&gt; buffer;
 
3033
// ...
 
3034
entry e = bdecode(buf.begin(), buf.end());
 
3035
</pre>
 
3036
<p>Or, if you have a raw char buffer:</p>
 
3037
<pre class="literal-block">
 
3038
const char* buf;
 
3039
// ...
 
3040
entry e = bdecode(buf, buf + data_size);
 
3041
</pre>
 
3042
<p>Now we just need to know how to retrieve information from the <a class="reference internal" href="#entry">entry</a>.</p>
 
3043
<p>If <tt class="docutils literal"><span class="pre">bdecode()</span></tt> encounters invalid encoded data in the range given to it
 
3044
it will throw <a class="reference internal" href="#invalid-encoding">invalid_encoding</a>.</p>
 
3045
</div>
 
3046
</div>
 
3047
<div class="section" id="alerts">
 
3048
<h1>alerts</h1>
 
3049
<p>The <tt class="docutils literal"><span class="pre">pop_alert()</span></tt> function on session is the interface for retrieving
 
3050
alerts, warnings, messages and errors from libtorrent. If there hasn't
 
3051
occurred any errors (matching your severity level) <tt class="docutils literal"><span class="pre">pop_alert()</span></tt> will
 
3052
return a zero pointer. If there has been some error, it will return a pointer
 
3053
to an alert object describing it. You can then use the alert object and query
 
3054
it for information about the error or message. To retrieve any alerts, you have
 
3055
to select a severity level using <tt class="docutils literal"><span class="pre">session::set_severity_level()</span></tt>. It defaults to
 
3056
<tt class="docutils literal"><span class="pre">alert::none</span></tt>, which means that you don't get any messages at all, ever.
 
3057
You have the following levels to select among:</p>
 
3058
<table border="1" class="docutils">
 
3059
<colgroup>
 
3060
<col width="19%" />
 
3061
<col width="81%" />
 
3062
</colgroup>
 
3063
<tbody valign="top">
 
3064
<tr><td><tt class="docutils literal"><span class="pre">none</span></tt></td>
 
3065
<td>No alert will ever have this severity level, which
 
3066
effectively filters all messages.</td>
 
3067
</tr>
 
3068
<tr><td><tt class="docutils literal"><span class="pre">fatal</span></tt></td>
 
3069
<td>Fatal errors will have this severity level. Examples can
 
3070
be disk full or something else that will make it
 
3071
impossible to continue normal execution.</td>
 
3072
</tr>
 
3073
<tr><td><tt class="docutils literal"><span class="pre">critical</span></tt></td>
 
3074
<td>Signals errors that requires user interaction or
 
3075
messages that almost never should be ignored. For
 
3076
example, a chat message received from another peer is
 
3077
announced as severity <tt class="docutils literal"><span class="pre">critical</span></tt>.</td>
 
3078
</tr>
 
3079
<tr><td><tt class="docutils literal"><span class="pre">warning</span></tt></td>
 
3080
<td>Messages with the warning severity can be a tracker that
 
3081
times out or responds with invalid data. It will be
 
3082
retried automatically, and the possible next tracker in
 
3083
a multitracker sequence will be tried. It does not
 
3084
require any user interaction.</td>
 
3085
</tr>
 
3086
<tr><td><tt class="docutils literal"><span class="pre">info</span></tt></td>
 
3087
<td>Events that can be considered normal, but still deserves
 
3088
an event. This could be a piece hash that fails.</td>
 
3089
</tr>
 
3090
<tr><td><tt class="docutils literal"><span class="pre">debug</span></tt></td>
 
3091
<td>This will include a lot of debug events that can be used
 
3092
both for debugging libtorrent but also when debugging
 
3093
other clients that are connected to libtorrent. It will
 
3094
report strange behaviors among the connected peers.</td>
 
3095
</tr>
 
3096
</tbody>
 
3097
</table>
 
3098
<p>When setting a severity level, you will receive messages of that severity and all
 
3099
messages that are more sever. If you set <tt class="docutils literal"><span class="pre">alert::none</span></tt> (the default) you will not receive
 
3100
any events at all.</p>
 
3101
<p>When you set a severity level other than <tt class="docutils literal"><span class="pre">none</span></tt>, you have the responsibility to call
 
3102
<tt class="docutils literal"><span class="pre">pop_alert()</span></tt> from time to time. If you don't do that, the alert queue will just grow.</p>
 
3103
<p>When you get an alert, you can use <tt class="docutils literal"><span class="pre">typeid()</span></tt> or <tt class="docutils literal"><span class="pre">dynamic_cast&lt;&gt;</span></tt> to get more detailed
 
3104
information on exactly which type it is. i.e. what kind of error it is. You can also use a
 
3105
<a class="reference internal" href="#dispatcher">dispatcher</a> mechanism that's available in libtorrent.</p>
 
3106
<p>All alert types are defined in the <tt class="docutils literal"><span class="pre">&lt;libtorrent/alert_types.hpp&gt;</span></tt> header file.</p>
 
3107
<p>The <tt class="docutils literal"><span class="pre">alert</span></tt> class is the base class that specific messages are derived from. This
 
3108
is its synopsis:</p>
 
3109
<pre class="literal-block">
 
3110
class alert
 
3111
{
 
3112
public:
 
3113
 
 
3114
        enum severity_t { debug, info, warning, critical, fatal, none };
 
3115
 
 
3116
        alert(severity_t severity, std::string const&amp; msg);
 
3117
        virtual ~alert();
 
3118
 
 
3119
        std::string const&amp; msg() const;
 
3120
        severity_t severity() const;
 
3121
 
 
3122
        virtual std::auto_ptr&lt;alert&gt; clone() const = 0;
 
3123
};
 
3124
</pre>
 
3125
<p>This means that all alerts have at least a string describing it. They also
 
3126
have a severity level that can be used to sort them or present them to the
 
3127
user in different ways.</p>
 
3128
<p>There's another alert base class that all most alerts derives from, all the
 
3129
alerts that are generated for a specific torrent are derived from:</p>
 
3130
<pre class="literal-block">
 
3131
struct torrent_alert: alert
 
3132
{
 
3133
        torrent_alert(torrent_handle const&amp; h, severity_t s, std::string const&amp; msg);
 
3134
 
 
3135
        torrent_handle handle;
 
3136
};
 
3137
</pre>
 
3138
<p>The specific alerts, that all derives from <tt class="docutils literal"><span class="pre">alert</span></tt>, are:</p>
 
3139
<div class="section" id="listen-failed-alert">
 
3140
<h2>listen_failed_alert</h2>
 
3141
<p>This alert is generated when none of the ports, given in the port range, to
 
3142
<a class="reference internal" href="#session">session</a> can be opened for listening. This alert is generated as severity
 
3143
level <tt class="docutils literal"><span class="pre">fatal</span></tt>.</p>
 
3144
<pre class="literal-block">
 
3145
struct listen_failed_alert: alert
 
3146
{
 
3147
        listen_failed_alert(const std::string&amp; msg);
 
3148
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3149
};
 
3150
</pre>
 
3151
</div>
 
3152
<div class="section" id="portmap-error-alert">
 
3153
<h2>portmap_error_alert</h2>
 
3154
<p>This alert is generated when a NAT router was successfully found but some
 
3155
part of the port mapping request failed. It contains a text message that
 
3156
may help the user figure out what is wrong. This alert is not generated in
 
3157
case it appears the client is not running on a NAT:ed network or if it
 
3158
appears there is no NAT router that can be remote controlled to add port
 
3159
mappings.</p>
 
3160
<p>The alert is generated as severity <tt class="docutils literal"><span class="pre">warning</span></tt>, since it should be displayed
 
3161
to the user somehow, and could mean reduced preformance.</p>
 
3162
<pre class="literal-block">
 
3163
struct portmap_error_alert: alert
 
3164
{
 
3165
        portmap_error_alert(const std::string&amp; msg);
 
3166
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3167
};
 
3168
</pre>
 
3169
</div>
 
3170
<div class="section" id="portmap-alert">
 
3171
<h2>portmap_alert</h2>
 
3172
<p>This alert is generated when a NAT router was successfully found and
 
3173
a port was successfully mapped on it. On a NAT:ed network with a NAT-PMP
 
3174
capable router, this is typically generated once when mapping the TCP
 
3175
port and, if DHT is enabled, when the UDP port is mapped. This is merely
 
3176
an informational alert, and is generated at severity level <tt class="docutils literal"><span class="pre">info</span></tt>.</p>
 
3177
<pre class="literal-block">
 
3178
struct portmap_alert: alert
 
3179
{
 
3180
        portmap_alert(const std::string&amp; msg);
 
3181
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3182
};
 
3183
</pre>
 
3184
</div>
 
3185
<div class="section" id="file-error-alert">
 
3186
<h2>file_error_alert</h2>
 
3187
<p>If the storage fails to read or write files that it needs access to, this alert is
 
3188
generated and the torrent is paused. It is generated as severity level <tt class="docutils literal"><span class="pre">fatal</span></tt>.</p>
 
3189
<pre class="literal-block">
 
3190
struct file_error_alert: torrent_alert
 
3191
{
 
3192
        file_error_alert(
 
3193
                const torrent_handle&amp; h
 
3194
                , const std::string&amp; msg);
 
3195
 
 
3196
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3197
};
 
3198
</pre>
 
3199
</div>
 
3200
<div class="section" id="tracker-announce-alert">
 
3201
<h2>tracker_announce_alert</h2>
 
3202
<p>This alert is generated each time a tracker announce is sent (or attempted to be sent).
 
3203
It is generated at severity level <tt class="docutils literal"><span class="pre">info</span></tt>.</p>
 
3204
<pre class="literal-block">
 
3205
struct tracker_announce_alert: torrent_alert
 
3206
{
 
3207
        tracker_announce_alert(
 
3208
                const torrent_handle&amp; h
 
3209
                , const std::string&amp; msg);
 
3210
 
 
3211
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3212
};
 
3213
</pre>
 
3214
</div>
 
3215
<div class="section" id="tracker-alert">
 
3216
<h2>tracker_alert</h2>
 
3217
<p>This alert is generated on tracker time outs, premature disconnects, invalid response or
 
3218
a HTTP response other than &quot;200 OK&quot;. From the alert you can get the handle to the torrent
 
3219
the tracker belongs to. This alert is generated as severity level <tt class="docutils literal"><span class="pre">warning</span></tt>.</p>
 
3220
<p>The <tt class="docutils literal"><span class="pre">times_in_row</span></tt> member says how many times in a row this tracker has failed.
 
3221
<tt class="docutils literal"><span class="pre">status_code</span></tt> is the code returned from the HTTP server. 401 means the tracker needs
 
3222
authentication, 404 means not found etc. If the tracker timed out, the code will be set
 
3223
to 0.</p>
 
3224
<pre class="literal-block">
 
3225
struct tracker_alert: torrent_alert
 
3226
{
 
3227
        tracker_alert(torrent_handle const&amp; h, int times, int status
 
3228
                , const std::string&amp; msg);
 
3229
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3230
 
 
3231
        int times_in_row;
 
3232
        int status_code;
 
3233
};
 
3234
</pre>
 
3235
</div>
 
3236
<div class="section" id="tracker-reply-alert">
 
3237
<h2>tracker_reply_alert</h2>
 
3238
<p>This alert is only for informational purpose. It is generated when a tracker announce
 
3239
succeeds. It is generated regardless what kind of tracker was used, be it UDP, HTTP or
 
3240
the DHT. It is generated with severity level <tt class="docutils literal"><span class="pre">info</span></tt>.</p>
 
3241
<pre class="literal-block">
 
3242
struct tracker_reply_alert: torrent_alert
 
3243
{
 
3244
        tracker_reply_alert(const torrent_handle&amp; h
 
3245
                , int num_peers
 
3246
                , const std::string&amp; msg);
 
3247
 
 
3248
        int num_peers;
 
3249
 
 
3250
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3251
};
 
3252
</pre>
 
3253
<p>The <tt class="docutils literal"><span class="pre">num_peers</span></tt> tells how many peers were returned from the tracker. This is
 
3254
not necessarily all new peers, some of them may already be connected.</p>
 
3255
</div>
 
3256
<div class="section" id="tracker-warning-alert">
 
3257
<h2>tracker_warning_alert</h2>
 
3258
<p>This alert is triggered if the tracker reply contains a warning field. Usually this
 
3259
means that the tracker announce was successful, but the tracker has a message to
 
3260
the client. The message string in the alert will contain the warning message from
 
3261
the tracker. It is generated with severity level <tt class="docutils literal"><span class="pre">warning</span></tt>.</p>
 
3262
<pre class="literal-block">
 
3263
struct tracker_warning_alert: torrent_alert
 
3264
{
 
3265
        tracker_warning_alert(torrent_handle const&amp; h
 
3266
                , std::string const&amp; msg);
 
3267
 
 
3268
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3269
};
 
3270
</pre>
 
3271
</div>
 
3272
<div class="section" id="scrape-reply-alert">
 
3273
<h2>scrape_reply_alert</h2>
 
3274
<pre class="literal-block">
 
3275
struct scrape_reply_alert: torrent_alert
 
3276
{
 
3277
        scrape_reply_alert(torrent_handle const&amp; h
 
3278
                , int incomplete_
 
3279
                , int complete_
 
3280
                , std::string const&amp; msg);
 
3281
 
 
3282
        int incomplete;
 
3283
        int complete;
 
3284
 
 
3285
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3286
};
 
3287
</pre>
 
3288
<p>This alert is generated when a scrape request succeeds. <tt class="docutils literal"><span class="pre">incomplete</span></tt>
 
3289
and <tt class="docutils literal"><span class="pre">complete</span></tt> is the data returned in the scrape response. These numbers
 
3290
may be -1 if the reponse was malformed.</p>
 
3291
</div>
 
3292
<div class="section" id="scrape-failed-alert">
 
3293
<h2>scrape_failed_alert</h2>
 
3294
<pre class="literal-block">
 
3295
struct scrape_failed_alert: torrent_alert
 
3296
{
 
3297
        scrape_failed_alert(torrent_handle const&amp; h
 
3298
                , std::string const&amp; msg);
 
3299
 
 
3300
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3301
};
 
3302
</pre>
 
3303
<p>If a scrape request fails, this alert is generated. This might be due
 
3304
to the tracker timing out, refusing connection or returning an http response
 
3305
code indicating an error.</p>
 
3306
</div>
 
3307
<div class="section" id="url-seed-alert">
 
3308
<h2>url_seed_alert</h2>
 
3309
<p>This alert is generated when a HTTP seed name lookup fails. This alert is
 
3310
generated as severity level <tt class="docutils literal"><span class="pre">warning</span></tt>.</p>
 
3311
<p>It contains <tt class="docutils literal"><span class="pre">url</span></tt> to the HTTP seed that failed along with an error message.</p>
 
3312
<pre class="literal-block">
 
3313
struct url_seed_alert: torrent_alert
 
3314
{
 
3315
        url_seed_alert(torrent_handle const&amp; h, std::string const&amp; url
 
3316
                , const std::string&amp; msg);
 
3317
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3318
 
 
3319
        std::string url;
 
3320
};
 
3321
</pre>
 
3322
</div>
 
3323
<div class="section" id="hash-failed-alert">
 
3324
<h2>hash_failed_alert</h2>
 
3325
<p>This alert is generated when a finished piece fails its hash check. You can get the handle
 
3326
to the torrent which got the failed piece and the index of the piece itself from the alert.
 
3327
This alert is generated as severity level <tt class="docutils literal"><span class="pre">info</span></tt>.</p>
 
3328
<pre class="literal-block">
 
3329
struct hash_failed_alert: torrent_alert
 
3330
{
 
3331
        hash_failed_alert(
 
3332
                torrent_handle const&amp; h
 
3333
                , int index
 
3334
                , const std::string&amp; msg);
 
3335
 
 
3336
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3337
 
 
3338
        int piece_index;
 
3339
};
 
3340
</pre>
 
3341
</div>
 
3342
<div class="section" id="peer-ban-alert">
 
3343
<h2>peer_ban_alert</h2>
 
3344
<p>This alert is generated when a peer is banned because it has sent too many corrupt pieces
 
3345
to us. It is generated at severity level <tt class="docutils literal"><span class="pre">info</span></tt>. The <tt class="docutils literal"><span class="pre">handle</span></tt> member is a <a class="reference internal" href="#torrent-handle">torrent_handle</a>
 
3346
to the torrent that this peer was a member of.</p>
 
3347
<pre class="literal-block">
 
3348
struct peer_ban_alert: torrent_alert
 
3349
{
 
3350
        peer_ban_alert(
 
3351
                asio::ip::tcp::endpoint const&amp; pip
 
3352
                , torrent_handle h
 
3353
                , const std::string&amp; msg);
 
3354
 
 
3355
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3356
 
 
3357
        asio::ip::tcp::endpoint ip;
 
3358
};
 
3359
</pre>
 
3360
</div>
 
3361
<div class="section" id="peer-error-alert">
 
3362
<h2>peer_error_alert</h2>
 
3363
<p>This alert is generated when a peer sends invalid data over the peer-peer protocol. The peer
 
3364
will be disconnected, but you get its ip address from the alert, to identify it. This alert
 
3365
is generated as severity level <tt class="docutils literal"><span class="pre">debug</span></tt>.</p>
 
3366
<pre class="literal-block">
 
3367
struct peer_error_alert: alert
 
3368
{
 
3369
        peer_error_alert(
 
3370
                asio::ip::tcp::endpoint const&amp; pip
 
3371
                , peer_id const&amp; pid
 
3372
                , const std::string&amp; msg);
 
3373
 
 
3374
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3375
        asio::ip::tcp::endpoint ip;
 
3376
        peer_id id;
 
3377
};
 
3378
</pre>
 
3379
</div>
 
3380
<div class="section" id="invalid-request-alert">
 
3381
<h2>invalid_request_alert</h2>
 
3382
<p>This is a debug alert that is generated by an incoming invalid piece request. The <tt class="docutils literal"><span class="pre">handle</span></tt>
 
3383
is a handle to the torrent the peer is a member of. <tt class="docutils literal"><span class="pre">ìp</span></tt> is the address of the peer and the
 
3384
<tt class="docutils literal"><span class="pre">request</span></tt> is the actual incoming request from the peer. The alert is generated as severity level
 
3385
<tt class="docutils literal"><span class="pre">debug</span></tt>.</p>
 
3386
<pre class="literal-block">
 
3387
struct invalid_request_alert: torrent_alert
 
3388
{
 
3389
        invalid_request_alert(
 
3390
                peer_request const&amp; r
 
3391
                , torrent_handle const&amp; h
 
3392
                , asio::ip::tcp::endpoint const&amp; send
 
3393
                , peer_id const&amp; pid
 
3394
                , std::string const&amp; msg);
 
3395
 
 
3396
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3397
 
 
3398
        asio::ip::tcp::endpoint ip;
 
3399
        peer_request request;
 
3400
        peer_id id;
 
3401
};
 
3402
 
 
3403
 
 
3404
struct peer_request
 
3405
{
 
3406
        int piece;
 
3407
        int start;
 
3408
        int length;
 
3409
        bool operator==(peer_request const&amp; r) const;
 
3410
};
 
3411
</pre>
 
3412
<p>The <tt class="docutils literal"><span class="pre">peer_request</span></tt> contains the values the client sent in its <tt class="docutils literal"><span class="pre">request</span></tt> message. <tt class="docutils literal"><span class="pre">piece</span></tt> is
 
3413
the index of the piece it want data from, <tt class="docutils literal"><span class="pre">start</span></tt> is the offset within the piece where the data
 
3414
should be read, and <tt class="docutils literal"><span class="pre">length</span></tt> is the amount of data it wants.</p>
 
3415
</div>
 
3416
<div class="section" id="torrent-finished-alert">
 
3417
<h2>torrent_finished_alert</h2>
 
3418
<p>This alert is generated when a torrent switches from being a downloader to a seed.
 
3419
It will only be generated once per torrent. It contains a torrent_handle to the
 
3420
torrent in question. This alert is generated as severity level <tt class="docutils literal"><span class="pre">info</span></tt>.</p>
 
3421
<pre class="literal-block">
 
3422
struct torrent_finished_alert: torrent_alert
 
3423
{
 
3424
        torrent_finished_alert(
 
3425
                const torrent_handle&amp; h
 
3426
                , const std::string&amp; msg);
 
3427
 
 
3428
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3429
};
 
3430
</pre>
 
3431
</div>
 
3432
<div class="section" id="metadata-failed-alert">
 
3433
<h2>metadata_failed_alert</h2>
 
3434
<p>This alert is generated when the metadata has been completely received and the info-hash
 
3435
failed to match it. i.e. the metadata that was received was corrupt. libtorrent will
 
3436
automatically retry to fetch it in this case. This is only relevant when running a
 
3437
torrent-less download, with the metadata extension provided by libtorrent.
 
3438
It is generated at severity level <tt class="docutils literal"><span class="pre">info</span></tt>.</p>
 
3439
<pre class="literal-block">
 
3440
struct metadata_failed_alert: torrent_alert
 
3441
{
 
3442
        metadata_failed_alert(
 
3443
                torrent_handle const&amp; h
 
3444
                , std::string const&amp; msg);
 
3445
 
 
3446
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3447
};
 
3448
</pre>
 
3449
</div>
 
3450
<div class="section" id="metadata-received-alert">
 
3451
<h2>metadata_received_alert</h2>
 
3452
<p>This alert is generated when the metadata has been completely received and the torrent
 
3453
can start downloading. It is not generated on torrents that are started with metadata, but
 
3454
only those that needs to download it from peers (when utilizing the libtorrent extension).
 
3455
It is generated at severity level <tt class="docutils literal"><span class="pre">info</span></tt>.</p>
 
3456
<pre class="literal-block">
 
3457
struct metadata_received_alert: torrent_alert
 
3458
{
 
3459
        metadata_received_alert(
 
3460
                torrent_handle const_&amp; h
 
3461
                , std::string const&amp; msg);
 
3462
 
 
3463
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3464
};
 
3465
</pre>
 
3466
</div>
 
3467
<div class="section" id="fastresume-rejected-alert">
 
3468
<h2>fastresume_rejected_alert</h2>
 
3469
<p>This alert is generated when a fastresume file has been passed to <tt class="docutils literal"><span class="pre">add_torrent</span></tt> but the
 
3470
files on disk did not match the fastresume file. The string explains the reason why the
 
3471
resume file was rejected. It is generated at severity level <tt class="docutils literal"><span class="pre">warning</span></tt>.</p>
 
3472
<pre class="literal-block">
 
3473
struct fastresume_rejected_alert: torrent_alert
 
3474
{
 
3475
        fastresume_rejected_alert(torrent_handle const&amp; h
 
3476
                , std::string const&amp; msg);
 
3477
 
 
3478
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3479
};
 
3480
</pre>
 
3481
</div>
 
3482
<div class="section" id="peer-blocked-alert">
 
3483
<h2>peer_blocked_alert</h2>
 
3484
<p>This alert is generated when a peer is blocked by the IP filter. It has the severity leve
 
3485
<tt class="docutils literal"><span class="pre">info</span></tt>. The <tt class="docutils literal"><span class="pre">ip</span></tt> member is the address that was blocked.</p>
 
3486
<pre class="literal-block">
 
3487
struct peer_blocked_alert: alert
 
3488
{
 
3489
        peer_blocked_alert(address const&amp; ip_
 
3490
                , std::string const&amp; msg);
 
3491
 
 
3492
        address ip;
 
3493
 
 
3494
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3495
};
 
3496
</pre>
 
3497
</div>
 
3498
<div class="section" id="storage-moved-alert">
 
3499
<h2>storage_moved_alert</h2>
 
3500
<p>The <tt class="docutils literal"><span class="pre">storage_moved_alert</span></tt> is generated when all the disk IO has completed and the
 
3501
files have been moved, as an effect of a call to <tt class="docutils literal"><span class="pre">torrent_handle::move_storage</span></tt>. This
 
3502
is useful to synchronize with the actual disk.</p>
 
3503
<pre class="literal-block">
 
3504
struct storage_moved_alert: torrent_alert
 
3505
{
 
3506
        storage_moved_alert(torrent_handle const&amp; h, std::string const&amp; path);
 
3507
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3508
};
 
3509
</pre>
 
3510
</div>
 
3511
<div class="section" id="torrent-paused-alert">
 
3512
<h2>torrent_paused_alert</h2>
 
3513
<p>This alert is generated as a response to a <tt class="docutils literal"><span class="pre">torrent_handle::pause</span></tt> request. It is
 
3514
generated once all disk IO is complete and the files in the torrent have been closed.
 
3515
This is useful for synchronizing with the disk.</p>
 
3516
<pre class="literal-block">
 
3517
struct torrent_paused_alert: torrent_alert
 
3518
{
 
3519
        torrent_paused_alert(torrent_handle const&amp; h, std::string const&amp; msg);
 
3520
        virtual std::auto_ptr&lt;alert&gt; clone() const;
 
3521
};
 
3522
</pre>
 
3523
</div>
 
3524
<div class="section" id="dispatcher">
 
3525
<h2>dispatcher</h2>
 
3526
<p>The <tt class="docutils literal"><span class="pre">handle_alert</span></tt> class is defined in <tt class="docutils literal"><span class="pre">&lt;libtorrent/alert.hpp&gt;</span></tt>.</p>
 
3527
<p>Examples usage:</p>
 
3528
<pre class="literal-block">
 
3529
struct my_handler
 
3530
{
 
3531
        void operator()(portmap_error_alert const&amp; a)
 
3532
        {
 
3533
                std::cout &lt;&lt; &quot;Portmapper: &quot; &lt;&lt; a.msg &lt;&lt; std::endl;
 
3534
        }
 
3535
 
 
3536
        void operator()(tracker_warning_alert const&amp; a)
 
3537
        {
 
3538
                std::cout &lt;&lt; &quot;Tracker warning: &quot; &lt;&lt; a.msg &lt;&lt; std::endl;
 
3539
        }
 
3540
 
 
3541
        void operator()(torrent_finished_alert const&amp; a)
 
3542
        {
 
3543
                // write fast resume data
 
3544
                // ...
 
3545
 
 
3546
                std::cout &lt;&lt; a.handle.get_torrent_info().name() &lt;&lt; &quot;completed&quot;
 
3547
                        &lt;&lt; std::endl;
 
3548
        }
 
3549
};
 
3550
</pre>
 
3551
<pre class="literal-block">
 
3552
std::auto_ptr&lt;alert&gt; a;
 
3553
a = ses.pop_alert();
 
3554
my_handler h;
 
3555
while (a.get())
 
3556
{
 
3557
        handle_alert&lt;portmap_error_alert
 
3558
                , tracker_warning_alert
 
3559
                , torrent_finished_alert
 
3560
        &gt;::handle_alert(h, a);
 
3561
        a = ses.pop_alert();
 
3562
}
 
3563
</pre>
 
3564
<p>In this example 3 alert types are used. You can use any number of template
 
3565
parameters to select between more types. If the number of types are more than
 
3566
15, you can define <tt class="docutils literal"><span class="pre">TORRENT_MAX_ALERT_TYPES</span></tt> to a greater number before
 
3567
including <tt class="docutils literal"><span class="pre">&lt;libtorrent/alert.hpp&gt;</span></tt>.</p>
 
3568
</div>
 
3569
</div>
 
3570
<div class="section" id="exceptions">
 
3571
<h1>exceptions</h1>
 
3572
<p>There are a number of exceptions that can be thrown from different places in libtorrent,
 
3573
here's a complete list with description.</p>
 
3574
<div class="section" id="invalid-handle">
 
3575
<h2>invalid_handle</h2>
 
3576
<p>This exception is thrown when querying information from a <a class="reference internal" href="#torrent-handle">torrent_handle</a> that hasn't
 
3577
been initialized or that has become invalid.</p>
 
3578
<pre class="literal-block">
 
3579
struct invalid_handle: std::exception
 
3580
{
 
3581
        const char* what() const throw();
 
3582
};
 
3583
</pre>
 
3584
</div>
 
3585
<div class="section" id="duplicate-torrent">
 
3586
<h2>duplicate_torrent</h2>
 
3587
<p>This is thrown by <a class="reference internal" href="#add-torrent">add_torrent()</a> if the torrent already has been added to
 
3588
the session. Since <a class="reference internal" href="#remove-torrent">remove_torrent()</a> is asynchronous, this exception may
 
3589
be thrown if the torrent is removed and then immediately added again.</p>
 
3590
<pre class="literal-block">
 
3591
struct duplicate_torrent: std::exception
 
3592
{
 
3593
        const char* what() const throw();
 
3594
};
 
3595
</pre>
 
3596
</div>
 
3597
<div class="section" id="invalid-encoding">
 
3598
<h2>invalid_encoding</h2>
 
3599
<p>This is thrown by <tt class="docutils literal"><span class="pre">bdecode()</span></tt> if the input data is not a valid bencoding.</p>
 
3600
<pre class="literal-block">
 
3601
struct invalid_encoding: std::exception
 
3602
{
 
3603
        const char* what() const throw();
 
3604
};
 
3605
</pre>
 
3606
</div>
 
3607
<div class="section" id="type-error">
 
3608
<h2>type_error</h2>
 
3609
<p>This is thrown from the accessors of <tt class="docutils literal"><span class="pre">entry</span></tt> if the data type of the <tt class="docutils literal"><span class="pre">entry</span></tt> doesn't
 
3610
match the type you want to extract from it.</p>
 
3611
<pre class="literal-block">
 
3612
struct type_error: std::runtime_error
 
3613
{
 
3614
        type_error(const char* error);
 
3615
};
 
3616
</pre>
 
3617
</div>
 
3618
<div class="section" id="invalid-torrent-file">
 
3619
<h2>invalid_torrent_file</h2>
 
3620
<p>This exception is thrown from the constructor of <tt class="docutils literal"><span class="pre">torrent_info</span></tt> if the given bencoded information
 
3621
doesn't meet the requirements on what information has to be present in a torrent file.</p>
 
3622
<pre class="literal-block">
 
3623
struct invalid_torrent_file: std::exception
 
3624
{
 
3625
        const char* what() const throw();
 
3626
};
 
3627
</pre>
 
3628
</div>
 
3629
</div>
 
3630
<div class="section" id="storage-interface">
 
3631
<h1>storage_interface</h1>
 
3632
<p>The storage interface is a pure virtual class that can be implemented to
 
3633
change the behavior of the actual file storage. The interface looks like
 
3634
this:</p>
 
3635
<pre class="literal-block">
 
3636
struct storage_interface
 
3637
{
 
3638
        virtual void initialize(bool allocate_files) = 0;
 
3639
        virtual size_type read(char* buf, int slot, int offset, int size) = 0;
 
3640
        virtual void write(const char* buf, int slot, int offset, int size) = 0;
 
3641
        virtual bool move_storage(fs::path save_path) = 0;
 
3642
        virtual bool verify_resume_data(entry&amp; rd, std::string&amp; error) = 0;
 
3643
        virtual void write_resume_data(entry&amp; rd) const = 0;
 
3644
        virtual void move_slot(int src_slot, int dst_slot) = 0;
 
3645
        virtual void swap_slots(int slot1, int slot2) = 0;
 
3646
        virtual void swap_slots3(int slot1, int slot2, int slot3) = 0;
 
3647
        virtual sha1_hash hash_for_slot(int slot, partial_hash&amp; h, int piece_size) = 0;
 
3648
        virtual void release_files() = 0;
 
3649
        virtual void delete_files() = 0;
 
3650
        virtual ~storage_interface() {}
 
3651
};
 
3652
</pre>
 
3653
<div class="section" id="initialize">
 
3654
<h2>initialize()</h2>
 
3655
<blockquote>
 
3656
<pre class="literal-block">
 
3657
void initialize(bool allocate_files) = 0;
 
3658
</pre>
 
3659
</blockquote>
 
3660
<p>This function is called when the storage is to be initialized. The default storage
 
3661
will create directories and empty files at this point. If <tt class="docutils literal"><span class="pre">allocate_files</span></tt> is true,
 
3662
it will also <tt class="docutils literal"><span class="pre">ftruncate</span></tt> all files to their target size.</p>
 
3663
</div>
 
3664
<div class="section" id="read">
 
3665
<h2>read()</h2>
 
3666
<blockquote>
 
3667
<pre class="literal-block">
 
3668
size_type read(char* buf, int slot, int offset, int size) = 0;
 
3669
</pre>
 
3670
</blockquote>
 
3671
<p>This function should read the data in the given slot and at the given offset
 
3672
and <tt class="docutils literal"><span class="pre">size</span></tt> number of bytes. The data is to be copied to <tt class="docutils literal"><span class="pre">buf</span></tt>.</p>
 
3673
<p>The return value is the number of bytes actually read.</p>
 
3674
</div>
 
3675
<div class="section" id="write">
 
3676
<h2>write()</h2>
 
3677
<blockquote>
 
3678
<pre class="literal-block">
 
3679
void write(const char* buf, int slot, int offset, int size) = 0;
 
3680
</pre>
 
3681
</blockquote>
 
3682
<p>This function should write the data in <tt class="docutils literal"><span class="pre">buf</span></tt> to the given slot (<tt class="docutils literal"><span class="pre">slot</span></tt>) at offset
 
3683
<tt class="docutils literal"><span class="pre">offset</span></tt> in that slot. The buffer size is <tt class="docutils literal"><span class="pre">size</span></tt>.</p>
 
3684
</div>
 
3685
<div class="section" id="id11">
 
3686
<h2>move_storage()</h2>
 
3687
<blockquote>
 
3688
<pre class="literal-block">
 
3689
bool move_storage(fs::path save_path) = 0;
 
3690
</pre>
 
3691
</blockquote>
 
3692
<p>This function should move all the files belonging to the storage to the new save_path.
 
3693
The default storage moves the single file or the directory of the torrent.</p>
 
3694
<p>Before moving the files, any open file handles may have to be closed, like
 
3695
<tt class="docutils literal"><span class="pre">release_files()</span></tt>.</p>
 
3696
</div>
 
3697
<div class="section" id="verify-resume-data">
 
3698
<h2>verify_resume_data()</h2>
 
3699
<blockquote>
 
3700
<pre class="literal-block">
 
3701
bool verify_resume_data(entry&amp; rd, std::string&amp; error) = 0;
 
3702
</pre>
 
3703
</blockquote>
 
3704
<p>This function should verify the resume data <tt class="docutils literal"><span class="pre">rd</span></tt> with the files
 
3705
on disk. If the resume data seems to be up-to-date, return true. If
 
3706
not, set <tt class="docutils literal"><span class="pre">error</span></tt> to a description of what mismatched and return false.</p>
 
3707
<p>The default storage may compare file sizes and time stamps of the files.</p>
 
3708
</div>
 
3709
<div class="section" id="id12">
 
3710
<h2>write_resume_data( )</h2>
 
3711
<blockquote>
 
3712
<pre class="literal-block">
 
3713
void write_resume_data(entry&amp; rd) const = 0;
 
3714
</pre>
 
3715
</blockquote>
 
3716
<p>This function should fill in resume data, the current state of the
 
3717
storage, in <tt class="docutils literal"><span class="pre">rd</span></tt>. The default storage adds file timestamps and
 
3718
sizes.</p>
 
3719
</div>
 
3720
<div class="section" id="move-slot">
 
3721
<h2>move_slot()</h2>
 
3722
<blockquote>
 
3723
<pre class="literal-block">
 
3724
void move_slot(int src_slot, int dst_slot) = 0;
 
3725
</pre>
 
3726
</blockquote>
 
3727
<p>This function should copy or move the data in slot <tt class="docutils literal"><span class="pre">src_slot</span></tt> to
 
3728
the slot <tt class="docutils literal"><span class="pre">dst_slot</span></tt>. This is only used in compact mode.</p>
 
3729
<p>If the storage caches slots, this could be implemented more
 
3730
efficient than reading and writing the data.</p>
 
3731
</div>
 
3732
<div class="section" id="swap-slots">
 
3733
<h2>swap_slots()</h2>
 
3734
<blockquote>
 
3735
<pre class="literal-block">
 
3736
void swap_slots(int slot1, int slot2) = 0;
 
3737
</pre>
 
3738
</blockquote>
 
3739
<p>This function should swap the data in <tt class="docutils literal"><span class="pre">slot1</span></tt> and <tt class="docutils literal"><span class="pre">slot2</span></tt>. The default
 
3740
storage uses a scratch buffer to read the data into, then moving the other
 
3741
slot and finally writing back the temporary slot's data</p>
 
3742
<p>This is only used in compact mode.</p>
 
3743
</div>
 
3744
<div class="section" id="swap-slots3">
 
3745
<h2>swap_slots3()</h2>
 
3746
<blockquote>
 
3747
<pre class="literal-block">
 
3748
void swap_slots3(int slot1, int slot2, int slot3) = 0;
 
3749
</pre>
 
3750
</blockquote>
 
3751
<p>This function should do a 3-way swap, or shift of the slots. <tt class="docutils literal"><span class="pre">slot1</span></tt>
 
3752
should move to <tt class="docutils literal"><span class="pre">slot2</span></tt>, which should be moved to <tt class="docutils literal"><span class="pre">slot3</span></tt> which in turn
 
3753
should be moved to <tt class="docutils literal"><span class="pre">slot1</span></tt>.</p>
 
3754
<p>This is only used in compact mode.</p>
 
3755
</div>
 
3756
<div class="section" id="hash-for-slot">
 
3757
<h2>hash_for_slot()</h2>
 
3758
<blockquote>
 
3759
<pre class="literal-block">
 
3760
sha1_hash hash_for_slot(int slot, partial_hash&amp; h, int piece_size) = 0;
 
3761
</pre>
 
3762
</blockquote>
 
3763
<p>The function should read the remaining bytes of the slot and hash it with the
 
3764
sha-1 state in <tt class="docutils literal"><span class="pre">partion_hash</span></tt>. The <tt class="docutils literal"><span class="pre">partial_hash</span></tt> struct looks like this:</p>
 
3765
<pre class="literal-block">
 
3766
struct partial_hash
 
3767
{
 
3768
        partial_hash();
 
3769
        int offset;
 
3770
        hasher h;
 
3771
};
 
3772
</pre>
 
3773
<p><tt class="docutils literal"><span class="pre">offset</span></tt> is the number of bytes in the slot that has already been hashed, and
 
3774
<tt class="docutils literal"><span class="pre">h</span></tt> is the sha-1 state of that hash. <tt class="docutils literal"><span class="pre">piece_size</span></tt> is the size of the piece
 
3775
that is stored in the given slot.</p>
 
3776
<p>The function should return the hash of the piece stored in the slot.</p>
 
3777
</div>
 
3778
<div class="section" id="release-files">
 
3779
<h2>release_files()</h2>
 
3780
<blockquote>
 
3781
<pre class="literal-block">
 
3782
void release_files() = 0;
 
3783
</pre>
 
3784
</blockquote>
 
3785
<p>This function should release all the file handles that it keeps open to files
 
3786
belonging to this storage. The default implementation just calls
 
3787
<tt class="docutils literal"><span class="pre">file_pool::release_files(this)</span></tt>.</p>
 
3788
</div>
 
3789
<div class="section" id="delete-files">
 
3790
<h2>delete_files()</h2>
 
3791
<blockquote>
 
3792
<pre class="literal-block">
 
3793
void delete_files() = 0;
 
3794
</pre>
 
3795
</blockquote>
 
3796
<p>This function should delete all files and directories belonging to this storage.</p>
 
3797
</div>
 
3798
</div>
 
3799
<div class="section" id="fast-resume">
 
3800
<h1>fast resume</h1>
 
3801
<p>The fast resume mechanism is a way to remember which pieces are downloaded
 
3802
and where they are put between sessions. You can generate fast resume data by
 
3803
calling <tt class="docutils literal"><span class="pre">torrent_handle::write_resume_data()</span></tt> on <a class="reference internal" href="#torrent-handle">torrent_handle</a>. You can
 
3804
then save this data to disk and use it when resuming the torrent. libtorrent
 
3805
will not check the piece hashes then, and rely on the information given in the
 
3806
fast-resume data. The fast-resume data also contains information about which
 
3807
blocks, in the unfinished pieces, were downloaded, so it will not have to
 
3808
start from scratch on the partially downloaded pieces.</p>
 
3809
<p>To use the fast-resume data you simply give it to <a class="reference internal" href="#add-torrent">add_torrent()</a>, and it
 
3810
will skip the time consuming checks. It may have to do the checking anyway, if
 
3811
the fast-resume data is corrupt or doesn't fit the storage for that torrent,
 
3812
then it will not trust the fast-resume data and just do the checking.</p>
 
3813
<div class="section" id="file-format">
 
3814
<h2>file format</h2>
 
3815
<p>The file format is a bencoded dictionary containing the following fields:</p>
 
3816
<table border="1" class="docutils">
 
3817
<colgroup>
 
3818
<col width="26%" />
 
3819
<col width="74%" />
 
3820
</colgroup>
 
3821
<tbody valign="top">
 
3822
<tr><td><tt class="docutils literal"><span class="pre">file-format</span></tt></td>
 
3823
<td>string: &quot;libtorrent resume file&quot;</td>
 
3824
</tr>
 
3825
<tr><td><tt class="docutils literal"><span class="pre">file-version</span></tt></td>
 
3826
<td>integer: 1</td>
 
3827
</tr>
 
3828
<tr><td><tt class="docutils literal"><span class="pre">info-hash</span></tt></td>
 
3829
<td>string, the info hash of the torrent this data is saved for.</td>
 
3830
</tr>
 
3831
<tr><td><tt class="docutils literal"><span class="pre">blocks</span> <span class="pre">per</span> <span class="pre">piece</span></tt></td>
 
3832
<td>integer, the number of blocks per piece. Must be: piece_size
 
3833
/ (16 * 1024). Clamped to be within the range [1, 256]. It
 
3834
is the number of blocks per (normal sized) piece. Usually
 
3835
each block is 16 * 1024 bytes in size. But if piece size is
 
3836
greater than 4 megabytes, the block size will increase.</td>
 
3837
</tr>
 
3838
<tr><td><tt class="docutils literal"><span class="pre">slots</span></tt></td>
 
3839
<td><p class="first">list of integers. The list maps slots to piece indices. It
 
3840
tells which piece is on which slot. If piece index is -2 it
 
3841
means it is free, that there's no piece there. If it is -1,
 
3842
means the slot isn't allocated on disk yet. The pieces have
 
3843
to meet the following requirement:</p>
 
3844
<p class="last">If there's a slot at the position of the piece index,
 
3845
the piece must be located in that slot.</p>
 
3846
</td>
 
3847
</tr>
 
3848
<tr><td><tt class="docutils literal"><span class="pre">peers</span></tt></td>
 
3849
<td><p class="first">list of dictionaries. Each dictionary has the following
 
3850
layout:</p>
 
3851
<table border="1" class="docutils">
 
3852
<colgroup>
 
3853
<col width="18%" />
 
3854
<col width="82%" />
 
3855
</colgroup>
 
3856
<tbody valign="top">
 
3857
<tr><td><tt class="docutils literal"><span class="pre">ip</span></tt></td>
 
3858
<td>string, the ip address of the peer. This is
 
3859
not a binary representation of the ip
 
3860
address, but the string representation. It
 
3861
may be an IPv6 string or an IPv4 string.</td>
 
3862
</tr>
 
3863
<tr><td><tt class="docutils literal"><span class="pre">port</span></tt></td>
 
3864
<td>integer, the listen port of the peer</td>
 
3865
</tr>
 
3866
</tbody>
 
3867
</table>
 
3868
<p class="last">These are the local peers we were connected to when this
 
3869
fast-resume data was saved.</p>
 
3870
</td>
 
3871
</tr>
 
3872
<tr><td><tt class="docutils literal"><span class="pre">unfinished</span></tt></td>
 
3873
<td><p class="first">list of dictionaries. Each dictionary represents an
 
3874
piece, and has the following layout:</p>
 
3875
<table border="1" class="last docutils">
 
3876
<colgroup>
 
3877
<col width="23%" />
 
3878
<col width="77%" />
 
3879
</colgroup>
 
3880
<tbody valign="top">
 
3881
<tr><td><tt class="docutils literal"><span class="pre">piece</span></tt></td>
 
3882
<td>integer, the index of the piece this entry
 
3883
refers to.</td>
 
3884
</tr>
 
3885
<tr><td><tt class="docutils literal"><span class="pre">bitmask</span></tt></td>
 
3886
<td>string, a binary bitmask representing the
 
3887
blocks that have been downloaded in this
 
3888
piece.</td>
 
3889
</tr>
 
3890
<tr><td><tt class="docutils literal"><span class="pre">adler32</span></tt></td>
 
3891
<td>The adler32 checksum of the data in the
 
3892
blocks specified by <tt class="docutils literal"><span class="pre">bitmask</span></tt>.</td>
 
3893
</tr>
 
3894
</tbody>
 
3895
</table>
 
3896
</td>
 
3897
</tr>
 
3898
<tr><td><tt class="docutils literal"><span class="pre">file</span> <span class="pre">sizes</span></tt></td>
 
3899
<td>list where each entry corresponds to a file in the file list
 
3900
in the metadata. Each entry has a list of two values, the
 
3901
first value is the size of the file in bytes, the second
 
3902
is the time stamp when the last time someone wrote to it.
 
3903
This information is used to compare with the files on disk.
 
3904
All the files must match exactly this information in order
 
3905
to consider the resume data as current. Otherwise a full
 
3906
re-check is issued.</td>
 
3907
</tr>
 
3908
<tr><td><tt class="docutils literal"><span class="pre">allocation</span></tt></td>
 
3909
<td>The allocation mode for the storage. Can be either <tt class="docutils literal"><span class="pre">full</span></tt>
 
3910
or <tt class="docutils literal"><span class="pre">compact</span></tt>. If this is full, the file sizes and
 
3911
timestamps are disregarded. Pieces are assumed not to have
 
3912
moved around even if the files have been modified after the
 
3913
last resume data checkpoint.</td>
 
3914
</tr>
 
3915
</tbody>
 
3916
</table>
 
3917
</div>
 
3918
</div>
 
3919
<div class="section" id="threads">
 
3920
<h1>threads</h1>
 
3921
<p>libtorrent starts 2 or 3 threads.</p>
 
3922
<blockquote>
 
3923
<ul class="simple">
 
3924
<li>The first thread is the main thread that will sit
 
3925
idle in a <tt class="docutils literal"><span class="pre">select()</span></tt> call most of the time. This thread runs the main loop
 
3926
that will send and receive data on all connections.</li>
 
3927
<li>The second thread is a hash-check thread. Whenever a torrent is added it will
 
3928
first be passed to this thread for checking the files that may already have been
 
3929
downloaded. If there is any resume data this thread will make sure it is valid
 
3930
and matches the files. Once the torrent has been checked, it is passed on to the
 
3931
main thread that will start it. The hash-check thread has a queue of torrents,
 
3932
it will only check one torrent at a time.</li>
 
3933
<li>The third thread is spawned by asio on systems that don't support
 
3934
non-blocking host name resolution to simulate non-blocking behavior.</li>
 
3935
</ul>
 
3936
</blockquote>
 
3937
</div>
 
3938
<div class="section" id="storage-allocation">
 
3939
<h1>storage allocation</h1>
 
3940
<p>There are three modes in which storage (files on disk) are allocated in libtorrent.</p>
 
3941
<ol class="arabic simple">
 
3942
<li>The traditional <em>full allocation</em> mode, where the entire files are filled up with
 
3943
zeros before anything is downloaded. libtorrent will look for sparse files support
 
3944
in the filesystem that is used for storage, and use sparse files or file system
 
3945
zero fill support if present. This means that on NTFS, full allocation mode will
 
3946
only allocate storage for the downloaded pieces.</li>
 
3947
<li>The <em>compact allocation</em> mode, where only files are allocated for actual
 
3948
pieces that have been downloaded. This is the default allocation mode in libtorrent.</li>
 
3949
<li>The <em>sparce allocation</em>, sparse files are used, and pieces are downloaded directly
 
3950
to where they belong. This is the recommended (and default) mode.</li>
 
3951
</ol>
 
3952
<p>The allocation mode is selected when a torrent is started. It is passed as an
 
3953
argument to <tt class="docutils literal"><span class="pre">session::add_torrent()</span></tt> (see <a class="reference internal" href="#add-torrent">add_torrent()</a>).</p>
 
3954
<p>The decision to use full allocation or compact allocation typically depends on whether
 
3955
any files are filtered and if the filesystem supports sparse files.</p>
 
3956
<div class="section" id="sparse-allocation">
 
3957
<h2>sparse allocation</h2>
 
3958
<p>On filesystems that supports sparse files, this allocation mode will only use
 
3959
as much space as has been downloaded.</p>
 
3960
<blockquote>
 
3961
<ul class="simple">
 
3962
<li>It does not require an allocation pass on startup.</li>
 
3963
<li>It supports skipping files (setting prioirty to 0 to not download).</li>
 
3964
<li>Fast resume data will remain valid even when file time stamps are out of date.</li>
 
3965
</ul>
 
3966
</blockquote>
 
3967
</div>
 
3968
<div class="section" id="full-allocation">
 
3969
<h2>full allocation</h2>
 
3970
<p>When a torrent is started in full allocation mode, the checker thread (see <a class="reference internal" href="#threads">threads</a>)
 
3971
will make sure that the entire storage is allocated, and fill any gaps with zeros.
 
3972
This will be skipped if the filesystem supports sparse files or automatic zero filling.
 
3973
It will of course still check for existing pieces and fast resume data. The main
 
3974
drawbacks of this mode are:</p>
 
3975
<blockquote>
 
3976
<ul class="simple">
 
3977
<li>It may take longer to start the torrent, since it will need to fill the files
 
3978
with zeros on some systems. This delay is linearly dependent on the size of
 
3979
the download.</li>
 
3980
<li>The download may occupy unnecessary disk space between download sessions. In case
 
3981
sparse files are not supported.</li>
 
3982
<li>Disk caches usually perform extremely poorly with random access to large files
 
3983
and may slow down a download considerably.</li>
 
3984
</ul>
 
3985
</blockquote>
 
3986
<p>The benefits of this mode are:</p>
 
3987
<blockquote>
 
3988
<ul class="simple">
 
3989
<li>Downloaded pieces are written directly to their final place in the files and the
 
3990
total number of disk operations will be fewer and may also play nicer to
 
3991
filesystems' file allocation, and reduce fragmentation.</li>
 
3992
<li>No risk of a download failing because of a full disk during download. Unless
 
3993
sparse files are being used.</li>
 
3994
<li>The fast resume data will be more likely to be usable, regardless of crashes or
 
3995
out of date data, since pieces won't move around.</li>
 
3996
<li>Can be used with the filter files feature.</li>
 
3997
</ul>
 
3998
</blockquote>
 
3999
</div>
 
4000
<div class="section" id="compact-allocation">
 
4001
<h2>compact allocation</h2>
 
4002
<p>The compact allocation will only allocate as much storage as it needs to keep the
 
4003
pieces downloaded so far. This means that pieces will be moved around to be placed
 
4004
at their final position in the files while downloading (to make sure the completed
 
4005
download has all its pieces in the correct place). So, the main drawbacks are:</p>
 
4006
<blockquote>
 
4007
<ul class="simple">
 
4008
<li>More disk operations while downloading since pieces are moved around.</li>
 
4009
<li>Potentially more fragmentation in the filesystem.</li>
 
4010
<li>Cannot be used while filtering files.</li>
 
4011
</ul>
 
4012
</blockquote>
 
4013
<p>The benefits though, are:</p>
 
4014
<blockquote>
 
4015
<ul class="simple">
 
4016
<li>No startup delay, since the files doesn't need allocating.</li>
 
4017
<li>The download will not use unnecessary disk space.</li>
 
4018
<li>Disk caches perform much better than in full allocation and raises the download
 
4019
speed limit imposed by the disk.</li>
 
4020
<li>Works well on filesystems that doesn't support sparse files.</li>
 
4021
</ul>
 
4022
</blockquote>
 
4023
<p>The algorithm that is used when allocating pieces and slots isn't very complicated.
 
4024
For the interested, a description follows.</p>
 
4025
<p>storing a piece:</p>
 
4026
<ol class="arabic simple">
 
4027
<li>let <strong>A</strong> be a newly downloaded piece, with index <strong>n</strong>.</li>
 
4028
<li>let <strong>s</strong> be the number of slots allocated in the file we're
 
4029
downloading to. (the number of pieces it has room for).</li>
 
4030
<li>if <strong>n</strong> &gt;= <strong>s</strong> then allocate a new slot and put the piece there.</li>
 
4031
<li>if <strong>n</strong> &lt; <strong>s</strong> then allocate a new slot, move the data at
 
4032
slot <strong>n</strong> to the new slot and put <strong>A</strong> in slot <strong>n</strong>.</li>
 
4033
</ol>
 
4034
<p>allocating a new slot:</p>
 
4035
<ol class="arabic simple">
 
4036
<li>if there's an unassigned slot (a slot that doesn't
 
4037
contain any piece), return that slot index.</li>
 
4038
<li>append the new slot at the end of the file (or find an unused slot).</li>
 
4039
<li>let <strong>i</strong> be the index of newly allocated slot</li>
 
4040
<li>if we have downloaded piece index <strong>i</strong> already (to slot <strong>j</strong>) then<ol class="arabic">
 
4041
<li>move the data at slot <strong>j</strong> to slot <strong>i</strong>.</li>
 
4042
<li>return slot index <strong>j</strong> as the newly allocated free slot.</li>
 
4043
</ol>
 
4044
</li>
 
4045
<li>return <strong>i</strong> as the newly allocated slot.</li>
 
4046
</ol>
 
4047
</div>
 
4048
</div>
 
4049
<div class="section" id="extensions">
 
4050
<h1>extensions</h1>
 
4051
<p>These extensions all operates within the <a class="reference external" href="extension_protocol.html">extension protocol</a>. The
 
4052
name of the extension is the name used in the extension-list packets,
 
4053
and the payload is the data in the extended message (not counting the
 
4054
length-prefix, message-id nor extension-id).</p>
 
4055
<p>Note that since this protocol relies on one of the reserved bits in the
 
4056
handshake, it may be incompatible with future versions of the mainline
 
4057
bittorrent client.</p>
 
4058
<p>These are the extensions that are currently implemented.</p>
 
4059
<div class="section" id="metadata-from-peers">
 
4060
<h2>metadata from peers</h2>
 
4061
<p>Extension name: &quot;LT_metadata&quot;</p>
 
4062
<p>The point with this extension is that you don't have to distribute the
 
4063
metadata (.torrent-file) separately. The metadata can be distributed
 
4064
through the bittorrent swarm. The only thing you need to download such
 
4065
a torrent is the tracker url and the info-hash of the torrent.</p>
 
4066
<p>It works by assuming that the initial seeder has the metadata and that
 
4067
the metadata will propagate through the network as more peers join.</p>
 
4068
<p>There are three kinds of messages in the metadata extension. These packets
 
4069
are put as payload to the extension message. The three packets are:</p>
 
4070
<blockquote>
 
4071
<ul class="simple">
 
4072
<li>request metadata</li>
 
4073
<li>metadata</li>
 
4074
<li>don't have metadata</li>
 
4075
</ul>
 
4076
</blockquote>
 
4077
<p>request metadata:</p>
 
4078
<table border="1" class="docutils">
 
4079
<colgroup>
 
4080
<col width="17%" />
 
4081
<col width="23%" />
 
4082
<col width="61%" />
 
4083
</colgroup>
 
4084
<thead valign="bottom">
 
4085
<tr><th class="head">size</th>
 
4086
<th class="head">name</th>
 
4087
<th class="head">description</th>
 
4088
</tr>
 
4089
</thead>
 
4090
<tbody valign="top">
 
4091
<tr><td>uint8_t</td>
 
4092
<td>msg_type</td>
 
4093
<td>Determines the kind of message this is
 
4094
0 means 'request metadata'</td>
 
4095
</tr>
 
4096
<tr><td>uint8_t</td>
 
4097
<td>start</td>
 
4098
<td>The start of the metadata block that
 
4099
is requested. It is given in 256:ths
 
4100
of the total size of the metadata,
 
4101
since the requesting client don't know
 
4102
the size of the metadata.</td>
 
4103
</tr>
 
4104
<tr><td>uint8_t</td>
 
4105
<td>size</td>
 
4106
<td>The size of the metadata block that is
 
4107
requested. This is also given in
 
4108
256:ths of the total size of the
 
4109
metadata. The size is given as size-1.
 
4110
That means that if this field is set
 
4111
0, the request wants one 256:th of the
 
4112
metadata.</td>
 
4113
</tr>
 
4114
</tbody>
 
4115
</table>
 
4116
<p>metadata:</p>
 
4117
<table border="1" class="docutils">
 
4118
<colgroup>
 
4119
<col width="17%" />
 
4120
<col width="23%" />
 
4121
<col width="61%" />
 
4122
</colgroup>
 
4123
<thead valign="bottom">
 
4124
<tr><th class="head">size</th>
 
4125
<th class="head">name</th>
 
4126
<th class="head">description</th>
 
4127
</tr>
 
4128
</thead>
 
4129
<tbody valign="top">
 
4130
<tr><td>uint8_t</td>
 
4131
<td>msg_type</td>
 
4132
<td>1 means 'metadata'</td>
 
4133
</tr>
 
4134
<tr><td>int32_t</td>
 
4135
<td>total_size</td>
 
4136
<td>The total size of the metadata, given
 
4137
in number of bytes.</td>
 
4138
</tr>
 
4139
<tr><td>int32_t</td>
 
4140
<td>offset</td>
 
4141
<td>The offset of where the metadata block
 
4142
in this message belongs in the final
 
4143
metadata. This is given in bytes.</td>
 
4144
</tr>
 
4145
<tr><td>uint8_t[]</td>
 
4146
<td>metadata</td>
 
4147
<td>The actual metadata block. The size of
 
4148
this part is given implicit by the
 
4149
length prefix in the bittorrent
 
4150
protocol packet.</td>
 
4151
</tr>
 
4152
</tbody>
 
4153
</table>
 
4154
<p>Don't have metadata:</p>
 
4155
<table border="1" class="docutils">
 
4156
<colgroup>
 
4157
<col width="17%" />
 
4158
<col width="23%" />
 
4159
<col width="61%" />
 
4160
</colgroup>
 
4161
<thead valign="bottom">
 
4162
<tr><th class="head">size</th>
 
4163
<th class="head">name</th>
 
4164
<th class="head">description</th>
 
4165
</tr>
 
4166
</thead>
 
4167
<tbody valign="top">
 
4168
<tr><td>uint8_t</td>
 
4169
<td>msg_type</td>
 
4170
<td>2 means 'I don't have metadata'.
 
4171
This message is sent as a reply to a
 
4172
metadata request if the the client
 
4173
doesn't have any metadata.</td>
 
4174
</tr>
 
4175
</tbody>
 
4176
</table>
 
4177
</div>
 
4178
<div class="section" id="http-seeding">
 
4179
<h2>HTTP seeding</h2>
 
4180
<p>The HTTP seed extension implements <a class="reference external" href="http://www.getright.com/seedtorrent.html">this specification</a>.</p>
 
4181
<p>The libtorrent implementation assumes that, if the URL ends with a slash
 
4182
('/'), the filename should be appended to it in order to request pieces from
 
4183
that file. The way this works is that if the torrent is a single-file torrent,
 
4184
only that filename is appended. If the torrent is a multi-file torrent, the
 
4185
torrent's name '/' the file name is appended. This is the same directory
 
4186
structure that libtorrent will download torrents into.</p>
 
4187
</div>
 
4188
</div>
 
4189
<div class="section" id="filename-checks">
 
4190
<h1>filename checks</h1>
 
4191
<p>Boost.Filesystem will by default check all its paths to make sure they conform
 
4192
to filename requirements on many platforms. If you don't want this check, you can
 
4193
set it to either only check for native filesystem requirements or turn it off
 
4194
altogether. You can use:</p>
 
4195
<pre class="literal-block">
 
4196
boost::filesystem::path::default_name_check(boost::filesystem::native);
 
4197
</pre>
 
4198
<p>for example. For more information, see the <a class="reference external" href="http://www.boost.org/libs/filesystem/doc/index.htm">Boost.Filesystem docs</a>.</p>
 
4199
</div>
 
4200
<div class="section" id="acknowledgments">
 
4201
<h1>acknowledgments</h1>
 
4202
<p>Written by Arvid Norberg. Copyright © 2003-2006</p>
 
4203
<p>Contributions by Magnus Jonsson, Daniel Wallin and Cory Nelson</p>
 
4204
<p>Lots of testing, suggestions and contributions by Massaroddel and Tianhao Qiu.</p>
 
4205
<p>Big thanks to Michael Wojciechowski and Peter Koeleman for making the autotools
 
4206
scripts.</p>
 
4207
<p>Thanks to Reimond Retz for bugfixes, suggestions and testing</p>
 
4208
<p>Thanks to <a class="reference external" href="http://www.cs.umu.se">University of Umeå</a> for providing development and test hardware.</p>
 
4209
<p>Project is hosted by sourceforge.</p>
 
4210
<p><a class="reference external" href="http://sourceforge.net"><img alt="sf_logo" src="http://sourceforge.net/sflogo.php?group_id=7994" /></a></p>
 
4211
</div>
 
4212
</div>
 
4213
</body>
 
4214
</html>