Skip to content

Commit

Permalink
Change "Recieve" methods so that failures return null packets (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored and cheenamalhotra committed Dec 12, 2019
1 parent 30db14c commit c95b0b3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
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

0 comments on commit c95b0b3

Please sign in to comment.