From 38726e7272662ae0e0c8215128129ea3fa61044a Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 4 May 2009 15:39:36 +0200 Subject: [PATCH] various clean ups; HTTPConnection (js side) inherits from TCPConnection --- src/http.cc | 47 ++++++++++++++++++++++++++++++++----------- src/http.h | 17 +++++++++++++--- src/net.cc | 40 +++++++++++++++++-------------------- src/net.h | 57 +++++++++++++++++++++++------------------------------ src/node.cc | 14 ++++++------- 5 files changed, 99 insertions(+), 76 deletions(-) diff --git a/src/http.cc b/src/http.cc index a87ee1ab88c..a63c9537d38 100644 --- a/src/http.cc +++ b/src/http.cc @@ -23,7 +23,6 @@ using namespace v8; using namespace node; using namespace std; - // Native Helper Functions static Persistent _fill_field; @@ -72,34 +71,40 @@ appendHeaderValue (Handle message, Handle d) } +Persistent HTTPConnection::constructor_template; + void HTTPConnection::Initialize (Handle target) { HandleScope scope; - Local t = FunctionTemplate::New(HTTPConnection::v8New); + Local t = FunctionTemplate::New(HTTPConnection::v8NewClient); t->InstanceTemplate()->SetInternalFieldCount(1); + t->Inherit(Connection::constructor_template); target->Set(String::NewSymbol("HTTPClient"), t->GetFunction()); - NODE_SET_METHOD(t->InstanceTemplate(), "connect", Connection::v8Connect); - NODE_SET_METHOD(t->InstanceTemplate(), "close", Connection::v8Close); - NODE_SET_METHOD(t->InstanceTemplate(), "send", Connection::v8Send); - NODE_SET_METHOD(t->InstanceTemplate(), "sendEOF", Connection::v8SendEOF); + constructor_template = Persistent::New(t); } Handle -HTTPConnection::v8New (const Arguments& args) +HTTPConnection::v8NewClient (const Arguments& args) { HandleScope scope; - if (args[0]->IsFunction() == false) return ThrowException(String::New("Must pass a class as the first argument.")); - Local protocol_class = Local::Cast(args[0]); - - // changeme the type should come from javascript new HTTPConnection(args.This(), protocol_class, HTTP_RESPONSE); + return args.This(); +} +Handle +HTTPConnection::v8NewServer (const Arguments& args) +{ + HandleScope scope; + if (args[0]->IsFunction() == false) + return ThrowException(String::New("Must pass a class as the first argument.")); + Local protocol_class = Local::Cast(args[0]); + new HTTPConnection(args.This(), protocol_class, HTTP_REQUEST); return args.This(); } @@ -109,10 +114,12 @@ HTTPConnection::OnReceive (const void *buf, size_t len) http_parser_execute(&parser_, static_cast(buf), len); if (http_parser_has_error(&parser_)) { - // do something. + // do something? Close(); return; } + + // XXX when do we close the connection? } int @@ -278,3 +285,19 @@ HTTPConnection::HTTPConnection (Handle handle, Handle protocol parser_.data = this; } +HTTPServer::HTTPServer (Handle handle, Handle options) + :Acceptor(handle, options) +{ +} + +Handle +HTTPServer::v8New (const Arguments& args) +{ +} + +Connection* +HTTPServer::OnConnection (struct sockaddr *addr, socklen_t len) +{ +} + + diff --git a/src/http.h b/src/http.h index cb17fb75f1c..d62f169c764 100644 --- a/src/http.h +++ b/src/http.h @@ -11,13 +11,15 @@ class HTTPConnection : public Connection { public: static void Initialize (v8::Handle target); +protected: + static v8::Persistent constructor_template; + static v8::Handle v8NewClient (const v8::Arguments& args); + static v8::Handle v8NewServer (const v8::Arguments& args); + HTTPConnection (v8::Handle handle, v8::Handle protocol_class, enum http_parser_type type); - static v8::Handle v8New (const v8::Arguments& args); - -protected: void OnReceive (const void *buf, size_t len); static int on_message_begin (http_parser *parser); @@ -34,5 +36,14 @@ protected: http_parser parser_; }; +class HTTPServer : public Acceptor { +public: + HTTPServer (v8::Handle handle, v8::Handle options); + +protected: + static v8::Handle v8New (const v8::Arguments& args); + Connection* OnConnection (struct sockaddr *addr, socklen_t len); +}; + } // namespace node #endif diff --git a/src/net.cc b/src/net.cc index 2e3b1ecf7c2..457f7996621 100644 --- a/src/net.cc +++ b/src/net.cc @@ -41,23 +41,23 @@ static const struct addrinfo tcp_hints = /* ai_next */ , NULL }; -Persistent tcp_connection_constructor; +Persistent Connection::constructor_template; void Connection::Initialize (v8::Handle target) { HandleScope scope; - Local t = FunctionTemplate::New(Connection::v8New); + Local t = FunctionTemplate::New(v8New); t->InstanceTemplate()->SetInternalFieldCount(1); target->Set(String::NewSymbol("TCPConnection"), t->GetFunction()); - tcp_connection_constructor = Persistent::New(t->GetFunction()); + NODE_SET_METHOD(t->InstanceTemplate(), "connect", v8Connect); + NODE_SET_METHOD(t->InstanceTemplate(), "close", v8Close); + NODE_SET_METHOD(t->InstanceTemplate(), "send", v8Send); + NODE_SET_METHOD(t->InstanceTemplate(), "sendEOF", v8SendEOF); - NODE_SET_METHOD(t->InstanceTemplate(), "connect", Connection::v8Connect); - NODE_SET_METHOD(t->InstanceTemplate(), "close", Connection::v8Close); - NODE_SET_METHOD(t->InstanceTemplate(), "send", Connection::v8Send); - NODE_SET_METHOD(t->InstanceTemplate(), "sendEOF", Connection::v8SendEOF); + constructor_template = Persistent::New(t); } Connection::Connection (Handle handle, Handle protocol_class) @@ -323,24 +323,21 @@ DEFINE_SIMPLE_CALLBACK(Connection::OnDisconnect, ON_DISCONNECT_SYMBOL) DEFINE_SIMPLE_CALLBACK(Connection::OnTimeout, ON_TIMEOUT_SYMBOL) DEFINE_SIMPLE_CALLBACK(Connection::OnEOF, ON_EOF_SYMBOL) +Persistent Acceptor::constructor_template; + void Acceptor::Initialize (Handle target) { HandleScope scope; - Local tcp_server_template = - FunctionTemplate::New(Acceptor::v8New); - tcp_server_template->InstanceTemplate()->SetInternalFieldCount(1); - target->Set(String::NewSymbol("TCPServer"), tcp_server_template->GetFunction()); + Local t = FunctionTemplate::New(v8New); + t->InstanceTemplate()->SetInternalFieldCount(1); + target->Set(String::NewSymbol("TCPServer"), t->GetFunction()); - NODE_SET_METHOD( tcp_server_template->InstanceTemplate() - , "listen" - , Acceptor::v8Listen - ); - NODE_SET_METHOD( tcp_server_template->InstanceTemplate() - , "close" - , Acceptor::v8Close - ); + NODE_SET_METHOD(t->InstanceTemplate(), "listen", v8Listen); + NODE_SET_METHOD(t->InstanceTemplate(), "close", v8Close); + + constructor_template = Persistent::New(t); } Acceptor::Acceptor (Handle handle, Handle options) @@ -378,7 +375,8 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len) Local protocol_class = Local::Cast(protocol_class_v); Handle argv[] = { protocol_class }; - Local connection_handle = tcp_connection_constructor->NewInstance(1, argv); + Local connection_handle = + Connection::constructor_template->GetFunction()->NewInstance(1, argv); Connection *connection = NODE_UNWRAP(Connection, connection_handle); connection->SetAcceptor(handle_); @@ -418,8 +416,6 @@ Acceptor::v8New (const Arguments& args) new Acceptor(args.Holder(), options); - Acceptor *acceptor = NODE_UNWRAP(Acceptor, args.Holder()); - return args.This(); } diff --git a/src/net.h b/src/net.h index 5384d855e5d..115cef6b8f9 100644 --- a/src/net.h +++ b/src/net.h @@ -13,34 +13,25 @@ class Connection : public ObjectWrap { public: static void Initialize (v8::Handle target); - Connection (v8::Handle handle, v8::Handle protocol_class); - virtual ~Connection () { Close(); } - - int Connect (struct addrinfo *address) { - return oi_socket_connect (&socket_, address); - } - - void Send (oi_buf *buf) { - oi_socket_write (&socket_, buf); - } - - void SendEOF (void) { - oi_socket_write_eof (&socket_); - } - - void Close (void) { - oi_socket_close (&socket_); - } - - void SetAcceptor (v8::Handle acceptor_handle); - protected: + /* v8 interface */ + static v8::Persistent constructor_template; static v8::Handle v8New (const v8::Arguments& args); static v8::Handle v8Connect (const v8::Arguments& args); static v8::Handle v8Send (const v8::Arguments& args); static v8::Handle v8SendEOF (const v8::Arguments& args); static v8::Handle v8Close (const v8::Arguments& args); + Connection (v8::Handle handle, v8::Handle protocol_class); + virtual ~Connection () { Close(); } + + int Connect (struct addrinfo *address) { return oi_socket_connect (&socket_, address); } + void Send (oi_buf *buf) { oi_socket_write (&socket_, buf); } + void SendEOF (void) { oi_socket_write_eof (&socket_); } + void Close (void) { oi_socket_close (&socket_); } + + void SetAcceptor (v8::Handle acceptor_handle); + virtual void OnConnect (void); virtual void OnReceive (const void *buf, size_t len); virtual void OnDrain (void); @@ -51,7 +42,10 @@ protected: v8::Local GetProtocol (void); -//private: + enum encoding encoding_; + +private: + /* liboi callbacks */ static void on_connect (oi_socket *s) { Connection *connection = static_cast (s->data); @@ -88,8 +82,6 @@ protected: static int Resolve (eio_req *req); static int AfterResolve (eio_req *req); - - enum encoding encoding_; char *host_; char *port_; oi_socket socket_; @@ -101,8 +93,14 @@ class Acceptor : public ObjectWrap { public: static void Initialize (v8::Handle target); +protected: + static v8::Persistent constructor_template; + static v8::Handle v8New (const v8::Arguments& args); + static v8::Handle v8Listen (const v8::Arguments& args); + static v8::Handle v8Close (const v8::Arguments& args); + Acceptor (v8::Handle handle, v8::Handle options); - ~Acceptor () { Close(); } + virtual ~Acceptor () { Close(); } int Listen (struct addrinfo *address) { int r = oi_server_listen (&server_, address); @@ -115,13 +113,8 @@ public: oi_server_close (&server_); } -protected: - Connection* OnConnection (struct sockaddr *addr, socklen_t len); - void OnError (struct oi_error error); - - static v8::Handle v8New (const v8::Arguments& args); - static v8::Handle v8Listen (const v8::Arguments& args); - static v8::Handle v8Close (const v8::Arguments& args); + virtual Connection* OnConnection (struct sockaddr *addr, socklen_t len); + virtual void OnError (struct oi_error error); private: static oi_socket* on_connection (oi_server *s, struct sockaddr *addr, socklen_t len) { diff --git a/src/node.cc b/src/node.cc index 527cafcd3aa..7a556fac3c7 100644 --- a/src/node.cc +++ b/src/node.cc @@ -176,10 +176,10 @@ node::fatal_exception (TryCatch &try_catch) ::exit(1); } -static ev_async thread_pool_watcher; +static ev_async eio_watcher; static void -thread_pool_cb (EV_P_ ev_async *w, int revents) +node_eio_cb (EV_P_ ev_async *w, int revents) { int r = eio_poll(); /* returns 0 if all requests were handled, -1 if not, or the value of EIO_FINISH if != 0 */ @@ -192,15 +192,15 @@ thread_pool_cb (EV_P_ ev_async *w, int revents) } static void -thread_pool_want_poll (void) +eio_want_poll (void) { - ev_async_send(EV_DEFAULT_UC_ &thread_pool_watcher); + ev_async_send(EV_DEFAULT_UC_ &eio_watcher); } void node::eio_warmup (void) { - ev_async_start(EV_DEFAULT_UC_ &thread_pool_watcher); + ev_async_start(EV_DEFAULT_UC_ &eio_watcher); } int @@ -209,8 +209,8 @@ main (int argc, char *argv[]) ev_default_loop(EVFLAG_AUTO); // initialize the default ev loop. // start eio thread pool - ev_async_init(&thread_pool_watcher, thread_pool_cb); - eio_init(thread_pool_want_poll, NULL); + ev_async_init(&eio_watcher, node_eio_cb); + eio_init(eio_want_poll, NULL); V8::SetFlagsFromCommandLine(&argc, argv, true);