Back to Site
Loading...
Searching...
No Matches
peer_network.h
Go to the documentation of this file.
1#pragma once
2
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 <string>
25#include <vector>
26
27namespace librats {
28
29class Peer;
30
31class RATS_API PeerNetwork {
32public:
33 virtual ~PeerNetwork() = default;
34 using MessageHandler = std::function<void(const Peer&, ByteView)>;
35
36 using PeerEventHandler = std::function<void(const Peer&)>;
42 using PeerDisconnectHandler = std::function<void(const PeerId&, CloseReason)>;
43 using DialFailedHandler = std::function<void(const Address&)>;
44
45 virtual const PeerId& local_id() const = 0;
46 virtual uint16_t listen_port() const = 0;
52 virtual uint8_t transports() const { return PeerTransportTcp; }
53 virtual const std::string& protocol() const = 0;
54 virtual void connect(const Address& address) = 0;
61 virtual bool send(const PeerId& to, MessageType type, ByteView payload) = 0;
64 virtual bool broadcast(MessageType type, ByteView payload) = 0;
65 virtual std::vector<PeerId> connected_peers() const = 0;
66 virtual std::vector<PeerInfo> peers() const = 0;
67 virtual void on(MessageType type, MessageHandler handler) = 0;
68
69 // Lifecycle hooks. Multiple subsystems (and the application) may subscribe;
70 // all run on a reactor thread. Register before start().
71 virtual void on_peer_connected(PeerEventHandler handler) = 0;
73 // An outbound dial WE initiated closed before it ever established (TCP connect
74 // refused/timed out, or the handshake failed). Carries the address we dialed.
75 // There is no on_peer_disconnected for a connection that never came up, so this
76 // is the only signal a redial policy gets that an in-flight dial has resolved.
77 virtual void on_dial_failed(DialFailedHandler handler) = 0;
82 virtual void on_peer_writable(PeerEventHandler /*handler*/) {}
91 virtual bool peer_writable(const PeerId& /*id*/) const { return true; }
92};
93
94struct NodeContext; // node/node_context.h — bundles network + events + services
95
100class RATS_API Subsystem {
101public:
102 virtual ~Subsystem() = default;
103 virtual void attach(NodeContext& ctx) = 0;
104 virtual void start() = 0;
105 virtual void stop() = 0;
106};
107
108} // 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
virtual bool broadcast(MessageType type, ByteView payload)=0
Send to every connected peer.
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 const PeerId & local_id() const =0
virtual void connect(const Address &address)=0
dial a discovered 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.
std::function< void(const Peer &, ByteView)> MessageHandler
virtual bool send(const PeerId &to, MessageType type, ByteView payload)=0
Send to one peer.
virtual void on_peer_disconnected(PeerDisconnectHandler handler)=0
virtual ~PeerNetwork()=default
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 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 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:73
@ PeerTransportTcp
Definition peer_info.h:24
MessageType
Inner-message kind. Application traffic uses App, addressed by channel.
Definition frame.h:39
Self-certifying peer identity.
Addressing/metadata for a peer — the shareable, persistable identity.