Пример #1
0
        /// <summary>
        /// 暂停下载
        /// </summary>
        public void Pause()
        {
            if (this.status != DownLoadStatus.Downloading)
            {
                throw new ApplicationException("只有正在下载的客户端才能暂停.");
            }

            // 后台线程会查看状态,如果状态时暂停的,
            // 下载将会被暂停并且状态将随之改为暂停.
            this.status = DownLoadStatus.Pausing;
        }
Пример #2
0
        /// <summary>
        /// 取消下载
        /// </summary>
        public void Cancel()
        {
            // 只有正在下载的或者是暂停的客户端才能被取消.
            if (this.status != DownLoadStatus.Paused && this.status != DownLoadStatus.Downloading)
            {
                throw new ApplicationException("只有正在下载的或者是暂停的客户端才能被取消.");
            }

            // 后台线程将查看状态.如果是正在取消,
            // 那么下载将被取消并且状态将改成已取消.
            this.status = DownLoadStatus.Canceling;
        }
Пример #3
0
 /// <summary>
 /// 更新下载状态
 /// </summary>
 private void ChangeStatus()
 {
     if (this.status == DownLoadStatus.Pausing)
     {
         this.status = DownLoadStatus.Paused;
     }
     else if (this.status == DownLoadStatus.Canceling)
     {
         this.status = DownLoadStatus.Canceled;
     }
     else
     {
         this.status = DownLoadStatus.Completed;
         return;
     }
 }
Пример #4
0
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="file"></param>
 /// <param name="localAdress"></param>
 /// <param name="bufferSize"></param>
 /// <param name="cacheSize"></param>
 public DownLoad(IDownLoadFile file, string localAdress, int bufferSize, int cacheSize)
 {
     this._file        = file;
     this.stream       = _file.GetFileStream();
     this.localAdress  = localAdress;
     this.status       = DownLoadStatus.Idle;
     this.cacheSize    = cacheSize;
     this.bufferSize   = bufferSize;
     this.downLoadSize = 0;
     this.useTime      = TimeSpan.Zero;
     this.allTime      = TimeSpan.Zero;
     this.speed        = 0.00;
     System.Timers.Timer t = new System.Timers.Timer();
     t.Interval = 1000;
     t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
     t.Start();
 }
Пример #5
0
        /// <summary>
        /// 具体的下载方法
        /// </summary>
        private void Download()
        {
            //进入下载状态
            this.status = DownLoadStatus.Downloading;

            //最近一次开始下载时间点
            this.lastStartTime = DateTime.Now;

            //读取服务器响应流缓存
            byte[] downloadBuffer = new byte[bufferSize];

            //服务器实际相应的字节流大小
            int bytesSize = 0;

            //实际使用缓存的大小
            long cache = 0;

            //内存缓存
            MemoryStream downloadCache = new MemoryStream(cacheSize);

            while (true)
            {
                bytesSize = stream.Read(downloadBuffer, 0, downloadBuffer.Length);

                //如果不在下载状态 或 本地缓存放不下 或 读到的字节数为0
                if (this.status != DownLoadStatus.Downloading || cache + bytesSize >= cacheSize || bytesSize == 0)
                {
                    //为避免写入文件时,当前文件正被另一进程占用而写入失败,进行五次写入尝试
                    int tryTimes = 0;
                    for (; tryTimes < 5; tryTimes++)
                    {
                        if (WriteCacheToFile(downloadCache, (int)cache))
                        {
                            downLoadSize += cache;

                            //本地缓存写入磁盘后,应清空本地缓存(或把当前位置移回到MemoryStream的头部)
                            cache = 0;
                            downloadCache.Seek(0, SeekOrigin.Begin);
                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    if (tryTimes >= 5)
                    {
                        Console.WriteLine("DownLoad Failed");
                        break;
                    }
                }

                //如果不在下载状态 或 文件下载完成
                if (this.status != DownLoadStatus.Downloading || downLoadSize >= _file.FileSize)
                {
                    if (downLoadSize == _file.FileSize)
                    {
                        Console.WriteLine("Perfect completed!");
                    }

                    break;
                }
                //注:如果不在下载状态,那么本次bytesSize大小的数据并没有写入磁盘
                downloadCache.Write(downloadBuffer, 0, bytesSize);
                cache            += bytesSize;
                downLoadSizeFlag += bytesSize;
            }
            //更改状态
            ChangeStatus();

            //清理资源
            if (stream != null)
            {
                stream.Close();
            }
            if (downloadCache != null)
            {
                downloadCache.Close();
            }
            Console.WriteLine("completed!");
        }
Пример #6
0
        /// <summary>
        /// 具体的下载方法
        /// </summary>
        private void Download()
        {
            //进入下载状态
            this.status = DownLoadStatus.Downloading;

            //最近一次开始下载时间点
            this.lastStartTime = DateTime.Now;

            //读取服务器响应流缓存
            byte[] downloadBuffer = new byte[bufferSize];

            //服务器实际相应的字节流大小
            int bytesSize = 0;

            //实际使用缓存的大小
            long cache = 0;

            //内存缓存
            MemoryStream downloadCache = new MemoryStream(cacheSize);
            while (true)
            {
                bytesSize = stream.Read(downloadBuffer, 0, downloadBuffer.Length);

                //如果不在下载状态 或 本地缓存放不下 或 读到的字节数为0
                if (this.status != DownLoadStatus.Downloading || cache + bytesSize >= cacheSize || bytesSize == 0)
                {
                    //为避免写入文件时,当前文件正被另一进程占用而写入失败,进行五次写入尝试
                    int tryTimes = 0;
                    for (; tryTimes < 5; tryTimes++)
                    {
                        if (WriteCacheToFile(downloadCache, (int)cache))
                        {
                            downLoadSize += cache;

                            //本地缓存写入磁盘后,应清空本地缓存(或把当前位置移回到MemoryStream的头部)
                            cache = 0;
                            downloadCache.Seek(0, SeekOrigin.Begin);
                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    if (tryTimes >= 5)
                    {
                        Console.WriteLine("DownLoad Failed");
                        break;
                    }
                }

                //如果不在下载状态 或 文件下载完成
                if (this.status != DownLoadStatus.Downloading || downLoadSize >= _file.FileSize)
                {
                    if (downLoadSize == _file.FileSize)
                    {
                        Console.WriteLine("Perfect completed!");
                    }

                    break;
                }
                //注:如果不在下载状态,那么本次bytesSize大小的数据并没有写入磁盘
                downloadCache.Write(downloadBuffer, 0, bytesSize);
                cache += bytesSize;
                downLoadSizeFlag += bytesSize;
            }
            //更改状态
            ChangeStatus();

            //清理资源
            if (stream != null)
                stream.Close();
            if (downloadCache != null)
                downloadCache.Close();
            Console.WriteLine("completed!");
        }
Пример #7
0
        public void DownLoadStart()//异步下载下载漫画
        {
            var  read       = 0;
            var  pos        = 0;
            var  errorCount = 0;
            long fileLen    = 0;
            var  fileName   = "";

            Task task = new Task(() =>
            {
                int downProgress = 0;

                foreach (var i in m_fileList)
                {
                    if (m_status == DownLoadStatus.Pause)
                    {
                        break;
                    }

                    Stream retStream = DownLoad(i, ReferUrl);//下载url
                    errorCount       = 0;

                    while (retStream == null && errorCount < 3)
                    {
                        Thread.Sleep(100);
                        retStream = DownLoad(i, ReferUrl);
                        errorCount++;
                    }

                    if (retStream == null)
                    {
                        downPaused(this, new DownLoadArgs(false, ComicName, downProgress, m_fileList, fileLen));//操作超时,下载暂停
                        return;
                    }

                    if (i.Contains("webp") == false)
                    {
                        fileName = DownLoadPath + (pos++).ToString() + ".jpg";
                    }
                    else
                    {
                        fileName = DownLoadPath + (pos++).ToString() + ".webp";
                    }

                    FileStream file = new FileStream(fileName, FileMode.Create);
                    byte[] buffer   = new byte[32768];

                    while ((read = retStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        file.Write(buffer, 0, read);
                    }

                    fileLen = file.Length;
                    file.Flush();
                    retStream.Flush();
                    file.Close();
                    retStream.Close();
                    downProgress++;
                    downLoadOneFinished(this, new DownLoadArgs(true, ComicName, m_fileList.Count, m_fileList, fileLen));
                    Thread.Sleep(100);
                }

                if (m_status != DownLoadStatus.Pause)
                {
                    downFinished(this, new DownLoadArgs(true, ComicName, m_fileList.Count, m_fileList, fileLen));//触发下载完成事件
                }
                else
                {
                    downPaused(this, new DownLoadArgs(false, ComicName, downProgress, m_fileList, fileLen));
                }
            });

            task.Start();
            m_status = DownLoadStatus.DownLoading;
        }
Пример #8
0
 /// <summary>
 /// 更新下载状态
 /// </summary>
 private void ChangeStatus()
 {
     if (this.status == DownLoadStatus.Pausing)
     {
         this.status = DownLoadStatus.Paused;
     }
     else if (this.status == DownLoadStatus.Canceling)
     {
         this.status = DownLoadStatus.Canceled;
     }
     else
     {
         this.status = DownLoadStatus.Completed;
         return;
     }
 }
Пример #9
0
        /// <summary>
        /// 暂停下载
        /// </summary>
        public void Pause()
        {
            if (this.status != DownLoadStatus.Downloading)
                throw new ApplicationException("只有正在下载的客户端才能暂停.");

            // 后台线程会查看状态,如果状态时暂停的,
            // 下载将会被暂停并且状态将随之改为暂停.
            this.status = DownLoadStatus.Pausing;
        }
Пример #10
0
        /// <summary>
        /// 取消下载
        /// </summary>
        public void Cancel()
        {
            // 只有正在下载的或者是暂停的客户端才能被取消.
            if (this.status != DownLoadStatus.Paused && this.status != DownLoadStatus.Downloading)
                throw new ApplicationException("只有正在下载的或者是暂停的客户端才能被取消.");

            // 后台线程将查看状态.如果是正在取消,
            // 那么下载将被取消并且状态将改成已取消.
            this.status = DownLoadStatus.Canceling;
        }
Пример #11
0
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="file"></param>
 /// <param name="localAdress"></param>
 /// <param name="bufferSize"></param>
 /// <param name="cacheSize"></param>
 public DownLoad(IDownLoadFile file, string localAdress, int bufferSize, int cacheSize)
 {
     this._file = file;
     this.stream = _file.GetFileStream();
     this.localAdress = localAdress;
     this.status = DownLoadStatus.Idle;
     this.cacheSize = cacheSize;
     this.bufferSize = bufferSize;
     this.downLoadSize = 0;
     this.useTime = TimeSpan.Zero;
     this.allTime = TimeSpan.Zero;
     this.speed = 0.00;
     System.Timers.Timer t = new System.Timers.Timer();
     t.Interval = 1000;
     t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
     t.Start();
 }
Пример #12
0
 public void StopDownLoad()
 {
     m_status = DownLoadStatus.Pause;
 }