Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge common code bases for TdsParserStateObject.cs (5) #2302

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -262,38 +262,6 @@ internal void StartSession(object cancellationOwner)
_cancellationOwner.Target = cancellationOwner;
}

/// <summary>
/// Checks to see if the underlying connection is still valid (used by idle connection resiliency - for active connections)
/// NOTE: This is not safe to do on a connection that is currently in use
/// NOTE: This will mark the connection as broken if it is found to be dead
/// </summary>
/// <returns>True if the connection is still alive, otherwise false</returns>
internal bool ValidateSNIConnection()
{
if ((_parser == null) || ((_parser.State == TdsParserState.Broken) || (_parser.State == TdsParserState.Closed)))
{
return false;
}

if (DateTime.UtcNow.Ticks - _lastSuccessfulIOTimer._value <= CheckConnectionWindow)
{
return true;
}

uint error = TdsEnums.SNI_SUCCESS;
SniContext = SniContext.Snix_Connect;
try
{
Interlocked.Increment(ref _readingCount);
error = CheckConnection();
}
finally
{
Interlocked.Decrement(ref _readingCount);
}
return (error == TdsEnums.SNI_SUCCESS) || (error == TdsEnums.SNI_WAIT_TIMEOUT);
}

// This method should only be called by ReadSni! If not - it may have problems with timeouts!
private void ReadSniError(TdsParserStateObject stateObj, uint error)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,12 @@ internal PacketHandle ReadAsync(SessionHandle handle, out uint error)
return readPacket;
}

internal uint CheckConnection() => SNINativeMethodWrapper.SNICheckConnection(Handle);
/// <remarks>
/// Note <see cref="Handle"/> is not supposed to be null when this method is called and the native implementation doesn't expect null values here.
/// The handle will not be null unless this <see cref="TdsParserStateObject"/> instance has been disposed.
/// We could probably throw an exception instead of returning success (like managed netcore version does).
/// </remarks>
internal uint CheckConnection() => Handle == null ? TdsEnums.SNI_SUCCESS : SNINativeMethodWrapper.SNICheckConnection(Handle);

internal void ReleasePacket(PacketHandle syncReadPacket) => SNINativeMethodWrapper.SNIPacketRelease(syncReadPacket);

Expand Down Expand Up @@ -387,42 +392,6 @@ internal void StartSession(int objectID)
_allowObjectID = objectID;
}

/// <summary>
/// Checks to see if the underlying connection is still valid (used by idle connection resiliency - for active connections)
/// NOTE: This is not safe to do on a connection that is currently in use
/// NOTE: This will mark the connection as broken if it is found to be dead
/// </summary>
/// <returns>True if the connection is still alive, otherwise false</returns>
internal bool ValidateSNIConnection()
{
if ((_parser == null) || ((_parser.State == TdsParserState.Broken) || (_parser.State == TdsParserState.Closed)))
{
return false;
}

if (DateTime.UtcNow.Ticks - _lastSuccessfulIOTimer._value <= CheckConnectionWindow)
{
return true;
}

uint error = TdsEnums.SNI_SUCCESS;
SniContext = SniContext.Snix_Connect;
try
{
Interlocked.Increment(ref _readingCount);
SNIHandle handle = Handle;
if (handle != null)
{
error = SNINativeMethodWrapper.SNICheckConnection(handle);
}
}
finally
{
Interlocked.Decrement(ref _readingCount);
}
return (error == TdsEnums.SNI_SUCCESS) || (error == TdsEnums.SNI_WAIT_TIMEOUT);
}

// This method should only be called by ReadSni! If not - it may have problems with timeouts!
private void ReadSniError(TdsParserStateObject stateObj, uint error)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,37 @@ internal bool IsConnectionAlive(bool throwOnException)
return isAlive;
}

/// <summary>
/// Checks to see if the underlying connection is still valid (used by idle connection resiliency - for active connections)
/// NOTE: This is not safe to do on a connection that is currently in use
/// NOTE: This will mark the connection as broken if it is found to be dead
/// </summary>
/// <returns>True if the connection is still alive, otherwise false</returns>
internal bool ValidateSNIConnection()
{
if ((_parser == null) || ((_parser.State == TdsParserState.Broken) || (_parser.State == TdsParserState.Closed)))
{
return false;
}

if (DateTime.UtcNow.Ticks - _lastSuccessfulIOTimer._value <= CheckConnectionWindow)
{
return true;
}

SniContext = SniContext.Snix_Connect;
try
{
Interlocked.Increment(ref _readingCount);
uint error = CheckConnection();
return (error == TdsEnums.SNI_SUCCESS) || (error == TdsEnums.SNI_WAIT_TIMEOUT);
}
finally
{
Interlocked.Decrement(ref _readingCount);
}
}

/*

// leave this in. comes handy if you have to do Console.WriteLine style debugging ;)
Expand Down
Loading