Back to Site
Loading...
Searching...
No Matches
peer_network.h
Go to the documentation of this file.
1#pragma once
2
13
14#include "librats/util/rats_export.h"
15#include "librats/core/bytes.h"
16#include "librats/wire/frame.h" // MessageType
20#include "librats/core/types.h" // CloseReason
21
22#include <cstdint>
23#include <functional>
24#include <optional>
25#include <string>
26#include <vector>
27
28namespace librats {
29
30class Peer;
31
32class RATS_API PeerNetwork {
33public:
34 virtual ~PeerNetwork() = default;
35 using MessageHandler = std::function<void(const Peer&, ByteView)>;
42 using DeadlineMessageHandler = std::function<void(const Peer&, ByteView, Deadline)>;
43
44 using PeerEventHandler = std::function<void(const Peer&)>;
50 using PeerDisconnectHandler = std::function<void(const PeerId&, CloseReason)>;
51 using DialFailedHandler = std::function<void(const Address&)>;
52 using PeerPathHandler = std::function<void(const Peer&, const PathStats&)>;
55 using LiveOutcomeHandler = std::function<void(const Peer&, uint64_t tag, bool delivered)>;
56
57 virtual const PeerId& local_id() const = 0;
58 virtual uint16_t listen_port() const = 0;
64 virtual uint8_t transports() const { return PeerTransportTcp; }
65 virtual const std::string& protocol() const = 0;
66 virtual void connect(const Address& address) = 0;
74 virtual bool send(const PeerId& to, MessageType type, ByteView payload,
75 SendOptions options = {}) = 0;
78 virtual bool broadcast(MessageType type, ByteView payload, SendOptions options = {}) = 0;
79 virtual std::vector<PeerId> connected_peers() const = 0;
80 virtual std::vector<PeerInfo> peers() const = 0;
81 virtual void on(MessageType type, MessageHandler handler) = 0;
86 on(type, [handler = std::move(handler)](const Peer& peer, ByteView payload) {
87 handler(peer, payload, Deadline{});
88 });
89 }
90
91 // Lifecycle hooks. Multiple subsystems (and the application) may subscribe;
92 // all run on a reactor thread. Register before start().
93 virtual void on_peer_connected(PeerEventHandler handler) = 0;
95 // An outbound dial WE initiated closed before it ever established (TCP connect
96 // refused/timed out, or the handshake failed). Carries the address we dialed.
97 // There is no on_peer_disconnected for a connection that never came up, so this
98 // is the only signal a redial policy gets that an in-flight dial has resolved.
99 virtual void on_dial_failed(DialFailedHandler handler) = 0;
104 virtual void on_peer_writable(PeerEventHandler /*handler*/) {}
113 virtual bool peer_writable(const PeerId& /*id*/) const { return true; }
121 virtual void on_peer_rerouted(PeerEventHandler /*handler*/) {}
128 virtual void on_peer_moved(PeerEventHandler /*handler*/) {}
133 virtual void on_peer_path(PeerPathHandler /*handler*/) {}
136 virtual std::optional<PathStats> path_stats(const PeerId& /*id*/) const { return std::nullopt; }
140 bool answers(const PeerId& id) const {
141 if (const std::optional<PathStats> path = path_stats(id))
142 return path->answers(std::chrono::steady_clock::now());
143 const std::vector<PeerId> peers = connected_peers();
144 for (const PeerId& peer : peers)
145 if (peer == id) return true;
146 return false;
147 }
148
156 virtual void set_send_ceiling(const PeerId& /*id*/, uint64_t /*bits_per_second*/) {}
164 virtual void on_live_outcome(MessageType /*type*/, LiveOutcomeHandler /*handler*/) {}
165};
166
167struct NodeContext; // node/node_context.h — bundles network + events + services
168
176class RATS_API Subsystem {
177public:
178 virtual ~Subsystem() = default;
179 virtual void attach(NodeContext& ctx) = 0;
180 virtual void start() = 0;
181 virtual void stop() = 0;
182};
183
184} // namespace librats
A dialable transport endpoint: a numeric IP + port.
Lightweight byte container aliases and a non-owning byte view.
Non-owning view over a contiguous run of bytes.
Definition bytes.h:27
std::function< void(const Peer &, ByteView, Deadline)> DeadlineMessageHandler
A message handler that is also told each message's deadline: for a live one (SendOptions::lifetime),...
virtual void on_peer_moved(PeerEventHandler)
The path to a connected peer changed under its connection: one end's address did (a NAT gave it anoth...
virtual void on(MessageType type, MessageHandler handler)=0
std::function< void(const Address &)> DialFailedHandler
virtual const std::string & protocol() const =0
app protocol id (e.g. "librats/1.0"); namespaces discovery
virtual void on_live_outcome(MessageType, LiveOutcomeHandler)
What becomes of each live message of type sent with a tag (SendOptions::tag), once per peer it went t...
virtual const PeerId & local_id() const =0
virtual void connect(const Address &address)=0
dial a discovered peer
virtual bool broadcast(MessageType type, ByteView payload, SendOptions options={})=0
Send to every connected peer.
virtual uint8_t transports() const
Which transports that port actually accepts, as a PeerTransports bitmask — the same value identify re...
std::function< void(const PeerId &, CloseReason)> PeerDisconnectHandler
A peer went away, and why.
virtual void on_with_deadline(MessageType type, DeadlineMessageHandler handler)
on(), with each message's deadline.
std::function< void(const Peer &, ByteView)> MessageHandler
virtual std::optional< PathStats > path_stats(const PeerId &) const
The latest path sample for a peer, from any thread; nullopt if the peer is not connected,...
std::function< void(const Peer &, const PathStats &)> PeerPathHandler
virtual void set_send_ceiling(const PeerId &, uint64_t)
Send to id no faster than bits_per_second, whatever its congestion controller paces at; 0 lifts the c...
bool answers(const PeerId &id) const
Whether id is reached over a link that answers (PathStats::answers) — connected, and not over a link ...
virtual void on_peer_disconnected(PeerDisconnectHandler handler)=0
std::function< void(const Peer &, uint64_t tag, bool delivered)> LiveOutcomeHandler
What became of a live message sent with a tag: delivered, or given up on (see LiveOutcome).
virtual ~PeerNetwork()=default
virtual void on_peer_rerouted(PeerEventHandler)
A connected peer's traffic moved to another connection: a cross-connect or a reconnect settled on a l...
virtual void on_peer_connected(PeerEventHandler handler)=0
virtual bool peer_writable(const PeerId &) const
The same question send() answers — "has this peer room for more?" — asked without sending anything.
virtual void on_dial_failed(DialFailedHandler handler)=0
virtual std::vector< PeerInfo > peers() const =0
snapshot incl. dialable addresses
virtual void on_peer_path(PeerPathHandler)
A periodic sample of each connected peer's path (see PathStats) — what a sender that sizes its own ou...
virtual uint16_t listen_port() const =0
our advertised listen port (shared by both transports)
virtual void on_peer_writable(PeerEventHandler)
A peer whose send queue had filled up has drained back under its mark, so sending to it may resume.
std::function< void(const Peer &)> PeerEventHandler
virtual bool send(const PeerId &to, MessageType type, ByteView payload, SendOptions options={})=0
Send to one peer.
virtual std::vector< PeerId > connected_peers() const =0
A pluggable network subsystem.
virtual void start()=0
virtual void stop()=0
virtual ~Subsystem()=default
virtual void attach(NodeContext &ctx)=0
Two-level wire framing: outer length-prefixed blocks, inner messages.
Definition node.h:75
@ PeerTransportTcp
Definition peer_info.h:25
MessageType
Inner-message kind. Application traffic uses App, addressed by channel.
Definition frame.h:40
Self-certifying peer identity.
Addressing/metadata for a peer — the shareable, persistable identity.