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