From 5824f4cd55b2b49cbaedd771ca3684559640bd96 Mon Sep 17 00:00:00 2001 From: dudu Date: Fri, 24 Jan 2025 20:23:34 +0800 Subject: [PATCH] fix: read ssl stream until no more bytes --- src/Enyim.Caching/Memcached/PooledSocket.cs | 92 +++++++++++++-------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/src/Enyim.Caching/Memcached/PooledSocket.cs b/src/Enyim.Caching/Memcached/PooledSocket.cs index 8c47f1d9..c2e93222 100755 --- a/src/Enyim.Caching/Memcached/PooledSocket.cs +++ b/src/Enyim.Caching/Memcached/PooledSocket.cs @@ -421,32 +421,42 @@ public async Task ReadAsync(byte[] buffer, int offset, int count) int read = 0; int shouldRead = count; - while (read < count) + try { - try + if (_useSslStream) { - int currentRead = (_useSslStream - ? await _sslStream.ReadAsync(buffer, offset, shouldRead).ConfigureAwait(false) - : await _inputStream.ReadAsync(buffer, offset, shouldRead).ConfigureAwait(false)); - if (currentRead == count) - break; - if (currentRead < 1) - throw new IOException("The socket seems to be disconnected"); - - read += currentRead; - offset += currentRead; - shouldRead -= currentRead; + int currentRead = -1; + do + { + currentRead = await _sslStream.ReadAsync(buffer, offset, shouldRead).ConfigureAwait(false); + } + while (currentRead != 0); } - catch (Exception ex) + else { - if (ex is IOException || ex is SocketException) + while (read < count) { - _isAlive = false; + int currentRead = await _inputStream.ReadAsync(buffer, offset, shouldRead).ConfigureAwait(false); + if (currentRead == count) + break; + if (currentRead < 1) + throw new IOException("The socket seems to be disconnected"); + + read += currentRead; + offset += currentRead; + shouldRead -= currentRead; } - - throw; } } + catch (Exception ex) + { + if (ex is IOException || ex is SocketException) + { + _isAlive = false; + } + + throw; + } } /// @@ -463,32 +473,42 @@ public void Read(byte[] buffer, int offset, int count) int read = 0; int shouldRead = count; - while (read < count) + try { - try + if (_useSslStream) { - int currentRead = (_useSslStream - ? _sslStream.Read(buffer, offset, shouldRead) - : _inputStream.Read(buffer, offset, shouldRead)); - if (currentRead == count) - break; - if (currentRead < 1) - throw new IOException("The socket seems to be disconnected"); - - read += currentRead; - offset += currentRead; - shouldRead -= currentRead; + int currentRead = -1; + do + { + currentRead = _sslStream.Read(buffer, offset, shouldRead); + } + while (currentRead != 0); } - catch (Exception ex) + else { - if (ex is IOException || ex is SocketException) + while (read < count) { - _isAlive = false; + int currentRead = _inputStream.Read(buffer, offset, shouldRead); + if (currentRead == count) + break; + if (currentRead < 1) + throw new IOException("The socket seems to be disconnected"); + + read += currentRead; + offset += currentRead; + shouldRead -= currentRead; } - - throw; } } + catch (Exception ex) + { + if (ex is IOException || ex is SocketException) + { + _isAlive = false; + } + + throw; + } } public void Write(byte[] data, int offset, int length)