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

Change Recieve methods so that failures return null packets #350

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public override void Dispose()

public override uint Receive(out SNIPacket packet, int timeout)
{
SNIPacket errorPacket;
lock (this)
{
packet = null;
Expand All @@ -156,17 +157,23 @@ public override uint Receive(out SNIPacket packet, int timeout)

if (packet.Length == 0)
{
errorPacket = packet;
packet = null;
var e = new Win32Exception();
return ReportErrorAndReleasePacket(packet, (uint)e.NativeErrorCode, 0, e.Message);
return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message);
}
}
catch (ObjectDisposedException ode)
{
return ReportErrorAndReleasePacket(packet, ode);
errorPacket = packet;
packet = null;
return ReportErrorAndReleasePacket(errorPacket, ode);
}
catch (IOException ioe)
{
return ReportErrorAndReleasePacket(packet, ioe);
errorPacket = packet;
packet = null;
return ReportErrorAndReleasePacket(errorPacket, ioe);
}

return TdsEnums.SNI_SUCCESS;
Expand All @@ -175,6 +182,7 @@ public override uint Receive(out SNIPacket packet, int timeout)

public override uint ReceiveAsync(ref SNIPacket packet)
{
SNIPacket errorPacket;
packet = new SNIPacket(headerSize: 0, dataSize: _bufferSize);

try
Expand All @@ -184,11 +192,15 @@ public override uint ReceiveAsync(ref SNIPacket packet)
}
catch (ObjectDisposedException ode)
{
return ReportErrorAndReleasePacket(packet, ode);
errorPacket = packet;
packet = null;
return ReportErrorAndReleasePacket(errorPacket, ode);
}
catch (IOException ioe)
{
return ReportErrorAndReleasePacket(packet, ioe);
errorPacket = packet;
packet = null;
return ReportErrorAndReleasePacket(errorPacket, ioe);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ public override uint Send(SNIPacket packet)
/// <returns>SNI error code</returns>
public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
{
SNIPacket errorPacket;
lock (this)
{
packet = null;
Expand All @@ -487,24 +488,32 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)

if (packet.Length == 0)
{
errorPacket = packet;
packet = null;
var e = new Win32Exception();
return ReportErrorAndReleasePacket(packet, (uint)e.NativeErrorCode, 0, e.Message);
return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message);
}

return TdsEnums.SNI_SUCCESS;
}
catch (ObjectDisposedException ode)
{
return ReportErrorAndReleasePacket(packet, ode);
errorPacket = packet;
packet = null;
return ReportErrorAndReleasePacket(errorPacket, ode);
}
catch (SocketException se)
{
return ReportErrorAndReleasePacket(packet, se);
errorPacket = packet;
packet = null;
return ReportErrorAndReleasePacket(errorPacket, se);
}
catch (IOException ioe)
{
uint errorCode = ReportErrorAndReleasePacket(packet, ioe);
if (ioe.InnerException is SocketException && ((SocketException)(ioe.InnerException)).SocketErrorCode == SocketError.TimedOut)
errorPacket = packet;
packet = null;
uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe);
if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut)
{
errorCode = TdsEnums.SNI_WAIT_TIMEOUT;
}
Expand Down Expand Up @@ -553,6 +562,7 @@ public override uint SendAsync(SNIPacket packet, bool disposePacketAfterSendAsyn
/// <returns>SNI error code</returns>
public override uint ReceiveAsync(ref SNIPacket packet)
{
SNIPacket errorPacket;
packet = new SNIPacket(headerSize: 0, dataSize: _bufferSize);

try
Expand All @@ -562,7 +572,9 @@ public override uint ReceiveAsync(ref SNIPacket packet)
}
catch (Exception e) when (e is ObjectDisposedException || e is SocketException || e is IOException)
{
return ReportErrorAndReleasePacket(packet, e);
errorPacket = packet;
packet = null;
return ReportErrorAndReleasePacket(errorPacket, e);
}
}

Expand Down