@@ -89,6 +89,7 @@ const {
89
89
ERR_INVALID_ARG_VALUE ,
90
90
ERR_INVALID_FD_TYPE ,
91
91
ERR_INVALID_IP_ADDRESS ,
92
+ ERR_INVALID_HANDLE_TYPE ,
92
93
ERR_SERVER_ALREADY_LISTEN ,
93
94
ERR_SERVER_NOT_RUNNING ,
94
95
ERR_SOCKET_CLOSED ,
@@ -640,6 +641,21 @@ Socket.prototype.end = function(data, encoding, callback) {
640
641
return this ;
641
642
} ;
642
643
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
+ } ;
643
659
644
660
Socket . prototype . pause = function ( ) {
645
661
if ( this [ kBuffer ] && ! this . connecting && this . _handle &&
@@ -710,10 +726,20 @@ Socket.prototype._destroy = function(exception, cb) {
710
726
this [ kBytesRead ] = this . _handle . bytesRead ;
711
727
this [ kBytesWritten ] = this . _handle . bytesWritten ;
712
728
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
+ }
717
743
this . _handle . onread = noop ;
718
744
this . _handle = null ;
719
745
this . _sockname = null ;
@@ -732,6 +758,12 @@ Socket.prototype._destroy = function(exception, cb) {
732
758
}
733
759
} ;
734
760
761
+ Socket . prototype . _reset = function ( ) {
762
+ debug ( 'reset connection' ) ;
763
+ this . resetAndClosing = true ;
764
+ return this . destroy ( ) ;
765
+ } ;
766
+
735
767
Socket . prototype . _getpeername = function ( ) {
736
768
if ( ! this . _handle || ! this . _handle . getpeername || this . connecting ) {
737
769
return this . _peername || { } ;
0 commit comments