31#include <unordered_map>
51 std::optional<JsonRpcError>
error;
57 return {
"", std::move(err)};
70 std::function<void(std::string_view params_json)>;
97 std::string_view params_json,
101 bool notify(std::string_view method, std::string_view params_json);
104 void handle_message(
const Message& msg);
105 void handle_object(std::string_view obj_json);
106 void dispatch_response(std::string_view response_json);
107 void dispatch_request(std::string_view request_json);
110 std::atomic<std::uint64_t> next_id_{1};
113 std::unordered_map<std::string, JsonRpcMethodHandler> methods_;
114 std::unordered_map<std::string, JsonRpcNotificationHandler> notifications_;
115 std::unordered_map<std::uint64_t, JsonRpcResponseCallback> pending_;
bool notify(std::string_view method, std::string_view params_json)
Emit a notification.
JsonRpcPeer & operator=(const JsonRpcPeer &)=delete
void on_notification(std::string_view name, JsonRpcNotificationHandler handler)
Register a notification handler.
JsonRpcPeer(const JsonRpcPeer &)=delete
void register_method(std::string_view name, JsonRpcMethodHandler handler)
Register a method handler.
bool send_request(std::string_view method, std::string_view params_json, JsonRpcResponseCallback callback)
Send a request.
JsonRpcPeer(MessageChannel &channel)
Wrap a message channel.
Abstract base for message-oriented channels.
Definition message_channel.hpp:70
Definition generated_event_source.hpp:33
std::function< void(std::string_view params_json)> JsonRpcNotificationHandler
Notification handler (incoming notifications carry no id and expect no response).
Definition json_rpc.hpp:69
std::function< void(const JsonRpcResult &response)> JsonRpcResponseCallback
Callback for the response to a request we sent.
Definition json_rpc.hpp:73
std::function< JsonRpcResult(std::string_view params_json)> JsonRpcMethodHandler
Request handler: given a JSON-encoded params string (empty if omitted), return a JsonRpcResult.
Definition json_rpc.hpp:64
Structured JSON-RPC error. Matches spec §5.1.
Definition json_rpc.hpp:36
static JsonRpcError method_not_found()
Definition json_rpc.hpp:43
static JsonRpcError internal_error()
Definition json_rpc.hpp:45
int code
Definition json_rpc.hpp:37
static JsonRpcError invalid_params()
Definition json_rpc.hpp:44
static JsonRpcError invalid_request()
Definition json_rpc.hpp:42
std::string data_json
Optional JSON-encoded error.data (empty → omit).
Definition json_rpc.hpp:39
static JsonRpcError parse_error()
Definition json_rpc.hpp:41
std::string message
Definition json_rpc.hpp:38
Result of a request handler: success JSON (result field) or error.
Definition json_rpc.hpp:49
std::optional< JsonRpcError > error
Definition json_rpc.hpp:51
static JsonRpcResult ok(std::string result_json)
Definition json_rpc.hpp:53
std::string result_json
JSON-encoded result (e.g., "42" or "\"ok\"").
Definition json_rpc.hpp:50
static JsonRpcResult fail(JsonRpcError err)
Definition json_rpc.hpp:56
A single structured message as delivered by the channel.
Definition message_channel.hpp:39