Skip to content
Snippets Groups Projects
Commit 1ce44098 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

dhtrunner: add pluggable transport

parent fddd7e9a
No related branches found
No related tags found
No related merge requests found
...@@ -66,6 +66,7 @@ public: ...@@ -66,6 +66,7 @@ public:
struct Context { struct Context {
std::unique_ptr<Logger> logger {}; std::unique_ptr<Logger> logger {};
std::unique_ptr<net::DatagramSocket> sock;
std::shared_ptr<PeerDiscovery> peerDiscovery {}; std::shared_ptr<PeerDiscovery> peerDiscovery {};
StatusCallback statusChangedCallback {}; StatusCallback statusChangedCallback {};
CertificateStoreQuery certificateStore {}; CertificateStoreQuery certificateStore {};
...@@ -400,6 +401,8 @@ public: ...@@ -400,6 +401,8 @@ public:
*/ */
void run(const char* ip4, const char* ip6, const char* service, const Config& config, Context&& context = {}); void run(const char* ip4, const char* ip6, const char* service, const Config& config, Context&& context = {});
void run(const Config& config, Context&& context);
void setOnStatusChanged(StatusCallback&& cb) { void setOnStatusChanged(StatusCallback&& cb) {
statusCb = std::move(cb); statusCb = std::move(cb);
} }
......
...@@ -77,7 +77,7 @@ DhtRunner::~DhtRunner() ...@@ -77,7 +77,7 @@ DhtRunner::~DhtRunner()
} }
void void
DhtRunner::run(in_port_t port, const DhtRunner::Config& config, Context&& context) DhtRunner::run(in_port_t port, const Config& config, Context&& context)
{ {
SockAddr sin4; SockAddr sin4;
sin4.setFamily(AF_INET); sin4.setFamily(AF_INET);
...@@ -89,7 +89,7 @@ DhtRunner::run(in_port_t port, const DhtRunner::Config& config, Context&& contex ...@@ -89,7 +89,7 @@ DhtRunner::run(in_port_t port, const DhtRunner::Config& config, Context&& contex
} }
void void
DhtRunner::run(const char* ip4, const char* ip6, const char* service, const DhtRunner::Config& config, Context&& context) DhtRunner::run(const char* ip4, const char* ip6, const char* service, const Config& config, Context&& context)
{ {
auto res4 = SockAddr::resolve(ip4, service); auto res4 = SockAddr::resolve(ip4, service);
auto res6 = SockAddr::resolve(ip6, service); auto res6 = SockAddr::resolve(ip6, service);
...@@ -98,13 +98,22 @@ DhtRunner::run(const char* ip4, const char* ip6, const char* service, const DhtR ...@@ -98,13 +98,22 @@ DhtRunner::run(const char* ip4, const char* ip6, const char* service, const DhtR
} }
void void
DhtRunner::run(const SockAddr& local4, const SockAddr& local6, const DhtRunner::Config& config, Context&& context) DhtRunner::run(const SockAddr& local4, const SockAddr& local6, const Config& config, Context&& context)
{
if (not running) {
if (not context.sock)
context.sock.reset(new net::UdpSocket(local4, local6));
run(config, std::move(context));
}
}
void
DhtRunner::run(const Config& config, Context&& context)
{ {
if (running) if (running)
return; return;
auto sock = std::unique_ptr<net::UdpSocket>(new net::UdpSocket(local4, local6)); context.sock->setOnReceive([&] (std::unique_ptr<net::ReceivedPacket>&& pkt) {
sock->setOnReceive([&] (std::unique_ptr<net::ReceivedPacket>&& pkt) {
{ {
std::lock_guard<std::mutex> lck(sock_mtx); std::lock_guard<std::mutex> lck(sock_mtx);
if (rcv.size() >= RX_QUEUE_MAX_SIZE) { if (rcv.size() >= RX_QUEUE_MAX_SIZE) {
...@@ -116,7 +125,7 @@ DhtRunner::run(const SockAddr& local4, const SockAddr& local6, const DhtRunner:: ...@@ -116,7 +125,7 @@ DhtRunner::run(const SockAddr& local4, const SockAddr& local6, const DhtRunner::
cv.notify_all(); cv.notify_all();
}); });
auto dht = std::unique_ptr<DhtInterface>(new Dht(std::move(sock), SecureDht::getConfig(config.dht_config))); auto dht = std::unique_ptr<DhtInterface>(new Dht(std::move(context.sock), SecureDht::getConfig(config.dht_config)));
dht_ = std::unique_ptr<SecureDht>(new SecureDht(std::move(dht), config.dht_config)); dht_ = std::unique_ptr<SecureDht>(new SecureDht(std::move(dht), config.dht_config));
#ifdef OPENDHT_PROXY_CLIENT #ifdef OPENDHT_PROXY_CLIENT
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment