Skip to content

Commit 9423902

Browse files
committed
fix: read ssl stream until no more bytes
1 parent 33ebfcf commit 9423902

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/Enyim.Caching/Memcached/PooledSocket.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -421,32 +421,41 @@ 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+
} while (currentRead != 0);
439433
}
440-
catch (Exception ex)
434+
else
441435
{
442-
if (ex is IOException || ex is SocketException)
436+
while (read < count)
443437
{
444-
_isAlive = false;
438+
int currentRead = await _inputStream.ReadAsync(buffer, offset, shouldRead).ConfigureAwait(false);
439+
if (currentRead == count)
440+
break;
441+
if (currentRead < 1)
442+
throw new IOException("The socket seems to be disconnected");
443+
444+
read += currentRead;
445+
offset += currentRead;
446+
shouldRead -= currentRead;
445447
}
446-
447-
throw;
448448
}
449449
}
450+
catch (Exception ex)
451+
{
452+
if (ex is IOException || ex is SocketException)
453+
{
454+
_isAlive = false;
455+
}
456+
457+
throw;
458+
}
450459
}
451460

452461
/// <summary>

0 commit comments

Comments
 (0)