From e43340c961e0e6c719e70899dc9421c14a7e7592 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 2 Nov 2009 20:43:38 +0000 Subject: [PATCH] made it possible to build without RTTI --- ChangeLog | 1 + include/libtorrent/bt_peer_connection.hpp | 14 ++---------- include/libtorrent/http_seed_connection.hpp | 2 ++ include/libtorrent/peer_connection.hpp | 9 ++++++++ include/libtorrent/web_peer_connection.hpp | 2 ++ src/lt_trackers.cpp | 6 +++-- src/metadata_transfer.cpp | 25 +++++---------------- src/policy.cpp | 4 ++-- src/torrent.cpp | 23 ++++++++++++++----- src/ut_metadata.cpp | 6 +++-- src/ut_pex.cpp | 18 ++++++++++----- 11 files changed, 60 insertions(+), 50 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9fc96304b..ade3eaffb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ * replaced dependency on boost.thread by asio's internal thread primitives * added support for i2p torrents * cleaned up usage of MAX_PATH and related macros + * made it possible to build libtorrent without RTTI support 0.15 release diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index 6d9d72e72..06c5250b3 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -111,6 +111,8 @@ namespace libtorrent { return m_encrypted; } #endif + virtual int type() const { return peer_connection::bittorrent_connection; } + enum message_type { // standard messages @@ -151,18 +153,6 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS bool support_extensions() const { return m_supports_extensions; } - - template - T* supports_extension() const - { - for (extension_list_t::const_iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) - { - T* ret = dynamic_cast(i->get()); - if (ret) return ret; - } - return 0; - } #endif // the message handlers are called diff --git a/include/libtorrent/http_seed_connection.hpp b/include/libtorrent/http_seed_connection.hpp index a3ca52cc7..5b4873090 100644 --- a/include/libtorrent/http_seed_connection.hpp +++ b/include/libtorrent/http_seed_connection.hpp @@ -101,6 +101,8 @@ namespace libtorrent ~http_seed_connection(); + virtual int type() const { return peer_connection::http_seed_connection; } + // called from the main loop when this connection has any // work to do. void on_sent(error_code const& error diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index b70ac87a7..b371203a7 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -134,6 +134,15 @@ namespace libtorrent friend class invariant_access; public: + enum connection_type + { + bittorrent_connection = 0, + url_seed_connection = 1, + http_seed_connection = 2 + }; + + virtual int type() const = 0; + enum channels { upload_channel, diff --git a/include/libtorrent/web_peer_connection.hpp b/include/libtorrent/web_peer_connection.hpp index 2d357b787..c4797cb53 100644 --- a/include/libtorrent/web_peer_connection.hpp +++ b/include/libtorrent/web_peer_connection.hpp @@ -101,6 +101,8 @@ namespace libtorrent ~web_peer_connection(); + virtual int type() const { return peer_connection::url_seed_connection; } + // called from the main loop when this connection has any // work to do. void on_sent(error_code const& error diff --git a/src/lt_trackers.cpp b/src/lt_trackers.cpp index e8fb7c92f..8c3abc3bd 100644 --- a/src/lt_trackers.cpp +++ b/src/lt_trackers.cpp @@ -327,8 +327,10 @@ namespace libtorrent { namespace boost::shared_ptr lt_tracker_plugin::new_connection( peer_connection* pc) { - bt_peer_connection* c = dynamic_cast(pc); - if (!c) return boost::shared_ptr(); + if (pc->type() != peer_connection::bittorrent_connection) + return boost::shared_ptr(); + + bt_peer_connection* c = static_cast(pc); return boost::shared_ptr(new lt_tracker_peer_plugin(m_torrent, *c, *this)); } diff --git a/src/metadata_transfer.cpp b/src/metadata_transfer.cpp index 6cb052c8b..729ff5f6f 100644 --- a/src/metadata_transfer.cpp +++ b/src/metadata_transfer.cpp @@ -527,32 +527,17 @@ 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(); + if (pc->type() != peer_connection::bittorrent_connection) + return boost::shared_ptr(); + + bt_peer_connection* c = static_cast(pc); return boost::shared_ptr(new metadata_peer_plugin(m_torrent, *pc, *this)); } std::pair metadata_plugin::metadata_request() { - // count the number of peers that supports the - // extension and that has metadata - int peers = 0; -#ifndef TORRENT_DISABLE_EXTENSIONS - for (torrent::peer_iterator i = m_torrent.begin() - , end(m_torrent.end()); i != end; ++i) - { - bt_peer_connection* c = dynamic_cast(*i); - if (c == 0) continue; - metadata_peer_plugin* p - = c->supports_extension(); - if (p == 0) continue; - if (!p->has_metadata()) continue; - ++peers; - } -#endif - // the number of blocks to request - int num_blocks = 256 / (peers + 1); + int num_blocks = 256 / 4; if (num_blocks < 1) num_blocks = 1; TORRENT_ASSERT(num_blocks <= 128); diff --git a/src/policy.cpp b/src/policy.cpp index 1bfb4d225..918bb0272 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -136,7 +136,7 @@ namespace libtorrent if (!t.are_files_checked()) return; TORRENT_ASSERT(t.valid_metadata()); - TORRENT_ASSERT(c.peer_info_struct() != 0 || !dynamic_cast(&c)); + TORRENT_ASSERT(c.peer_info_struct() != 0 || c.type() != peer_connection::bittorrent_connection); int num_requests = c.desired_queue_size() - (int)c.download_queue().size() - (int)c.request_queue().size(); @@ -1345,7 +1345,7 @@ namespace libtorrent if ((*i)->is_disconnecting()) continue; // ignore web_peer_connections since they are not managed // by the policy class - if (dynamic_cast(*i)) continue; + if ((*i)->type() != peer_connection::bittorrent_connection) continue; ++num_torrent_peers; } diff --git a/src/torrent.cpp b/src/torrent.cpp index 3f13b1c2b..84156465d 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3897,12 +3897,12 @@ namespace libtorrent , bind(&peer_connection::remote, _1) == peerinfo->ip()); #if TORRENT_USE_I2P TORRENT_ASSERT(i_ == m_connections.end() - || dynamic_cast(*i_) == 0 + || (*i_)->type() != peer_connection::bittorrent_connection || peerinfo->is_i2p_addr ); #else TORRENT_ASSERT(i_ == m_connections.end() - || dynamic_cast(*i_) == 0 + || (*i_)->type() != peer_connection::bittorrent_connection ); #endif #endif @@ -5315,10 +5315,21 @@ namespace libtorrent for (peer_iterator i = m_connections.begin(); i != m_connections.end(); ++i) { - web_peer_connection* p = dynamic_cast(*i); - if (p) web_seeds.insert(web_seed_entry(p->url(), web_seed_entry::url_seed)); - http_seed_connection* s = dynamic_cast(*i); - if (s) web_seeds.insert(web_seed_entry(s->url(), web_seed_entry::http_seed)); + switch ((*i)->type()) + { + case peer_connection::url_seed_connection: + { + web_peer_connection* p = static_cast(*i); + web_seeds.insert(web_seed_entry(p->url(), web_seed_entry::url_seed)); + break; + } + case peer_connection::http_seed_connection: + { + http_seed_connection* p = static_cast(*i); + web_seeds.insert(web_seed_entry(p->url(), web_seed_entry::http_seed)); + break; + } + } } for (std::set::iterator i = m_resolving_web_seeds.begin() diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index efd3a6f54..b2df5901c 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -406,8 +406,10 @@ namespace libtorrent { namespace boost::shared_ptr ut_metadata_plugin::new_connection( peer_connection* pc) { - bt_peer_connection* c = dynamic_cast(pc); - if (!c) return boost::shared_ptr(); + if (pc->type() != peer_connection::bittorrent_connection) + return boost::shared_ptr(); + + bt_peer_connection* c = static_cast(pc); return boost::shared_ptr(new ut_metadata_peer_plugin(m_torrent, *c, *this)); } diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index 3c0755741..dce9600c4 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -136,8 +136,10 @@ namespace libtorrent { namespace if (num_added >= max_peer_entries) break; // only send proper bittorrent peers - bt_peer_connection* p = dynamic_cast(peer); - if (!p) continue; + if (peer->type() != peer_connection::bittorrent_connection) + continue; + + bt_peer_connection* p = static_cast(peer); // no supported flags to set yet // 0x01 - peer supports encryption @@ -369,8 +371,10 @@ namespace libtorrent { namespace if (num_added >= max_peer_entries) break; // only send proper bittorrent peers - bt_peer_connection* p = dynamic_cast(peer); - if (!p) continue; + if (peer->type() != peer_connection::bittorrent_connection) + continue; + + bt_peer_connection* p = static_cast(peer); // no supported flags to set yet // 0x01 - peer supports encryption @@ -425,8 +429,10 @@ 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(); + if (pc->type() != peer_connection::bittorrent_connection) + return boost::shared_ptr(); + + bt_peer_connection* c = static_cast(pc); return boost::shared_ptr(new ut_pex_peer_plugin(m_torrent , *pc, *this)); }