extend plugin API to allow hooking when peers are added to the peer list

This commit is contained in:
Arvid Norberg 2011-05-02 01:45:56 +00:00
parent ddb70f3f68
commit c003e4f810
5 changed files with 95 additions and 0 deletions

View File

@ -119,6 +119,16 @@ The synopsis for ``torrent_plugin`` follows::
virtual bool on_resume(); virtual bool on_resume();
virtual void on_files_checked(); virtual void on_files_checked();
virtual void on_state(int s);
enum flags_t {
first_time = 1,
filtered = 2
};
virtual void on_add_peer(tcp::endpoint const& ip
, int src, int flags);
}; };
This is the base class for a torrent_plugin. Your derived class is (if added This is the base class for a torrent_plugin. Your derived class is (if added
@ -206,6 +216,44 @@ checked. If there are no files to check, this function is called immediately.
i.e. This function is always called when the torrent is in a state where it i.e. This function is always called when the torrent is in a state where it
can start downloading. can start downloading.
on_files_checked()
------------------
::
enum flags_t {
first_time = 1,
filtered = 2
};
virtual void on_add_peer(tcp::endpoint const& ip
, int src, int flags);
This function is called whenever we hear about a peer from any peer source,
such as the tracker, PEX, DHT or Local peer discovery.
``src`` is a bitmask of ``peer_info::peer_source_flags``::
enum peer_source_flags
{
tracker = 0x1,
dht = 0x2,
pex = 0x4,
lsd = 0x8,
resume_data = 0x10,
incoming = 0x20
};
``flags`` is a bitmask of::
enum flags_t {
first_time = 1,
filtered = 2
};
If the ``filtered`` flag is set, it means the peer wasn't added to the
peer list because of and IP filter, port filter, reserved ports filter.
peer_plugin peer_plugin
=========== ===========

View File

@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <vector> #include <vector>
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/buffer.hpp" #include "libtorrent/buffer.hpp"
#include "libtorrent/socket.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -119,6 +120,21 @@ namespace libtorrent
// the state is one of torrent_status::state_t // the state is one of torrent_status::state_t
// enum members // enum members
virtual void on_state(int s) {} virtual void on_state(int s) {}
// called every time policy::add_peer is called
// src is a bitmask of which sources this peer
// has been seen from. flags is a bitmask of:
enum flags_t {
// this is the first time we see this peer
first_time = 1,
// this peer was not added because it was
// filtered by the IP filter
filtered = 2
};
virtual void on_add_peer(tcp::endpoint const& ip
, int src, int flags) {}
}; };
struct TORRENT_EXPORT peer_plugin struct TORRENT_EXPORT peer_plugin

View File

@ -131,6 +131,7 @@ namespace libtorrent
void add_extension(boost::shared_ptr<torrent_plugin>); void add_extension(boost::shared_ptr<torrent_plugin>);
void add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> const& ext void add_extension(boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> const& ext
, void* userdata); , void* userdata);
void notify_extension_add_peer(tcp::endpoint const& ip, int src, int flags);
#endif #endif
#ifdef TORRENT_DEBUG #ifdef TORRENT_DEBUG

View File

@ -56,6 +56,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/broadcast_socket.hpp" #include "libtorrent/broadcast_socket.hpp"
#include "libtorrent/peer_info.hpp" #include "libtorrent/peer_info.hpp"
#include "libtorrent/random.hpp" #include "libtorrent/random.hpp"
#include "libtorrent/extensions.hpp"
#ifdef TORRENT_DEBUG #ifdef TORRENT_DEBUG
#include "libtorrent/bt_peer_connection.hpp" #include "libtorrent/bt_peer_connection.hpp"
@ -1207,6 +1208,9 @@ namespace libtorrent
{ {
if (ses.m_alerts.should_post<peer_blocked_alert>()) if (ses.m_alerts.should_post<peer_blocked_alert>())
ses.m_alerts.post_alert(peer_blocked_alert(m_torrent->get_handle(), remote.address())); ses.m_alerts.post_alert(peer_blocked_alert(m_torrent->get_handle(), remote.address()));
#ifndef TORRENT_DISABLE_EXTENSIONS
m_torrent->notify_extension_add_peer(remote, src, torrent_plugin::filtered);
#endif
return 0; return 0;
} }
@ -1214,6 +1218,9 @@ namespace libtorrent
{ {
if (ses.m_alerts.should_post<peer_blocked_alert>()) if (ses.m_alerts.should_post<peer_blocked_alert>())
ses.m_alerts.post_alert(peer_blocked_alert(m_torrent->get_handle(), remote.address())); ses.m_alerts.post_alert(peer_blocked_alert(m_torrent->get_handle(), remote.address()));
#ifndef TORRENT_DISABLE_EXTENSIONS
m_torrent->notify_extension_add_peer(remote, src, torrent_plugin::filtered);
#endif
return 0; return 0;
} }
@ -1223,6 +1230,9 @@ namespace libtorrent
{ {
if (ses.m_alerts.should_post<peer_blocked_alert>()) if (ses.m_alerts.should_post<peer_blocked_alert>())
ses.m_alerts.post_alert(peer_blocked_alert(m_torrent->get_handle(), remote.address())); ses.m_alerts.post_alert(peer_blocked_alert(m_torrent->get_handle(), remote.address()));
#ifndef TORRENT_DISABLE_EXTENSIONS
m_torrent->notify_extension_add_peer(remote, src, torrent_plugin::filtered);
#endif
return 0; return 0;
} }
@ -1290,11 +1300,17 @@ namespace libtorrent
m_torrent->session().m_ipv4_peer_pool.free((ipv4_peer*)p); m_torrent->session().m_ipv4_peer_pool.free((ipv4_peer*)p);
return 0; return 0;
} }
#ifndef TORRENT_DISABLE_EXTENSIONS
m_torrent->notify_extension_add_peer(remote, src, torrent_plugin::first_time);
#endif
} }
else else
{ {
p = *iter; p = *iter;
update_peer(p, src, flags, remote, 0); update_peer(p, src, flags, remote, 0);
#ifndef TORRENT_DISABLE_EXTENSIONS
m_torrent->notify_extension_add_peer(remote, src, 0);
#endif
} }
return p; return p;

View File

@ -7375,6 +7375,20 @@ namespace libtorrent
#endif #endif
} }
#ifndef TORRENT_DISABLE_EXTENSIONS
void torrent::notify_extension_add_peer(tcp::endpoint const& ip
, int src, int flags)
{
for (extension_list_t::iterator i = m_extensions.begin()
, end(m_extensions.end()); i != end; ++i)
{
TORRENT_TRY {
(*i)->on_add_peer(ip, src, flags);
} TORRENT_CATCH (std::exception&) {}
}
}
#endif
void torrent::status(torrent_status* st, boost::uint32_t flags) void torrent::status(torrent_status* st, boost::uint32_t flags)
{ {
INVARIANT_CHECK; INVARIANT_CHECK;