Esempio n. 1
0
        internal void CloseAsIs(bool abortive)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"_innerSocket={_innerSocket}");
            }

#if DEBUG
            // If this throws it could be very bad.
            try
            {
#endif
            InnerSafeCloseSocket innerSocket = _innerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref _innerSocket, null);

            Dispose();
            if (innerSocket != null)
            {
                bool canceledOperations = false;

                // Wait until it's safe.
                SpinWait sw = new SpinWait();
                while (!_released)
                {
                    // The socket was not released due to the SafeHandle being used.
                    // Try to make those on-going calls return.
                    // On Linux, TryUnblockSocket will unblock current operations but it doesn't prevent
                    // a new one from starting. So we must call TryUnblockSocket multiple times.
                    canceledOperations |= innerSocket.TryUnblockSocket(abortive, _hasShutdownSend);
                    sw.SpinOnce();
                }

                canceledOperations |= DoReleaseHandle();

                // In case we cancel operations, switch to an abortive close.
                // Unless the user requested a normal close using Socket.Shutdown.
                if (canceledOperations && !_hasShutdownSend)
                {
                    abortive = true;
                }

                innerSocket.Close(abortive);
            }
#if DEBUG
        }

        catch (Exception exception) when(!ExceptionCheck.IsFatal(exception))
        {
            NetEventSource.Fail(this, $"handle:{handle}, error:{exception}");
            throw;
        }
#endif
        }
Esempio n. 2
0
        internal void CloseAsIs(bool abortive)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"_innerSocket={_innerSocket}");
            }

#if DEBUG
            // If this throws it could be very bad.
            try
            {
#endif
            InnerSafeCloseSocket innerSocket = _innerSocket == null ? null : Interlocked.Exchange <InnerSafeCloseSocket>(ref _innerSocket, null);

            Dispose();
            if (innerSocket != null)
            {
                // Wait until it's safe.
                SpinWait sw = new SpinWait();
                while (!_released)
                {
                    // The socket was not released due to the SafeHandle being used.
                    // Try to make those on-going calls return.
                    // On Linux, TryUnblockSocket will unblock current operations but it doesn't prevent
                    // a new one from starting. So we must call TryUnblockSocket multiple times.
                    abortive |= innerSocket.TryUnblockSocket();
                    sw.SpinOnce();
                }

                abortive |= InnerReleaseHandle();

                innerSocket.Close(abortive);
            }
#if DEBUG
        }

        catch (Exception exception) when(!ExceptionCheck.IsFatal(exception))
        {
            NetEventSource.Fail(this, $"handle:{handle}, error:{exception}");
            throw;
        }
#endif
        }