From ec2962dd6280e0cea0635bedf2661eb40f32c8cf Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 2 Apr 2007 20:00:24 +0000 Subject: [PATCH] extensions are now instantiated for web seeds as well --- include/libtorrent/extensions.hpp | 5 +++ src/bt_peer_connection.cpp | 72 ------------------------------ src/metadata_transfer.cpp | 2 + src/peer_connection.cpp | 73 +++++++++++++++++++++++++++++++ src/session_impl.cpp | 2 + src/torrent.cpp | 9 ++++ src/ut_pex.cpp | 2 + 7 files changed, 93 insertions(+), 72 deletions(-) diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 8514774b4..5f8172649 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -88,6 +88,7 @@ namespace libtorrent virtual ~peer_plugin() {} // can add entries to the extension handshake + // this is not called for web seeds virtual void add_handshake(entry&) {} // throwing an exception from any of the handlers (except add_handshake) @@ -96,12 +97,14 @@ namespace libtorrent // this is called when the initial BT handshake is received. Returning false // means that the other end doesn't support this extension and will remove // it from the list of plugins. + // this is not called for web seeds virtual bool on_handshake() { return true; } // called when the extension handshake from the other end is received // if this returns false, it means that this extension isn't // supported by this peer. It will result in this peer_plugin // being removed from the peer_connection and destructed. + // this is not called for web seeds virtual bool on_extension_handshake(entry const& h) { return true; } // returning true from any of the message handlers @@ -141,10 +144,12 @@ namespace libtorrent // the message is not processed by any other plugin and if false // is returned the next plugin in the chain will receive it to // be able to handle it + // this is not called for web seeds virtual bool on_extended(int length , int msg, buffer::const_interval body) { return false; } + // this is not called for web seeds virtual bool on_unknown_message(int length, int msg , buffer::const_interval body) { return false; } diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 8b4f02904..75547ee48 100755 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -373,14 +373,6 @@ namespace libtorrent m_statistics.received_bytes(0, received); if (!packet_finished()) return; -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_choke()) return; - } -#endif - incoming_choke(); } @@ -398,14 +390,6 @@ namespace libtorrent m_statistics.received_bytes(0, received); if (!packet_finished()) return; -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_unchoke()) return; - } -#endif - incoming_unchoke(); } @@ -423,14 +407,6 @@ namespace libtorrent m_statistics.received_bytes(0, received); if (!packet_finished()) return; -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_interested()) return; - } -#endif - incoming_interested(); } @@ -448,14 +424,6 @@ namespace libtorrent m_statistics.received_bytes(0, received); if (!packet_finished()) return; -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_not_interested()) return; - } -#endif - incoming_not_interested(); } @@ -478,14 +446,6 @@ namespace libtorrent const char* ptr = recv_buffer.begin + 1; int index = detail::read_int32(ptr); -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_have(index)) return; - } -#endif - incoming_have(index); } @@ -527,14 +487,6 @@ namespace libtorrent for (int i = 0; i < (int)bitfield.size(); ++i) bitfield[i] = (recv_buffer[1 + (i>>3)] & (1 << (7 - (i&7)))) != 0; -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_bitfield(bitfield)) return; - } -#endif - incoming_bitfield(bitfield); } @@ -560,14 +512,6 @@ namespace libtorrent r.start = detail::read_int32(ptr); r.length = detail::read_int32(ptr); -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_request(r)) return; - } -#endif - incoming_request(r); } @@ -612,14 +556,6 @@ namespace libtorrent p.start = detail::read_int32(ptr); p.length = packet_size() - 9; -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_piece(p, recv_buffer.begin + 9)) return; - } -#endif - incoming_piece(p, recv_buffer.begin + 9); } @@ -645,14 +581,6 @@ namespace libtorrent r.start = detail::read_int32(ptr); r.length = detail::read_int32(ptr); -#ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - if ((*i)->on_cancel(r)) return; - } -#endif - incoming_cancel(r); } diff --git a/src/metadata_transfer.cpp b/src/metadata_transfer.cpp index 0bb2623df..12743d93e 100644 --- a/src/metadata_transfer.cpp +++ b/src/metadata_transfer.cpp @@ -487,6 +487,8 @@ namespace libtorrent { namespace boost::shared_ptr metadata_plugin::new_connection( peer_connection* pc) { + bt_peer_connection* c = dynamic_cast(pc); + if (!c) return boost::shared_ptr(); return boost::shared_ptr(new metadata_peer_plugin(m_torrent, *pc, *this)); } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index bdf773bdf..1f6022f2e 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -532,6 +532,14 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_choke()) return; + } +#endif + #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; (*m_logger) << to_simple_string(second_clock::universal_time()) @@ -574,6 +582,14 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_unchoke()) return; + } +#endif + #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; (*m_logger) << to_simple_string(second_clock::universal_time()) @@ -594,6 +610,14 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_interested()) return; + } +#endif + #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; (*m_logger) << to_simple_string(second_clock::universal_time()) @@ -611,6 +635,14 @@ namespace libtorrent { INVARIANT_CHECK; +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_not_interested()) return; + } +#endif + m_became_uninterested = second_clock::universal_time(); // clear the request queue if the client isn't interested @@ -641,6 +673,14 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_have(index)) return; + } +#endif + #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; (*m_logger) << to_simple_string(second_clock::universal_time()) @@ -700,6 +740,14 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_bitfield(bitfield)) return; + } +#endif + #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; (*m_logger) << to_simple_string(second_clock::universal_time()) @@ -784,6 +832,14 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_request(r)) return; + } +#endif + if (!t->valid_metadata()) { // if we don't have valid metadata yet, @@ -922,6 +978,15 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); assert(t); + +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_piece(p, data)) return; + } +#endif + #ifndef NDEBUG check_postcondition post_checker_(t); t->check_invariant(); @@ -1173,6 +1238,14 @@ namespace libtorrent { INVARIANT_CHECK; +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + if ((*i)->on_cancel(r)) return; + } +#endif + #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; (*m_logger) << to_simple_string(second_clock::universal_time()) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index b975384a8..12fa4b674 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1693,6 +1693,8 @@ namespace libtorrent { namespace detail m_thread->join(); + assert(m_torrents.empty()); + // it's important that the main thread is closed completely before // the checker thread is terminated. Because all the connections // have to be closed and removed from the torrents before they diff --git a/src/torrent.cpp b/src/torrent.cpp index 60ec6fc8f..8b17a0c3d 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1488,6 +1488,15 @@ namespace libtorrent c->m_in_constructor = false; #endif +#ifndef TORRENT_DISABLE_EXTENSIONS + for (extension_list_t::iterator i = m_extensions.begin() + , end(m_extensions.end()); i != end; ++i) + { + boost::shared_ptr pp((*i)->new_connection(c.get())); + if (pp) c->add_extension(pp); + } +#endif + try { m_ses.m_connection_queue.push_back(c); diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index 698f58f40..26c9392c1 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -321,6 +321,8 @@ namespace libtorrent { namespace boost::shared_ptr ut_pex_plugin::new_connection(peer_connection* pc) { + bt_peer_connection* c = dynamic_cast(pc); + if (!c) return boost::shared_ptr(); return boost::shared_ptr(new ut_pex_peer_plugin(m_torrent , *pc, *this)); }