Example #1
0
        public override uint Receive(out SNIPacket packet, int timeout)
        {
            lock (this)
            {
                packet = null;
                try
                {
                    packet = new SNIPacket(_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
        /// <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(_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 #3
0
        public override uint Receive(out SNIPacket packet, int timeout)
        {
            long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className);

            try
            {
                SNIPacket errorPacket;
                lock (this)
                {
                    packet = null;
                    try
                    {
                        packet = RentPacket(headerSize: 0, dataSize: _bufferSize);
                        packet.ReadFromStream(_stream);
                        SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Rented and read packet, dataLeft {1}", args0: _connectionId, args1: packet?.DataLeft);

                        if (packet.Length == 0)
                        {
                            errorPacket = packet;
                            packet      = null;
                            var e = new Win32Exception();
                            SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Packet length found 0, Win32 exception raised: {1}", args0: _connectionId, args1: e?.Message);
                            return(ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message));
                        }
                    }
                    catch (ObjectDisposedException ode)
                    {
                        errorPacket = packet;
                        packet      = null;
                        SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, ObjectDisposedException occurred: {1}.", args0: _connectionId, args1: ode?.Message);
                        return(ReportErrorAndReleasePacket(errorPacket, ode));
                    }
                    catch (IOException ioe)
                    {
                        errorPacket = packet;
                        packet      = null;
                        SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IOException occurred: {1}.", args0: _connectionId, args1: ioe?.Message);
                        return(ReportErrorAndReleasePacket(errorPacket, ioe));
                    }
                    return(TdsEnums.SNI_SUCCESS);
                }
            }
            finally
            {
                SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID);
            }
        }
Example #4
0
        public override uint Receive(out SNIPacket packet, int timeout)
        {
            long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("<sc.SNI.SNINpHandle.Receive |SNI|INFO>");

            try
            {
                SNIPacket errorPacket;
                lock (this)
                {
                    packet = null;
                    try
                    {
                        packet = RentPacket(headerSize: 0, dataSize: _bufferSize);
                        packet.ReadFromStream(_stream);

                        if (packet.Length == 0)
                        {
                            errorPacket = packet;
                            packet      = null;
                            var e = new Win32Exception();
                            SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.SNINpHandle.Receive |SNI|ERR> packet length is 0.");
                            return(ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message));
                        }
                    }
                    catch (ObjectDisposedException ode)
                    {
                        errorPacket = packet;
                        packet      = null;
                        SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.SNINpHandle.Receive |SNI|ERR> ObjectDisposedException message = {0}.", ode.Message);
                        return(ReportErrorAndReleasePacket(errorPacket, ode));
                    }
                    catch (IOException ioe)
                    {
                        errorPacket = packet;
                        packet      = null;
                        SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.SNINpHandle.Receive |SNI|ERR> IOException message = {0}.", ioe.Message);
                        return(ReportErrorAndReleasePacket(errorPacket, ioe));
                    }
                    return(TdsEnums.SNI_SUCCESS);
                }
            }
            finally
            {
                SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID);
            }
        }
Example #5
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)
        {
            SNIPacket errorPacket;

            lock (this)
            {
                packet = null;
                try
                {
                    if (timeoutInMilliseconds > 0)
                    {
                        _socket.ReceiveTimeout = timeoutInMilliseconds;
                    }
                    else if (timeoutInMilliseconds == -1)
                    {
                        // SqlClient 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
                        SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Error 258, Timeout error occurred.", args0: _connectionId);
                        ReportTcpSNIError(0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11);
                        return(TdsEnums.SNI_WAIT_TIMEOUT);
                    }

                    packet = RentPacket(headerSize: 0, dataSize: _bufferSize);
                    packet.ReadFromStream(_stream);

                    if (packet.Length == 0)
                    {
                        errorPacket = packet;
                        packet      = null;
                        var e = new Win32Exception();
                        SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Win32 exception occurred: {1}", args0: _connectionId, args1: e?.Message);
                        return(ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message));
                    }

                    SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Data read from stream synchronously", args0: _connectionId);
                    return(TdsEnums.SNI_SUCCESS);
                }
                catch (ObjectDisposedException ode)
                {
                    errorPacket = packet;
                    packet      = null;
                    SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, ObjectDisposedException occurred: {1}", args0: _connectionId, args1: ode?.Message);
                    return(ReportErrorAndReleasePacket(errorPacket, ode));
                }
                catch (SocketException se)
                {
                    errorPacket = packet;
                    packet      = null;
                    SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Socket exception occurred: {1}", args0: _connectionId, args1: se?.Message);
                    return(ReportErrorAndReleasePacket(errorPacket, se));
                }
                catch (IOException ioe)
                {
                    errorPacket = packet;
                    packet      = null;
                    uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe);
                    if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut)
                    {
                        SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", args0: _connectionId, args1: ioe?.Message);
                        errorCode = TdsEnums.SNI_WAIT_TIMEOUT;
                    }

                    SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IO exception occurred: {1}", args0: _connectionId, args1: ioe?.Message);
                    return(errorCode);
                }
                finally
                {
                    _socket.ReceiveTimeout = 0;
                }
            }
        }