Browse Source

add io watcher

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
f219938b69
  1. 8
      src/node.cc
  2. 126
      src/node_io_watcher.cc
  3. 40
      src/node_io_watcher.h
  4. 1
      wscript

8
src/node.cc

@ -11,6 +11,7 @@
#include <dlfcn.h> /* dlopen(), dlsym() */
#include <node_buffer.h>
#include <node_io_watcher.h>
#include <node_events.h>
#include <node_dns.h>
#include <node_net.h>
@ -849,10 +850,13 @@ static Local<Object> Load(int argc, char *argv[]) {
// Initialize the C++ modules..................filename of module
InitBuffer(process); // buffer.cc
Stdio::Initialize(process); // stdio.cc
IOWatcher::Initialize(process); // io_watcher.cc
Timer::Initialize(process); // timer.cc
SignalHandler::Initialize(process); // signal_handler.cc
Stat::Initialize(process); // stat.cc
SignalHandler::Initialize(process); // signal_handler.cc
Stdio::Initialize(process); // stdio.cc
ChildProcess::Initialize(process); // child_process.cc
DefineConstants(process); // constants.cc
// Create node.dns

126
src/node_io_watcher.cc

@ -0,0 +1,126 @@
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#include <node_io_watcher.h>
#include <assert.h>
namespace node {
using namespace v8;
Persistent<FunctionTemplate> IOWatcher::constructor_template;
void IOWatcher::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(IOWatcher::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(2);
constructor_template->SetClassName(String::NewSymbol("IOWatcher"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", IOWatcher::Start);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", IOWatcher::Stop);
target->Set(String::NewSymbol("IOWatcher"), constructor_template->GetFunction());
}
void IOWatcher::Callback(EV_P_ ev_io *w, int revents) {
IOWatcher *io = static_cast<IOWatcher*>(w->data);
assert(w == &io->watcher_);
HandleScope scope;
Local<Value> callback_v = io->handle_->GetInternalField(1);
assert(callback_v->IsFunction());
Local<Function> callback = Local<Function>::Cast(callback_v);
TryCatch try_catch;
Local<Value> argv[2];
argv[0] = Local<Value>::New(revents & EV_READ ? True() : False());
argv[1] = Local<Value>::New(revents & EV_WRITE ? True() : False());
callback->Call(io->handle_, 2, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
//
// var io = new process.IOWatcher(fd, true, true, function (readable, writable) {
//
// });
//
Handle<Value> IOWatcher::New(const Arguments& args) {
HandleScope scope;
if (!args[0]->IsInt32()) {
return ThrowException(Exception::TypeError(
String::New("First arg should be a file descriptor.")));
}
int fd = args[0]->Int32Value();
if (!args[1]->IsBoolean()) {
return ThrowException(Exception::TypeError(
String::New("Second arg should boolean (readable).")));
}
int events = 0;
if (args[1]->IsTrue()) events |= EV_READ;
if (!args[2]->IsBoolean()) {
return ThrowException(Exception::TypeError(
String::New("Third arg should boolean (writable).")));
}
if (args[2]->IsTrue()) events |= EV_WRITE;
if (!args[3]->IsFunction()) {
return ThrowException(Exception::TypeError(
String::New("Fourth arg should a callback.")));
}
Local<Function> callback = Local<Function>::Cast(args[3]);
IOWatcher *s = new IOWatcher(fd, events);
s->Wrap(args.This());
s->handle_->SetInternalField(1, callback);
return args.This();
}
Handle<Value> IOWatcher::Start(const Arguments& args) {
HandleScope scope;
IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
ev_io_start(EV_DEFAULT_UC_ &io->watcher_);
io->Ref();
return Undefined();
}
Handle<Value> IOWatcher::Stop(const Arguments& args) {
HandleScope scope;
IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
io->Stop();
return Undefined();
}
void IOWatcher::Stop () {
if (watcher_.active) {
ev_io_stop(EV_DEFAULT_UC_ &watcher_);
Unref();
}
}
} // namespace node

40
src/node_io_watcher.h

@ -0,0 +1,40 @@
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#ifndef NODE_IO_H_
#define NODE_IO_H_
#include <node.h>
#include <ev.h>
namespace node {
class IOWatcher : ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
protected:
static v8::Persistent<v8::FunctionTemplate> constructor_template;
IOWatcher(int fd, int events) : ObjectWrap() {
ev_io_init(&watcher_, IOWatcher::Callback, fd, events);
watcher_.data = this;
}
~IOWatcher() {
Stop();
}
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> Start(const v8::Arguments& args);
static v8::Handle<v8::Value> Stop(const v8::Arguments& args);
private:
static void Callback(EV_P_ ev_io *watcher, int revents);
void Stop();
ev_io watcher_;
};
} // namespace node
#endif // NODE_IO_H_

1
wscript

@ -323,6 +323,7 @@ def build(bld):
node.source = """
src/node.cc
src/node_buffer.cc
src/node_io_watcher.cc
src/node_child_process.cc
src/node_constants.cc
src/node_dns.cc

Loading…
Cancel
Save