PEX optimization

This commit is contained in:
Arvid Norberg 2008-11-26 08:27:57 +00:00
parent 3fd5405fa9
commit 5ed3daa49a
1 changed files with 14 additions and 2 deletions

View File

@ -72,7 +72,7 @@ namespace libtorrent { namespace
struct ut_pex_plugin: torrent_plugin struct ut_pex_plugin: torrent_plugin
{ {
ut_pex_plugin(torrent& t): m_torrent(t), m_1_minute(55) {} ut_pex_plugin(torrent& t): m_torrent(t), m_1_minute(55), m_peers_in_message(0) {}
virtual boost::shared_ptr<peer_plugin> new_connection(peer_connection* pc); virtual boost::shared_ptr<peer_plugin> new_connection(peer_connection* pc);
@ -81,6 +81,11 @@ namespace libtorrent { namespace
return m_ut_pex_msg; return m_ut_pex_msg;
} }
int peers_in_msg() const
{
return m_peers_in_message;
}
// the second tick of the torrent // the second tick of the torrent
// each minute the new lists of "added" + "added.f" and "dropped" // each minute the new lists of "added" + "added.f" and "dropped"
// are calculated here and the pex message is created // are calculated here and the pex message is created
@ -109,6 +114,7 @@ namespace libtorrent { namespace
std::set<tcp::endpoint> dropped; std::set<tcp::endpoint> dropped;
m_old_peers.swap(dropped); m_old_peers.swap(dropped);
m_peers_in_message = 0;
int num_added = 0; int num_added = 0;
for (torrent::peer_iterator i = m_torrent.begin() for (torrent::peer_iterator i = m_torrent.begin()
, end(m_torrent.end()); i != end; ++i) , end(m_torrent.end()); i != end; ++i)
@ -148,6 +154,7 @@ namespace libtorrent { namespace
detail::write_uint8(flags, plf6_out); detail::write_uint8(flags, plf6_out);
} }
++num_added; ++num_added;
++m_peers_in_message;
} }
else else
{ {
@ -164,6 +171,7 @@ namespace libtorrent { namespace
detail::write_endpoint(*i, pld_out); detail::write_endpoint(*i, pld_out);
else else
detail::write_endpoint(*i, pld6_out); detail::write_endpoint(*i, pld6_out);
++m_peers_in_message;
} }
m_ut_pex_msg.clear(); m_ut_pex_msg.clear();
@ -176,6 +184,7 @@ namespace libtorrent { namespace
std::set<tcp::endpoint> m_old_peers; std::set<tcp::endpoint> m_old_peers;
int m_1_minute; int m_1_minute;
std::vector<char> m_ut_pex_msg; std::vector<char> m_ut_pex_msg;
int m_peers_in_message;
}; };
@ -224,7 +233,7 @@ namespace libtorrent { namespace
lazy_entry pex_msg; lazy_entry pex_msg;
int ret = lazy_bdecode(body.begin, body.end, pex_msg); int ret = lazy_bdecode(body.begin, body.end, pex_msg);
if (pex_msg.type() != lazy_entry::dict_t) if (ret != 0 || pex_msg.type() != lazy_entry::dict_t)
{ {
m_pc.disconnect("invalid bencoding in ut_metadata message", 2); m_pc.disconnect("invalid bencoding in ut_metadata message", 2);
return true; return true;
@ -300,6 +309,9 @@ namespace libtorrent { namespace
void send_ut_peer_diff() void send_ut_peer_diff()
{ {
// if there's no change in out peer set, don't send anything
if (m_tp.peers_in_msg() == 0) return;
std::vector<char> const& pex_msg = m_tp.get_ut_pex_msg(); std::vector<char> const& pex_msg = m_tp.get_ut_pex_msg();
buffer::interval i = m_pc.allocate_send_buffer(6 + pex_msg.size()); buffer::interval i = m_pc.allocate_send_buffer(6 + pex_msg.size());