mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 07:00:59 +01:00
various clean ups; HTTPConnection (js side) inherits from TCPConnection
This commit is contained in:
parent
09c2ae5c3e
commit
38726e7272
47
src/http.cc
47
src/http.cc
@ -23,7 +23,6 @@ using namespace v8;
|
||||
using namespace node;
|
||||
using namespace std;
|
||||
|
||||
|
||||
// Native Helper Functions
|
||||
|
||||
static Persistent<Function> _fill_field;
|
||||
@ -72,34 +71,40 @@ appendHeaderValue (Handle<Value> message, Handle<Value> d)
|
||||
}
|
||||
|
||||
|
||||
Persistent<FunctionTemplate> HTTPConnection::constructor_template;
|
||||
|
||||
void
|
||||
HTTPConnection::Initialize (Handle<Object> target)
|
||||
{
|
||||
HandleScope scope;
|
||||
|
||||
Local<FunctionTemplate> t = FunctionTemplate::New(HTTPConnection::v8New);
|
||||
Local<FunctionTemplate> 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<FunctionTemplate>::New(t);
|
||||
}
|
||||
|
||||
Handle<Value>
|
||||
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<Function> protocol_class = Local<Function>::Cast(args[0]);
|
||||
|
||||
// changeme the type should come from javascript
|
||||
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();
|
||||
}
|
||||
|
||||
@ -109,10 +114,12 @@ HTTPConnection::OnReceive (const void *buf, size_t len)
|
||||
http_parser_execute(&parser_, static_cast<const char*>(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<Object> handle, Handle<Function> protocol
|
||||
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
17
src/http.h
@ -11,13 +11,15 @@ class HTTPConnection : public Connection {
|
||||
public:
|
||||
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,
|
||||
v8::Handle<v8::Function> protocol_class,
|
||||
enum http_parser_type type);
|
||||
|
||||
static v8::Handle<v8::Value> 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<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
|
||||
#endif
|
||||
|
40
src/net.cc
40
src/net.cc
@ -41,23 +41,23 @@ static const struct addrinfo tcp_hints =
|
||||
/* ai_next */ , NULL
|
||||
};
|
||||
|
||||
Persistent<Function> tcp_connection_constructor;
|
||||
Persistent<FunctionTemplate> Connection::constructor_template;
|
||||
|
||||
void
|
||||
Connection::Initialize (v8::Handle<v8::Object> target)
|
||||
{
|
||||
HandleScope scope;
|
||||
|
||||
Local<FunctionTemplate> t = FunctionTemplate::New(Connection::v8New);
|
||||
Local<FunctionTemplate> t = FunctionTemplate::New(v8New);
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
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);
|
||||
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<FunctionTemplate>::New(t);
|
||||
}
|
||||
|
||||
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::OnEOF, ON_EOF_SYMBOL)
|
||||
|
||||
Persistent<FunctionTemplate> Acceptor::constructor_template;
|
||||
|
||||
void
|
||||
Acceptor::Initialize (Handle<Object> target)
|
||||
{
|
||||
HandleScope scope;
|
||||
|
||||
Local<FunctionTemplate> tcp_server_template =
|
||||
FunctionTemplate::New(Acceptor::v8New);
|
||||
tcp_server_template->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
target->Set(String::NewSymbol("TCPServer"), tcp_server_template->GetFunction());
|
||||
Local<FunctionTemplate> 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<FunctionTemplate>::New(t);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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->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();
|
||||
}
|
||||
|
||||
|
57
src/net.h
57
src/net.h
@ -13,34 +13,25 @@ class Connection : public ObjectWrap {
|
||||
public:
|
||||
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:
|
||||
/* v8 interface */
|
||||
static v8::Persistent<v8::FunctionTemplate> constructor_template;
|
||||
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> v8Send (const v8::Arguments& args);
|
||||
static v8::Handle<v8::Value> v8SendEOF (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 OnReceive (const void *buf, size_t len);
|
||||
virtual void OnDrain (void);
|
||||
@ -51,7 +42,10 @@ protected:
|
||||
|
||||
v8::Local<v8::Object> GetProtocol (void);
|
||||
|
||||
//private:
|
||||
enum encoding encoding_;
|
||||
|
||||
private:
|
||||
|
||||
/* liboi callbacks */
|
||||
static void on_connect (oi_socket *s) {
|
||||
Connection *connection = static_cast<Connection*> (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<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 () { 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<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);
|
||||
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) {
|
||||
|
14
src/node.cc
14
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user