From f6064bb04b9d951f83efdc0727a86b78892d38a9 Mon Sep 17 00:00:00 2001 From: panoskj Date: Sat, 19 Mar 2022 18:32:55 +0200 Subject: [PATCH] Merging of TdsParserStateObject.ProcessSniPacket method. --- .../Data/SqlClient/TdsParserStateObject.cs | 73 ----------------- .../Data/SqlClient/TdsParserStateObject.cs | 72 +---------------- .../Data/SqlClient/TdsParserStateObject.cs | 80 +++++++++++++++++++ 3 files changed, 83 insertions(+), 142 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index d54f872020..308bc4be80 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -350,79 +350,6 @@ internal void ResetSnapshot() _snapshotReplay = false; } - public void ProcessSniPacket(PacketHandle packet, uint error) - { - if (error != 0) - { - if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken)) - { - // Do nothing with callback if closed or broken and error not 0 - callback can occur - // after connection has been closed. PROBLEM IN NETLIB - DESIGN FLAW. - return; - } - - AddError(_parser.ProcessSNIError(this)); - AssertValidState(); - } - else - { - uint dataSize = 0; - - uint getDataError = SNIPacketGetData(packet, _inBuff, ref dataSize); - - if (getDataError == TdsEnums.SNI_SUCCESS) - { - if (_inBuff.Length < dataSize) - { - Debug.Assert(true, "Unexpected dataSize on Read"); - throw SQL.InvalidInternalPacketSize(StringsHelper.GetString(Strings.SqlMisc_InvalidArraySizeMessage)); - } - - _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks; - _inBytesRead = (int)dataSize; - _inBytesUsed = 0; - - if (_snapshot != null) - { - _snapshot.PushBuffer(_inBuff, _inBytesRead); - if (_snapshotReplay) - { - _snapshot.Replay(); -#if DEBUG - _snapshot.AssertCurrent(); -#endif - } - } - - SniReadStatisticsAndTracing(); - - - AssertValidState(); - } - else - { - throw SQL.ParsingError(); - } - } - } - - private void ChangeNetworkPacketTimeout(int dueTime, int period) - { - Timer networkPacketTimeout = _networkPacketTimeout; - if (networkPacketTimeout != null) - { - try - { - networkPacketTimeout.Change(dueTime, period); - } - catch (ObjectDisposedException) - { - // _networkPacketTimeout is set to null before Disposing, but there is still a slight chance - // that object was disposed after we took a copy - } - } - } - private void SetBufferSecureStrings() { if (_securePasswords != null) diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index 65084c13ac..a8be809f89 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -494,75 +494,9 @@ private uint CheckConnection() return handle == null ? TdsEnums.SNI_SUCCESS : SNINativeMethodWrapper.SNICheckConnection(handle); } - // TODO: - does this need to be MUSTRUN??? - public void ProcessSniPacket(IntPtr packet, UInt32 error) - { - if (error != 0) - { - if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken)) - { - // Do nothing with callback if closed or broken and error not 0 - callback can occur - // after connection has been closed. PROBLEM IN NETLIB - DESIGN FLAW. - return; - } - - AddError(_parser.ProcessSNIError(this)); - AssertValidState(); - } - else - { - UInt32 dataSize = 0; - UInt32 getDataError = SNINativeMethodWrapper.SNIPacketGetData(packet, _inBuff, ref dataSize); - - if (getDataError == TdsEnums.SNI_SUCCESS) - { - if (_inBuff.Length < dataSize) - { - Debug.Assert(true, "Unexpected dataSize on Read"); - throw SQL.InvalidInternalPacketSize(StringsHelper.GetString(Strings.SqlMisc_InvalidArraySizeMessage)); - } - - _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks; - _inBytesRead = (int)dataSize; - _inBytesUsed = 0; - - if (_snapshot != null) - { - _snapshot.PushBuffer(_inBuff, _inBytesRead); - if (_snapshotReplay) - { - _snapshot.Replay(); -#if DEBUG - _snapshot.AssertCurrent(); -#endif - } - } - SniReadStatisticsAndTracing(); - SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.ReadNetworkPacketAsyncCallback | INFO | ADV | State Object Id {0}, Packet read. In Buffer {1}, In Bytes Read: {2}", ObjectID, _inBuff, (ushort)_inBytesRead); - AssertValidState(); - } - else - { - throw SQL.ParsingError(ParsingErrorState.ProcessSniPacketFailed); - } - } - } - - private void ChangeNetworkPacketTimeout(int dueTime, int period) - { - Timer networkPacketTimeout = _networkPacketTimeout; - if (networkPacketTimeout != null) - { - try - { - networkPacketTimeout.Change(dueTime, period); - } - catch (ObjectDisposedException) - { - // _networkPacketTimeout is set to null before Disposing, but there is still a slight chance - // that object was disposed after we took a copy - } - } + private uint SNIPacketGetData(IntPtr packet, byte[] _inBuff, ref uint dataSize) + { + return SNINativeMethodWrapper.SNIPacketGetData(packet, _inBuff, ref dataSize); } public void ReadAsyncCallback(IntPtr key, IntPtr packet, UInt32 error) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index c3da96190a..025613df80 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -2555,5 +2555,85 @@ private void ReadSniError(TdsParserStateObject stateObj, uint error) AssertValidState(); } + + public void ProcessSniPacket(PacketHandle packet, uint error) + { + if (error != 0) + { + if ((_parser.State == TdsParserState.Closed) || (_parser.State == TdsParserState.Broken)) + { + // Do nothing with callback if closed or broken and error not 0 - callback can occur + // after connection has been closed. PROBLEM IN NETLIB - DESIGN FLAW. + return; + } + + AddError(_parser.ProcessSNIError(this)); + AssertValidState(); + } + else + { + uint dataSize = 0; + + uint getDataError = SNIPacketGetData(packet, _inBuff, ref dataSize); + + if (getDataError == TdsEnums.SNI_SUCCESS) + { + if (_inBuff.Length < dataSize) + { + Debug.Assert(true, "Unexpected dataSize on Read"); + throw SQL.InvalidInternalPacketSize(StringsHelper.GetString(Strings.SqlMisc_InvalidArraySizeMessage)); + } + + _lastSuccessfulIOTimer._value = DateTime.UtcNow.Ticks; + _inBytesRead = (int)dataSize; + _inBytesUsed = 0; + + if (_snapshot != null) + { + _snapshot.PushBuffer(_inBuff, _inBytesRead); + if (_snapshotReplay) + { + _snapshot.Replay(); +#if DEBUG + _snapshot.AssertCurrent(); +#endif + } + } + + SniReadStatisticsAndTracing(); + +#if NETFRAMEWORK + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.ReadNetworkPacketAsyncCallback | INFO | ADV | State Object Id {0}, Packet read. In Buffer {1}, In Bytes Read: {2}", ObjectID, _inBuff, (ushort)_inBytesRead); +#endif + + AssertValidState(); + } + else + { +#if NETFRAMEWORK + throw SQL.ParsingError(ParsingErrorState.ProcessSniPacketFailed); +#else + throw SQL.ParsingError(); +#endif + } + } + } + + private void ChangeNetworkPacketTimeout(int dueTime, int period) + { + Timer networkPacketTimeout = _networkPacketTimeout; + if (networkPacketTimeout != null) + { + try + { + networkPacketTimeout.Change(dueTime, period); + } + catch (ObjectDisposedException) + { + // _networkPacketTimeout is set to null before Disposing, but there is still a slight chance + // that object was disposed after we took a copy + } + } + } } }