rpc.hpp
3.0 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#pragma once
#include <functional>
#include <future>
#include <thread>
#include "concurrent_deque.hpp"
#include "json.hpp"
#include "jsc/jsc_types.hpp"
#include "jsc/jsc_protected.hpp"
namespace realm {
class ObjectSchema;
namespace rpc {
using json = nlohmann::json;
using RPCObjectID = u_int64_t;
using RPCRequest = std::function<json(const json)>;
class RPCWorker {
public:
RPCWorker();
~RPCWorker();
template<typename Fn>
json add_task(Fn&&);
void invoke_callback(json);
json resolve_callback(json args);
std::future<json> add_promise();
bool try_run_task();
void stop();
json try_pop_callback();
bool should_stop();
private:
bool m_stop = false;
int m_depth = 0;
#if __APPLE__
std::thread m_thread;
CFRunLoopRef m_loop;
#endif
ConcurrentDeque<std::function<void()>> m_tasks;
ConcurrentDeque<std::promise<json>> m_promises;
ConcurrentDeque<json> m_callbacks;
};
class RPCServer {
public:
RPCServer();
~RPCServer();
json perform_request(std::string const& name, json&& args);
bool try_run_task();
private:
JSGlobalContextRef m_context;
std::mutex m_request_mutex;
std::map<std::string, RPCRequest> m_requests;
std::map<RPCObjectID, js::Protected<JSObjectRef>> m_objects;
std::map<RPCObjectID, js::Protected<JSObjectRef>> m_callbacks;
// The key here is the same as the value in m_callbacks. We use the raw pointer as a key here,
// because protecting the value in m_callbacks pins the function object and prevents it from being moved
// by the garbage collector upon compaction.
std::map<JSObjectRef, RPCObjectID> m_callback_ids;
RPCObjectID m_session_id;
RPCWorker m_worker;
u_int64_t m_callback_call_counter;
uint64_t m_reset_counter = 0;
std::mutex m_pending_callbacks_mutex;
std::map<std::pair<uint64_t, uint64_t>, std::promise<json>> m_pending_callbacks;
static JSValueRef run_callback(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *exception);
RPCObjectID store_object(JSObjectRef object);
JSObjectRef get_object(RPCObjectID) const;
JSObjectRef get_realm_constructor() const;
json serialize_json_value(JSValueRef value);
JSValueRef deserialize_json_value(const json dict);
};
} // rpc
} // realm