public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
{
lock (this)
{
packet = null;
try
{
if (timeoutInMilliseconds > 0)
{
_socket.ReceiveTimeout = timeoutInMilliseconds;
}
else if (timeoutInMilliseconds == -1)
{ // SqlCient internally represents infinite timeout by -1, and for TcpClient this is translated to a timeout of 0
_socket.ReceiveTimeout = 0;
}
else
{
// otherwise it is timeout for 0 or less than -1
ReportTcpSNIError(0, SNICommon.ConnTimeoutError, string.Empty);
return TdsEnums.SNI_WAIT_TIMEOUT;
}
packet = new SNIPacket(null);
packet.Allocate(_bufferSize);
packet.ReadFromStream(_stream);
if (packet.Length == 0)
{
return ReportErrorAndReleasePacket(packet, 0, SNICommon.ConnTerminatedError, string.Empty);
}
return TdsEnums.SNI_SUCCESS;
}
catch (ObjectDisposedException ode)
{
return ReportErrorAndReleasePacket(packet, ode);
}
catch (SocketException se)
{
return ReportErrorAndReleasePacket(packet, se);
}
catch (IOException ioe)
{
uint errorCode = ReportErrorAndReleasePacket(packet, ioe);
if (ioe.InnerException is SocketException && ((SocketException)(ioe.InnerException)).SocketErrorCode == SocketError.TimedOut)
{
errorCode = TdsEnums.SNI_WAIT_TIMEOUT;
}
return errorCode;
}
finally
{
_socket.ReceiveTimeout = 0;
}
}
}