Example #1
0
        /// <summary>
        /// Dispose(bool disposing) executes in two distinct scenarios.
        /// If disposing equals true, the method has been called directly
        /// or indirectly by a user's code. Managed and unmanaged resources
        /// can be disposed.
        /// If disposing equals false, the method has been called by the
        /// runtime from inside the finalizer and you should not reference
        /// other objects. Only unmanaged resources can be disposed.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this._disposed)
            {
                // Set the web socket state to closed.
                if (_context != null)
                {
                    _context.SocketState = SocketState.Closed;
                }

                // Release the receive and send spin wait handler.
                Interlocked.Exchange(ref _exitWaitReceiveIndicator, 0);
                Interlocked.Exchange(ref _exitWaitSendIndicator, 0);
                Interlocked.Exchange(ref _isContextActive, 0);

                // Note disposing has been done.
                _disposed = true;

                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Close the connection.
                    CloseEx();

                    if (_socket != null)
                    {
                        _socket.Dispose();
                    }

                    if (_sslStream != null)
                    {
                        _sslStream.Dispose();
                    }

                    if (_networkStream != null)
                    {
                        _networkStream.Dispose();
                    }

                    if (_requestBuffer != null)
                    {
                        _requestBuffer.Dispose();
                    }

                    if (_responseBuffer != null)
                    {
                        _responseBuffer.Dispose();
                    }

                    if (_requestStream != null)
                    {
                        _requestStream.Dispose();
                    }

                    if (_responseStream != null)
                    {
                        _responseStream.Dispose();
                    }

                    if (_context != null)
                    {
                        if (_context.ContextState != null)
                        {
                            // If the current state context
                            // implements IDisposable then
                            // dispose of the resources.
                            if (_context.ContextState is IDisposable)
                            {
                                IDisposable disposable = (IDisposable)_context.ContextState;
                                disposable.Dispose();
                            }
                        }
                        _context.Dispose();
                    }
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                _socket                = null;
                _sslStream             = null;
                _networkStream         = null;
                _x509Certificate       = null;
                _endConnectionCallback = null;

                _requestBuffer  = null;
                _responseBuffer = null;

                _requestStream  = null;
                _responseStream = null;

                _request  = null;
                _response = null;

                _context.ContextState = null;
                _context = null;
            }
        }
Example #2
0
        /// <summary>
        /// Triggered when all resources are released and parent resources need to be released.
        /// </summary>
        protected override void DisposeOfResources()
        {
            // Set the web socket state to closed.
            if (_context != null)
            {
                _context.SocketState = SocketState.Closed;
            }

            // Release the receive and send spin wait handler.
            Interlocked.Exchange(ref _exitWaitReceiveIndicator, 0);
            Interlocked.Exchange(ref _exitWaitSendIndicator, 0);
            Interlocked.Exchange(ref _isContextActive, 0);
            Interlocked.Exchange(ref _isAsyncModeActive, 0);

            base.State = null;

            _lockReceiver   = null;
            _lockSender     = null;
            _lockFromServer = null;

            if (_requestBuffer != null)
            {
                _requestBuffer.Dispose();
            }

            if (_responseBuffer != null)
            {
                _responseBuffer.Dispose();
            }

            if (_requestStream != null)
            {
                _requestStream.Dispose();
            }

            if (_responseStream != null)
            {
                _responseStream.Dispose();
            }

            if (_context != null)
            {
                if (_context.ContextState != null)
                {
                    // If the current state context
                    // implements IDisposable then
                    // dispose of the resources.
                    if (_context.ContextState is IDisposable)
                    {
                        IDisposable disposable = (IDisposable)_context.ContextState;
                        disposable.Dispose();
                    }
                }

                if (_context.State != null)
                {
                    // If the current state context
                    // implements IDisposable then
                    // dispose of the resources.
                    if (_context.State is IDisposable)
                    {
                        IDisposable disposable = (IDisposable)_context.State;
                        disposable.Dispose();
                    }
                }
                _context.Dispose();
            }

            _requestBuffer  = null;
            _responseBuffer = null;

            _requestStream  = null;
            _responseStream = null;

            _request  = null;
            _response = null;

            _context.State        = null;
            _context.ContextState = null;
            _context = null;
        }