internal void CloseSocket()
 {
     _networkStream.Close();
     _client.Dispose();
 }
Example #2
0
        //TODO: Add this to FxCopBaseline.cs once https://github.com/dotnet/roslyn/issues/15728 is fixed
        void ICloseEx.CloseEx(CloseExState closeState)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"state = {closeState}");
            }

            lock (this)
            {
                if (_closing == true)
                {
                    return;
                }
                _closing   = true;
                _writeable = false;
                _readable  = false;
            }

            try
            {
                try
                {
                    if ((closeState & CloseExState.Abort) == 0)
                    {
                        _networkStream.Close(DefaultCloseTimeout);
                    }
                    else
                    {
                        _networkStream.Close(0);
                    }
                }
                finally
                {
                    _request.DataStreamClosed(closeState);
                }
            }
            catch (Exception exception)
            {
                bool         doThrow      = true;
                WebException webException = exception as WebException;
                if (webException != null)
                {
                    FtpWebResponse response = webException.Response as FtpWebResponse;
                    if (response != null)
                    {
                        if (!_isFullyRead &&
                            response.StatusCode == FtpStatusCode.ConnectionClosed)
                        {
                            doThrow = false;
                        }
                    }
                }

                if (doThrow)
                {
                    if ((closeState & CloseExState.Silent) == 0)
                    {
                        throw;
                    }
                }
            }
        }