add getters for peer_class_filter and peer_class_type_filter

This commit is contained in:
arvidn 2017-12-20 11:11:12 +01:00 committed by Arvid Norberg
parent 67be35739c
commit b7decd452e
7 changed files with 76 additions and 12 deletions

View File

@ -1,4 +1,5 @@
* add getters for peer_class_filter and peer_class_type_filter
* make torrent_handler::set_priority() to use peer_classes
* fix support for boost-1.66 (requires C++11)
* fix i2p support

View File

@ -66,6 +66,12 @@ struct ip_range
Addr first;
Addr last;
boost::uint32_t flags;
friend bool operator==(ip_range const& lhs, ip_range const& rhs)
{
return lhs.first == rhs.first
&& lhs.last == rhs.last
&& lhs.flags == rhs.flags;
}
};
namespace detail

View File

@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
typedef boost::uint32_t peer_class_t;
typedef int peer_class_t;
// holds settings for a peer class. Used in set_peer_class() and
// get_peer_class() calls.

View File

@ -33,8 +33,8 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_PEER_CLASS_TYPE_FILTER_HPP_INCLUDED
#define TORRENT_PEER_CLASS_TYPE_FILTER_HPP_INCLUDED
#include <string.h> // for memset
#include <boost/cstdint.hpp>
#include <boost/array.hpp>
namespace libtorrent
{
@ -46,8 +46,8 @@ namespace libtorrent
{
peer_class_type_filter()
{
memset(m_peer_class_type_mask, 0xff, sizeof(m_peer_class_type_mask));
memset(m_peer_class_type, 0, sizeof(m_peer_class_type));
m_peer_class_type_mask.fill(0xffffffff);
m_peer_class_type.fill(0);
}
enum socket_type_t
@ -64,7 +64,7 @@ namespace libtorrent
// ``add()`` and ``remove()`` adds and removes a peer class to be added
// to new peers based on socket type.
void add(socket_type_t st, int peer_class)
void add(socket_type_t st, peer_class_t peer_class)
{
TORRENT_ASSERT(peer_class >= 0);
TORRENT_ASSERT(peer_class < 32);
@ -74,7 +74,7 @@ namespace libtorrent
if (st < 0 || st >= num_socket_types) return;
m_peer_class_type[st] |= 1 << peer_class;
}
void remove(socket_type_t st, int peer_class)
void remove(socket_type_t st, peer_class_t peer_class)
{
TORRENT_ASSERT(peer_class >= 0);
TORRENT_ASSERT(peer_class < 32);
@ -90,7 +90,7 @@ namespace libtorrent
//
// The ``peer_class`` argument cannot be greater than 31. The bitmasks representing
// peer classes in the ``peer_class_type_filter`` are 32 bits.
void disallow(socket_type_t st, int peer_class)
void disallow(socket_type_t st, peer_class_t peer_class)
{
TORRENT_ASSERT(peer_class >= 0);
TORRENT_ASSERT(peer_class < 32);
@ -100,7 +100,7 @@ namespace libtorrent
if (st < 0 || st >= num_socket_types) return;
m_peer_class_type_mask[st] &= ~(1 << peer_class);
}
void allow(socket_type_t st, int peer_class)
void allow(socket_type_t st, peer_class_t peer_class)
{
TORRENT_ASSERT(peer_class >= 0);
TORRENT_ASSERT(peer_class < 32);
@ -126,12 +126,19 @@ namespace libtorrent
return peer_class_mask;
}
friend bool operator==(peer_class_type_filter const& lhs
, peer_class_type_filter const& rhs)
{
return lhs.m_peer_class_type_mask == rhs.m_peer_class_type_mask
&& lhs.m_peer_class_type == rhs.m_peer_class_type;
}
private:
// maps socket type to a bitmask that's used to filter out
// (mask) bits from the m_peer_class_filter.
boost::uint32_t m_peer_class_type_mask[num_socket_types];
boost::array<boost::uint32_t, num_socket_types> m_peer_class_type_mask;
// peer class bitfield added based on socket type
boost::uint32_t m_peer_class_type[num_socket_types];
boost::array<boost::uint32_t, num_socket_types> m_peer_class_type;
};
}

View File

@ -634,7 +634,7 @@ namespace libtorrent
// .. code:: c++
//
// ip_filter f;
// int my_class = ses.create_peer_class("200.1.x.x IP range");
// peer_class_t my_class = ses.create_peer_class("200.1.x.x IP range");
// f.add_rule(address_v4::from_string("200.1.1.0")
// , address_v4::from_string("200.1.255.255")
// , 1 << my_class);
@ -651,8 +651,11 @@ namespace libtorrent
// The ``peer_class`` argument cannot be greater than 31. The bitmasks
// representing peer classes in the ``peer_class_filter`` are 32 bits.
//
// The ``get_peer_class_filter()`` function returns the current filter.
//
// For more information, see peer-classes_.
void set_peer_class_filter(ip_filter const& f);
ip_filter get_peer_class_filter() const;
// Sets and gets the *peer class type filter*. This is controls automatic
// peer class assignments to peers based on what kind of socket it is.
@ -667,8 +670,8 @@ namespace libtorrent
// 3. peer-class type filter, adding classes
//
// For more information, see peer-classes_.
// TODO: add get_peer_class_type_filter() as well
void set_peer_class_type_filter(peer_class_type_filter const& f);
peer_class_type_filter get_peer_class_type_filter() const;
// Creates a new peer class (see peer-classes_) with the given name. The
// returned integer is the new peer class identifier. Peer classes may

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/session_call.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/lazy_entry.hpp"
#include "libtorrent/peer_class_type_filter.hpp"
#ifdef __GNUC__
#pragma GCC diagnostic push
@ -611,11 +612,21 @@ namespace libtorrent
TORRENT_ASYNC_CALL1(set_peer_class_filter, f);
}
ip_filter session_handle::get_peer_class_filter() const
{
return TORRENT_SYNC_CALL_RET(ip_filter, get_peer_class_filter);
}
void session_handle::set_peer_class_type_filter(peer_class_type_filter const& f)
{
TORRENT_ASYNC_CALL1(set_peer_class_type_filter, f);
}
peer_class_type_filter session_handle::get_peer_class_type_filter() const
{
return TORRENT_SYNC_CALL_RET(peer_class_type_filter, get_peer_class_type_filter);
}
int session_handle::create_peer_class(char const* name)
{
return TORRENT_SYNC_CALL_RET1(int, create_peer_class, name);

View File

@ -34,6 +34,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/peer_class.hpp"
#include "libtorrent/peer_class_set.hpp"
#include "libtorrent/peer_class_type_filter.hpp"
#include "libtorrent/ip_filter.hpp"
#include "libtorrent/session.hpp"
using namespace libtorrent;
@ -114,3 +116,37 @@ TORRENT_TEST(peer_class)
TEST_CHECK(pool.at(id1) == NULL);
}
TORRENT_TEST(session_peer_class_filter)
{
using namespace libtorrent;
session ses;
peer_class_t my_class = ses.create_peer_class("200.1.x.x IP range");
ip_filter f;
f.add_rule(address_v4::from_string("200.1.1.0")
, address_v4::from_string("200.1.255.255")
, 1 << my_class);
ses.set_peer_class_filter(f);
#if TORRENT_USE_IPV6
TEST_CHECK(boost::get<0>(ses.get_peer_class_filter().export_filter())
== boost::get<0>(f.export_filter()));
#else
TEST_CHECK(ses.get_peer_class_filter().export_filter() == f.export_filter());
#endif
}
TORRENT_TEST(session_peer_class_type_filter)
{
using namespace libtorrent;
session ses;
peer_class_t my_class = ses.create_peer_class("all utp sockets");
peer_class_type_filter f;
f.add(peer_class_type_filter::utp_socket, my_class);
f.disallow(peer_class_type_filter::utp_socket, session::global_peer_class_id);
ses.set_peer_class_type_filter(f);
TEST_CHECK(ses.get_peer_class_type_filter() == f);
}