From a01d8a0d68511b14b3348143bce990329f14cc5d Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 23 Mar 2010 17:28:20 -0700 Subject: [PATCH] Timer clean ups Also bugfix getting pointer to C++ class in watcher --- src/node_timer.cc | 7 ++++--- src/node_timer.h | 1 + test/pummel/test-tcp-timeout.js | 13 +++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/node_timer.cc b/src/node_timer.cc index 5ab409b048..1c1b01ce7a 100644 --- a/src/node_timer.cc +++ b/src/node_timer.cc @@ -91,7 +91,7 @@ Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents) Timer::~Timer () { - ev_timer_stop (EV_DEFAULT_UC_ &watcher_); + ev_timer_stop(EV_DEFAULT_UC_ &watcher_); } Handle @@ -114,14 +114,15 @@ Timer::Start (const Arguments& args) if (args.Length() != 2) return ThrowException(String::New("Bad arguments")); + bool was_active = ev_is_active(&timer->watcher_); + ev_tstamp after = NODE_V8_UNIXTIME(args[0]); ev_tstamp repeat = NODE_V8_UNIXTIME(args[1]); - ev_timer_init(&timer->watcher_, Timer::OnTimeout, after, repeat); timer->watcher_.data = timer; ev_timer_start(EV_DEFAULT_UC_ &timer->watcher_); - timer->Ref(); + if (!was_active) timer->Ref(); return Undefined(); } diff --git a/src/node_timer.h b/src/node_timer.h index 56352912a6..5af5272896 100644 --- a/src/node_timer.h +++ b/src/node_timer.h @@ -18,6 +18,7 @@ class Timer : ObjectWrap { Timer () : ObjectWrap () { // dummy timeout values ev_timer_init(&watcher_, OnTimeout, 0., 1.); + watcher_.data = this; } ~Timer(); diff --git a/test/pummel/test-tcp-timeout.js b/test/pummel/test-tcp-timeout.js index b06c81da69..3f7b8f3157 100644 --- a/test/pummel/test-tcp-timeout.js +++ b/test/pummel/test-tcp-timeout.js @@ -1,21 +1,22 @@ require("../common"); -tcp = require("tcp"); +net = require("net"); exchanges = 0; starttime = null; timeouttime = null; timeout = 1000; -var echo_server = tcp.createServer(function (socket) { +var echo_server = net.createServer(function (socket) { socket.setTimeout(timeout); - socket.addListener("timeout", function (d) { + socket.addListener("timeout", function () { puts("server timeout"); timeouttime = new Date; p(timeouttime); + socket.forceClose(); }); socket.addListener("data", function (d) { - p(d); + puts(d); socket.write(d); }); @@ -27,7 +28,7 @@ var echo_server = tcp.createServer(function (socket) { echo_server.listen(PORT); puts("server listening at " + PORT); -var client = tcp.createConnection(PORT); +var client = net.createConnection(PORT); client.setEncoding("UTF8"); client.setTimeout(0); // disable the timeout for client client.addListener("connect", function () { @@ -53,7 +54,7 @@ client.addListener("data", function (chunk) { client.addListener("timeout", function () { puts("client timeout - this shouldn't happen"); - assert.equal(false, true); + assert.ok(false); }); client.addListener("end", function () {