From 8cfdd326a858262545dbf335da9ce0507145d8e5 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 4 Jun 2009 12:31:45 +0200 Subject: [PATCH] Add "had_error" argument to the "onDisconnect" in node.tcp.Client This is a boolean value which allows one to detect if the socket was closed due to errors. There is not yet a way to look up the actual error code. --- src/net.cc | 18 +++++++++++++++++- test/test-pingpong.js | 3 ++- test/test-reconnecting-socket.js | 19 ++++++++++++------- website/api.html | 9 +++++++-- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/net.cc b/src/net.cc index 047df0e..aea6b95 100644 --- a/src/net.cc +++ b/src/net.cc @@ -422,6 +422,23 @@ Connection::OnReceive (const void *buf, size_t len) FatalException(try_catch); } +void +Connection::OnDisconnect () +{ + HandleScope scope; + Local callback_v = handle_->Get(ON_DISCONNECT_SYMBOL); + if (!callback_v->IsFunction()) return; + Handle callback = Handle::Cast(callback_v); + + Handle argv[1]; + argv[0] = socket_.errorno == 0 ? False() : True(); + + TryCatch try_catch; + callback->Call(handle_, 1, argv); + if (try_catch.HasCaught()) + node::FatalException(try_catch); +} + #define DEFINE_SIMPLE_CALLBACK(name, symbol) \ void name () \ { \ @@ -437,7 +454,6 @@ void name () \ DEFINE_SIMPLE_CALLBACK(Connection::OnConnect, ON_CONNECT_SYMBOL) DEFINE_SIMPLE_CALLBACK(Connection::OnDrain, ON_DRAIN_SYMBOL) -DEFINE_SIMPLE_CALLBACK(Connection::OnDisconnect, ON_DISCONNECT_SYMBOL) DEFINE_SIMPLE_CALLBACK(Connection::OnTimeout, ON_TIMEOUT_SYMBOL) DEFINE_SIMPLE_CALLBACK(Connection::OnEOF, ON_EOF_SYMBOL) diff --git a/test/test-pingpong.js b/test/test-pingpong.js index 8d02afd..619670d 100644 --- a/test/test-pingpong.js +++ b/test/test-pingpong.js @@ -26,7 +26,8 @@ function Ponger (socket) { socket.close(); }; - socket.onDisconnect = function () { + socket.onDisconnect = function (had_error) { + assertFalse(had_error); assertEquals("closed", socket.readyState); puts("ponger: onDisconnect"); socket.server.close(); diff --git a/test/test-reconnecting-socket.js b/test/test-reconnecting-socket.js index a06070f..593fb5e 100644 --- a/test/test-reconnecting-socket.js +++ b/test/test-reconnecting-socket.js @@ -3,7 +3,7 @@ var port = 8921; function onLoad () { - new node.tcp.Server(function (socket) { + var server = new node.tcp.Server(function (socket) { puts("new connection"); socket.onConnect = function () { socket.send("hello\r\n"); @@ -13,10 +13,12 @@ function onLoad () { socket.close(); }; - socket.onDisconnect = function () { - socket.server.close(); + socket.onDisconnect = function (had_error) { + //puts("server had_error: " + JSON.stringify(had_error)); + assertFalse(had_error); }; - }).listen(port); + }); + server.listen(port); var count = 0; var client = new node.tcp.Connection(); @@ -32,10 +34,13 @@ function onLoad () { client.fullClose(); }; - client.onDisconnect = function () { + client.onDisconnect = function (had_error) { + assertFalse(had_error); puts("client disconnected"); - if (count++ < 5) - client.connect(port); + if (count++ < 5) + client.connect(port); // reconnect + else + server.close(); }; client.connect(port); diff --git a/website/api.html b/website/api.html index 3a8ec11..63486fd 100644 --- a/website/api.html +++ b/website/api.html @@ -398,8 +398,13 @@ server.listen(7000, "localhost"); You should probably just call connection.close() in this callback. -
conneciton.onDisconnect = function () { };
-
Called once the connection is fully disconnected.
+
conneciton.onDisconnect = function (had_error) { };
+
Called once the connection is fully disconnected. + +

The callback is passed one boolean argument had_error. + This lets one know if the connect was closed due to an error. (TODO: look + up error codes.) +

conneciton.onError = function () { };
Called on an error.
-- 2.7.4