diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 7c2f22411..a35ffe448 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -276,7 +276,8 @@ namespace libtorrent libtorrent::session_settings deprecated_settings() const; #endif - void apply_settings_pack(settings_pack* pack); + void apply_settings_pack(boost::shared_ptr pack); + void apply_settings_pack_impl(settings_pack const& pack); session_settings const& settings() const { return m_settings; } #ifndef TORRENT_DISABLE_DHT diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index 1d4a0a93c..7d1e0a5db 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -253,7 +253,7 @@ namespace libtorrent { namespace aux virtual void trigger_auto_manage() = 0; - virtual void apply_settings_pack(settings_pack* pack) = 0; + virtual void apply_settings_pack(boost::shared_ptr pack) = 0; virtual session_settings const& settings() const = 0; virtual void queue_tracker_request(tracker_request& req diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index c7f475f9c..9bd064ea1 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -290,7 +290,7 @@ namespace libtorrent , int block_size = 16 * 1024); ~disk_io_thread(); - void set_settings(settings_pack* sett, alert_manager& alerts); + void set_settings(settings_pack const* sett, alert_manager& alerts); void set_num_threads(int i, bool wait = true); void async_read(piece_manager* storage, peer_request const& r @@ -586,7 +586,7 @@ namespace libtorrent // dedicated to do hashing condition_variable m_hash_job_cond; tailqueue m_queued_hash_jobs; - + // used to rate limit disk performance warnings time_point m_last_disk_aio_performance_warning; diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 5fe62a336..317046ad5 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -39,9 +39,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_push.hpp" #include -#ifdef _MSC_VER -# include -#endif #include "libtorrent/aux_/disable_warnings_pop.hpp" @@ -50,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/entry.hpp" #include "libtorrent/version.hpp" #include "libtorrent/fingerprint.hpp" -#include "libtorrent/disk_io_thread.hpp" +#include "libtorrent/disk_io_thread.hpp" // for cached_piece_info #include "libtorrent/peer_id.hpp" #include "libtorrent/alert.hpp" // alert::error_notification #include "libtorrent/add_torrent_params.hpp" @@ -389,7 +386,7 @@ namespace libtorrent #endif torrent_handle add_torrent(add_torrent_params const& params, error_code& ec); void async_add_torrent(add_torrent_params const& params); - + #ifndef BOOST_NO_EXCEPTIONS #ifndef TORRENT_NO_DEPRECATE // deprecated in 0.14 diff --git a/include/libtorrent/settings_pack.hpp b/include/libtorrent/settings_pack.hpp index 8299bcb3a..c53c0d35f 100644 --- a/include/libtorrent/settings_pack.hpp +++ b/include/libtorrent/settings_pack.hpp @@ -36,6 +36,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/entry.hpp" #include +#ifndef TORRENT_NO_DEPRECATE +#include +#endif + // OVERVIEW // // You have some control over session configuration through the session::apply_settings() @@ -53,7 +57,7 @@ namespace libtorrent struct settings_pack; struct bdecode_node; - TORRENT_EXTRA_EXPORT settings_pack* load_pack_from_dict(bdecode_node const& settings); + 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); @@ -62,7 +66,7 @@ namespace libtorrent #ifndef TORRENT_NO_DEPRECATE struct session_settings; - settings_pack* load_pack_from_struct(aux::session_settings const& current, session_settings const& s); + boost::shared_ptr load_pack_from_struct(aux::session_settings const& current, session_settings const& s); void load_struct_from_settings(aux::session_settings const& current, session_settings& ret); #endif @@ -183,7 +187,7 @@ namespace libtorrent // when using a poxy, this is the hostname where the proxy is running // see proxy_type. proxy_hostname, - + // when using a proxy, these are the credentials (if any) to use whne // connecting to it. see proxy_type proxy_username, diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 32242aae3..635977c74 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -262,7 +262,7 @@ namespace libtorrent m_blocks_to_reclaim.clear(); } - void disk_io_thread::set_settings(settings_pack* pack, alert_manager& alerts) + void disk_io_thread::set_settings(settings_pack const* pack, alert_manager& alerts) { TORRENT_ASSERT(m_magic == 0x1337); mutex::scoped_lock l(m_cache_mutex); diff --git a/src/session.cpp b/src/session.cpp index 0a13134ff..81236a88a 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -968,7 +968,7 @@ namespace libtorrent void session::apply_settings(settings_pack const& s) { - settings_pack* copy = new settings_pack(s); + boost::shared_ptr copy = boost::make_shared(s); TORRENT_ASYNC_CALL1(apply_settings_pack, copy); } @@ -1209,14 +1209,14 @@ namespace libtorrent p.set_bool(settings_pack::enable_lsd, true); apply_settings(p); } - + void session::start_natpmp() { settings_pack p; p.set_bool(settings_pack::enable_natpmp, true); apply_settings(p); } - + void session::start_upnp() { settings_pack p; @@ -1230,22 +1230,22 @@ namespace libtorrent p.set_bool(settings_pack::enable_lsd, false); apply_settings(p); } - + void session::stop_natpmp() { settings_pack p; p.set_bool(settings_pack::enable_natpmp, false); apply_settings(p); } - + void session::stop_upnp() { settings_pack p; p.set_bool(settings_pack::enable_upnp, false); apply_settings(p); } -#endif - +#endif // TORRENT_NO_DEPRECATED + int session::add_port_mapping(protocol_type t, int external_port, int local_port) { return TORRENT_SYNC_CALL_RET3(int, add_port_mapping, int(t), external_port, local_port); @@ -1255,7 +1255,7 @@ namespace libtorrent { TORRENT_ASYNC_CALL1(delete_port_mapping, handle); } - + #ifndef TORRENT_NO_DEPRECATE session_settings::session_settings(std::string const& user_agent_) { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 1b2dbcb5d..b920a1fe1 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -425,9 +425,6 @@ namespace aux { , m_need_auto_manage(false) , m_abort(false) , m_paused(false) -#if TORRENT_USE_ASSERTS && defined BOOST_HAS_PTHREADS - , m_network_thread(0) -#endif { #if TORRENT_USE_ASSERTS m_posting_torrent_updates = false; @@ -448,6 +445,10 @@ namespace aux { TORRENT_ASSERT_VAL(!ec, ec); } + // 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) { m_alerts.set_alert_mask(pack.get_int(settings_pack::alert_mask)); @@ -546,7 +547,7 @@ namespace aux { session_log(" generated peer ID: %s", m_peer_id.to_string().c_str()); #endif - settings_pack* copy = new settings_pack(pack); + boost::shared_ptr copy = boost::make_shared(pack); m_io_service.post(boost::bind(&session_impl::apply_settings_pack, this, copy)); // call update_* after settings set initialized m_io_service.post(boost::bind(&session_impl::init_settings, this)); @@ -806,7 +807,7 @@ namespace aux { settings = e->dict_find_dict("settings"); if (settings) { - settings_pack* pack = load_pack_from_dict(settings); + boost::shared_ptr pack = load_pack_from_dict(settings); apply_settings_pack(pack); } @@ -1520,22 +1521,24 @@ namespace aux { return ret; } - // session_impl is responsible for deleting 'pack', but it - // will pass it on to the disk io thread, which will take - // over ownership of it - void session_impl::apply_settings_pack(settings_pack* pack) + // session_impl is responsible for deleting 'pack' + void session_impl::apply_settings_pack(boost::shared_ptr pack) + { + apply_settings_pack_impl(*pack); + } + + void session_impl::apply_settings_pack_impl(settings_pack const& pack) { bool reopen_listen_port = - (pack->has_val(settings_pack::ssl_listen) - && pack->get_int(settings_pack::ssl_listen) + (pack.has_val(settings_pack::ssl_listen) + && pack.get_int(settings_pack::ssl_listen) != m_settings.get_int(settings_pack::ssl_listen)) - || (pack->has_val(settings_pack::listen_interfaces) - && pack->get_str(settings_pack::listen_interfaces) + || (pack.has_val(settings_pack::listen_interfaces) + && pack.get_str(settings_pack::listen_interfaces) != m_settings.get_str(settings_pack::listen_interfaces)); - apply_pack(pack, m_settings, this); - m_disk_thread.set_settings(pack, m_alerts); - delete pack; + apply_pack(&pack, m_settings, this); + m_disk_thread.set_settings(&pack, m_alerts); if (reopen_listen_port) { @@ -1549,7 +1552,7 @@ namespace aux { { INVARIANT_CHECK; TORRENT_ASSERT(is_single_thread()); - settings_pack* p = load_pack_from_struct(m_settings, s); + boost::shared_ptr p = load_pack_from_struct(m_settings, s); apply_settings_pack(p); } @@ -5696,44 +5699,44 @@ retry: void session_impl::set_local_download_rate_limit(int bytes_per_second) { - settings_pack* p = new settings_pack; - p->set_int(settings_pack::local_download_rate_limit, bytes_per_second); - apply_settings_pack(p); + settings_pack p; + p.set_int(settings_pack::local_download_rate_limit, bytes_per_second); + apply_settings_pack_impl(p); } void session_impl::set_local_upload_rate_limit(int bytes_per_second) { - settings_pack* p = new settings_pack; - p->set_int(settings_pack::local_upload_rate_limit, bytes_per_second); - apply_settings_pack(p); + settings_pack p; + p.set_int(settings_pack::local_upload_rate_limit, bytes_per_second); + apply_settings_pack_impl(p); } void session_impl::set_download_rate_limit(int bytes_per_second) { - settings_pack* p = new settings_pack; - p->set_int(settings_pack::download_rate_limit, bytes_per_second); - apply_settings_pack(p); + settings_pack p; + p.set_int(settings_pack::download_rate_limit, bytes_per_second); + apply_settings_pack_impl(p); } void session_impl::set_upload_rate_limit(int bytes_per_second) { - settings_pack* p = new settings_pack; - p->set_int(settings_pack::upload_rate_limit, bytes_per_second); - apply_settings_pack(p); + settings_pack p; + p.set_int(settings_pack::upload_rate_limit, bytes_per_second); + apply_settings_pack_impl(p); } void session_impl::set_max_connections(int limit) { - settings_pack* p = new settings_pack; - p->set_int(settings_pack::connections_limit, limit); - apply_settings_pack(p); + settings_pack p; + p.set_int(settings_pack::connections_limit, limit); + apply_settings_pack_impl(p); } void session_impl::set_max_uploads(int limit) { - settings_pack* p = new settings_pack; - p->set_int(settings_pack::unchoke_slots_limit, limit); - apply_settings_pack(p); + settings_pack p; + p.set_int(settings_pack::unchoke_slots_limit, limit); + apply_settings_pack_impl(p); } int session_impl::local_upload_rate_limit() const diff --git a/src/settings_pack.cpp b/src/settings_pack.cpp index c4393c51a..f65b9bb5c 100644 --- a/src/settings_pack.cpp +++ b/src/settings_pack.cpp @@ -390,9 +390,9 @@ namespace libtorrent return ""; } - settings_pack* load_pack_from_dict(bdecode_node const& settings) + boost::shared_ptr load_pack_from_dict(bdecode_node const& settings) { - settings_pack* pack = new settings_pack; + boost::shared_ptr pack = boost::make_shared(); for (int i = 0; i < settings.dict_size(); ++i) { @@ -462,9 +462,10 @@ namespace libtorrent } #ifndef TORRENT_NO_DEPRECATE - settings_pack* load_pack_from_struct(aux::session_settings const& current, session_settings const& s) + boost::shared_ptr load_pack_from_struct( + aux::session_settings const& current, session_settings const& s) { - settings_pack* p = new settings_pack; + boost::shared_ptr p = boost::make_shared(); for (int i = 0; i < settings_pack::num_string_settings; ++i) { @@ -474,7 +475,7 @@ namespace libtorrent if (val == current.get_str(setting_name)) continue; p->set_str(setting_name, val); } - + for (int i = 0; i < settings_pack::num_int_settings; ++i) { if (int_settings[i].offset == 0) continue; @@ -521,7 +522,7 @@ namespace libtorrent std::string& val = *(std::string*)(((char*)&ret) + str_settings[i].offset); val = current.get_str(settings_pack::string_type_base + i); } - + for (int i = 0; i < settings_pack::num_int_settings; ++i) { if (int_settings[i].offset == 0) continue; diff --git a/src/torrent.cpp b/src/torrent.cpp index 93f75255b..164b27ad7 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2771,7 +2771,7 @@ namespace libtorrent #ifndef TORRENT_NO_DEPRECATE void torrent::use_interface(std::string net_interfaces) { - settings_pack* p = new settings_pack; + boost::shared_ptr p = boost::make_shared(); p->set_str(settings_pack::outgoing_interfaces, net_interfaces); m_ses.apply_settings_pack(p); } @@ -2792,7 +2792,7 @@ namespace libtorrent void torrent::on_tracker_announce() { TORRENT_ASSERT(is_single_thread()); - m_waiting_tracker = false; + m_waiting_tracker = false; if (m_abort) return; announce_with_tracker(); }