Initial Worker threads/context aware support

This commit is contained in:
Alex Hultman 2019-12-05 04:07:18 +01:00
parent 4f6c9d85b7
commit 2c87f3593d
9 changed files with 199 additions and 112 deletions

View File

@ -55,8 +55,8 @@ void prepare() {
/* Build for Unix systems */ /* Build for Unix systems */
void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, char *arch) { void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, char *arch) {
char *c_shared = "-DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c"; char *c_shared = "-DLIBUS_USE_LIBUV -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c";
char *cpp_shared = "-DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp"; char *cpp_shared = "-DLIBUS_USE_LIBUV -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp";
for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) {
run("%s %s -I targets/node-%s/include/node", compiler, c_shared, versions[i].name); run("%s %s -I targets/node-%s/include/node", compiler, c_shared, versions[i].name);
@ -77,8 +77,8 @@ void copy_files() {
void build_windows(char *arch) { void build_windows(char *arch) {
/* For all versions */ /* For all versions */
for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) {
run("cl /D \"LIBUS_USE_LIBUV\" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c " run("cl /D \"LIBUS_USE_LIBUV\" /D \"LIBUS_USE_OPENSSL\" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c "
"uWebSockets/uSockets/src/eventing/*.c /I targets/node-%s/include/node /I uWebSockets/src /EHsc " "uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c /I targets/node-%s/include/node /I uWebSockets/src /EHsc "
"/Ox /LD /Fedist/uws_win32_%s_%s.node src/addon.cpp targets/node-%s/node.lib", "/Ox /LD /Fedist/uws_win32_%s_%s.node src/addon.cpp targets/node-%s/node.lib",
versions[i].name, arch, versions[i].abi, versions[i].name); versions[i].name, arch, versions[i].abi, versions[i].name);
} }

33
examples/WorkerThreads.js vendored Normal file
View File

@ -0,0 +1,33 @@
/* This example spawns two worker threads, each with their own
* server listening to the same port (Linux feature). */
const uWS = require('../dist/uws.js');
const port = 9001;
const { Worker, isMainThread, threadId } = require('worker_threads');
const os = require('os');
if (isMainThread) {
/* Main thread loops over all CPUs */
/* In this case we only spawn two (hardcoded) */
/*os.cpus()*/[0, 1].forEach(() => {
/* Spawn a new thread running this source file */
new Worker(__filename);
});
/* I guess main thread joins by default? */
} else {
/* Here we are inside a worker thread */
const app = uWS./*SSL*/App({
key_file_name: 'misc/key.pem',
cert_file_name: 'misc/cert.pem',
passphrase: '1234'
}).get('/*', (res, req) => {
res.end('Hello Worker!');
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port + ' from thread ' + threadId);
} else {
console.log('Failed to listen to port ' + port + ' from thread ' + threadId);
}
});
}

View File

@ -6,6 +6,11 @@ using namespace v8;
/* uWS.App.ws('/pattern', behavior) */ /* uWS.App.ws('/pattern', behavior) */
template <typename APP> template <typename APP>
void uWS_App_ws(const FunctionCallbackInfo<Value> &args) { void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
/* This one is default constructed with defaults */ /* This one is default constructed with defaults */
typename APP::WebSocketBehavior behavior = {}; typename APP::WebSocketBehavior behavior = {};
@ -63,15 +68,16 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
} }
/* Open handler is NOT optional for the wrapper */ /* Open handler is NOT optional for the wrapper */
behavior.open = [openPf = std::move(openPf)](auto *ws, auto *req) { behavior.open = [openPf = std::move(openPf), perContextData](auto *ws, auto *req) {
Isolate *isolate = perContextData->isolate;
HandleScope hs(isolate); HandleScope hs(isolate);
/* Create a new websocket object */ /* Create a new websocket object */
Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>(); Local<Object> wsObject = perContextData->wsTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
wsObject->SetAlignedPointerInInternalField(0, ws); wsObject->SetAlignedPointerInInternalField(0, ws);
/* Create the HttpRequest wrapper */ /* Create the HttpRequest wrapper */
Local<Object> reqObject = HttpRequestWrapper::getReqInstance(); Local<Object> reqObject = perContextData->reqTemplate.Get(isolate)->Clone();
reqObject->SetAlignedPointerInInternalField(0, req); reqObject->SetAlignedPointerInInternalField(0, req);
/* Attach a new V8 object with pointer to us, to us */ /* Attach a new V8 object with pointer to us, to us */
@ -88,7 +94,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
/* Message handler is always optional */ /* Message handler is always optional */
if (messagePf != Undefined(isolate)) { if (messagePf != Undefined(isolate)) {
behavior.message = [messagePf = std::move(messagePf)](auto *ws, std::string_view message, uWS::OpCode opCode) { behavior.message = [messagePf = std::move(messagePf), isolate](auto *ws, std::string_view message, uWS::OpCode opCode) {
HandleScope hs(isolate); HandleScope hs(isolate);
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length()); Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
@ -106,7 +112,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
/* Drain handler is always optional */ /* Drain handler is always optional */
if (drainPf != Undefined(isolate)) { if (drainPf != Undefined(isolate)) {
behavior.drain = [drainPf = std::move(drainPf)](auto *ws) { behavior.drain = [drainPf = std::move(drainPf), isolate](auto *ws) {
HandleScope hs(isolate); HandleScope hs(isolate);
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
@ -126,7 +132,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
}; };
/* Close handler is NOT optional for the wrapper */ /* Close handler is NOT optional for the wrapper */
behavior.close = [closePf = std::move(closePf)](auto *ws, int code, std::string_view message) { behavior.close = [closePf = std::move(closePf), isolate](auto *ws, int code, std::string_view message) {
HandleScope hs(isolate); HandleScope hs(isolate);
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length()); Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
@ -165,21 +171,22 @@ void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
return; return;
} }
/* todo: make it UniquePersistent */ /* This function requires perContextData */
std::shared_ptr<Persistent<Function>> pf(new Persistent<Function>); PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1])); UniquePersistent<Function> cb(args.GetIsolate(), Local<Function>::Cast(args[1]));
(app->*f)(std::string(pattern.getString()), [pf](auto *res, auto *req) { (app->*f)(std::string(pattern.getString()), [cb = std::move(cb), perContextData](auto *res, auto *req) {
Isolate *isolate = perContextData->isolate;
HandleScope hs(isolate); HandleScope hs(isolate);
Local<Object> resObject = HttpResponseWrapper::getResInstance<APP>(); Local<Object> resObject = perContextData->resTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
resObject->SetAlignedPointerInInternalField(0, res); resObject->SetAlignedPointerInInternalField(0, res);
Local<Object> reqObject = HttpRequestWrapper::getReqInstance(); Local<Object> reqObject = perContextData->reqTemplate.Get(isolate)->Clone();
reqObject->SetAlignedPointerInInternalField(0, req); reqObject->SetAlignedPointerInInternalField(0, req);
Local<Value> argv[] = {resObject, reqObject}; Local<Value> argv[] = {resObject, reqObject};
Local<Function>::New(isolate, *pf)->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 2, argv).IsEmpty(); cb.Get(isolate)->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 2, argv).IsEmpty();
/* Properly invalidate req */ /* Properly invalidate req */
reqObject->SetAlignedPointerInInternalField(0, nullptr); reqObject->SetAlignedPointerInInternalField(0, nullptr);
@ -195,6 +202,8 @@ template <typename APP>
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) { void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
Isolate *isolate = args.GetIsolate();
/* Require at least two arguments */ /* Require at least two arguments */
if (args.Length() < 2) { if (args.Length() < 2) {
/* Throw here */ /* Throw here */
@ -203,7 +212,7 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
} }
/* Callback is last */ /* Callback is last */
auto cb = [&args](auto *token) { auto cb = [&args, isolate](auto *token) {
/* Return a false boolean if listen failed */ /* Return a false boolean if listen failed */
Local<Value> argv[] = {token ? Local<Value>::Cast(External::New(isolate, token)) : Local<Value>::Cast(Boolean::New(isolate, false))}; Local<Value> argv[] = {token ? Local<Value>::Cast(External::New(isolate, token)) : Local<Value>::Cast(Boolean::New(isolate, false))};
Local<Function>::Cast(args[args.Length() - 1])->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 1, argv).IsEmpty(); Local<Function>::Cast(args[args.Length() - 1])->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 1, argv).IsEmpty();
@ -236,6 +245,8 @@ template <typename APP>
void uWS_App_publish(const FunctionCallbackInfo<Value> &args) { void uWS_App_publish(const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
Isolate *isolate = args.GetIsolate();
NativeString topic(isolate, args[0]); NativeString topic(isolate, args[0]);
NativeString message(isolate, args[1]); NativeString message(isolate, args[1]);
app->publish(topic.getString(), message.getString(), BooleanValue(isolate, args[2]) ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, BooleanValue(isolate, args[3])); app->publish(topic.getString(), message.getString(), BooleanValue(isolate, args[2]) ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, BooleanValue(isolate, args[3]));
@ -243,10 +254,19 @@ void uWS_App_publish(const FunctionCallbackInfo<Value> &args) {
template <typename APP> template <typename APP>
void uWS_App(const FunctionCallbackInfo<Value> &args) { void uWS_App(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate); Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
APP *app; APP *app;
/* These won't outlive the function, uSockets will have to copy strings it wants to keep! */
std::string keyFileName;
std::string certFileName;
std::string passphrase;
std::string dhParamsFileName;
/* Name differs based on type */ /* Name differs based on type */
if (std::is_same<APP, uWS::SSLApp>::value) { if (std::is_same<APP, uWS::SSLApp>::value) {
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLApp", NewStringType::kNormal).ToLocalChecked()); appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLApp", NewStringType::kNormal).ToLocalChecked());
@ -254,11 +274,6 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
/* We fill these below */ /* We fill these below */
us_socket_context_options_t ssl_options = {}; us_socket_context_options_t ssl_options = {};
static std::string keyFileName;
static std::string certFileName;
static std::string passphrase;
static std::string dhParamsFileName;
/* Read the options object (SSL options) */ /* Read the options object (SSL options) */
if (args.Length() == 1) { if (args.Length() == 1) {
@ -308,6 +323,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
ssl_options.ssl_prefer_low_memory_usage = BooleanValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ssl_prefer_low_memory_usage", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); ssl_options.ssl_prefer_low_memory_usage = BooleanValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ssl_prefer_low_memory_usage", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
} }
/* uSockets should really copy strings it wants to keep */
app = new APP(ssl_options); app = new APP(ssl_options);
} else { } else {
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App", NewStringType::kNormal).ToLocalChecked()); appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App", NewStringType::kNormal).ToLocalChecked());
@ -326,58 +342,60 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
/* All the http methods */ /* All the http methods */
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::get, args); uWS_App_get<APP>(&APP::get, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "post", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "post", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::post, args); uWS_App_get<APP>(&APP::post, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "options", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "options", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::options, args); uWS_App_get<APP>(&APP::options, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "del", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "del", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::del, args); uWS_App_get<APP>(&APP::del, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "patch", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "patch", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::patch, args); uWS_App_get<APP>(&APP::patch, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "put", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "put", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::put, args); uWS_App_get<APP>(&APP::put, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "head", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "head", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::head, args); uWS_App_get<APP>(&APP::head, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "connect", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "connect", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::connect, args); uWS_App_get<APP>(&APP::connect, args);
})); }, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "trace", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "trace", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::trace, args); uWS_App_get<APP>(&APP::trace, args);
})); }, args.Data()));
/* Any http method */ /* Any http method */
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "any", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) { appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "any", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, [](auto &args) {
uWS_App_get<APP>(&APP::any, args); uWS_App_get<APP>(&APP::any, args);
})); }, args.Data()));
/* ws, listen */ /* ws, listen */
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_ws<APP>)); appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_ws<APP>, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_listen<APP>)); appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_listen<APP>, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "publish", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_publish<APP>)); appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "publish", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_publish<APP>, args.Data()));
Local<Object> localApp = appTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); Local<Object> localApp = appTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
localApp->SetAlignedPointerInInternalField(0, app); localApp->SetAlignedPointerInInternalField(0, app);
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
/* Add this to our delete list */ /* Add this to our delete list */
if constexpr (std::is_same<APP, uWS::SSLApp>::value) { if constexpr (std::is_same<APP, uWS::SSLApp>::value) {
sslApps.emplace_back(app); perContextData->sslApps.emplace_back(app);
} else { } else {
apps.emplace_back(app); perContextData->apps.emplace_back(app);
} }
args.GetReturnValue().Set(localApp); args.GetReturnValue().Set(localApp);

View File

@ -1,13 +1,15 @@
#include "App.h" #include "App.h"
#include <v8.h>
#include "Utilities.h" #include "Utilities.h"
#include <v8.h>
using namespace v8; using namespace v8;
/* This one is the same for SSL and non-SSL */ /* This one is the same for SSL and non-SSL */
struct HttpRequestWrapper { struct HttpRequestWrapper {
static Persistent<Object> reqTemplate;
/* Unwraps the HttpRequest from V8 object */
static inline uWS::HttpRequest *getHttpRequest(const FunctionCallbackInfo<Value> &args) { static inline uWS::HttpRequest *getHttpRequest(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
/* Thow on deleted request */ /* Thow on deleted request */
auto *req = (uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0); auto *req = (uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0);
if (!req) { if (!req) {
@ -18,6 +20,7 @@ struct HttpRequestWrapper {
/* Takes function of string, string. Returns this (doesn't really but should) */ /* Takes function of string, string. Returns this (doesn't really but should) */
static void req_forEach(const FunctionCallbackInfo<Value> &args) { static void req_forEach(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *req = getHttpRequest(args); auto *req = getHttpRequest(args);
if (req) { if (req) {
Local<Function> cb = Local<Function>::Cast(args[0]); Local<Function> cb = Local<Function>::Cast(args[0]);
@ -32,6 +35,7 @@ struct HttpRequestWrapper {
/* Takes int, returns string (must be in bounds) */ /* Takes int, returns string (must be in bounds) */
static void req_getParameter(const FunctionCallbackInfo<Value> &args) { static void req_getParameter(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *req = getHttpRequest(args); auto *req = getHttpRequest(args);
if (req) { if (req) {
int index = args[0]->Uint32Value(isolate->GetCurrentContext()).ToChecked(); int index = args[0]->Uint32Value(isolate->GetCurrentContext()).ToChecked();
@ -43,6 +47,7 @@ struct HttpRequestWrapper {
/* Takes nothing, returns string */ /* Takes nothing, returns string */
static void req_getUrl(const FunctionCallbackInfo<Value> &args) { static void req_getUrl(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *req = getHttpRequest(args); auto *req = getHttpRequest(args);
if (req) { if (req) {
std::string_view url = req->getUrl(); std::string_view url = req->getUrl();
@ -53,6 +58,7 @@ struct HttpRequestWrapper {
/* Takes String, returns String */ /* Takes String, returns String */
static void req_getHeader(const FunctionCallbackInfo<Value> &args) { static void req_getHeader(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *req = getHttpRequest(args); auto *req = getHttpRequest(args);
if (req) { if (req) {
NativeString data(args.GetIsolate(), args[0]); NativeString data(args.GetIsolate(), args[0]);
@ -68,6 +74,7 @@ struct HttpRequestWrapper {
/* Takes nothing, returns string */ /* Takes nothing, returns string */
static void req_getMethod(const FunctionCallbackInfo<Value> &args) { static void req_getMethod(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *req = getHttpRequest(args); auto *req = getHttpRequest(args);
if (req) { if (req) {
std::string_view method = req->getMethod(); std::string_view method = req->getMethod();
@ -77,6 +84,7 @@ struct HttpRequestWrapper {
} }
static void req_getQuery(const FunctionCallbackInfo<Value> &args) { static void req_getQuery(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *req = getHttpRequest(args); auto *req = getHttpRequest(args);
if (req) { if (req) {
std::string_view query = req->getQuery(); std::string_view query = req->getQuery();
@ -85,7 +93,8 @@ struct HttpRequestWrapper {
} }
} }
static void initReqTemplate() { /* Returns a clonable object wrapping an HttpRequest */
static Local<Object> init(Isolate *isolate) {
/* We do clone every request object, we could share them, they are illegal to use outside the function anyways */ /* We do clone every request object, we could share them, they are illegal to use outside the function anyways */
Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate); Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate);
reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest", NewStringType::kNormal).ToLocalChecked()); reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest", NewStringType::kNormal).ToLocalChecked());
@ -101,12 +110,7 @@ struct HttpRequestWrapper {
/* Create the template */ /* Create the template */
Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
reqTemplate.Reset(isolate, reqObjectLocal);
}
static Local<Object> getReqInstance() { return reqObjectLocal;
return Local<Object>::New(isolate, reqTemplate)->Clone();
} }
}; };
Persistent<Object> HttpRequestWrapper::reqTemplate;

View File

@ -1,13 +1,14 @@
#include "App.h" #include "App.h"
#include <v8.h>
#include "Utilities.h" #include "Utilities.h"
#include <v8.h>
using namespace v8; using namespace v8;
struct HttpResponseWrapper { struct HttpResponseWrapper {
static Persistent<Object> resTemplate[2];
template <bool SSL> template <bool SSL>
static inline uWS::HttpResponse<SSL> *getHttpResponse(const FunctionCallbackInfo<Value> &args) { static inline uWS::HttpResponse<SSL> *getHttpResponse(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = (uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0); auto *res = (uWS::HttpResponse<SSL> *) args.Holder()->GetAlignedPointerFromInternalField(0);
if (!res) { if (!res) {
args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "Invalid access of discarded (invalid, deleted) uWS.HttpResponse/SSLHttpResponse.", NewStringType::kNormal).ToLocalChecked())); args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "Invalid access of discarded (invalid, deleted) uWS.HttpResponse/SSLHttpResponse.", NewStringType::kNormal).ToLocalChecked()));
@ -34,12 +35,13 @@ struct HttpResponseWrapper {
/* Takes function of data and isLast. Expects nothing from callback, returns this */ /* Takes function of data and isLast. Expects nothing from callback, returns this */
template <bool SSL> template <bool SSL>
static void res_onData(const FunctionCallbackInfo<Value> &args) { static void res_onData(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
/* This thing perfectly fits in with unique_function, and will Reset on destructor */ /* This thing perfectly fits in with unique_function, and will Reset on destructor */
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0])); UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
res->onData([p = std::move(p)](std::string_view data, bool last) { res->onData([p = std::move(p), isolate](std::string_view data, bool last) {
HandleScope hs(isolate); HandleScope hs(isolate);
Local<ArrayBuffer> dataArrayBuffer = ArrayBuffer::New(isolate, (void *) data.data(), data.length()); Local<ArrayBuffer> dataArrayBuffer = ArrayBuffer::New(isolate, (void *) data.data(), data.length());
@ -57,6 +59,7 @@ struct HttpResponseWrapper {
/* Takes nothing, returns nothing. Cb wants nothing returned. */ /* Takes nothing, returns nothing. Cb wants nothing returned. */
template <bool SSL> template <bool SSL>
static void res_onAborted(const FunctionCallbackInfo<Value> &args) { static void res_onAborted(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
/* This thing perfectly fits in with unique_function, and will Reset on destructor */ /* This thing perfectly fits in with unique_function, and will Reset on destructor */
@ -65,7 +68,7 @@ struct HttpResponseWrapper {
/* This is how we capture res (C++ this in invocation of this function) */ /* This is how we capture res (C++ this in invocation of this function) */
UniquePersistent<Object> resObject(isolate, args.Holder()); UniquePersistent<Object> resObject(isolate, args.Holder());
res->onAborted([p = std::move(p), resObject = std::move(resObject)]() { res->onAborted([p = std::move(p), resObject = std::move(resObject), isolate]() {
HandleScope hs(isolate); HandleScope hs(isolate);
/* Mark this resObject invalid */ /* Mark this resObject invalid */
@ -81,6 +84,7 @@ struct HttpResponseWrapper {
/* Takes nothing, returns arraybuffer */ /* Takes nothing, returns arraybuffer */
template <bool SSL> template <bool SSL>
static void res_getRemoteAddress(const FunctionCallbackInfo<Value> &args) { static void res_getRemoteAddress(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
std::string_view ip = res->getRemoteAddress(); std::string_view ip = res->getRemoteAddress();
@ -93,6 +97,7 @@ struct HttpResponseWrapper {
/* Returns the current write offset */ /* Returns the current write offset */
template <bool SSL> template <bool SSL>
static void res_getWriteOffset(const FunctionCallbackInfo<Value> &args) { static void res_getWriteOffset(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
args.GetReturnValue().Set(Integer::New(isolate, getHttpResponse<SSL>(args)->getWriteOffset())); args.GetReturnValue().Set(Integer::New(isolate, getHttpResponse<SSL>(args)->getWriteOffset()));
@ -102,12 +107,13 @@ struct HttpResponseWrapper {
/* Takes function of bool(int), returns this */ /* Takes function of bool(int), returns this */
template <bool SSL> template <bool SSL>
static void res_onWritable(const FunctionCallbackInfo<Value> &args) { static void res_onWritable(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
/* This thing perfectly fits in with unique_function, and will Reset on destructor */ /* This thing perfectly fits in with unique_function, and will Reset on destructor */
UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0])); UniquePersistent<Function> p(isolate, Local<Function>::Cast(args[0]));
res->onWritable([p = std::move(p)](int offset) -> bool { res->onWritable([p = std::move(p), isolate](int offset) -> bool {
HandleScope hs(isolate); HandleScope hs(isolate);
Local<Value> argv[] = {Integer::NewFromUnsigned(isolate, offset)}; Local<Value> argv[] = {Integer::NewFromUnsigned(isolate, offset)};
@ -161,6 +167,7 @@ struct HttpResponseWrapper {
/* Takes data and optionally totalLength, returns true for success, false for backpressure */ /* Takes data and optionally totalLength, returns true for success, false for backpressure */
template <bool SSL> template <bool SSL>
static void res_tryEnd(const FunctionCallbackInfo<Value> &args) { static void res_tryEnd(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
NativeString data(args.GetIsolate(), args[0]); NativeString data(args.GetIsolate(), args[0]);
@ -192,6 +199,7 @@ struct HttpResponseWrapper {
/* Takes data, returns true for success, false for backpressure */ /* Takes data, returns true for success, false for backpressure */
template <bool SSL> template <bool SSL>
static void res_write(const FunctionCallbackInfo<Value> &args) { static void res_write(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
NativeString data(args.GetIsolate(), args[0]); NativeString data(args.GetIsolate(), args[0]);
@ -207,6 +215,7 @@ struct HttpResponseWrapper {
/* Takes key, value. Returns this */ /* Takes key, value. Returns this */
template <bool SSL> template <bool SSL>
static void res_writeHeader(const FunctionCallbackInfo<Value> &args) { static void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
NativeString header(args.GetIsolate(), args[0]); NativeString header(args.GetIsolate(), args[0]);
@ -226,10 +235,11 @@ struct HttpResponseWrapper {
/* Takes function, returns this (EXPERIMENTAL) */ /* Takes function, returns this (EXPERIMENTAL) */
template <bool SSL> template <bool SSL>
static void res_cork(const FunctionCallbackInfo<Value> &args) { static void res_cork(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args); auto *res = getHttpResponse<SSL>(args);
if (res) { if (res) {
res->cork([cb = Local<Function>::Cast(args[0])]() { res->cork([cb = Local<Function>::Cast(args[0]), isolate]() {
cb->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 0, nullptr).IsEmpty(); cb->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 0, nullptr).IsEmpty();
}); });
@ -238,7 +248,7 @@ struct HttpResponseWrapper {
} }
template <bool SSL> template <bool SSL>
static void initResTemplate() { static Local<Object> init(Isolate *isolate) {
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate); Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
if (SSL) { if (SSL) {
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLHttpResponse", NewStringType::kNormal).ToLocalChecked()); resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLHttpResponse", NewStringType::kNormal).ToLocalChecked());
@ -263,13 +273,7 @@ struct HttpResponseWrapper {
/* Create our template */ /* Create our template */
Local<Object> resObjectLocal = resTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); Local<Object> resObjectLocal = resTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
resTemplate[SSL].Reset(isolate, resObjectLocal);
}
template <class APP> return resObjectLocal;
static Local<Object> getResInstance() {
return Local<Object>::New(isolate, resTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
} }
}; };
Persistent<Object> HttpResponseWrapper::resTemplate[2];

View File

@ -4,6 +4,23 @@
#include <v8.h> #include <v8.h>
using namespace v8; using namespace v8;
struct PerContextData {
Isolate *isolate;
UniquePersistent<Object> reqTemplate;
UniquePersistent<Object> resTemplate[2];
UniquePersistent<Object> wsTemplate[2];
/* We hold all apps until free */
std::vector<std::unique_ptr<uWS::App>> apps;
std::vector<std::unique_ptr<uWS::SSLApp>> sslApps;
};
template <class APP>
static constexpr int getAppTypeIndex() {
/* Returns 1 for SSLApp and 0 for App */
return std::is_same<APP, uWS::SSLApp>::value;
}
class NativeString { class NativeString {
char *data; char *data;
size_t length; size_t length;
@ -36,7 +53,7 @@ public:
bool isInvalid(const FunctionCallbackInfo<Value> &args) { bool isInvalid(const FunctionCallbackInfo<Value> &args) {
if (invalid) { if (invalid) {
args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "Text and data can only be passed by String, ArrayBuffer or TypedArray.", NewStringType::kNormal).ToLocalChecked())); args.GetReturnValue().Set(args.GetIsolate()->ThrowException(String::NewFromUtf8(args.GetIsolate(), "Text and data can only be passed by String, ArrayBuffer or TypedArray.", NewStringType::kNormal).ToLocalChecked()));
} }
return invalid; return invalid;
} }

View File

@ -1,15 +1,16 @@
#include "App.h" #include "App.h"
#include <v8.h>
#include "Utilities.h" #include "Utilities.h"
#include <v8.h>
using namespace v8; using namespace v8;
/* todo: probably isCorked, cork should be exposed? */ /* todo: probably isCorked, cork should be exposed? */
struct WebSocketWrapper { struct WebSocketWrapper {
static Persistent<Object> wsTemplate[2];
template <bool SSL> template <bool SSL>
static inline uWS::WebSocket<SSL, true> *getWebSocket(const FunctionCallbackInfo<Value> &args) { static inline uWS::WebSocket<SSL, true> *getWebSocket(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = (uWS::WebSocket<SSL, true> *) args.Holder()->GetAlignedPointerFromInternalField(0); auto *ws = (uWS::WebSocket<SSL, true> *) args.Holder()->GetAlignedPointerFromInternalField(0);
if (!ws) { if (!ws) {
args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "Invalid access of closed uWS.WebSocket/SSLWebSocket.", NewStringType::kNormal).ToLocalChecked())); args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "Invalid access of closed uWS.WebSocket/SSLWebSocket.", NewStringType::kNormal).ToLocalChecked()));
@ -24,6 +25,7 @@ struct WebSocketWrapper {
/* Takes string topic */ /* Takes string topic */
template <bool SSL> template <bool SSL>
static void uWS_WebSocket_subscribe(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_subscribe(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args); auto *ws = getWebSocket<SSL>(args);
if (ws) { if (ws) {
NativeString topic(isolate, args[0]); NativeString topic(isolate, args[0]);
@ -37,6 +39,7 @@ struct WebSocketWrapper {
/* Takes string topic, returns boolean success */ /* Takes string topic, returns boolean success */
template <bool SSL> template <bool SSL>
static void uWS_WebSocket_unsubscribe(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_unsubscribe(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args); auto *ws = getWebSocket<SSL>(args);
if (ws) { if (ws) {
NativeString topic(isolate, args[0]); NativeString topic(isolate, args[0]);
@ -51,6 +54,7 @@ struct WebSocketWrapper {
/* Takes string topic, message */ /* Takes string topic, message */
template <bool SSL> template <bool SSL>
static void uWS_WebSocket_publish(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_publish(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args); auto *ws = getWebSocket<SSL>(args);
if (ws) { if (ws) {
NativeString topic(isolate, args[0]); NativeString topic(isolate, args[0]);
@ -75,6 +79,7 @@ struct WebSocketWrapper {
/* Takes code, message, returns undefined */ /* Takes code, message, returns undefined */
template <bool SSL> template <bool SSL>
static void uWS_WebSocket_end(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_end(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args); auto *ws = getWebSocket<SSL>(args);
if (ws) { if (ws) {
int code = 0; int code = 0;
@ -95,6 +100,7 @@ struct WebSocketWrapper {
/* Takes nothing returns arraybuffer */ /* Takes nothing returns arraybuffer */
template <bool SSL> template <bool SSL>
static void uWS_WebSocket_getRemoteAddress(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_getRemoteAddress(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args); auto *ws = getWebSocket<SSL>(args);
if (ws) { if (ws) {
std::string_view ip = ws->getRemoteAddress(); std::string_view ip = ws->getRemoteAddress();
@ -107,6 +113,7 @@ struct WebSocketWrapper {
/* Takes nothing, returns integer */ /* Takes nothing, returns integer */
template <bool SSL> template <bool SSL>
static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args); auto *ws = getWebSocket<SSL>(args);
if (ws) { if (ws) {
int bufferedAmount = ws->getBufferedAmount(); int bufferedAmount = ws->getBufferedAmount();
@ -117,6 +124,7 @@ struct WebSocketWrapper {
/* Takes message, isBinary. Returns true on success, false otherwise */ /* Takes message, isBinary. Returns true on success, false otherwise */
template <bool SSL> template <bool SSL>
static void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args); auto *ws = getWebSocket<SSL>(args);
if (ws) { if (ws) {
NativeString message(args.GetIsolate(), args[0]); NativeString message(args.GetIsolate(), args[0]);
@ -131,7 +139,7 @@ struct WebSocketWrapper {
} }
template <bool SSL> template <bool SSL>
static void initWsTemplate() { static Local<Object> init(Isolate *isolate) {
Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate); Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate);
if (SSL) { if (SSL) {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket", NewStringType::kNormal).ToLocalChecked()); wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket", NewStringType::kNormal).ToLocalChecked());
@ -152,15 +160,7 @@ struct WebSocketWrapper {
/* Create the template */ /* Create the template */
Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
wsTemplate[SSL].Reset(isolate, wsObjectLocal);
}
/* This is where we output an instance */ return wsObjectLocal;
template <class APP>
static Local<Object> getWsInstance() {
return Local<Object>::New(isolate, wsTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
} }
}; };
/* Fix this, should be nicer */
Persistent<Object> WebSocketWrapper::wsTemplate[2];

View File

@ -17,20 +17,14 @@
/* We are only allowed to depend on µWS and V8 in this layer. */ /* We are only allowed to depend on µWS and V8 in this layer. */
#include "App.h" #include "App.h"
#include <v8.h>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <type_traits> #include <type_traits>
#include <v8.h>
using namespace v8; using namespace v8;
/* These two are definitely static */
Isolate *isolate;
bool valid = true;
/* We hold all apps until free */
std::vector<std::unique_ptr<uWS::App>> apps;
std::vector<std::unique_ptr<uWS::SSLApp>> sslApps;
/* Compatibility for V8 7.0 and earlier */ /* Compatibility for V8 7.0 and earlier */
#include <v8-version.h> #include <v8-version.h>
bool BooleanValue(Isolate *isolate, Local<Value> value) { bool BooleanValue(Isolate *isolate, Local<Value> value) {
@ -53,13 +47,27 @@ bool BooleanValue(Isolate *isolate, Local<Value> value) {
/* This has to be called in beforeExit, but exit also seems okay */ /* This has to be called in beforeExit, but exit also seems okay */
void uWS_free(const FunctionCallbackInfo<Value> &args) { void uWS_free(const FunctionCallbackInfo<Value> &args) {
if (valid) {
/* We get the External holding perContextData */
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
/* Todo: this will always be true */
if (perContextData) {
/* Freeing apps here, it could be done earlier but not sooner */ /* Freeing apps here, it could be done earlier but not sooner */
apps.clear(); perContextData->apps.clear();
sslApps.clear(); perContextData->sslApps.clear();
/* Freeing the loop here means we give time for our timers to close, etc */ /* Freeing the loop here means we give time for our timers to close, etc */
uWS::Loop::get()->free(); uWS::Loop::get()->free();
valid = false;
// we need to mark this
delete perContextData;
// we can override the exports->free function to null after!
//args.Data()
//Local<External>::Cast(args.Data())->
} }
} }
@ -69,23 +77,31 @@ void uWS_us_listen_socket_close(const FunctionCallbackInfo<Value> &args) {
us_listen_socket_close(0, (struct us_listen_socket_t *) External::Cast(*args[0])->Value()); us_listen_socket_close(0, (struct us_listen_socket_t *) External::Cast(*args[0])->Value());
} }
#include <uv.h>
void Main(Local<Object> exports) { void Main(Local<Object> exports) {
/* I guess we store this statically */
isolate = exports->GetIsolate(); /* We pass isolate everywhere */
Isolate *isolate = exports->GetIsolate();
/* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */ /* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */
// todo: setting this might be crashing nodejs? // todo: setting this might be crashing nodejs?
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kAuto); isolate->SetMicrotasksPolicy(MicrotasksPolicy::kAuto);
/* Integrate with existing libuv loop, we just pass a boolean basically */ /* Init the template objects, SSL and non-SSL, store it in per context data */
uWS::Loop::get(uv_default_loop()); PerContextData *perContextData = new PerContextData;
perContextData->isolate = isolate;
perContextData->reqTemplate.Reset(isolate, HttpRequestWrapper::init(isolate));
perContextData->resTemplate[0].Reset(isolate, HttpResponseWrapper::init<0>(isolate));
perContextData->resTemplate[1].Reset(isolate, HttpResponseWrapper::init<1>(isolate));
perContextData->wsTemplate[0].Reset(isolate, WebSocketWrapper::init<0>(isolate));
perContextData->wsTemplate[1].Reset(isolate, WebSocketWrapper::init<1>(isolate));
/* Refer to per context data via External */
Local<External> externalPerContextData = External::New(isolate, perContextData);
/* uWS namespace */ /* uWS namespace */
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "App", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::App>)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "App", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::App>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SSLApp", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SSLApp", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "free", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_free)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "free", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_free, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
/* Expose some µSockets functions directly under uWS namespace */ /* Expose some µSockets functions directly under uWS namespace */
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "us_listen_socket_close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "us_listen_socket_close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
@ -97,21 +113,16 @@ void Main(Local<Object> exports) {
/* Listen options */ /* Listen options */
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "LIBUS_LISTEN_EXCLUSIVE_PORT", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, LIBUS_LISTEN_EXCLUSIVE_PORT)).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "LIBUS_LISTEN_EXCLUSIVE_PORT", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, LIBUS_LISTEN_EXCLUSIVE_PORT)).ToChecked();
/* The template for websockets */
WebSocketWrapper::initWsTemplate<0>();
WebSocketWrapper::initWsTemplate<1>();
/* Initialize SSL and non-SSL templates */
HttpResponseWrapper::initResTemplate<0>();
HttpResponseWrapper::initResTemplate<1>();
/* Init a shared request object */
HttpRequestWrapper::initReqTemplate();
} }
/* This is required when building as a Node.js addon */ /* This is required when building as a Node.js addon */
#ifndef ADDON_IS_HOST #ifndef ADDON_IS_HOST
#include <node.h> #include <node.h>
NODE_MODULE(uWS, Main) extern "C" NODE_MODULE_EXPORT void
NODE_MODULE_INITIALIZER(Local<Object> exports, Local<Value> module, Local<Context> context) {
/* Integrate uSockets with existing libuv loop */
uWS::Loop::get(node::GetCurrentEventLoop(context->GetIsolate()));
/* Register vanilla V8 addon */
Main(exports);
}
#endif #endif

@ -1 +1 @@
Subproject commit f358601505374371f24f8e5b50f630a5646bc682 Subproject commit 02ad3979a61cc4df9c15f2c776a7bbe2ba6dd09e