My Project 3.2.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
peer_discovery.h
1/*
2 * Copyright (C) 2014-2023 Savoir-faire Linux Inc.
3 * Author(s) : Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
4 * Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "def.h"
23#include "sockaddr.h"
24#include "infohash.h"
25#include "logger.h"
26#include "utils.h"
27
28#include <asio/steady_timer.hpp>
29#include <thread>
30
31namespace asio {
32class io_context;
33}
34
35namespace dht {
36
37class OPENDHT_PUBLIC PeerDiscovery
38{
39public:
40 static constexpr in_port_t DEFAULT_PORT = 8888;
41 using ServiceDiscoveredCallback = std::function<void(msgpack::object &&, SockAddr &&)>;
42
43 PeerDiscovery(in_port_t port = DEFAULT_PORT,
44 std::shared_ptr<asio::io_context> ioContext = {},
45 std::shared_ptr<Logger> logger = {});
47
52 void startDiscovery(const std::string &type, ServiceDiscoveredCallback callback);
53
54 template <typename T>
55 void startDiscovery(const std::string &type, std::function<void(T &&, SockAddr &&)> cb) {
56 startDiscovery(type, [cb](msgpack::object &&ob, SockAddr &&addr) {
57 cb(ob.as<T>(), std::move(addr));
58 });
59 }
60
64 void startPublish(const std::string &type, const msgpack::sbuffer &pack_buf);
65 void startPublish(sa_family_t domain, const std::string &type, const msgpack::sbuffer &pack_buf);
66
67 template <typename T>
68 void startPublish(const std::string &type, const T &object) {
69 msgpack::sbuffer buf;
70 msgpack::pack(buf, object);
71 startPublish(type, buf);
72 }
73
77 void stop();
78
82 bool stopDiscovery(const std::string &type);
83
87 bool stopPublish(const std::string &type);
88 bool stopPublish(sa_family_t domain, const std::string &type);
89
90 void connectivityChanged();
91
92 void stopConnectivityChanged();
93
94private:
95 class DomainPeerDiscovery;
96 std::unique_ptr<DomainPeerDiscovery> peerDiscovery4_;
97 std::unique_ptr<DomainPeerDiscovery> peerDiscovery6_;
98 std::shared_ptr<asio::io_context> ioContext_;
99 std::thread ioRunnner_;
100};
101
102} // namespace dht
bool stopDiscovery(const std::string &type)
bool stopPublish(const std::string &type)
void startDiscovery(const std::string &type, ServiceDiscoveredCallback callback)
void startPublish(const std::string &type, const msgpack::sbuffer &pack_buf)