forked from premiere/premiere-libtorrent
Merge pull request #13 from aldenml/more-dht-api
Added dht_get_peers and dht_announce at session_impl level.
This commit is contained in:
commit
41eff1801e
|
@ -192,6 +192,9 @@ namespace libtorrent {
|
|||
// enables dht_log_alert, debug logging for the DHT
|
||||
dht_log_notification = 0x20000,
|
||||
|
||||
// enable events from pure dht operations not related to torrents
|
||||
dht_operation_notification = 0x40000,
|
||||
|
||||
// The full bitmask, representing all available categories.
|
||||
//
|
||||
// since the enum is signed, make sure this isn't
|
||||
|
|
|
@ -2356,12 +2356,34 @@ namespace libtorrent
|
|||
int m_size;
|
||||
};
|
||||
|
||||
struct TORRENT_EXPORT dht_get_peers_reply_alert: alert {
|
||||
|
||||
dht_get_peers_reply_alert(aux::stack_allocator& alloc
|
||||
, sha1_hash const& ih
|
||||
, std::vector<tcp::endpoint> const& v);
|
||||
|
||||
static const int static_category = alert::dht_operation_notification;
|
||||
TORRENT_DEFINE_ALERT(dht_get_peers_reply_alert, 87)
|
||||
|
||||
virtual std::string message() const;
|
||||
|
||||
sha1_hash info_hash;
|
||||
|
||||
int num_peers() const;
|
||||
void peers(std::vector<tcp::endpoint>& peers) const;
|
||||
|
||||
private:
|
||||
aux::stack_allocator& m_alloc;
|
||||
int m_num_peers;
|
||||
int m_peers_idx;
|
||||
};
|
||||
|
||||
#undef TORRENT_DEFINE_ALERT_IMPL
|
||||
#undef TORRENT_DEFINE_ALERT
|
||||
#undef TORRENT_DEFINE_ALERT_PRIO
|
||||
#undef TORRENT_CLONE
|
||||
|
||||
enum { num_alert_types = 87 };
|
||||
enum { num_alert_types = 88 };
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -311,9 +311,12 @@ namespace libtorrent
|
|||
|
||||
void dht_put_mutable_item(boost::array<char, 32> key
|
||||
, boost::function<void(entry&, boost::array<char,64>&
|
||||
, boost::uint64_t&, std::string const&)> cb
|
||||
, boost::uint64_t&, std::string const&)> cb
|
||||
, std::string salt = std::string());
|
||||
|
||||
void dht_get_peers(sha1_hash const& info_hash);
|
||||
void dht_announce(sha1_hash const& info_hash, int port = 0, int flags = 0);
|
||||
|
||||
#ifndef TORRENT_NO_DEPRECATE
|
||||
entry dht_state() const;
|
||||
#endif
|
||||
|
|
|
@ -86,6 +86,8 @@ namespace libtorrent { namespace dht
|
|||
entry state() const;
|
||||
|
||||
enum flags_t { flag_seed = 1, flag_implied_port = 2 };
|
||||
void get_peers(sha1_hash const& ih
|
||||
, boost::function<void(std::vector<tcp::endpoint> const&)> f);
|
||||
void announce(sha1_hash const& ih, int listen_port, int flags
|
||||
, boost::function<void(std::vector<tcp::endpoint> const&)> f);
|
||||
|
||||
|
|
|
@ -1777,5 +1777,57 @@ namespace libtorrent {
|
|||
return buf;
|
||||
}
|
||||
|
||||
dht_get_peers_reply_alert::dht_get_peers_reply_alert(aux::stack_allocator& alloc
|
||||
, sha1_hash const& ih
|
||||
, std::vector<tcp::endpoint> const& peers)
|
||||
: m_alloc(alloc)
|
||||
, info_hash(ih)
|
||||
, m_num_peers(peers.size())
|
||||
{
|
||||
std::size_t total_size = m_num_peers; // num bytes for sizes
|
||||
for (int i = 0; i < m_num_peers; i++) {
|
||||
total_size += peers[i].size();
|
||||
}
|
||||
|
||||
m_peers_idx = alloc.allocate(total_size);
|
||||
|
||||
char *ptr = alloc.ptr(m_peers_idx);
|
||||
for (int i = 0; i < m_num_peers; i++) {
|
||||
tcp::endpoint endp = peers[i];
|
||||
std::size_t size = endp.size();
|
||||
|
||||
detail::write_uint8((boost::uint8_t)size, ptr);
|
||||
memcpy(ptr, endp.data(), size);
|
||||
ptr += size;
|
||||
}
|
||||
}
|
||||
|
||||
std::string dht_get_peers_reply_alert::message() const
|
||||
{
|
||||
char ih_hex[41];
|
||||
to_hex((const char*)&info_hash[0], 20, ih_hex);
|
||||
char msg[200];
|
||||
snprintf(msg, sizeof(msg), "incoming dht get_peers reply: %s, peers %ld", ih_hex, m_num_peers);
|
||||
return msg;
|
||||
}
|
||||
|
||||
int dht_get_peers_reply_alert::num_peers() const
|
||||
{
|
||||
return m_num_peers;
|
||||
}
|
||||
|
||||
void dht_get_peers_reply_alert::peers(std::vector<tcp::endpoint>& peers) const
|
||||
{
|
||||
peers.resize(m_num_peers);
|
||||
|
||||
const char *ptr = m_alloc.ptr(m_peers_idx);
|
||||
for (int i = 0; i < m_num_peers; i++) {
|
||||
|
||||
std::size_t size = detail::read_uint8(ptr);
|
||||
memcpy(peers[i].data(), ptr, size);
|
||||
ptr += size;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace libtorrent
|
||||
|
||||
|
|
|
@ -222,6 +222,12 @@ namespace libtorrent { namespace dht
|
|||
#endif
|
||||
}
|
||||
|
||||
void dht_tracker::get_peers(sha1_hash const& ih
|
||||
, boost::function<void(std::vector<tcp::endpoint> const&)> f)
|
||||
{
|
||||
m_dht.get_peers(ih, f, NULL, false);
|
||||
}
|
||||
|
||||
void dht_tracker::announce(sha1_hash const& ih, int listen_port, int flags
|
||||
, boost::function<void(std::vector<tcp::endpoint> const&)> f)
|
||||
{
|
||||
|
|
|
@ -5531,6 +5531,12 @@ retry:
|
|||
alerts.emplace_alert<dht_put_alert>(pk, sig, salt, seq);
|
||||
}
|
||||
|
||||
void on_dht_get_peers(alert_manager& alerts, sha1_hash info_hash, std::vector<tcp::endpoint> const& peers)
|
||||
{
|
||||
if (alerts.should_post<dht_get_peers_reply_alert>())
|
||||
alerts.emplace_alert<dht_get_peers_reply_alert>(info_hash, peers);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void session_impl::dht_put_item(entry data, sha1_hash target)
|
||||
|
@ -5542,7 +5548,7 @@ retry:
|
|||
|
||||
void session_impl::dht_put_mutable_item(boost::array<char, 32> key
|
||||
, boost::function<void(entry&, boost::array<char,64>&
|
||||
, boost::uint64_t&, std::string const&)> cb
|
||||
, boost::uint64_t&, std::string const&)> cb
|
||||
, std::string salt)
|
||||
{
|
||||
if (!m_dht) return;
|
||||
|
@ -5550,6 +5556,18 @@ retry:
|
|||
, boost::ref(m_alerts), _1, cb), salt);
|
||||
}
|
||||
|
||||
void session_impl::dht_get_peers(sha1_hash const& info_hash)
|
||||
{
|
||||
if (!m_dht) return;
|
||||
m_dht->get_peers(info_hash, boost::bind(&on_dht_get_peers, boost::ref(m_alerts), info_hash, _1));
|
||||
}
|
||||
|
||||
void session_impl::dht_announce(sha1_hash const& info_hash, int port, int flags)
|
||||
{
|
||||
if (!m_dht) return;
|
||||
m_dht->announce(info_hash, port, flags, boost::bind(&on_dht_get_peers, boost::ref(m_alerts), info_hash, _1));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void session_impl::maybe_update_udp_mapping(int nat, int local_port, int external_port)
|
||||
|
|
Loading…
Reference in New Issue