Browse Source

node.tcp.Server's backlog option is now an argument to listen()

v0.7.4-release
Ryan 16 years ago
parent
commit
5373c6869a
  1. 32
      src/net.cc
  2. 4
      src/net.h
  3. 31
      website/api.txt

32
src/net.cc

@ -1,6 +1,8 @@
#include "net.h"
#include "events.h"
#include <udns.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
@ -585,16 +587,33 @@ Server::Listen (const Arguments& args)
Server *server = ObjectWrap::Unwrap<Server>(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;
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()) {
String::Utf8Value host_sv(args[1]->ToString());
host = strdup(*host_sv);
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);

4
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();

31
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()+::

Loading…
Cancel
Save