示例#1
0
        public DownloadTask StartDownload(
            string fileName,
            string url,
            ContentType contentType,
            FinishHandler finishHandler,
            TimeoutHandler timeoutHandler = null,
            float timeout = 60)
        {
            if (string.IsNullOrEmpty(fileName) ||
                string.IsNullOrEmpty(url))
            {
                return(null);
            }

            if (_currentTask != null &&
                _currentTask.FileName == fileName)
            {
                return(_currentTask);
            }

            lock (_taskQueue)
            {
                foreach (var t in _taskQueue)
                {
                    if (t.FileName == fileName)
                    {
                        return(t);
                    }
                }

                var task = new DownloadTask(
                    fileName,
                    url,
                    contentType,
                    finishHandler,
                    timeoutHandler,
                    timeout);

                _taskQueue.Enqueue(task);
                MyLog.InfoWithFrame(name, string.Format("Start download: {0}", fileName));
                return(task);
            }
        }
示例#2
0
        public void StopAllDownloadTask()
        {
            if (_currentTask != null)
            {
                _currentTask.Cancel();
                _currentTask = null;
            }

            lock (_taskQueue)
            {
                while (_taskQueue.Count > 0)
                {
                    var t = _taskQueue.Dequeue();
                    t.Cancel();
                }

                _taskQueue.Clear();
            }
        }
示例#3
0
        private void DownloadFinish(DownloadTask task)
        {
            if (task == null)
            {
                return;
            }

            var www = task.Downloader;

            if (www == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(www.error))
            {
                MyLog.ErrorWithFrame(name, string.Format("Download failed! url: {0}, error: {1}", task.Url, www.error));
                return;
            }

            try
            {
                var type = task.Type;
                if (type == ContentType.Image)
                {
                    // 将下载的图片保存到路径中。
                    if (!www.texture)
                    {
                        return;
                    }

                    if (string.IsNullOrEmpty(task.FileName))
                    {
                        return;
                    }

                    var bytes = www.texture.EncodeToPNG();
                    _filePicManager.SavePic(bytes, task.FileName);
                }
                else if (type == ContentType.Bytes)
                {
                    if (!Directory.Exists(FilePath.BinaryFilePath()))
                    {
                        Directory.CreateDirectory(FilePath.BinaryFilePath());
                    }

                    var path = FilePath.BinaryFilePath() + task.FileName;
                    File.WriteAllBytes(path, www.bytes);
                }
                else if (type == ContentType.Text)
                {
                    if (!Directory.Exists(FilePath.TextPath()))
                    {
                        Directory.CreateDirectory(FilePath.TextPath());
                    }

                    var path = FilePath.TextPath() + task.FileName;
                    File.WriteAllBytes(path, www.bytes);
                }

                MyLog.InfoWithFrame(name, string.Format("Finish download: {0}", task.FileName));
            }
            catch (Exception e)
            {
                MyLog.ErrorWithFrame(name, e.Message);
            }
        }
 protected bool Equals(DownloadTask other)
 {
     return(string.Equals(FileName, other.FileName));
 }