Skip to content

Commit ad31bb8

Browse files
authored
Rework MARSConnection state machine (#933)
1 parent 6a5a670 commit ad31bb8

File tree

11 files changed

+679
-386
lines changed

11 files changed

+679
-386
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@
534534
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIMarsQueuedPacket.cs" />
535535
<Compile Include="Microsoft\Data\SqlClient\SNI\SNINpHandle.cs" />
536536
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPacket.cs" />
537+
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPacket.Debug.cs" />
537538
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPacketPool.cs" />
538539
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIPhysicalHandle.cs" />
539540
<Compile Include="Microsoft\Data\SqlClient\SNI\SNIProxy.cs" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,63 +35,94 @@ internal enum SNIProviders
3535
/// <summary>
3636
/// SMUX packet header
3737
/// </summary>
38-
internal sealed class SNISMUXHeader
38+
internal struct SNISMUXHeader
3939
{
4040
public const int HEADER_LENGTH = 16;
4141

42-
public byte SMID;
43-
public byte flags;
44-
public ushort sessionId;
45-
public uint length;
46-
public uint sequenceNumber;
47-
public uint highwater;
42+
public byte SMID { get; private set; }
43+
public byte Flags { get; private set; }
44+
public ushort SessionId { get; private set; }
45+
public uint Length { get; private set; }
46+
public uint SequenceNumber { get; private set; }
47+
public uint Highwater { get; private set; }
48+
49+
public void Set(byte smid, byte flags, ushort sessionID, uint length, uint sequenceNumber, uint highwater)
50+
{
51+
SMID = smid;
52+
Flags = flags;
53+
SessionId = sessionID;
54+
Length = length;
55+
SequenceNumber = sequenceNumber;
56+
Highwater = highwater;
57+
}
4858

4959
public void Read(byte[] bytes)
5060
{
5161
SMID = bytes[0];
52-
flags = bytes[1];
53-
sessionId = BitConverter.ToUInt16(bytes, 2);
54-
length = BitConverter.ToUInt32(bytes, 4) - SNISMUXHeader.HEADER_LENGTH;
55-
sequenceNumber = BitConverter.ToUInt32(bytes, 8);
56-
highwater = BitConverter.ToUInt32(bytes, 12);
62+
Flags = bytes[1];
63+
SessionId = BitConverter.ToUInt16(bytes, 2);
64+
Length = BitConverter.ToUInt32(bytes, 4) - SNISMUXHeader.HEADER_LENGTH;
65+
SequenceNumber = BitConverter.ToUInt32(bytes, 8);
66+
Highwater = BitConverter.ToUInt32(bytes, 12);
5767
}
5868

5969
public void Write(Span<byte> bytes)
6070
{
61-
uint value = highwater;
71+
uint value = Highwater;
6272
// access the highest element first to cause the largest range check in the jit, then fill in the rest of the value and carry on as normal
6373
bytes[15] = (byte)((value >> 24) & 0xff);
6474
bytes[12] = (byte)(value & 0xff); // BitConverter.GetBytes(_currentHeader.highwater).CopyTo(headerBytes, 12);
6575
bytes[13] = (byte)((value >> 8) & 0xff);
6676
bytes[14] = (byte)((value >> 16) & 0xff);
6777

6878
bytes[0] = SMID; // BitConverter.GetBytes(_currentHeader.SMID).CopyTo(headerBytes, 0);
69-
bytes[1] = flags; // BitConverter.GetBytes(_currentHeader.flags).CopyTo(headerBytes, 1);
79+
bytes[1] = Flags; // BitConverter.GetBytes(_currentHeader.flags).CopyTo(headerBytes, 1);
7080

71-
value = sessionId;
81+
value = SessionId;
7282
bytes[2] = (byte)(value & 0xff); // BitConverter.GetBytes(_currentHeader.sessionId).CopyTo(headerBytes, 2);
7383
bytes[3] = (byte)((value >> 8) & 0xff);
7484

75-
value = length;
85+
value = Length;
7686
bytes[4] = (byte)(value & 0xff); // BitConverter.GetBytes(_currentHeader.length).CopyTo(headerBytes, 4);
7787
bytes[5] = (byte)((value >> 8) & 0xff);
7888
bytes[6] = (byte)((value >> 16) & 0xff);
7989
bytes[7] = (byte)((value >> 24) & 0xff);
8090

81-
value = sequenceNumber;
91+
value = SequenceNumber;
8292
bytes[8] = (byte)(value & 0xff); // BitConverter.GetBytes(_currentHeader.sequenceNumber).CopyTo(headerBytes, 8);
8393
bytes[9] = (byte)((value >> 8) & 0xff);
8494
bytes[10] = (byte)((value >> 16) & 0xff);
8595
bytes[11] = (byte)((value >> 24) & 0xff);
8696

8797
}
98+
99+
public SNISMUXHeader Clone()
100+
{
101+
SNISMUXHeader copy = new SNISMUXHeader();
102+
copy.SMID = SMID;
103+
copy.Flags = Flags;
104+
copy.SessionId = SessionId;
105+
copy.Length = Length;
106+
copy.SequenceNumber = SequenceNumber;
107+
copy.Highwater = Highwater;
108+
return copy;
109+
}
110+
111+
public void Clear()
112+
{
113+
SMID = 0;
114+
Flags = 0;
115+
SessionId = 0;
116+
Length = 0;
117+
SequenceNumber = 0;
118+
Highwater = 0;
119+
}
88120
}
89121

90122
/// <summary>
91123
/// SMUX packet flags
92124
/// </summary>
93-
[Flags]
94-
internal enum SNISMUXFlags
125+
internal enum SNISMUXFlags : uint
95126
{
96127
SMUX_SYN = 1, // Begin SMUX connection
97128
SMUX_ACK = 2, // Acknowledge SMUX packets

0 commit comments

Comments
 (0)