www.pudn.com > Fetion.rar > Downloader.cs


namespace Imps.Client.Pc 
{ 
    using Imps.Client.Base; 
    using Imps.Client.Core; 
    using System; 
    using System.IO; 
    using System.Net; 
    using System.Runtime.CompilerServices; 
    using System.Threading; 
 
    internal class Downloader 
    { 
        private volatile bool _Downloading; 
        private long _FileSize; 
        private long _HasDownloadedSize; 
        private DateTime _LastModified = DateTime.MinValue; 
        private IWebProxy _WebProxy = WebRequest.DefaultWebProxy; 
        public object DownloadLocker = new object(); 
        private const int MaxTryTimes = 10; 
 
        public event EventHandler BeginDownload; 
 
        public event EventHandler EndDownload; 
 
        public event EventHandler PartDownloaded; 
 
        public void Download(string url, string path) 
        { 
            bool isComplete = true; 
            try 
            { 
                this._Downloading = true; 
                this.RaiseBeginDownload(); 
                if (System.IO.File.Exists(path)) 
                { 
                    System.IO.File.SetAttributes(path, FileAttributes.Normal); 
                    System.IO.File.Delete(path); 
                } 
                string str = path + ".Downloading"; 
                if (!Directory.Exists(Path.GetDirectoryName(str))) 
                { 
                    Directory.CreateDirectory(Path.GetDirectoryName(str)); 
                } 
                if (!System.IO.File.Exists(str)) 
                { 
                    this._HasDownloadedSize = 0L; 
                } 
                else 
                { 
                    long length = new FileInfo(str).Length; 
                    if (this._HasDownloadedSize > length) 
                    { 
                        this._HasDownloadedSize = 0L; 
                        System.IO.File.Delete(str); 
                    } 
                    else if (this._HasDownloadedSize < length) 
                    { 
                        this._HasDownloadedSize = length; 
                        using (FileStream stream = new FileStream(str, FileMode.Open, FileAccess.Write)) 
                        { 
                            stream.SetLength(this._HasDownloadedSize); 
                            this.RaisePartDownloadedEvent(this._HasDownloadedSize); 
                        } 
                    } 
                } 
                FileStream stream2 = new FileStream(str, FileMode.Append, FileAccess.Write, FileShare.None); 
                try 
                { 
                    HttpWebRequest request = ConnectionFactory.CreateHttpWebRequest(url, null, false, false); 
                    if (this.HasDownloadedSize > 0L) 
                    { 
                        request.AddRange((int) this.HasDownloadedSize); 
                    } 
                    HttpWebResponse response = null; 
                    int num2 = 0; 
                    while (true) 
                    { 
                        try 
                        { 
                            response = (HttpWebResponse) request.GetResponse(); 
                            break; 
                        } 
                        catch (WebException exception) 
                        { 
                            if (num2 >= 10) 
                            { 
                                throw; 
                            } 
                            if (exception.Response == null) 
                            { 
                                throw; 
                            } 
                            if ((exception.Response as HttpWebResponse).StatusCode != HttpStatusCode.RequestTimeout) 
                            { 
                                throw; 
                            } 
                        } 
                        catch (Exception) 
                        { 
                            throw; 
                        } 
                        num2++; 
                    } 
                    using (response) 
                    { 
                        int statusCode = (int) response.StatusCode; 
                        if ((statusCode < 200) || (statusCode > 0x12b)) 
                        { 
                            throw new DownloadException(string.Empty, statusCode); 
                        } 
                        if (statusCode == 0xcc) 
                        { 
                            stream2.Close(); 
                            System.IO.File.Delete(str); 
                            return; 
                        } 
                        using (Stream stream3 = response.GetResponseStream()) 
                        { 
                            byte[] buffer = new byte[0xc800]; 
                            try 
                            { 
                                int num4; 
                                while ((num4 = stream3.Read(buffer, 0, buffer.Length)) > 0) 
                                { 
                                    lock (this.DownloadLocker) 
                                    { 
                                        stream2.Write(buffer, 0, num4); 
                                        this._HasDownloadedSize = stream2.Length; 
                                    } 
                                    this.RaisePartDownloadedEvent((long) num4); 
                                } 
                            } 
                            finally 
                            { 
                                this._HasDownloadedSize = stream2.Length; 
                                this.RaisePartDownloadedEvent(0L); 
                            } 
                        } 
                    } 
                } 
                catch (DownloadException) 
                { 
                    isComplete = false; 
                    throw; 
                } 
                catch (WebException exception2) 
                { 
                    isComplete = false; 
                    HttpWebResponse response2 = exception2.Response as HttpWebResponse; 
                    throw new DownloadException(exception2.Message + "\tUrl:" + url, (response2 == null) ? ((int) ((HttpStatusCode) 0)) : ((int) response2.StatusCode), exception2); 
                } 
                catch (ThreadAbortException) 
                { 
                    isComplete = true; 
                    throw; 
                } 
                catch (Exception exception3) 
                { 
                    isComplete = false; 
                    throw new DownloadException(exception3.Message + "\tUrl:" + url, exception3); 
                } 
                finally 
                { 
                    if (stream2 != null) 
                    { 
                        stream2.Dispose(); 
                    } 
                } 
                System.IO.File.Move(str, path); 
            } 
            finally 
            { 
                this._Downloading = false; 
                this.RaiseEndDownload(isComplete); 
            } 
        } 
 
        private void RaiseBeginDownload() 
        { 
            FuncDispatcher.InvokeEventHandlerInUiThread(this, this.BeginDownload, null); 
        } 
 
        private void RaiseEndDownload(bool isComplete) 
        { 
            FuncDispatcher.InvokeEventHandlerInUiThread(this, this.EndDownload, new EndDownloadedEventArgs(isComplete)); 
        } 
 
        private void RaisePartDownloadedEvent(long size) 
        { 
            FuncDispatcher.InvokeEventHandlerInUiThread(this, this.PartDownloaded, new PartDownloadedEventArgs(size)); 
        } 
 
        public bool Downloading 
        { 
            get 
            { 
                return this._Downloading; 
            } 
        } 
 
        public long FileSize 
        { 
            get 
            { 
                return this._FileSize; 
            } 
            set 
            { 
                if (this.Downloading) 
                { 
                    throw new DownloadException("在下载过程中更改了文件大小"); 
                } 
                if (value < 0L) 
                { 
                    throw new DownloadException("设置了小于零的字节数"); 
                } 
                this._FileSize = value; 
            } 
        } 
 
        public long HasDownloadedSize 
        { 
            get 
            { 
                return this._HasDownloadedSize; 
            } 
            set 
            { 
                if (this.Downloading) 
                { 
                    throw new DownloadException("在下载过程中更改了已下载字节大小"); 
                } 
                if (value < 0L) 
                { 
                    throw new ArgumentException("设置了小于零的已下载字节大小"); 
                } 
                this._HasDownloadedSize = value; 
            } 
        } 
 
        public DateTime LastModified 
        { 
            get 
            { 
                return this._LastModified; 
            } 
            set 
            { 
                if (this.Downloading) 
                { 
                    throw new DownloadException("在下载过程中更改了上次修改时间"); 
                } 
                this._LastModified = value; 
            } 
        } 
 
        public IWebProxy WebProxy 
        { 
            get 
            { 
                return this._WebProxy; 
            } 
            set 
            { 
                this._WebProxy = value; 
            } 
        } 
    } 
}