Exemple #1
0
        //protected void parsePath()
        //{
        //    string[] pathSplit = { "?" };
        //    string[] pathList = m_path.Split(pathSplit, StringSplitOptions.RemoveEmptyEntries);
        //    m_path = pathList[0];

        //    if (pathList.Length > 1)
        //    {
        //        string[] equalSplit = { "=" };
        //        string[] equalList = pathList[1].Split(equalSplit, StringSplitOptions.RemoveEmptyEntries);
        //        m_version = equalList[1];
        //    }
        //}

        override public void load()
        {
            //parsePath();
            base.load();
            if (ResLoadType.eStreamingAssets == m_resLoadType)
            {
                loadFromStreamingAssets();
            }
            else if (ResLoadType.ePersistentData == m_resLoadType)
            {
                loadFromPersistentData();
            }
            else if (ResLoadType.eLoadWeb == m_resLoadType)
            {
                //Ctx.m_instance.m_coroutineMgr.StartCoroutine(downloadAsset());
                //Ctx.m_instance.m_coroutineMgr.StartCoroutine(coroutWebDown());
                m_localPath = Path.Combine(Ctx.m_instance.m_localFileSys.getLocalWriteDir(), UtilLogic.getRelPath(m_path));
                if (!string.IsNullOrEmpty(m_version))
                {
                    m_localPath = UtilLogic.combineVerPath(m_localPath, m_version);
                }

                Ctx.m_instance.m_logSys.log(string.Format("添加下载任务 {0}", m_path));
                Ctx.m_instance.m_TaskQueue.push(this);
            }
        }
Exemple #2
0
        // 线程下载
        public void runTask()
        {
            Ctx.m_instance.m_logSys.log(string.Format("线程开始下载下载任务 {0}", m_path));

            string saveFile    = Path.Combine(Ctx.m_instance.m_localFileSys.getLocalWriteDir(), UtilLogic.getRelPath(m_path));
            string origFile    = saveFile;  // 没有版本号的文件名字,如果本地没有这个文件,需要先建立这个文件,等下载完成后,然后再改名字,保证下载的文件除了网络传输因素外,肯定正确
            bool   bNeedReName = false;

            if (!string.IsNullOrEmpty(m_version))
            {
                saveFile = UtilLogic.combineVerPath(saveFile, m_version);
            }

            try
            {
                //打开网络连接
                string webPath;
                if (!string.IsNullOrEmpty(m_version))
                {
                    webPath = string.Format("{0}?v={1}", m_path, m_version);
                }
                else
                {
                    webPath = m_path;
                }

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(webPath);
                request.Method      = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                request.KeepAlive   = false;
                request.Proxy       = null;
                request.Timeout     = 5000;

                ServicePointManager.DefaultConnectionLimit = 50;

                // GetRequestStream 总是出错,因此只能使用 GET 方式
                //StreamWriter requestWriter = null;
                //Stream webStream = request.GetRequestStream();
                //requestWriter = new StreamWriter(webStream);
                //try
                //{
                //    string postString = string.Format("v={0}", m_version);
                //    requestWriter.Write(postString);
                //}
                //catch (Exception ex2)
                //{
                //    Ctx.m_instance.m_logSys.asynclog("error");
                //}
                HttpWebResponse response      = (HttpWebResponse)request.GetResponse();
                long            contentLength = response.ContentLength;
                long            readedLength  = 0;

                long       lStartPos  = 0;
                FileStream fileStream = null;
                if (File.Exists(saveFile))
                {
                    fileStream = File.OpenWrite(saveFile);
                    lStartPos  = fileStream.Length;
                    if (contentLength - lStartPos <= 0)     // 文件已经完成
                    {
                        fileStream.Close();
                        onRunTaskEnd();
                        Ctx.m_instance.m_logSys.log("之前文件已经下载完成,不用重新下载");
                        return;
                    }
                    fileStream.Seek(lStartPos, SeekOrigin.Current); //移动文件流中的当前指针
                }
                else
                {
                    bNeedReName = true;
                    try
                    {
                        fileStream = new FileStream(origFile, System.IO.FileMode.Create);
                    }
                    catch (Exception /*ex2*/)
                    {
                        Ctx.m_instance.m_logSys.error(string.Format("{0} 文件创建失败", saveFile));
                    }
                }

                if (lStartPos > 0)
                {
                    request.AddRange((int)lStartPos); //设置Range值
                    contentLength -= lStartPos;
                }

                //向服务器请求,获得服务器回应数据流
                System.IO.Stream retStream = response.GetResponseStream();
                int len = 1024 * 8;
                m_bytes = new byte[len];
                int    nReadSize = 0;
                string logStr;
                bool   isBytesValid = true;      // m_bytes 中数据是否有效
                while (readedLength != contentLength)
                {
                    nReadSize = retStream.Read(m_bytes, 0, len);
                    fileStream.Write(m_bytes, 0, nReadSize);
                    readedLength += nReadSize;
                    //logStr = "已下载:" + fs.Length / 1024 + "kb /" + contentLength / 1024 + "kb";
                    logStr = string.Format("文件 {0} 已下载: {1} b / {2} b", m_path, fileStream.Length, contentLength);
                    Ctx.m_instance.m_logSys.log(logStr);

                    if (isBytesValid)
                    {
                        if (readedLength != contentLength)
                        {
                            isBytesValid = false;
                        }
                    }
                }

                // 释放资源
                request.Abort();
                request = null;
                response.Close();
                response = null;

                retStream.Close();
                fileStream.Close();

                // 修改文件名字
                if (bNeedReName)
                {
                    UtilApi.renameFile(origFile, saveFile);
                }

                if (!isBytesValid)
                {
                    m_bytes = null;
                }

                if (readedLength == contentLength)
                {
                    m_isRunSuccess = true;
                }
                else
                {
                    m_isRunSuccess = false;
                }
                onRunTaskEnd();
            }
            catch (Exception /*err*/)
            {
                m_isRunSuccess = false;
                onRunTaskEnd();
            }
        }