Port to Windows

This commit is contained in:
Alex Hultman 2019-01-16 21:31:17 +01:00
parent 68a4a6b53f
commit 3feee30666
3 changed files with 89 additions and 115 deletions

View File

@ -1,33 +1,38 @@
C_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c # Note: for Windows, run "nmake Windows" in x64 developer terminal
CPP_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp C_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c
CPP_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp
C_OSX = -mmacosx-version-min=10.7
CPP_OSX = -stdlib=libc++ $(C_OSX)
C_OSX := -mmacosx-version-min=10.7 Unices:
CPP_OSX := -stdlib=libc++ $(C_OSX)
default:
make targets make targets
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)` NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)` NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)`
for f in dist/*.node; do chmod +x $$f; done for f in dist/*.node; do chmod +x $$f; done
targets: targets:
# Node.js 10
mkdir targets mkdir targets
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets curl -OJ https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz
curl https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz | tar xz -C targets tar xzf node-v10.0.0-headers.tar.gz -C targets
curl https://nodejs.org/dist/v10.0.0/win-x64/node.lib > targets/node-v10.0.0/node.lib
# Node.js 11
curl -OJ https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz
tar xzf node-v11.1.0-headers.tar.gz -C targets
curl https://nodejs.org/dist/v11.1.0/win-x64/node.lib > targets/node-v11.1.0/node.lib
Linux: Linux:
clang $(C_SHARED) -I $$NODE/include/node clang $(C_SHARED) -I $$NODE/include/node
clang++ $(CPP_SHARED) -I $$NODE/include/node clang++ $(CPP_SHARED) -I $$NODE/include/node
clang++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node clang++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node
Darwin: Darwin:
gcc $(C_OSX) $(C_SHARED) -I $$NODE/include/node clang $(C_OSX) $(C_SHARED) -I $$NODE/include/node
g++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node clang++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node
g++ $(CPP_OSX) -flto -O3 *.o -std=c++17 -shared -undefined dynamic_lookup -o dist/uws_darwin_$$ABI.node clang++ $(CPP_OSX) -flto -O3 *.o -std=c++17 -shared -undefined dynamic_lookup -o dist/uws_darwin_$$ABI.node
.PHONY: clean
clean: Windows:
rm -f dist/README.md nmake targets
rm -f dist/LICENSE cl /D "LIBUS_USE_LIBUV" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c /I targets/node-v10.0.0/include/node /I uWebSockets/src /EHsc /Ox /LD /Fedist/uws_win32_64.node src/addon.cpp targets/node-v10.0.0/node.lib
rm -f dist/uws_*.node cl /D "LIBUS_USE_LIBUV" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c /I targets/node-v11.1.0/include/node /I uWebSockets/src /EHsc /Ox /LD /Fedist/uws_win32_67.node src/addon.cpp targets/node-v11.1.0/node.lib
rm -f dist/uws.js
rm -rf dist/src
rm -rf targets

View File

@ -1,27 +0,0 @@
call "%VS140COMNTOOLS%..\..\vc\vcvarsall.bat" amd64
if not exist targets (
mkdir targets
curl https://nodejs.org/dist/v8.1.2/node-v8.1.2-headers.tar.gz | tar xz -C targets
curl https://nodejs.org/dist/v8.1.2/win-x64/node.lib > targets/node-v8.1.2/node.lib
curl https://nodejs.org/dist/v9.2.0/node-v9.2.0-headers.tar.gz | tar xz -C targets
curl https://nodejs.org/dist/v9.2.0/win-x64/node.lib > targets/node-v9.2.0/node.lib
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets
curl https://nodejs.org/dist/v10.0.0/win-x64/node.lib > targets/node-v10.0.0/node.lib
)
cp README.md dist/README.md
cp ../uWebSockets/LICENSE dist/LICENSE
cp -r ../uWebSockets/src dist/
cp src/addon.cpp dist/src/addon.cpp
cp src/addon.h dist/src/addon.h
cp src/http.h dist/src/http.h
cp src/uws.js dist/uws.js
cl /I targets/node-v8.1.2/include/node /EHsc /Ox /LD /Fedist/uws_win32_57.node dist/src/*.cpp targets/node-v8.1.2/node.lib
cl /I targets/node-v9.2.0/include/node /EHsc /Ox /LD /Fedist/uws_win32_59.node dist/src/*.cpp targets/node-v9.2.0/node.lib
cl /I targets/node-v10.0.0/include/node /EHsc /Ox /LD /Fedist/uws_win32_64.node dist/src/*.cpp targets/node-v10.0.0/node.lib
rm *.obj
rm dist/*.exp
rm dist/*.lib

View File

@ -10,6 +10,7 @@ using namespace v8;
template <typename APP> template <typename APP>
void uWS_App_ws(const FunctionCallbackInfo<Value> &args) { void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
APP::WebSocketBehavior behavior = {};
// pattern // pattern
NativeString nativeString(args.GetIsolate(), args[0]); NativeString nativeString(args.GetIsolate(), args[0]);
@ -20,12 +21,6 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
Persistent<Function> *drainPf = new Persistent<Function>(); Persistent<Function> *drainPf = new Persistent<Function>();
Persistent<Function> *closePf = new Persistent<Function>(); Persistent<Function> *closePf = new Persistent<Function>();
int maxPayloadLength = 0;
/* For now, let's have 0, 1, 2 be from nothing to shared, to dedicated */
int compression = 0;
uWS::CompressOptions mappedCompression = uWS::CompressOptions::DISABLED;
struct PerSocketData { struct PerSocketData {
Persistent<Object> *socketPf; Persistent<Object> *socketPf;
}; };
@ -35,10 +30,17 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
Local<Object> behaviorObject = Local<Object>::Cast(args[1]); Local<Object> behaviorObject = Local<Object>::Cast(args[1]);
/* maxPayloadLength */ /* maxPayloadLength */
maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value(); behavior.maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value();
/* Compression */ /* Compression, map from 0, 1, 2 to disabled, shared, dedicated */
compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value(); int compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
if (compression == 0) {
behavior.compression = uWS::CompressOptions::DISABLED;
} else if (compression == 1) {
behavior.compression = uWS::CompressOptions::SHARED_COMPRESSOR;
} else if (compression == 2) {
behavior.compression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
}
/* Open */ /* Open */
openPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open")))); openPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
@ -50,19 +52,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
closePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close")))); closePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
} }
/* Map compression options from integer values */ behavior.open = [openPf](auto *ws, auto *req) {
if (compression == 1) {
mappedCompression = uWS::CompressOptions::SHARED_COMPRESSOR;
} else if (compression == 2) {
mappedCompression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
}
app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), {
/* idleTimeout */
.compression = mappedCompression,
.maxPayloadLength = maxPayloadLength,
/* Handlers */
.open = [openPf](auto *ws, auto *req) {
HandleScope hs(isolate); HandleScope hs(isolate);
/* Create a new websocket object */ /* Create a new websocket object */
@ -76,8 +66,9 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
Local<Value> argv[] = {wsObject}; Local<Value> argv[] = {wsObject};
Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv); Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
}, };
.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
behavior.message = [messagePf](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());
@ -91,22 +82,26 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */ /* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
messageArrayBuffer->Neuter(); messageArrayBuffer->Neuter();
}, };
.drain = [drainPf](auto *ws) {
behavior.drain = [drainPf](auto *ws) {
HandleScope hs(isolate); HandleScope hs(isolate);
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf)) Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
}; };
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv); Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
}, };
.ping = [](auto *ws) {
}, behavior.ping = [](auto *ws) {
.pong = [](auto *ws) {
}, };
.close = [closePf](auto *ws, int code, std::string_view message) {
behavior.pong = [](auto *ws) {
};
behavior.close = [closePf](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());
@ -120,8 +115,9 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
/* Again, here we clear the buffer to avoid strange bugs */ /* Again, here we clear the buffer to avoid strange bugs */
messageArrayBuffer->Neuter(); messageArrayBuffer->Neuter();
} };
});
app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), std::move(behavior));
/* Return this */ /* Return this */
args.GetReturnValue().Set(args.Holder()); args.GetReturnValue().Set(args.Holder());