Back to Site
Loading...
Searching...
No Matches
peer_network.h
Go to the documentation of this file.
1#pragma once
2
14#include "core/bytes.h"
15#include "wire/frame.h" // MessageType
16#include "peer/peer_id.h"
17#include "peer/peer_info.h"
18#include "core/address.h"
19
20#include <cstdint>
21#include <functional>
22#include <vector>
23
24namespace librats {
25
26class Peer;
27
29public:
30 virtual ~PeerNetwork() = default;
31 using MessageHandler = std::function<void(const Peer&, ByteView)>;
32
33 using PeerEventHandler = std::function<void(const Peer&)>;
34 using PeerDisconnectHandler = std::function<void(const PeerId&)>;
35 using DialFailedHandler = std::function<void(const Address&)>;
36
37 virtual const PeerId& local_id() const = 0;
38 virtual uint16_t listen_port() const = 0;
39 virtual void connect(const Address& address) = 0;
40 virtual void send(const PeerId& to, MessageType type, ByteView payload) = 0;
41 virtual void broadcast(MessageType type, ByteView payload) = 0;
42 virtual std::vector<PeerId> connected_peers() const = 0;
43 virtual std::vector<PeerInfo> peers() const = 0;
44 virtual void on(MessageType type, MessageHandler handler) = 0;
45
46 // Lifecycle hooks. Multiple subsystems (and the application) may subscribe;
47 // all run on a reactor thread. Register before start().
48 virtual void on_peer_connected(PeerEventHandler handler) = 0;
50 // An outbound dial WE initiated closed before it ever established (TCP connect
51 // refused/timed out, or the handshake failed). Carries the address we dialed.
52 // There is no on_peer_disconnected for a connection that never came up, so this
53 // is the only signal a redial policy gets that an in-flight dial has resolved.
54 virtual void on_dial_failed(DialFailedHandler handler) = 0;
55};
56
57struct NodeContext; // node/node_context.h — bundles network + events + services
58
63class Subsystem {
64public:
65 virtual ~Subsystem() = default;
66 virtual void attach(NodeContext& ctx) = 0;
67 virtual void start() = 0;
68 virtual void stop() = 0;
69};
70
71} // namespace librats
A dialable transport address (ip + port).
virtual void on(MessageType type, MessageHandler handler)=0
std::function< void(const Address &)> DialFailedHandler
virtual const PeerId & local_id() const =0
virtual void connect(const Address &address)=0
dial a discovered peer
virtual void broadcast(MessageType type, ByteView payload)=0
std::function< void(const Peer &, ByteView)> MessageHandler
virtual void send(const PeerId &to, MessageType type, ByteView payload)=0
virtual void on_peer_disconnected(PeerDisconnectHandler handler)=0
virtual ~PeerNetwork()=default
virtual void on_peer_connected(PeerEventHandler handler)=0
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 TCP port
std::function< void(const Peer &)> PeerEventHandler
virtual std::vector< PeerId > connected_peers() const =0
std::function< void(const PeerId &)> PeerDisconnectHandler
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:65
MessageType
Inner-message kind. Application traffic uses App, addressed by channel.
Definition frame.h:38
Self-certifying peer identity.
Addressing/metadata for a peer — the shareable, persistable identity.