@ -132,11 +132,9 @@ Connection::~Connection ()
}
}
void
void
Connection : : SetAccepto r ( Handle < Object > accepto r_handle)
Connection : : SetServe r ( Handle < Object > serve r_handle)
{
{
HandleScope scope ;
HandleScope scope ;
handle_ - > Set ( SERVER_SYMBOL , acceptor_handle ) ;
Attach ( ) ;
}
}
Handle < Value >
Handle < Value >
@ -480,10 +478,10 @@ DEFINE_SIMPLE_CALLBACK(Connection::OnDrain, "Drain")
DEFINE_SIMPLE_CALLBACK ( Connection : : OnTimeout , " Timeout " )
DEFINE_SIMPLE_CALLBACK ( Connection : : OnTimeout , " Timeout " )
DEFINE_SIMPLE_CALLBACK ( Connection : : OnEOF , " EOF " )
DEFINE_SIMPLE_CALLBACK ( Connection : : OnEOF , " EOF " )
Persistent < FunctionTemplate > Accepto r: : constructor_template ;
Persistent < FunctionTemplate > Serve r: : constructor_template ;
void
void
Accepto r: : Initialize ( Handle < Object > target )
Serve r: : Initialize ( Handle < Object > target )
{
{
HandleScope scope ;
HandleScope scope ;
@ -498,12 +496,13 @@ Acceptor::Initialize (Handle<Object> target)
target - > Set ( String : : NewSymbol ( " Server " ) , constructor_template - > GetFunction ( ) ) ;
target - > Set ( String : : NewSymbol ( " Server " ) , constructor_template - > GetFunction ( ) ) ;
}
}
Acceptor : : Accepto r ( Handle < Object > handle , Handle < Function > connection_handler , Handle < Object > options )
Server : : Serve r ( Handle < Object > handle )
: EventEmitter ( handle )
: EventEmitter ( handle )
{
{
HandleScope scope ;
HandleScope scope ;
#if 0
#if 0
// TODO SetOptions
handle_ - > SetHiddenValue ( CONNECTION_HANDLER_SYMBOL , connection_handler ) ;
handle_ - > SetHiddenValue ( CONNECTION_HANDLER_SYMBOL , connection_handler ) ;
int backlog = 1024 ; // default value
int backlog = 1024 ; // default value
@ -514,12 +513,12 @@ Acceptor::Acceptor (Handle<Object> handle, Handle<Function> connection_handler,
# endif
# endif
oi_server_init ( & server_ , 1024 ) ;
oi_server_init ( & server_ , 1024 ) ;
server_ . on_connection = Accepto r: : on_connection ;
server_ . on_connection = Serve r: : on_connection ;
server_ . data = this ;
server_ . data = this ;
}
}
static void
static Local < String >
SetRemoteAddress ( Local < Object > connection_handle , struct sockaddr * addr )
GetAddressString ( struct sockaddr * addr )
{
{
HandleScope scope ;
HandleScope scope ;
char ip [ INET6_ADDRSTRLEN ] ;
char ip [ INET6_ADDRSTRLEN ] ;
@ -537,24 +536,24 @@ SetRemoteAddress (Local<Object> connection_handle, struct sockaddr *addr)
} else assert ( 0 & & " received a bad sa_family " ) ;
} else assert ( 0 & & " received a bad sa_family " ) ;
connection_handle - > Set ( REMOTE_ADDRESS_SYMBOL , remote_address ) ;
return scope . Close ( remote_address ) ;
}
}
Handle < FunctionTemplate >
Handle < FunctionTemplate >
Accepto r: : GetConnectionTemplate ( void )
Serve r: : GetConnectionTemplate ( void )
{
{
return Connection : : constructor_template ;
return Connection : : constructor_template ;
}
}
Connection *
Connection *
Accepto r: : UnwrapConnection ( Local < Object > connection )
Serve r: : UnwrapConnection ( Local < Object > connection )
{
{
HandleScope scope ;
HandleScope scope ;
return NODE_UNWRAP ( Connection , connection ) ;
return NODE_UNWRAP ( Connection , connection ) ;
}
}
Connection *
Connection *
Accepto r: : OnConnection ( struct sockaddr * addr , socklen_t len )
Serve r: : OnConnection ( struct sockaddr * addr , socklen_t len )
{
{
HandleScope scope ;
HandleScope scope ;
@ -568,12 +567,14 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
return NULL ;
return NULL ;
}
}
SetRemoteAddress ( js_connection , addr ) ;
Local < String > remote_address = GetAddressString ( addr ) ;
js_connection - > Set ( REMOTE_ADDRESS_SYMBOL , remote_address ) ;
js_connection - > Set ( SERVER_SYMBOL , handle_ ) ;
Connection * connection = UnwrapConnection ( js_connection ) ;
Connection * connection = UnwrapConnection ( js_connection ) ;
if ( ! connection ) return NULL ;
if ( ! connection ) return NULL ;
connection - > SetAcceptor ( handle_ ) ;
connection - > Attach ( ) ;
Handle < Value > argv [ 1 ] = { js_connection } ;
Handle < Value > argv [ 1 ] = { js_connection } ;
@ -582,34 +583,25 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
return connection ;
return connection ;
}
}
// TODO Server->SetOptions
// TODO Server -> Server rename
Handle < Value >
Handle < Value >
Acceptor : : New ( const Arguments & args )
Serve r: : New ( const Arguments & args )
{
{
HandleScope scope ;
HandleScope scope ;
if ( args . Length ( ) < 1 | | args [ 0 ] - > IsFunction ( ) = = false )
Server * a = new Server ( args . This ( ) ) ;
return ThrowException ( String : : New ( " Must at give connection handler as the first argument " ) ) ;
Local < Function > connection_handler = Local < Function > : : Cast ( args [ 0 ] ) ;
Local < Object > options ;
if ( args . Length ( ) > 1 & & args [ 1 ] - > IsObject ( ) ) {
options = args [ 1 ] - > ToObject ( ) ;
} else {
options = Object : : New ( ) ;
}
Acceptor * a = new Acceptor ( args . This ( ) , connection_handler , options ) ;
ObjectWrap : : InformV8ofAllocation ( a ) ;
ObjectWrap : : InformV8ofAllocation ( a ) ;
return args . This ( ) ;
return args . This ( ) ;
}
}
Handle < Value >
Handle < Value >
Accepto r: : Listen ( const Arguments & args )
Server : : Listen ( const Arguments & args )
{
{
Acceptor * accepto r = NODE_UNWRAP ( Accepto r, args . Holder ( ) ) ;
Server * server = NODE_UNWRAP ( Server , args . Holder ( ) ) ;
if ( ! accepto r) return Handle < Value > ( ) ;
if ( ! server ) return Handle < Value > ( ) ;
if ( args . Length ( ) = = 0 )
if ( args . Length ( ) = = 0 )
return ThrowException ( String : : New ( " Must give at least a port as argument. " ) ) ;
return ThrowException ( String : : New ( " Must give at least a port as argument. " ) ) ;
@ -635,7 +627,7 @@ Acceptor::Listen (const Arguments& args)
address = AddressDefaultToIPv4 ( address_list ) ;
address = AddressDefaultToIPv4 ( address_list ) ;
accepto r- > Listen ( address ) ;
serve r- > Listen ( address ) ;
if ( address_list ) freeaddrinfo ( address_list ) ;
if ( address_list ) freeaddrinfo ( address_list ) ;
@ -643,11 +635,11 @@ Acceptor::Listen (const Arguments& args)
}
}
Handle < Value >
Handle < Value >
Accepto r: : Close ( const Arguments & args )
Serve r: : Close ( const Arguments & args )
{
{
Acceptor * accepto r = NODE_UNWRAP ( Accepto r, args . Holder ( ) ) ;
Server * serve r = NODE_UNWRAP ( Serve r, args . Holder ( ) ) ;
if ( ! accepto r) return Handle < Value > ( ) ;
if ( ! serve r) return Handle < Value > ( ) ;
accepto r- > Close ( ) ;
serve r- > Close ( ) ;
return Undefined ( ) ;
return Undefined ( ) ;
}
}