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