|
@ -39,6 +39,9 @@ const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap; |
|
|
const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap; |
|
|
const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap; |
|
|
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap; |
|
|
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap; |
|
|
const WriteWrap = process.binding('stream_wrap').WriteWrap; |
|
|
const WriteWrap = process.binding('stream_wrap').WriteWrap; |
|
|
|
|
|
const async_id_symbol = process.binding('async_wrap').async_id_symbol; |
|
|
|
|
|
const { newUid, setInitTriggerId } = require('async_hooks'); |
|
|
|
|
|
const nextTick = require('internal/process/next_tick').nextTick; |
|
|
|
|
|
|
|
|
var cluster; |
|
|
var cluster; |
|
|
var dns; |
|
|
var dns; |
|
@ -57,6 +60,12 @@ function createHandle(fd) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getNewAsyncId(handle) { |
|
|
|
|
|
return (!handle || typeof handle.getAsyncId !== 'function') ? |
|
|
|
|
|
newUid() : handle.getAsyncId(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const debug = util.debuglog('net'); |
|
|
const debug = util.debuglog('net'); |
|
|
|
|
|
|
|
|
function isPipeName(s) { |
|
|
function isPipeName(s) { |
|
@ -147,6 +156,7 @@ function initSocketHandle(self) { |
|
|
if (self._handle) { |
|
|
if (self._handle) { |
|
|
self._handle.owner = self; |
|
|
self._handle.owner = self; |
|
|
self._handle.onread = onread; |
|
|
self._handle.onread = onread; |
|
|
|
|
|
self[async_id_symbol] = getNewAsyncId(self._handle); |
|
|
|
|
|
|
|
|
// If handle doesn't support writev - neither do we
|
|
|
// If handle doesn't support writev - neither do we
|
|
|
if (!self._handle.writev) |
|
|
if (!self._handle.writev) |
|
@ -162,6 +172,10 @@ function Socket(options) { |
|
|
if (!(this instanceof Socket)) return new Socket(options); |
|
|
if (!(this instanceof Socket)) return new Socket(options); |
|
|
|
|
|
|
|
|
this.connecting = false; |
|
|
this.connecting = false; |
|
|
|
|
|
// Problem with this is that users can supply their own handle, that may not
|
|
|
|
|
|
// have _handle.getAsyncId(). In this case an[async_id_symbol] should
|
|
|
|
|
|
// probably be supplied by async_hooks.
|
|
|
|
|
|
this[async_id_symbol] = -1; |
|
|
this._hadError = false; |
|
|
this._hadError = false; |
|
|
this._handle = null; |
|
|
this._handle = null; |
|
|
this._parent = null; |
|
|
this._parent = null; |
|
@ -176,9 +190,11 @@ function Socket(options) { |
|
|
|
|
|
|
|
|
if (options.handle) { |
|
|
if (options.handle) { |
|
|
this._handle = options.handle; // private
|
|
|
this._handle = options.handle; // private
|
|
|
|
|
|
this[async_id_symbol] = getNewAsyncId(this._handle); |
|
|
} else if (options.fd !== undefined) { |
|
|
} else if (options.fd !== undefined) { |
|
|
this._handle = createHandle(options.fd); |
|
|
this._handle = createHandle(options.fd); |
|
|
this._handle.open(options.fd); |
|
|
this._handle.open(options.fd); |
|
|
|
|
|
this[async_id_symbol] = this._handle.getAsyncId(); |
|
|
// options.fd can be string (since it is user-defined),
|
|
|
// options.fd can be string (since it is user-defined),
|
|
|
// so changing this to === would be semver-major
|
|
|
// so changing this to === would be semver-major
|
|
|
// See: https://github.com/nodejs/node/pull/11513
|
|
|
// See: https://github.com/nodejs/node/pull/11513
|
|
@ -264,6 +280,10 @@ function onSocketFinish() { |
|
|
var req = new ShutdownWrap(); |
|
|
var req = new ShutdownWrap(); |
|
|
req.oncomplete = afterShutdown; |
|
|
req.oncomplete = afterShutdown; |
|
|
req.handle = this._handle; |
|
|
req.handle = this._handle; |
|
|
|
|
|
// node::ShutdownWrap isn't instantiated and attached to the JS instance of
|
|
|
|
|
|
// ShutdownWrap above until shutdown() is called. So don't set the init
|
|
|
|
|
|
// trigger id until now.
|
|
|
|
|
|
setInitTriggerId(this[async_id_symbol]); |
|
|
var err = this._handle.shutdown(req); |
|
|
var err = this._handle.shutdown(req); |
|
|
|
|
|
|
|
|
if (err) |
|
|
if (err) |
|
@ -329,7 +349,7 @@ function writeAfterFIN(chunk, encoding, cb) { |
|
|
// TODO: defer error events consistently everywhere, not just the cb
|
|
|
// TODO: defer error events consistently everywhere, not just the cb
|
|
|
this.emit('error', er); |
|
|
this.emit('error', er); |
|
|
if (typeof cb === 'function') { |
|
|
if (typeof cb === 'function') { |
|
|
process.nextTick(cb, er); |
|
|
nextTick(this[async_id_symbol], cb, er); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -887,6 +907,10 @@ function internalConnect( |
|
|
req.localAddress = localAddress; |
|
|
req.localAddress = localAddress; |
|
|
req.localPort = localPort; |
|
|
req.localPort = localPort; |
|
|
|
|
|
|
|
|
|
|
|
// node::TCPConnectWrap isn't instantiated and attached to the JS instance
|
|
|
|
|
|
// of TCPConnectWrap above until connect() is called. So don't set the init
|
|
|
|
|
|
// trigger id until now.
|
|
|
|
|
|
setInitTriggerId(self[async_id_symbol]); |
|
|
if (addressType === 4) |
|
|
if (addressType === 4) |
|
|
err = self._handle.connect(req, address, port); |
|
|
err = self._handle.connect(req, address, port); |
|
|
else |
|
|
else |
|
@ -896,6 +920,10 @@ function internalConnect( |
|
|
const req = new PipeConnectWrap(); |
|
|
const req = new PipeConnectWrap(); |
|
|
req.address = address; |
|
|
req.address = address; |
|
|
req.oncomplete = afterConnect; |
|
|
req.oncomplete = afterConnect; |
|
|
|
|
|
// node::PipeConnectWrap isn't instantiated and attached to the JS instance
|
|
|
|
|
|
// of PipeConnectWrap above until connect() is called. So don't set the
|
|
|
|
|
|
// init trigger id until now.
|
|
|
|
|
|
setInitTriggerId(self[async_id_symbol]); |
|
|
err = self._handle.connect(req, address, afterConnect); |
|
|
err = self._handle.connect(req, address, afterConnect); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -1020,6 +1048,7 @@ function lookupAndConnect(self, options) { |
|
|
debug('connect: dns options', dnsopts); |
|
|
debug('connect: dns options', dnsopts); |
|
|
self._host = host; |
|
|
self._host = host; |
|
|
var lookup = options.lookup || dns.lookup; |
|
|
var lookup = options.lookup || dns.lookup; |
|
|
|
|
|
setInitTriggerId(self[async_id_symbol]); |
|
|
lookup(host, dnsopts, function emitLookup(err, ip, addressType) { |
|
|
lookup(host, dnsopts, function emitLookup(err, ip, addressType) { |
|
|
self.emit('lookup', err, ip, addressType, host); |
|
|
self.emit('lookup', err, ip, addressType, host); |
|
|
|
|
|
|
|
@ -1167,6 +1196,7 @@ function Server(options, connectionListener) { |
|
|
configurable: true, enumerable: false |
|
|
configurable: true, enumerable: false |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this[async_id_symbol] = -1; |
|
|
this._handle = null; |
|
|
this._handle = null; |
|
|
this._usingSlaves = false; |
|
|
this._usingSlaves = false; |
|
|
this._slaves = []; |
|
|
this._slaves = []; |
|
@ -1274,6 +1304,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) { |
|
|
this._handle = rval; |
|
|
this._handle = rval; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this[async_id_symbol] = getNewAsyncId(this._handle); |
|
|
this._handle.onconnection = onconnection; |
|
|
this._handle.onconnection = onconnection; |
|
|
this._handle.owner = this; |
|
|
this._handle.owner = this; |
|
|
|
|
|
|
|
@ -1286,7 +1317,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) { |
|
|
var ex = exceptionWithHostPort(err, 'listen', address, port); |
|
|
var ex = exceptionWithHostPort(err, 'listen', address, port); |
|
|
this._handle.close(); |
|
|
this._handle.close(); |
|
|
this._handle = null; |
|
|
this._handle = null; |
|
|
process.nextTick(emitErrorNT, this, ex); |
|
|
nextTick(this[async_id_symbol], emitErrorNT, this, ex); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -1297,7 +1328,7 @@ function setupListenHandle(address, port, addressType, backlog, fd) { |
|
|
if (this._unref) |
|
|
if (this._unref) |
|
|
this.unref(); |
|
|
this.unref(); |
|
|
|
|
|
|
|
|
process.nextTick(emitListeningNT, this); |
|
|
nextTick(this[async_id_symbol], emitListeningNT, this); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Server.prototype._listen2 = setupListenHandle; // legacy alias
|
|
|
Server.prototype._listen2 = setupListenHandle; // legacy alias
|
|
@ -1398,6 +1429,7 @@ Server.prototype.listen = function() { |
|
|
// (handle[, backlog][, cb]) where handle is an object with a handle
|
|
|
// (handle[, backlog][, cb]) where handle is an object with a handle
|
|
|
if (options instanceof TCP) { |
|
|
if (options instanceof TCP) { |
|
|
this._handle = options; |
|
|
this._handle = options; |
|
|
|
|
|
this[async_id_symbol] = this._handle.getAsyncId(); |
|
|
listenInCluster(this, null, -1, -1, backlogFromArgs); |
|
|
listenInCluster(this, null, -1, -1, backlogFromArgs); |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
@ -1521,8 +1553,10 @@ function onconnection(err, clientHandle) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Server.prototype.getConnections = function(cb) { |
|
|
Server.prototype.getConnections = function(cb) { |
|
|
|
|
|
const self = this; |
|
|
|
|
|
|
|
|
function end(err, connections) { |
|
|
function end(err, connections) { |
|
|
process.nextTick(cb, err, connections); |
|
|
nextTick(self[async_id_symbol], cb, err, connections); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!this._usingSlaves) { |
|
|
if (!this._usingSlaves) { |
|
@ -1597,7 +1631,8 @@ Server.prototype._emitCloseIfDrained = function() { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
process.nextTick(emitCloseNT, this); |
|
|
const asyncId = this._handle ? this[async_id_symbol] : null; |
|
|
|
|
|
nextTick(asyncId, emitCloseNT, this); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|