@@ -421,32 +421,42 @@ public async Task ReadAsync(byte[] buffer, int offset, int count)
421421 int read = 0 ;
422422 int shouldRead = count ;
423423
424- while ( read < count )
424+ try
425425 {
426- try
426+ if ( _useSslStream )
427427 {
428- int currentRead = ( _useSslStream
429- ? await _sslStream . ReadAsync ( buffer , offset , shouldRead ) . ConfigureAwait ( false )
430- : await _inputStream . ReadAsync ( buffer , offset , shouldRead ) . ConfigureAwait ( false ) ) ;
431- if ( currentRead == count )
432- break ;
433- if ( currentRead < 1 )
434- throw new IOException ( "The socket seems to be disconnected" ) ;
435-
436- read += currentRead ;
437- offset += currentRead ;
438- shouldRead -= currentRead ;
428+ int currentRead = - 1 ;
429+ do
430+ {
431+ currentRead = await _sslStream . ReadAsync ( buffer , offset , shouldRead ) . ConfigureAwait ( false ) ;
432+ }
433+ while ( currentRead != 0 ) ;
439434 }
440- catch ( Exception ex )
435+ else
441436 {
442- if ( ex is IOException || ex is SocketException )
437+ while ( read < count )
443438 {
444- _isAlive = false ;
439+ int currentRead = await _inputStream . ReadAsync ( buffer , offset , shouldRead ) . ConfigureAwait ( false ) ;
440+ if ( currentRead == count )
441+ break ;
442+ if ( currentRead < 1 )
443+ throw new IOException ( "The socket seems to be disconnected" ) ;
444+
445+ read += currentRead ;
446+ offset += currentRead ;
447+ shouldRead -= currentRead ;
445448 }
446-
447- throw ;
448449 }
449450 }
451+ catch ( Exception ex )
452+ {
453+ if ( ex is IOException || ex is SocketException )
454+ {
455+ _isAlive = false ;
456+ }
457+
458+ throw ;
459+ }
450460 }
451461
452462 /// <summary>
@@ -463,32 +473,42 @@ public void Read(byte[] buffer, int offset, int count)
463473 int read = 0 ;
464474 int shouldRead = count ;
465475
466- while ( read < count )
476+ try
467477 {
468- try
478+ if ( _useSslStream )
469479 {
470- int currentRead = ( _useSslStream
471- ? _sslStream . Read ( buffer , offset , shouldRead )
472- : _inputStream . Read ( buffer , offset , shouldRead ) ) ;
473- if ( currentRead == count )
474- break ;
475- if ( currentRead < 1 )
476- throw new IOException ( "The socket seems to be disconnected" ) ;
477-
478- read += currentRead ;
479- offset += currentRead ;
480- shouldRead -= currentRead ;
480+ int currentRead = - 1 ;
481+ do
482+ {
483+ currentRead = _sslStream . Read ( buffer , offset , shouldRead ) ;
484+ }
485+ while ( currentRead != 0 ) ;
481486 }
482- catch ( Exception ex )
487+ else
483488 {
484- if ( ex is IOException || ex is SocketException )
489+ while ( read < count )
485490 {
486- _isAlive = false ;
491+ int currentRead = _inputStream . Read ( buffer , offset , shouldRead ) ;
492+ if ( currentRead == count )
493+ break ;
494+ if ( currentRead < 1 )
495+ throw new IOException ( "The socket seems to be disconnected" ) ;
496+
497+ read += currentRead ;
498+ offset += currentRead ;
499+ shouldRead -= currentRead ;
487500 }
488-
489- throw ;
490501 }
491502 }
503+ catch ( Exception ex )
504+ {
505+ if ( ex is IOException || ex is SocketException )
506+ {
507+ _isAlive = false ;
508+ }
509+
510+ throw ;
511+ }
492512 }
493513
494514 public void Write ( byte [ ] data , int offset , int length )
0 commit comments