/src/libtorrent/include/libtorrent/session.hpp
Line | Count | Source |
1 | | /* |
2 | | |
3 | | Copyright (c) 2003-2004, 2006-2007, 2009-2010, 2013-2020, Arvid Norberg |
4 | | Copyright (c) 2015, Steven Siloti |
5 | | Copyright (c) 2016, Alden Torres |
6 | | All rights reserved. |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | |
12 | | * Redistributions of source code must retain the above copyright |
13 | | notice, this list of conditions and the following disclaimer. |
14 | | * Redistributions in binary form must reproduce the above copyright |
15 | | notice, this list of conditions and the following disclaimer in |
16 | | the documentation and/or other materials provided with the distribution. |
17 | | * Neither the name of the author nor the names of its |
18 | | contributors may be used to endorse or promote products derived |
19 | | from this software without specific prior written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
22 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
25 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
26 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
27 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
29 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
30 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 | | POSSIBILITY OF SUCH DAMAGE. |
32 | | |
33 | | */ |
34 | | |
35 | | #ifndef TORRENT_SESSION_HPP_INCLUDED |
36 | | #define TORRENT_SESSION_HPP_INCLUDED |
37 | | |
38 | | #include <thread> |
39 | | |
40 | | #include "libtorrent/config.hpp" |
41 | | #include "libtorrent/io_context.hpp" |
42 | | #include "libtorrent/settings_pack.hpp" |
43 | | #include "libtorrent/session_handle.hpp" |
44 | | #include "libtorrent/kademlia/dht_storage.hpp" |
45 | | #include "libtorrent/session_params.hpp" |
46 | | #include "libtorrent/session_types.hpp" // for session_flags_t |
47 | | |
48 | | #if TORRENT_ABI_VERSION == 1 |
49 | | #include "libtorrent/fingerprint.hpp" |
50 | | #include <cstdio> // for snprintf |
51 | | #endif |
52 | | |
53 | | namespace libtorrent { |
54 | | |
55 | | TORRENT_VERSION_NAMESPACE_3 |
56 | | struct plugin; |
57 | | struct session_params; |
58 | | TORRENT_VERSION_NAMESPACE_3_END |
59 | | |
60 | | // The default values of the session settings are set for a regular |
61 | | // bittorrent client running on a desktop system. There are functions that |
62 | | // can set the session settings to pre set settings for other environments. |
63 | | // These can be used for the basis, and should be tweaked to fit your needs |
64 | | // better. |
65 | | // |
66 | | // ``min_memory_usage`` returns settings that will use the minimal amount of |
67 | | // RAM, at the potential expense of upload and download performance. It |
68 | | // adjusts the socket buffer sizes, disables the disk cache, lowers the send |
69 | | // buffer watermarks so that each connection only has at most one block in |
70 | | // use at any one time. It lowers the outstanding blocks send to the disk |
71 | | // I/O thread so that connections only have one block waiting to be flushed |
72 | | // to disk at any given time. It lowers the max number of peers in the peer |
73 | | // list for torrents. It performs multiple smaller reads when it hashes |
74 | | // pieces, instead of reading it all into memory before hashing. |
75 | | // |
76 | | // This configuration is intended to be the starting point for embedded |
77 | | // devices. It will significantly reduce memory usage. |
78 | | // |
79 | | // ``high_performance_seed`` returns settings optimized for a seed box, |
80 | | // serving many peers and that doesn't do any downloading. It has a 128 MB |
81 | | // disk cache and has a limit of 400 files in its file pool. It support fast |
82 | | // upload rates by allowing large send buffers. |
83 | | TORRENT_EXPORT settings_pack min_memory_usage(); |
84 | | TORRENT_EXPORT settings_pack high_performance_seed(); |
85 | | #if TORRENT_ABI_VERSION == 1 |
86 | | TORRENT_DEPRECATED |
87 | | inline void min_memory_usage(settings_pack& set) |
88 | 0 | { set = min_memory_usage(); } |
89 | | TORRENT_DEPRECATED |
90 | | inline void high_performance_seed(settings_pack& set) |
91 | 0 | { set = high_performance_seed(); } |
92 | | #endif |
93 | | |
94 | | namespace aux { |
95 | | |
96 | | struct session_impl; |
97 | | } |
98 | | |
99 | | struct disk_interface; |
100 | | struct counters; |
101 | | struct settings_interface; |
102 | | |
103 | | // the constructor function for the default storage. On systems that support |
104 | | // memory mapped files (and a 64 bit address space) the memory mapped storage |
105 | | // will be constructed, otherwise the portable posix storage. |
106 | | TORRENT_EXPORT std::unique_ptr<disk_interface> default_disk_io_constructor( |
107 | | io_context& ios, settings_interface const&, counters& cnt); |
108 | | |
109 | | // this is a holder for the internal session implementation object. Once the |
110 | | // session destruction is explicitly initiated, this holder is used to |
111 | | // synchronize the completion of the shutdown. The lifetime of this object |
112 | | // may outlive session, causing the session destructor to not block. The |
113 | | // session_proxy destructor will block however, until the underlying session |
114 | | // is done shutting down. |
115 | | struct TORRENT_EXPORT session_proxy |
116 | | { |
117 | | friend struct session; |
118 | | // default constructor, does not refer to any session |
119 | | // implementation object. |
120 | | session_proxy(); |
121 | | ~session_proxy(); |
122 | | session_proxy(session_proxy const&); |
123 | | session_proxy& operator=(session_proxy const&) &; |
124 | | session_proxy(session_proxy&&) noexcept; |
125 | | session_proxy& operator=(session_proxy&&) & noexcept; |
126 | | private: |
127 | | session_proxy( |
128 | | std::shared_ptr<io_context> ios |
129 | | , std::shared_ptr<std::thread> t |
130 | | , std::shared_ptr<aux::session_impl> impl); |
131 | | |
132 | | std::shared_ptr<io_context> m_io_service; |
133 | | std::shared_ptr<std::thread> m_thread; |
134 | | std::shared_ptr<aux::session_impl> m_impl; |
135 | | }; |
136 | | |
137 | | // The session holds all state that spans multiple torrents. Among other |
138 | | // things it runs the network loop and manages all torrents. Once it's |
139 | | // created, the session object will spawn the main thread that will do all |
140 | | // the work. The main thread will be idle as long it doesn't have any |
141 | | // torrents to participate in. |
142 | | // |
143 | | // You have some control over session configuration through the |
144 | | // ``session_handle::apply_settings()`` member function. To change one or more |
145 | | // configuration options, create a settings_pack. object and fill it with |
146 | | // the settings to be set and pass it in to ``session::apply_settings()``. |
147 | | // |
148 | | // see apply_settings(). |
149 | | struct TORRENT_EXPORT session : session_handle |
150 | | { |
151 | | // Constructs the session objects which acts as the container of torrents. |
152 | | // In order to avoid a race condition between starting the session and |
153 | | // configuring it, you can pass in a session_params object. Its settings |
154 | | // will take effect before the session starts up. |
155 | | // |
156 | | // The overloads taking ``flags`` can be used to start a session in |
157 | | // paused mode (by passing in ``session::paused``). Note that |
158 | | // ``add_default_plugins`` do not have an affect on constructors that |
159 | | // take a session_params object. It already contains the plugins to use. |
160 | | explicit session(session_params const& params); |
161 | | explicit session(session_params&& params); |
162 | | session(session_params const& params, session_flags_t flags); |
163 | | session(session_params&& params, session_flags_t flags); |
164 | | session(); |
165 | | |
166 | | // Overload of the constructor that takes an external io_context to run |
167 | | // the session object on. This is primarily useful for tests that may want |
168 | | // to run multiple sessions on a single io_context, or low resource |
169 | | // systems where additional threads are expensive and sharing an |
170 | | // io_context with other events is fine. |
171 | | // |
172 | | // .. warning:: |
173 | | // The session object does not cleanly terminate with an external |
174 | | // ``io_context``. The ``io_context::run()`` call *must* have returned |
175 | | // before it's safe to destruct the session. Which means you *MUST* |
176 | | // call session::abort() and save the session_proxy first, then |
177 | | // destruct the session object, then sync with the io_context, then |
178 | | // destruct the session_proxy object. |
179 | | session(session_params&& params, io_context& ios); |
180 | | session(session_params const& params, io_context& ios); |
181 | | session(session_params&& params, io_context& ios, session_flags_t); |
182 | | session(session_params const& params, io_context& ios, session_flags_t); |
183 | | |
184 | | // hidden |
185 | | session(session&&); |
186 | | session& operator=(session&&) &; |
187 | | |
188 | | // hidden |
189 | | session(session const&) = delete; |
190 | | session& operator=(session const&) = delete; |
191 | | |
192 | | #if TORRENT_ABI_VERSION <= 2 |
193 | | #include "libtorrent/aux_/disable_deprecation_warnings_push.hpp" |
194 | | |
195 | | // Constructs the session objects which acts as the container of torrents. |
196 | | // It provides configuration options across torrents (such as rate limits, |
197 | | // disk cache, ip filter etc.). In order to avoid a race condition between |
198 | | // starting the session and configuring it, you can pass in a |
199 | | // settings_pack object. Its settings will take effect before the session |
200 | | // starts up. |
201 | | // |
202 | | // The ``flags`` parameter can be used to start default features (UPnP & |
203 | | // NAT-PMP) and default plugins (ut_metadata, ut_pex and smart_ban). The |
204 | | // default is to start those features. If you do not want them to start, |
205 | | // pass 0 as the flags parameter. |
206 | | TORRENT_DEPRECATED |
207 | | session(settings_pack&& pack, session_flags_t const flags); |
208 | | TORRENT_DEPRECATED |
209 | | session(settings_pack const& pack, session_flags_t const flags); |
210 | 0 | explicit session(settings_pack&& pack) : session(std::move(pack), add_default_plugins) {} |
211 | 1 | explicit session(settings_pack const& pack) : session(pack, add_default_plugins) {} |
212 | | |
213 | | // overload of the constructor that takes an external io_context to run |
214 | | // the session object on. This is primarily useful for tests that may want |
215 | | // to run multiple sessions on a single io_context, or low resource |
216 | | // systems where additional threads are expensive and sharing an |
217 | | // io_context with other events is fine. |
218 | | // |
219 | | // .. warning:: |
220 | | // The session object does not cleanly terminate with an external |
221 | | // ``io_context``. The ``io_context::run()`` call _must_ have returned |
222 | | // before it's safe to destruct the session. Which means you *MUST* |
223 | | // call session::abort() and save the session_proxy first, then |
224 | | // destruct the session object, then sync with the io_context, then |
225 | | // destruct the session_proxy object. |
226 | | TORRENT_DEPRECATED |
227 | | session(settings_pack&&, io_context&, session_flags_t); |
228 | | TORRENT_DEPRECATED |
229 | | session(settings_pack const&, io_context&, session_flags_t); |
230 | 0 | session(settings_pack&& pack, io_context& ios) : session(std::move(pack), ios, add_default_plugins) {} |
231 | 0 | session(settings_pack const& pack, io_context& ios) : session(pack, ios, add_default_plugins) {} |
232 | | |
233 | | #include "libtorrent/aux_/disable_warnings_pop.hpp" |
234 | | #endif // TORRENT_ABI_VERSION |
235 | | |
236 | | #if TORRENT_ABI_VERSION == 1 |
237 | | #include "libtorrent/aux_/disable_deprecation_warnings_push.hpp" |
238 | | |
239 | | TORRENT_DEPRECATED |
240 | | session(fingerprint const& print |
241 | | , session_flags_t const flags = start_default_features | add_default_plugins |
242 | | , alert_category_t const alert_mask = alert_category::error); |
243 | | |
244 | | TORRENT_DEPRECATED |
245 | | session(fingerprint const& print |
246 | | , std::pair<int, int> listen_port_range |
247 | | , char const* listen_interface = "0.0.0.0" |
248 | | , session_flags_t const flags = start_default_features | add_default_plugins |
249 | | , alert_category_t const alert_mask = alert_category::error); |
250 | | |
251 | | #include "libtorrent/aux_/disable_warnings_pop.hpp" |
252 | | #endif // TORRENT_ABI_VERSION |
253 | | |
254 | | // The destructor of session will notify all trackers that our torrents |
255 | | // have been shut down. If some trackers are down, they will time out. |
256 | | // All this before the destructor of session returns. So, it's advised |
257 | | // that any kind of interface (such as windows) are closed before |
258 | | // destructing the session object. Because it can take a few second for |
259 | | // it to finish. The timeout can be set with apply_settings(). |
260 | | ~session(); |
261 | | |
262 | | // In case you want to destruct the session asynchronously, you can |
263 | | // request a session destruction proxy. If you don't do this, the |
264 | | // destructor of the session object will block while the trackers are |
265 | | // contacted. If you keep one ``session_proxy`` to the session when |
266 | | // destructing it, the destructor will not block, but start to close down |
267 | | // the session, the destructor of the proxy will then synchronize the |
268 | | // threads. So, the destruction of the session is performed from the |
269 | | // ``session`` destructor call until the ``session_proxy`` destructor |
270 | | // call. The ``session_proxy`` does not have any operations on it (since |
271 | | // the session is being closed down, no operations are allowed on it). |
272 | | // The only valid operation is calling the destructor:: |
273 | | // |
274 | | // struct session_proxy {}; |
275 | | session_proxy abort(); |
276 | | |
277 | | private: |
278 | | |
279 | | void start(session_flags_t, session_params&& params, io_context* ios); |
280 | | |
281 | | #if TORRENT_ABI_VERSION <= 2 |
282 | | void start(session_flags_t flags, settings_pack&& sp, io_context* ios); |
283 | | #endif |
284 | | |
285 | | void start(session_params const& params, io_context* ios) = delete; |
286 | | #if TORRENT_ABI_VERSION <= 2 |
287 | | void start(session_flags_t flags, settings_pack const& sp, io_context* ios) = delete; |
288 | | #endif |
289 | | |
290 | | // data shared between the main thread |
291 | | // and the working thread |
292 | | std::shared_ptr<io_context> m_io_service; |
293 | | std::shared_ptr<std::thread> m_thread; |
294 | | std::shared_ptr<aux::session_impl> m_impl; |
295 | | }; |
296 | | |
297 | | } |
298 | | |
299 | | #endif // TORRENT_SESSION_HPP_INCLUDED |