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

Public Member Functions

virtual ~PeerNetwork ()=default
 
virtual const PeerIdlocal_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)=0
 Send to one peer.
 
virtual bool broadcast (MessageType type, ByteView payload)=0
 Send to every connected peer.
 
virtual std::vector< PeerIdconnected_peers () const =0
 
virtual std::vector< PeerInfopeers () const =0
 snapshot incl. dialable addresses
 
virtual void on (MessageType type, MessageHandler handler)=0
 
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.
 

Detailed Description

Definition at line 31 of file peer_network.h.

Member Typedef Documentation

◆ DialFailedHandler

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

Definition at line 43 of file peer_network.h.

◆ MessageHandler

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

Definition at line 34 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 42 of file peer_network.h.

◆ PeerEventHandler

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

Definition at line 36 of file peer_network.h.

Constructor & Destructor Documentation

◆ ~PeerNetwork()

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

Member Function Documentation

◆ broadcast()

virtual bool librats::PeerNetwork::broadcast ( MessageType  type,
ByteView  payload 
)
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.

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

◆ on_dial_failed()

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

Implemented in librats::Node.

◆ 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_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 82 of file peer_network.h.

◆ 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 91 of file peer_network.h.

◆ peers()

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

snapshot incl. dialable addresses

Implemented in librats::Node.

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

Implemented in librats::Node.

◆ 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 52 of file peer_network.h.

References librats::PeerTransportTcp.


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