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

Commit

Permalink
Clean up readyState handling. Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 18, 2009
1 parent 69ab87c commit 310eed0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
33 changes: 15 additions & 18 deletions src/net.cc
Expand Up @@ -87,26 +87,13 @@ Connection::ReadyStateGetter (Local<String> _, const AccessorInfo& info)

HandleScope scope;

if (connection->socket_.got_full_close) {
return CLOSED_SYMBOL;
switch(connection->ReadyState()) {
case OPEN: return OPEN_SYMBOL;
case CLOSED: return CLOSED_SYMBOL;
case READ_ONLY: return READ_ONLY_SYMBOL;
case WRITE_ONLY: return WRITE_ONLY_SYMBOL;
}

if (connection->socket_.got_half_close) {
if (connection->socket_.read_action)
return READ_ONLY_SYMBOL;
else
return CLOSED_SYMBOL;
}

if (connection->socket_.read_action && connection->socket_.write_action)
return OPEN_SYMBOL;
else if (connection->socket_.write_action)
return WRITE_ONLY_SYMBOL;
else if (connection->socket_.read_action)
return READ_ONLY_SYMBOL;
else
return CLOSED_SYMBOL;

assert(0 && "This shouldnt happen");
return ThrowException(String::New("This shouldn't happen."));
}
Expand Down Expand Up @@ -343,6 +330,11 @@ Connection::SendUtf8 (const Arguments& args)
Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();

if ( connection->ReadyState() != OPEN
&& connection->ReadyState() != WRITE_ONLY
)
return ThrowException(String::New("Socket is not open for writing"));

if (!args[0]->IsString())
return ThrowException(String::New("Must have string argument"));

Expand All @@ -363,6 +355,11 @@ Connection::Send (const Arguments& args)
Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();

if ( connection->ReadyState() != OPEN
&& connection->ReadyState() != WRITE_ONLY
)
return ThrowException(String::New("Socket is not open for writing"));

// XXX
// A lot of improvement can be made here. First of all we're allocating
// oi_bufs for every send which is clearly inefficent - it should use a
Expand Down
19 changes: 19 additions & 0 deletions src/net.h
Expand Up @@ -56,6 +56,25 @@ class Connection : public ObjectWrap {

enum encoding encoding_;

enum readyState { OPEN, CLOSED, READ_ONLY, WRITE_ONLY };
enum readyState ReadyState ( )
{
if (socket_.got_full_close)
return CLOSED;

if (socket_.got_half_close)
return (socket_.read_action == NULL ? CLOSED : READ_ONLY);

if (socket_.read_action && socket_.write_action)
return OPEN;
else if (socket_.write_action)
return WRITE_ONLY;
else if (socket_.read_action)
return READ_ONLY;

return CLOSED;
}

private:

/* liboi callbacks */
Expand Down
8 changes: 7 additions & 1 deletion test/test-pingpong.js
Expand Up @@ -11,6 +11,7 @@ function Ponger (socket) {
puts("got socket.");

socket.onReceive = function (data) {
assertEquals("open", socket.readyState);
//puts("server recved data: " + JSON.stringify(data));
assertTrue(count <= N);
stdout.print("-");
Expand All @@ -20,11 +21,13 @@ function Ponger (socket) {
};

socket.onEOF = function () {
assertEquals("writeOnly", socket.readyState);
puts("ponger: onEOF");
socket.close();
};

socket.onDisconnect = function () {
assertEquals("closed", socket.readyState);
puts("ponger: onDisconnect");
socket.server.close();
};
Expand All @@ -35,15 +38,18 @@ function onLoad() {
server.listen(port);

var client = new node.tcp.Connection();
assertEquals("closed", client.readyState);

client.encoding = "UTF8";

client.onConnect = function () {
assertEquals("open", client.readyState);
puts("client is connected.");
client.send("PING");
};

client.onReceive = function (data) {
assertEquals("open", client.readyState);
//puts("client recved data: " + JSON.stringify(data));
stdout.print(".");
assertEquals("PONG", data);
Expand All @@ -55,7 +61,7 @@ function onLoad() {
client.close();
}
};

client.onEOF = function () {
puts("pinger: onEOF");
assertEquals(N, count);
Expand Down

0 comments on commit 310eed0

Please sign in to comment.