-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathproxy.cc
226 lines (177 loc) · 6.26 KB
/
proxy.cc
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "./proxy.h"
#include <cstdint>
#include "./context.h"
#include "./module.h"
#include "./socket.h"
#include "util/arguments.h"
#include "util/async_scope.h"
#include "util/error.h"
#include "util/uvwork.h"
#ifdef ZMQ_HAS_STEERABLE_PROXY
namespace zmq {
struct ProxyContext {
std::string address;
uint32_t error = 0;
explicit ProxyContext(std::string&& address) : address(std::move(address)) {}
};
Proxy::Proxy(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Proxy>(info), async_context(Env(), "Proxy"),
module(*static_cast<Module*>(info.Data())) {
Arg::Validator const args{
Arg::Required<Arg::Object>("Front-end must be a socket object"),
Arg::Required<Arg::Object>("Back-end must be a socket object"),
};
if (args.ThrowIfInvalid(info)) {
return;
}
front_ref.Reset(info[0].As<Napi::Object>(), 1);
Socket::Unwrap(front_ref.Value());
if (Env().IsExceptionPending()) {
return;
}
back_ref.Reset(info[1].As<Napi::Object>(), 1);
Socket::Unwrap(back_ref.Value());
if (Env().IsExceptionPending()) {
return;
}
}
Proxy::~Proxy() = default;
void Proxy::Close() {}
Napi::Value Proxy::Run(const Napi::CallbackInfo& info) {
if (Arg::Validator{}.ThrowIfInvalid(info)) {
return Env().Undefined();
}
auto* front = Socket::Unwrap(front_ref.Value());
if (Env().IsExceptionPending()) {
return Env().Undefined();
}
auto* back = Socket::Unwrap(back_ref.Value());
if (Env().IsExceptionPending()) {
return Env().Undefined();
}
if (front->endpoints == 0) {
ErrnoException(Env(), EINVAL, "Front-end socket must be bound or connected")
.ThrowAsJavaScriptException();
return Env().Undefined();
}
if (back->endpoints == 0) {
ErrnoException(Env(), EINVAL, "Back-end socket must be bound or connected")
.ThrowAsJavaScriptException();
return Env().Undefined();
}
auto* context = Context::Unwrap(front->context_ref.Value());
if (Env().IsExceptionPending()) {
return Env().Undefined();
}
control_sub = zmq_socket(context->context, ZMQ_DEALER);
if (control_sub == nullptr) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return Env().Undefined();
}
control_pub = zmq_socket(context->context, ZMQ_DEALER);
if (control_pub == nullptr) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return Env().Undefined();
}
/* Use `this` pointer as unique identifier for control socket. */
auto address = std::string("inproc://zmq.proxycontrol.")
+ std::to_string(reinterpret_cast<uintptr_t>(this));
/* Connect publisher so we can start queueing control messages. */
if (zmq_connect(control_pub, address.c_str()) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return Env().Undefined();
}
front->state = Socket::State::Blocked;
back->state = Socket::State::Blocked;
auto res = Napi::Promise::Deferred::New(Env());
auto run_ctx = std::make_shared<ProxyContext>(std::move(address));
auto* front_ptr = front->socket;
auto* back_ptr = back->socket;
auto status = UvQueue(
Env(),
[this, run_ctx, front_ptr, back_ptr]() {
/* Don't access V8 internals here! Executed in worker thread. */
if (zmq_bind(control_sub, run_ctx->address.c_str()) < 0) {
run_ctx->error = static_cast<uint32_t>(zmq_errno());
return;
}
if (zmq_proxy_steerable(front_ptr, back_ptr, nullptr, control_sub) < 0) {
run_ctx->error = static_cast<uint32_t>(zmq_errno());
return;
}
},
[this, front, back, run_ctx, res]() {
AsyncScope const scope(Env(), async_context);
front->Close();
back->Close();
[[maybe_unused]] auto err1 = zmq_close(control_pub);
assert(err1 == 0);
[[maybe_unused]] auto err2 = zmq_close(control_sub);
assert(err2 == 0);
control_pub = nullptr;
control_sub = nullptr;
if (run_ctx->error != 0) {
res.Reject(
ErrnoException(Env(), static_cast<int32_t>(run_ctx->error)).Value());
return;
}
res.Resolve(Env().Undefined());
});
if (status < 0) {
ErrnoException(Env(), EBADF).ThrowAsJavaScriptException();
return Env().Undefined();
}
return res.Promise();
}
void Proxy::SendCommand(const char* command) {
/* Don't send commands if the proxy has terminated. */
if (control_pub == nullptr) {
ErrnoException(Env(), EBADF).ThrowAsJavaScriptException();
return;
}
while (zmq_send_const(control_pub, command, strlen(command), ZMQ_DONTWAIT) < 0) {
if (zmq_errno() != EINTR) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return;
}
}
}
void Proxy::Pause(const Napi::CallbackInfo& info) {
if (Arg::Validator{}.ThrowIfInvalid(info)) {
return;
}
SendCommand("PAUSE");
}
void Proxy::Resume(const Napi::CallbackInfo& info) {
if (Arg::Validator{}.ThrowIfInvalid(info)) {
return;
}
SendCommand("RESUME");
}
void Proxy::Terminate(const Napi::CallbackInfo& info) {
if (Arg::Validator{}.ThrowIfInvalid(info)) {
return;
}
SendCommand("TERMINATE");
}
Napi::Value Proxy::GetFrontEnd(const Napi::CallbackInfo& /*info*/) {
return front_ref.Value();
}
Napi::Value Proxy::GetBackEnd(const Napi::CallbackInfo& /*info*/) {
return back_ref.Value();
}
void Proxy::Initialize(Module& module, Napi::Object& exports) {
auto proto = {
InstanceMethod<&Proxy::Run>("run"),
InstanceMethod<&Proxy::Pause>("pause"),
InstanceMethod<&Proxy::Resume>("resume"),
InstanceMethod<&Proxy::Terminate>("terminate"),
InstanceAccessor<&Proxy::GetFrontEnd>("frontEnd"),
InstanceAccessor<&Proxy::GetBackEnd>("backEnd"),
};
auto constructor = DefineClass(exports.Env(), "Proxy", proto, &module);
module.Proxy = Napi::Persistent(constructor);
exports.Set("Proxy", constructor);
}
} // namespace zmq
#endif