Browse Source

various clean ups; HTTPConnection (js side) inherits from TCPConnection

v0.7.4-release
Ryan 16 years ago
parent
commit
38726e7272
  1. 47
      src/http.cc
  2. 17
      src/http.h
  3. 42
      src/net.cc
  4. 57
      src/net.h
  5. 14
      src/node.cc

47
src/http.cc

@ -23,7 +23,6 @@ using namespace v8;
using namespace node; using namespace node;
using namespace std; using namespace std;
// Native Helper Functions // Native Helper Functions
static Persistent<Function> _fill_field; static Persistent<Function> _fill_field;
@ -72,34 +71,40 @@ appendHeaderValue (Handle<Value> message, Handle<Value> d)
} }
Persistent<FunctionTemplate> HTTPConnection::constructor_template;
void void
HTTPConnection::Initialize (Handle<Object> target) HTTPConnection::Initialize (Handle<Object> target)
{ {
HandleScope scope; HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(HTTPConnection::v8New); Local<FunctionTemplate> t = FunctionTemplate::New(HTTPConnection::v8NewClient);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(1);
t->Inherit(Connection::constructor_template);
target->Set(String::NewSymbol("HTTPClient"), t->GetFunction()); target->Set(String::NewSymbol("HTTPClient"), t->GetFunction());
NODE_SET_METHOD(t->InstanceTemplate(), "connect", Connection::v8Connect); constructor_template = Persistent<FunctionTemplate>::New(t);
NODE_SET_METHOD(t->InstanceTemplate(), "close", Connection::v8Close);
NODE_SET_METHOD(t->InstanceTemplate(), "send", Connection::v8Send);
NODE_SET_METHOD(t->InstanceTemplate(), "sendEOF", Connection::v8SendEOF);
} }
Handle<Value> Handle<Value>
HTTPConnection::v8New (const Arguments& args) HTTPConnection::v8NewClient (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
if (args[0]->IsFunction() == false) if (args[0]->IsFunction() == false)
return ThrowException(String::New("Must pass a class as the first argument.")); return ThrowException(String::New("Must pass a class as the first argument."));
Local<Function> protocol_class = Local<Function>::Cast(args[0]); Local<Function> protocol_class = Local<Function>::Cast(args[0]);
// changeme the type should come from javascript
new HTTPConnection(args.This(), protocol_class, HTTP_RESPONSE); new HTTPConnection(args.This(), protocol_class, HTTP_RESPONSE);
return args.This();
}
Handle<Value>
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<Function> protocol_class = Local<Function>::Cast(args[0]);
new HTTPConnection(args.This(), protocol_class, HTTP_REQUEST);
return args.This(); return args.This();
} }
@ -109,10 +114,12 @@ HTTPConnection::OnReceive (const void *buf, size_t len)
http_parser_execute(&parser_, static_cast<const char*>(buf), len); http_parser_execute(&parser_, static_cast<const char*>(buf), len);
if (http_parser_has_error(&parser_)) { if (http_parser_has_error(&parser_)) {
// do something. // do something?
Close(); Close();
return; return;
} }
// XXX when do we close the connection?
} }
int int
@ -278,3 +285,19 @@ HTTPConnection::HTTPConnection (Handle<Object> handle, Handle<Function> protocol
parser_.data = this; parser_.data = this;
} }
HTTPServer::HTTPServer (Handle<Object> handle, Handle<Object> options)
:Acceptor(handle, options)
{
}
Handle<Value>
HTTPServer::v8New (const Arguments& args)
{
}
Connection*
HTTPServer::OnConnection (struct sockaddr *addr, socklen_t len)
{
}

17
src/http.h

@ -11,13 +11,15 @@ class HTTPConnection : public Connection {
public: public:
static void Initialize (v8::Handle<v8::Object> target); static void Initialize (v8::Handle<v8::Object> target);
protected:
static v8::Persistent<v8::FunctionTemplate> constructor_template;
static v8::Handle<v8::Value> v8NewClient (const v8::Arguments& args);
static v8::Handle<v8::Value> v8NewServer (const v8::Arguments& args);
HTTPConnection (v8::Handle<v8::Object> handle, HTTPConnection (v8::Handle<v8::Object> handle,
v8::Handle<v8::Function> protocol_class, v8::Handle<v8::Function> protocol_class,
enum http_parser_type type); enum http_parser_type type);
static v8::Handle<v8::Value> v8New (const v8::Arguments& args);
protected:
void OnReceive (const void *buf, size_t len); void OnReceive (const void *buf, size_t len);
static int on_message_begin (http_parser *parser); static int on_message_begin (http_parser *parser);
@ -34,5 +36,14 @@ protected:
http_parser parser_; http_parser parser_;
}; };
class HTTPServer : public Acceptor {
public:
HTTPServer (v8::Handle<v8::Object> handle, v8::Handle<v8::Object> options);
protected:
static v8::Handle<v8::Value> v8New (const v8::Arguments& args);
Connection* OnConnection (struct sockaddr *addr, socklen_t len);
};
} // namespace node } // namespace node
#endif #endif

42
src/net.cc

@ -41,23 +41,23 @@ static const struct addrinfo tcp_hints =
/* ai_next */ , NULL /* ai_next */ , NULL
}; };
Persistent<Function> tcp_connection_constructor; Persistent<FunctionTemplate> Connection::constructor_template;
void void
Connection::Initialize (v8::Handle<v8::Object> target) Connection::Initialize (v8::Handle<v8::Object> target)
{ {
HandleScope scope; HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(Connection::v8New); Local<FunctionTemplate> t = FunctionTemplate::New(v8New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(1);
target->Set(String::NewSymbol("TCPConnection"), t->GetFunction()); target->Set(String::NewSymbol("TCPConnection"), t->GetFunction());
tcp_connection_constructor = Persistent<Function>::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); constructor_template = Persistent<FunctionTemplate>::New(t);
NODE_SET_METHOD(t->InstanceTemplate(), "close", Connection::v8Close);
NODE_SET_METHOD(t->InstanceTemplate(), "send", Connection::v8Send);
NODE_SET_METHOD(t->InstanceTemplate(), "sendEOF", Connection::v8SendEOF);
} }
Connection::Connection (Handle<Object> handle, Handle<Function> protocol_class) Connection::Connection (Handle<Object> handle, Handle<Function> 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::OnTimeout, ON_TIMEOUT_SYMBOL)
DEFINE_SIMPLE_CALLBACK(Connection::OnEOF, ON_EOF_SYMBOL) DEFINE_SIMPLE_CALLBACK(Connection::OnEOF, ON_EOF_SYMBOL)
Persistent<FunctionTemplate> Acceptor::constructor_template;
void void
Acceptor::Initialize (Handle<Object> target) Acceptor::Initialize (Handle<Object> target)
{ {
HandleScope scope; HandleScope scope;
Local<FunctionTemplate> tcp_server_template = Local<FunctionTemplate> t = FunctionTemplate::New(v8New);
FunctionTemplate::New(Acceptor::v8New); t->InstanceTemplate()->SetInternalFieldCount(1);
tcp_server_template->InstanceTemplate()->SetInternalFieldCount(1); target->Set(String::NewSymbol("TCPServer"), t->GetFunction());
target->Set(String::NewSymbol("TCPServer"), tcp_server_template->GetFunction());
NODE_SET_METHOD(t->InstanceTemplate(), "listen", v8Listen);
NODE_SET_METHOD( tcp_server_template->InstanceTemplate() NODE_SET_METHOD(t->InstanceTemplate(), "close", v8Close);
, "listen"
, Acceptor::v8Listen constructor_template = Persistent<FunctionTemplate>::New(t);
);
NODE_SET_METHOD( tcp_server_template->InstanceTemplate()
, "close"
, Acceptor::v8Close
);
} }
Acceptor::Acceptor (Handle<Object> handle, Handle<Object> options) Acceptor::Acceptor (Handle<Object> handle, Handle<Object> options)
@ -378,7 +375,8 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
Local<Function> protocol_class = Local<Function>::Cast(protocol_class_v); Local<Function> protocol_class = Local<Function>::Cast(protocol_class_v);
Handle<Value> argv[] = { protocol_class }; Handle<Value> argv[] = { protocol_class };
Local<Object> connection_handle = tcp_connection_constructor->NewInstance(1, argv); Local<Object> connection_handle =
Connection::constructor_template->GetFunction()->NewInstance(1, argv);
Connection *connection = NODE_UNWRAP(Connection, connection_handle); Connection *connection = NODE_UNWRAP(Connection, connection_handle);
connection->SetAcceptor(handle_); connection->SetAcceptor(handle_);
@ -418,8 +416,6 @@ Acceptor::v8New (const Arguments& args)
new Acceptor(args.Holder(), options); new Acceptor(args.Holder(), options);
Acceptor *acceptor = NODE_UNWRAP(Acceptor, args.Holder());
return args.This(); return args.This();
} }

57
src/net.h

@ -13,34 +13,25 @@ class Connection : public ObjectWrap {
public: public:
static void Initialize (v8::Handle<v8::Object> target); static void Initialize (v8::Handle<v8::Object> target);
Connection (v8::Handle<v8::Object> handle, v8::Handle<v8::Function> 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<v8::Object> acceptor_handle);
protected: protected:
/* v8 interface */
static v8::Persistent<v8::FunctionTemplate> constructor_template;
static v8::Handle<v8::Value> v8New (const v8::Arguments& args); 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> v8Connect (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Send (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> v8SendEOF (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Close (const v8::Arguments& args); static v8::Handle<v8::Value> v8Close (const v8::Arguments& args);
Connection (v8::Handle<v8::Object> handle, v8::Handle<v8::Function> 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<v8::Object> acceptor_handle);
virtual void OnConnect (void); virtual void OnConnect (void);
virtual void OnReceive (const void *buf, size_t len); virtual void OnReceive (const void *buf, size_t len);
virtual void OnDrain (void); virtual void OnDrain (void);
@ -51,7 +42,10 @@ protected:
v8::Local<v8::Object> GetProtocol (void); v8::Local<v8::Object> GetProtocol (void);
//private: enum encoding encoding_;
private:
/* liboi callbacks */ /* liboi callbacks */
static void on_connect (oi_socket *s) { static void on_connect (oi_socket *s) {
Connection *connection = static_cast<Connection*> (s->data); Connection *connection = static_cast<Connection*> (s->data);
@ -88,8 +82,6 @@ protected:
static int Resolve (eio_req *req); static int Resolve (eio_req *req);
static int AfterResolve (eio_req *req); static int AfterResolve (eio_req *req);
enum encoding encoding_;
char *host_; char *host_;
char *port_; char *port_;
oi_socket socket_; oi_socket socket_;
@ -101,8 +93,14 @@ class Acceptor : public ObjectWrap {
public: public:
static void Initialize (v8::Handle<v8::Object> target); static void Initialize (v8::Handle<v8::Object> target);
protected:
static v8::Persistent<v8::FunctionTemplate> constructor_template;
static v8::Handle<v8::Value> v8New (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Listen (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Close (const v8::Arguments& args);
Acceptor (v8::Handle<v8::Object> handle, v8::Handle<v8::Object> options); Acceptor (v8::Handle<v8::Object> handle, v8::Handle<v8::Object> options);
~Acceptor () { Close(); } virtual ~Acceptor () { Close(); }
int Listen (struct addrinfo *address) { int Listen (struct addrinfo *address) {
int r = oi_server_listen (&server_, address); int r = oi_server_listen (&server_, address);
@ -115,13 +113,8 @@ public:
oi_server_close (&server_); oi_server_close (&server_);
} }
protected: virtual Connection* OnConnection (struct sockaddr *addr, socklen_t len);
Connection* OnConnection (struct sockaddr *addr, socklen_t len); virtual void OnError (struct oi_error error);
void OnError (struct oi_error error);
static v8::Handle<v8::Value> v8New (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Listen (const v8::Arguments& args);
static v8::Handle<v8::Value> v8Close (const v8::Arguments& args);
private: private:
static oi_socket* on_connection (oi_server *s, struct sockaddr *addr, socklen_t len) { static oi_socket* on_connection (oi_server *s, struct sockaddr *addr, socklen_t len) {

14
src/node.cc

@ -176,10 +176,10 @@ node::fatal_exception (TryCatch &try_catch)
::exit(1); ::exit(1);
} }
static ev_async thread_pool_watcher; static ev_async eio_watcher;
static void 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(); int r = eio_poll();
/* returns 0 if all requests were handled, -1 if not, or the value of EIO_FINISH if != 0 */ /* 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 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 void
node::eio_warmup (void) node::eio_warmup (void)
{ {
ev_async_start(EV_DEFAULT_UC_ &thread_pool_watcher); ev_async_start(EV_DEFAULT_UC_ &eio_watcher);
} }
int int
@ -209,8 +209,8 @@ main (int argc, char *argv[])
ev_default_loop(EVFLAG_AUTO); // initialize the default ev loop. ev_default_loop(EVFLAG_AUTO); // initialize the default ev loop.
// start eio thread pool // start eio thread pool
ev_async_init(&thread_pool_watcher, thread_pool_cb); ev_async_init(&eio_watcher, node_eio_cb);
eio_init(thread_pool_want_poll, NULL); eio_init(eio_want_poll, NULL);
V8::SetFlagsFromCommandLine(&argc, argv, true); V8::SetFlagsFromCommandLine(&argc, argv, true);

Loading…
Cancel
Save