move shared_ptr instead of copying them, when constructing DHT observers

This commit is contained in:
arvidn 2017-11-06 03:17:56 +02:00 committed by Arvid Norberg
parent b0c101a475
commit f41d2b5d3b
13 changed files with 38 additions and 47 deletions

View File

@ -67,9 +67,9 @@ protected:
struct direct_observer : observer
{
direct_observer(std::shared_ptr<traversal_algorithm> const& algo
direct_observer(std::shared_ptr<traversal_algorithm> algo
, udp::endpoint const& ep, node_id const& id)
: observer(algo, ep, id)
: observer(std::move(algo), ep, id)
{}
void reply(msg const& m) override

View File

@ -76,9 +76,9 @@ protected:
struct find_data_observer : traversal_observer
{
find_data_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id)
: traversal_observer(algorithm, ep, id)
: traversal_observer(std::move(algorithm), ep, id)
{}
void reply(msg const&) override;

View File

@ -80,9 +80,9 @@ class get_item_observer : public find_data_observer
{
public:
get_item_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id)
: find_data_observer(algorithm, ep, id)
: find_data_observer(std::move(algorithm), ep, id)
{}
void reply(msg const&) override;

View File

@ -85,9 +85,9 @@ private:
struct get_peers_observer : find_data_observer
{
get_peers_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id)
: find_data_observer(algorithm, ep, id)
: find_data_observer(std::move(algorithm), ep, id)
{}
void reply(msg const&) override;
@ -100,9 +100,9 @@ private:
struct obfuscated_get_peers_observer : traversal_observer
{
obfuscated_get_peers_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id)
: traversal_observer(algorithm, ep, id)
: traversal_observer(std::move(algorithm), ep, id)
{}
void reply(msg const&) override;
};

View File

@ -68,9 +68,9 @@ TORRENT_EXTRA_EXPORT entry write_nodes_entry(std::vector<node_entry> const& node
class announce_observer : public observer
{
public:
announce_observer(std::shared_ptr<traversal_algorithm> const& algo
announce_observer(std::shared_ptr<traversal_algorithm> algo
, udp::endpoint const& ep, node_id const& id)
: observer(algo, ep, id)
: observer(std::move(algo), ep, id)
{}
void reply(msg const&) override { flags |= flag_done; }

View File

@ -54,22 +54,12 @@ using observer_flags_t = libtorrent::flags::bitfield_flag<std::uint8_t, observer
struct TORRENT_EXTRA_EXPORT observer
: std::enable_shared_from_this<observer>
{
observer(std::shared_ptr<traversal_algorithm> const& a
observer(std::shared_ptr<traversal_algorithm> a
, udp::endpoint const& ep, node_id const& id)
: m_sent()
, m_algorithm(a)
: m_algorithm(std::move(a))
, m_id(id)
, m_port(0)
, m_transaction_id()
, flags{}
{
TORRENT_ASSERT(a);
#if TORRENT_USE_ASSERTS
m_in_constructor = true;
m_was_sent = false;
m_was_abandoned = false;
m_in_use = true;
#endif
TORRENT_ASSERT(m_algorithm);
set_target(ep);
}
@ -149,18 +139,18 @@ private:
address_v4::bytes_type v4;
} m_addr;
std::uint16_t m_port;
std::uint16_t m_port = 0;
// the transaction ID for this call
std::uint16_t m_transaction_id;
std::uint16_t m_transaction_id = 0;
public:
observer_flags_t flags;
observer_flags_t flags{};
#if TORRENT_USE_ASSERTS
bool m_in_constructor:1;
bool m_was_sent:1;
bool m_was_abandoned:1;
bool m_in_use:1;
bool m_in_constructor = true;
bool m_was_sent = false;
bool m_was_abandoned = false;
bool m_in_use = true;
#endif
};

View File

@ -71,9 +71,9 @@ protected:
struct put_data_observer : traversal_observer
{
put_data_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id, std::string const& token)
: traversal_observer(algorithm, ep, id)
: traversal_observer(std::move(algorithm), ep, id)
, m_token(token)
{
}

View File

@ -57,8 +57,9 @@ struct socket_manager;
struct TORRENT_EXTRA_EXPORT null_observer : observer
{
null_observer(std::shared_ptr<traversal_algorithm> const& a
, udp::endpoint const& ep, node_id const& id): observer(a, ep, id) {}
null_observer(std::shared_ptr<traversal_algorithm> a
, udp::endpoint const& ep, node_id const& id)
: observer(std::move(a), ep, id) {}
void reply(msg const&) override { flags |= flag_done; }
};

View File

@ -68,7 +68,7 @@ class sample_infohashes_observer final : public traversal_observer
{
public:
sample_infohashes_observer(std::shared_ptr<traversal_algorithm> const& algorithm
sample_infohashes_observer(std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id);
void reply(msg const&) override;

View File

@ -156,9 +156,9 @@ void look_for_nodes(char const* nodes_key, udp const& protocol
struct traversal_observer : observer
{
traversal_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id)
: observer(algorithm, ep, id)
: observer(std::move(algorithm), ep, id)
{}
// parses out "nodes" and keeps traversing

View File

@ -463,7 +463,7 @@ void node::direct_request(udp::endpoint const& ep, entry& e
// not really a traversal
auto algo = std::make_shared<direct_traversal>(*this, node_id(), f);
auto o = m_rpc.allocate_observer<direct_observer>(algo, ep, node_id());
auto o = m_rpc.allocate_observer<direct_observer>(std::move(algo), ep, node_id());
if (!o) return;
#if TORRENT_USE_ASSERTS
o->m_in_constructor = false;
@ -603,9 +603,9 @@ void node::sample_infohashes(udp::endpoint const& ep, sha1_hash const& target
struct ping_observer : observer
{
ping_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id)
: observer(algorithm, ep, id)
: observer(std::move(algorithm), ep, id)
{}
// parses out "nodes"
@ -675,8 +675,8 @@ void node::send_single_refresh(udp::endpoint const& ep, int const bucket
target |= m_id & mask;
// create a dummy traversal_algorithm
auto const algo = std::make_shared<traversal_algorithm>(*this, node_id());
auto o = m_rpc.allocate_observer<ping_observer>(algo, ep, id);
auto algo = std::make_shared<traversal_algorithm>(*this, node_id());
auto o = m_rpc.allocate_observer<ping_observer>(std::move(algo), ep, id);
if (!o) return;
#if TORRENT_USE_ASSERTS
o->m_in_constructor = false;

View File

@ -62,9 +62,9 @@ void sample_infohashes::got_samples(time_duration interval
}
sample_infohashes_observer::sample_infohashes_observer(
std::shared_ptr<traversal_algorithm> const& algorithm
std::shared_ptr<traversal_algorithm> algorithm
, udp::endpoint const& ep, node_id const& id)
: traversal_observer(algorithm, ep, id) {}
: traversal_observer(std::move(algorithm), ep, id) {}
void sample_infohashes_observer::reply(msg const& m)
{

View File

@ -3372,7 +3372,7 @@ TORRENT_TEST(rpc_invalid_error_msg)
g_sent_packets.clear();
auto algo = std::make_shared<dht::traversal_algorithm>(node, node_id());
auto o = rpc.allocate_observer<null_observer>(algo, source, node_id());
auto o = rpc.allocate_observer<null_observer>(std::move(algo), source, node_id());
#if TORRENT_USE_ASSERTS
o->m_in_constructor = false;
#endif