Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

src: switch from ev_io to uv_poll_t

Update package.json, the minimum required node version is now 0.7.9.
  • Loading branch information...
commit 04dd6d73553ea2bcadfda74e7692ad58f41ecb53 1 parent 429c20f
@bnoordhuis authored
Showing with 28 additions and 28 deletions.
  1. +3 −6 package.json
  2. +25 −22 src/unix_dgram.cc
View
9 package.json
@@ -5,10 +5,10 @@
"url": "http://bnoordhuis.nl/"
},
"name": "unix-dgram",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "Unix datagram socket",
"main": "src/unix_dgram",
- "homepage": "https://github.com/bnoordhuis/node-unix-dgram/",
+ "homepage": "https://github.com/bnoordhuis/node-unix-dgram",
"license": {
"type": "ISC",
"url": "https://github.com/bnoordhuis/node-unix-dgram/LICENSE"
@@ -17,11 +17,8 @@
"type": "git",
"url": "git://github.com/bnoordhuis/node-unix-dgram.git"
},
- "scripts": {
- "preinstall": "node-waf configure build"
- },
"engines": {
- "node": ">= 0.4.0"
+ "node": ">=0.7.9"
},
"dependencies": {},
"devDependencies": {},
View
47 src/unix_dgram.cc
@@ -1,9 +1,13 @@
// -D_GNU_SOURCE makes SOCK_NONBLOCK etc. available on linux
-#ifndef _GNU_SOURCE
+#undef _GNU_SOURCE
#define _GNU_SOURCE
-#endif
+
+#include "uv.h"
+#include "node.h"
+#include "node_buffer.h"
#include <errno.h>
+#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
@@ -14,23 +18,27 @@
#include <sys/socket.h>
#include <sys/un.h>
-#include <node.h>
-#include <node_buffer.h>
-
#include <map>
+#define offset_of(type, member) \
+ ((intptr_t) ((char *) &(((type *) 8)->member) - 8))
+
+#define container_of(ptr, type, member) \
+ ((type *) ((char *) (ptr) - offset_of(type, member)))
+
using namespace v8;
using namespace node;
namespace {
-typedef std::map<int, ev_io*> watchers_t;
-
struct SocketContext {
Persistent<Function> cb_;
+ uv_poll_t handle_;
int fd_;
};
+typedef std::map<int, SocketContext*> watchers_t;
+
Persistent<String> errno_symbol;
watchers_t watchers;
@@ -67,7 +75,7 @@ void SetErrno(int errorno) {
}
-void OnRecv(EV_P_ ev_io* w, int revents) {
+void OnRecv(uv_poll_t* handle, int status, int events) {
HandleScope scope;
Handle<Value> argv[3];
sockaddr_storage ss;
@@ -78,14 +86,14 @@ void OnRecv(EV_P_ ev_io* w, int revents) {
int size;
int r;
+ sc = container_of(handle, SocketContext, handle_);
+
r = -1;
buf = NULL;
argv[0] = argv[1] = argv[2] = Null();
- assert(!(revents & ~EV_READ));
-
- sc = reinterpret_cast<SocketContext*>(w->data);
- assert(sc != NULL);
+ assert(0 == status);
+ assert(0 == (events & ~UV_READABLE));
if ((r = ioctl(sc->fd_, FIONREAD, &size)) == -1) {
SetErrno(errno);
@@ -131,13 +139,11 @@ void StartWatcher(int fd, Handle<Value> callback) {
sc->cb_ = Persistent<Function>::New(callback.As<Function>());
sc->fd_ = fd;
- ev_io* w = new ev_io;
- ev_io_init(w, OnRecv, fd, EV_READ);
- w->data = reinterpret_cast<void*>(sc);
- ev_io_start(EV_DEFAULT_UC_ w);
+ uv_poll_init(uv_default_loop(), &sc->handle_, fd);
+ uv_poll_start(&sc->handle_, UV_READABLE, OnRecv);
// so we can disarm the watcher when close(fd) is called
- watchers.insert(watchers_t::value_type(fd, w));
+ watchers.insert(watchers_t::value_type(fd, sc));
}
@@ -145,16 +151,13 @@ void StopWatcher(int fd) {
watchers_t::iterator iter = watchers.find(fd);
assert(iter != watchers.end());
- ev_io* w = iter->second;
- SocketContext* sc = reinterpret_cast<SocketContext*>(w->data);
+ SocketContext* sc = iter->second;
sc->cb_.Dispose();
sc->cb_.Clear();
- ev_io_stop(EV_DEFAULT_UC_ w);
+ uv_poll_stop(&sc->handle_);
watchers.erase(iter);
-
delete sc;
- delete w;
}
Please sign in to comment.
Something went wrong with that request. Please try again.