net: use kHandle symbol for accessing native handle

Use a common `kHandle` for all `StreamBase`-based streams.

PR-URL: https://github.com/nodejs/node/pull/26491
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Anna Henningsen 2019-03-07 12:22:36 +01:00 committed by Ruben Bridgewater
parent 3414bc7b25
commit e7f58868b5
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
3 changed files with 23 additions and 14 deletions

View File

@ -106,13 +106,13 @@ const {
updateSettingsBuffer updateSettingsBuffer
} = require('internal/http2/util'); } = require('internal/http2/util');
const { const {
createWriteWrap,
writeGeneric, writeGeneric,
writevGeneric, writevGeneric,
onStreamRead, onStreamRead,
kAfterAsyncWrite, kAfterAsyncWrite,
kMaybeDestroy, kMaybeDestroy,
kUpdateTimer, kUpdateTimer,
kHandle,
kSession, kSession,
setStreamTimeout setStreamTimeout
} = require('internal/stream_base_commons'); } = require('internal/stream_base_commons');
@ -149,7 +149,6 @@ const TLSServer = tls.Server;
const kAlpnProtocol = Symbol('alpnProtocol'); const kAlpnProtocol = Symbol('alpnProtocol');
const kAuthority = Symbol('authority'); const kAuthority = Symbol('authority');
const kEncrypted = Symbol('encrypted'); const kEncrypted = Symbol('encrypted');
const kHandle = Symbol('handle');
const kID = Symbol('id'); const kID = Symbol('id');
const kInit = Symbol('init'); const kInit = Symbol('init');
const kInfoHeaders = Symbol('sent-info-headers'); const kInfoHeaders = Symbol('sent-info-headers');
@ -1795,13 +1794,12 @@ class Http2Stream extends Duplex {
if (!this.headersSent) if (!this.headersSent)
this[kProceed](); this[kProceed]();
const req = createWriteWrap(this[kHandle]); let req;
req.stream = this[kID];
if (writev) if (writev)
writevGeneric(this, req, data, cb); req = writevGeneric(this, data, cb);
else else
writeGeneric(this, req, data, encoding, cb); req = writeGeneric(this, data, encoding, cb);
trackWriteState(this, req.bytes); trackWriteState(this, req.bytes);
} }

View File

@ -27,7 +27,8 @@ const {
const kMaybeDestroy = Symbol('kMaybeDestroy'); const kMaybeDestroy = Symbol('kMaybeDestroy');
const kUpdateTimer = Symbol('kUpdateTimer'); const kUpdateTimer = Symbol('kUpdateTimer');
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite'); const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
const kSession = Symbol('session'); const kHandle = Symbol('kHandle');
const kSession = Symbol('kSession');
function handleWriteReq(req, data, encoding) { function handleWriteReq(req, data, encoding) {
const { handle } = req; const { handle } = req;
@ -98,7 +99,8 @@ function createWriteWrap(handle) {
return req; return req;
} }
function writevGeneric(self, req, data, cb) { function writevGeneric(self, data, cb) {
const req = createWriteWrap(self[kHandle]);
var allBuffers = data.allBuffers; var allBuffers = data.allBuffers;
var chunks; var chunks;
var i; var i;
@ -120,12 +122,15 @@ function writevGeneric(self, req, data, cb) {
if (err === 0) req._chunks = chunks; if (err === 0) req._chunks = chunks;
afterWriteDispatched(self, req, err, cb); afterWriteDispatched(self, req, err, cb);
return req;
} }
function writeGeneric(self, req, data, encoding, cb) { function writeGeneric(self, data, encoding, cb) {
const req = createWriteWrap(self[kHandle]);
var err = handleWriteReq(req, data, encoding); var err = handleWriteReq(req, data, encoding);
afterWriteDispatched(self, req, err, cb); afterWriteDispatched(self, req, err, cb);
return req;
} }
function afterWriteDispatched(self, req, err, cb) { function afterWriteDispatched(self, req, err, cb) {
@ -229,6 +234,7 @@ module.exports = {
kAfterAsyncWrite, kAfterAsyncWrite,
kMaybeDestroy, kMaybeDestroy,
kUpdateTimer, kUpdateTimer,
kHandle,
kSession, kSession,
setStreamTimeout setStreamTimeout
}; };

View File

@ -58,11 +58,11 @@ const {
symbols: { async_id_symbol, owner_symbol } symbols: { async_id_symbol, owner_symbol }
} = require('internal/async_hooks'); } = require('internal/async_hooks');
const { const {
createWriteWrap,
writevGeneric, writevGeneric,
writeGeneric, writeGeneric,
onStreamRead, onStreamRead,
kAfterAsyncWrite, kAfterAsyncWrite,
kHandle,
kUpdateTimer, kUpdateTimer,
setStreamTimeout setStreamTimeout
} = require('internal/stream_base_commons'); } = require('internal/stream_base_commons');
@ -233,7 +233,7 @@ function Socket(options) {
// probably be supplied by async_hooks. // probably be supplied by async_hooks.
this[async_id_symbol] = -1; this[async_id_symbol] = -1;
this._hadError = false; this._hadError = false;
this._handle = null; this[kHandle] = null;
this._parent = null; this._parent = null;
this._host = null; this._host = null;
this[kLastWriteQueueSize] = 0; this[kLastWriteQueueSize] = 0;
@ -689,11 +689,11 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
this._unrefTimer(); this._unrefTimer();
var req = createWriteWrap(this._handle); let req;
if (writev) if (writev)
writevGeneric(this, req, data, cb); req = writevGeneric(this, data, cb);
else else
writeGeneric(this, req, data, encoding, cb); req = writeGeneric(this, data, encoding, cb);
if (req.async) if (req.async)
this[kLastWriteQueueSize] = req.bytes; this[kLastWriteQueueSize] = req.bytes;
}; };
@ -1584,6 +1584,11 @@ Object.defineProperty(TCP.prototype, 'owner', {
set(v) { return this[owner_symbol] = v; } set(v) { return this[owner_symbol] = v; }
}); });
Object.defineProperty(Socket.prototype, '_handle', {
get() { return this[kHandle]; },
set(v) { return this[kHandle] = v; }
});
Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) { Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
return this.listen({ fd: fd }); return this.listen({ fd: fd });