2007-05-23 10:45:12 +02:00
|
|
|
/*
|
|
|
|
|
2013-12-25 18:07:16 +01:00
|
|
|
Copyright (c) 2007-2013, 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
|
|
|
|
|
|
|
|
#include <boost/pool/pool.hpp>
|
|
|
|
#include <boost/detail/atomic_count.hpp>
|
|
|
|
#include <boost/intrusive_ptr.hpp>
|
2009-05-24 21:38:27 +02:00
|
|
|
#include <boost/cstdint.hpp>
|
2009-11-25 07:55:34 +01:00
|
|
|
#include <libtorrent/ptime.hpp>
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <libtorrent/address.hpp>
|
2007-05-23 10:45:12 +02:00
|
|
|
|
|
|
|
namespace libtorrent {
|
|
|
|
namespace dht {
|
|
|
|
|
|
|
|
struct observer;
|
|
|
|
struct msg;
|
2009-10-07 22:51:02 +02:00
|
|
|
struct traversal_algorithm;
|
2007-05-23 10:45:12 +02:00
|
|
|
|
|
|
|
// defined in rpc_manager.cpp
|
2012-03-20 04:53:07 +01:00
|
|
|
TORRENT_EXTRA_EXPORT void intrusive_ptr_add_ref(observer const*);
|
|
|
|
TORRENT_EXTRA_EXPORT void intrusive_ptr_release(observer const*);
|
2007-05-23 10:45:12 +02:00
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
// intended struct layout (on 32 bit architectures)
|
|
|
|
// offset size alignment field
|
|
|
|
// 0 8 8 sent
|
|
|
|
// 8 8 4 m_refs
|
|
|
|
// 16 4 4 pool_allocator
|
|
|
|
// 20 16 4 m_addr
|
|
|
|
// 36 2 2 m_port
|
2010-12-12 04:17:08 +01:00
|
|
|
// 38 1 1 flags
|
2009-09-20 02:23:36 +02:00
|
|
|
// 39 1 1 <padding>
|
|
|
|
// 40
|
|
|
|
|
2007-05-23 10:45:12 +02:00
|
|
|
struct observer : boost::noncopyable
|
|
|
|
{
|
2012-03-20 04:53:07 +01:00
|
|
|
friend TORRENT_EXTRA_EXPORT void intrusive_ptr_add_ref(observer const*);
|
|
|
|
friend TORRENT_EXTRA_EXPORT void intrusive_ptr_release(observer const*);
|
2007-05-23 10:45:12 +02:00
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
observer(boost::intrusive_ptr<traversal_algorithm> const& a
|
|
|
|
, udp::endpoint const& ep, node_id const& id)
|
2010-12-12 04:17:08 +01:00
|
|
|
: m_sent()
|
2007-05-23 10:45:12 +02:00
|
|
|
, m_refs(0)
|
2009-10-07 22:51:02 +02:00
|
|
|
, m_algorithm(a)
|
2010-11-05 20:06:50 +01:00
|
|
|
, m_id(id)
|
2011-02-21 06:24:41 +01:00
|
|
|
, m_port(0)
|
|
|
|
, m_transaction_id()
|
2010-12-12 04:17:08 +01:00
|
|
|
, flags(0)
|
2008-02-09 22:04:24 +01:00
|
|
|
{
|
2009-10-07 22:51:02 +02:00
|
|
|
TORRENT_ASSERT(a);
|
2011-05-08 11:04:59 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2008-02-09 22:04:24 +01:00
|
|
|
m_in_constructor = true;
|
2009-10-07 22:51:02 +02:00
|
|
|
m_was_sent = false;
|
2012-06-25 00:53:15 +02:00
|
|
|
m_was_abandoned = false;
|
2013-05-17 05:19:57 +02:00
|
|
|
m_in_use = true;
|
2008-02-09 22:04:24 +01:00
|
|
|
#endif
|
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
|
|
|
|
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();
|
|
|
|
|
2011-02-21 06:24:41 +01:00
|
|
|
bool has_short_timeout() const { return (flags & flag_short_timeout) != 0; }
|
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
|
|
|
|
// some timeout
|
2009-10-07 22:51:02 +02:00
|
|
|
void timeout();
|
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
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
ptime sent() const { return m_sent; }
|
|
|
|
|
|
|
|
void set_target(udp::endpoint const& ep);
|
|
|
|
address target_addr() const;
|
|
|
|
udp::endpoint target_ep() const;
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
void set_id(node_id const& id) { m_id = id; }
|
|
|
|
node_id const& id() const { return m_id; }
|
|
|
|
|
2009-10-07 22:51:02 +02:00
|
|
|
void set_transaction_id(boost::uint16_t tid)
|
|
|
|
{ m_transaction_id = tid; }
|
|
|
|
|
|
|
|
boost::uint16_t transaction_id() const
|
|
|
|
{ return m_transaction_id; }
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
enum {
|
|
|
|
flag_queried = 1,
|
|
|
|
flag_initial = 2,
|
|
|
|
flag_no_id = 4,
|
|
|
|
flag_short_timeout = 8,
|
|
|
|
flag_failed = 16,
|
|
|
|
flag_ipv6_address = 32,
|
2010-12-12 04:17:08 +01:00
|
|
|
flag_alive = 64,
|
|
|
|
flag_done = 128
|
2010-11-05 20:06:50 +01:00
|
|
|
};
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
#ifndef TORRENT_DHT_VERBOSE_LOGGING
|
2009-10-07 22:51:02 +02:00
|
|
|
protected:
|
2009-09-20 02:23:36 +02:00
|
|
|
#endif
|
|
|
|
|
2009-10-07 22:51:02 +02:00
|
|
|
void done();
|
|
|
|
|
2009-09-20 02:23:36 +02:00
|
|
|
ptime m_sent;
|
|
|
|
|
|
|
|
// reference counter for intrusive_ptr
|
|
|
|
mutable boost::detail::atomic_count m_refs;
|
2009-10-07 22:51:02 +02:00
|
|
|
|
|
|
|
const boost::intrusive_ptr<traversal_algorithm> m_algorithm;
|
|
|
|
|
2010-11-05 20:06:50 +01:00
|
|
|
node_id m_id;
|
|
|
|
|
2010-03-12 19:30:18 +01:00
|
|
|
TORRENT_UNION addr_t
|
2009-09-20 02:23:36 +02:00
|
|
|
{
|
2009-05-14 00:18:41 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2009-09-20 02:23:36 +02:00
|
|
|
address_v6::bytes_type v6;
|
2009-05-14 00:18:41 +02:00
|
|
|
#endif
|
2009-09-20 02:23:36 +02:00
|
|
|
address_v4::bytes_type v4;
|
|
|
|
} m_addr;
|
|
|
|
|
|
|
|
boost::uint16_t m_port;
|
|
|
|
|
2009-10-07 22:51:02 +02:00
|
|
|
// the transaction ID for this call
|
|
|
|
boost::uint16_t m_transaction_id;
|
2010-12-12 04:17:08 +01:00
|
|
|
public:
|
|
|
|
unsigned char flags;
|
2009-10-07 22:51:02 +02:00
|
|
|
|
2011-05-08 11:04:59 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2009-09-20 02:23:36 +02:00
|
|
|
bool m_in_constructor:1;
|
2009-10-07 22:51:02 +02:00
|
|
|
bool m_was_sent:1;
|
2012-06-25 00:53:15 +02:00
|
|
|
bool m_was_abandoned:1;
|
2013-05-17 05:19:57 +02:00
|
|
|
bool m_in_use:1;
|
2008-02-09 22:04:24 +01:00
|
|
|
#endif
|
2007-05-23 10:45:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef boost::intrusive_ptr<observer> observer_ptr;
|
|
|
|
|
|
|
|
} }
|
|
|
|
|
|
|
|
#endif
|
2009-09-20 02:23:36 +02:00
|
|
|
|