Skip to content

Commit

Permalink
net: refactor server.listen flow control
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Mar 5, 2017
1 parent ce3fc05 commit bcd4ff7
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var cluster;
const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort;
const isLegalPort = internalNet.isLegalPort;
const assertPort = internalNet.assertPort;

function noop() {}

Expand Down Expand Up @@ -1363,14 +1362,6 @@ Server.prototype.listen = function() {
if (hasCallback) {
this.once('listening', cb);
}

// ([port][, host][, backlog][, cb]) where port is omitted,
// that is, listen() or listen(cb),
if (args.length === 0 || typeof args[0] === 'function') {
// Bind to a random port.
options.port = 0;
}

const backlogFromArgs =
// (handle, backlog) or (path, backlog) or (port, backlog)
toNumber(args.length > 1 && args[1]) ||
Expand All @@ -1381,38 +1372,51 @@ Server.prototype.listen = function() {
if (options instanceof TCP) {
this._handle = options;
listen(this, null, -1, -1, backlogFromArgs);
} else if (typeof options.fd === 'number' && options.fd >= 0) {
// (handle[, backlog][, cb]) where handle is an object with a fd
return this;
}
// (handle[, backlog][, cb]) where handle is an object with a fd
if (typeof options.fd === 'number' && options.fd >= 0) {
listen(this, null, null, null, backlogFromArgs, options.fd);
} else {
const backlog = options.backlog || backlogFromArgs;
return this;
}

// ([port][, host][, backlog][, cb]) where port is specified
// or (options[, cb]) where options.port is specified
if (typeof options.port === 'number' || typeof options.port === 'string' ||
(typeof options.port === 'undefined' && 'port' in options)) {
// if (options[, cb]) where options.port is explicitly set as undefined,
// bind to an arbitrary unused port
assertPort(options.port);
// start TCP server listening on host:port
if (options.host) {
lookupAndListen(this, options.port | 0, options.host, backlog,
options.exclusive);
} else { // Undefined host, listens on unspecified IPv4 address
listen(this, null, options.port | 0, 4, backlog, undefined,
options.exclusive);
}
} else if (options.path && isPipeName(options.path)) {
// (path[, backlog][, cb]) or (options[, cb])
// where path or options.path is a UNIX domain socket or Windows pipe
const pipeName = this._pipeName = options.path;
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive);
} else {
throw new Error('Invalid listen argument: ' + options);
// ([port][, host][, backlog][, cb]) where port is omitted,
// that is, listen() or listen(cb),
// or (options[, cb]) where options.port is explicitly set as undefined,
// bind to an arbitrary unused port
if (args.length === 0 || typeof args[0] === 'function' ||
(typeof options.port === 'undefined' && 'port' in options)) {
options.port = 0;
}
// ([port][, host][, backlog][, cb]) where port is specified
// or (options[, cb]) where options.port is specified
// or if options.port is normalized as 0 before
if (typeof options.port === 'number' || typeof options.port === 'string') {
if (!isLegalPort(options.port)) {
throw new RangeError('"port" argument must be >= 0 and < 65536');
}
const backlog = options.backlog || backlogFromArgs;
// start TCP server listening on host:port
if (options.host) {
lookupAndListen(this, options.port | 0, options.host, backlog,
options.exclusive);
} else { // Undefined host, listens on unspecified address
listen(this, null, options.port | 0, 4, // addressType will be ignored
backlog, undefined, options.exclusive);
}
return this;
}

return this;
// (path[, backlog][, cb]) or (options[, cb])
// where path or options.path is a UNIX domain socket or Windows pipe
if (options.path && isPipeName(options.path)) {
const pipeName = this._pipeName = options.path;
const backlog = options.backlog || backlogFromArgs;
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive);
return this;
}

throw new Error('Invalid listen argument: ' + options);
};

function lookupAndListen(self, port, address, backlog, exclusive) {
Expand Down

0 comments on commit bcd4ff7

Please sign in to comment.