My Project: /home/noptrix/ba/blackarch/packages/opendht-c/src/opendht/include/opendht/peer_discovery.h Source File

My Project 3.7.7 C++ Distributed Hash Table

Loading...
Searching...
No Matches
peer_discovery.h
1// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "def.h"
6#include "sockaddr.h"
7#include "infohash.h"
8#include "logger.h"
9#include "utils.h"
10
11#include <asio/steady_timer.hpp>
12#include <thread>
13
14namespace asio {
15class io_context;
16}
17
18namespace dht {
19
20 class OPENDHT_PUBLIC PeerDiscovery
21{
22public:
23 static constexpr in_port_t DEFAULT_PORT = 8888;
24 using ServiceDiscoveredCallback = std::function<void(msgpack::object&&, SockAddr&&)>;
25
26 PeerDiscovery(in_port_t port = DEFAULT_PORT,
27 std::shared_ptr<asio::io_context> ioContext = {},
28 std::shared_ptr<Logger> logger = {});
29 ~PeerDiscovery();
30
35 void startDiscovery(std::string_view type, ServiceDiscoveredCallback callback);
36
37 template<typename T>
38 void startDiscovery(std::string_view type, std::function<void(T&&, SockAddr&&)> cb)
39 {
40 startDiscovery(type, [cb](msgpack::object&& ob, SockAddr&& addr) { cb(ob.as<T>(), std::move(addr)); });
41 }
42
46 void startPublish(std::string_view type, const msgpack::sbuffer& pack_buf);
47 void startPublish(sa_family_t domain, std::string_view type, const msgpack::sbuffer& pack_buf);
48
49 template<typename T>
50 void startPublish(std::string_view type, const T& object)
51 {
52 msgpack::sbuffer buf;
53 msgpack::pack(buf, object);
54 startPublish(type, buf);
55 }
56
60 void stop();
61
65 bool stopDiscovery(std::string_view type);
66
70 bool stopPublish(std::string_view type);
71 bool stopPublish(sa_family_t domain, std::string_view type);
72
73 void connectivityChanged();
74
75 void stopConnectivityChanged();
76
77private:
78 class DomainPeerDiscovery;
79 std::unique_ptr<DomainPeerDiscovery> peerDiscovery4_;
80 std::unique_ptr<DomainPeerDiscovery> peerDiscovery6_;
81 std::shared_ptr<asio::io_context> ioContext_;
82 std::thread ioRunnner_;
83};
84
85} // namespace dht
dht::PeerDiscovery::stop
void stop()
dht::PeerDiscovery::stopDiscovery
bool stopDiscovery(std::string_view type)
dht::PeerDiscovery::startPublish
void startPublish(std::string_view type, const msgpack::sbuffer &pack_buf)
dht::PeerDiscovery::startDiscovery
void startDiscovery(std::string_view type, ServiceDiscoveredCallback callback)
dht::PeerDiscovery::stopPublish
bool stopPublish(std::string_view type)
dht::SockAddr
Definition sockaddr.h:47
dht
Definition callbacks.h:17