Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
json_rpc.hpp
Go to the documentation of this file.
1#pragma once
2
3// JSON-RPC 2.0 over MessageChannel.
4//
5// `JsonRpcPeer` is symmetric: any peer can send requests, receive
6// requests, emit notifications, and receive notifications. One peer
7// plus one MessageChannel equals one side of a JSON-RPC session.
8//
9// Wire format is pure JSON text (per the spec). The peer serializes
10// into the channel via `send_text()`, so transports that distinguish
11// text from binary (WebSocket) will carry it as text frames.
12//
13// Error semantics follow JSON-RPC 2.0 §5:
14// -32700 Parse error
15// -32600 Invalid Request
16// -32601 Method not found
17// -32602 Invalid params
18// -32603 Internal error
19// User code can return custom codes from request handlers.
20
22
23#include <atomic>
24#include <cstdint>
25#include <functional>
26#include <memory>
27#include <mutex>
28#include <optional>
29#include <string>
30#include <string_view>
31#include <unordered_map>
32
33namespace pulp::runtime {
34
37 int code = 0;
38 std::string message;
39 std::string data_json;
40
41 static JsonRpcError parse_error() { return {-32700, "Parse error", ""}; }
42 static JsonRpcError invalid_request() { return {-32600, "Invalid Request", ""}; }
43 static JsonRpcError method_not_found() { return {-32601, "Method not found", ""}; }
44 static JsonRpcError invalid_params() { return {-32602, "Invalid params", ""}; }
45 static JsonRpcError internal_error() { return {-32603, "Internal error", ""}; }
46};
47
50 std::string result_json;
51 std::optional<JsonRpcError> error;
52
53 static JsonRpcResult ok(std::string result_json) {
54 return {std::move(result_json), std::nullopt};
55 }
57 return {"", std::move(err)};
58 }
59};
60
65 std::function<JsonRpcResult(std::string_view params_json)>;
66
70 std::function<void(std::string_view params_json)>;
71
74 std::function<void(const JsonRpcResult& response)>;
75
77public:
80 explicit JsonRpcPeer(MessageChannel& channel);
82
83 JsonRpcPeer(const JsonRpcPeer&) = delete;
85
88 void register_method(std::string_view name, JsonRpcMethodHandler handler);
89
91 void on_notification(std::string_view name, JsonRpcNotificationHandler handler);
92
96 bool send_request(std::string_view method,
97 std::string_view params_json,
99
101 bool notify(std::string_view method, std::string_view params_json);
102
103private:
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);
108
109 MessageChannel* channel_;
110 std::atomic<std::uint64_t> next_id_{1};
111
112 std::mutex mutex_;
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_;
116};
117
118} // namespace pulp::runtime
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