Merge pull request #70 from thomas-yuan/master
Call callback function immediately when get data for mutable data.
This commit is contained in:
commit
27c5a28637
|
@ -1993,7 +1993,8 @@ namespace libtorrent
|
|||
, boost::array<char, 64> sig
|
||||
, boost::uint64_t sequence
|
||||
, std::string const& s
|
||||
, entry const& i);
|
||||
, entry const& i
|
||||
, bool a);
|
||||
|
||||
TORRENT_DEFINE_ALERT_PRIO(dht_mutable_item_alert, 75)
|
||||
|
||||
|
@ -2022,6 +2023,9 @@ namespace libtorrent
|
|||
|
||||
// the data for this item
|
||||
entry item;
|
||||
|
||||
// the last response for mutable date is authoritative.
|
||||
bool authoritative;
|
||||
};
|
||||
|
||||
// this is posted when a DHT put operation completes. This is useful if the
|
||||
|
|
|
@ -300,7 +300,7 @@ namespace libtorrent
|
|||
|
||||
void get_immutable_callback(sha1_hash target
|
||||
, dht::item const& i);
|
||||
void get_mutable_callback(dht::item const& i);
|
||||
void get_mutable_callback(dht::item const& i, bool);
|
||||
|
||||
void dht_get_immutable_item(sha1_hash const& target);
|
||||
|
||||
|
|
|
@ -97,7 +97,7 @@ namespace libtorrent { namespace dht
|
|||
// key is a 32-byte binary string, the public key to look up.
|
||||
// the salt is optional
|
||||
void get_item(char const* key
|
||||
, boost::function<void(item const&)> cb
|
||||
, boost::function<void(item const&, bool)> cb
|
||||
, std::string salt = std::string());
|
||||
|
||||
void put_item(entry data
|
||||
|
|
|
@ -43,7 +43,7 @@ namespace libtorrent { namespace dht
|
|||
class get_item : public find_data
|
||||
{
|
||||
public:
|
||||
typedef boost::function<bool(item&)> data_callback;
|
||||
typedef boost::function<bool(item&, bool)> data_callback;
|
||||
|
||||
void got_data(bdecode_node const& v,
|
||||
char const* pk,
|
||||
|
|
|
@ -245,8 +245,8 @@ public:
|
|||
void announce(sha1_hash const& info_hash, int listen_port, int flags
|
||||
, boost::function<void(std::vector<tcp::endpoint> const&)> f);
|
||||
|
||||
void get_item(sha1_hash const& target, boost::function<bool(item&)> f);
|
||||
void get_item(char const* pk, std::string const& salt, boost::function<bool(item&)> f);
|
||||
void get_item(sha1_hash const& target, boost::function<bool(item&, bool)> f);
|
||||
void get_item(char const* pk, std::string const& salt, boost::function<bool(item&, bool)> f);
|
||||
|
||||
bool verify_token(std::string const& token, char const* info_hash
|
||||
, udp::endpoint const& addr) const;
|
||||
|
|
|
@ -1448,17 +1448,19 @@ namespace libtorrent {
|
|||
, boost::array<char, 64> sig
|
||||
, boost::uint64_t sequence
|
||||
, std::string const& s
|
||||
, entry const& i)
|
||||
: key(k), signature(sig), seq(sequence), salt(s), item(i)
|
||||
, entry const& i
|
||||
, bool a)
|
||||
: key(k), signature(sig), seq(sequence), salt(s), item(i), authoritative(a)
|
||||
{}
|
||||
|
||||
std::string dht_mutable_item_alert::message() const
|
||||
{
|
||||
char msg[1050];
|
||||
snprintf(msg, sizeof(msg), "DHT mutable item (key=%s salt=%s seq=%" PRId64 ") [ %s ]"
|
||||
snprintf(msg, sizeof(msg), "DHT mutable item (key=%s salt=%s seq=%" PRId64 " %s) [ %s ]"
|
||||
, to_hex(std::string(&key[0], 32)).c_str()
|
||||
, salt.c_str()
|
||||
, seq
|
||||
, authoritative ? "auth" : "non-auth"
|
||||
, item.to_string().c_str());
|
||||
return msg;
|
||||
}
|
||||
|
|
|
@ -247,12 +247,12 @@ namespace libtorrent { namespace dht
|
|||
return false;
|
||||
}
|
||||
|
||||
bool get_mutable_item_callback(item& it, boost::function<void(item const&)> f)
|
||||
bool get_mutable_item_callback(item& it, bool authoritative, boost::function<void(item const&, bool)> f)
|
||||
{
|
||||
// the reason to wrap here is to control the return value
|
||||
// since it controls whether we re-put the content
|
||||
TORRENT_ASSERT(it.is_mutable());
|
||||
f(it);
|
||||
f(it, authoritative);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -267,9 +267,11 @@ namespace libtorrent { namespace dht
|
|||
return true;
|
||||
}
|
||||
|
||||
bool put_mutable_item_callback(item& it, boost::function<void(item&)> cb)
|
||||
bool put_mutable_item_callback(item& it, bool authoritative, boost::function<void(item&)> cb)
|
||||
{
|
||||
cb(it);
|
||||
if (authoritative) {
|
||||
cb(it);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -284,10 +286,10 @@ namespace libtorrent { namespace dht
|
|||
// key is a 32-byte binary string, the public key to look up.
|
||||
// the salt is optional
|
||||
void dht_tracker::get_item(char const* key
|
||||
, boost::function<void(item const&)> cb
|
||||
, boost::function<void(item const&, bool)> cb
|
||||
, std::string salt)
|
||||
{
|
||||
m_dht.get_item(key, salt, boost::bind(&get_mutable_item_callback, _1, cb));
|
||||
m_dht.get_item(key, salt, boost::bind(&get_mutable_item_callback, _1, _2, cb));
|
||||
}
|
||||
|
||||
void dht_tracker::put_item(entry data
|
||||
|
@ -306,7 +308,7 @@ namespace libtorrent { namespace dht
|
|||
, boost::function<void(item&)> cb, std::string salt)
|
||||
{
|
||||
m_dht.get_item(key, salt, boost::bind(&put_mutable_item_callback
|
||||
, _1, cb));
|
||||
, _1, _2, cb));
|
||||
}
|
||||
|
||||
// translate bittorrent kademlia message into the generice kademlia message
|
||||
|
|
|
@ -70,6 +70,16 @@ void get_item::got_data(bdecode_node const& v,
|
|||
{
|
||||
if (!m_data.assign(v, salt, seq, pk, sig))
|
||||
return;
|
||||
|
||||
// for get_item, we should call callback when we get data,
|
||||
// even if the date is not authoritative, we can update later.
|
||||
// so caller can get response ASAP without waitting transaction
|
||||
// time-out (15 seconds).
|
||||
// for put_item, the callback function will do nothing
|
||||
// if the data is non-authoritative.
|
||||
// we can just ignore the return value here since for mutable
|
||||
// data, we always need the transaction done.
|
||||
m_data_callback(m_data, false);
|
||||
}
|
||||
}
|
||||
else if (m_data.empty())
|
||||
|
@ -78,7 +88,7 @@ void get_item::got_data(bdecode_node const& v,
|
|||
// and it's immutable
|
||||
|
||||
m_data.assign(v);
|
||||
bool put_requested = m_data_callback(m_data);
|
||||
bool put_requested = m_data_callback(m_data, true);
|
||||
|
||||
// if we intend to put, we need to keep going
|
||||
// until we find the closest nodes, since those
|
||||
|
@ -162,10 +172,10 @@ void get_item::done()
|
|||
{
|
||||
if (m_data.is_mutable() || m_data.empty())
|
||||
{
|
||||
// for mutable data, we only call the callback at the end,
|
||||
// when we've heard from everyone, to be sure we got the
|
||||
// for mutable data, now we have authoritative data since
|
||||
// we've heard from everyone, to be sure we got the
|
||||
// latest version of the data (i.e. highest sequence number)
|
||||
bool put_requested = m_data_callback(m_data);
|
||||
bool put_requested = m_data_callback(m_data, true);
|
||||
if (put_requested)
|
||||
{
|
||||
#if TORRENT_USE_ASSERTS
|
||||
|
|
|
@ -412,7 +412,7 @@ void node::announce(sha1_hash const& info_hash, int listen_port, int flags
|
|||
}
|
||||
|
||||
void node::get_item(sha1_hash const& target
|
||||
, boost::function<bool(item&)> f)
|
||||
, boost::function<bool(item&, bool)> f)
|
||||
{
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
if (m_observer)
|
||||
|
@ -430,7 +430,7 @@ void node::get_item(sha1_hash const& target
|
|||
}
|
||||
|
||||
void node::get_item(char const* pk, std::string const& salt
|
||||
, boost::function<bool(item&)> f)
|
||||
, boost::function<bool(item&, bool)> f)
|
||||
{
|
||||
#ifndef TORRENT_DISABLE_LOGGING
|
||||
if (m_observer)
|
||||
|
|
|
@ -5485,11 +5485,11 @@ retry:
|
|||
}
|
||||
|
||||
// callback for dht_mutable_get
|
||||
void session_impl::get_mutable_callback(dht::item const& i)
|
||||
void session_impl::get_mutable_callback(dht::item const& i, bool authoritative)
|
||||
{
|
||||
TORRENT_ASSERT(i.is_mutable());
|
||||
m_alerts.emplace_alert<dht_mutable_item_alert>(i.pk(), i.sig(), i.seq()
|
||||
, i.salt(), i.value());
|
||||
, i.salt(), i.value(), authoritative);
|
||||
}
|
||||
|
||||
// key is a 32-byte binary string, the public key to look up.
|
||||
|
@ -5499,7 +5499,7 @@ retry:
|
|||
{
|
||||
if (!m_dht) return;
|
||||
m_dht->get_item(key.data(), boost::bind(&session_impl::get_mutable_callback
|
||||
, this, _1), salt);
|
||||
, this, _1, _2), salt);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
|
|
@ -406,7 +406,7 @@ std::vector<dht::item> g_got_items;
|
|||
dht::item g_put_item;
|
||||
int g_put_count;
|
||||
|
||||
bool get_item_cb(dht::item& i)
|
||||
bool get_item_cb(dht::item& i, bool a)
|
||||
{
|
||||
if (!i.empty())
|
||||
g_got_items.push_back(i);
|
||||
|
|
Loading…
Reference in New Issue