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,78 +52,72 @@ 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) { HandleScope hs(isolate);
mappedCompression = uWS::CompressOptions::SHARED_COMPRESSOR;
} else if (compression == 2) {
mappedCompression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
}
app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), { /* Create a new websocket object */
/* idleTimeout */ Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>();
.compression = mappedCompression, wsObject->SetAlignedPointerInInternalField(0, ws);
.maxPayloadLength = maxPayloadLength,
/* Handlers */
.open = [openPf](auto *ws, auto *req) {
HandleScope hs(isolate);
/* Create a new websocket object */ /* Attach a new V8 object with pointer to us, to us */
Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>(); PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
wsObject->SetAlignedPointerInInternalField(0, ws); perSocketData->socketPf = new Persistent<Object>;
perSocketData->socketPf->Reset(isolate, wsObject);
/* Attach a new V8 object with pointer to us, to us */ Local<Value> argv[] = {wsObject};
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
perSocketData->socketPf = new Persistent<Object>; };
perSocketData->socketPf->Reset(isolate, wsObject);
Local<Value> argv[] = {wsObject}; behavior.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv); HandleScope hs(isolate);
},
.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
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());
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)), Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
/*ArrayBuffer::New(isolate, (void *) message.data(), message.length())*/ messageArrayBuffer, /*ArrayBuffer::New(isolate, (void *) message.data(), message.length())*/ messageArrayBuffer,
Boolean::New(isolate, opCode == uWS::OpCode::BINARY) Boolean::New(isolate, opCode == uWS::OpCode::BINARY)
}; };
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv); Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
/* 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) {
HandleScope hs(isolate);
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); behavior.drain = [drainPf](auto *ws) {
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf)) HandleScope hs(isolate);
};
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
},
.ping = [](auto *ws) {
}, PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
.pong = [](auto *ws) { Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
};
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
};
}, behavior.ping = [](auto *ws) {
.close = [closePf](auto *ws, int code, std::string_view message) {
HandleScope hs(isolate);
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length()); };
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); behavior.pong = [](auto *ws) {
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
Integer::New(isolate, code),
messageArrayBuffer
};
Local<Function>::New(isolate, *closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
/* Again, here we clear the buffer to avoid strange bugs */ };
messageArrayBuffer->Neuter();
} behavior.close = [closePf](auto *ws, int code, std::string_view message) {
}); HandleScope hs(isolate);
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
Integer::New(isolate, code),
messageArrayBuffer
};
Local<Function>::New(isolate, *closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
/* Again, here we clear the buffer to avoid strange bugs */
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());