Skip to content

Commit

Permalink
net: refactor Server.prototype.listen
Browse files Browse the repository at this point in the history
* Separate backlogFromArgs and options.backlog
* Comment the overloading process
  • Loading branch information
joyeecheung committed Mar 5, 2017
1 parent 1ea81d5 commit ce3fc05
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1351,49 +1351,60 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {


Server.prototype.listen = function() {
var args = new Array(arguments.length);
const args = new Array(arguments.length);
for (var i = 0; i < arguments.length; i++)
args[i] = arguments[i];
var [options, cb] = normalizeArgs(args);
// TODO(joyeecheung): use destructuring when V8 is fast enough
const normalized = normalizeArgs(args);
var options = normalized[0];
const cb = normalized[1];

if (typeof cb === 'function') {
var hasCallback = (cb !== null);
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;
}

// The third optional argument is the backlog size.
// When the ip is omitted it can be the second argument.
var backlog = toNumber(args.length > 1 && args[1]) ||
toNumber(args.length > 2 && args[2]);
const backlogFromArgs =
// (handle, backlog) or (path, backlog) or (port, backlog)
toNumber(args.length > 1 && args[1]) ||
toNumber(args.length > 2 && args[2]); // (port, host, backlog)

options = options._handle || options.handle || options;

// (handle[, backlog][, cb]) where handle is an object with a handle
if (options instanceof TCP) {
this._handle = options;
listen(this, null, -1, -1, backlog);
listen(this, null, -1, -1, backlogFromArgs);
} else if (typeof options.fd === 'number' && options.fd >= 0) {
listen(this, null, null, null, backlog, options.fd);
// (handle[, backlog][, cb]) where handle is an object with a fd
listen(this, null, null, null, backlogFromArgs, options.fd);
} else {
backlog = options.backlog || backlog;
const backlog = options.backlog || backlogFromArgs;

// ([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)) {
// Undefined is interpreted as zero (random port) for consistency
// with net.connect().
// 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 {
} 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)) {
// UNIX socket or Windows pipe.
// (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 {
Expand Down

0 comments on commit ce3fc05

Please sign in to comment.