mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 07:00:59 +01:00
Publicize Socket and Server for eventual cooperation with HttpServer.
Also changed Init_net() to Socket::Initialize() and Server::Initialize(). Seems more object oriented, but I'm unsure how this will play when I want load modules dynamically with dlopen(). I'll sit with it for a while and see how it feels.
This commit is contained in:
parent
0f5170339c
commit
cbd342a12d
98
src/net.cc
98
src/net.cc
@ -1,8 +1,4 @@
|
||||
#include "net.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <oi_socket.h>
|
||||
#include <oi_buf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
@ -14,8 +10,7 @@
|
||||
#include <netdb.h>
|
||||
|
||||
using namespace v8;
|
||||
|
||||
static Persistent<FunctionTemplate> 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<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);
|
||||
oi_server server_;
|
||||
};
|
||||
|
||||
class Socket : node::ObjectWrap {
|
||||
public:
|
||||
Socket (Handle<Object> handle, double timeout);
|
||||
~Socket ();
|
||||
|
||||
void SetEncoding (Handle<Value>);
|
||||
void SetTimeout (double);
|
||||
|
||||
static Handle<Value> New (const Arguments& args);
|
||||
static Handle<Value> Write (const Arguments& args);
|
||||
static Handle<Value> Close (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 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<Object> 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<Server*> (s->data);
|
||||
HandleScope scope;
|
||||
|
||||
Local<Object> 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<Value> 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<Object> handle, double timeout)
|
||||
port_ = NULL;
|
||||
}
|
||||
|
||||
Socket*
|
||||
Socket::NewConnection (double timeout)
|
||||
{
|
||||
HandleScope scope;
|
||||
|
||||
Local<Object> 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<Value> 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<Value> 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<Value> 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<Value> 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<Value> 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<Value> 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<Object> target)
|
||||
Socket::Initialize (Handle<Object> target)
|
||||
{
|
||||
HandleScope scope;
|
||||
|
||||
Local<FunctionTemplate> socket_template_local = FunctionTemplate::New(Socket::New);
|
||||
socket_template = Persistent<FunctionTemplate>::New(socket_template_local);
|
||||
Local<FunctionTemplate> template_local = FunctionTemplate::New(Socket::New);
|
||||
socket_template = Persistent<FunctionTemplate>::New(template_local);
|
||||
socket_template->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
target->Set(String::NewSymbol("Socket"), socket_template->GetFunction());
|
||||
|
||||
@ -572,6 +530,12 @@ node::Init_net (Handle<Object> 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<Object> target)
|
||||
{
|
||||
HandleScope scope;
|
||||
|
||||
Local<FunctionTemplate> server_template = FunctionTemplate::New(Server::New);
|
||||
server_template->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
|
57
src/net.h
57
src/net.h
@ -1,11 +1,66 @@
|
||||
#ifndef node_net_h
|
||||
#define node_net_h
|
||||
|
||||
#include "node.h"
|
||||
#include <v8.h>
|
||||
#include <oi_socket.h>
|
||||
|
||||
namespace node {
|
||||
|
||||
void Init_net (v8::Handle<v8::Object> target);
|
||||
class Server : ObjectWrap {
|
||||
public:
|
||||
Server (v8::Handle<v8::Object> handle, int backlog);
|
||||
~Server ();
|
||||
|
||||
static v8::Handle<v8::Value> New (const v8::Arguments& args);
|
||||
static v8::Handle<v8::Value> ListenTCP (const v8::Arguments& args);
|
||||
static v8::Handle<v8::Value> Close (const v8::Arguments& args);
|
||||
|
||||
static void Initialize (v8::Handle<v8::Object> target);
|
||||
private:
|
||||
static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t);
|
||||
oi_server server_;
|
||||
};
|
||||
|
||||
static v8::Persistent<v8::FunctionTemplate> socket_template;
|
||||
|
||||
class Socket : ObjectWrap {
|
||||
public:
|
||||
Socket (v8::Handle<v8::Object> handle, double timeout);
|
||||
~Socket ();
|
||||
// also a constructor
|
||||
static Socket* NewConnection (double timeout);
|
||||
|
||||
|
||||
void SetEncoding (v8::Handle<v8::Value>);
|
||||
void SetTimeout (double);
|
||||
|
||||
static v8::Handle<v8::Value> New (const v8::Arguments& args);
|
||||
static v8::Handle<v8::Value> Write (const v8::Arguments& args);
|
||||
static v8::Handle<v8::Value> Close (const v8::Arguments& args);
|
||||
static v8::Handle<v8::Value> ConnectTCP (const v8::Arguments& args);
|
||||
static v8::Handle<v8::Value> SetEncoding (const v8::Arguments& args);
|
||||
|
||||
static void Initialize (v8::Handle<v8::Object> 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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user