Skip to content

Commit 3feee30

Browse files
committed
Port to Windows
1 parent 68a4a6b commit 3feee30

3 files changed

Lines changed: 102 additions & 128 deletions

File tree

‎Makefile‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
C_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c
1+
# Note: for Windows, run "nmake Windows" in x64 developer terminal
22

3-
CPP_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp
3+
C_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c
4+
CPP_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp
5+
C_OSX = -mmacosx-version-min=10.7
6+
CPP_OSX = -stdlib=libc++ $(C_OSX)
47

5-
C_OSX := -mmacosx-version-min=10.7
6-
7-
CPP_OSX := -stdlib=libc++ $(C_OSX)
8-
9-
default:
8+
Unices:
109
make targets
1110
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
1211
NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)`
1312
for f in dist/*.node; do chmod +x $$f; done
13+
1414
targets:
15+
# Node.js 10
1516
mkdir targets
16-
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets
17-
curl https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz | tar xz -C targets
17+
curl -OJ https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz
18+
tar xzf node-v10.0.0-headers.tar.gz -C targets
19+
curl https://nodejs.org/dist/v10.0.0/win-x64/node.lib > targets/node-v10.0.0/node.lib
20+
# Node.js 11
21+
curl -OJ https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz
22+
tar xzf node-v11.1.0-headers.tar.gz -C targets
23+
curl https://nodejs.org/dist/v11.1.0/win-x64/node.lib > targets/node-v11.1.0/node.lib
24+
1825
Linux:
1926
clang $(C_SHARED) -I $$NODE/include/node
2027
clang++ $(CPP_SHARED) -I $$NODE/include/node
2128
clang++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node
29+
2230
Darwin:
23-
gcc $(C_OSX) $(C_SHARED) -I $$NODE/include/node
24-
g++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node
25-
g++ $(CPP_OSX) -flto -O3 *.o -std=c++17 -shared -undefined dynamic_lookup -o dist/uws_darwin_$$ABI.node
26-
.PHONY: clean
27-
clean:
28-
rm -f dist/README.md
29-
rm -f dist/LICENSE
30-
rm -f dist/uws_*.node
31-
rm -f dist/uws.js
32-
rm -rf dist/src
33-
rm -rf targets
31+
clang $(C_OSX) $(C_SHARED) -I $$NODE/include/node
32+
clang++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node
33+
clang++ $(CPP_OSX) -flto -O3 *.o -std=c++17 -shared -undefined dynamic_lookup -o dist/uws_darwin_$$ABI.node
34+
35+
Windows:
36+
nmake targets
37+
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
38+
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

‎make.bat‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/AppWrapper.h‎

Lines changed: 77 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using namespace v8;
1010
template <typename APP>
1111
void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
1212
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
13+
APP::WebSocketBehavior behavior = {};
1314

1415
// pattern
1516
NativeString nativeString(args.GetIsolate(), args[0]);
@@ -20,12 +21,6 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
2021
Persistent<Function> *drainPf = new Persistent<Function>();
2122
Persistent<Function> *closePf = new Persistent<Function>();
2223

23-
int maxPayloadLength = 0;
24-
25-
/* For now, let's have 0, 1, 2 be from nothing to shared, to dedicated */
26-
int compression = 0;
27-
uWS::CompressOptions mappedCompression = uWS::CompressOptions::DISABLED;
28-
2924
struct PerSocketData {
3025
Persistent<Object> *socketPf;
3126
};
@@ -35,10 +30,17 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
3530
Local<Object> behaviorObject = Local<Object>::Cast(args[1]);
3631

3732
/* maxPayloadLength */
38-
maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value();
39-
40-
/* Compression */
41-
compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
33+
behavior.maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value();
34+
35+
/* Compression, map from 0, 1, 2 to disabled, shared, dedicated */
36+
int compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
37+
if (compression == 0) {
38+
behavior.compression = uWS::CompressOptions::DISABLED;
39+
} else if (compression == 1) {
40+
behavior.compression = uWS::CompressOptions::SHARED_COMPRESSOR;
41+
} else if (compression == 2) {
42+
behavior.compression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
43+
}
4244

4345
/* Open */
4446
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) {
5052
closePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
5153
}
5254

53-
/* Map compression options from integer values */
54-
if (compression == 1) {
55-
mappedCompression = uWS::CompressOptions::SHARED_COMPRESSOR;
56-
} else if (compression == 2) {
57-
mappedCompression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
58-
}
55+
behavior.open = [openPf](auto *ws, auto *req) {
56+
HandleScope hs(isolate);
5957

60-
app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), {
61-
/* idleTimeout */
62-
.compression = mappedCompression,
63-
.maxPayloadLength = maxPayloadLength,
64-
/* Handlers */
65-
.open = [openPf](auto *ws, auto *req) {
66-
HandleScope hs(isolate);
67-
68-
/* Create a new websocket object */
69-
Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>();
70-
wsObject->SetAlignedPointerInInternalField(0, ws);
71-
72-
/* Attach a new V8 object with pointer to us, to us */
73-
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
74-
perSocketData->socketPf = new Persistent<Object>;
75-
perSocketData->socketPf->Reset(isolate, wsObject);
76-
77-
Local<Value> argv[] = {wsObject};
78-
Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
79-
},
80-
.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
81-
HandleScope hs(isolate);
82-
83-
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
84-
85-
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
86-
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
87-
/*ArrayBuffer::New(isolate, (void *) message.data(), message.length())*/ messageArrayBuffer,
88-
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)
89-
};
90-
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
91-
92-
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
93-
messageArrayBuffer->Neuter();
94-
},
95-
.drain = [drainPf](auto *ws) {
96-
HandleScope hs(isolate);
97-
98-
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
99-
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
100-
};
101-
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
102-
},
103-
.ping = [](auto *ws) {
104-
105-
},
106-
.pong = [](auto *ws) {
107-
108-
},
109-
.close = [closePf](auto *ws, int code, std::string_view message) {
110-
HandleScope hs(isolate);
111-
112-
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
113-
114-
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
115-
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
116-
Integer::New(isolate, code),
117-
messageArrayBuffer
118-
};
119-
Local<Function>::New(isolate, *closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
120-
121-
/* Again, here we clear the buffer to avoid strange bugs */
122-
messageArrayBuffer->Neuter();
123-
}
124-
});
58+
/* Create a new websocket object */
59+
Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>();
60+
wsObject->SetAlignedPointerInInternalField(0, ws);
61+
62+
/* Attach a new V8 object with pointer to us, to us */
63+
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
64+
perSocketData->socketPf = new Persistent<Object>;
65+
perSocketData->socketPf->Reset(isolate, wsObject);
66+
67+
Local<Value> argv[] = {wsObject};
68+
Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
69+
};
70+
71+
behavior.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
72+
HandleScope hs(isolate);
73+
74+
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
75+
76+
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
77+
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
78+
/*ArrayBuffer::New(isolate, (void *) message.data(), message.length())*/ messageArrayBuffer,
79+
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)
80+
};
81+
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
82+
83+
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
84+
messageArrayBuffer->Neuter();
85+
};
86+
87+
behavior.drain = [drainPf](auto *ws) {
88+
HandleScope hs(isolate);
89+
90+
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
91+
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
92+
};
93+
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
94+
};
95+
96+
behavior.ping = [](auto *ws) {
97+
98+
};
99+
100+
behavior.pong = [](auto *ws) {
101+
102+
};
103+
104+
behavior.close = [closePf](auto *ws, int code, std::string_view message) {
105+
HandleScope hs(isolate);
106+
107+
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
108+
109+
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
110+
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
111+
Integer::New(isolate, code),
112+
messageArrayBuffer
113+
};
114+
Local<Function>::New(isolate, *closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
115+
116+
/* Again, here we clear the buffer to avoid strange bugs */
117+
messageArrayBuffer->Neuter();
118+
};
119+
120+
app->template ws<PerSocketData>(std::string(nativeString.getData(), nativeString.getLength()), std::move(behavior));
125121

126122
/* Return this */
127123
args.GetReturnValue().Set(args.Holder());

0 commit comments

Comments
 (0)