Skip to content

Commit

Permalink
move session flags into the session_params object
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Dec 15, 2020
1 parent 735c486 commit d5b27ee
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* move session_flags to session_params
* the entry class is now a standard variant type
* use std::string_view instead of boost counterpart
* libtorrent now requires C++17 to build
Expand Down
7 changes: 4 additions & 3 deletions bindings/python/src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ namespace
std::shared_ptr<lt::session> make_session(boost::python::dict sett
, session_flags_t const flags)
{
settings_pack p;
make_settings_pack(p, sett);
return std::make_shared<lt::session>(p, flags);
session_params p;
make_settings_pack(p.settings, sett);
p.flags = flags;
return std::make_shared<lt::session>(std::move(p));
}

void session_apply_settings(lt::session& ses, dict const& sett_dict)
Expand Down
17 changes: 8 additions & 9 deletions include/libtorrent/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,12 @@ namespace aux {
// In order to avoid a race condition between starting the session and
// configuring it, you can pass in a session_params object. Its settings
// will take effect before the session starts up.
//
// The overloads taking ``flags`` can be used to start a session in
// paused mode (by passing in ``session::paused``). Note that
// ``add_default_plugins`` do not have an affect on constructors that
// take a session_params object. It already contains the plugins to use.
explicit session(session_params const& params);
explicit session(session_params&& params);
session(session_params const& params, session_flags_t flags);
session(session_params&& params, session_flags_t flags);
#if TORRENT_ABI_VERSION < 4
TORRENT_DEPRECATED session(session_params const& params, session_flags_t flags);
TORRENT_DEPRECATED session(session_params&& params, session_flags_t flags);
#endif
session();

// Overload of the constructor that takes an external io_context to run
Expand All @@ -155,8 +152,10 @@ namespace aux {
// destruct the session_proxy object.
session(session_params&& params, io_context& ios);
session(session_params const& params, io_context& ios);
session(session_params&& params, io_context& ios, session_flags_t);
session(session_params const& params, io_context& ios, session_flags_t);
#if TORRENT_ABI_VERSION < 4
TORRENT_DEPRECATED session(session_params&& params, io_context& ios, session_flags_t);
TORRENT_DEPRECATED session(session_params const& params, io_context& ios, session_flags_t);
#endif

// hidden
session(session&&);
Expand Down
27 changes: 16 additions & 11 deletions include/libtorrent/session_params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ see LICENSE file.
#include "libtorrent/session_types.hpp"
#include "libtorrent/kademlia/dht_storage.hpp"
#include "libtorrent/ip_filter.hpp"
#include "libtorrent/session_types.hpp" // for session_flags_t

#if TORRENT_ABI_VERSION <= 2
#include "libtorrent/kademlia/dht_settings.hpp"
Expand Down Expand Up @@ -73,20 +74,13 @@ struct TORRENT_EXPORT session_params
// The settings to configure the session with
settings_pack settings;

// specifies flags affecting the session construction. E.g. they can be used
// to start a session in paused mode (by passing in ``session::paused``).
session_flags_t flags{};

// the plugins to add to the session as it is constructed
std::vector<std::shared_ptr<plugin>> extensions;

#if TORRENT_ABI_VERSION <= 2

#include "libtorrent/aux_/disable_deprecation_warnings_push.hpp"

// this is deprecated. Use the dht_* settings instead.
dht::dht_settings dht_settings;

#include "libtorrent/aux_/disable_warnings_pop.hpp"

#endif

// DHT node ID and node addresses to bootstrap the DHT with.
dht::dht_state dht_state;

Expand All @@ -105,6 +99,17 @@ struct TORRENT_EXPORT session_params
// the IP filter to use for the session. This restricts which peers are allowed
// to connect. As if passed to set_ip_filter().
libtorrent::ip_filter ip_filter;

#if TORRENT_ABI_VERSION <= 2

#include "libtorrent/aux_/disable_deprecation_warnings_push.hpp"

// this is deprecated. Use the dht_* settings instead.
dht::dht_settings dht_settings;

#include "libtorrent/aux_/disable_warnings_pop.hpp"

#endif
};

TORRENT_VERSION_NAMESPACE_3_END
Expand Down
35 changes: 34 additions & 1 deletion simulation/test_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ TORRENT_TEST(tie_listen_ports)

// make sure passing in the session::paused flag does indeed start the session
// paused
TORRENT_TEST(construct_paused_session)
#if TORRENT_ABI_VERSION < 4
TORRENT_TEST(construct_paused_session_deprecated)
{
using namespace libtorrent;

Expand Down Expand Up @@ -252,3 +253,35 @@ TORRENT_TEST(construct_paused_session)

sim.run();
}
#endif

TORRENT_TEST(construct_paused_session)
{
using namespace libtorrent;

sim::default_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context ios { sim, addr("50.0.0.1")};

lt::session_proxy zombie;

// create session
lt::session_params p;
p.settings = settings();
p.settings.set_str(settings_pack::listen_interfaces, "0.0.0.0:0");
p.settings.set_int(settings_pack::alert_mask, alert_category::error
| alert_category::status
| alert_category::torrent_log);
p.flags |= session::paused;

auto ses = std::make_shared<lt::session>(p, ios);

sim::timer t(sim, lt::seconds(30), [&](boost::system::error_code const&)
{
TEST_CHECK(ses->is_paused());
zombie = ses->abort();
ses.reset();
});

sim.run();
}
16 changes: 10 additions & 6 deletions src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ namespace {
#endif
#endif

// TODO: start() should just use flags out of the session_params object,
m_impl = std::make_shared<aux::session_impl>(std::ref(*ios)
, std::move(params.settings)
, std::move(params.disk_io_constructor)
Expand Down Expand Up @@ -317,14 +318,15 @@ namespace {

session::session(session_params const& params)
{
start({}, session_params(params), nullptr);
start(params.flags, session_params(params), nullptr);
}

session::session(session_params&& params)
{
start({}, std::move(params), nullptr);
start(params.flags, std::move(params), nullptr);
}

#if TORRENT_ABI_VERSION < 4
session::session(session_params const& params, session_flags_t const flags)
{
start(flags, session_params(params), nullptr);
Expand All @@ -334,23 +336,25 @@ namespace {
{
start(flags, std::move(params), nullptr);
}
#endif

session::session()
{
session_params params;
start({}, std::move(params), nullptr);
start(params.flags, std::move(params), nullptr);
}

session::session(session_params&& params, io_context& ios)
{
start({}, std::move(params), &ios);
start(params.flags, std::move(params), &ios);
}

session::session(session_params const& params, io_context& ios)
{
start({}, session_params(params), &ios);
start(params.flags, session_params(params), &ios);
}

#if TORRENT_ABI_VERSION < 4
session::session(session_params&& params, io_context& ios, session_flags_t const flags)
{
start(flags, std::move(params), &ios);
Expand All @@ -360,7 +364,7 @@ namespace {
{
start(flags, session_params(params), &ios);
}

#endif

#if TORRENT_ABI_VERSION <= 2
session::session(settings_pack&& pack, session_flags_t const flags)
Expand Down

0 comments on commit d5b27ee

Please sign in to comment.