Browse Source

add Connection::SendEOF. modify test accordingly.

v0.7.4-release
Ryan 16 years ago
parent
commit
1713386580
  1. 33
      src/net.cc
  2. 15
      src/net.h
  3. 35
      test/test-pingpong.js

33
src/net.cc

@ -19,6 +19,7 @@ using namespace node;
#define ON_DRAIN_SYMBOL String::NewSymbol("onDrain")
#define ON_TIMEOUT_SYMBOL String::NewSymbol("onTimeout")
#define ON_ERROR_SYMBOL String::NewSymbol("onError")
#define ON_EOF_SYMBOL String::NewSymbol("onEOF")
#define SEND_SYMBOL String::NewSymbol("send")
#define DISCONNECT_SYMBOL String::NewSymbol("disconnect")
@ -55,14 +56,15 @@ Connection::Initialize (v8::Handle<v8::Object> target)
NODE_SET_METHOD(t->InstanceTemplate(), "connect", Connection::v8Connect);
NODE_SET_METHOD(t->InstanceTemplate(), "disconnect", Connection::v8Disconnect);
NODE_SET_METHOD(t->InstanceTemplate(), "send", Connection::v8Send);
NODE_SET_METHOD(t->InstanceTemplate(), "sendEOF", Connection::v8SendEOF);
}
Local<Object>
Connection::NewInstance (Local<Function> protocol)
Connection::NewServerSideInstance (Local<Function> protocol, Handle<Object> server)
{
HandleScope scope;
Handle<Value> argv[] = { protocol };
Local<Object> instance = tcp_connection_constructor->NewInstance(1, argv);
Handle<Value> argv[] = { protocol, server };
Local<Object> instance = tcp_connection_constructor->NewInstance(2, argv);
return scope.Close(instance);
}
@ -124,8 +126,16 @@ Connection::v8New (const Arguments& args)
return ThrowException(String::New("Must pass a class as the first argument."));
Handle<Function> protocol = Handle<Function>::Cast(args[0]);
Handle<Value> argv[] = { args.This() };
Local<Object> protocol_instance = protocol->NewInstance(1, argv);
int argc = args.Length();
Handle<Value> argv[argc];
argv[0] = args.This();
for (int i = 1; i < args.Length(); i++) {
argv[i] = args[i];
}
Local<Object> protocol_instance = protocol->NewInstance(argc, argv);
args.This()->Set(PROTOCOL_SYMBOL, protocol_instance);
new Connection(args.This());
@ -252,6 +262,15 @@ Connection::v8Send (const Arguments& args)
return Undefined();
}
Handle<Value>
Connection::v8SendEOF (const Arguments& args)
{
HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder());
connection->SendEOF();
return Undefined();
}
void
Connection::OnReceive (const void *buf, size_t len)
{
@ -312,6 +331,7 @@ 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)
void
Acceptor::Initialize (Handle<Object> target)
@ -367,7 +387,8 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
}
Local<Function> protocol = Local<Function>::Cast(protocol_v);
Local<Object> connection_handle = Connection::NewInstance(protocol);
Local<Object> connection_handle =
Connection::NewServerSideInstance(protocol, handle_);
Connection *connection = new Connection(connection_handle);
return connection;

15
src/net.h

@ -24,7 +24,11 @@ public:
oi_socket_write (&socket_, buf);
}
void Disconnect () {
void SendEOF (void) {
oi_socket_write_eof (&socket_);
}
void Disconnect (void) {
oi_socket_close (&socket_);
}
@ -32,17 +36,21 @@ protected:
static v8::Handle<v8::Value> v8New (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Connect (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Send (const v8::Arguments& args);
static v8::Handle<v8::Value> v8SendEOF (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Disconnect (const v8::Arguments& args);
void OnConnect (void);
void OnReceive (const void *buf, size_t len);
void OnDrain (void);
void OnEOF (void);
void OnDisconnect (void);
void OnError (oi_error e);
void OnTimeout (void);
v8::Local<v8::Object> GetProtocol (void);
static v8::Local<v8::Object> NewInstance (v8::Local<v8::Function> protocol);
static v8::Local<v8::Object> NewServerSideInstance (
v8::Local<v8::Function> protocol,
v8::Handle<v8::Object> server);
private:
static void _OnConnect (oi_socket *s) {
@ -52,6 +60,9 @@ private:
static void _OnReceive (oi_socket *s, const void *buf, size_t len) {
Connection *connection = static_cast<Connection*> (s->data);
if (len == 0)
connection->OnEOF();
else
connection->OnReceive(buf, len);
}

35
test/test-pingpong.js

@ -4,10 +4,9 @@ var port = 12123;
var N = 100;
var count = 0;
var server;
function Ponger (socket) {
function Ponger (socket, server) {
this.encoding = "UTF8";
this.timeout = 0;
this.onConnect = function () {
puts("got socket.");
@ -16,13 +15,21 @@ function Ponger (socket) {
this.onReceive = function (data) {
assertTrue(count <= N);
stdout.print ("-");
if (/QUIT/.exec(data)) {
socket.disconnect();
//server.close();
} else if (/PING/.exec(data)) {
if (/PING/.exec(data)) {
socket.send("PONG");
}
};
this.onEOF = function () {
puts("ponger: onEOF");
socket.send("QUIT");
socket.sendEOF();
};
this.onDisconnect = function () {
puts("ponger: onDisconnect");
server.close();
};
}
function Pinger (socket) {
@ -35,27 +42,23 @@ function Pinger (socket) {
this.onReceive = function (data) {
stdout.print(".");
assertEquals("PONG", data);
setTimeout(function() {
count += 1;
if (count < N) {
socket.send("PING");
} else {
stdout.write("\n");
socket.send("QUIT\n");
socket.disconnect();
puts("sending FIN");
socket.sendEOF();
}
}, 10);
};
this.onDisconnect = function () {
puts("socket close.");
this.onEOF = function () {
puts("pinger: onEOF");
assertEquals(N, count);
server.close();
};
}
function onLoad() {
server = new TCPServer(Ponger);
var server = new TCPServer(Ponger);
server.listen(port);
var client = new TCPConnection(Pinger);

Loading…
Cancel
Save