diff --git a/src/net.cc b/src/net.cc index 5f75d8b2fce..c11a5392d21 100644 --- a/src/net.cc +++ b/src/net.cc @@ -1,6 +1,8 @@ #include "net.h" #include "events.h" +#include + #include #include #include @@ -585,16 +587,33 @@ Server::Listen (const Arguments& args) Server *server = ObjectWrap::Unwrap(args.Holder()); assert(server); + HandleScope scope; + if (args.Length() == 0) return ThrowException(String::New("Must give at least a port as argument.")); - HandleScope scope; String::AsciiValue port(args[0]->ToString()); - char *host = NULL; - if (args[1]->IsString()) { - String::Utf8Value host_sv(args[1]->ToString()); - host = strdup(*host_sv); + char host[DNS_MAXNAME+1] = "\0"; + int backlog = 1024; + + if (args.Length() == 2) { + if (args[1]->IsInt32()) { + backlog = args[1]->Int32Value(); + } else if (args[1]->IsString()) { + args[1]->ToString()->WriteAscii(host, 0, DNS_MAXNAME+1); + } + } else if (args.Length() > 2) { + if (args[1]->IsString()) { + args[1]->ToString()->WriteAscii(host, 0, DNS_MAXNAME+1); + } + + if (!args[2]->IsInt32()) { + return ThrowException( + Exception::TypeError(String::New("backlog must be an integer"))); + } + + backlog = args[2]->Int32Value(); } // For servers call getaddrinfo inline. This is blocking but it shouldn't @@ -602,14 +621,13 @@ Server::Listen (const Arguments& args) // with a libeio call. struct addrinfo *address = NULL, *address_list = NULL; - int r = getaddrinfo(host, *port, &server_tcp_hints, &address_list); - free(host); + int r = getaddrinfo(strlen(host) ? host : NULL, *port, &server_tcp_hints, &address_list); if (r != 0) return ThrowException(String::New(strerror(errno))); address = AddressDefaultToIPv4(address_list); - server->Listen(address); + server->Listen(address, backlog); if (address_list) freeaddrinfo(address_list); diff --git a/src/net.h b/src/net.h index d4d57b122ed..e18e76c08e4 100644 --- a/src/net.h +++ b/src/net.h @@ -143,8 +143,8 @@ protected: evcom_server_close (&server_); } - int Listen (struct addrinfo *address) { - int r = evcom_server_listen (&server_, address, 1024); + int Listen (struct addrinfo *address, int backlog) { + int r = evcom_server_listen (&server_, address, backlog); if(r != 0) return r; evcom_server_attach (EV_DEFAULT_ &server_); Attach(); diff --git a/website/api.txt b/website/api.txt index a977c72983e..fdd5649295f 100644 --- a/website/api.txt +++ b/website/api.txt @@ -892,7 +892,7 @@ function echo (socket) { socket.close(); }); } -var server = node.tcp.createServer(echo, {backlog: 1024}); +var server = node.tcp.createServer(echo); server.listen(7000, "localhost"); ---------------------------------------- @@ -908,24 +908,25 @@ server.listen(7000, "localhost"); error occured +errorno+ will be 0. |========================================================= -+node.tcp.createServer(connection_listener, options={});+ :: ++node.tcp.createServer(connection_listener);+ :: Creates a new TCP server. + The +connection_listener+ argument is automatically set as a listener for the +"connection"+ event. -+ -+options+ for now only supports one option: -+backlog+ which should be an integer and describes -how large of a connection backlog the operating system should -maintain for this server. The +backlog+ defaults -to 1024. -+server.listen(port, host=null)+ :: -Tells the server to listen for TCP connections to +port+ -and +host+. Note, +host+ is optional. If -+host+ is not specified the server will accept -connections to any IP address on the specified port. ++server.listen(port, host=null, backlog=1024)+ :: +Tells the server to listen for TCP connections to +port+ and +host+. + ++host+ is optional. If +host+ is not specified the server will accept client +connections on any network address. + +The third argument, +backlog+, is also optional and defaults to 1024. The ++backlog+ argument defines the maximum length to which the queue of pending +connections for the server may grow. If a connection request arrives when +the queue is full, the client may receive a "ECONNREFUSED" error or, if the +underlying protocol supports retransmission, the request may be ignored so +that a later reattempt at connection succeeds +server.close()+::