Browse Source

Clean up readyState handling. Add test.

v0.7.4-release
Ryan 16 years ago
parent
commit
310eed03e0
  1. 33
      src/net.cc
  2. 19
      src/net.h
  3. 8
      test/test-pingpong.js

33
src/net.cc

@ -87,26 +87,13 @@ Connection::ReadyStateGetter (Local<String> _, const AccessorInfo& info)
HandleScope scope; HandleScope scope;
if (connection->socket_.got_full_close) { switch(connection->ReadyState()) {
return CLOSED_SYMBOL; 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"); assert(0 && "This shouldnt happen");
return ThrowException(String::New("This shouldn't happen.")); return ThrowException(String::New("This shouldn't happen."));
} }
@ -343,6 +330,11 @@ Connection::SendUtf8 (const Arguments& args)
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>(); 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()) if (!args[0]->IsString())
return ThrowException(String::New("Must have string argument")); return ThrowException(String::New("Must have string argument"));
@ -363,6 +355,11 @@ Connection::Send (const Arguments& args)
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();
if ( connection->ReadyState() != OPEN
&& connection->ReadyState() != WRITE_ONLY
)
return ThrowException(String::New("Socket is not open for writing"));
// XXX // XXX
// A lot of improvement can be made here. First of all we're allocating // 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 // oi_bufs for every send which is clearly inefficent - it should use a

19
src/net.h

@ -56,6 +56,25 @@ protected:
enum encoding encoding_; 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: private:
/* liboi callbacks */ /* liboi callbacks */

8
test/test-pingpong.js

@ -11,6 +11,7 @@ function Ponger (socket) {
puts("got socket."); puts("got socket.");
socket.onReceive = function (data) { socket.onReceive = function (data) {
assertEquals("open", socket.readyState);
//puts("server recved data: " + JSON.stringify(data)); //puts("server recved data: " + JSON.stringify(data));
assertTrue(count <= N); assertTrue(count <= N);
stdout.print("-"); stdout.print("-");
@ -20,11 +21,13 @@ function Ponger (socket) {
}; };
socket.onEOF = function () { socket.onEOF = function () {
assertEquals("writeOnly", socket.readyState);
puts("ponger: onEOF"); puts("ponger: onEOF");
socket.close(); socket.close();
}; };
socket.onDisconnect = function () { socket.onDisconnect = function () {
assertEquals("closed", socket.readyState);
puts("ponger: onDisconnect"); puts("ponger: onDisconnect");
socket.server.close(); socket.server.close();
}; };
@ -35,15 +38,18 @@ function onLoad() {
server.listen(port); server.listen(port);
var client = new node.tcp.Connection(); var client = new node.tcp.Connection();
assertEquals("closed", client.readyState);
client.encoding = "UTF8"; client.encoding = "UTF8";
client.onConnect = function () { client.onConnect = function () {
assertEquals("open", client.readyState);
puts("client is connected."); puts("client is connected.");
client.send("PING"); client.send("PING");
}; };
client.onReceive = function (data) { client.onReceive = function (data) {
assertEquals("open", client.readyState);
//puts("client recved data: " + JSON.stringify(data)); //puts("client recved data: " + JSON.stringify(data));
stdout.print("."); stdout.print(".");
assertEquals("PONG", data); assertEquals("PONG", data);
@ -55,7 +61,7 @@ function onLoad() {
client.close(); client.close();
} }
}; };
client.onEOF = function () { client.onEOF = function () {
puts("pinger: onEOF"); puts("pinger: onEOF");
assertEquals(N, count); assertEquals(N, count);

Loading…
Cancel
Save