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
CPP_OSX := -stdlib=libc++ $(C_OSX)
default:
Unices:
make targets
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)`
for f in dist/*.node; do chmod +x $$f; done
targets:
# Node.js 10
mkdir targets
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets
curl https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz | tar xz -C targets
curl -OJ https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz
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:
clang $(C_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
Darwin:
gcc $(C_OSX) $(C_SHARED) -I $$NODE/include/node
g++ $(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
.PHONY: clean
clean:
rm -f dist/README.md
rm -f dist/LICENSE
rm -f dist/uws_*.node
rm -f dist/uws.js
rm -rf dist/src
rm -rf targets
clang $(C_OSX) $(C_SHARED) -I $$NODE/include/node
clang++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node
clang++ $(CPP_OSX) -flto -O3 *.o -std=c++17 -shared -undefined dynamic_lookup -o dist/uws_darwin_$$ABI.node
Windows:
nmake targets
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
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

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>
void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
APP::WebSocketBehavior behavior = {};
// pattern
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> *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 {
Persistent<Object> *socketPf;
};
@ -35,10 +30,17 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
Local<Object> behaviorObject = Local<Object>::Cast(args[1]);
/* maxPayloadLength */
maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value();
behavior.maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value();
/* Compression */
compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
/* Compression, map from 0, 1, 2 to disabled, shared, dedicated */
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 */
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"))));
}
/* Map compression options from integer values */
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) {
behavior.open = [openPf](auto *ws, auto *req) {
HandleScope hs(isolate);
/* Create a new websocket object */
@ -76,8 +66,9 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
Local<Value> argv[] = {wsObject};
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);
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 */
messageArrayBuffer->Neuter();
},
.drain = [drainPf](auto *ws) {
};
behavior.drain = [drainPf](auto *ws) {
HandleScope hs(isolate);
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
};
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
},
.ping = [](auto *ws) {
};
},
.pong = [](auto *ws) {
behavior.ping = [](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);
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 */
messageArrayBuffer->Neuter();
}
});
};
app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), std::move(behavior));
/* Return this */
args.GetReturnValue().Set(args.Holder());