diff --git a/deps/libeio/xthread.h b/deps/libeio/xthread.h index c39d39d47e..63ff20cbd4 100644 --- a/deps/libeio/xthread.h +++ b/deps/libeio/xthread.h @@ -127,10 +127,8 @@ thread_create (thread_t *tid, void *(*proc)(void *), void *arg) pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); - /* - pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 - ? sizeof (long) * 4096 : PTHREAD_STACK_MIN); - */ + pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 * 4 + ? sizeof (long) * 4096 * 4 : PTHREAD_STACK_MIN); #ifdef PTHREAD_SCOPE_PROCESS pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); #endif diff --git a/src/http.cc b/src/http.cc index de934143e4..131f39d71c 100644 --- a/src/http.cc +++ b/src/http.cc @@ -44,10 +44,10 @@ static Persistent unlock_str; #define INVALID_STATE_ERR 1 -class Server { +class HttpServer { public: - Server (Handle _js_server); - ~Server (); + HttpServer (Handle _js_server); + ~HttpServer (); int Start(struct addrinfo *servinfo); void Stop(); @@ -83,7 +83,7 @@ private: ebb_request_parser parser; list requests; list finished_requests; - friend class Server; + friend class HttpServer; }; class HttpRequest { @@ -461,7 +461,7 @@ on_connection (oi_server *_server, struct sockaddr *addr, socklen_t len) { HandleScope scope; - Server *server = static_cast (_server->data); + HttpServer *server = static_cast (_server->data); Handle callback_v = server->Callback(); @@ -560,11 +560,11 @@ Connection::Close ( ) static void server_destroy (Persistent _, void *data) { - Server *server = static_cast (data); + HttpServer *server = static_cast (data); delete server; } -Server::Server (Handle _js_server) +HttpServer::HttpServer (Handle _js_server) { oi_server_init(&server, 1024); server.on_connection = on_connection; @@ -576,7 +576,7 @@ Server::Server (Handle _js_server) js_server.MakeWeak (this, server_destroy); } -Server::~Server () +HttpServer::~HttpServer () { Stop(); js_server.Dispose(); @@ -584,7 +584,7 @@ Server::~Server () } int -Server::Start(struct addrinfo *servinfo) +HttpServer::Start(struct addrinfo *servinfo) { int r = oi_server_listen(&server, servinfo); if(r == 0) @@ -593,7 +593,7 @@ Server::Start(struct addrinfo *servinfo) } void -Server::Stop() +HttpServer::Stop() { oi_server_close (&server); oi_server_detach (&server); @@ -601,7 +601,7 @@ Server::Stop() /* This constructor takes 2 arguments: host, port. */ static Handle -newHTTPServer (const Arguments& args) +newHTTPHttpServer (const Arguments& args) { if (args.Length() < 3) return Undefined(); @@ -639,7 +639,7 @@ newHTTPServer (const Arguments& args) // // - Server *server = new Server(args.This()); + HttpServer *server = new HttpServer(args.This()); if(server == NULL) return Undefined(); // XXX raise error? @@ -655,7 +655,7 @@ NodeInit_http (Handle target) { HandleScope scope; - Local server_t = FunctionTemplate::New(newHTTPServer); + Local server_t = FunctionTemplate::New(newHTTPHttpServer); server_t->InstanceTemplate()->SetInternalFieldCount(1); server_t->Set("INVALID_STATE_ERR", Integer::New(INVALID_STATE_ERR)); diff --git a/src/net.cc b/src/net.cc index b0fefcfca9..97bfb3f73e 100644 --- a/src/net.cc +++ b/src/net.cc @@ -29,20 +29,38 @@ static const struct addrinfo tcp_hints = /* ai_next */ , NULL }; +class Server { +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); + static Server* Unwrap (Handle handle); + static void MakeWeak (Persistent _, void *data); + oi_server server_; + Persistent handle_; +}; class Socket { public: - Socket (Handle obj, double timeout); + Socket (Handle handle, double timeout); ~Socket (); - void SetEncoding (enum encoding); + 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); @@ -50,10 +68,6 @@ public: static void OnClose (oi_socket *s); static void OnTimeout (oi_socket *s); - char *host_; - char *port_; - -private: static int Resolve (eio_req *req); static int AfterResolve (eio_req *req); @@ -63,8 +77,72 @@ private: enum {UTF8, RAW} encoding_; oi_socket socket_; Persistent handle_; + + char *host_; + char *port_; }; +Server::Server (Handle handle, int backlog) +{ + oi_server_init(&server_, backlog); + server_.on_connection = Server::OnConnection; +// server_.on_error = Server::OnError; + server_.data = this; + + HandleScope scope; + handle_ = Persistent::New(handle); + handle_->SetInternalField(0, External::New(this)); + handle_.MakeWeak(this, Server::MakeWeak); +} + +Server::~Server () +{ + oi_server_close(&server_); + oi_server_detach(&server_); + handle_.Dispose(); + handle_.Clear(); // necessary? +} + +Handle +Server::New (const Arguments& args) +{ + ; +} + +Handle +Server::ListenTCP (const Arguments& args) +{ + ; +} + +Handle +Server::Close (const Arguments& args) +{ + ; +} + +oi_socket* +Server::OnConnection (oi_server *, struct sockaddr *remote_addr, socklen_t remote_addr_len) +{ + ; +} + +Server* +Server::Unwrap (Handle handle) +{ + HandleScope scope; + Handle field = Handle::Cast(handle->GetInternalField(0)); + Server* server = static_cast(field->Value()); + return server; +} + +void +Server::MakeWeak (Persistent _, void *data) +{ + Server *s = static_cast (data); + delete s; +} + Handle Socket::New(const Arguments& args) { @@ -105,6 +183,22 @@ Socket::New(const Arguments& args) return args.This(); } +void +Socket::SetEncoding (Handle encoding_value) +{ + if (encoding_value->IsString()) { + HandleScope scope; + Local encoding_string = encoding_value->ToString(); + char buf[5]; // need enough room for "utf8" or "raw" + encoding_string->WriteAscii(buf, 0, 4); + buf[4] = '\0'; + if(strcasecmp(buf, "utf8") == 0) + encoding_ = UTF8; + else + encoding_ = RAW; + } +} + Socket* Socket::Unwrap (Handle handle) { @@ -232,10 +326,10 @@ Socket::Socket(Handle handle, double timeout) HandleScope scope; handle_ = Persistent::New(handle); - handle_->SetInternalField (0, External::New(this)); - handle_.MakeWeak (this, Socket::MakeWeak); + handle_->SetInternalField(0, External::New(this)); + handle_.MakeWeak(this, Socket::MakeWeak); - encoding_ = UTF8; + encoding_ = UTF8; // default encoding. host_ = NULL; port_ = NULL; } @@ -250,6 +344,14 @@ Socket::~Socket () handle_.Clear(); // necessary? } +Handle +Socket::SetEncoding (const Arguments& args) +{ + Socket *socket = Socket::Unwrap(args.Holder()); + socket->SetEncoding(args[0]); + return Undefined(); +} + Handle Socket::Write (const Arguments& args) { @@ -410,8 +512,6 @@ Socket::OnTimeout (oi_socket *s) Socket *socket = static_cast (s->data); HandleScope scope; - printf("timeout\n"); - Handle ontimeout_value = socket->handle_->Get( String::NewSymbol("onTimeout") ); if (!ontimeout_value->IsFunction()) return; Handle ontimeout = Handle::Cast(ontimeout_value); @@ -438,6 +538,12 @@ NodeInit_net (Handle target) //NODE_SET_METHOD(socket_template->InstanceTemplate(), "connectUNIX", Socket::ConnectUNIX); 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); + + Local server_template = FunctionTemplate::New(Server::New); + server_template->InstanceTemplate()->SetInternalFieldCount(1); + target->Set(String::NewSymbol("Server"), server_template->GetFunction()); + NODE_SET_METHOD(server_template->InstanceTemplate(), "listenTCP", Server::ListenTCP); } diff --git a/wscript b/wscript index 906e193a0c..15e55b7a94 100644 --- a/wscript +++ b/wscript @@ -63,8 +63,8 @@ def configure(conf): # Configure default variant conf.setenv('default') - conf.env.append_value('CCFLAGS', ['-DNDEBUG', '-O2', '-g']) - conf.env.append_value('CXXFLAGS', ['-DNDEBUG', '-O2', '-g']) + conf.env.append_value('CCFLAGS', ['-DNDEBUG', '-O0', '-g']) + conf.env.append_value('CXXFLAGS', ['-DNDEBUG', '-O0', '-g']) conf.write_config_header("config.h") def build(bld):