Skip to content

Commit 5373c68

Browse files
committed
node.tcp.Server's backlog option is now an argument to listen()
1 parent 4db8bb9 commit 5373c68

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

src/net.cc

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "net.h"
22
#include "events.h"
33

4+
#include <udns.h>
5+
46
#include <assert.h>
57
#include <stdlib.h>
68
#include <string.h>
@@ -585,31 +587,47 @@ Server::Listen (const Arguments& args)
585587
Server *server = ObjectWrap::Unwrap<Server>(args.Holder());
586588
assert(server);
587589

590+
HandleScope scope;
591+
588592
if (args.Length() == 0)
589593
return ThrowException(String::New("Must give at least a port as argument."));
590594

591-
HandleScope scope;
592595
String::AsciiValue port(args[0]->ToString());
593596

594-
char *host = NULL;
595-
if (args[1]->IsString()) {
596-
String::Utf8Value host_sv(args[1]->ToString());
597-
host = strdup(*host_sv);
597+
char host[DNS_MAXNAME+1] = "\0";
598+
int backlog = 1024;
599+
600+
if (args.Length() == 2) {
601+
if (args[1]->IsInt32()) {
602+
backlog = args[1]->Int32Value();
603+
} else if (args[1]->IsString()) {
604+
args[1]->ToString()->WriteAscii(host, 0, DNS_MAXNAME+1);
605+
}
606+
} else if (args.Length() > 2) {
607+
if (args[1]->IsString()) {
608+
args[1]->ToString()->WriteAscii(host, 0, DNS_MAXNAME+1);
609+
}
610+
611+
if (!args[2]->IsInt32()) {
612+
return ThrowException(
613+
Exception::TypeError(String::New("backlog must be an integer")));
614+
}
615+
616+
backlog = args[2]->Int32Value();
598617
}
599618

600619
// For servers call getaddrinfo inline. This is blocking but it shouldn't
601620
// matter much. If someone actually complains then simply swap it out
602621
// with a libeio call.
603622
struct addrinfo *address = NULL,
604623
*address_list = NULL;
605-
int r = getaddrinfo(host, *port, &server_tcp_hints, &address_list);
606-
free(host);
624+
int r = getaddrinfo(strlen(host) ? host : NULL, *port, &server_tcp_hints, &address_list);
607625
if (r != 0)
608626
return ThrowException(String::New(strerror(errno)));
609627

610628
address = AddressDefaultToIPv4(address_list);
611629

612-
server->Listen(address);
630+
server->Listen(address, backlog);
613631

614632
if (address_list) freeaddrinfo(address_list);
615633

src/net.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ class Server : public EventEmitter {
143143
evcom_server_close (&server_);
144144
}
145145

146-
int Listen (struct addrinfo *address) {
147-
int r = evcom_server_listen (&server_, address, 1024);
146+
int Listen (struct addrinfo *address, int backlog) {
147+
int r = evcom_server_listen (&server_, address, backlog);
148148
if(r != 0) return r;
149149
evcom_server_attach (EV_DEFAULT_ &server_);
150150
Attach();

website/api.txt

+16-15
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ function echo (socket) {
892892
socket.close();
893893
});
894894
}
895-
var server = node.tcp.createServer(echo, {backlog: 1024});
895+
var server = node.tcp.createServer(echo);
896896
server.listen(7000, "localhost");
897897
----------------------------------------
898898

@@ -908,24 +908,25 @@ server.listen(7000, "localhost");
908908
error occured +errorno+ will be 0.
909909
|=========================================================
910910

911-
+node.tcp.createServer(connection_listener, options={});+ ::
911+
+node.tcp.createServer(connection_listener);+ ::
912912
Creates a new TCP server.
913913
+
914914
The +connection_listener+ argument is automatically set as a listener for
915915
the +"connection"+ event.
916-
+
917-
+options+ for now only supports one option:
918-
+backlog+ which should be an integer and describes
919-
how large of a connection backlog the operating system should
920-
maintain for this server. The +backlog+ defaults
921-
to 1024.
922-
923-
924-
+server.listen(port, host=null)+ ::
925-
Tells the server to listen for TCP connections to +port+
926-
and +host+. Note, +host+ is optional. If
927-
+host+ is not specified the server will accept
928-
connections to any IP address on the specified port.
916+
917+
918+
+server.listen(port, host=null, backlog=1024)+ ::
919+
Tells the server to listen for TCP connections to +port+ and +host+.
920+
921+
+host+ is optional. If +host+ is not specified the server will accept client
922+
connections on any network address.
923+
924+
The third argument, +backlog+, is also optional and defaults to 1024. The
925+
+backlog+ argument defines the maximum length to which the queue of pending
926+
connections for the server may grow. If a connection request arrives when
927+
the queue is full, the client may receive a "ECONNREFUSED" error or, if the
928+
underlying protocol supports retransmission, the request may be ignored so
929+
that a later reattempt at connection succeeds
929930

930931

931932
+server.close()+::

0 commit comments

Comments
 (0)