Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 624
Expand file tree
/
Copy pathWebSocketWrapper.h
More file actions
412 lines (345 loc) · 18.7 KB
/
Copy pathWebSocketWrapper.h
File metadata and controls
412 lines (345 loc) · 18.7 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Authored by Alex Hultman, 2018-2026.
* Intellectual property of third-party.
* 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.
*/
#include "App.h"
#include "Utilities.h"
#include <v8.h>
#include "v8-fast-api-calls.h"
using namespace v8;
/* todo: probably isCorked, cork should be exposed? */
struct WebSocketWrapper {
template <bool SSL>
static inline uWS::WebSocket<SSL, true, PerSocketData> *getWebSocket(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0);
if (!ws) {
args.GetReturnValue().Set(isolate->ThrowException(v8::Exception::Error(String::NewFromUtf8(isolate, "Invalid access of closed uWS.WebSocket/SSLWebSocket.", NewStringType::kNormal).ToLocalChecked())));
}
return ws;
}
static inline void invalidateWsObject(const FunctionCallbackInfo<Value> &args) {
//args.This()->SetAlignedPointerInInternalField(0, nullptr);
setInternalPointer(args.This(), nullptr);
}
/* Takes nothing returns holder (only used to fool TypeScript, as a conversion from WS to UserData) */
template <bool SSL>
static void uWS_WebSocket_getUserData(const FunctionCallbackInfo<Value> &args) {
args.GetReturnValue().Set(args.This());
}
/* Takes string topic */
template <bool SSL>
static void uWS_WebSocket_subscribe(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
NativeString<true> topic(isolate, args[0]);
if (topic.isInvalid(args)) {
return;
}
bool success = ws->subscribe(topic.getString());
args.GetReturnValue().Set(Boolean::New(isolate, success));
}
}
/* Takes string topic, returns boolean success */
template <bool SSL>
static void uWS_WebSocket_unsubscribe(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
NativeString<true> topic(isolate, args[0]);
if (topic.isInvalid(args)) {
return;
}
bool success = ws->unsubscribe(topic.getString());
args.GetReturnValue().Set(Boolean::New(isolate, success));
}
}
/* Takes string topic, message, returns boolean success */
template <bool SSL>
static void uWS_WebSocket_publish(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
if (missingArguments(2, args)) {
return;
}
bool isBinary = args[2]->BooleanValue(isolate);
bool compress = args[3]->BooleanValue(isolate);
NativeString<true> topic(isolate, args[0]);
if (topic.isInvalid(args)) {
return;
}
NativeString<true> message(isolate, args[1]);
if (message.isInvalid(args)) {
return;
}
bool success = ws->publish(topic.getString(), message.getString(), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
args.GetReturnValue().Set(Boolean::New(isolate, success));
}
}
/* It would make sense to call terminate "close" and call close "end" to line up with HTTP */
/* That also makes sense seince close takes message and code -> you can end with a string message */
/* Takes nothing returns nothing */
template <bool SSL>
static void uWS_WebSocket_close(const FunctionCallbackInfo<Value> &args) {
auto *ws = getWebSocket<SSL>(args);
if (ws) {
invalidateWsObject(args);
ws->close();
}
}
/* Takes code, message, returns undefined */
template <bool SSL>
static void uWS_WebSocket_end(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
int code = 0;
if (args.Length() >= 1) {
code = args[0]->Uint32Value(isolate->GetCurrentContext()).ToChecked();
}
NativeString<true> message(args.GetIsolate(), args[1]);
if (message.isInvalid(args)) {
return;
}
invalidateWsObject(args);
ws->end(code, message.getString());
}
}
/* Takes nothing returns arraybuffer */
template <bool SSL>
static void uWS_WebSocket_getRemoteAddress(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
std::string_view ip = ws->getRemoteAddress();
args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length()));
}
}
/* Takes nothing returns arraybuffer */
template <bool SSL>
static void uWS_WebSocket_getRemoteAddressAsText(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
std::string_view ip = ws->getRemoteAddressAsText();
args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length()));
}
}
/* Takes nothing, returns integer */
template <bool SSL>
static void uWS_WebSocket_getRemotePort(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
unsigned int port = ws->getRemotePort();
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port));
}
}
/* Takes nothing, returns integer */
template <bool SSL>
static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
unsigned int bufferedAmount = ws->getBufferedAmount();
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, bufferedAmount));
}
}
/* Takes message, isBinary, compressed. Returns true on success, false otherwise */
template <bool SSL>
static void uWS_WebSocket_sendFirstFragment(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
NativeString message(args.GetIsolate(), args[0]);
if (message.isInvalid(args)) {
return;
}
unsigned int sendStatus = ws->sendFirstFragment(message.getString(), args[1]->BooleanValue(isolate) ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, args[2]->BooleanValue(isolate));
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, sendStatus));
}
}
/* Takes message, compressed. Returns true on success, false otherwise */
template <bool SSL>
static void uWS_WebSocket_sendFragment(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
NativeString message(args.GetIsolate(), args[0]);
if (message.isInvalid(args)) {
return;
}
unsigned int sendStatus = ws->sendFragment(message.getString(), args[1]->BooleanValue(isolate));
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, sendStatus));
}
}
/* Takes message, compressed. Returns true on success, false otherwise */
template <bool SSL>
static void uWS_WebSocket_sendLastFragment(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
NativeString message(args.GetIsolate(), args[0]);
if (message.isInvalid(args)) {
return;
}
unsigned int sendStatus = ws->sendLastFragment(message.getString(), args[1]->BooleanValue(isolate));
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, sendStatus));
}
}
/* Takes message, isBinary, compressed. Returns true on success, false otherwise */
template <bool SSL>
static void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
bool isBinary = args[1]->BooleanValue(isolate);
bool compress = args[2]->BooleanValue(isolate);
NativeString<true> message(args.GetIsolate(), args[0]);
if (message.isInvalid(args)) {
return;
}
unsigned int sendStatus = ws->send(message.getString(), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, sendStatus));
}
}
/* Takes topic string, returns bool */
template <bool SSL>
static void uWS_WebSocket_isSubscribed(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
NativeString<true> topic(args.GetIsolate(), args[0]);
if (topic.isInvalid(args)) {
return;
}
bool subscribed = ws->isSubscribed(topic.getString());
args.GetReturnValue().Set(Boolean::New(isolate, subscribed));
}
}
/* Takes message. Returns true on success, false otherwise */
template <bool SSL>
static void uWS_WebSocket_ping(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
NativeString<true> message(args.GetIsolate(), args[0]);
if (message.isInvalid(args)) {
return;
}
/* This is a wrapper that does not exist in the C++ project */
unsigned int sendStatus = ws->send(message.getString(), uWS::OpCode::PING);
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, sendStatus));
}
}
/* Takes function, returns this */
template <bool SSL>
static void uWS_WebSocket_cork(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
ws->cork([cb = Local<Function>::Cast(args[0]), isolate]() {
/* No need for CallJS here */
cb->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 0, nullptr).IsEmpty();
});
args.GetReturnValue().Set(args.This());
}
}
/* This one is wrapped instead of iterateTopics as JS-people will put their hands in wood chipper for sure. */
template <bool SSL>
static void uWS_WebSocket_getTopics(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
Local<Array> topicsArray = Array::New(isolate, 0);
ws->iterateTopics([&topicsArray, isolate](std::string_view topic) {
Local<String> topicString = String::NewFromUtf8(isolate, topic.data(), NewStringType::kNormal, topic.length()).ToLocalChecked();
topicsArray->Set(isolate->GetCurrentContext(), topicsArray->Length(), topicString).IsNothing();
});
args.GetReturnValue().Set(topicsArray);
}
}
/* V8 fast call fast path for send - called directly from JIT-optimised code.
* Requirements: no JS heap allocation, no JS execution, Uint8Array arg only.
* A null internal-field pointer (closed socket) sets options.fallback = true so V8
* re-invokes the slow path which throws the proper exception. */
// Version A: Handles Strings
template <bool SSL>
static uint32_t uWS_WebSocket_send_fast_string(v8::Local<v8::Object> receiver,
const v8::FastOneByteString& message,
bool isBinary, bool compress) {
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) getInternalPointer(receiver);//->GetAlignedPointerFromInternalField(0);
if (!ws) return 0;
return ws->send(std::string_view(message.data, message.length),
isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
}
// Version B: Handles ArrayBuffer/TypedArray
template <bool SSL>
static uint32_t uWS_WebSocket_send_fast_buffer(v8::Local<v8::Object> receiver,
v8::Local<v8::Value> message,
bool isBinary, bool compress) {
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) getInternalPointer(receiver);//->GetAlignedPointerFromInternalField(0);
if (!ws) return 0;
char* data = nullptr;
size_t length = 0;
if (message->IsArrayBufferView()) {
auto view = message.As<v8::ArrayBufferView>();
length = view->ByteLength();
data = static_cast<char*>(view->Buffer()->GetBackingStore()->Data()) + view->ByteOffset();
} else if (message->IsArrayBuffer()) {
auto ab = message.As<v8::ArrayBuffer>();
length = ab->ByteLength();
data = static_cast<char*>(ab->GetBackingStore()->Data());
} else {
// Not a buffer type we handle fast
return 0;
}
return ws->send(std::string_view(data, length),
isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
}
template <bool SSL>
static Local<Object> init(Isolate *isolate) {
Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate);
if (SSL) {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket", NewStringType::kNormal).ToLocalChecked());
} else {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket", NewStringType::kNormal).ToLocalChecked());
}
wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
/* Register our functions */
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "sendFirstFragment", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_sendFirstFragment<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "sendFragment", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_sendFragment<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "sendLastFragment", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_sendLastFragment<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getUserData", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getUserData<SSL>));
static v8::CFunction fast_send = v8::CFunction::Make(uWS_WebSocket_send_fast_buffer<SSL>);
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_send<SSL>/*, Local<Value>(), Local<Signature>(), 0, ConstructorBehavior::kThrow, SideEffectType::kHasSideEffect, &fast_send*/));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_end<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_close<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getBufferedAmount", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getBufferedAmount<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddress<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "subscribe", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_subscribe<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "unsubscribe", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_unsubscribe<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "publish", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_publish<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_cork<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ping", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_ping<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemotePort<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed<SSL>));
/* This one does not exist in C++ */
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getTopics", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getTopics<SSL>));
/* Create the template */
Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
return wsObjectLocal;
}
};
You can’t perform that action at this time.
