Skip to content

Commit

Permalink
net: use kHandle symbol for accessing native handle
Browse files Browse the repository at this point in the history
Use a common `kHandle` for all `StreamBase`-based streams.

PR-URL: #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>
  • Loading branch information
addaleax authored and BridgeAR committed Mar 14, 2019
1 parent 3e54f90 commit c14aa07
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
12 changes: 5 additions & 7 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ const {
updateSettingsBuffer
} = require('internal/http2/util');
const {
createWriteWrap,
writeGeneric,
writevGeneric,
onStreamRead,
kAfterAsyncWrite,
kMaybeDestroy,
kUpdateTimer
kUpdateTimer,
kHandle
} = require('internal/stream_base_commons');
const {
kTimeout,
Expand Down Expand Up @@ -151,7 +151,6 @@ const TLSServer = tls.Server;
const kAlpnProtocol = Symbol('alpnProtocol');
const kAuthority = Symbol('authority');
const kEncrypted = Symbol('encrypted');
const kHandle = Symbol('handle');
const kID = Symbol('id');
const kInit = Symbol('init');
const kInfoHeaders = Symbol('sent-info-headers');
Expand Down Expand Up @@ -1798,13 +1797,12 @@ class Http2Stream extends Duplex {
if (!this.headersSent)
this[kProceed]();

const req = createWriteWrap(this[kHandle]);
req.stream = this[kID];
let req;

if (writev)
writevGeneric(this, req, data, cb);
req = writevGeneric(this, data, cb);
else
writeGeneric(this, req, data, encoding, cb);
req = writeGeneric(this, data, encoding, cb);

trackWriteState(this, req.bytes);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const { owner_symbol } = require('internal/async_hooks').symbols;
const kMaybeDestroy = Symbol('kMaybeDestroy');
const kUpdateTimer = Symbol('kUpdateTimer');
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
const kHandle = Symbol('kHandle');

function handleWriteReq(req, data, encoding) {
const { handle } = req;
Expand Down Expand Up @@ -87,7 +88,8 @@ function createWriteWrap(handle) {
return req;
}

function writevGeneric(self, req, data, cb) {
function writevGeneric(self, data, cb) {
const req = createWriteWrap(self[kHandle]);
var allBuffers = data.allBuffers;
var chunks;
var i;
Expand All @@ -109,12 +111,15 @@ function writevGeneric(self, req, data, cb) {
if (err === 0) req._chunks = chunks;

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);

afterWriteDispatched(self, req, err, cb);
return req;
}

function afterWriteDispatched(self, req, err, cb) {
Expand Down Expand Up @@ -186,4 +191,5 @@ module.exports = {
kAfterAsyncWrite,
kMaybeDestroy,
kUpdateTimer,
kHandle
};
15 changes: 10 additions & 5 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ const {
symbols: { async_id_symbol, owner_symbol }
} = require('internal/async_hooks');
const {
createWriteWrap,
writevGeneric,
writeGeneric,
onStreamRead,
kAfterAsyncWrite,
kHandle,
kUpdateTimer
} = require('internal/stream_base_commons');
const {
Expand Down Expand Up @@ -236,7 +236,7 @@ function Socket(options) {
// probably be supplied by async_hooks.
this[async_id_symbol] = -1;
this._hadError = false;
this._handle = null;
this[kHandle] = null;
this._parent = null;
this._host = null;
this[kLastWriteQueueSize] = 0;
Expand Down Expand Up @@ -714,11 +714,11 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {

this._unrefTimer();

var req = createWriteWrap(this._handle);
let req;
if (writev)
writevGeneric(this, req, data, cb);
req = writevGeneric(this, data, cb);
else
writeGeneric(this, req, data, encoding, cb);
req = writeGeneric(this, data, encoding, cb);
if (req.async)
this[kLastWriteQueueSize] = req.bytes;
};
Expand Down Expand Up @@ -1608,6 +1608,11 @@ Object.defineProperty(TCP.prototype, 'owner', {
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) {
return this.listen({ fd: fd });
Expand Down

0 comments on commit c14aa07

Please sign in to comment.