@@ -245,7 +245,7 @@ function Socket(options) {
245
245
246
246
// shut down the socket when we're finished with it.
247
247
this . on ( 'finish' , onSocketFinish ) ;
248
- this . on ( '_socketEnd ' , onSocketEnd ) ;
248
+ this . on ( 'end ' , onReadableStreamEnd ) ;
249
249
250
250
initSocketHandle ( this ) ;
251
251
@@ -341,32 +341,6 @@ function afterShutdown(status, handle) {
341
341
}
342
342
}
343
343
344
- // the EOF has been received, and no more bytes are coming.
345
- // if the writable side has ended already, then clean everything
346
- // up.
347
- function onSocketEnd ( ) {
348
- // XXX Should not have to do as much in this function.
349
- // ended should already be true, since this is called *after*
350
- // the EOF errno and onread has eof'ed
351
- debug ( 'onSocketEnd' , this . _readableState ) ;
352
- this . _readableState . ended = true ;
353
- if ( this . _readableState . endEmitted ) {
354
- this . readable = false ;
355
- maybeDestroy ( this ) ;
356
- } else {
357
- this . once ( 'end' , function end ( ) {
358
- this . readable = false ;
359
- maybeDestroy ( this ) ;
360
- } ) ;
361
- this . read ( 0 ) ;
362
- }
363
-
364
- if ( ! this . allowHalfOpen ) {
365
- this . write = writeAfterFIN ;
366
- this . destroySoon ( ) ;
367
- }
368
- }
369
-
370
344
// Provide a better error message when we call end() as a result
371
345
// of the other side sending a FIN. The standard 'write after end'
372
346
// is overly vague, and makes it seem like the user's code is to blame.
@@ -512,6 +486,12 @@ Socket.prototype.end = function(data, encoding, callback) {
512
486
} ;
513
487
514
488
489
+ // Called when the 'end' event is emitted.
490
+ function onReadableStreamEnd ( ) {
491
+ maybeDestroy ( this ) ;
492
+ }
493
+
494
+
515
495
// Call whenever we set writable=false or readable=false
516
496
function maybeDestroy ( socket ) {
517
497
if ( ! socket . readable &&
@@ -625,10 +605,11 @@ function onread(nread, buffer) {
625
605
// Do it before `maybeDestroy` for correct order of events:
626
606
// `end` -> `close`
627
607
self . push ( null ) ;
608
+ self . read ( 0 ) ;
628
609
629
- if ( self . readableLength === 0 ) {
630
- self . readable = false ;
631
- maybeDestroy ( self ) ;
610
+ if ( ! self . allowHalfOpen ) {
611
+ self . write = writeAfterFIN ;
612
+ self . destroySoon ( ) ;
632
613
}
633
614
634
615
// internal end event so that we know that the actual socket
0 commit comments