aboutsummaryrefslogtreecommitdiff
path: root/src/rpc_server.h
blob: f5e1f4b74034c4ccabdf8bf07ab8de6421d77f63 (plain)
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
49
50
51
#ifndef RPC_SERVER
#define RPC_SERVER

#include <map>
#include <string>

#include <rapidjson/document.h>

#include "http_server.h"

class RPCRequest {
    private:
        bool valid = false;
        HTTPRequest* request;

        rapidjson::Document body;

        rapidjson::Value id;
        std::string method;
        rapidjson::Value params;

        void response(rapidjson::Value& result);

    public:
        RPCRequest(HTTPRequest* request);
        ~RPCRequest();

        bool is_valid() { return this->valid; };

        rapidjson::MemoryPoolAllocator<>& get_allocator() { return this->body.GetAllocator(); };
        std::string get_method() { return this->method; }
        rapidjson::Value& get_params() { return this->params; }

        void result(rapidjson::Value result);
        void error(rapidjson::Value error);
        void close();
};

class RPCServer {
    private:
        HTTPServer http_server;

    public:
        RPCServer(unsigned long addr, unsigned short port);

        HTTPServer& get_http_server() { return this->http_server; }

        RPCRequest* receive_request();
};

#endif