Back to Site
Loading...
Searching...
No Matches
ping_service.h
Go to the documentation of this file.
1#pragma once
2
18#include "node/peer_network.h"
19#include "peer/peer.h"
20#include "peer/peer_id.h"
21
22#include <atomic>
23#include <chrono>
24#include <condition_variable>
25#include <cstdint>
26#include <mutex>
27#include <optional>
28#include <thread>
29#include <unordered_map>
30
31namespace librats {
32
33class PingService final : public Subsystem {
34public:
35 explicit PingService(std::chrono::milliseconds interval = std::chrono::seconds(10));
36 ~PingService() override;
37
38 void attach(NodeContext& ctx) override;
39 void start() override;
40 void stop() override;
41
43 std::optional<std::chrono::milliseconds> last_rtt(const PeerId& id) const;
44
46 size_t alive_peer_count() const;
47
48private:
49 void run();
50 void handle(const Peer& peer, ByteView payload);
51 void ping_all();
52
53 PeerNetwork* network_ = nullptr;
54 std::chrono::milliseconds interval_;
55
56 std::thread thread_;
57 std::atomic<bool> running_{false};
58 std::mutex wait_mutex_;
59 std::condition_variable wake_;
60
61 mutable std::mutex mutex_;
62 std::unordered_map<PeerId, std::chrono::milliseconds, PeerId::Hash> rtt_;
63};
64
65} // namespace librats
void attach(NodeContext &ctx) override
std::optional< std::chrono::milliseconds > last_rtt(const PeerId &id) const
Most recent measured round-trip time to a peer, if one has been seen.
~PingService() override
PingService(std::chrono::milliseconds interval=std::chrono::seconds(10))
void stop() override
size_t alive_peer_count() const
Number of peers we have received at least one pong from.
void start() override
A pluggable network subsystem.
Definition node.h:65
A lightweight handle to a connected peer.
Self-certifying peer identity.
The narrow contract a subsystem needs from the node — and nothing more.