2007-05-23 10:45:12 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2007-2018, Arvid Norberg
|
2007-05-23 10:45:12 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OBSERVER_HPP
|
|
|
|
#define OBSERVER_HPP
|
|
|
|
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <cstdint>
|
2016-09-03 03:05:11 +02:00
|
|
|
#include <memory>
|
2016-06-20 17:32:06 +02:00
|
|
|
|
2015-04-19 00:00:27 +02:00
|
|
|
#include <libtorrent/time.hpp>
|
|
|
|
#include <libtorrent/address.hpp>
|
2017-08-05 15:45:28 +02:00
|
|
|
#include <libtorrent/flags.hpp>
|
|
|
|
|
|
|
|
namespace libtorrent {
|
|
|
|
namespace dht {
|
2015-04-19 00:00:27 +02:00
|
|
|
|
2015-05-22 04:42:26 +02:00
|
|
|
struct dht_observer;
|
2007-05-23 10:45:12 +02:00
|
|
|
struct observer;
|
|
|
|
struct msg;
|
2009-10-07 22:51:02 +02:00
|
|
|
struct traversal_algorithm;
|
2007-05-23 10:45:12 +02:00
|
|
|
|
2018-03-22 23:28:55 +01:00
|
|
|
using observer_flags_t = libtorrent::flags::bitfield_flag<std::uint8_t, struct observer_flags_tag>;
|
2017-08-05 15:45:28 +02:00
|
|
|
|
|
|
|
struct TORRENT_EXTRA_EXPORT observer
|
|
|
|
: std::enable_shared_from_this<observer>
|
2007-05-23 10:45:12 +02:00
|
|
|
{
|
2017-11-06 02:17:56 +01:00
|
|
|
observer(std::shared_ptr<traversal_algorithm> a
|
2010-11-05 20:06:50 +01:00
|
|
|
, udp::endpoint const& ep, node_id const& id)
|
2017-11-06 02:17:56 +01:00
|
|
|
: m_algorithm(std::move(a))
|
2010-11-05 20:06:50 +01:00
|
|
|
, m_id(id)
|
2008-02-09 22:04:24 +01:00
|
|
|
{
|
2017-11-06 02:17:56 +01:00
|
|
|
TORRENT_ASSERT(m_algorithm);
|
2010-11-05 20:06:50 +01:00
|
|
|
set_target(ep);
|
2008-02-09 22:04:24 +01:00
|
|
|
}
|
2007-05-23 10:45:12 +02:00
|
|
|
|
2017-08-05 15:45:28 +02:00
|
|
|
observer(observer const&) = delete;
|
|
|
|
observer& operator=(observer const&) = delete;
|
|
|
|
|
2013-05-17 05:19:57 +02:00
|
|
|
// defined in rpc_manager.cpp
|
2009-10-07 22:51:02 +02:00
|
|
|
virtual ~observer();
|
2007-05-23 10:45:12 +02:00
|
|
|
|
|
|
|
// this is called when a reply is received
|
|
|
|
virtual void reply(msg const& m) = 0;
|
|
|
|
|
2009-09-29 19:06:08 +02:00
|
|
|
// this is called if no response has been received after
|
|
|
|
// a few seconds, before the request has timed out
|
2009-10-07 22:51:02 +02:00
|
|
|
void short_timeout();
|
|
|
|
|
2017-08-05 15:45:28 +02:00
|
|
|
bool has_short_timeout() const { return bool(flags & flag_short_timeout); }
|
2009-09-29 19:06:08 +02:00
|
|
|
|
2007-05-23 10:45:12 +02:00
|
|
|
// this is called when no reply has been received within
|
2015-09-01 04:37:46 +02:00
|
|
|
// some timeout, or a reply with incorrect format.
|
2014-02-17 06:56:49 +01:00
|
|
|
virtual void timeout();
|
2015-05-22 04:42:26 +02:00
|
|
|
|
2007-05-23 10:45:12 +02:00
|
|
|
// if this is called the destructor should
|
|
|
|
// not invoke any new messages, and should
|
|
|
|
// only clean up. It means the rpc-manager
|
|
|
|
// is being destructed
|
2009-10-07 22:51:02 +02:00
|
|
|
void abort();
|
2007-05-23 10:45:12 +02:00
|
|
|
|
2015-05-22 04:42:26 +02:00
|
|
|
dht_observer* get_observer() const;
|
|
|
|
|
|
|
|
traversal_algorithm* algorithm() const { return m_algorithm.get(); }
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point sent() const { return m_sent; }
|
2009-09-20 02:23:36 +02:00
|
|
|
|
|
|
|
void set_target(udp::endpoint const& ep);
|
|
|
|
address target_addr() const;
|
|
|
|
udp::endpoint target_ep() const;
|
|
|
|
|
2014-03-03 06:09:53 +01:00
|
|
|
void set_id(node_id const& id);
|
2010-11-05 20:06:50 +01:00
|
|
|
node_id const& id() const { return m_id; }
|
|
|
|
|
2017-08-05 15:45:28 +02:00
|
|
|
static constexpr observer_flags_t flag_queried = 0_bit;
|
|
|
|
static constexpr observer_flags_t flag_initial = 1_bit;
|
|
|
|
static constexpr observer_flags_t flag_no_id = 2_bit;
|
|
|
|
static constexpr observer_flags_t flag_short_timeout = 3_bit;
|
|
|
|
static constexpr observer_flags_t flag_failed = 4_bit;
|
|
|
|
static constexpr observer_flags_t flag_ipv6_address = 5_bit;
|
|
|
|
static constexpr observer_flags_t flag_alive = 6_bit;
|
|
|
|
static constexpr observer_flags_t flag_done = 7_bit;
|
2010-11-05 20:06:50 +01:00
|
|
|
|
2009-10-07 22:51:02 +02:00
|
|
|
protected:
|
2009-09-20 02:23:36 +02:00
|
|
|
|
2009-10-07 22:51:02 +02:00
|
|
|
void done();
|
|
|
|
|
2015-05-22 04:42:26 +02:00
|
|
|
private:
|
|
|
|
|
2016-09-03 03:05:11 +02:00
|
|
|
std::shared_ptr<observer> self()
|
|
|
|
{ return shared_from_this(); }
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point m_sent;
|
2009-09-20 02:23:36 +02:00
|
|
|
|
2017-08-05 15:45:28 +02:00
|
|
|
std::shared_ptr<traversal_algorithm> const m_algorithm;
|
2009-10-07 22:51:02 +02:00
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
node_id m_id;
|
|
|
|
|
2016-06-04 16:03:13 +02:00
|
|
|
union addr_t
|
2009-09-20 02:23:36 +02:00
|
|
|
{
|
|
|
|
address_v6::bytes_type v6;
|
|
|
|
address_v4::bytes_type v4;
|
|
|
|
} m_addr;
|
|
|
|
|
2017-11-06 02:17:56 +01:00
|
|
|
std::uint16_t m_port = 0;
|
2009-09-20 02:23:36 +02:00
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
public:
|
2017-11-06 02:17:56 +01:00
|
|
|
observer_flags_t flags{};
|
2009-10-07 22:51:02 +02:00
|
|
|
|
2016-06-18 14:31:07 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2017-11-06 02:17:56 +01:00
|
|
|
bool m_in_constructor = true;
|
|
|
|
bool m_was_sent = false;
|
|
|
|
bool m_was_abandoned = false;
|
|
|
|
bool m_in_use = true;
|
2008-02-09 22:04:24 +01:00
|
|
|
#endif
|
2007-05-23 10:45:12 +02:00
|
|
|
};
|
|
|
|
|
2017-08-05 15:45:28 +02:00
|
|
|
using observer_ptr = std::shared_ptr<observer>;
|
2007-05-23 10:45:12 +02:00
|
|
|
|
2017-08-05 15:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-23 10:45:12 +02:00
|
|
|
|
|
|
|
#endif
|