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/

相关文章:

python - 如何使用 libtorrent for python 获取 info_hash

bittorrent - Kademlia/DHT 如何获取节点 ID 并更新离开 swarm 的对等节点?

blockchain - 以太坊协议(protocol)如何与 geth 协同工作

c++ - 使用 std::thread (C++ 11) 多线程无法正常工作

c++ - 按类属性(例如名称)搜索类对象的 std::vector

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

c++ - 默认情况下 libtorrent 传输是否加密?

python - libtorrent 中的 set_sequential_download() 和 set_piece_deadline()

Java 等效于 C++ sf_readf_double()

c++ - 如何在 Windows 中编译 scintilla?