Ejemplo n.º 1
0
 protected virtual void OnBlockDownloadFileCompleted(BlockThead blockThread, System.ComponentModel.AsyncCompletedEventArgs args)
 {
     if (this.BlockDownloadFileCompleted != null)
     {
         this.BlockDownloadFileCompleted(blockThread, args);
     }
 }
Ejemplo n.º 2
0
        private void OnBlockDownloadFileCompletedHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs args)
        {
            async_children_thread.Remove(sender);
            async_children_finished.Add(sender);
            if (args.Error != null)
            {
                Debug.LogWarning(args.Error);
                if (args.Error is CacheFileException)
                {
                    string tmpName = ((CacheFileException)args.Error).fileName;
                    File.Delete(tmpName);
                    Debug.Log("delete error file " + tmpName);
                }
                async_children_error.Add(args);
            }

            if (async_children_thread.Count == 0 && async_children_error.Count == 0 && async_children_finished.Count > 0)
            {
                async_children_finished.Sort(new BlockTheadComparer());
                BlockThead block       = null;  // (BlockThead) async_children_finished[0]; //find first
                string     tmpFileName = string.Empty;
                int        num2        = 32768; //32KB
                byte[]     array       = new byte[num2];
                FileStream tmpFs;
                int        num4 = 0;
                using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) {
                    for (int i = 0; i < async_children_finished.Count; i++)
                    {
                        block       = (BlockThead)async_children_finished[i];
                        tmpFileName = GetTmpFileName(block.fileName);
                        using (tmpFs = new FileStream(tmpFileName, FileMode.Open)) {
                            while ((num4 = tmpFs.Read(array, 0, num2)) != 0)
                            {
                                fileStream.Write(array, 0, num4);
                            }
                        }
                        File.Delete(tmpFileName);
                        Debug.LogFormat("delete file {0}", tmpFileName);
                    }
                }
                //delete etag
                string etagFileName = GetETagfiedFileName(fileName);
                File.Delete(etagFileName);
                this.OnDownloadFileCompleted(args);
            }
            else if (async_children_thread.Count == 0 && async_children_error.Count > 0)
            {
                this.OnDownloadFileCompleted((System.ComponentModel.AsyncCompletedEventArgs)async_children_error[0]);
            }
        }
Ejemplo n.º 3
0
        public void DownloadFileMultiAsync(System.Uri address, string fileName, object userToken, int maxThreading = 3)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            CheckFileDirectory(fileName);

            System.Exception ex  = null;
            long             len = GetContentLength(address, out ex);

            if (ex != null)
            {
                this.OnBlockDownloadFileCompleted(null, new System.ComponentModel.AsyncCompletedEventArgs(ex, false, userToken));
                return;
            }

            progressChangedEventArgs = new DownloadingProgressChangedEventArgs(0, len, userToken);
            bool delCache = true;

            if (this.responseHeaders != null)
            {
                //check etag
                string svrETag = responseHeaders["ETag"];
                if (string.IsNullOrEmpty(svrETag))
                {
                    Debug.LogWarningFormat("url {0} Etag is Null", fileName);
                    svrETag = responseHeaders["Last-Modified"];
                }

                if (string.IsNullOrEmpty(svrETag))
                {
                    Debug.LogWarningFormat("url {0} Last-Modified is Null", fileName);
                    svrETag = System.DateTime.Now.ToString();
                }

                delCache = CheckCacheModified(fileName, svrETag);
                RecordETag(fileName, svrETag);

                //debug
                // Debug.LogFormat("clear cache {0} ={1}",delCache,fileName);
                // var sb = new System.Text.StringBuilder ();
                // foreach (var kv in responseHeaders.AllKeys) {
                //     sb.AppendFormat ("{0}={1}\r\n", kv, responseHeaders[kv]); //Last-Modified=Tue, 21 Nov 2017 02:03:05 GMT
                //     // Debug.LogFormat("{0}={1}",kv,responseHeaders[kv]);//ETag="5a138959-c68171c"
                // }
                // Debug.Log (sb.ToString ());
            }

            lock (this) {
                this.SetBusy();
                this.fileName = fileName;
                this.async    = true;
                int block = (int)len / maxThreading;

                int    from             = 0;
                int    to               = -1;
                string childrenFileName = null;
                this.async_children_thread.Clear();
                this.async_children_finished.Clear();
                this.async_children_error.Clear();
                for (int i = 0; i < maxThreading; i++)
                {
                    from = to + 1;
                    if (i == maxThreading - 1)
                    {
                        to = (int)len - 1;
                    }
                    else
                    {
                        to = from + block;
                    }

                    childrenFileName = string.Format("{0}.{1}", fileName, i);

                    var blockThead = new BlockThead();

                    object[] parameter = new object[] {
                        address,
                        childrenFileName,
                        userToken,
                        new int[] { from, to },
                        blockThead
                    };

                    blockThead.index        = i;
                    blockThead.fileName     = childrenFileName;
                    blockThead.clearCache   = delCache;
                    blockThead.async_thread = new Thread(
                        delegate(object state) {
                        object[] array = (object[])state;
                        try {
                            string fname    = (string)array[1];
                            int[] fromAndTo = (int[])array[3];
                            bool clearCache = ((BlockThead)array[4]).clearCache;
                            if (clearCache && File.Exists(GetTmpFileName(fname)))
                            {
                                File.Delete(GetTmpFileName(fname));
                            }
                            this.DownloadBlockFileCore((System.Uri)array[0], fname, array[2], fromAndTo[0], fromAndTo[1]);
                            this.OnBlockDownloadFileCompleted(blockThead, new System.ComponentModel.AsyncCompletedEventArgs(null, false, array[2]));
                        } catch (ThreadInterruptedException e) {
                            this.OnBlockDownloadFileCompleted(blockThead, new System.ComponentModel.AsyncCompletedEventArgs(e, true, array[2]));
                        } catch (Exception error) {
                            this.OnBlockDownloadFileCompleted(blockThead, new System.ComponentModel.AsyncCompletedEventArgs(error, false, array[2]));
                        }
                    });
                    blockThead.parameter = parameter;

                    this.async_children_thread.Add(blockThead);

                    blockThead.Start();
                }
            }
        }