From c9fb311e1e8bc94fc3b624b1c3485152f30356f4 Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Fri, 30 Aug 2019 19:50:27 +0100 Subject: [PATCH 1/3] simplify SllOverTdsStream.ReadInternal change --- .../Data/SqlClient/SNI/SslOverTdsStream.cs | 75 +++++++------------ 1 file changed, 29 insertions(+), 46 deletions(-) diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs index 4c0620ba7b8c..4c4f0ab25323 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -88,67 +88,50 @@ public override Task ReadAsync(byte[] buffer, int offset, int count, Cancel /// Read Internal is called synchronosly when async is false /// private async Task ReadInternal(byte[] buffer, int offset, int count, CancellationToken token, bool async) - { - if (_encapsulate) - { - return await ReadInternalEncapsulate(buffer, offset, count, token, async).ConfigureAwait(false); - } - else if (async) - { - return await ReadInternalAsync(buffer, offset, count, token).ConfigureAwait(false); - } - else - { - return ReadInternalSync(buffer, offset, count); - } - } - - private async Task ReadInternalEncapsulate(byte[] buffer, int offset, int count, CancellationToken token, bool async) { int readBytes = 0; - byte[] packetData = ArrayPool.Shared.Rent(count < TdsEnums.HEADER_LEN ? TdsEnums.HEADER_LEN : count); + byte[] packetData = null; - if (_packetBytes == 0) + if (_encapsulate) { - // Account for split packets - while (readBytes < TdsEnums.HEADER_LEN) + packetData = ArrayPool.Shared.Rent(count < TdsEnums.HEADER_LEN ? TdsEnums.HEADER_LEN : count); + if (_packetBytes == 0) { - readBytes += (async ? - await ReadInternalAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, token).ConfigureAwait(false) : - ReadInternalSync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes) - ); + // Account for split packets + while (readBytes < TdsEnums.HEADER_LEN) + { + readBytes += async ? + await _stream.ReadAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, token).ConfigureAwait(false) : + _stream.Read(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes); + } + + _packetBytes = (packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8) | packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1]; + _packetBytes -= TdsEnums.HEADER_LEN; } - _packetBytes = (packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8) | packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1]; - _packetBytes -= TdsEnums.HEADER_LEN; + if (count > _packetBytes) + { + count = _packetBytes; + } } - if (count > _packetBytes) + readBytes = async ? + await _stream.ReadAsync(packetData ?? buffer, 0, count, token).ConfigureAwait(false) : + _stream.Read(packetData ?? buffer, 0, count); + + if (_encapsulate) { - count = _packetBytes; + _packetBytes -= readBytes; } - readBytes = (async ? - await ReadInternalAsync(packetData, 0, count, token).ConfigureAwait(false) : - ReadInternalSync(packetData, 0, count) - ); - - - _packetBytes -= readBytes; - - Buffer.BlockCopy(packetData, 0, buffer, offset, readBytes); - - Array.Clear(packetData, 0, readBytes); - ArrayPool.Shared.Return(packetData, clearArray: false); - + if (packetData != null) + { + Buffer.BlockCopy(packetData, 0, buffer, offset, readBytes); + ArrayPool.Shared.Return(packetData, clearArray: true); + } return readBytes; } - private async Task ReadInternalAsync(byte[] buffer, int offset, int count, CancellationToken token) - { - return await _stream.ReadAsync(buffer, 0, count, token).ConfigureAwait(false); - } - private int ReadInternalSync(byte[] buffer, int offset, int count) { return _stream.Read(buffer, 0, count); From f3cc08d17272a80229a2faf8ae8dc707afab2330 Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Fri, 30 Aug 2019 20:40:19 +0100 Subject: [PATCH 2/3] fix SslOverTdsSTream.ReadInternal --- .../src/System/Data/SqlClient/SNI/SslOverTdsStream.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs index 4c4f0ab25323..9632f21ca8bd 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -91,10 +91,13 @@ private async Task ReadInternal(byte[] buffer, int offset, int count, Cance { int readBytes = 0; byte[] packetData = null; - + byte[] readTarget = buffer; + int readOffset = offset; if (_encapsulate) { packetData = ArrayPool.Shared.Rent(count < TdsEnums.HEADER_LEN ? TdsEnums.HEADER_LEN : count); + readTarget = packetData; + readOffset = 0; if (_packetBytes == 0) { // Account for split packets @@ -116,14 +119,13 @@ await _stream.ReadAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, } readBytes = async ? - await _stream.ReadAsync(packetData ?? buffer, 0, count, token).ConfigureAwait(false) : - _stream.Read(packetData ?? buffer, 0, count); + await _stream.ReadAsync(readTarget, readOffset, count, token).ConfigureAwait(false) : + _stream.Read(readTarget, readOffset, count); if (_encapsulate) { _packetBytes -= readBytes; } - if (packetData != null) { Buffer.BlockCopy(packetData, 0, buffer, offset, readBytes); From c48e3ff32d3f2c80f8d066d0cf850838f5fde3ac Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Wed, 4 Sep 2019 18:13:57 +0100 Subject: [PATCH 3/3] remove ReadInternalSync --- .../src/System/Data/SqlClient/SNI/SslOverTdsStream.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs index 9632f21ca8bd..1555a8218d40 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -134,11 +134,6 @@ await _stream.ReadAsync(readTarget, readOffset, count, token).ConfigureAwait(fal return readBytes; } - private int ReadInternalSync(byte[] buffer, int offset, int count) - { - return _stream.Read(buffer, 0, count); - } - /// /// The internal write method calls Sync APIs when Async flag is false ///