From 0f37e3403c620466b3bdff30b7a6bc22f958667a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 6 Jan 2015 08:08:49 +0000 Subject: [PATCH] transition from intrusive_ptr to shared_ptr. don't export initialize_default_settings --- CMakeLists.txt | 1 + Jamfile | 1 + include/libtorrent/Makefile.am | 1 - include/libtorrent/aux_/session_impl.hpp | 6 +- include/libtorrent/aux_/session_settings.hpp | 9 ++- include/libtorrent/block_cache.hpp | 5 -- include/libtorrent/broadcast_socket.hpp | 6 +- include/libtorrent/file.hpp | 9 ++- include/libtorrent/lsd.hpp | 8 ++- include/libtorrent/natpmp.hpp | 11 ++-- include/libtorrent/peer_class.hpp | 7 +-- include/libtorrent/peer_connection.hpp | 1 - include/libtorrent/settings_pack.hpp | 1 - include/libtorrent/storage.hpp | 1 - include/libtorrent/upnp.hpp | 10 +++- src/Makefile.am | 1 + src/broadcast_socket.cpp | 19 +++--- src/file_pool.cpp | 6 +- src/lsd.cpp | 21 ++++--- src/natpmp.cpp | 5 +- src/peer_class.cpp | 4 +- src/piece_picker.cpp | 2 +- src/session_impl.cpp | 45 +++++++------- .../session_settings.cpp | 58 +++---------------- src/torrent.cpp | 2 +- src/upnp.cpp | 25 ++++---- test/test_dht.cpp | 2 +- test/test_transfer.cpp | 2 +- test/test_upnp.cpp | 12 ++-- 29 files changed, 124 insertions(+), 157 deletions(-) rename include/libtorrent/intrusive_ptr_base.hpp => src/session_settings.cpp (54%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b3bc7e80a..0fd7f10e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,7 @@ set(sources session session_call session_impl + session_settings session_stats settings_pack socket_io diff --git a/Jamfile b/Jamfile index 337365fff..120dabf8a 100755 --- a/Jamfile +++ b/Jamfile @@ -617,6 +617,7 @@ SOURCES = session_stats performance_counters resolver + session_settings # -- extensions -- metadata_transfer diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index b01142633..b47fb6c5d 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -63,7 +63,6 @@ nobase_include_HEADERS = \ i2p_stream.hpp \ identify_client.hpp \ instantiate_connection.hpp \ - intrusive_ptr_base.hpp \ invariant_check.hpp \ io.hpp \ io_service.hpp \ diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index e81d79eb2..2ebe9ba67 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -1010,9 +1010,9 @@ namespace libtorrent // this is deducted from the connect speed int m_boost_connections; - boost::intrusive_ptr m_natpmp; - boost::intrusive_ptr m_upnp; - boost::intrusive_ptr m_lsd; + boost::shared_ptr m_natpmp; + boost::shared_ptr m_upnp; + boost::shared_ptr m_lsd; // mask is a bitmask of which protocols to remap on: // 1: NAT-PMP diff --git a/include/libtorrent/aux_/session_settings.hpp b/include/libtorrent/aux_/session_settings.hpp index c5bf3dc45..2a4dc4736 100644 --- a/include/libtorrent/aux_/session_settings.hpp +++ b/include/libtorrent/aux_/session_settings.hpp @@ -40,6 +40,11 @@ POSSIBILITY OF SUCH DAMAGE. #include +namespace libtorrent +{ + TORRENT_EXTRA_EXPORT void initialize_default_settings(aux::session_settings& s); +} + namespace libtorrent { namespace aux { @@ -65,12 +70,12 @@ namespace libtorrent { namespace aux void set_bool(int name, bool value) { SET(bool); } bool get_bool(int name) const { GET(bool, false); } - session_settings() { initialize_default_settings(*this); } + session_settings(); private: std::string m_strings[settings_pack::num_string_settings]; int m_ints[settings_pack::num_int_settings]; - // TODO: 2 make this a bitfield + // TODO: make this a bitfield bool m_bools[settings_pack::num_bool_settings]; }; diff --git a/include/libtorrent/block_cache.hpp b/include/libtorrent/block_cache.hpp index d772d348e..5a9529167 100644 --- a/include/libtorrent/block_cache.hpp +++ b/include/libtorrent/block_cache.hpp @@ -205,11 +205,6 @@ namespace libtorrent // the pointers to the block data. If this is a ghost // cache entry, there won't be any data here - - // TODO: 3 could this be a scoped_array instead? does cached_piece_entry - // really need to be copyable? cached_piece_entry does need to be - // copyable since it's part of a container, but it's possible it could be - // a raw pointer or boost::unique_ptr perhaps boost::shared_array blocks; // the last time a block was written to this piece diff --git a/include/libtorrent/broadcast_socket.hpp b/include/libtorrent/broadcast_socket.hpp index a2d4a609a..d1c39e284 100644 --- a/include/libtorrent/broadcast_socket.hpp +++ b/include/libtorrent/broadcast_socket.hpp @@ -66,11 +66,11 @@ namespace libtorrent class TORRENT_EXTRA_EXPORT broadcast_socket { public: - broadcast_socket(udp::endpoint const& multicast_endpoint - , receive_handler_t const& handler); + broadcast_socket(udp::endpoint const& multicast_endpoint); ~broadcast_socket() { close(); } - void open(io_service& ios, error_code& ec, bool loopback = true); + void open(receive_handler_t const& handler, io_service& ios + , error_code& ec, bool loopback = true); enum flags_t { broadcast = 1 }; void send(char const* buffer, int size, error_code& ec, int flags = 0); diff --git a/include/libtorrent/file.hpp b/include/libtorrent/file.hpp index f3ac47f85..67d663e1c 100644 --- a/include/libtorrent/file.hpp +++ b/include/libtorrent/file.hpp @@ -41,7 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #endif #include -#include +#include #ifdef _MSC_VER #pragma warning(pop) @@ -51,7 +51,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/error_code.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/time.hpp" -#include "libtorrent/intrusive_ptr_base.hpp" #ifdef TORRENT_WINDOWS // windows part @@ -216,15 +215,15 @@ namespace libtorrent char stack[2048]; private: - boost::intrusive_ptr m_file; + boost::shared_ptr m_file; }; void TORRENT_EXTRA_EXPORT print_open_files(char const* event, char const* name); #else -typedef boost::intrusive_ptr file_handle; + typedef boost::shared_ptr file_handle; #endif - struct TORRENT_EXTRA_EXPORT file: boost::noncopyable, intrusive_ptr_base + struct TORRENT_EXTRA_EXPORT file: boost::noncopyable { // the open mode for files. Used for the file constructor or // file::open(). diff --git a/include/libtorrent/lsd.hpp b/include/libtorrent/lsd.hpp index a4f0e65ed..68b2566cc 100644 --- a/include/libtorrent/lsd.hpp +++ b/include/libtorrent/lsd.hpp @@ -36,12 +36,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/peer_id.hpp" #include "libtorrent/broadcast_socket.hpp" -#include "libtorrent/intrusive_ptr_base.hpp" #include "libtorrent/deadline_timer.hpp" #include #include #include +#include #if defined TORRENT_LOGGING #include @@ -52,19 +52,21 @@ namespace libtorrent typedef boost::function peer_callback_t; -class lsd : public intrusive_ptr_base +class lsd : public boost::enable_shared_from_this { public: lsd(io_service& ios, peer_callback_t const& cb); ~lsd(); -// void rebind(address const& listen_interface); + void start(); void announce(sha1_hash const& ih, int listen_port, bool broadcast = false); void close(); private: + boost::shared_ptr self() { return shared_from_this(); } + void announce_impl(sha1_hash const& ih, int listen_port , bool broadcast, int retry_count); void resend_announce(error_code const& e, sha1_hash const& ih diff --git a/include/libtorrent/natpmp.hpp b/include/libtorrent/natpmp.hpp index 81c919824..a818837d5 100644 --- a/include/libtorrent/natpmp.hpp +++ b/include/libtorrent/natpmp.hpp @@ -38,11 +38,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/address.hpp" #include "libtorrent/thread.hpp" #include "libtorrent/error_code.hpp" -#include "libtorrent/intrusive_ptr_base.hpp" #include "libtorrent/deadline_timer.hpp" #include #include +#include namespace libtorrent { @@ -53,14 +53,13 @@ namespace libtorrent typedef boost::function portmap_callback_t; typedef boost::function log_callback_t; -class natpmp : public intrusive_ptr_base +class natpmp : public boost::enable_shared_from_this { public: - natpmp(io_service& ios, address const& listen_interface - , portmap_callback_t const& cb + natpmp(io_service& ios, portmap_callback_t const& cb , log_callback_t const& lcb); - void rebind(address const& listen_interface); + void start(); // maps the ports, if a port is set to 0 // it will not be mapped @@ -73,6 +72,8 @@ public: private: + boost::shared_ptr self() { return shared_from_this(); } + void update_mapping(int i, mutex::scoped_lock& l); void send_map_request(int i, mutex::scoped_lock& l); void send_get_ip_address_request(mutex::scoped_lock& l); diff --git a/include/libtorrent/peer_class.hpp b/include/libtorrent/peer_class.hpp index 9da4952cc..0d376b2b6 100644 --- a/include/libtorrent/peer_class.hpp +++ b/include/libtorrent/peer_class.hpp @@ -33,13 +33,12 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_PEER_CLASS_HPP_INCLUDED #define TORRENT_PEER_CLASS_HPP_INCLUDED -#include "libtorrent/intrusive_ptr_base.hpp" #include "libtorrent/bandwidth_limit.hpp" #include "libtorrent/assert.hpp" #include #include -#include +#include #include namespace libtorrent @@ -83,7 +82,7 @@ namespace libtorrent int download_priority; }; - struct TORRENT_EXTRA_EXPORT peer_class : intrusive_ptr_base + struct TORRENT_EXTRA_EXPORT peer_class : boost::enable_shared_from_this { friend struct peer_class_pool; @@ -136,7 +135,7 @@ namespace libtorrent // state for peer classes (a peer can belong to multiple classes) // this can control - std::vector > m_peer_classes; + std::vector > m_peer_classes; // indices in m_peer_classes that are no longer used std::vector m_free_list; diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 4389493f6..e3e39e747 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -48,7 +48,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include #include #include #include diff --git a/include/libtorrent/settings_pack.hpp b/include/libtorrent/settings_pack.hpp index 8cd14f8a4..9f5c348bb 100644 --- a/include/libtorrent/settings_pack.hpp +++ b/include/libtorrent/settings_pack.hpp @@ -55,7 +55,6 @@ namespace libtorrent TORRENT_EXTRA_EXPORT settings_pack* load_pack_from_dict(lazy_entry const* settings); TORRENT_EXTRA_EXPORT void save_settings_to_dict(aux::session_settings const& s, entry::dictionary_type& sett); - TORRENT_EXPORT void initialize_default_settings(aux::session_settings& s); TORRENT_EXTRA_EXPORT void apply_pack(settings_pack const* pack, aux::session_settings& sett, aux::session_impl* ses = 0); TORRENT_EXPORT int setting_by_name(std::string const& name); diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 1aa7f585a..30fabefde 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -46,7 +46,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include #include diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index 25c700e06..204346e7d 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -37,7 +37,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/error_code.hpp" #include "libtorrent/broadcast_socket.hpp" #include "libtorrent/http_connection.hpp" -#include "libtorrent/intrusive_ptr_base.hpp" #include "libtorrent/thread.hpp" #include "libtorrent/deadline_timer.hpp" #include "libtorrent/enum_net.hpp" @@ -47,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include namespace libtorrent @@ -106,15 +106,17 @@ typedef boost::function portmap_call typedef boost::function log_callback_t; // TODO: support using the windows API for UPnP operations as well -class TORRENT_EXTRA_EXPORT upnp : public intrusive_ptr_base +class TORRENT_EXTRA_EXPORT upnp : public boost::enable_shared_from_this { public: upnp(io_service& ios , address const& listen_interface, std::string const& user_agent , portmap_callback_t const& cb, log_callback_t const& lcb - , bool ignore_nonrouters, void* state = 0); + , bool ignore_nonrouters); ~upnp(); + void start(void* state = 0); + void* drain_state(); enum protocol_type { none = 0, udp = 1, tcp = 2 }; @@ -157,6 +159,8 @@ public: private: + boost::shared_ptr self() { return shared_from_this(); } + void map_timer(error_code const& ec); void try_map_upnp(mutex::scoped_lock& l, bool timer = false); void discover_device_impl(mutex::scoped_lock& l); diff --git a/src/Makefile.am b/src/Makefile.am index 7f8654294..618644c9f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -104,6 +104,7 @@ libtorrent_rasterbar_la_SOURCES = \ session.cpp \ session_call.cpp \ session_impl.cpp \ + session_settings.cpp \ settings_pack.cpp \ sha1.cpp \ smart_ban.cpp \ diff --git a/src/broadcast_socket.cpp b/src/broadcast_socket.cpp index e44dcb722..5ef4d5428 100644 --- a/src/broadcast_socket.cpp +++ b/src/broadcast_socket.cpp @@ -226,10 +226,8 @@ namespace libtorrent } broadcast_socket::broadcast_socket( - udp::endpoint const& multicast_endpoint - , receive_handler_t const& handler) + udp::endpoint const& multicast_endpoint) : m_multicast_endpoint(multicast_endpoint) - , m_on_receive(handler) , m_outstanding_operations(0) , m_abort(false) { @@ -238,8 +236,11 @@ namespace libtorrent using namespace asio::ip::multicast; } - void broadcast_socket::open(io_service& ios, error_code& ec, bool loopback) + void broadcast_socket::open(receive_handler_t const& handler + , io_service& ios, error_code& ec, bool loopback) { + m_on_receive = handler; + std::vector interfaces = enum_net_interfaces(ios, ec); #if TORRENT_USE_IPV6 @@ -279,11 +280,11 @@ namespace libtorrent #endif open_multicast_socket(ios, i->interface_address, loopback, ec); #ifdef TORRENT_DEBUG -// fprintf(stderr, "broadcast socket [ if: %s group: %s mask: %s ] %s\n" -// , i->interface_address.to_string().c_str() -// , m_multicast_endpoint.address().to_string().c_str() -// , i->netmask.to_string().c_str() -// , ec.message().c_str()); + fprintf(stderr, "broadcast socket [ if: %s group: %s mask: %s ] %s\n" + , i->interface_address.to_string().c_str() + , m_multicast_endpoint.address().to_string().c_str() + , i->netmask.to_string().c_str() + , ec.message().c_str()); #endif open_unicast_socket(ios, i->interface_address , i->netmask.is_v4() ? i->netmask.to_v4() : address_v4()); diff --git a/src/file_pool.cpp b/src/file_pool.cpp index 2bf74db99..759c0fd80 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -163,7 +163,7 @@ namespace libtorrent // be outstanding operations on it, we can't close the // file, we can only delete our reference to it. // if this is the only reference to the file, it will be closed - e.file_ptr.reset(new (std::nothrow)file); + e.file_ptr = boost::make_shared(); std::string full_path = fs.file_path(file_index, p); if (!e.file_ptr->open(full_path, m, ec)) @@ -183,7 +183,7 @@ namespace libtorrent } lru_file_entry e; - e.file_ptr.reset(new (std::nothrow)file); + e.file_ptr = boost::make_shared(); if (!e.file_ptr) { ec = error_code(ENOMEM, get_posix_category()); @@ -307,7 +307,7 @@ namespace libtorrent for (file_set::const_iterator i = m_files.begin(); i != m_files.end(); ++i) { - if (i->second.key == st && i->second.file_ptr->refcount() > 1) + if (i->second.key == st && !i->second.file_ptr.unique()) return false; } return true; diff --git a/src/lsd.cpp b/src/lsd.cpp index 1d20833fd..429b9a2b6 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -67,11 +67,9 @@ static error_code ec; lsd::lsd(io_service& ios, peer_callback_t const& cb) : m_callback(cb) - , m_socket(udp::endpoint(address_v4::from_string("239.192.152.143", ec), 6771) - , boost::bind(&lsd::on_announce, self(), _1, _2, _3)) + , m_socket(udp::endpoint(address_v4::from_string("239.192.152.143", ec), 6771)) #if TORRENT_USE_IPV6 - , m_socket6(udp::endpoint(address_v6::from_string("ff15::efc0:988f", ec), 6771) - , boost::bind(&lsd::on_announce, self(), _1, _2, _3)) + , m_socket6(udp::endpoint(address_v6::from_string("ff15::efc0:988f", ec), 6771)) #endif , m_broadcast_timer(ios) , m_cookie(random()) @@ -79,9 +77,16 @@ lsd::lsd(io_service& ios, peer_callback_t const& cb) #if TORRENT_USE_IPV6 , m_disabled6(false) #endif +#if defined TORRENT_LOGGING + , m_log(NULL) +#endif +{ +} + +void lsd::start() { #if defined TORRENT_LOGGING - // TODO: instead if writing to a file, post alerts. Or call a log callback + // TODO: 3 instead if writing to a file, post alerts. Or call a log callback m_log = fopen("lsd.log", "w+"); if (m_log == NULL) { @@ -91,7 +96,8 @@ lsd::lsd(io_service& ios, peer_callback_t const& cb) #endif error_code ec; - m_socket.open(ios, ec); + m_socket.open(boost::bind(&lsd::on_announce, self(), _1, _2, _3) + , m_broadcast_timer.get_io_service(), ec); #if defined TORRENT_LOGGING if (ec) @@ -102,7 +108,8 @@ lsd::lsd(io_service& ios, peer_callback_t const& cb) #endif #if TORRENT_USE_IPV6 - m_socket6.open(ios, ec); + m_socket6.open(boost::bind(&lsd::on_announce, self(), _1, _2, _3) + , m_broadcast_timer.get_io_service(), ec); #if defined TORRENT_LOGGING if (ec) { diff --git a/src/natpmp.cpp b/src/natpmp.cpp index 5f12f49f1..c404003af 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -63,7 +63,7 @@ POSSIBILITY OF SUCH DAMAGE. using namespace libtorrent; -natpmp::natpmp(io_service& ios, address const& listen_interface +natpmp::natpmp(io_service& ios , portmap_callback_t const& cb, log_callback_t const& lcb) : m_callback(cb) , m_log_callback(lcb) @@ -80,10 +80,9 @@ natpmp::natpmp(io_service& ios, address const& listen_interface // for this array not to be reallocated, by passing // around pointers to its elements. so reserve size for now m_mappings.reserve(10); - rebind(listen_interface); } -void natpmp::rebind(address const& listen_interface) +void natpmp::start() { mutex::scoped_lock l(m_mutex); diff --git a/src/peer_class.cpp b/src/peer_class.cpp index 105ad53ba..c6dc22406 100644 --- a/src/peer_class.cpp +++ b/src/peer_class.cpp @@ -88,11 +88,11 @@ namespace libtorrent else { ret = m_peer_classes.size(); - m_peer_classes.push_back(boost::intrusive_ptr()); + m_peer_classes.push_back(boost::shared_ptr()); } TORRENT_ASSERT(m_peer_classes[ret].get() == 0); - m_peer_classes[ret] = new peer_class(label); + m_peer_classes[ret] = boost::make_shared(label); return ret; } diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 84eb54d9e..1b879bda6 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -3163,7 +3163,7 @@ namespace libtorrent i->locked = true; } - // TODO: 3 it would be nice if this could be folded into lock_piece() + // TODO: 2 it would be nice if this could be folded into lock_piece() // the main distinction is that this also maintains the m_num_passed // counter and the passed_hash_check member void piece_picker::write_failed(piece_block block) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 5f1b74fdf..5f12b913e 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1989,7 +1989,7 @@ retry: void session_impl::remap_tcp_ports(boost::uint32_t mask, int tcp_port, int ssl_port) { - if ((mask & 1) && m_natpmp.get()) + if ((mask & 1) && m_natpmp) { if (m_tcp_mapping[0] != -1) m_natpmp->delete_mapping(m_tcp_mapping[0]); m_tcp_mapping[0] = m_natpmp->add_mapping(natpmp::tcp, tcp_port, tcp_port); @@ -1999,7 +1999,7 @@ retry: , ssl_port, ssl_port); #endif } - if ((mask & 2) && m_upnp.get()) + if ((mask & 2) && m_upnp) { if (m_tcp_mapping[1] != -1) m_upnp->delete_mapping(m_tcp_mapping[1]); m_tcp_mapping[1] = m_upnp->add_mapping(upnp::tcp, tcp_port, tcp_port); @@ -2841,7 +2841,8 @@ retry: if (now - m_last_second_tick < seconds(1)) return; #ifndef TORRENT_DISABLE_DHT - if (m_dht_interval_update_torrents < 40 + if (m_dht + && m_dht_interval_update_torrents < 40 && m_dht_interval_update_torrents != int(m_torrents.size())) update_dht_announce_interval(); #endif @@ -5135,7 +5136,7 @@ retry: void session_impl::announce_lsd(sha1_hash const& ih, int port, bool broadcast) { // use internal listen port for local peers - if (m_lsd.get()) + if (m_lsd) m_lsd->announce(ih, port, broadcast); } @@ -5514,7 +5515,7 @@ retry: void session_impl::maybe_update_udp_mapping(int nat, int local_port, int external_port) { int local, external, protocol; - if (nat == 0 && m_natpmp.get()) + if (nat == 0 && m_natpmp) { if (m_udp_mapping[nat] != -1) { @@ -5530,7 +5531,7 @@ retry: , local_port, external_port); return; } - else if (nat == 1 && m_upnp.get()) + else if (nat == 1 && m_upnp) { if (m_udp_mapping[nat] != -1) { @@ -6206,7 +6207,7 @@ retry: if (m_lsd) return; - m_lsd = new lsd(m_io_service + m_lsd = boost::make_shared(m_io_service , boost::bind(&session_impl::on_lsd_peer, this, _1, _2)); } @@ -6218,15 +6219,12 @@ retry: // the natpmp constructor may fail and call the callbacks // into the session_impl. - natpmp* n = new (std::nothrow) natpmp(m_io_service - , m_listen_interface.address() + m_natpmp = boost::make_shared(m_io_service , boost::bind(&session_impl::on_port_mapping , this, _1, _2, _3, _4, 0) , boost::bind(&session_impl::on_port_map_log , this, _1, 0)); - if (n == 0) return 0; - - m_natpmp = n; + m_natpmp->start(); int ssl_port = ssl_listen_port(); @@ -6246,7 +6244,7 @@ retry: , ssl_port, ssl_port); } #endif - return n; + return m_natpmp.get(); } upnp* session_impl::start_upnp() @@ -6256,7 +6254,7 @@ retry: if (m_upnp) return m_upnp.get(); // the upnp constructor may fail and call the callbacks - upnp* u = new (std::nothrow) upnp(m_io_service + m_upnp = boost::make_shared(m_io_service , m_listen_interface.address() , m_settings.get_str(settings_pack::user_agent) , boost::bind(&session_impl::on_port_mapping @@ -6264,10 +6262,7 @@ retry: , boost::bind(&session_impl::on_port_map_log , this, _1, 1) , m_settings.get_bool(settings_pack::upnp_ignore_nonrouters)); - - if (u == 0) return 0; - - m_upnp = u; + m_upnp->start(); int ssl_port = ssl_listen_port(); @@ -6288,7 +6283,7 @@ retry: , ssl_port, ssl_port); } #endif - return u; + return m_upnp.get(); } int session_impl::add_port_mapping(int t, int external_port @@ -6310,14 +6305,14 @@ retry: void session_impl::stop_lsd() { - if (m_lsd.get()) + if (m_lsd) m_lsd->close(); - m_lsd = 0; + m_lsd.reset(); } void session_impl::stop_natpmp() { - if (m_natpmp.get()) + if (m_natpmp) { m_natpmp->close(); m_udp_mapping[0] = -1; @@ -6327,12 +6322,12 @@ retry: m_ssl_udp_mapping[0] = -1; #endif } - m_natpmp = 0; + m_natpmp.reset(); } void session_impl::stop_upnp() { - if (m_upnp.get()) + if (m_upnp) { m_upnp->close(); m_udp_mapping[1] = -1; @@ -6342,7 +6337,7 @@ retry: m_ssl_udp_mapping[1] = -1; #endif } - m_upnp = 0; + m_upnp.reset(); } external_ip const& session_impl::external_address() const diff --git a/include/libtorrent/intrusive_ptr_base.hpp b/src/session_settings.cpp similarity index 54% rename from include/libtorrent/intrusive_ptr_base.hpp rename to src/session_settings.cpp index db268d0c9..75f751143 100644 --- a/include/libtorrent/intrusive_ptr_base.hpp +++ b/src/session_settings.cpp @@ -1,6 +1,6 @@ /* -Copyright (c) 2007-2014, Arvid Norberg +Copyright (c) 2015, Arvid Norberg All rights reserved. Redistribution and use in source and binary forms, with or without @@ -30,57 +30,13 @@ POSSIBILITY OF SUCH DAMAGE. */ -#ifndef TORRENT_INTRUSIVE_PTR_BASE -#define TORRENT_INTRUSIVE_PTR_BASE +#include "libtorrent/aux_/session_settings.hpp" -#include -#include -#include "libtorrent/config.hpp" -#include "libtorrent/assert.hpp" -#include - -namespace libtorrent +namespace libtorrent { namespace aux { - // TODO: 2 remove this class and transition over to using shared_ptr and - // make_shared instead - template - struct intrusive_ptr_base + session_settings::session_settings() { - intrusive_ptr_base(intrusive_ptr_base const&) - : m_refs(0) {} - - friend void intrusive_ptr_add_ref(intrusive_ptr_base const* s) - { - TORRENT_ASSERT(s != 0); - TORRENT_ASSERT(s->m_refs >= 0); - ++s->m_refs; - } - - friend void intrusive_ptr_release(intrusive_ptr_base const* s) - { - TORRENT_ASSERT(s != 0); - TORRENT_ASSERT(s->m_refs > 0); - if (--s->m_refs == 0) - boost::checked_delete(static_cast(s)); - } - - boost::intrusive_ptr self() - { return boost::intrusive_ptr((T*)this); } - - boost::intrusive_ptr self() const - { return boost::intrusive_ptr((T const*)this); } - - int refcount() const { return m_refs; } - - intrusive_ptr_base(): m_refs(0) {} - - private: - - // reference counter for intrusive_ptr - mutable boost::atomic m_refs; - }; - -} - -#endif + initialize_default_settings(*this); + } +} } diff --git a/src/torrent.cpp b/src/torrent.cpp index 8ffdd770b..c1c0bf261 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -7710,7 +7710,7 @@ namespace libtorrent // if it's lower rank than the incoming connection peer_connection* peer = find_lowest_ranking_peer(); - // TODO: 3 if peer is a really good peer, maybe we shouldn't disconnect it + // TODO: 2 if peer is a really good peer, maybe we shouldn't disconnect it if (peer && peer->peer_rank() < p->peer_rank()) { peer->disconnect(errors::too_many_connections, peer_connection_interface::op_bittorrent); diff --git a/src/upnp.cpp b/src/upnp.cpp index 55fd9a82c..077b3948f 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -72,15 +72,14 @@ static error_code ec; upnp::upnp(io_service& ios , address const& listen_interface, std::string const& user_agent , portmap_callback_t const& cb, log_callback_t const& lcb - , bool ignore_nonrouters, void* state) + , bool ignore_nonrouters) : m_user_agent(user_agent) , m_callback(cb) , m_log_callback(lcb) , m_retry_count(0) , m_io_service(ios) , m_resolver(ios) - , m_socket(udp::endpoint(address_v4::from_string("239.255.255.250", ec), 1900) - , boost::bind(&upnp::on_reply, self(), _1, _2, _3)) + , m_socket(udp::endpoint(address_v4::from_string("239.255.255.250", ec), 1900)) , m_broadcast_timer(ios) , m_refresh_timer(ios) , m_map_timer(ios) @@ -90,9 +89,13 @@ upnp::upnp(io_service& ios , m_last_if_update(min_time()) { TORRENT_ASSERT(cb); +} +void upnp::start(void* state) +{ error_code ec; - m_socket.open(ios, ec); + m_socket.open(boost::bind(&upnp::on_reply, self(), _1, _2, _3) + , m_refresh_timer.get_io_service(), ec); if (state) { @@ -275,7 +278,7 @@ void upnp::resend_request(error_code const& ec) #endif if (ec) return; - boost::intrusive_ptr me(self()); + boost::shared_ptr me(self()); mutex::scoped_lock l(m_mutex); @@ -330,7 +333,7 @@ void upnp::resend_request(error_code const& ec) void upnp::on_reply(udp::endpoint const& from, char* buffer , std::size_t bytes_transferred) { - boost::intrusive_ptr me(self()); + boost::shared_ptr me(self()); mutex::scoped_lock l(m_mutex); @@ -742,7 +745,7 @@ void upnp::update_map(rootdevice& d, int i, mutex::scoped_lock& l) if (d.upnp_connection) return; - boost::intrusive_ptr me(self()); + boost::shared_ptr me(self()); mapping_t& m = d.mapping[i]; @@ -920,7 +923,7 @@ void upnp::on_upnp_xml(error_code const& e , libtorrent::http_parser const& p, rootdevice& d , http_connection& c) { - boost::intrusive_ptr me(self()); + boost::shared_ptr me(self()); mutex::scoped_lock l(m_mutex); @@ -1227,7 +1230,7 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e , libtorrent::http_parser const& p, rootdevice& d , http_connection& c) { - boost::intrusive_ptr me(self()); + boost::shared_ptr me(self()); mutex::scoped_lock l(m_mutex); @@ -1307,7 +1310,7 @@ void upnp::on_upnp_map_response(error_code const& e , libtorrent::http_parser const& p, rootdevice& d, int mapping , http_connection& c) { - boost::intrusive_ptr me(self()); + boost::shared_ptr me(self()); mutex::scoped_lock l(m_mutex); @@ -1475,7 +1478,7 @@ void upnp::on_upnp_unmap_response(error_code const& e , libtorrent::http_parser const& p, rootdevice& d, int mapping , http_connection& c) { - boost::intrusive_ptr me(self()); + boost::shared_ptr me(self()); mutex::scoped_lock l(m_mutex); diff --git a/test/test_dht.cpp b/test/test_dht.cpp index f4dc072a1..a343c77ce 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -433,7 +433,7 @@ bool get_item_cb(dht::item& i) return false; } -// TODO: 3 test obfuscated_get_peers +// TODO: 2 test obfuscated_get_peers int test_main() { dht_settings sett; diff --git a/test/test_transfer.cpp b/test/test_transfer.cpp index 36b14c3ff..572b1da62 100644 --- a/test/test_transfer.cpp +++ b/test/test_transfer.cpp @@ -285,7 +285,7 @@ void test_transfer(int proxy_type, settings_pack const& sett // us catch all events that failed (and would put the torrent // back into upload mode) before we restart it. - // TODO: 3 factor out the disk-full test into its own unit test + // TODO: 2 factor out the disk-full test into its own unit test if (test_disk_full && st2.upload_mode && ++upload_mode_timer > 10) { test_disk_full = false; diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index 33f6061bf..992238b52 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -38,7 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include +#include #include using namespace libtorrent; @@ -144,15 +144,17 @@ int run_upnp_test(char const* root_filename, char const* router_model, char cons xml.write(soap_add_response, sizeof(soap_add_response)-1); xml.close(); - sock = new broadcast_socket(udp::endpoint(address_v4::from_string("239.255.255.250"), 1900) - , &incoming_msearch); + sock = new broadcast_socket(udp::endpoint(address_v4::from_string("239.255.255.250") + , 1900)); - sock->open(ios, ec); + sock->open(&incoming_msearch, ios, ec); std::string user_agent = "test agent"; - boost::intrusive_ptr upnp_handler = new upnp(ios, address_v4::from_string("127.0.0.1") + boost::shared_ptr upnp_handler = boost::make_shared(ios + , address_v4::from_string("127.0.0.1") , user_agent, &callback, &log_callback, false); + upnp_handler->start(); upnp_handler->discover_device(); for (int i = 0; i < 20; ++i)