Example #1
0
        public override uint Receive(out SNIPacket packet, int timeout)
        {
            lock (this)
            {
                packet = null;
                try
                {
                    packet = new SNIPacket(null);
                    packet.Allocate(_bufferSize);
                    packet.ReadFromStream(_stream);

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

                return(TdsEnums.SNI_SUCCESS);
            }
        }
Example #2
0
        public override uint Receive(out SNIPacket packet, int timeout)
        {
            lock (this)
            {
                packet = null;
                try
                {
                    packet = new SNIPacket(null);
                    packet.Allocate(_bufferSize);
                    packet.ReadFromStream(_stream);

                    if (packet.Length == 0)
                    {
                        return(ReportErrorAndReleasePacket(packet, 0, SNICommon.ConnTerminatedError, string.Empty));
                    }
                }
                catch (ObjectDisposedException ode)
                {
                    return(ReportErrorAndReleasePacket(packet, ode));
                }
                catch (IOException ioe)
                {
                    return(ReportErrorAndReleasePacket(packet, ioe));
                }

                return(TdsEnums.SNI_SUCCESS);
            }
        }
Example #3
0
        /// <summary>
        /// Receive a packet synchronously
        /// </summary>
        /// <param name="packet">SNI packet</param>
        /// <param name="timeoutInMilliseconds">Timeout in Milliseconds</param>
        /// <returns>SNI error code</returns>
        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)
                    {
                        var e = new Win32Exception();
                        return(ReportErrorAndReleasePacket(packet, (uint)e.NativeErrorCode, 0, e.Message));
                    }

                    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;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Receive a packet synchronously
        /// </summary>
        /// <param name="packet">SNI packet</param>
        /// <param name="timeoutInMilliseconds">Timeout in Milliseconds</param>
        /// <returns>SNI error code</returns>
        public sealed override SNIError Receive(ref SNIPacket packet, int timeoutInMilliseconds)
        {
            packet = packet ?? new SNIPacket();

            using (_debugLock.Acquire(this))
            {
                try
                {
                    SNIError timeoutError = SetupTimeoutForReceive(timeoutInMilliseconds);
                    if (timeoutError != null)
                    {
                        return(timeoutError);
                    }

                    packet.Allocate(_bufferSize);
                    packet.ReadFromStream(_stream);

                    if (packet.Length == 0)
                    {
                        return(new SNIError(ProviderNumber, 0, 0, "Connection was terminated"));
                    }

                    return(null);
                }
                catch (ObjectDisposedException ode)
                {
                    packet = null;
                    return(new SNIError(ProviderNumber, 0, ode));
                }
                catch (SocketException se)
                {
                    packet = null;
                    return(new SNIError(ProviderNumber, 0, se));
                }
                catch (IOException ioe)
                {
                    SNIErrorCode errorCode = SNIErrorCode.NoError;
                    if (ioe.InnerException is SocketException && ((SocketException)(ioe.InnerException)).SocketErrorCode == SocketError.TimedOut)
                    {
                        errorCode = SNIErrorCode.ConnTimeoutError;
                    }

                    packet = null;
                    return(new SNIError(ProviderNumber, errorCode, ioe));
                }
            }
        }
Example #5
0
        /// <summary>
        /// Receive a packet synchronously
        /// </summary>
        /// <param name="packet">SNI packet</param>
        /// <param name="timeout">Timeout</param>
        /// <returns>SNI error code</returns>
        public override uint Receive(ref SNIPacket packet, int timeout)
        {
            lock (this)
            {
                try
                {
                    _tcpClient.ReceiveTimeout = (timeout != 0) ? timeout : 1;
                    packet = new SNIPacket(null);
                    packet.Allocate(_bufferSize);
                    packet.ReadFromStream(_stream);

                    if (packet.Length == 0)
                    {
                        return(ReportErrorAndReleasePacket(packet, "Connection was terminated"));
                    }

                    return(TdsEnums.SNI_SUCCESS);
                }
                catch (ObjectDisposedException ode)
                {
                    return(ReportErrorAndReleasePacket(packet, ode.Message));
                }
                catch (SocketException se)
                {
                    return(ReportErrorAndReleasePacket(packet, se.Message));
                }
                catch (IOException ioe)
                {
                    uint errorCode = ReportErrorAndReleasePacket(packet, ioe.Message);
                    if (ioe.InnerException is SocketException && ((SocketException)(ioe.InnerException)).SocketErrorCode == SocketError.TimedOut)
                    {
                        errorCode = TdsEnums.SNI_WAIT_TIMEOUT;
                    }

                    return(errorCode);
                }
                finally
                {
                    _tcpClient.ReceiveTimeout = 0;
                }
            }
        }