Browse Source

Timer clean ups

Also bugfix getting pointer to C++ class in watcher
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
a01d8a0d68
  1. 7
      src/node_timer.cc
  2. 1
      src/node_timer.h
  3. 13
      test/pummel/test-tcp-timeout.js

7
src/node_timer.cc

@ -91,7 +91,7 @@ Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents)
Timer::~Timer () Timer::~Timer ()
{ {
ev_timer_stop (EV_DEFAULT_UC_ &watcher_); ev_timer_stop(EV_DEFAULT_UC_ &watcher_);
} }
Handle<Value> Handle<Value>
@ -114,14 +114,15 @@ Timer::Start (const Arguments& args)
if (args.Length() != 2) if (args.Length() != 2)
return ThrowException(String::New("Bad arguments")); return ThrowException(String::New("Bad arguments"));
bool was_active = ev_is_active(&timer->watcher_);
ev_tstamp after = NODE_V8_UNIXTIME(args[0]); ev_tstamp after = NODE_V8_UNIXTIME(args[0]);
ev_tstamp repeat = NODE_V8_UNIXTIME(args[1]); ev_tstamp repeat = NODE_V8_UNIXTIME(args[1]);
ev_timer_init(&timer->watcher_, Timer::OnTimeout, after, repeat); ev_timer_init(&timer->watcher_, Timer::OnTimeout, after, repeat);
timer->watcher_.data = timer; timer->watcher_.data = timer;
ev_timer_start(EV_DEFAULT_UC_ &timer->watcher_); ev_timer_start(EV_DEFAULT_UC_ &timer->watcher_);
timer->Ref(); if (!was_active) timer->Ref();
return Undefined(); return Undefined();
} }

1
src/node_timer.h

@ -18,6 +18,7 @@ class Timer : ObjectWrap {
Timer () : ObjectWrap () { Timer () : ObjectWrap () {
// dummy timeout values // dummy timeout values
ev_timer_init(&watcher_, OnTimeout, 0., 1.); ev_timer_init(&watcher_, OnTimeout, 0., 1.);
watcher_.data = this;
} }
~Timer(); ~Timer();

13
test/pummel/test-tcp-timeout.js

@ -1,21 +1,22 @@
require("../common"); require("../common");
tcp = require("tcp"); net = require("net");
exchanges = 0; exchanges = 0;
starttime = null; starttime = null;
timeouttime = null; timeouttime = null;
timeout = 1000; timeout = 1000;
var echo_server = tcp.createServer(function (socket) { var echo_server = net.createServer(function (socket) {
socket.setTimeout(timeout); socket.setTimeout(timeout);
socket.addListener("timeout", function (d) { socket.addListener("timeout", function () {
puts("server timeout"); puts("server timeout");
timeouttime = new Date; timeouttime = new Date;
p(timeouttime); p(timeouttime);
socket.forceClose();
}); });
socket.addListener("data", function (d) { socket.addListener("data", function (d) {
p(d); puts(d);
socket.write(d); socket.write(d);
}); });
@ -27,7 +28,7 @@ var echo_server = tcp.createServer(function (socket) {
echo_server.listen(PORT); echo_server.listen(PORT);
puts("server listening at " + PORT); puts("server listening at " + PORT);
var client = tcp.createConnection(PORT); var client = net.createConnection(PORT);
client.setEncoding("UTF8"); client.setEncoding("UTF8");
client.setTimeout(0); // disable the timeout for client client.setTimeout(0); // disable the timeout for client
client.addListener("connect", function () { client.addListener("connect", function () {
@ -53,7 +54,7 @@ client.addListener("data", function (chunk) {
client.addListener("timeout", function () { client.addListener("timeout", function () {
puts("client timeout - this shouldn't happen"); puts("client timeout - this shouldn't happen");
assert.equal(false, true); assert.ok(false);
}); });
client.addListener("end", function () { client.addListener("end", function () {

Loading…
Cancel
Save