Re-implemented dht_get_peers_reply_alert using the stack_allocator for peers vector storage.

This commit is contained in:
Alden Torres 2015-06-29 23:26:12 -04:00
parent cbba63ce94
commit d9246d12c4
2 changed files with 49 additions and 7 deletions

View File

@ -2362,13 +2362,20 @@ namespace libtorrent
, sha1_hash const& ih , sha1_hash const& ih
, std::vector<tcp::endpoint> const& v); , std::vector<tcp::endpoint> const& v);
const static int static_category = alert::dht_operation_notification; static const int static_category = alert::dht_operation_notification;
TORRENT_DEFINE_ALERT(dht_get_peers_reply_alert, 87) TORRENT_DEFINE_ALERT(dht_get_peers_reply_alert, 87)
virtual std::string message() const; virtual std::string message() const;
sha1_hash info_hash; sha1_hash info_hash;
std::vector<tcp::endpoint> peers;
int num_peers() const;
tcp::endpoint get_peer(int index) const;
private:
aux::stack_allocator& m_alloc;
int m_num_peers;
int m_peers_idx; // coded like a pair of (index to actual endpoint bytes, size of endpoint)
}; };
#undef TORRENT_DEFINE_ALERT_IMPL #undef TORRENT_DEFINE_ALERT_IMPL

View File

@ -1777,20 +1777,55 @@ namespace libtorrent {
return buf; return buf;
} }
dht_get_peers_reply_alert::dht_get_peers_reply_alert(aux::stack_allocator& dht_get_peers_reply_alert::dht_get_peers_reply_alert(aux::stack_allocator& alloc
, sha1_hash const& ih , sha1_hash const& ih
, std::vector<tcp::endpoint> const& v) , std::vector<tcp::endpoint> const& peers)
: info_hash(ih), peers(v) : m_alloc(alloc)
{} , info_hash(ih)
, m_num_peers(peers.size())
, m_peers_idx(alloc.allocate(sizeof(int) * peers.size() * 2))
{
for (int i = 0; i < m_num_peers; i++) {
tcp::endpoint endp = peers[i];
int size = endp.size();
int idx = alloc.copy_buffer((char*)endp.data(), size);
memcpy(alloc.ptr(sizeof(int) * (2 * i)) , (void*)&idx , sizeof(int));
memcpy(alloc.ptr(sizeof(int) * (2 * i + 1)), (void*)&size, sizeof(int));
}
}
std::string dht_get_peers_reply_alert::message() const std::string dht_get_peers_reply_alert::message() const
{ {
char ih_hex[41]; char ih_hex[41];
to_hex((const char*)&info_hash[0], 20, ih_hex); to_hex((const char*)&info_hash[0], 20, ih_hex);
char msg[200]; char msg[200];
snprintf(msg, sizeof(msg), "incoming dht get_peers reply: %s, peers %ld", ih_hex, peers.size()); snprintf(msg, sizeof(msg), "incoming dht get_peers reply: %s, peers %ld", ih_hex, m_num_peers);
return msg; return msg;
} }
int dht_get_peers_reply_alert::num_peers() const
{
return m_num_peers;
}
tcp::endpoint dht_get_peers_reply_alert::get_peer(int index) const
{
int idx, size;
memcpy((void*)&idx , m_alloc.ptr(sizeof(int) * (2 * index)) , sizeof(int));
memcpy((void*)&size, m_alloc.ptr(sizeof(int) * (2 * index + 1)), sizeof(int));
tcp::endpoint endp;
if (size == sizeof(boost::asio::detail::sockaddr_in6_type))
{
endp.resize(sizeof(boost::asio::detail::sockaddr_in6_type));
}
memcpy((void*)endp.data(), m_alloc.ptr(idx), size);
return endp;
}
} // namespace libtorrent } // namespace libtorrent