#include <node.h>
Public Member Functions | |
| Node (NodeConfig config) | |
| Construct a node from its configuration (see NodeConfig). | |
| ~Node () override | |
| Stops the node if still running, then releases all resources. | |
| Node (const Node &)=delete | |
| Node & | operator= (const Node &)=delete |
| template<class T> | |
| T * | add_subsystem (std::unique_ptr< T > subsystem) |
| Attach a subsystem (DHT, GossipSub, PingService…). | |
| bool | start () |
| Bring the node up: open the listener (if enabled), start the reactor pool, then start every attached subsystem. | |
| void | stop () |
| Tear the node down: stop subsystems (reverse order), close all connections, and join the reactor pool. | |
| const PeerId & | local_id () const noexcept override |
| Our self-certifying peer identity (the public key peers authenticate). | |
| uint16_t | listen_port () const noexcept override |
| The bound listen port (the actual port when the config requested 0). | |
| uint8_t | transports () const noexcept override |
| Transports this node is actually running, as a PeerTransports bitmask. | |
| const std::string & | protocol () const noexcept override |
| Application protocol identity bound into the handshake (see NodeConfig). | |
| EventBus & | events () noexcept |
| ServiceRegistry & | services () noexcept |
| void | connect (const Address &address) override |
| Dial a peer. | |
| void | connect (const std::string &host, uint16_t port) |
| Dial a peer. | |
| size_t | peer_count () const noexcept |
| Number of currently-established peers. | |
| size_t | standbys () const |
| Links held as standbys now: duplicates the peer table turned down, kept a while in case the one they lost to has gone dead (see on_established). | |
| std::vector< PeerInfo > | peers () const override |
| Snapshot of all established peers (id, addresses, direction, timing). | |
| std::optional< Peer > | peer (const PeerId &id) |
| Handle to a connected peer by id, or std::nullopt if not connected. | |
| std::vector< Address > | observed_addresses () const |
| Our own addresses as remote peers reported observing us at — their observed IP paired with our listen port. | |
| const NatStatus & | nat_status () const noexcept |
| What the mesh has shown about our own side of the NAT, from the endpoints datagram peers report observing our shared UDP socket at (see nat_status.h). | |
| bool | dial_direct (const Address &addr, TransportKind kind, const DialProfile &profile) override |
| Start exactly one dial to addr over kind, bypassing the transport race. | |
| std::optional< PeerRoute > | adopt_circuit (const PeerId &carrier, std::unique_ptr< Link > link, ConnRole role, bool connected) override |
| Adopt link as a connection carried by the peer carrier, on the reactor that owns the carrier's own connection. | |
| void | wake_circuit (PeerRoute route, uint32_t events) override |
| Deliver poll-equivalent events (PollIn / PollOut / PollErr, see core/io_poller.h) to a circuit connection — the events a socket-backed link would have got from the poller. | |
| void | close_circuit (PeerRoute route, CloseReason reason) override |
| Tear a circuit connection down. | |
| std::optional< PeerRoute > | dial_relayed (const PeerId &carrier, uint64_t cid, ForwardedCircuit circuit) override |
| Dial the far end through carrier, the relay. The connection is Outbound. | |
| std::optional< PeerRoute > | expect_relayed (const PeerId &carrier, uint64_t cid, ForwardedCircuit circuit) override |
| Accept the dial that will come through carrier, the relay, as an Inbound connection — under the same admission gate as adopt_circuit. | |
| bool | forward_datagrams (const PeerId &dialer, const PeerId &responder, uint64_t cid, std::shared_ptr< std::atomic< uint64_t > > bytes, uint64_t rate, uint64_t burst) override |
| As the relay: forward the packets of cid between dialer and responder, counting their bytes into bytes, at most rate bytes a second each way with bursts of burst (0: no rate; how it is held is UdpMux's, "Relayed connections"). | |
| void | stop_forwarding (uint64_t cid) override |
| size_t | max_peers () const noexcept |
| void | set_max_peers (size_t n) noexcept |
| bool | peer_limit_reached () const noexcept |
| bool | send (const PeerId &to, std::string_view channel, ByteView payload, SendOptions options={}) |
| Send raw bytes to one peer on a named channel. | |
| bool | broadcast (std::string_view channel, ByteView payload, SendOptions options={}) |
| Send raw bytes on a named channel to every connected peer. | |
| bool | peer_writable (const PeerId &id) const override |
| Whether a peer's send queue has room for more. | |
| void | on_peer_connected (PeerNetwork::PeerEventHandler cb) override |
| Subscribe to peer-connected events. The handler runs on a reactor thread. | |
| void | on_peer_disconnected (PeerNetwork::PeerDisconnectHandler cb) override |
| Subscribe to peer-disconnected events. The handler runs on a reactor thread. | |
| void | on_dial_failed (PeerNetwork::DialFailedHandler cb) override |
| Subscribe to failed-outbound-dial events. The handler runs on a reactor thread. | |
| void | on_peer_writable (PeerNetwork::PeerEventHandler cb) override |
| Subscribe to "this peer can be written to again" — fired when a peer whose send queue had filled past its mark has drained back under it. | |
| void | on_peer_rerouted (PeerNetwork::PeerEventHandler cb) override |
| Subscribe to "this peer's traffic moved to another connection" (see PeerNetwork::on_peer_rerouted). | |
| void | on_peer_moved (PeerNetwork::PeerEventHandler cb) override |
| Subscribe to "the path to this peer changed: one end moved" (see PeerNetwork::on_peer_moved). | |
| void | on_peer_path (PeerNetwork::PeerPathHandler cb) override |
| Subscribe to periodic path samples of every connected peer (see PathStats): the estimate to size a live sender to, the queue in front of its next message, round trip and loss. | |
| void | on_live_outcome (MessageType type, PeerNetwork::LiveOutcomeHandler cb) override |
| Subscribe to what becomes of the live messages of type sent with a tag (SendOptions::tag): delivered, or given up on. | |
| std::optional< PathStats > | path_stats (const PeerId &id) const override |
| The latest path sample for a peer — safe from any thread. | |
| void | set_send_ceiling (const PeerId &id, uint64_t bits_per_second) override |
| Send to id no faster than bits_per_second, whatever its congestion controller paces at; 0 lifts the ceiling. | |
| void | on (std::string_view channel, MessageRouter::Handler cb) |
| Register a handler for inbound messages on a named channel. | |
| template<class T> | |
| T * | subsystem () noexcept |
| MessageJson * | json () noexcept |
| The JSON messaging module if one was attached (add_subsystem<MessageJson>), else nullptr. | |
| bool | send (const PeerId &to, MessageType type, ByteView payload, SendOptions options={}) override |
| Send to one peer. | |
| bool | broadcast (MessageType type, ByteView payload, SendOptions options={}) override |
| Send to every connected peer. | |
| std::vector< PeerId > | connected_peers () const override |
| void | on (MessageType type, PeerNetwork::MessageHandler cb) override |
| void | on_with_deadline (MessageType type, PeerNetwork::DeadlineMessageHandler cb) override |
| on(), with each message's deadline. | |
| Public Member Functions inherited from librats::PeerNetwork | |
| virtual | ~PeerNetwork ()=default |
| bool | answers (const PeerId &id) const |
| Whether id is reached over a link that answers (PathStats::answers) — connected, and not over a link gone dead, which a peer that moved leaves behind until it times out. | |
| Public Member Functions inherited from librats::DialService | |
| virtual | ~DialService ()=default |
| Public Member Functions inherited from librats::CircuitService | |
| virtual | ~CircuitService ()=default |
Friends | |
| class | Peer |
Additional Inherited Members | |
| Public Types inherited from librats::PeerNetwork | |
| using | MessageHandler = std::function<void(const Peer&, ByteView)> |
| using | DeadlineMessageHandler = std::function<void(const Peer&, ByteView, Deadline)> |
| A message handler that is also told each message's deadline: for a live one (SendOptions::lifetime), the moment it was due to arrive here by the time its sender gave it, on this node's clock (Frame::deadline) — what a node passing it on gives the next hop, so that a message relayed does not get its lifetime afresh at each. | |
| using | PeerEventHandler = std::function<void(const Peer&)> |
| using | PeerDisconnectHandler = std::function<void(const PeerId&, CloseReason)> |
| A peer went away, and why. | |
| using | DialFailedHandler = std::function<void(const Address&)> |
| using | PeerPathHandler = std::function<void(const Peer&, const PathStats&)> |
| using | LiveOutcomeHandler = std::function<void(const Peer&, uint64_t tag, bool delivered)> |
| What became of a live message sent with a tag: delivered, or given up on (see LiveOutcome). | |
|
explicit |
Construct a node from its configuration (see NodeConfig).
This only loads the identity and prepares the layers; no socket is opened until start().
Referenced by Node(), and operator=().
|
override |
Stops the node if still running, then releases all resources.
|
delete |
References Node().
|
inline |
Attach a subsystem (DHT, GossipSub, PingService…).
Call before start(); the node owns it (single ownership), gives it a PeerNetwork on start(), and stops it on stop(). Returns a non-owning pointer to the just-added subsystem so the caller can drive its API without a separate get()/move dance — valid for the node's lifetime: auto* files = node.add_subsystem(std::make_unique<FileTransfer>("./dl"));
Definition at line 101 of file node.h.
References subsystem().
|
overridevirtual |
Adopt link as a connection carried by the peer carrier, on the reactor that owns the carrier's own connection.
| role | Outbound for a circuit we opened, Inbound for one opened to us. It is what the secure handshake and the peer table's duplicate resolution read, so it must say who asked. |
| connected | whether the far end is already there. An inbound circuit is (the opener would not have sent it otherwise); an outbound one is not until its acceptance arrives, and until then the connection waits in Connecting for a PollOut. |
Implements librats::CircuitService.
|
overridevirtual |
Send to every connected peer.
Implements librats::PeerNetwork.
| bool librats::Node::broadcast | ( | std::string_view | channel, |
| ByteView | payload, | ||
| SendOptions | options = {} ) |
Send raw bytes on a named channel to every connected peer.
Coarser than send(): the answer is the reactors' view as they last left it and does not count what this call has just handed over, because a broadcast is dispatched per reactor rather than per peer. Good enough to pace a periodic fan-out; when precise backpressure matters — a file transfer, a large stream — address peers individually with send().
|
overridevirtual |
Tear a circuit connection down.
Thread-safe; a no-op for a route that is already gone.
Implements librats::CircuitService.
|
overridevirtual |
Dial a peer.
Non-blocking: the connection (transport + handshake) completes asynchronously and surfaces via on_peer_connected. A duplicate or self-connection is detected and dropped after the handshake.
The transport is chosen by the dialer (see node/dialer.h): the preferred one first, the other raced in after NodeConfig::transport_fallback_ms if the first has not come up. Whichever completes its handshake first wins.
Implements librats::PeerNetwork.
| void librats::Node::connect | ( | const std::string & | host, |
| uint16_t | port ) |
Dial a peer.
Non-blocking: the connection (transport + handshake) completes asynchronously and surfaces via on_peer_connected. A duplicate or self-connection is detected and dropped after the handshake.
The transport is chosen by the dialer (see node/dialer.h): the preferred one first, the other raced in after NodeConfig::transport_fallback_ms if the first has not come up. Whichever completes its handshake first wins.
|
overridevirtual |
Implements librats::PeerNetwork.
|
overridevirtual |
Start exactly one dial to addr over kind, bypassing the transport race.
Non-blocking; the outcome surfaces through the node's ordinary peer-connected event. Returns false when the node cannot carry that transport at all (no datagram socket, or an address family the one socket cannot reach), in which case nothing was started and no failure event will follow.
Implements librats::DialService.
|
overridevirtual |
Dial the far end through carrier, the relay. The connection is Outbound.
Implements librats::CircuitService.
|
inlinenoexcept |
Definition at line 134 of file node.h.
Referenced by wake_circuit().
|
overridevirtual |
Accept the dial that will come through carrier, the relay, as an Inbound connection — under the same admission gate as adopt_circuit.
Implements librats::CircuitService.
|
overridevirtual |
As the relay: forward the packets of cid between dialer and responder, counting their bytes into bytes, at most rate bytes a second each way with bursts of burst (0: no rate; how it is held is UdpMux's, "Relayed connections").
False when the two are not both datagram peers on one socket — the circuit must then be a byte stream.
Implements librats::CircuitService.
|
noexcept |
The JSON messaging module if one was attached (add_subsystem<MessageJson>), else nullptr.
Convenience over subsystem<MessageJson>(); defined in node.cpp.
|
inlineoverridevirtualnoexcept |
The bound listen port (the actual port when the config requested 0).
Both transports share it, so one advertised address is dialable over either.
Implements librats::PeerNetwork.
|
inlineoverridevirtualnoexcept |
Our self-certifying peer identity (the public key peers authenticate).
Implements librats::PeerNetwork.
|
inlinenoexcept |
What the mesh has shown about our own side of the NAT, from the endpoints datagram peers report observing our shared UDP socket at (see nat_status.h).
Also published in services() as ExternalAddressService, which is how a NAT-traversal subsystem reaches it.
| std::vector< Address > librats::Node::observed_addresses | ( | ) | const |
Our own addresses as remote peers reported observing us at — their observed IP paired with our listen port.
De-duplicated and bounded; populated as peers send their identify message. Useful for NAT awareness / advertising.
|
inlineoverridevirtual |
Implements librats::PeerNetwork.
|
inline |
|
inlineoverridevirtual |
Subscribe to failed-outbound-dial events. The handler runs on a reactor thread.
Implements librats::PeerNetwork.
|
inlineoverridevirtual |
Subscribe to what becomes of the live messages of type sent with a tag (SendOptions::tag): delivered, or given up on.
The handler runs on a reactor thread, once per tagged message per peer it went to.
Reimplemented from librats::PeerNetwork.
Definition at line 281 of file node.h.
Referenced by Peer.
|
inlineoverridevirtual |
Subscribe to peer-connected events. The handler runs on a reactor thread.
Implements librats::PeerNetwork.
|
inlineoverridevirtual |
Subscribe to peer-disconnected events. The handler runs on a reactor thread.
Implements librats::PeerNetwork.
|
inlineoverridevirtual |
Subscribe to "the path to this peer changed: one end moved" (see PeerNetwork::on_peer_moved).
The handler runs on a reactor thread.
Reimplemented from librats::PeerNetwork.
|
inlineoverridevirtual |
Subscribe to periodic path samples of every connected peer (see PathStats): the estimate to size a live sender to, the queue in front of its next message, round trip and loss.
Registering turns sampling on, every NodeConfig::path_sample_ms (100 ms if that is 0). The handler runs on a reactor thread, once per peer per interval, so keep it cheap.
Reimplemented from librats::PeerNetwork.
|
inlineoverridevirtual |
Subscribe to "this peer's traffic moved to another connection" (see PeerNetwork::on_peer_rerouted).
The handler runs on a reactor thread.
Reimplemented from librats::PeerNetwork.
|
inlineoverridevirtual |
Subscribe to "this peer can be written to again" — fired when a peer whose send queue had filled past its mark has drained back under it.
The other half of send() returning false; an application that never checks that return never needs this. The handler runs on a reactor thread.
Reimplemented from librats::PeerNetwork.
|
inlineoverridevirtual |
on(), with each message's deadline.
One handler per type, of either kind. Default: every deadline the epoch, all a mock that merely moves messages can say.
Reimplemented from librats::PeerNetwork.
|
inlineoverridevirtual |
The latest path sample for a peer — safe from any thread.
nullopt when the peer is not connected, no sample has been taken yet, or sampling is off (NodeConfig::path_sample_ms == 0 and no on_peer_path handler).
Reimplemented from librats::PeerNetwork.
Handle to a connected peer by id, or std::nullopt if not connected.
|
inlinenoexcept |
|
inlinenoexcept |
|
overridevirtual |
Whether a peer's send queue has room for more.
False for a peer that is not connected.
The same question send() answers, asked without sending anything: it weighs both halves of what the peer is carrying — the bytes the reactor has queued, and the bytes a caller has handed to send() that the reactor has not taken up yet. So it is safe to poll: a caller that has just filled the queue in a tight loop keeps being told "no room" until the reactor has actually looked at what it was given, rather than being told "go on" because nothing observable has changed yet.
It stays a hint about a queue that drains as it is read, so the ordinary flow is unchanged: the signal to stop is the return of send(), and the signal to resume is on_peer_writable. This is for a caller that must wait for room on a thread of its own — the event alone cannot serve it, because a queue that filled only with bytes still in transit never crossed anything on the connection and so raises no event when they drain.
Reimplemented from librats::PeerNetwork.
|
inlineoverridevirtual |
Snapshot of all established peers (id, addresses, direction, timing).
Implements librats::PeerNetwork.
|
inlineoverridevirtualnoexcept |
Application protocol identity bound into the handshake (see NodeConfig).
Implements librats::PeerNetwork.
|
overridevirtual |
Send to one peer.
Implements librats::PeerNetwork.
References send().
| bool librats::Node::send | ( | const PeerId & | to, |
| std::string_view | channel, | ||
| ByteView | payload, | ||
| SendOptions | options = {} ) |
Send raw bytes to one peer on a named channel.
Non-blocking; the payload is copied into the peer's send queue. No-op if the peer is not connected.
| to | destination peer id |
| channel | application channel name (interned to a 16-bit id) |
| payload | message bytes (copied) |
| options | how it is delivered: reliably by default, or as a live message with a lifetime — node.send(peer, "video", frame, {150ms}) (see SendOptions) |
"Stop" is meant literally, and yielding is part of it: the mark is re-tested inside the reactor task this call hands off to, and it is that test which flips the peer to unwritable and later raises on_peer_writable. A caller that answers a false by looping straight back into send() therefore starves the very thread that would tell it to stop — the queue keeps growing while the signal it is waiting for never gets a turn to be produced.
Referenced by send().
|
inlinenoexcept |
|
overridevirtual |
Send to id no faster than bits_per_second, whatever its congestion controller paces at; 0 lifts the ceiling.
For a sender that knows several connections share one uplink narrower than each path beyond it — copies of a stream to several peers — and keeps their bursts from filling its queue together, which no one connection's controller sees. Kept for the peer across a change of link; forgotten when it disconnects. Paced on UDP connections (TCP leaves pacing to the kernel). Any thread. Default: not offered (a mock's sends are not paced).
Reimplemented from librats::PeerNetwork.
| size_t librats::Node::standbys | ( | ) | const |
Links held as standbys now: duplicates the peer table turned down, kept a while in case the one they lost to has gone dead (see on_established).
Diagnostics and tests.
| bool librats::Node::start | ( | ) |
Bring the node up: open the listener (if enabled), start the reactor pool, then start every attached subsystem.
Register callbacks and attach subsystems BEFORE calling this.
| void librats::Node::stop | ( | ) |
Tear the node down: stop subsystems (reverse order), close all connections, and join the reactor pool.
Safe to call once; idempotent.
|
overridevirtual |
Implements librats::CircuitService.
|
inlinenoexcept |
Definition at line 297 of file node.h.
Referenced by add_subsystem().
|
inlineoverridevirtualnoexcept |
Transports this node is actually running, as a PeerTransports bitmask.
May be narrower than the config asked for — a UDP socket that could not be bound leaves the node TCP-only rather than failing to start.
Reimplemented from librats::PeerNetwork.
|
overridevirtual |
Deliver poll-equivalent events (PollIn / PollOut / PollErr, see core/io_poller.h) to a circuit connection — the events a socket-backed link would have got from the poller.
Thread-safe, and a no-op for a route that is already gone.
Implements librats::CircuitService.
References events().
|
friend |