Back to Site
Loading...
Searching...
No Matches
librats::PeerNetwork Class Referenceabstract

#include <peer_network.h>

Inheritance diagram for librats::PeerNetwork:
[legend]

Public Types

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).

Public Member Functions

virtual ~PeerNetwork ()=default
virtual const PeerId & local_id () const =0
virtual uint16_t listen_port () const =0
 our advertised listen port (shared by both transports)
virtual uint8_t transports () const
 Which transports that port actually accepts, as a PeerTransports bitmask — the same value identify reports to peers.
virtual const std::string & protocol () const =0
 app protocol id (e.g. "librats/1.0"); namespaces discovery
virtual void connect (const Address &address)=0
 dial a discovered peer
virtual bool send (const PeerId &to, MessageType type, ByteView payload, SendOptions options={})=0
 Send to one peer.
virtual bool broadcast (MessageType type, ByteView payload, SendOptions options={})=0
 Send to every connected peer.
virtual std::vector< PeerId > connected_peers () const =0
virtual std::vector< PeerInfo > peers () const =0
 snapshot incl. dialable addresses
virtual void on (MessageType type, MessageHandler handler)=0
virtual void on_with_deadline (MessageType type, DeadlineMessageHandler handler)
 on(), with each message's deadline.
virtual void on_peer_connected (PeerEventHandler handler)=0
virtual void on_peer_disconnected (PeerDisconnectHandler handler)=0
virtual void on_dial_failed (DialFailedHandler handler)=0
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.
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_peer_rerouted (PeerEventHandler)
 A connected peer's traffic moved to another connection: a cross-connect or a reconnect settled on a link other than the one "connected" fired for, and that one is closed.
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 another port, its network changed) and the connection followed it there.
virtual void on_peer_path (PeerPathHandler)
 A periodic sample of each connected peer's path (see PathStats) — what a sender that sizes its own output to the path, a live encoder above all, steers by.
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, sampling is off, or no sample has been taken yet.
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.
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 ceiling.
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 to: the moment it is delivered or given up on, as far as the sender can tell — sooner than a receiver could say, and for a message given up on before a byte of it left, the only word there is.

Detailed Description

Definition at line 32 of file peer_network.h.

Member Typedef Documentation

◆ DeadlineMessageHandler

using librats::PeerNetwork::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.

The epoch for a reliable message, and for a live one that came over TCP or a byte-stream relay, which do not say.

Definition at line 42 of file peer_network.h.

◆ DialFailedHandler

using librats::PeerNetwork::DialFailedHandler = std::function<void(const Address&)>

Definition at line 51 of file peer_network.h.

◆ LiveOutcomeHandler

using librats::PeerNetwork::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).

Definition at line 55 of file peer_network.h.

◆ MessageHandler

using librats::PeerNetwork::MessageHandler = std::function<void(const Peer&, ByteView)>

Definition at line 35 of file peer_network.h.

◆ PeerDisconnectHandler

using librats::PeerNetwork::PeerDisconnectHandler = std::function<void(const PeerId&, CloseReason)>

A peer went away, and why.

The reason matters as much as the event: an application dropped as a slow consumer (CloseReason::SlowConsumer) has to slow down, while one whose peer simply left should reconnect — and without the reason those look identical, so the usual answer to both is to redial and repeat whatever caused it.

Definition at line 50 of file peer_network.h.

◆ PeerEventHandler

using librats::PeerNetwork::PeerEventHandler = std::function<void(const Peer&)>

Definition at line 44 of file peer_network.h.

◆ PeerPathHandler

using librats::PeerNetwork::PeerPathHandler = std::function<void(const Peer&, const PathStats&)>

Definition at line 52 of file peer_network.h.

Constructor & Destructor Documentation

◆ ~PeerNetwork()

virtual librats::PeerNetwork::~PeerNetwork ( )
virtualdefault

Member Function Documentation

◆ answers()

bool librats::PeerNetwork::answers ( const PeerId & id) const
inline

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.

Where there are no path samples, connected.

Definition at line 140 of file peer_network.h.

References connected_peers(), path_stats(), and peers().

◆ broadcast()

virtual bool librats::PeerNetwork::broadcast ( MessageType type,
ByteView payload,
SendOptions options = {} )
pure virtual

Send to every connected peer.

Returns
whether every one of them still has room, so a subsystem that fans out can pause on the slowest.

Implemented in librats::Node.

◆ connect()

virtual void librats::PeerNetwork::connect ( const Address & address)
pure virtual

dial a discovered peer

Implemented in librats::Node.

◆ connected_peers()

virtual std::vector< PeerId > librats::PeerNetwork::connected_peers ( ) const
pure virtual

Implemented in librats::Node.

Referenced by answers().

◆ listen_port()

virtual uint16_t librats::PeerNetwork::listen_port ( ) const
pure virtual

our advertised listen port (shared by both transports)

Implemented in librats::Node.

◆ local_id()

virtual const PeerId & librats::PeerNetwork::local_id ( ) const
pure virtual

Implemented in librats::Node.

◆ on()

virtual void librats::PeerNetwork::on ( MessageType type,
MessageHandler handler )
pure virtual

Implemented in librats::Node.

Referenced by on_with_deadline().

◆ on_dial_failed()

virtual void librats::PeerNetwork::on_dial_failed ( DialFailedHandler handler)
pure virtual

Implemented in librats::Node.

◆ on_live_outcome()

virtual void librats::PeerNetwork::on_live_outcome ( MessageType ,
LiveOutcomeHandler  )
inlinevirtual

What becomes of each live message of type sent with a tag (SendOptions::tag), once per peer it went to: the moment it is delivered or given up on, as far as the sender can tell — sooner than a receiver could say, and for a message given up on before a byte of it left, the only word there is.

Nothing for a message to a peer that disconnects first. Runs on a reactor thread; register before start(). Default: not offered (nothing is reported).

Reimplemented in librats::Node.

Definition at line 164 of file peer_network.h.

◆ on_peer_connected()

virtual void librats::PeerNetwork::on_peer_connected ( PeerEventHandler handler)
pure virtual

Implemented in librats::Node.

◆ on_peer_disconnected()

virtual void librats::PeerNetwork::on_peer_disconnected ( PeerDisconnectHandler handler)
pure virtual

Implemented in librats::Node.

◆ on_peer_moved()

virtual void librats::PeerNetwork::on_peer_moved ( PeerEventHandler )
inlinevirtual

The path to a connected peer changed under its connection: one end's address did (a NAT gave it another port, its network changed) and the connection followed it there.

PeerInfo::endpoint is where the peer is seen now. Its other peers, which knew it at the old address, may not hear it from the new one until told (Rendezvous tells them). Runs on a reactor thread. Default: not offered (a mock's peers never move).

Reimplemented in librats::Node.

Definition at line 128 of file peer_network.h.

◆ on_peer_path()

virtual void librats::PeerNetwork::on_peer_path ( PeerPathHandler )
inlinevirtual

A periodic sample of each connected peer's path (see PathStats) — what a sender that sizes its own output to the path, a live encoder above all, steers by.

Registering one turns sampling on. Runs on a reactor thread. Default: not offered (nothing subscribes).

Reimplemented in librats::Node.

Definition at line 133 of file peer_network.h.

◆ on_peer_rerouted()

virtual void librats::PeerNetwork::on_peer_rerouted ( PeerEventHandler )
inlinevirtual

A connected peer's traffic moved to another connection: a cross-connect or a reconnect settled on a link other than the one "connected" fired for, and that one is closed.

What was sent on it and not yet delivered is lost, with no disconnect to say so — a subsystem that tells a peer something once, on connect (what it offers, what it asks for, a request), tells it again here. What did arrive may so arrive twice. Runs on a reactor thread. Default: not offered (a mock's peers never move).

Reimplemented in librats::Node.

Definition at line 121 of file peer_network.h.

◆ on_peer_writable()

virtual void librats::PeerNetwork::on_peer_writable ( PeerEventHandler )
inlinevirtual

A peer whose send queue had filled up has drained back under its mark, so sending to it may resume.

The counterpart of send() returning false; a subsystem that never checks that return never needs this either. Runs on a reactor thread. Default: not offered (nothing subscribes).

Reimplemented in librats::Node.

Definition at line 104 of file peer_network.h.

◆ on_with_deadline()

virtual void librats::PeerNetwork::on_with_deadline ( MessageType type,
DeadlineMessageHandler handler )
inlinevirtual

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 in librats::Node.

Definition at line 85 of file peer_network.h.

References on().

◆ path_stats()

virtual std::optional< PathStats > librats::PeerNetwork::path_stats ( const PeerId & ) const
inlinevirtual

The latest path sample for a peer, from any thread; nullopt if the peer is not connected, sampling is off, or no sample has been taken yet.

Reimplemented in librats::Node.

Definition at line 136 of file peer_network.h.

Referenced by answers().

◆ peer_writable()

virtual bool librats::PeerNetwork::peer_writable ( const PeerId & ) const
inlinevirtual

The same question send() answers — "has this peer room for more?" — asked without sending anything.

For a subsystem that paces a long fan-out from a thread of its own (StorageManager streaming a snapshot): the event alone cannot serve it, because a queue filled only with bytes handed to send() that the reactor has not taken up yet never crossed anything on the connection, so nothing raises on_peer_writable when they drain. Safe to poll — it weighs those in-flight bytes too. False for an unknown peer. Default: always writable, all a mock that merely moves messages can claim.

Reimplemented in librats::Node.

Definition at line 113 of file peer_network.h.

◆ peers()

virtual std::vector< PeerInfo > librats::PeerNetwork::peers ( ) const
pure virtual

snapshot incl. dialable addresses

Implemented in librats::Node.

Referenced by answers().

◆ protocol()

virtual const std::string & librats::PeerNetwork::protocol ( ) const
pure virtual

app protocol id (e.g. "librats/1.0"); namespaces discovery

Implemented in librats::Node.

◆ send()

virtual bool librats::PeerNetwork::send ( const PeerId & to,
MessageType type,
ByteView payload,
SendOptions options = {} )
pure virtual

Send to one peer.

Returns
whether that peer's send queue still has room; false means "stop and wait for on_peer_writable" — the message is queued either way, but continuing past this is what gets a peer dropped as a slow consumer. Also false if the peer is not connected. Wait on the event and not on a poll of the queue's state: the answer is re-derived on the reactor thread, so a caller that spins instead of yielding never lets it change. options can make it a live message (see SendOptions).

Implemented in librats::Node.

◆ set_send_ceiling()

virtual void librats::PeerNetwork::set_send_ceiling ( const PeerId & ,
uint64_t  )
inlinevirtual

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 in librats::Node.

Definition at line 156 of file peer_network.h.

◆ transports()

virtual uint8_t librats::PeerNetwork::transports ( ) const
inlinevirtual

Which transports that port actually accepts, as a PeerTransports bitmask — the same value identify reports to peers.

A subsystem that has to make the port reachable from outside (PortMappingService) needs to know which protocols to ask the router for. Default: TCP only, which is all a mock that merely moves messages has to claim.

Reimplemented in librats::Node.

Definition at line 64 of file peer_network.h.

References librats::PeerTransportTcp.


The documentation for this class was generated from the following file: