diff --git a/src/net.cc b/src/net.cc index e7a2defaa5..0b42ffe72b 100644 --- a/src/net.cc +++ b/src/net.cc @@ -1,8 +1,4 @@ #include "net.h" -#include "node.h" - -#include -#include #include #include @@ -14,8 +10,7 @@ #include using namespace v8; - -static Persistent socket_template; +using namespace node; #define ON_CONNECT_SYMBOL String::NewSymbol("onConnect") #define ON_CONNECTION_SYMBOL String::NewSymbol("onConnection") @@ -32,53 +27,6 @@ static const struct addrinfo tcp_hints = /* ai_next */ , NULL }; -class Server : node::ObjectWrap { -public: - Server (Handle handle, int backlog); - ~Server (); - - static Handle New (const Arguments& args); - static Handle ListenTCP (const Arguments& args); - static Handle Close (const Arguments& args); - -private: - static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t); - oi_server server_; -}; - -class Socket : node::ObjectWrap { -public: - Socket (Handle handle, double timeout); - ~Socket (); - - void SetEncoding (Handle); - void SetTimeout (double); - - static Handle New (const Arguments& args); - static Handle Write (const Arguments& args); - static Handle Close (const Arguments& args); - static Handle ConnectTCP (const Arguments& args); - static Handle SetEncoding (const Arguments& args); - -private: - static void OnConnect (oi_socket *socket); - static void OnRead (oi_socket *s, const void *buf, size_t count); - static void OnDrain (oi_socket *s); - static void OnError (oi_socket *s, oi_error e); - static void OnClose (oi_socket *s); - static void OnTimeout (oi_socket *s); - - static int Resolve (eio_req *req); - static int AfterResolve (eio_req *req); - - enum {UTF8, RAW} encoding_; - oi_socket socket_; - - char *host_; - char *port_; - - friend class Server; -}; Server::Server (Handle handle, int backlog) : ObjectWrap(handle) @@ -169,9 +117,7 @@ Server::OnConnection (oi_server *s, struct sockaddr *remote_addr, socklen_t remo Server *server = static_cast (s->data); HandleScope scope; - Local socket_handle = socket_template->GetFunction()->NewInstance(); - Socket *socket = new Socket(socket_handle, 60.0); - socket->handle_->Delete(String::NewSymbol("connectTCP")); + Socket *socket = Socket::NewConnection(60.0); Local callback_v = server->handle_->Get(ON_CONNECTION_SYMBOL); if (!callback_v->IsFunction()) @@ -270,7 +216,7 @@ Socket::ConnectTCP (const Arguments& args) * In the future I will move to a system using adns or udns: * http://lists.schmorp.de/pipermail/libev/2009q1/000632.html */ - node::eio_warmup(); + eio_warmup(); eio_req *req = eio_custom (Socket::Resolve, EIO_PRI_DEFAULT, Socket::AfterResolve, socket); return Undefined(); @@ -328,7 +274,7 @@ Socket::AfterResolve (eio_req *req) onconnect->Call(socket->handle_, argc, argv); if(try_catch.HasCaught()) - node::fatal_exception(try_catch); + fatal_exception(try_catch); return 0; } @@ -360,6 +306,18 @@ Socket::Socket(Handle handle, double timeout) port_ = NULL; } +Socket* +Socket::NewConnection (double timeout) +{ + HandleScope scope; + + Local socket_handle = socket_template->GetFunction()->NewInstance(); + Socket *socket = new Socket(socket_handle, 60.0); + socket->handle_->Delete(String::NewSymbol("connectTCP")); + + return socket; +} + Socket::~Socket () { oi_socket_close(&socket_); @@ -437,7 +395,7 @@ Socket::OnConnect (oi_socket *s) Handle r = on_connect->Call(socket->handle_, argc, argv); if(try_catch.HasCaught()) - node::fatal_exception(try_catch); + fatal_exception(try_catch); } void @@ -476,7 +434,7 @@ Socket::OnRead (oi_socket *s, const void *buf, size_t count) Handle r = onread->Call(socket->handle_, argc, argv); if(try_catch.HasCaught()) - node::fatal_exception(try_catch); + fatal_exception(try_catch); } void @@ -497,7 +455,7 @@ Socket::OnClose (oi_socket *s) Handle r = onclose->Call(socket->handle_, 0, NULL); if(try_catch.HasCaught()) - node::fatal_exception(try_catch); + fatal_exception(try_catch); delete socket; } @@ -517,7 +475,7 @@ Socket::OnDrain (oi_socket *s) Handle r = ondrain->Call(socket->handle_, 0, NULL); if(try_catch.HasCaught()) - node::fatal_exception(try_catch); + fatal_exception(try_catch); } @@ -536,7 +494,7 @@ Socket::OnError (oi_socket *s, oi_error e) Handle r = onerror->Call(socket->handle_, 0, NULL); if(try_catch.HasCaught()) - node::fatal_exception(try_catch); + fatal_exception(try_catch); } void @@ -554,16 +512,16 @@ Socket::OnTimeout (oi_socket *s) Handle r = ontimeout->Call(socket->handle_, 0, NULL); if(try_catch.HasCaught()) - node::fatal_exception(try_catch); + fatal_exception(try_catch); } void -node::Init_net (Handle target) +Socket::Initialize (Handle target) { HandleScope scope; - Local socket_template_local = FunctionTemplate::New(Socket::New); - socket_template = Persistent::New(socket_template_local); + Local template_local = FunctionTemplate::New(Socket::New); + socket_template = Persistent::New(template_local); socket_template->InstanceTemplate()->SetInternalFieldCount(1); target->Set(String::NewSymbol("Socket"), socket_template->GetFunction()); @@ -572,6 +530,12 @@ node::Init_net (Handle target) NODE_SET_METHOD(socket_template->InstanceTemplate(), "write", Socket::Write); NODE_SET_METHOD(socket_template->InstanceTemplate(), "close", Socket::Close); NODE_SET_METHOD(socket_template->InstanceTemplate(), "setEncoding", Socket::SetEncoding); +} + +void +Server::Initialize (Handle target) +{ + HandleScope scope; Local server_template = FunctionTemplate::New(Server::New); server_template->InstanceTemplate()->SetInternalFieldCount(1); diff --git a/src/net.h b/src/net.h index 9e6662f9b4..f335b74a16 100644 --- a/src/net.h +++ b/src/net.h @@ -1,11 +1,66 @@ #ifndef node_net_h #define node_net_h +#include "node.h" #include +#include namespace node { -void Init_net (v8::Handle target); +class Server : ObjectWrap { +public: + Server (v8::Handle handle, int backlog); + ~Server (); + + static v8::Handle New (const v8::Arguments& args); + static v8::Handle ListenTCP (const v8::Arguments& args); + static v8::Handle Close (const v8::Arguments& args); + + static void Initialize (v8::Handle target); +private: + static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t); + oi_server server_; +}; + +static v8::Persistent socket_template; + +class Socket : ObjectWrap { +public: + Socket (v8::Handle handle, double timeout); + ~Socket (); + // also a constructor + static Socket* NewConnection (double timeout); + + + void SetEncoding (v8::Handle); + void SetTimeout (double); + + static v8::Handle New (const v8::Arguments& args); + static v8::Handle Write (const v8::Arguments& args); + static v8::Handle Close (const v8::Arguments& args); + static v8::Handle ConnectTCP (const v8::Arguments& args); + static v8::Handle SetEncoding (const v8::Arguments& args); + + static void Initialize (v8::Handle target); +private: + static void OnConnect (oi_socket *socket); + static void OnRead (oi_socket *s, const void *buf, size_t count); + static void OnDrain (oi_socket *s); + static void OnError (oi_socket *s, oi_error e); + static void OnClose (oi_socket *s); + static void OnTimeout (oi_socket *s); + + static int Resolve (eio_req *req); + static int AfterResolve (eio_req *req); + + enum encoding encoding_; + oi_socket socket_; + + char *host_; + char *port_; + + friend class Server; +}; } // namespace node #endif diff --git a/src/node.cc b/src/node.cc index 92f3c0370a..9f425394bd 100644 --- a/src/node.cc +++ b/src/node.cc @@ -246,7 +246,8 @@ main (int argc, char *argv[]) // BUILT-IN MODULES - node::Init_net(g); + Socket::Initialize(g); + Server::Initialize(g); node::Init_timer(g); node::Init_file(g); node::Init_http(g);