Coverage Report

Created: 2026-04-28 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
You may use, distribute and modify this code under the terms of the BSD license,
9
see LICENSE file.
10
*/
11
12
#ifndef TORRENT_SESSION_HPP_INCLUDED
13
#define TORRENT_SESSION_HPP_INCLUDED
14
15
#include <thread>
16
17
#include "libtorrent/config.hpp"
18
#include "libtorrent/io_context.hpp"
19
#include "libtorrent/settings_pack.hpp"
20
#include "libtorrent/session_handle.hpp"
21
#include "libtorrent/kademlia/dht_storage.hpp"
22
#include "libtorrent/session_params.hpp"
23
#include "libtorrent/session_types.hpp" // for session_flags_t
24
25
#if TORRENT_ABI_VERSION == 1
26
#include "libtorrent/fingerprint.hpp"
27
#include <cstdio> // for snprintf
28
#endif
29
30
namespace libtorrent {
31
32
TORRENT_VERSION_NAMESPACE_3
33
  struct plugin;
34
  struct session_params;
35
TORRENT_VERSION_NAMESPACE_3_END
36
37
  // The default values of the session settings are set for a regular
38
  // bittorrent client running on a desktop system. There are functions that
39
  // can set the session settings to pre set settings for other environments.
40
  // These can be used for the basis, and should be tweaked to fit your needs
41
  // better.
42
  //
43
  // ``min_memory_usage`` returns settings that will use the minimal amount of
44
  // RAM, at the potential expense of upload and download performance. It
45
  // adjusts the socket buffer sizes, disables the disk cache, lowers the send
46
  // buffer watermarks so that each connection only has at most one block in
47
  // use at any one time. It lowers the outstanding blocks send to the disk
48
  // I/O thread so that connections only have one block waiting to be flushed
49
  // to disk at any given time. It lowers the max number of peers in the peer
50
  // list for torrents. It performs multiple smaller reads when it hashes
51
  // pieces, instead of reading it all into memory before hashing.
52
  //
53
  // This configuration is intended to be the starting point for embedded
54
  // devices. It will significantly reduce memory usage.
55
  //
56
  // ``high_performance_seed`` returns settings optimized for a seed box,
57
  // serving many peers and that doesn't do any downloading. It has a 128 MB
58
  // disk cache and has a limit of 400 files in its file pool. It support fast
59
  // upload rates by allowing large send buffers.
60
  TORRENT_EXPORT settings_pack min_memory_usage();
61
  TORRENT_EXPORT settings_pack high_performance_seed();
62
#if TORRENT_ABI_VERSION == 1
63
  TORRENT_DEPRECATED
64
  inline void min_memory_usage(settings_pack& set)
65
  { set = min_memory_usage(); }
66
  TORRENT_DEPRECATED
67
  inline void high_performance_seed(settings_pack& set)
68
  { set = high_performance_seed(); }
69
#endif
70
71
namespace aux {
72
73
  struct session_impl;
74
}
75
76
  struct disk_interface;
77
  struct counters;
78
  struct settings_interface;
79
80
  // the constructor function for the default storage. On systems that support
81
  // memory mapped files (and a 64 bit address space) the memory mapped storage
82
  // will be constructed, otherwise the portable posix storage.
83
  TORRENT_EXPORT std::unique_ptr<disk_interface> default_disk_io_constructor(
84
    io_context& ios, settings_interface const&, counters& cnt);
85
86
  // this is a holder for the internal session implementation object. Once the
87
  // session destruction is explicitly initiated, this holder is used to
88
  // synchronize the completion of the shutdown. The lifetime of this object
89
  // may outlive session, causing the session destructor to not block. The
90
  // session_proxy destructor will block however, until the underlying session
91
  // is done shutting down.
92
  struct TORRENT_EXPORT session_proxy
93
  {
94
    friend struct session;
95
    // default constructor, does not refer to any session
96
    // implementation object.
97
    session_proxy();
98
    ~session_proxy();
99
    session_proxy(session_proxy const&);
100
    session_proxy& operator=(session_proxy const&) &;
101
    session_proxy(session_proxy&&) noexcept;
102
    session_proxy& operator=(session_proxy&&) & noexcept;
103
  private:
104
    session_proxy(
105
      std::shared_ptr<io_context> ios
106
      , std::shared_ptr<std::thread> t
107
      , std::shared_ptr<aux::session_impl> impl);
108
109
    std::shared_ptr<io_context> m_io_service;
110
    std::shared_ptr<std::thread> m_thread;
111
    std::shared_ptr<aux::session_impl> m_impl;
112
  };
113
114
  // The session holds all state that spans multiple torrents. Among other
115
  // things it runs the network loop and manages all torrents. Once it's
116
  // created, the session object will spawn the main thread that will do all
117
  // the work. The main thread will be idle as long it doesn't have any
118
  // torrents to participate in.
119
  //
120
  // You have some control over session configuration through the
121
  // ``session_handle::apply_settings()`` member function. To change one or more
122
  // configuration options, create a settings_pack. object and fill it with
123
  // the settings to be set and pass it in to ``session::apply_settings()``.
124
  //
125
  // see apply_settings().
126
  struct TORRENT_EXPORT session : session_handle
127
  {
128
    // Constructs the session objects which acts as the container of torrents.
129
    // In order to avoid a race condition between starting the session and
130
    // configuring it, you can pass in a session_params object. Its settings
131
    // will take effect before the session starts up.
132
    explicit session(session_params const& params);
133
    explicit session(session_params&& params);
134
#if TORRENT_ABI_VERSION < 4
135
    TORRENT_DEPRECATED session(session_params const& params, session_flags_t flags);
136
    TORRENT_DEPRECATED session(session_params&& params, session_flags_t flags);
137
#endif
138
    session();
139
140
    // Overload of the constructor that takes an external io_context to run
141
    // the session object on. This is primarily useful for tests that may want
142
    // to run multiple sessions on a single io_context, or low resource
143
    // systems where additional threads are expensive and sharing an
144
    // io_context with other events is fine.
145
    //
146
    // .. warning::
147
    //  The session object does not cleanly terminate with an external
148
    //  ``io_context``. The ``io_context::run()`` call *must* have returned
149
    //  before it's safe to destruct the session. Which means you *MUST*
150
    //  call session::abort() and save the session_proxy first, then
151
    //  destruct the session object, then sync with the io_context, then
152
    //  destruct the session_proxy object.
153
    session(session_params&& params, io_context& ios);
154
    session(session_params const& params, io_context& ios);
155
#if TORRENT_ABI_VERSION < 4
156
    TORRENT_DEPRECATED session(session_params&& params, io_context& ios, session_flags_t);
157
    TORRENT_DEPRECATED session(session_params const& params, io_context& ios, session_flags_t);
158
#endif
159
160
    // hidden
161
    session(session&&);
162
    session& operator=(session&&) &;
163
164
    // hidden
165
    session(session const&) = delete;
166
    session& operator=(session const&) = delete;
167
168
#if TORRENT_ABI_VERSION <= 2
169
#include "libtorrent/aux_/disable_deprecation_warnings_push.hpp"
170
171
    // Constructs the session objects which acts as the container of torrents.
172
    // It provides configuration options across torrents (such as rate limits,
173
    // disk cache, ip filter etc.). In order to avoid a race condition between
174
    // starting the session and configuring it, you can pass in a
175
    // settings_pack object. Its settings will take effect before the session
176
    // starts up.
177
    //
178
    // The ``flags`` parameter can be used to start default features (UPnP &
179
    // NAT-PMP) and default plugins (ut_metadata, ut_pex and smart_ban). The
180
    // default is to start those features. If you do not want them to start,
181
    // pass 0 as the flags parameter.
182
    TORRENT_DEPRECATED
183
    session(settings_pack&& pack, session_flags_t const flags);
184
    TORRENT_DEPRECATED
185
    session(settings_pack const& pack, session_flags_t const flags);
186
0
    explicit session(settings_pack&& pack) : session(std::move(pack), add_default_plugins) {}
187
1
    explicit session(settings_pack const& pack) : session(pack, add_default_plugins) {}
188
189
    // overload of the constructor that takes an external io_context to run
190
    // the session object on. This is primarily useful for tests that may want
191
    // to run multiple sessions on a single io_context, or low resource
192
    // systems where additional threads are expensive and sharing an
193
    // io_context with other events is fine.
194
    //
195
    // .. warning::
196
    //  The session object does not cleanly terminate with an external
197
    //  ``io_context``. The ``io_context::run()`` call _must_ have returned
198
    //  before it's safe to destruct the session. Which means you *MUST*
199
    //  call session::abort() and save the session_proxy first, then
200
    //  destruct the session object, then sync with the io_context, then
201
    //  destruct the session_proxy object.
202
    TORRENT_DEPRECATED
203
    session(settings_pack&&, io_context&, session_flags_t);
204
    TORRENT_DEPRECATED
205
    session(settings_pack const&, io_context&, session_flags_t);
206
0
    session(settings_pack&& pack, io_context& ios) : session(std::move(pack), ios, add_default_plugins) {}
207
0
    session(settings_pack const& pack, io_context& ios) : session(pack, ios, add_default_plugins) {}
208
209
#include "libtorrent/aux_/disable_warnings_pop.hpp"
210
#endif // TORRENT_ABI_VERSION
211
212
#if TORRENT_ABI_VERSION == 1
213
#include "libtorrent/aux_/disable_deprecation_warnings_push.hpp"
214
215
    TORRENT_DEPRECATED
216
    session(fingerprint const& print
217
      , session_flags_t const flags = start_default_features | add_default_plugins
218
      , alert_category_t const alert_mask = alert_category::error);
219
220
    TORRENT_DEPRECATED
221
    session(fingerprint const& print
222
      , std::pair<int, int> listen_port_range
223
      , char const* listen_interface = "0.0.0.0"
224
      , session_flags_t const flags = start_default_features | add_default_plugins
225
      , alert_category_t const alert_mask = alert_category::error);
226
227
#include "libtorrent/aux_/disable_warnings_pop.hpp"
228
#endif // TORRENT_ABI_VERSION
229
230
    // The destructor of session will notify all trackers that our torrents
231
    // have been shut down. If some trackers are down, they will time out.
232
    // All this before the destructor of session returns. So, it's advised
233
    // that any kind of interface (such as windows) are closed before
234
    // destructing the session object. Because it can take a few second for
235
    // it to finish. The timeout can be set with apply_settings().
236
    ~session();
237
238
    // In case you want to destruct the session asynchronously, you can
239
    // request a session destruction proxy. If you don't do this, the
240
    // destructor of the session object will block while the trackers are
241
    // contacted. If you keep one ``session_proxy`` to the session when
242
    // destructing it, the destructor will not block, but start to close down
243
    // the session, the destructor of the proxy will then synchronize the
244
    // threads. So, the destruction of the session is performed from the
245
    // ``session`` destructor call until the ``session_proxy`` destructor
246
    // call. The ``session_proxy`` does not have any operations on it (since
247
    // the session is being closed down, no operations are allowed on it).
248
    // The only valid operation is calling the destructor::
249
    //
250
    //  struct session_proxy {};
251
    session_proxy abort();
252
253
  private:
254
255
    void start(session_flags_t, session_params&& params, io_context* ios);
256
257
#if TORRENT_ABI_VERSION <= 2
258
    void start(session_flags_t flags, settings_pack&& sp, io_context* ios);
259
#endif
260
261
    void start(session_params const& params, io_context* ios) = delete;
262
#if TORRENT_ABI_VERSION <= 2
263
    void start(session_flags_t flags, settings_pack const& sp, io_context* ios) = delete;
264
#endif
265
266
    // data shared between the main thread
267
    // and the working thread
268
    std::shared_ptr<io_context> m_io_service;
269
    std::shared_ptr<std::thread> m_thread;
270
    std::shared_ptr<aux::session_impl> m_impl;
271
  };
272
273
}
274
275
#endif // TORRENT_SESSION_HPP_INCLUDED