summaryrefslogtreecommitdiff
path: root/lib/net.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/net.js')
-rw-r--r--lib/net.js40
1 files changed, 36 insertions, 4 deletions
diff --git a/lib/net.js b/lib/net.js
index 4318beb501..0110531619 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 || {};