33#include "core/bytes.h"
39#include <unordered_map>
46 using Handler = std::function<void(
const PeerId& from,
const librats::Json& data)>;
47 using SendCallback = std::function<void(
bool ok,
const std::string& error)>;
57 void off(
const std::string& type);
61 void send(
const std::string& type,
const librats::Json& data,
SendCallback cb =
nullptr);
65 void send(
const PeerId& to,
const std::string& type,
const librats::Json& data,
74 void on_typed(
const PeerId& from, ByteView payload);
75 static Bytes encode(
const std::string& type,
const librats::Json& data);
82 PeerNetwork* network_ =
nullptr;
83 mutable std::mutex mutex_;
84 std::unordered_map<std::string, std::vector<Entry>> handlers_;
void attach(NodeContext &ctx) override
void on(const std::string &type, Handler handler)
Register a handler for type.
void once(const std::string &type, Handler handler)
Like on(), but the handler is removed right after it fires once.
void send(const PeerId &to, const std::string &type, const librats::Json &data, SendCallback cb=nullptr)
Send data of type to one peer.
std::function< void(bool ok, const std::string &error)> SendCallback
void send(const std::string &type, const librats::Json &data, SendCallback cb=nullptr)
Broadcast data of type to all connected peers.
std::function< void(const PeerId &from, const librats::Json &data)> Handler
void off(const std::string &type)
Remove every handler registered for type.
A pluggable network subsystem.
A lightweight handle to a connected peer.
Self-certifying peer identity.
The narrow contract a subsystem needs from the node — and nothing more.