From 4b467f82aca5a6d8b7c89031e1a7986f20b1dd16 Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 30 Nov 2017 16:04:03 +0100 Subject: [PATCH] fix issue with initializing settings on session construction --- ChangeLog | 1 + include/libtorrent/aux_/session_impl.hpp | 12 ++-- include/libtorrent/aux_/session_settings.hpp | 2 + include/libtorrent/settings_pack.hpp | 1 + src/session.cpp | 4 +- src/session_impl.cpp | 59 +++++++------------- src/session_settings.cpp | 7 +++ src/settings_pack.cpp | 22 ++++++++ 8 files changed, 60 insertions(+), 48 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6b349f3b2..a5c76d14a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ + * fix issue with initializing settings on session construction * fix issue with receiving interested before metadata * fix IPv6 tracker announce issue * restore path sanitization behavior of ":" diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index a7637f7aa..260163e81 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -182,10 +182,10 @@ namespace libtorrent typedef std::map > torrent_map; #endif - session_impl(io_service& ios); + session_impl(io_service& ios, settings_pack const& pack); virtual ~session_impl(); - void start_session(settings_pack const& pack); + void start_session(); void set_load_function(user_load_function_t fun) { m_user_load_torrent = fun; } @@ -620,6 +620,9 @@ namespace libtorrent { return &m_utp_socket_manager; } void inc_boost_connections() TORRENT_OVERRIDE { ++m_boost_connections; } + // the settings for the client + aux::session_settings m_settings; + #ifndef TORRENT_NO_DEPRECATE // the time when the next rss feed needs updating time_point m_next_rss_update; @@ -684,7 +687,7 @@ namespace libtorrent peer_class_pool m_classes; - void init(boost::shared_ptr pack); + void init(); void submit_disk_jobs(); @@ -693,9 +696,6 @@ namespace libtorrent void on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih); void setup_socket_buffers(socket_type& s) TORRENT_OVERRIDE; - // the settings for the client - aux::session_settings m_settings; - counters m_stats_counters; // this is a pool allocator for torrent_peer objects diff --git a/include/libtorrent/aux_/session_settings.hpp b/include/libtorrent/aux_/session_settings.hpp index ad981fbe5..a4295db8d 100644 --- a/include/libtorrent/aux_/session_settings.hpp +++ b/include/libtorrent/aux_/session_settings.hpp @@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { + struct settings_pack; TORRENT_EXTRA_EXPORT void initialize_default_settings(aux::session_settings& s); } @@ -71,6 +72,7 @@ namespace libtorrent { namespace aux bool get_bool(int name) const { GET(bool, false); } session_settings(); + session_settings(settings_pack const&); private: std::string m_strings[settings_pack::num_string_settings]; diff --git a/include/libtorrent/settings_pack.hpp b/include/libtorrent/settings_pack.hpp index 675157c69..415f7e571 100644 --- a/include/libtorrent/settings_pack.hpp +++ b/include/libtorrent/settings_pack.hpp @@ -66,6 +66,7 @@ namespace libtorrent TORRENT_EXTRA_EXPORT boost::shared_ptr load_pack_from_dict(bdecode_node const& settings); TORRENT_EXTRA_EXPORT void save_settings_to_dict(aux::session_settings const& s, entry::dictionary_type& sett); TORRENT_EXTRA_EXPORT void apply_pack(settings_pack const* pack, aux::session_settings& sett, aux::session_impl* ses = 0); + TORRENT_EXTRA_EXPORT void run_all_updates(aux::session_impl& ses); TORRENT_EXPORT int setting_by_name(std::string const& name); TORRENT_EXPORT char const* name_for_setting(int s); diff --git a/src/session.cpp b/src/session.cpp index 227c78b7c..ee565bb00 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -347,7 +347,7 @@ namespace libtorrent ios = m_io_service.get(); } - m_impl = boost::make_shared(boost::ref(*ios)); + m_impl = boost::make_shared(boost::ref(*ios), boost::ref(pack)); *static_cast(this) = session_handle(m_impl.get()); #ifndef TORRENT_DISABLE_EXTENSIONS @@ -361,7 +361,7 @@ namespace libtorrent TORRENT_UNUSED(flags); #endif - m_impl->start_session(pack); + m_impl->start_session(); if (internal_executor) { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 9553106fb..5048f612d 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -336,21 +336,20 @@ namespace aux { } // anonymous namesoace #endif - session_impl::session_impl(io_service& ios) - : + session_impl::session_impl(io_service& ios, settings_pack const& pack) + : m_settings(pack) #ifndef TORRENT_NO_DEPRECATE - m_next_rss_update(min_time()) - , + , m_next_rss_update(min_time()) #endif #ifndef TORRENT_DISABLE_POOL_ALLOCATOR - m_send_buffers(send_buffer_size()) - , + , m_send_buffers(send_buffer_size()) #endif - m_io_service(ios) + , m_io_service(ios) #ifdef TORRENT_USE_OPENSSL , m_ssl_ctx(m_io_service, boost::asio::ssl::context::sslv23) #endif - , m_alerts(m_settings.get_int(settings_pack::alert_queue_size), alert::all_categories) + , m_alerts(m_settings.get_int(settings_pack::alert_queue_size) + , m_settings.get_int(settings_pack::alert_mask)) #ifndef TORRENT_NO_DEPRECATE , m_alert_pointer_pos(0) #endif @@ -448,19 +447,15 @@ namespace aux { TORRENT_ASSERT_VAL(!ec, ec); update_time_now(); + m_disk_thread.set_settings(&pack, m_alerts); } // This function is called by the creating thread, not in the message loop's // / io_service thread. // TODO: 2 is there a reason not to move all of this into init()? and just // post it to the io_service? - void session_impl::start_session(settings_pack const& pack) + void session_impl::start_session() { - if (pack.has_val(settings_pack::alert_mask)) - { - m_alerts.set_alert_mask(pack.get_int(settings_pack::alert_mask)); - } - #ifndef TORRENT_DISABLE_LOGGING session_log("start session"); #endif @@ -540,13 +535,11 @@ namespace aux { session_log(" generated peer ID: %s", m_peer_id.to_string().c_str()); #endif - boost::shared_ptr copy = boost::make_shared(pack); - m_io_service.post(boost::bind(&session_impl::init, this, copy)); + m_io_service.post(boost::bind(&session_impl::init, this)); } - void session_impl::init(boost::shared_ptr pack) + void session_impl::init() { - INVARIANT_CHECK; // this is a debug facility // see single_threaded in debug.hpp thread_started(); @@ -604,32 +597,20 @@ namespace aux { session_log(" done starting session"); #endif - apply_settings_pack(pack); + // apply all m_settings to this session + run_all_updates(*this); - // call update_* after settings set initialized - -#ifndef TORRENT_NO_DEPRECATE - update_local_download_rate(); - update_local_upload_rate(); -#endif - update_download_rate(); - update_upload_rate(); - update_connections_limit(); - update_unchoke_limit(); - update_disk_threads(); - update_network_threads(); - update_upnp(); - update_natpmp(); - update_lsd(); - update_dht(); - update_peer_fingerprint(); - update_dht_bootstrap_nodes(); + // this applies unchoke settings from m_settings + recalculate_unchoke_slots(); if (m_listen_sockets.empty()) { update_listen_interfaces(); open_listen_port(); } +#if TORRENT_USE_INVARIANT_CHECKS + check_invariant(); +#endif } void session_impl::async_resolve(std::string const& host, int flags @@ -4191,7 +4172,6 @@ retry: void session_impl::recalculate_unchoke_slots() { TORRENT_ASSERT(is_single_thread()); - INVARIANT_CHECK; time_point const now = aux::time_now(); time_duration const unchoke_interval = now - m_last_choke; @@ -6309,7 +6289,7 @@ retry: void session_impl::update_queued_disk_bytes() { - boost::uint64_t cache_size = m_settings.get_int(settings_pack::cache_size); + boost::uint64_t const cache_size = m_settings.get_int(settings_pack::cache_size); if (m_settings.get_int(settings_pack::max_queued_disk_bytes) / 16 / 1024 > cache_size / 2 && cache_size > 5 @@ -7179,7 +7159,6 @@ retry: #else std::set unique_peers; #endif - TORRENT_ASSERT(m_settings.get_int(settings_pack::connections_limit) > 0); int unchokes = 0; int unchokes_all = 0; diff --git a/src/session_settings.cpp b/src/session_settings.cpp index cd506422d..3d35fdbd2 100644 --- a/src/session_settings.cpp +++ b/src/session_settings.cpp @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/aux_/session_settings.hpp" +#include "libtorrent/settings_pack.hpp" namespace libtorrent { namespace aux { @@ -38,5 +39,11 @@ namespace libtorrent { namespace aux { initialize_default_settings(*this); } + + session_settings::session_settings(settings_pack const& p) + { + initialize_default_settings(*this); + apply_pack(&p, *this); + } } } diff --git a/src/settings_pack.cpp b/src/settings_pack.cpp index e908d24b1..036da6665 100644 --- a/src/settings_pack.cpp +++ b/src/settings_pack.cpp @@ -471,6 +471,28 @@ namespace libtorrent } } + void run_all_updates(aux::session_impl& ses) + { + typedef void (aux::session_impl::*fun_t)(); + for (int i = 0; i < settings_pack::num_string_settings; ++i) + { + fun_t const& f = str_settings[i].fun; + if (f) (ses.*f)(); + } + + for (int i = 0; i < settings_pack::num_int_settings; ++i) + { + fun_t const& f = int_settings[i].fun; + if (f) (ses.*f)(); + } + + for (int i = 0; i < settings_pack::num_bool_settings; ++i) + { + fun_t const& f = bool_settings[i].fun; + if (f) (ses.*f)(); + } + } + #ifndef TORRENT_NO_DEPRECATE #include "libtorrent/aux_/disable_warnings_push.hpp"