3 Copyright (c) 2006, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_SESSION_HPP_INCLUDED
34 #define TORRENT_SESSION_HPP_INCLUDED
44 #pragma warning(push, 1)
47 #include <boost/limits.hpp>
48 #include <boost/tuple/tuple.hpp>
49 #include <boost/filesystem/path.hpp>
50 #include <boost/thread.hpp>
56 #include "libtorrent/config.hpp"
57 #include "libtorrent/torrent_handle.hpp"
58 #include "libtorrent/entry.hpp"
59 #include "libtorrent/alert.hpp"
60 #include "libtorrent/session_status.hpp"
61 #include "libtorrent/version.hpp"
62 #include "libtorrent/fingerprint.hpp"
63 #include "libtorrent/time.hpp"
64 #include "libtorrent/disk_io_thread.hpp"
65 #include "libtorrent/peer_id.hpp"
67 #include "libtorrent/storage.hpp"
75 struct torrent_plugin
;
79 class connection_queue
;
83 namespace fs
= boost::filesystem
;
87 // workaround for microsofts
88 // hardware exceptions that makes
89 // it hard to debug stuff
95 ::_set_se_translator(straight_to_debugger
);
98 static void straight_to_debugger(unsigned int, _EXCEPTION_POINTERS
*)
102 struct eh_initializer
{};
106 struct filesystem_init
113 class TORRENT_EXPORT session_proxy
115 friend class session
;
119 session_proxy(boost::shared_ptr
<aux::session_impl
> impl
)
121 boost::shared_ptr
<aux::session_impl
> m_impl
;
124 struct add_torrent_params
126 add_torrent_params(storage_constructor_type sc
= default_storage_constructor
)
130 , storage_mode(storage_mode_sparse
)
133 , duplicate_is_error(false)
138 boost::intrusive_ptr
<torrent_info
> ti
;
139 char const* tracker_url
;
143 std::vector
<char>* resume_data
;
144 storage_mode_t storage_mode
;
147 bool duplicate_is_error
;
148 storage_constructor_type storage
;
152 class TORRENT_EXPORT session
: public boost::noncopyable
, aux::eh_initializer
156 session(fingerprint
const& print
= fingerprint("LT"
157 , LIBTORRENT_VERSION_MAJOR
, LIBTORRENT_VERSION_MINOR
, 0, 0)
158 #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
159 , fs::path logpath
= "."
163 fingerprint
const& print
164 , std::pair
<int, int> listen_port_range
165 , char const* listen_interface
= "0.0.0.0"
166 #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
167 , fs::path logpath
= "."
173 // returns a list of all torrents in this session
174 std::vector
<torrent_handle
> get_torrents() const;
176 // returns an invalid handle in case the torrent doesn't exist
177 torrent_handle
find_torrent(sha1_hash
const& info_hash
) const;
179 // all torrent_handles must be destructed before the session is destructed!
180 torrent_handle
add_torrent(add_torrent_params
const& params
);
182 #ifndef TORRENT_NO_DEPRECATE
183 // deprecated in 0.14
184 torrent_handle
add_torrent(
185 torrent_info
const& ti
186 , fs::path
const& save_path
187 , entry
const& resume_data
= entry()
188 , storage_mode_t storage_mode
= storage_mode_sparse
189 , bool paused
= false
190 , storage_constructor_type sc
= default_storage_constructor
) TORRENT_DEPRECATED
;
192 // deprecated in 0.14
193 torrent_handle
add_torrent(
194 boost::intrusive_ptr
<torrent_info
> ti
195 , fs::path
const& save_path
196 , entry
const& resume_data
= entry()
197 , storage_mode_t storage_mode
= storage_mode_sparse
198 , bool paused
= false
199 , storage_constructor_type sc
= default_storage_constructor
200 , void* userdata
= 0) TORRENT_DEPRECATED
;
202 // deprecated in 0.14
203 torrent_handle
add_torrent(
204 char const* tracker_url
205 , sha1_hash
const& info_hash
207 , fs::path
const& save_path
208 , entry
const& resume_data
= entry()
209 , storage_mode_t storage_mode
= storage_mode_sparse
210 , bool paused
= false
211 , storage_constructor_type sc
= default_storage_constructor
212 , void* userdata
= 0) TORRENT_DEPRECATED
;
215 session_proxy
abort() { return session_proxy(m_impl
); }
219 bool is_paused() const;
221 session_status
status() const;
222 cache_status
get_cache_status() const;
224 void get_cache_info(sha1_hash
const& ih
225 , std::vector
<cached_piece_info
>& ret
) const;
227 #ifndef TORRENT_DISABLE_DHT
228 void start_dht(entry
const& startup_state
= entry());
230 void set_dht_settings(dht_settings
const& settings
);
231 entry
dht_state() const;
232 void add_dht_node(std::pair
<std::string
, int> const& node
);
233 void add_dht_router(std::pair
<std::string
, int> const& node
);
236 #ifndef TORRENT_DISABLE_ENCRYPTION
237 void set_pe_settings(pe_settings
const& settings
);
238 pe_settings
const& get_pe_settings() const;
241 #ifndef TORRENT_DISABLE_EXTENSIONS
242 void add_extension(boost::function
<boost::shared_ptr
<torrent_plugin
>(torrent
*, void*)> ext
);
245 #ifndef TORRENT_DISABLE_GEO_IP
246 int as_for_ip(address
const& addr
);
247 bool load_asnum_db(char const* file
);
248 bool load_country_db(char const* file
);
251 void load_state(entry
const& ses_state
);
254 void set_ip_filter(ip_filter
const& f
);
255 void set_port_filter(port_filter
const& f
);
256 void set_peer_id(peer_id
const& pid
);
257 void set_key(int key
);
260 bool is_listening() const;
262 // if the listen port failed in some way
263 // you can retry to listen on another port-
264 // range with this function. If the listener
265 // succeeded and is currently listening,
266 // a call to this function will shut down the
267 // listen port and reopen it using these new
268 // properties (the given interface and port range).
269 // As usual, if the interface is left as 0
270 // this function will return false on failure.
271 // If it fails, it will also generate alerts describing
272 // the error. It will return true on success.
274 std::pair
<int, int> const& port_range
275 , const char* net_interface
= 0);
277 // returns the port we ended up listening on
278 unsigned short listen_port() const;
280 // Get the number of uploads.
281 int num_uploads() const;
283 // Get the number of connections. This number also contains the
284 // number of half open connections.
285 int num_connections() const;
293 void remove_torrent(const torrent_handle
& h
, int options
= none
);
295 void set_settings(session_settings
const& s
);
296 session_settings
const& settings();
298 void set_peer_proxy(proxy_settings
const& s
);
299 void set_web_seed_proxy(proxy_settings
const& s
);
300 void set_tracker_proxy(proxy_settings
const& s
);
302 proxy_settings
const& peer_proxy() const;
303 proxy_settings
const& web_seed_proxy() const;
304 proxy_settings
const& tracker_proxy() const;
306 #ifndef TORRENT_DISABLE_DHT
307 void set_dht_proxy(proxy_settings
const& s
);
308 proxy_settings
const& dht_proxy() const;
311 int upload_rate_limit() const;
312 int download_rate_limit() const;
313 int max_half_open_connections() const;
315 void set_upload_rate_limit(int bytes_per_second
);
316 void set_download_rate_limit(int bytes_per_second
);
317 void set_max_uploads(int limit
);
318 void set_max_connections(int limit
);
319 void set_max_half_open_connections(int limit
);
321 std::auto_ptr
<alert
> pop_alert();
322 #ifndef TORRENT_NO_DEPRECATE
323 void set_severity_level(alert::severity_t s
) TORRENT_DEPRECATED
;
325 void set_alert_mask(int m
);
327 alert
const* wait_for_alert(time_duration max_wait
);
329 connection_queue
& get_connection_queue();
331 // starts/stops UPnP, NATPMP or LSD port mappers
332 // they are stopped by default
334 natpmp
* start_natpmp();
343 // just a way to initialize boost.filesystem
344 // before the session_impl is created
345 aux::filesystem_init m_dummy
;
347 // data shared between the main thread
348 // and the working thread
349 boost::shared_ptr
<aux::session_impl
> m_impl
;
354 #endif // TORRENT_SESSION_HPP_INCLUDED