Last active
October 18, 2018 16:17
-
-
Save jachiang/c5a92823009a71aa72ceb49002bf4fe4 to your computer and use it in GitHub Desktop.
Deriving from p2p class (to attach custom/derived sessions & protocols)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bitcoin/network.hpp> | |
BC_USE_LIBBITCOIN_MAIN | |
// Inherit from P2P class. | |
class derived_from_p2p | |
: public bc::network::p2p | |
{ | |
public: | |
typedef std::shared_ptr<derived_from_p2p> ptr; | |
derived_from_p2p(const bc::network::settings settings) | |
: bc::network::p2p(settings) | |
{ | |
// Prints out correct number of seeds. | |
bc::cout << "Number of seeds during constructor: " << std::endl; | |
bc::cout << network_settings().seeds.size() << std::endl; | |
} | |
}; | |
int bc::main(int argc, char* argv[]) | |
{ | |
bc::network::settings settings(bc::config::settings::testnet); | |
settings.connect_batch_size = 10; | |
settings.host_pool_capacity = 100; | |
settings.seeds.clear(); | |
settings.seeds.push_back({"testnet2.libbitcoin.net",18333}); | |
settings.seeds.push_back({"testnet2.libbitcoin.net",18333}); | |
settings.seeds.push_back({"testnet3.libbitcoin.net",18333}); | |
// Promise which signals completion to main loop. | |
std::promise<bc::code> complete; | |
// Create derived P2P object (Not sure how to place this below Handlers, | |
// as these refer to derived P2P object). | |
derived_from_p2p my_network(settings); | |
// Problem: Seed list is empty after constructor. | |
// ...seed session fails because no seeds returned. | |
bc::cout << "Number of seeds after constructor: " << std::endl; | |
bc::cout << my_network.network_settings().seeds.size() << std::endl; | |
// Handlers | |
// ------------------------------------------------------------------------- | |
const auto run_handler = [&my_network](const bc::code& ec) | |
{ | |
// TODO | |
}; | |
const auto start_handler = [&my_network, &complete, &run_handler]( | |
const bc::code& ec) | |
{ | |
// (ignore error::peer_throttling) | |
if (ec && ec != bc::error::peer_throttling) | |
{ | |
complete.set_value(ec); | |
} | |
my_network.run(run_handler); | |
}; | |
// Start P2P object. | |
// ------------------------------------------------------------------------- | |
my_network.start(start_handler); | |
bc::cout << complete.get_future().get().message() << std::endl; | |
return 0; | |
} | |
// g++ -std=c++11 `pkg-config --cflags libbitcoin-network --libs libbitcoin-network` -o derived_from_network derived_from_network.cpp -DBOOST_LOG_DYN_LINK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment