Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add "had_error" argument to the "onDisconnect" in node.tcp.Client
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ry committed Jun 4, 2009
1 parent b933743 commit 8cfdd32
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
18 changes: 17 additions & 1 deletion src/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,23 @@ Connection::OnReceive (const void *buf, size_t len)
FatalException(try_catch);
}

void
Connection::OnDisconnect ()
{
HandleScope scope;
Local<Value> callback_v = handle_->Get(ON_DISCONNECT_SYMBOL);
if (!callback_v->IsFunction()) return;
Handle<Function> callback = Handle<Function>::Cast(callback_v);

Handle<Value> 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 () \
{ \
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion test/test-pingpong.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 12 additions & 7 deletions test/test-reconnecting-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
Expand All @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions website/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,13 @@ <h3 id="tcp_connection"><code>node.tcp.Connection</code></h3>
You should probably just call <code>connection.close()</code> in this
callback.

<dt><code>conneciton.onDisconnect = function () { };</code></dt>
<dd>Called once the connection is fully disconnected.</dd>
<dt><code>conneciton.onDisconnect = function (had_error) { };</code></dt>
<dd>Called once the connection is fully disconnected.

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

<dt><code>conneciton.onError = function () { };</code></dt>
<dd>Called on an error.</dd>
Expand Down

0 comments on commit 8cfdd32

Please sign in to comment.