30#include "util/rats_export.h"
34#include "core/bytes.h"
40#include <unordered_map>
47 using Handler = std::function<void(
const PeerId& from,
const librats::Json& data)>;
48 using SendCallback = std::function<void(
bool ok,
const std::string& error)>;
58 void off(
const std::string& type);
62 void send(
const std::string& type,
const librats::Json& data,
SendCallback cb =
nullptr);
66 void send(
const PeerId& to,
const std::string& type,
const librats::Json& data,
75 void on_typed(
const PeerId& from, ByteView payload);
76 static Bytes encode(
const std::string& type,
const librats::Json& data);
83 PeerNetwork* network_ =
nullptr;
84 mutable std::mutex mutex_;
85 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.