Skip to content

Commit 43ec92d

Browse files
committed
move write methods that are the same to shared file
1 parent b00a17c commit 43ec92d

File tree

4 files changed

+155
-284
lines changed

4 files changed

+155
-284
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.netcore.cs

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -842,145 +842,6 @@ internal void WriteByte(byte b)
842842
_outBuff[_outBytesUsed++] = b;
843843
}
844844

845-
internal Task WriteByteSpan(ReadOnlySpan<byte> span, bool canAccumulate = true, TaskCompletionSource<object> completion = null)
846-
{
847-
return WriteBytes(span, span.Length, 0, canAccumulate, completion);
848-
}
849-
850-
internal Task WriteByteArray(byte[] b, int len, int offsetBuffer, bool canAccumulate = true, TaskCompletionSource<object> completion = null)
851-
{
852-
return WriteBytes(ReadOnlySpan<byte>.Empty, len, offsetBuffer, canAccumulate, completion, b);
853-
}
854-
855-
//
856-
// Takes a span or a byte array and writes it to the buffer
857-
// If you pass in a span and a null array then the span wil be used.
858-
// If you pass in a non-null array then the array will be used and the span is ignored.
859-
// if the span cannot be written into the current packet then the remaining contents of the span are copied to a
860-
// new heap allocated array that will used to callback into the method to continue the write operation.
861-
private Task WriteBytes(ReadOnlySpan<byte> b, int len, int offsetBuffer, bool canAccumulate = true, TaskCompletionSource<object> completion = null, byte[] array = null)
862-
{
863-
if (array != null)
864-
{
865-
b = new ReadOnlySpan<byte>(array, offsetBuffer, len);
866-
}
867-
try
868-
{
869-
TdsParser.ReliabilitySection.Assert("unreliable call to WriteByteArray"); // you need to setup for a thread abort somewhere before you call this method
870-
871-
bool async = _parser._asyncWrite; // NOTE: We are capturing this now for the assert after the Task is returned, since WritePacket will turn off async if there is an exception
872-
Debug.Assert(async || _asyncWriteCount == 0);
873-
// Do we have to send out in packet size chunks, or can we rely on netlib layer to break it up?
874-
// would prefer to do something like:
875-
//
876-
// if (len > what we have room for || len > out buf)
877-
// flush buffer
878-
// UnsafeNativeMethods.Write(b)
879-
//
880-
881-
int offset = offsetBuffer;
882-
883-
Debug.Assert(b.Length >= len, "Invalid length sent to WriteBytes()!");
884-
885-
// loop through and write the entire array
886-
do
887-
{
888-
if ((_outBytesUsed + len) > _outBuff.Length)
889-
{
890-
// If the remainder of the data won't fit into the buffer, then we have to put
891-
// whatever we can into the buffer, and flush that so we can then put more into
892-
// the buffer on the next loop of the while.
893-
894-
int remainder = _outBuff.Length - _outBytesUsed;
895-
896-
// write the remainder
897-
Span<byte> copyTo = _outBuff.AsSpan(_outBytesUsed, remainder);
898-
ReadOnlySpan<byte> copyFrom = b.Slice(0, remainder);
899-
900-
Debug.Assert(copyTo.Length == copyFrom.Length, $"copyTo.Length:{copyTo.Length} and copyFrom.Length{copyFrom.Length:D} should be the same");
901-
902-
copyFrom.CopyTo(copyTo);
903-
904-
offset += remainder;
905-
_outBytesUsed += remainder;
906-
len -= remainder;
907-
b = b.Slice(remainder, len);
908-
909-
Task packetTask = WritePacket(TdsEnums.SOFTFLUSH, canAccumulate);
910-
911-
if (packetTask != null)
912-
{
913-
Task task = null;
914-
Debug.Assert(async, "Returned task in sync mode");
915-
if (completion == null)
916-
{
917-
completion = new TaskCompletionSource<object>();
918-
task = completion.Task; // we only care about return from topmost call, so do not access Task property in other cases
919-
}
920-
921-
if (array == null)
922-
{
923-
byte[] tempArray = new byte[len];
924-
Span<byte> copyTempTo = tempArray.AsSpan();
925-
926-
Debug.Assert(copyTempTo.Length == b.Length, $"copyTempTo.Length:{copyTempTo.Length} and copyTempFrom.Length:{b.Length:D} should be the same");
927-
928-
b.CopyTo(copyTempTo);
929-
array = tempArray;
930-
offset = 0;
931-
}
932-
933-
WriteBytesSetupContinuation(array, len, completion, offset, packetTask);
934-
return task;
935-
}
936-
}
937-
else
938-
{
939-
//((stateObj._outBytesUsed + len) <= stateObj._outBuff.Length )
940-
// Else the remainder of the string will fit into the buffer, so copy it into the
941-
// buffer and then break out of the loop.
942-
943-
Span<byte> copyTo = _outBuff.AsSpan(_outBytesUsed, len);
944-
ReadOnlySpan<byte> copyFrom = b.Slice(0, len);
945-
946-
Debug.Assert(copyTo.Length == copyFrom.Length, $"copyTo.Length:{copyTo.Length} and copyFrom.Length:{copyFrom.Length:D} should be the same");
947-
948-
copyFrom.CopyTo(copyTo);
949-
950-
// handle out buffer bytes used counter
951-
_outBytesUsed += len;
952-
break;
953-
}
954-
} while (len > 0);
955-
956-
if (completion != null)
957-
{
958-
completion.SetResult(null);
959-
}
960-
return null;
961-
}
962-
catch (Exception e)
963-
{
964-
if (completion != null)
965-
{
966-
completion.SetException(e);
967-
return null;
968-
}
969-
else
970-
{
971-
throw;
972-
}
973-
}
974-
}
975-
976-
// This is in its own method to avoid always allocating the lambda in WriteBytes
977-
private void WriteBytesSetupContinuation(byte[] array, int len, TaskCompletionSource<object> completion, int offset, Task packetTask)
978-
{
979-
AsyncHelper.ContinueTask(packetTask, completion,
980-
onSuccess: () => WriteBytes(ReadOnlySpan<byte>.Empty, len: len, offsetBuffer: offset, canAccumulate: false, completion: completion, array)
981-
);
982-
}
983-
984845
// Dumps contents of buffer to SNI for network write.
985846
internal Task WritePacket(byte flushMode, bool canAccumulate = false)
986847
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -931,145 +931,6 @@ internal void WriteByte(byte b)
931931
// set byte in buffer and increment the counter for number of bytes used in the out buffer
932932
_outBuff[_outBytesUsed++] = b;
933933
}
934-
internal Task WriteByteSpan(ReadOnlySpan<byte> span, bool canAccumulate = true, TaskCompletionSource<object> completion = null)
935-
{
936-
return WriteBytes(span, span.Length, 0, canAccumulate, completion);
937-
}
938-
939-
internal Task WriteByteArray(byte[] b, int len, int offsetBuffer, bool canAccumulate = true, TaskCompletionSource<object> completion = null)
940-
{
941-
return WriteBytes(ReadOnlySpan<byte>.Empty, len, offsetBuffer, canAccumulate, completion, b);
942-
}
943-
944-
//
945-
// Takes a span or a byte array and writes it to the buffer
946-
// If you pass in a span and a null array then the span wil be used.
947-
// If you pass in a non-null array then the array will be used and the span is ignored.
948-
// if the span cannot be written into the current packet then the remaining contents of the span are copied to a
949-
// new heap allocated array that will used to callback into the method to continue the write operation.
950-
private Task WriteBytes(ReadOnlySpan<byte> b, int len, int offsetBuffer, bool canAccumulate = true, TaskCompletionSource<object> completion = null, byte[] array = null)
951-
{
952-
if (array != null)
953-
{
954-
b = new ReadOnlySpan<byte>(array, offsetBuffer, len);
955-
}
956-
try
957-
{
958-
TdsParser.ReliabilitySection.Assert("unreliable call to WriteByteArray"); // you need to setup for a thread abort somewhere before you call this method
959-
960-
bool async = _parser._asyncWrite; // NOTE: We are capturing this now for the assert after the Task is returned, since WritePacket will turn off async if there is an exception
961-
Debug.Assert(async || _asyncWriteCount == 0);
962-
// Do we have to send out in packet size chunks, or can we rely on netlib layer to break it up?
963-
// would prefer to do something like:
964-
//
965-
// if (len > what we have room for || len > out buf)
966-
// flush buffer
967-
// UnsafeNativeMethods.Write(b)
968-
//
969-
970-
int offset = offsetBuffer;
971-
972-
Debug.Assert(b.Length >= len, "Invalid length sent to WriteBytes()!");
973-
974-
// loop through and write the entire array
975-
do
976-
{
977-
if ((_outBytesUsed + len) > _outBuff.Length)
978-
{
979-
// If the remainder of the data won't fit into the buffer, then we have to put
980-
// whatever we can into the buffer, and flush that so we can then put more into
981-
// the buffer on the next loop of the while.
982-
983-
int remainder = _outBuff.Length - _outBytesUsed;
984-
985-
// write the remainder
986-
Span<byte> copyTo = _outBuff.AsSpan(_outBytesUsed, remainder);
987-
ReadOnlySpan<byte> copyFrom = b.Slice(0, remainder);
988-
989-
Debug.Assert(copyTo.Length == copyFrom.Length, $"copyTo.Length:{copyTo.Length} and copyFrom.Length{copyFrom.Length:D} should be the same");
990-
991-
copyFrom.CopyTo(copyTo);
992-
993-
offset += remainder;
994-
_outBytesUsed += remainder;
995-
len -= remainder;
996-
b = b.Slice(remainder, len);
997-
998-
Task packetTask = WritePacket(TdsEnums.SOFTFLUSH, canAccumulate);
999-
1000-
if (packetTask != null)
1001-
{
1002-
Task task = null;
1003-
Debug.Assert(async, "Returned task in sync mode");
1004-
if (completion == null)
1005-
{
1006-
completion = new TaskCompletionSource<object>();
1007-
task = completion.Task; // we only care about return from topmost call, so do not access Task property in other cases
1008-
}
1009-
1010-
if (array == null)
1011-
{
1012-
byte[] tempArray = new byte[len];
1013-
Span<byte> copyTempTo = tempArray.AsSpan();
1014-
1015-
Debug.Assert(copyTempTo.Length == b.Length, $"copyTempTo.Length:{copyTempTo.Length} and copyTempFrom.Length:{b.Length:D} should be the same");
1016-
1017-
b.CopyTo(copyTempTo);
1018-
array = tempArray;
1019-
offset = 0;
1020-
}
1021-
1022-
WriteBytesSetupContinuation(array, len, completion, offset, packetTask);
1023-
return task;
1024-
}
1025-
}
1026-
else
1027-
{
1028-
//((stateObj._outBytesUsed + len) <= stateObj._outBuff.Length )
1029-
// Else the remainder of the string will fit into the buffer, so copy it into the
1030-
// buffer and then break out of the loop.
1031-
1032-
Span<byte> copyTo = _outBuff.AsSpan(_outBytesUsed, len);
1033-
ReadOnlySpan<byte> copyFrom = b.Slice(0, len);
1034-
1035-
Debug.Assert(copyTo.Length == copyFrom.Length, $"copyTo.Length:{copyTo.Length} and copyFrom.Length:{copyFrom.Length:D} should be the same");
1036-
1037-
copyFrom.CopyTo(copyTo);
1038-
1039-
// handle out buffer bytes used counter
1040-
_outBytesUsed += len;
1041-
break;
1042-
}
1043-
} while (len > 0);
1044-
1045-
if (completion != null)
1046-
{
1047-
completion.SetResult(null);
1048-
}
1049-
return null;
1050-
}
1051-
catch (Exception e)
1052-
{
1053-
if (completion != null)
1054-
{
1055-
completion.SetException(e);
1056-
return null;
1057-
}
1058-
else
1059-
{
1060-
throw;
1061-
}
1062-
}
1063-
}
1064-
1065-
// This is in its own method to avoid always allocating the lambda in WriteByteArray
1066-
private void WriteBytesSetupContinuation(byte[] b, int len, TaskCompletionSource<object> completion, int offset, Task packetTask)
1067-
{
1068-
AsyncHelper.ContinueTask(packetTask, completion,
1069-
() => WriteByteArray(b, len: len, offsetBuffer: offset, canAccumulate: false, completion: completion),
1070-
connectionToDoom: _parser.Connection
1071-
);
1072-
}
1073934

1074935
// Dumps contents of buffer to SNI for network write.
1075936
internal Task WritePacket(byte flushMode, bool canAccumulate = false)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SSPI/NegotiateSSPIContextProvider.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,35 @@ namespace Microsoft.Data.SqlClient
1111
{
1212
internal sealed class NegotiateSSPIContextProvider : SSPIContextProvider
1313
{
14+
private NegotiateAuthentication? _negotiateAuth = null;
15+
1416
protected override void GenerateSspiClientContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, byte[][] _sniSpnBuffer)
1517
{
18+
NegotiateAuthenticationStatusCode statusCode = NegotiateAuthenticationStatusCode.UnknownCredentials;
19+
1620
for (int i = 0; i < _sniSpnBuffer.Length; i++)
1721
{
18-
var negotiateAuth = new NegotiateAuthentication(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[i]) });
19-
var sendBuff = negotiateAuth.GetOutgoingBlob(incomingBlob, out var statusCode)!;
22+
_negotiateAuth ??= new(new NegotiateAuthenticationClientOptions { Package = "Negotiate", TargetName = Encoding.Unicode.GetString(_sniSpnBuffer[i]) });
23+
var sendBuff = _negotiateAuth.GetOutgoingBlob(incomingBlob, out statusCode)!;
2024

2125
// Log session id, status code and the actual SPN used in the negotiation
2226
SqlClientEventSource.Log.TryTraceEvent("{0}.{1} | Info | Session Id {2}, StatusCode={3}, SPN={4}", nameof(NegotiateSSPIContextProvider),
23-
nameof(GenerateSspiClientContext), _physicalStateObj.SessionId, statusCode, negotiateAuth.TargetName);
24-
27+
nameof(GenerateSspiClientContext), _physicalStateObj.SessionId, statusCode, _negotiateAuth.TargetName);
2528
if (statusCode == NegotiateAuthenticationStatusCode.Completed || statusCode == NegotiateAuthenticationStatusCode.ContinueNeeded)
2629
{
2730
outgoingBlobWriter.Write(sendBuff);
28-
return; // Successful case, exit the loop with current SPN.
31+
break; // Successful case, exit the loop with current SPN.
32+
}
33+
else
34+
{
35+
_negotiateAuth = null; // Reset _negotiateAuth to be generated again for next SPN.
2936
}
3037
}
3138

32-
throw new InvalidOperationException(SQLMessage.SSPIGenerateError());
39+
if (statusCode is not NegotiateAuthenticationStatusCode.Completed and not NegotiateAuthenticationStatusCode.ContinueNeeded)
40+
{
41+
throw new InvalidOperationException(SQLMessage.SSPIGenerateError() + Environment.NewLine + statusCode);
42+
}
3343
}
3444
}
3545
}

0 commit comments

Comments
 (0)