diff options
author | pupilTong <pupiltong@outlook.com> | 2022-05-15 16:54:06 +0300 |
---|---|---|
committer | Bryan English <bryan@bryanenglish.com> | 2022-05-30 19:33:54 +0300 |
commit | 8f5b4570e50f3b8333f70065dac7b09148c02dc4 (patch) | |
tree | 7439870f198cecd2ebca5f411f7cd6f3037cd8c5 /lib | |
parent | 41b69e3cf469ea683592e20278f0b04c413ff34f (diff) |
net: add ability to reset a tcp socket
Fixes: https://github.com/nodejs/node/issues/27428
PR-URL: https://github.com/nodejs/node/pull/43112
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/net.js | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/lib/net.js b/lib/net.js index 4318beb501b..0110531619a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -89,6 +89,7 @@ const { ERR_INVALID_ARG_VALUE, ERR_INVALID_FD_TYPE, ERR_INVALID_IP_ADDRESS, + ERR_INVALID_HANDLE_TYPE, ERR_SERVER_ALREADY_LISTEN, ERR_SERVER_NOT_RUNNING, ERR_SOCKET_CLOSED, @@ -640,6 +641,21 @@ Socket.prototype.end = function(data, encoding, callback) { return this; }; +Socket.prototype.resetAndDestroy = function() { + if (this._handle) { + if (!(this._handle instanceof TCP)) + throw new ERR_INVALID_HANDLE_TYPE(); + if (this.connecting) { + debug('reset wait for connection'); + this.once('connect', () => this._reset()); + } else { + this._reset(); + } + } else { + this.destroy(new ERR_SOCKET_CLOSED()); + } + return this; +}; Socket.prototype.pause = function() { if (this[kBuffer] && !this.connecting && this._handle && @@ -710,10 +726,20 @@ Socket.prototype._destroy = function(exception, cb) { this[kBytesRead] = this._handle.bytesRead; this[kBytesWritten] = this._handle.bytesWritten; - this._handle.close(() => { - debug('emit close'); - this.emit('close', isException); - }); + if (this.resetAndClosing) { + this.resetAndClosing = false; + const err = this._handle.reset(() => { + debug('emit close'); + this.emit('close', isException); + }); + if (err) + this.emit('error', errnoException(err, 'reset')); + } else { + this._handle.close(() => { + debug('emit close'); + this.emit('close', isException); + }); + } this._handle.onread = noop; this._handle = null; this._sockname = null; @@ -732,6 +758,12 @@ Socket.prototype._destroy = function(exception, cb) { } }; +Socket.prototype._reset = function() { + debug('reset connection'); + this.resetAndClosing = true; + return this.destroy(); +}; + Socket.prototype._getpeername = function() { if (!this._handle || !this._handle.getpeername || this.connecting) { return this._peername || {}; |