Ejemplo n.º 1
0
        protected void DebugLog(string message, params object[] args)
        {
#if ENABLE_DEBUG
            message = string.Format("[{0}] {1}", hash_, message);
#endif
            FunDebug.DebugLog(message, args);
        }
Ejemplo n.º 2
0
        public void Add(string hostname, UInt16 port)
        {
            IPAddress[] list = Dns.GetHostAddresses(hostname);
            if (list == null)
            {
                FunDebug.Log("ConnectList - Can't find any ip address with hostname [{0}].", hostname);
                return;
            }

            foreach (IPAddress ip in list)
            {
                addr_list_.Add(new HostIP(hostname, ip, port));
                FunDebug.DebugLog("address > " + ip + " : " + ip.AddressFamily);
            }

            FunDebug.DebugLog("[{0}] Dns address count : {1}", hostname, addr_list_.Count);
        }
Ejemplo n.º 3
0
        void loadCachedList()
        {
            cached_list_.Clear();

            string path = target_path_ + kCachedFileName;

            if (!File.Exists(path))
            {
                return;
            }

            StreamReader stream = File.OpenText(path);
            string       data   = stream.ReadToEnd();

            stream.Close();

            if (data.Length <= 0)
            {
                FunDebug.LogWarning("Failed to get a cached file list.");
                return;
            }

            Dictionary <string, object> json = Json.Deserialize(data) as Dictionary <string, object>;
            List <object> list = json["list"] as List <object>;

            foreach (Dictionary <string, object> node in list)
            {
                DownloadFileInfo info = new DownloadFileInfo();
                info.path = node["path"] as string;
                info.size = Convert.ToUInt32(node["size"]);
                info.hash = node["hash"] as string;
                if (node.ContainsKey("front"))
                {
                    info.hash_front = node["front"] as string;
                }
                else
                {
                    info.hash_front = "";
                }

                cached_list_.Add(info);
            }

            FunDebug.DebugLog("Loads cached list : {0}", cached_list_.Count);
        }
Ejemplo n.º 4
0
        void updateCachedList()
        {
            StringBuilder data = new StringBuilder();

            data.Append("{ \"list\": [ ");

            int index = 0;

            foreach (DownloadFileInfo info in cached_list_)
            {
                data.AppendFormat("{{ \"path\":\"{0}\", ", info.path);
                data.AppendFormat("\"size\":{0}, ", info.size);
                if (info.hash_front.Length > 0)
                {
                    data.AppendFormat("\"front\":\"{0}\", ", info.hash_front);
                }
                data.AppendFormat("\"hash\":\"{0}\" }}", info.hash);

                if (++index < cached_list_.Count)
                {
                    data.Append(", ");
                }
            }

            data.Append(" ] }");

            string       path   = target_path_ + kCachedFileName;
            FileStream   file   = File.Open(path, FileMode.Create);
            StreamWriter stream = new StreamWriter(file);

            stream.Write(data.ToString());
            stream.Flush();
            stream.Close();

            FunDebug.DebugLog("Updates cached list : {0}", cached_list_.Count);
        }
Ejemplo n.º 5
0
        void checkFileList(List <DownloadFileInfo> list)
#endif
        {
            List <DownloadFileInfo> tmp_list         = new List <DownloadFileInfo>(list);
            List <string>           verify_file_list = new List <string>();
            List <string>           remove_list      = new List <string>();
            Queue <int>             rnd_list         = new Queue <int>();
            bool verify_success = true;
            int  rnd_index      = -1;

            DateTime  cached_time  = File.GetLastWriteTime(target_path_ + kCachedFileName);
            Stopwatch elapsed_time = new Stopwatch();

            elapsed_time.Start();

            delete_file_list_.Clear();

            // Randomly check list
            if (cached_list_.Count > 0)
            {
                int           max_count = cached_list_.Count;
                int           count     = Math.Min(Math.Max(1, max_count / 10), 10);
                System.Random rnd       = new System.Random((int)DateTime.Now.Ticks);

                while (rnd_list.Count < count)
                {
                    rnd_index = rnd.Next(1, max_count + 1) - 1;
                    if (!rnd_list.Contains(rnd_index))
                    {
                        rnd_list.Enqueue(rnd_index);
                    }
                }
                FunDebug.DebugLog("Random check file count is {0}", rnd_list.Count);

                rnd_index = rnd_list.Count > 0 ? rnd_list.Dequeue() : -1;
            }

            // Checks local files
            int index = 0;

            foreach (DownloadFileInfo file in cached_list_)
            {
                DownloadFileInfo item = list.Find(i => i.path == file.path);
                if (item != null)
                {
                    string   path = target_path_ + file.path;
                    FileInfo info = new FileInfo(path);

                    if (!File.Exists(path) || item.size != info.Length || item.hash != file.hash)
                    {
                        remove_list.Add(file.path);
                    }
                    else
                    {
                        string filename = Path.GetFileName(item.path);
                        if (filename[0] == '_' || index == rnd_index ||
                            File.GetLastWriteTime(path).Ticks > cached_time.Ticks)
                        {
                            if (index == rnd_index)
                            {
                                rnd_index = rnd_list.Count > 0 ? rnd_list.Dequeue() : -1;
                            }

                            verify_file_list.Add(file.path);

                            MD5Async.Compute(mono, ref path, ref item,
                                             delegate(string p, DownloadFileInfo f, bool is_match)
                            {
                                if (VerifyCallback != null)
                                {
                                    VerifyCallback(p);
                                }

                                verify_file_list.Remove(f.path);

                                if (is_match)
                                {
                                    list.Remove(f);
                                }
                                else
                                {
                                    remove_list.Add(f.path);
                                    verify_success = false;
                                }
                            }
                                             );
                        }
                        else
                        {
                            list.Remove(item);
                        }
                    }
                }
                else
                {
                    remove_list.Add(file.path);
                }

                ++index;
            }

            while (verify_file_list.Count > 0)
            {
#if !NO_UNITY
                yield return(new WaitForSeconds(0.1f));
#else
                Thread.Sleep(100);
#endif
            }

            removeCachedList(remove_list);

            FunDebug.Log("Random validation has {0}", (verify_success ? "succeeded" : "failed"));

            // Checks all local files
            if (!verify_success)
            {
                foreach (DownloadFileInfo file in cached_list_)
                {
                    DownloadFileInfo item = tmp_list.Find(i => i.path == file.path);
                    if (item != null)
                    {
                        verify_file_list.Add(file.path);

                        string path = target_path_ + file.path;
                        MD5Async.Compute(mono, ref path, ref item,
                                         delegate(string p, DownloadFileInfo f, bool is_match)
                        {
                            if (VerifyCallback != null)
                            {
                                VerifyCallback(p);
                            }

                            verify_file_list.Remove(f.path);

                            if (!is_match)
                            {
                                remove_list.Add(f.path);

                                if (!list.Contains(f))
                                {
                                    list.Add(f);
                                }
                            }
                        }
                                         );
                    }
                }

                while (verify_file_list.Count > 0)
                {
#if !NO_UNITY
                    yield return(new WaitForSeconds(0.1f));
#else
                    Thread.Sleep(100);
#endif
                }

                removeCachedList(remove_list);
            }

            elapsed_time.Stop();
            FunDebug.Log("File check total time - {0:F2}s", elapsed_time.ElapsedMilliseconds / 1000f);

            total_download_count_ = list.Count;

            foreach (DownloadFileInfo item in list)
            {
                total_download_size_ += item.size;
            }

            if (total_download_count_ > 0)
            {
                state_ = State.Ready;

                event_list.Add(delegate
                {
                    if (ReadyCallback != null)
                    {
                        ReadyCallback(total_download_count_, total_download_size_);
                    }
                });
            }
            else
            {
                deleteLocalFiles();
                updateCachedList();

                state_ = State.Completed;
                FunDebug.Log("All resources are up to date.");
                onFinished(DownloadResult.SUCCESS);
            }
        }