c++ - 需要一个使用 dht_put_item 和 libtorrent 的例子

标签 c++ dht libtorrent

我正在尝试使用 dht 将可变数据保存在 libtorrent 中。据我所知,正确的方法是使用 session 中的 dht_put_item 方法。问题是我需要传递一个回调函数,但我不知道自己做错了什么……我的代码如下所示

namespace lt = libtorrent;

//The callback function
void cb(lt::entry& cdentry, boost::array<char,64>& cbarray, boost::uint64_t& cbint, std::string const& cbstring){
    //My stuff here
}

void main(){
    //The session
    lt::session ses;
    //The data I want to insert into DHT
    std::string cadenaStr = "519d818411de49652b4aaf34850321de28bb2dce";        

    //Now I create the keys
    unsigned char seed[32];
    unsigned char public_key[32];
    unsigned char private_key[64];
    unsigned char signature[32];
    ed25519_create_seed(seed);
    ed25519_create_keypair(public_key, private_key, seed);
    ed25519_sign(signature, cadenaStr.c_str(), sizeof(cadenaStr.c_str()), public_key, private_key);

    //How can I use this?, where is the data supposed to go? :|
    ses.dht_put_item(public_key, cb, false);
}

在 libtorrent/session_handler.hpp 上,此方法定义为

void dht_put_item(boost::array<char, 32> key
    , boost::function<void(entry&, boost::array<char,64>&
    , boost::uint64_t&, std::string const&)> cb
    , std::string salt = std::string());

谁能告诉我我做错了什么。

谢谢!

最佳答案

我用于测试的 libtorrent 存储库中有一个示例。它可以生成键,放置和获取可变和不可变项目。

https://github.com/arvidn/libtorrent/blob/master/tools/dht_put.cpp

How can I use this?, where is the data supposed to go? :|

您在调用的回调中提供数据。使用这种 API 的原因是,在某些用例中,您想要改变数据,然后您需要首先知道该键下是否已经存储了某些内容,以及它是什么。

关于c++ - 需要一个使用 dht_put_item 和 libtorrent 的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52349221/

相关文章:

c++ - GDB 断点不命中模板函数

c# - 160位哈希算术运算的最高有效字节是多少?

c++ - libtorrent 内置跟踪器

.net - .NET 的 BLOB 分布式存储?

java - Kademlia Java 实现

python - libtorrent-rasterbar 的 python 绑定(bind)是否适用于 Python 3?

hadoop - 如何通过hdfs MapReduce直接计算hdfs上文件的磁链?

c++ - 代码意外地给出了无限循环

c++ - C++ 中 mpfr 变量动态数组的内存泄漏

c++ - 无法弄清楚为什么它不会停止循环