Skip to content

Commit

Permalink
Update node host to the new API. Update node dependencies. Fix unnece…
Browse files Browse the repository at this point in the history
…ssary ES6 depdendency.
  • Loading branch information
seemk committed Oct 2, 2018
1 parent abf93b8 commit 8e640f5
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -91,7 +91,7 @@ install(EXPORT WuTargets
DESTINATION lib/cmake/Wu
)

add_executable(EchoServer examples/EchoServer.cpp)
add_executable(EchoServer examples/EchoServer.c)
target_link_libraries(EchoServer WuHost)

set_target_properties(Wu WuHost EchoServer PROPERTIES
Expand Down
14 changes: 13 additions & 1 deletion Wu.cpp
Expand Up @@ -733,10 +733,22 @@ void WuSetErrorCallback(Wu* wu, WuErrorFn callback) {
}
}

void WuDestroy(Wu* wu) {
void WuDestroy(Wu* wu) {
if (!wu) {
return;
}

free(wu);
}

WuClient* WuFindClient(const Wu* wu, WuAddress address) {
for (int32_t i = 0; i < wu->numClients; i++) {
WuClient* c = wu->clients[i];

if (c->address.host == address.host && c->address.port == address.port) {
return c;
}
}

return NULL;
}
1 change: 1 addition & 0 deletions Wu.h
Expand Up @@ -67,6 +67,7 @@ void WuSetUDPWriteFunction(Wu* wu, WuWriteFn write);
void WuSetUserData(Wu* wu, void* userData);
void WuSetErrorCallback(Wu* wu, WuErrorFn callback);
WuAddress WuClientGetAddress(const WuClient* client);
WuClient* WuFindClient(const Wu* wu, WuAddress address);

#ifdef __cplusplus
}
Expand Down
10 changes: 9 additions & 1 deletion WuHost.h
Expand Up @@ -11,13 +11,21 @@ typedef struct WuHost WuHost;
int32_t WuHostCreate(const char* hostAddr, const char* port, int32_t maxClients,
WuHost** host);
void WuHostDestroy(WuHost* host);
int32_t WuHostServe(WuHost* host, WuEvent* evt);
/*
* Timeout:
* -1 = Block until an event
* 0 = Return immediately
* >0 = Block for N milliseconds
* Returns 1 if an event was received, 0 otherwise.
*/
int32_t WuHostServe(WuHost* host, WuEvent* evt, int timeout);
void WuHostRemoveClient(WuHost* wu, WuClient* client);
int32_t WuHostSendText(WuHost* host, WuClient* client, const char* text,
int32_t length);
int32_t WuHostSendBinary(WuHost* host, WuClient* client, const uint8_t* data,
int32_t length);
void WuHostSetErrorCallback(WuHost* host, WuErrorFn callback);
WuClient* WuHostFindClient(const WuHost* host, WuAddress address);
#ifdef __cplusplus
}
#endif
8 changes: 6 additions & 2 deletions WuHostEpoll.cpp
Expand Up @@ -154,15 +154,15 @@ static void HandleHttpRequest(WuHost* host, WuConnectionBuffer* conn) {
}
}

int32_t WuHostServe(WuHost* host, WuEvent* evt) {
int32_t WuHostServe(WuHost* host, WuEvent* evt, int timeout) {
int32_t hres = WuUpdate(host->wu, evt);

if (hres) {
return hres;
}

int n =
epoll_wait(host->epfd, host->events, host->maxEvents, host->pollTimeout);
epoll_wait(host->epfd, host->events, host->maxEvents, timeout);

for (int i = 0; i < n; i++) {
struct epoll_event* e = &host->events[i];
Expand Down Expand Up @@ -377,3 +377,7 @@ void WuHostDestroy(WuHost* host) {
free(host->events);
}
}

WuClient* WuHostFindClient(const WuHost* host, WuAddress address) {
return WuFindClient(host->wu, address);
}
54 changes: 44 additions & 10 deletions WuHostNode.cpp
Expand Up @@ -79,7 +79,9 @@ void WuHostWrap::HandleClientJoin(WuClient* client) {

const int argc = 1;
v8::Local<v8::Value> argv[argc] = {args};
clientJoinCallback.Call(argc, argv);

Nan::AsyncResource ar("ClientJoin");
clientJoinCallback.Call(argc, argv, &ar);
}

void WuHostWrap::HandleClientLeave(WuClient* client) {
Expand All @@ -95,7 +97,9 @@ void WuHostWrap::HandleClientLeave(WuClient* client) {

const int argc = 1;
v8::Local<v8::Value> argv[argc] = {args};
clientLeaveCallback.Call(argc, argv);

Nan::AsyncResource ar("ClientLeave");
clientLeaveCallback.Call(argc, argv, &ar);

WuRemoveClient(host.wu, client);
}
Expand All @@ -118,13 +122,17 @@ void WuHostWrap::HandleContent(const WuEvent* evt) {
Nan::Set(args, Nan::New("text").ToLocalChecked(),
Nan::New((const char*)evt->data, evt->length).ToLocalChecked());
v8::Local<v8::Value> argv[argc] = {args};
textDataCallback.Call(argc, argv);

Nan::AsyncResource ar("TextData");
textDataCallback.Call(argc, argv, &ar);
} else if (evt->type == WuEvent_BinaryData) {
auto buf = Nan::CopyBuffer((const char*)evt->data, (uint32_t)evt->length)
.ToLocalChecked();
Nan::Set(args, Nan::New("data").ToLocalChecked(), buf);
v8::Local<v8::Value> argv[argc] = {args};
binaryDataCallback.Call(argc, argv);

Nan::AsyncResource ar("BinaryData");
binaryDataCallback.Call(argc, argv, &ar);
}
}

Expand All @@ -146,7 +154,9 @@ void WriteUDPData(const uint8_t* data, size_t length, const WuClient* client,

const int argc = 2;
v8::Local<v8::Value> argv[argc] = {buf, addr};
wrap->udpWriteCallback.Call(argc, argv);

Nan::AsyncResource ar("UDPWrite");
wrap->udpWriteCallback.Call(argc, argv, &ar);
}

void WuHostWrap::RemoveClient(uint32_t id) {
Expand Down Expand Up @@ -196,12 +206,36 @@ NAN_METHOD(WuHostWrap::New) {
std::string port =
*v8::String::Utf8Value(Nan::To<v8::String>(info[1]).ToLocalChecked());

WuConf conf;
conf.host = host.c_str();
conf.port = port.c_str();
int maxClients = 512;
if (info.Length() >= 3 && info[2]->IsObject()) {
Nan::MaybeLocal<v8::Object> maybeExtraArgs = Nan::To<v8::Object>(info[2]);

if (!maybeExtraArgs.IsEmpty()) {
v8::Local<v8::Object> extraArgs = maybeExtraArgs.ToLocalChecked();

v8::MaybeLocal<v8::Value> maybeMaxClientsVal =
Nan::Get(extraArgs, Nan::New("maxClients").ToLocalChecked());

if (!maybeMaxClientsVal.IsEmpty() &&
maybeMaxClientsVal.ToLocalChecked()->IsNumber()) {
Nan::Maybe<int32_t> maybeMaxClients =
Nan::To<int32_t>(maybeMaxClientsVal.ToLocalChecked());

if (maybeMaxClients.IsJust()) {
maxClients = maybeMaxClients.FromJust();

if (maxClients <= 0) {
maxClients = 1;
}
}
}
}
}

Wu* wu = nullptr;
int32_t status = WuCreate(host.c_str(), port.c_str(), maxClients, &wu);

Wu* wu = (Wu*)calloc(1, sizeof(Wu));
if (!WuInit(wu, &conf)) {
if (status != WU_OK) {
Nan::ThrowError("Initialization error");
return;
}
Expand Down
12 changes: 6 additions & 6 deletions examples/EchoServer.cpp → examples/EchoServer.c
Expand Up @@ -17,17 +17,13 @@ int main(int argc, char** argv) {

int32_t status = WuHostCreate(hostAddr, port, maxClients, &host);
if (status != WU_OK) {
printf("init fail\n");
printf("failed to create host\n");
return 1;
}

WuHostSetErrorCallback(host, [](const char* err, void*) {
printf("error: %s\n", err);
});

for (;;) {
WuEvent evt;
while (WuHostServe(host, &evt)) {
while (WuHostServe(host, &evt, 0)) {
switch (evt.type) {
case WuEvent_ClientJoin: {
printf("EchoServer: client join\n");
Expand All @@ -44,6 +40,10 @@ int main(int argc, char** argv) {
WuHostSendText(host, evt.client, text, length);
break;
}
case WuEvent_BinaryData: {
WuHostSendBinary(host, evt.client, evt.data, evt.length);
break;
}
default:
break;
}
Expand Down
5 changes: 4 additions & 1 deletion examples/EchoServer.js
Expand Up @@ -5,14 +5,17 @@ const dgram = require("dgram");

const HOST = "127.0.0.1";
const PORT = 9555;
const MAX_CLIENTS = 512;

/* An example how to use the WebUDP node addon. */
let app = express();
app.use(cors());
app.use(require("body-parser").text());

let udp = dgram.createSocket("udp4");
let host = new WebUDP.Host(HOST, PORT);
let host = new WebUDP.Host(HOST, PORT, {
maxClients: MAX_CLIENTS
});

host.setUDPWriteFunction((msg, {port, address}) => {
udp.send(msg, port, address);
Expand Down
11 changes: 6 additions & 5 deletions examples/client/wusocket.js
Expand Up @@ -10,9 +10,10 @@ var WuSocket = function(address) {
WuSocket.prototype.send = function(data) {
if (this.open) {
this.channel.send(data);
} else {
console.log("attempt to send in closed state");
return true;
}

return false;
};

WuSocket.prototype.close = function() {
Expand Down Expand Up @@ -76,13 +77,13 @@ WuSocket.prototype.beginConnection = function() {
peer.createOffer().then(function(offer) {
return peer.setLocalDescription(offer);
}).then(function() {
let request = new XMLHttpRequest();
var request = new XMLHttpRequest();
request.open("POST", socket.address);
request.onload = function() {
if (request.status == 200) {
let response = JSON.parse(request.responseText);
var response = JSON.parse(request.responseText);
peer.setRemoteDescription(new RTCSessionDescription(response.answer)).then(function() {
let candidate = new RTCIceCandidate(response.candidate);
var candidate = new RTCIceCandidate(response.candidate);
peer.addIceCandidate(candidate).then(function() {
console.log("add ice candidate success");
}).catch(function(err) {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -8,12 +8,12 @@
"build": "node-gyp build"
},
"dependencies": {
"express": "4.16.2",
"express": "4.16.3",
"cors": "2.8.4",
"body-parser": "1.18.2"
"body-parser": "1.18.3"
},
"devDependencies": {
"node-gyp": "3.6.2",
"nan": "2.8.0"
"node-gyp": "3.8.0",
"nan": "2.11.1"
}
}

0 comments on commit 8e640f5

Please sign in to comment.