diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index d02e005b3..db46ff99a 100755 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -102,6 +102,8 @@ namespace libtorrent , boost::shared_ptr s , policy::peer* peerinfo); + void start(); + ~bt_peer_connection(); #ifndef TORRENT_DISABLE_ENCRYPTION diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 1ed0712c7..1ebac5894 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -122,6 +122,12 @@ namespace libtorrent , boost::shared_ptr s , policy::peer* peerinfo); + // this function is called after it has been constructed and properly + // reference counted. It is safe to call self() in this function + // and schedule events with references to itself (that is not safe to + // do in the constructor). + virtual void start(); + virtual ~peer_connection(); void set_peer_info(policy::peer* pi) @@ -515,6 +521,14 @@ namespace libtorrent char m_country[2]; #endif +#ifndef NDEBUG + boost::intrusive_ptr self() + { + TORRENT_ASSERT(!m_in_constructor); + return intrusive_ptr_base::self(); + } +#endif + private: void fill_send_buffer(); diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index b103309c3..74499cc3c 100755 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -163,13 +163,19 @@ namespace libtorrent m_bandwidth_limit[upload_channel].assign(80); #endif +#ifndef NDEBUG + m_in_constructor = false; +#endif + } + + void bt_peer_connection::start() + { + peer_connection::start(); + // start in the state where we are trying to read the // handshake from the other side reset_recv_buffer(20); setup_receive(); -#ifndef NDEBUG - m_in_constructor = false; -#endif } bt_peer_connection::~bt_peer_connection() diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 1a2ccd8f0..a6a486869 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -140,12 +140,7 @@ namespace libtorrent piece_failed = false; #endif - boost::shared_ptr t = m_torrent.lock(); - TORRENT_ASSERT(t); std::fill(m_peer_id.begin(), m_peer_id.end(), 0); - - if (t->ready_for_connections()) - init(); } // incoming connection @@ -269,6 +264,14 @@ namespace libtorrent m_downloaded_at_last_unchoke = m_statistics.total_payload_download(); } + void peer_connection::start() + { + boost::shared_ptr t = m_torrent.lock(); + + if (t && t->ready_for_connections()) + init(); + } + void peer_connection::update_interest() { INVARIANT_CHECK; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 12bcbedc3..68ef8f2af 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -720,7 +720,11 @@ namespace aux { c->m_in_constructor = false; #endif - if (!c->is_disconnecting()) m_connections.insert(c); + if (!c->is_disconnecting()) + { + m_connections.insert(c); + c->start(); + } } catch (std::exception& exc) { diff --git a/src/torrent.cpp b/src/torrent.cpp index aed52884d..8c16bfc78 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2047,6 +2047,7 @@ namespace libtorrent #ifndef NDEBUG c->m_in_constructor = false; #endif + c->start(); #ifndef TORRENT_DISABLE_EXTENSIONS for (extension_list_t::iterator i = m_extensions.begin() @@ -2365,6 +2366,7 @@ namespace libtorrent // add the newly connected peer to this torrent's peer list m_connections.insert(boost::get_pointer(c)); m_ses.m_connections.insert(c); + c->start(); int timeout = settings().peer_connect_timeout; if (peerinfo) timeout += 3 * peerinfo->failcount;