Skip to content

Commit

Permalink
2. Merging "Buffer read" methods of TdsParserStateObject, port dotnet…
Browse files Browse the repository at this point in the history
…#667 to netfx.
  • Loading branch information
panoskj committed Oct 25, 2023
1 parent 77f6992 commit 019943d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
Expand Up @@ -313,6 +313,9 @@ internal bool TryReadInt16(out short value)

internal bool TryReadInt32(out int value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadInt32"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
Span<byte> buffer = stackalloc byte[4];
if (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4))
Expand Down Expand Up @@ -342,6 +345,9 @@ internal bool TryReadInt32(out int value)
// This method is safe to call when doing async without snapshot
internal bool TryReadInt64(out long value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadInt64"); // you need to setup for a thread abort somewhere before you call this method
#endif
if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead))
{
if (!TryPrepareBuffer())
Expand Down Expand Up @@ -420,6 +426,9 @@ internal bool TryReadUInt16(out ushort value)
// This method is safe to call when doing async without replay
internal bool TryReadUInt32(out uint value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadUInt32"); // you need to setup for a thread abort somewhere before you call this method
#endif
if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead))
{
if (!TryPrepareBuffer())
Expand Down Expand Up @@ -468,6 +477,9 @@ internal bool TryReadUInt32(out uint value)

internal bool TryReadSingle(out float value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadSingle"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
if (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4))
{
Expand Down Expand Up @@ -501,6 +513,9 @@ internal bool TryReadSingle(out float value)

internal bool TryReadDouble(out double value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadDouble"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
if (((_inBytesUsed + 8) > _inBytesRead) || (_inBytesPacket < 8))
{
Expand Down Expand Up @@ -534,6 +549,9 @@ internal bool TryReadDouble(out double value)

internal bool TryReadString(int length, out string value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadString"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
int cBytes = length << 1;
byte[] buf;
Expand Down Expand Up @@ -574,6 +592,9 @@ internal bool TryReadString(int length, out string value)

internal bool TryReadStringWithEncoding(int length, System.Text.Encoding encoding, bool isPlp, out string value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadStringWithEncoding"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");

if (null == encoding)
Expand Down
Expand Up @@ -380,75 +380,68 @@ internal bool TryReadInt16(out short value)
{
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");

byte[] buffer;
int offset;
Span<byte> buffer = stackalloc byte[2];
if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2))
{
// If the int16 isn't fully in the buffer, or if it isn't fully in the packet,
// then use ReadByteArray since the logic is there to take care of that.
if (!TryReadByteArray(_bTmp, 2))
if (!TryReadByteArray(buffer, 2))
{
value = default;
return false;
}

buffer = _bTmp;
offset = 0;
}
else
{
// The entire int16 is in the packet and in the buffer, so just return it
// and take care of the counters.

buffer = _inBuff;
offset = _inBytesUsed;

buffer = _inBuff.AsSpan(_inBytesUsed, 2);
_inBytesUsed += 2;
_inBytesPacket -= 2;
}

AssertValidState();
value = (short)((buffer[offset + 1] << 8) + buffer[offset]);
value = (short)((buffer[1] << 8) + buffer[0]);
return true;
}

internal bool TryReadInt32(out int value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadInt32"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
Span<byte> buffer = stackalloc byte[4];
if (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4))
{
// If the int isn't fully in the buffer, or if it isn't fully in the packet,
// then use ReadByteArray since the logic is there to take care of that.
if (!TryReadByteArray(_bTmp, 4))
if (!TryReadByteArray(buffer, 4))
{
value = 0;
return false;
}

AssertValidState();
value = BitConverter.ToInt32(_bTmp, 0);
return true;
}
else
{
// The entire int is in the packet and in the buffer, so just return it
// and take care of the counters.

value = BitConverter.ToInt32(_inBuff, _inBytesUsed);

buffer = _inBuff.AsSpan(_inBytesUsed, 4);
_inBytesUsed += 4;
_inBytesPacket -= 4;
}

AssertValidState();
value = (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) + buffer[0];
return true;
}
}

// This method is safe to call when doing async without snapshot
internal bool TryReadInt64(out long value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadInt64"); // you need to setup for a thread abort somewhere before you call this method
#endif
if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead))
{
if (!TryPrepareBuffer())
Expand Down Expand Up @@ -499,43 +492,37 @@ internal bool TryReadUInt16(out ushort value)
{
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");

byte[] buffer;
int offset;
Span<byte> buffer = stackalloc byte[2];
if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2))
{
// If the uint16 isn't fully in the buffer, or if it isn't fully in the packet,
// then use ReadByteArray since the logic is there to take care of that.

if (!TryReadByteArray(_bTmp, 2))
if (!TryReadByteArray(buffer, 2))
{
value = default;
return false;
}

buffer = _bTmp;
offset = 0;
}
else
{
// The entire uint16 is in the packet and in the buffer, so just return it
// and take care of the counters.

buffer = _inBuff;
offset = _inBytesUsed;

buffer = _inBuff.AsSpan(_inBytesUsed, 2);
_inBytesUsed += 2;
_inBytesPacket -= 2;
}

AssertValidState();
value = (ushort)((buffer[offset + 1] << 8) + buffer[offset]);
value = (ushort)((buffer[1] << 8) + buffer[0]);
return true;
}

// This method is safe to call when doing async without replay
internal bool TryReadUInt32(out uint value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadUInt32"); // you need to setup for a thread abort somewhere before you call this method
#endif
if ((_inBytesPacket == 0) || (_inBytesUsed == _inBytesRead))
{
if (!TryPrepareBuffer())
Expand Down Expand Up @@ -584,7 +571,9 @@ internal bool TryReadUInt32(out uint value)

internal bool TryReadSingle(out float value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadSingle"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
if (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4))
{
Expand Down Expand Up @@ -618,7 +607,9 @@ internal bool TryReadSingle(out float value)

internal bool TryReadDouble(out double value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadDouble"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
if (((_inBytesUsed + 8) > _inBytesRead) || (_inBytesPacket < 8))
{
Expand Down Expand Up @@ -652,7 +643,9 @@ internal bool TryReadDouble(out double value)

internal bool TryReadString(int length, out string value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadString"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
int cBytes = length << 1;
byte[] buf;
Expand Down Expand Up @@ -693,7 +686,9 @@ internal bool TryReadString(int length, out string value)

internal bool TryReadStringWithEncoding(int length, System.Text.Encoding encoding, bool isPlp, out string value)
{
#if NETFRAMEWORK
TdsParser.ReliabilitySection.Assert("unreliable call to ReadStringWithEncoding"); // you need to setup for a thread abort somewhere before you call this method
#endif
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");

if (null == encoding)
Expand Down

0 comments on commit 019943d

Please sign in to comment.