Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 30decee

Browse files
Wraith2cheenamalhotra
authored andcommitted
Fix | SqlClient fix for managed encryption connection failure (#40732)
1 parent 5108d92 commit 30decee

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -89,71 +89,51 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
8989
/// </summary>
9090
private async Task<int> ReadInternal(byte[] buffer, int offset, int count, CancellationToken token, bool async)
9191
{
92+
int readBytes = 0;
93+
byte[] packetData = null;
94+
byte[] readTarget = buffer;
95+
int readOffset = offset;
9296
if (_encapsulate)
9397
{
94-
return await ReadInternalEncapsulate(buffer, offset, count, token, async).ConfigureAwait(false);
95-
}
96-
else if (async)
97-
{
98-
return await ReadInternalAsync(buffer, offset, count, token).ConfigureAwait(false);
99-
}
100-
else
101-
{
102-
return ReadInternalSync(buffer, offset, count);
103-
}
104-
}
98+
packetData = ArrayPool<byte>.Shared.Rent(count < TdsEnums.HEADER_LEN ? TdsEnums.HEADER_LEN : count);
99+
readTarget = packetData;
100+
readOffset = 0;
101+
if (_packetBytes == 0)
102+
{
103+
// Account for split packets
104+
while (readBytes < TdsEnums.HEADER_LEN)
105+
{
106+
readBytes += async ?
107+
await _stream.ReadAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, token).ConfigureAwait(false) :
108+
_stream.Read(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes);
109+
}
105110

106-
private async Task<int> ReadInternalEncapsulate(byte[] buffer, int offset, int count, CancellationToken token, bool async)
107-
{
108-
int readBytes = 0;
109-
byte[] packetData = ArrayPool<byte>.Shared.Rent(count < TdsEnums.HEADER_LEN ? TdsEnums.HEADER_LEN : count);
111+
_packetBytes = (packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8) | packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1];
112+
_packetBytes -= TdsEnums.HEADER_LEN;
113+
}
110114

111-
if (_packetBytes == 0)
112-
{
113-
// Account for split packets
114-
while (readBytes < TdsEnums.HEADER_LEN)
115+
if (count > _packetBytes)
115116
{
116-
readBytes += (async ?
117-
await ReadInternalAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, token).ConfigureAwait(false) :
118-
ReadInternalSync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes)
119-
);
117+
count = _packetBytes;
120118
}
121-
122-
_packetBytes = (packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8) | packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1];
123-
_packetBytes -= TdsEnums.HEADER_LEN;
124119
}
125120

126-
if (count > _packetBytes)
121+
readBytes = async ?
122+
await _stream.ReadAsync(readTarget, readOffset, count, token).ConfigureAwait(false) :
123+
_stream.Read(readTarget, readOffset, count);
124+
125+
if (_encapsulate)
127126
{
128-
count = _packetBytes;
127+
_packetBytes -= readBytes;
128+
}
129+
if (packetData != null)
130+
{
131+
Buffer.BlockCopy(packetData, 0, buffer, offset, readBytes);
132+
ArrayPool<byte>.Shared.Return(packetData, clearArray: true);
129133
}
130-
131-
readBytes = (async ?
132-
await ReadInternalAsync(packetData, 0, count, token).ConfigureAwait(false) :
133-
ReadInternalSync(packetData, 0, count)
134-
);
135-
136-
137-
_packetBytes -= readBytes;
138-
139-
Buffer.BlockCopy(packetData, 0, buffer, offset, readBytes);
140-
141-
Array.Clear(packetData, 0, readBytes);
142-
ArrayPool<byte>.Shared.Return(packetData, clearArray: false);
143-
144134
return readBytes;
145135
}
146136

147-
private async Task<int> ReadInternalAsync(byte[] buffer, int offset, int count, CancellationToken token)
148-
{
149-
return await _stream.ReadAsync(buffer, 0, count, token).ConfigureAwait(false);
150-
}
151-
152-
private int ReadInternalSync(byte[] buffer, int offset, int count)
153-
{
154-
return _stream.Read(buffer, 0, count);
155-
}
156-
157137
/// <summary>
158138
/// The internal write method calls Sync APIs when Async flag is false
159139
/// </summary>

0 commit comments

Comments
 (0)