Clean up readyState handling. Add test.
This commit is contained in:
Родитель
69ab87ca53
Коммит
310eed03e0
33
src/net.cc
33
src/net.cc
|
@ -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."));
|
||||
}
|
||||
|
@ -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"));
|
||||
|
||||
|
@ -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
|
||||
|
|
19
src/net.h
19
src/net.h
|
@ -56,6 +56,25 @@ protected:
|
|||
|
||||
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 */
|
||||
|
|
|
@ -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("-");
|
||||
|
@ -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();
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -55,7 +61,7 @@ function onLoad() {
|
|||
client.close();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
client.onEOF = function () {
|
||||
puts("pinger: onEOF");
|
||||
assertEquals(N, count);
|
||||
|
|
Загрузка…
Ссылка в новой задаче