@@ -89,6 +89,7 @@ const {
8989 ERR_INVALID_ARG_VALUE ,
9090 ERR_INVALID_FD_TYPE ,
9191 ERR_INVALID_IP_ADDRESS ,
92+ ERR_INVALID_HANDLE_TYPE ,
9293 ERR_SERVER_ALREADY_LISTEN ,
9394 ERR_SERVER_NOT_RUNNING ,
9495 ERR_SOCKET_CLOSED ,
@@ -640,6 +641,21 @@ Socket.prototype.end = function(data, encoding, callback) {
640641 return this ;
641642} ;
642643
644+ Socket . prototype . resetAndDestroy = function ( ) {
645+ if ( this . _handle ) {
646+ if ( ! ( this . _handle instanceof TCP ) )
647+ throw new ERR_INVALID_HANDLE_TYPE ( ) ;
648+ if ( this . connecting ) {
649+ debug ( 'reset wait for connection' ) ;
650+ this . once ( 'connect' , ( ) => this . _reset ( ) ) ;
651+ } else {
652+ this . _reset ( ) ;
653+ }
654+ } else {
655+ this . destroy ( new ERR_SOCKET_CLOSED ( ) ) ;
656+ }
657+ return this ;
658+ } ;
643659
644660Socket . prototype . pause = function ( ) {
645661 if ( this [ kBuffer ] && ! this . connecting && this . _handle &&
@@ -710,10 +726,20 @@ Socket.prototype._destroy = function(exception, cb) {
710726 this [ kBytesRead ] = this . _handle . bytesRead ;
711727 this [ kBytesWritten ] = this . _handle . bytesWritten ;
712728
713- this . _handle . close ( ( ) => {
714- debug ( 'emit close' ) ;
715- this . emit ( 'close' , isException ) ;
716- } ) ;
729+ if ( this . resetAndClosing ) {
730+ this . resetAndClosing = false ;
731+ const err = this . _handle . reset ( ( ) => {
732+ debug ( 'emit close' ) ;
733+ this . emit ( 'close' , isException ) ;
734+ } ) ;
735+ if ( err )
736+ this . emit ( 'error' , errnoException ( err , 'reset' ) ) ;
737+ } else {
738+ this . _handle . close ( ( ) => {
739+ debug ( 'emit close' ) ;
740+ this . emit ( 'close' , isException ) ;
741+ } ) ;
742+ }
717743 this . _handle . onread = noop ;
718744 this . _handle = null ;
719745 this . _sockname = null ;
@@ -732,6 +758,12 @@ Socket.prototype._destroy = function(exception, cb) {
732758 }
733759} ;
734760
761+ Socket . prototype . _reset = function ( ) {
762+ debug ( 'reset connection' ) ;
763+ this . resetAndClosing = true ;
764+ return this . destroy ( ) ;
765+ } ;
766+
735767Socket . prototype . _getpeername = function ( ) {
736768 if ( ! this . _handle || ! this . _handle . getpeername || this . connecting ) {
737769 return this . _peername || { } ;
0 commit comments