1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
local CResumeDownManager = class() CResumeDownManager.__name = "CResumeDownManager"
local table = table local tostring = tostring local pairs = pairs
function CResumeDownManager.Init(self) self._requestTbl = {} end
function CResumeDownManager.StartDownCnt(self, url, savePath, completeFn, progressFn, cnt) local wrapFn if completeFn and cnt then wrapFn = function(isSucc, ...) if not isSucc and cnt > 1 then cnt = cnt - 1 local isOk = self:StartDown(url, savePath, wrapFn, progressFn) if not isOk then cnt = 0 end else completeFn(isSucc, ...) end end end self:StartDown(url, savePath, wrapFn, progressFn) end
function CResumeDownManager.StartDown(self, url, savePath, completeFn, progressFn) if self._requestTbl[url] then return false end if table.count(self._requestTbl) == 0 then UpdateBeat:Add(self.Update, self) end
local rdHdl = ResumeDownHandler.New(savePath) if completeFn then rdHdl:RegComplete(function(code, downLen, totalLen) gTimeMgr:SetTimeOut(0.2, function () local isSucc = code == 206 and totalLen > 0 and downLen == totalLen completeFn(isSucc, downLen, totalLen, url, savePath) end) end) end
if progressFn then rdHdl:RegProgress(progressFn) end local request = UnityEngine.Networking.UnityWebRequest.Get(url) self._requestTbl[url] = request request.chunkedTransfer = true request.disposeDownloadHandlerOnDispose = true request:SetRequestHeader("Range", string.formatExt("bytes={0}-", rdHdl.DownedLength)) request.downloadHandler = rdHdl request.useHttpContinue = true request:SendWebRequest() return true end
function CResumeDownManager.StopDown(self, url) local request = self._requestTbl[url] if not request then return end self._requestTbl[url] = nil
request.downloadHandler:OnDispose() request:Abort() request:Dispose()
if table.count(self._requestTbl) == 0 then UpdateBeat:Remove(self.Update, self) end end
function CResumeDownManager.StopAll(self) local keyTbl = table.keys(self._requestTbl) for _,url in ipairs(keyTbl) do self:StopDown(url) end end
function CResumeDownManager.Update(self) local rmTbl for url,request in pairs(self._requestTbl) do if request.isDone then request.downloadHandler:OnComplete(Utils.LongToInt(request.responseCode))
if not rmTbl then rmTbl = {} end table.insert(rmTbl, url) end end
if rmTbl then for _,url in ipairs(rmTbl) do self:StopDown(url) end rmTbl = nil end end
return CResumeDownManager
|