-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathjsonrpc.h
48 lines (39 loc) · 1.36 KB
/
jsonrpc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
struct JsonRpcParams {
string jsonrpcVersion;
};
struct JsonRpcQuery {
int id;
Json json;
condition_variable *completionCV;
mutex *completionMtx;
};
class JsonRpcException : public exception {
string _msg;
public:
JsonRpcException(string msg) { _msg = msg; };
virtual char const *what() const noexcept { return _msg.c_str(); }
};
class CJsonRpc : public IMessageObserver, public IJsonRpc {
private:
std::string _jsonrpcVersion;
unsigned int _lastId;
ILogger *_logger;
IWebSocketClient *_wsc;
// Map between request IDs and waiting requests
map<int, JsonRpcQuery> _queries;
mutex _queryMtx;
// Map between subscription IDs and subscribers
map<int, IWebSocketMessageObserver *> _wsSubscribers;
ConcurrentMapQueue<Json, int> *_responseQueue;
void updateConsumerThread(int subscriptionId);
int getNextId();
public:
CJsonRpc(IWebSocketClient *wsc, ILogger *logger, JsonRpcParams params);
virtual ~CJsonRpc() override { delete _responseQueue; }
virtual int connect(string node_url = "");
virtual void disconnect();
virtual Json request(Json jsonMap, long timeout_s = RESPONSE_TIMEOUT_S);
virtual void handleMessage(const string &payload);
virtual int subscribeWs(Json jsonMap, IWebSocketMessageObserver *observer);
virtual int unsubscribeWs(int subscriptionId, string method);
};