Browse Source

add framework for tcp server. change eio stacksize to 16kb.

v0.7.4-release
Ryan 16 years ago
parent
commit
822d7fa27e
  1. 6
      deps/libeio/xthread.h
  2. 26
      src/http.cc
  3. 128
      src/net.cc
  4. 4
      wscript

6
deps/libeio/xthread.h

@ -127,10 +127,8 @@ thread_create (thread_t *tid, void *(*proc)(void *), void *arg)
pthread_attr_init (&attr); pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
/* pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 * 4
pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < sizeof (long) * 4096 ? sizeof (long) * 4096 * 4 : PTHREAD_STACK_MIN);
? sizeof (long) * 4096 : PTHREAD_STACK_MIN);
*/
#ifdef PTHREAD_SCOPE_PROCESS #ifdef PTHREAD_SCOPE_PROCESS
pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
#endif #endif

26
src/http.cc

@ -44,10 +44,10 @@ static Persistent<String> unlock_str;
#define INVALID_STATE_ERR 1 #define INVALID_STATE_ERR 1
class Server { class HttpServer {
public: public:
Server (Handle<Object> _js_server); HttpServer (Handle<Object> _js_server);
~Server (); ~HttpServer ();
int Start(struct addrinfo *servinfo); int Start(struct addrinfo *servinfo);
void Stop(); void Stop();
@ -83,7 +83,7 @@ private:
ebb_request_parser parser; ebb_request_parser parser;
list<HttpRequest*> requests; list<HttpRequest*> requests;
list<HttpRequest*> finished_requests; list<HttpRequest*> finished_requests;
friend class Server; friend class HttpServer;
}; };
class HttpRequest { class HttpRequest {
@ -461,7 +461,7 @@ on_connection (oi_server *_server, struct sockaddr *addr, socklen_t len)
{ {
HandleScope scope; HandleScope scope;
Server *server = static_cast<Server*> (_server->data); HttpServer *server = static_cast<HttpServer*> (_server->data);
Handle<Value> callback_v = server->Callback(); Handle<Value> callback_v = server->Callback();
@ -560,11 +560,11 @@ Connection::Close ( )
static void static void
server_destroy (Persistent<Value> _, void *data) server_destroy (Persistent<Value> _, void *data)
{ {
Server *server = static_cast<Server *> (data); HttpServer *server = static_cast<HttpServer *> (data);
delete server; delete server;
} }
Server::Server (Handle<Object> _js_server) HttpServer::HttpServer (Handle<Object> _js_server)
{ {
oi_server_init(&server, 1024); oi_server_init(&server, 1024);
server.on_connection = on_connection; server.on_connection = on_connection;
@ -576,7 +576,7 @@ Server::Server (Handle<Object> _js_server)
js_server.MakeWeak (this, server_destroy); js_server.MakeWeak (this, server_destroy);
} }
Server::~Server () HttpServer::~HttpServer ()
{ {
Stop(); Stop();
js_server.Dispose(); js_server.Dispose();
@ -584,7 +584,7 @@ Server::~Server ()
} }
int int
Server::Start(struct addrinfo *servinfo) HttpServer::Start(struct addrinfo *servinfo)
{ {
int r = oi_server_listen(&server, servinfo); int r = oi_server_listen(&server, servinfo);
if(r == 0) if(r == 0)
@ -593,7 +593,7 @@ Server::Start(struct addrinfo *servinfo)
} }
void void
Server::Stop() HttpServer::Stop()
{ {
oi_server_close (&server); oi_server_close (&server);
oi_server_detach (&server); oi_server_detach (&server);
@ -601,7 +601,7 @@ Server::Stop()
/* This constructor takes 2 arguments: host, port. */ /* This constructor takes 2 arguments: host, port. */
static Handle<Value> static Handle<Value>
newHTTPServer (const Arguments& args) newHTTPHttpServer (const Arguments& args)
{ {
if (args.Length() < 3) if (args.Length() < 3)
return Undefined(); 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) if(server == NULL)
return Undefined(); // XXX raise error? return Undefined(); // XXX raise error?
@ -655,7 +655,7 @@ NodeInit_http (Handle<Object> target)
{ {
HandleScope scope; HandleScope scope;
Local<FunctionTemplate> server_t = FunctionTemplate::New(newHTTPServer); Local<FunctionTemplate> server_t = FunctionTemplate::New(newHTTPHttpServer);
server_t->InstanceTemplate()->SetInternalFieldCount(1); server_t->InstanceTemplate()->SetInternalFieldCount(1);
server_t->Set("INVALID_STATE_ERR", Integer::New(INVALID_STATE_ERR)); server_t->Set("INVALID_STATE_ERR", Integer::New(INVALID_STATE_ERR));

128
src/net.cc

@ -29,20 +29,38 @@ static const struct addrinfo tcp_hints =
/* ai_next */ , NULL /* ai_next */ , NULL
}; };
class Server {
public:
Server (Handle<Object> handle, int backlog);
~Server ();
static Handle<Value> New (const Arguments& args);
static Handle<Value> ListenTCP (const Arguments& args);
static Handle<Value> Close (const Arguments& args);
private:
static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t);
static Server* Unwrap (Handle<Object> handle);
static void MakeWeak (Persistent<Value> _, void *data);
oi_server server_;
Persistent<Object> handle_;
};
class Socket { class Socket {
public: public:
Socket (Handle<Object> obj, double timeout); Socket (Handle<Object> handle, double timeout);
~Socket (); ~Socket ();
void SetEncoding (enum encoding); void SetEncoding (Handle<Value>);
void SetTimeout (double); void SetTimeout (double);
static Handle<Value> New (const Arguments& args); static Handle<Value> New (const Arguments& args);
static Handle<Value> Write (const Arguments& args); static Handle<Value> Write (const Arguments& args);
static Handle<Value> Close (const Arguments& args); static Handle<Value> Close (const Arguments& args);
static Handle<Value> ConnectTCP (const Arguments& args); static Handle<Value> ConnectTCP (const Arguments& args);
static Handle<Value> SetEncoding (const Arguments& args);
private:
static void OnConnect (oi_socket *socket); static void OnConnect (oi_socket *socket);
static void OnRead (oi_socket *s, const void *buf, size_t count); static void OnRead (oi_socket *s, const void *buf, size_t count);
static void OnDrain (oi_socket *s); static void OnDrain (oi_socket *s);
@ -50,10 +68,6 @@ public:
static void OnClose (oi_socket *s); static void OnClose (oi_socket *s);
static void OnTimeout (oi_socket *s); static void OnTimeout (oi_socket *s);
char *host_;
char *port_;
private:
static int Resolve (eio_req *req); static int Resolve (eio_req *req);
static int AfterResolve (eio_req *req); static int AfterResolve (eio_req *req);
@ -63,8 +77,72 @@ private:
enum {UTF8, RAW} encoding_; enum {UTF8, RAW} encoding_;
oi_socket socket_; oi_socket socket_;
Persistent<Object> handle_; Persistent<Object> handle_;
char *host_;
char *port_;
}; };
Server::Server (Handle<Object> 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<Object>::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<Value>
Server::New (const Arguments& args)
{
;
}
Handle<Value>
Server::ListenTCP (const Arguments& args)
{
;
}
Handle<Value>
Server::Close (const Arguments& args)
{
;
}
oi_socket*
Server::OnConnection (oi_server *, struct sockaddr *remote_addr, socklen_t remote_addr_len)
{
;
}
Server*
Server::Unwrap (Handle<Object> handle)
{
HandleScope scope;
Handle<External> field = Handle<External>::Cast(handle->GetInternalField(0));
Server* server = static_cast<Server*>(field->Value());
return server;
}
void
Server::MakeWeak (Persistent<Value> _, void *data)
{
Server *s = static_cast<Server*> (data);
delete s;
}
Handle<Value> Handle<Value>
Socket::New(const Arguments& args) Socket::New(const Arguments& args)
{ {
@ -105,6 +183,22 @@ Socket::New(const Arguments& args)
return args.This(); return args.This();
} }
void
Socket::SetEncoding (Handle<Value> encoding_value)
{
if (encoding_value->IsString()) {
HandleScope scope;
Local<String> 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*
Socket::Unwrap (Handle<Object> handle) Socket::Unwrap (Handle<Object> handle)
{ {
@ -232,10 +326,10 @@ Socket::Socket(Handle<Object> handle, double timeout)
HandleScope scope; HandleScope scope;
handle_ = Persistent<Object>::New(handle); handle_ = Persistent<Object>::New(handle);
handle_->SetInternalField (0, External::New(this)); handle_->SetInternalField(0, External::New(this));
handle_.MakeWeak (this, Socket::MakeWeak); handle_.MakeWeak(this, Socket::MakeWeak);
encoding_ = UTF8; encoding_ = UTF8; // default encoding.
host_ = NULL; host_ = NULL;
port_ = NULL; port_ = NULL;
} }
@ -250,6 +344,14 @@ Socket::~Socket ()
handle_.Clear(); // necessary? handle_.Clear(); // necessary?
} }
Handle<Value>
Socket::SetEncoding (const Arguments& args)
{
Socket *socket = Socket::Unwrap(args.Holder());
socket->SetEncoding(args[0]);
return Undefined();
}
Handle<Value> Handle<Value>
Socket::Write (const Arguments& args) Socket::Write (const Arguments& args)
{ {
@ -410,8 +512,6 @@ Socket::OnTimeout (oi_socket *s)
Socket *socket = static_cast<Socket*> (s->data); Socket *socket = static_cast<Socket*> (s->data);
HandleScope scope; HandleScope scope;
printf("timeout\n");
Handle<Value> ontimeout_value = socket->handle_->Get( String::NewSymbol("onTimeout") ); Handle<Value> ontimeout_value = socket->handle_->Get( String::NewSymbol("onTimeout") );
if (!ontimeout_value->IsFunction()) return; if (!ontimeout_value->IsFunction()) return;
Handle<Function> ontimeout = Handle<Function>::Cast(ontimeout_value); Handle<Function> ontimeout = Handle<Function>::Cast(ontimeout_value);
@ -438,6 +538,12 @@ NodeInit_net (Handle<Object> target)
//NODE_SET_METHOD(socket_template->InstanceTemplate(), "connectUNIX", Socket::ConnectUNIX); //NODE_SET_METHOD(socket_template->InstanceTemplate(), "connectUNIX", Socket::ConnectUNIX);
NODE_SET_METHOD(socket_template->InstanceTemplate(), "write", Socket::Write); NODE_SET_METHOD(socket_template->InstanceTemplate(), "write", Socket::Write);
NODE_SET_METHOD(socket_template->InstanceTemplate(), "close", Socket::Close); NODE_SET_METHOD(socket_template->InstanceTemplate(), "close", Socket::Close);
NODE_SET_METHOD(socket_template->InstanceTemplate(), "setEncoding", Socket::SetEncoding);
Local<FunctionTemplate> 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);
} }

4
wscript

@ -63,8 +63,8 @@ def configure(conf):
# Configure default variant # Configure default variant
conf.setenv('default') conf.setenv('default')
conf.env.append_value('CCFLAGS', ['-DNDEBUG', '-O2', '-g']) conf.env.append_value('CCFLAGS', ['-DNDEBUG', '-O0', '-g'])
conf.env.append_value('CXXFLAGS', ['-DNDEBUG', '-O2', '-g']) conf.env.append_value('CXXFLAGS', ['-DNDEBUG', '-O0', '-g'])
conf.write_config_header("config.h") conf.write_config_header("config.h")
def build(bld): def build(bld):

Loading…
Cancel
Save