ModNet.vb 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033
  1. Imports System.Net
  2. Public Module ModNet
  3. Public Const NetDownloadEnd As String = ".PCLDownloading"
  4. ''' <summary>
  5. ''' 测试 Ping。失败则返回 -1。
  6. ''' </summary>
  7. Public Function Ping(Ip As String, Optional Timeout As Integer = 10000, Optional MakeLog As Boolean = True) As Integer
  8. Dim PingResult As NetworkInformation.PingReply
  9. Try
  10. PingResult = (New NetworkInformation.Ping).Send(Ip)
  11. Catch ex As Exception
  12. If MakeLog Then Log("[Net] Ping " & Ip & " 失败:" & ex.Message)
  13. Return -1
  14. End Try
  15. If PingResult.Status = NetworkInformation.IPStatus.Success Then
  16. If MakeLog Then Log("[Net] Ping " & Ip & " 结束:" & PingResult.RoundtripTime & "ms")
  17. Return PingResult.RoundtripTime
  18. Else
  19. If MakeLog Then Log("[Net] Ping " & Ip & " 失败")
  20. Return -1
  21. End If
  22. End Function
  23. ''' <summary>
  24. ''' 以 WebClient 获取网页源代码。会进行至多 45 秒 3 次的尝试,允许最长 30s 的超时。
  25. ''' </summary>
  26. ''' <param name="Url">网页的 Url。</param>
  27. ''' <param name="Encoding">网页的编码,通常为 UTF-8。</param>
  28. Public Function NetGetCodeByClient(Url As String, Encoding As Encoding, Optional Accept As String = "application/json, text/javascript, */*; q=0.01", Optional UseBrowserUserAgent As Boolean = False) As String
  29. Dim RetryCount As Integer = 0
  30. Dim RetryException As Exception = Nothing
  31. Dim StartTime As Long = GetTimeTick()
  32. Try
  33. Retry:
  34. Select Case RetryCount
  35. Case 0 '正常尝试
  36. Return NetGetCodeByClient(Url, Encoding, 10000, Accept, UseBrowserUserAgent)
  37. Case 1 '慢速重试
  38. Thread.Sleep(500)
  39. Return NetGetCodeByClient(Url, Encoding, 30000, Accept, UseBrowserUserAgent)
  40. Case Else '快速重试
  41. If GetTimeTick() - StartTime > 5500 Then
  42. '若前两次加载耗费 5 秒以上,才进行重试
  43. Thread.Sleep(500)
  44. Return NetGetCodeByClient(Url, Encoding, 4000, Accept, UseBrowserUserAgent)
  45. Else
  46. Throw RetryException
  47. End If
  48. End Select
  49. Catch ex As Exception
  50. Select Case RetryCount
  51. Case 0
  52. RetryException = ex
  53. RetryCount += 1
  54. GoTo Retry
  55. Case 1
  56. RetryCount += 1
  57. GoTo Retry
  58. Case Else
  59. Throw
  60. End Select
  61. End Try
  62. End Function
  63. Public Function NetGetCodeByClient(Url As String, Encoding As Encoding, Timeout As Integer, Accept As String, Optional UseBrowserUserAgent As Boolean = False) As String
  64. Url = SecretCdnSign(Url)
  65. Log("[Net] 获取客户端网络结果:" & Url & ",最大超时 " & Timeout)
  66. Dim Request As CookieWebClient
  67. Dim res As HttpWebResponse = Nothing
  68. Dim HttpStream As Stream = Nothing
  69. Try
  70. Request = New CookieWebClient With {
  71. .Encoding = Encoding,
  72. .Timeout = Timeout
  73. }
  74. Request.Headers("Accept") = Accept
  75. Request.Headers("Accept-Language") = "en-US,en;q=0.5"
  76. Request.Headers("X-Requested-With") = "XMLHttpRequest"
  77. SecretHeadersSign(Url, Request, UseBrowserUserAgent)
  78. Return Request.DownloadString(Url)
  79. Catch ex As Exception
  80. If ex.GetType.Equals(GetType(WebException)) AndAlso CType(ex, WebException).Status = WebExceptionStatus.Timeout Then
  81. Throw New TimeoutException("连接服务器超时(" & Url & ")", ex)
  82. Else
  83. Throw New WebException("获取结果失败," & ex.Message & "(" & Url & ")", ex)
  84. End If
  85. Finally
  86. If Not IsNothing(HttpStream) Then HttpStream.Dispose()
  87. If Not IsNothing(res) Then res.Dispose()
  88. End Try
  89. End Function
  90. ''' <summary>
  91. ''' 以 WebRequest 获取网页源代码或 Json。会进行至多 45 秒 3 次的尝试,允许最长 30s 的超时。
  92. ''' </summary>
  93. ''' <param name="Url">网页的 Url。</param>
  94. ''' <param name="Encode">网页的编码,通常为 UTF-8。</param>
  95. ''' <param name="BackupUrl">如果第一次尝试失败,换用的备用 URL。</param>
  96. Public Function NetGetCodeByRequestRetry(Url As String, Optional Encode As Encoding = Nothing, Optional Accept As String = "",
  97. Optional IsJson As Boolean = False, Optional BackupUrl As String = Nothing, Optional UseBrowserUserAgent As Boolean = False)
  98. Dim RetryCount As Integer = 0
  99. Dim RetryException As Exception = Nothing
  100. Dim StartTime As Long = GetTimeTick()
  101. Try
  102. Retry:
  103. Select Case RetryCount
  104. Case 0 '正常尝试
  105. Return NetGetCodeByRequestOnce(Url, Encode, 10000, IsJson, Accept, UseBrowserUserAgent)
  106. Case 1 '慢速重试
  107. Thread.Sleep(500)
  108. Return NetGetCodeByRequestOnce(If(BackupUrl, Url), Encode, 30000, IsJson, Accept, UseBrowserUserAgent)
  109. Case Else '快速重试
  110. If GetTimeTick() - StartTime > 5500 Then
  111. '若前两次加载耗费 5 秒以上,才进行重试
  112. Thread.Sleep(500)
  113. Return NetGetCodeByRequestOnce(If(BackupUrl, Url), Encode, 4000, IsJson, Accept, UseBrowserUserAgent)
  114. Else
  115. Throw RetryException
  116. End If
  117. End Select
  118. Catch ex As ThreadInterruptedException
  119. Throw
  120. Catch ex As Exception
  121. Select Case RetryCount
  122. Case 0
  123. RetryException = ex
  124. RetryCount += 1
  125. GoTo Retry
  126. Case 1
  127. RetryCount += 1
  128. GoTo Retry
  129. Case Else
  130. Throw
  131. End Select
  132. End Try
  133. End Function
  134. ''' <summary>
  135. ''' 以 WebRequest 获取网页源代码或 Json。会逐渐生成 4 个尝试线程,并在 60s 后超时。
  136. ''' </summary>
  137. ''' <param name="Url">网页的 Url。</param>
  138. ''' <param name="Encode">网页的编码,通常为 UTF-8。</param>
  139. Public Function NetGetCodeByRequestMultiple(Url As String, Optional Encode As Encoding = Nothing, Optional Accept As String = "", Optional IsJson As Boolean = False)
  140. Dim Threads As New List(Of Thread)
  141. Dim RequestResult = Nothing
  142. Dim RequestEx As Exception = Nothing
  143. Dim FailCount As Integer = 0
  144. For i = 1 To 4
  145. Dim th As New Thread(Sub()
  146. Try
  147. RequestResult = NetGetCodeByRequestOnce(Url, Encode, 30000, IsJson, Accept)
  148. Catch ex As Exception
  149. FailCount += 1
  150. RequestEx = ex
  151. End Try
  152. End Sub)
  153. th.Start()
  154. Threads.Add(th)
  155. Thread.Sleep(i * 250)
  156. If RequestResult IsNot Nothing Then GoTo RequestFinished
  157. Next
  158. Do While True
  159. If RequestResult IsNot Nothing Then
  160. RequestFinished:
  161. Try
  162. For Each th In Threads
  163. If th.IsAlive Then th.Interrupt()
  164. Next
  165. Catch
  166. End Try
  167. Return RequestResult
  168. ElseIf FailCount = 4 Then
  169. Try
  170. For Each th In Threads
  171. If th.IsAlive Then th.Interrupt()
  172. Next
  173. Catch
  174. End Try
  175. Throw RequestEx
  176. End If
  177. Thread.Sleep(20)
  178. Loop
  179. Throw New Exception("未知错误")
  180. End Function
  181. Public Function NetGetCodeByRequestOnce(Url As String, Optional Encode As Encoding = Nothing, Optional Timeout As Integer = 30000, Optional IsJson As Boolean = False, Optional Accept As String = "", Optional UseBrowserUserAgent As Boolean = False)
  182. If RunInUi() AndAlso Not Url.Contains("//127.") Then Throw New Exception("在 UI 线程执行了网络请求")
  183. Url = SecretCdnSign(Url)
  184. Log($"[Net] 获取网络结果:{Url},超时 {Timeout}ms{If(IsJson, ",要求 json", "")}")
  185. Dim Request As HttpWebRequest = WebRequest.Create(Url)
  186. Dim Result As New List(Of Byte)
  187. Try
  188. If Url.StartsWithF("https", True) Then Request.ProtocolVersion = HttpVersion.Version11
  189. Request.Timeout = Timeout
  190. Request.Accept = Accept
  191. SecretHeadersSign(Url, Request, UseBrowserUserAgent)
  192. Using res As HttpWebResponse = Request.GetResponse()
  193. Using HttpStream As Stream = res.GetResponseStream()
  194. HttpStream.ReadTimeout = Timeout
  195. Dim HttpData As Byte() = New Byte(16384) {}
  196. Using Reader As New StreamReader(HttpStream, If(Encode, Encoding.UTF8))
  197. Dim ResultString As String = Reader.ReadToEnd
  198. Return If(IsJson, GetJson(ResultString), ResultString)
  199. End Using
  200. End Using
  201. End Using
  202. Catch ex As ThreadInterruptedException
  203. Throw
  204. Catch ex As Exception
  205. If TypeOf ex Is WebException AndAlso CType(ex, WebException).Status = WebExceptionStatus.Timeout Then
  206. Throw New TimeoutException($"获取结果失败({CType(ex, WebException).Status},{ex.Message},{Url})", ex)
  207. Else
  208. Throw New WebException($"获取结果失败({If(TypeOf ex Is WebException, CType(ex, WebException).Status & ",", "")}{ex.Message},{Url})", ex)
  209. End If
  210. Finally
  211. Request.Abort()
  212. End Try
  213. End Function
  214. ''' <summary>
  215. ''' 以多线程下载网页文件的方式获取网页源代码。
  216. ''' </summary>
  217. ''' <param name="Url">网页的 Url。</param>
  218. Public Function NetGetCodeByDownload(Url As String, Optional Timeout As Integer = 45000, Optional IsJson As Boolean = False, Optional UseBrowserUserAgent As Boolean = False) As String
  219. Dim Temp As String = PathTemp & "Cache\Code\" & Url.GetHashCode() & "_" & GetUuid()
  220. Dim NewTask As New LoaderDownload("源码获取 " & GetUuid() & "#", New List(Of NetFile) From {New NetFile({Url}, Temp, New FileChecker With {.IsJson = IsJson}, UseBrowserUserAgent)})
  221. Try
  222. NewTask.WaitForExitTime(Timeout, TimeoutMessage:="连接服务器超时(" & Url & ")")
  223. NetGetCodeByDownload = ReadFile(Temp)
  224. File.Delete(Temp)
  225. Finally
  226. NewTask.Abort()
  227. End Try
  228. End Function
  229. ''' <summary>
  230. ''' 以多线程下载网页文件的方式获取网页源代码。
  231. ''' </summary>
  232. ''' <param name="Urls">网页的 Url 列表。</param>
  233. Public Function NetGetCodeByDownload(Urls As IEnumerable(Of String), Optional Timeout As Integer = 45000, Optional IsJson As Boolean = False, Optional UseBrowserUserAgent As Boolean = False) As String
  234. Dim Temp As String = PathTemp & "Cache\Code\" & Urls.First.GetHashCode() & "_" & GetUuid()
  235. Dim NewTask As New LoaderDownload("源码获取 " & GetUuid() & "#", New List(Of NetFile) From {New NetFile(Urls, Temp, New FileChecker With {.IsJson = IsJson}, UseBrowserUserAgent)})
  236. Try
  237. NewTask.WaitForExitTime(Timeout, TimeoutMessage:="连接服务器超时(第一下载源:" & Urls.First & ")")
  238. NetGetCodeByDownload = ReadFile(Temp)
  239. File.Delete(Temp)
  240. Finally
  241. NewTask.Abort()
  242. End Try
  243. End Function
  244. ''' <summary>
  245. ''' 从网络中直接下载文件。这不能下载 CDN 中的文件。
  246. ''' </summary>
  247. ''' <param name="Url">网络 Url。</param>
  248. ''' <param name="LocalFile">下载的本地地址。</param>
  249. Public Sub NetDownload(Url As String, LocalFile As String, Optional UseBrowserUserAgent As Boolean = False)
  250. Log("[Net] 直接下载文件:" & Url)
  251. '初始化
  252. Try
  253. '建立目录
  254. Directory.CreateDirectory(GetPathFromFullPath(LocalFile))
  255. '尝试删除原文件
  256. File.Delete(LocalFile)
  257. Catch ex As Exception
  258. Throw New WebException("预处理下载文件路径失败(" & LocalFile & ")。", ex)
  259. End Try
  260. '下载
  261. Using Client As New WebClient
  262. Try
  263. SecretHeadersSign(Url, Client, UseBrowserUserAgent)
  264. Client.DownloadFile(Url, LocalFile)
  265. Catch ex As Exception
  266. File.Delete(LocalFile)
  267. Throw New WebException("直接下载文件失败(" & Url & ")。", ex)
  268. End Try
  269. End Using
  270. End Sub
  271. ''' <summary>
  272. ''' 发送一个网络请求并获取返回内容,会重试三次并在最长 45s 后超时。
  273. ''' </summary>
  274. ''' <param name="Url">请求的服务器地址。</param>
  275. ''' <param name="Method">请求方式(POST 或 GET)。</param>
  276. ''' <param name="Data">请求的内容。</param>
  277. ''' <param name="ContentType">请求的套接字类型。</param>
  278. ''' <param name="DontRetryOnRefused">当返回 40x 时不重试。</param>
  279. Public Function NetRequestRetry(Url As String, Method As String, Data As Object, ContentType As String, Optional DontRetryOnRefused As Boolean = True, Optional Headers As Dictionary(Of String, String) = Nothing) As String
  280. Dim RetryCount As Integer = 0
  281. Dim RetryException As Exception = Nothing
  282. Dim StartTime As Long = GetTimeTick()
  283. Try
  284. Retry:
  285. Select Case RetryCount
  286. Case 0 '正常尝试
  287. Return NetRequestOnce(Url, Method, Data, ContentType, 15000, Headers)
  288. Case 1 '慢速重试
  289. Thread.Sleep(500)
  290. Return NetRequestOnce(Url, Method, Data, ContentType, 25000, Headers)
  291. Case Else '快速重试
  292. If GetTimeTick() - StartTime > 5500 Then
  293. '若前两次加载耗费 5 秒以上,才进行重试
  294. Thread.Sleep(500)
  295. Return NetRequestOnce(Url, Method, Data, ContentType, 4000, Headers)
  296. Else
  297. Throw RetryException
  298. End If
  299. End Select
  300. Catch ex As ThreadInterruptedException
  301. Throw
  302. Catch ex As Exception
  303. If ex.InnerException IsNot Nothing AndAlso ex.InnerException.Message.Contains("(40") AndAlso DontRetryOnRefused Then Throw
  304. Select Case RetryCount
  305. Case 0
  306. If ModeDebug Then Log(ex, "[Net] 网络请求第一次失败(" & Url & ")")
  307. RetryException = ex
  308. RetryCount += 1
  309. GoTo Retry
  310. Case 1
  311. If ModeDebug Then Log(ex, "[Net] 网络请求第二次失败(" & Url & ")")
  312. RetryCount += 1
  313. GoTo Retry
  314. Case Else
  315. Throw
  316. End Select
  317. End Try
  318. End Function
  319. ''' <summary>
  320. ''' 同时发送多个网络请求并要求返回内容。
  321. ''' </summary>
  322. Public Function NetRequestMultiple(Url As String, Method As String, Data As Object, ContentType As String, Optional RequestCount As Integer = 4, Optional Headers As Dictionary(Of String, String) = Nothing, Optional MakeLog As Boolean = True)
  323. Dim Threads As New List(Of Thread)
  324. Dim RequestResult = Nothing
  325. Dim RequestEx As Exception = Nothing
  326. Dim FailCount As Integer = 0
  327. For i = 1 To RequestCount
  328. Dim th As New Thread(
  329. Sub()
  330. Try
  331. RequestResult = NetRequestOnce(Url, Method, Data, ContentType, 30000, Headers, MakeLog)
  332. Catch ex As Exception
  333. FailCount += 1
  334. RequestEx = ex
  335. End Try
  336. End Sub)
  337. th.Start()
  338. Threads.Add(th)
  339. Thread.Sleep(i * 250)
  340. If RequestResult IsNot Nothing Then GoTo RequestFinished
  341. Next
  342. Do While True
  343. If RequestResult IsNot Nothing Then
  344. RequestFinished:
  345. For Each th In Threads
  346. If th.IsAlive Then th.Interrupt()
  347. Next
  348. Return RequestResult
  349. ElseIf FailCount = RequestCount Then
  350. For Each th In Threads
  351. If th.IsAlive Then th.Interrupt()
  352. Next
  353. Throw RequestEx
  354. End If
  355. Thread.Sleep(20)
  356. Loop
  357. Throw New Exception("未知错误")
  358. End Function
  359. ''' <summary>
  360. ''' 发送一次网络请求并获取返回内容。
  361. ''' </summary>
  362. Public Function NetRequestOnce(Url As String, Method As String, Data As Object, ContentType As String, Optional Timeout As Integer = 25000, Optional Headers As Dictionary(Of String, String) = Nothing, Optional MakeLog As Boolean = True, Optional UseBrowserUserAgent As Boolean = False) As String
  363. If RunInUi() AndAlso Not Url.Contains("//127.") Then Throw New Exception("在 UI 线程执行了网络请求")
  364. Url = SecretCdnSign(Url)
  365. If MakeLog Then Log("[Net] 发起网络请求(" & Method & "," & Url & "),最大超时 " & Timeout)
  366. Dim DataStream As Stream = Nothing
  367. Dim Resp As WebResponse = Nothing
  368. Dim Req As HttpWebRequest
  369. Try
  370. Req = WebRequest.Create(Url)
  371. Req.Method = Method
  372. Dim SendData As Byte()
  373. If TypeOf Data Is Byte() Then
  374. SendData = Data
  375. Else
  376. SendData = New UTF8Encoding(False).GetBytes(Data.ToString)
  377. End If
  378. If Headers IsNot Nothing Then
  379. For Each Pair In Headers
  380. Req.Headers.Add(Pair.Key, Pair.Value)
  381. Next
  382. End If
  383. Req.ContentType = ContentType
  384. Req.Timeout = Timeout
  385. SecretHeadersSign(Url, Req, UseBrowserUserAgent)
  386. If Url.StartsWithF("https", True) Then Req.ProtocolVersion = HttpVersion.Version11
  387. If Method = "POST" OrElse Method = "PUT" Then
  388. Req.ContentLength = SendData.Length
  389. DataStream = Req.GetRequestStream()
  390. DataStream.WriteTimeout = Timeout
  391. DataStream.ReadTimeout = Timeout
  392. DataStream.Write(SendData, 0, SendData.Length)
  393. DataStream.Close()
  394. End If
  395. Resp = Req.GetResponse()
  396. DataStream = Resp.GetResponseStream()
  397. DataStream.WriteTimeout = Timeout
  398. DataStream.ReadTimeout = Timeout
  399. Using Reader As New StreamReader(DataStream)
  400. Return Reader.ReadToEnd()
  401. End Using
  402. Catch ex As ThreadInterruptedException
  403. Throw
  404. Catch ex As WebException
  405. If ex.Status = WebExceptionStatus.Timeout Then
  406. ex = New WebException($"连接服务器超时,请检查你的网络环境是否良好({ex.Message},{Url})", ex)
  407. Else
  408. '获取请求失败的返回
  409. Dim Res As String = ""
  410. Try
  411. If ex.Response Is Nothing Then Exit Try
  412. DataStream = ex.Response.GetResponseStream()
  413. DataStream.WriteTimeout = Timeout
  414. DataStream.ReadTimeout = Timeout
  415. Using Reader As New StreamReader(DataStream)
  416. Res = Reader.ReadToEnd()
  417. End Using
  418. Catch
  419. End Try
  420. If Res = "" Then
  421. ex = New WebException($"网络请求失败({ex.Status},{ex.Message},{Url})", ex)
  422. Else
  423. ex = New ResponsedWebException($"服务器返回错误({ex.Status},{ex.Message},{Url}){vbCrLf}{Res}", Res, ex)
  424. End If
  425. End If
  426. If MakeLog Then Log(ex, "NetRequestOnce 失败", LogLevel.Developer)
  427. Throw ex
  428. Catch ex As Exception
  429. ex = New WebException("网络请求失败(" & Url & ")", ex)
  430. If MakeLog Then Log(ex, "NetRequestOnce 失败", LogLevel.Developer)
  431. Throw ex
  432. Finally
  433. If DataStream IsNot Nothing Then DataStream.Dispose()
  434. If Resp IsNot Nothing Then Resp.Dispose()
  435. End Try
  436. End Function
  437. Public Class ResponsedWebException
  438. Inherits WebException
  439. ''' <summary>
  440. ''' 远程服务器给予的回复。
  441. ''' </summary>
  442. Public Overloads Property Response As String
  443. Public Sub New(Message As String, Response As String, InnerException As Exception)
  444. MyBase.New(Message, InnerException)
  445. Me.Response = Response
  446. End Sub
  447. End Class
  448. ''' <summary>
  449. ''' 最大线程数。
  450. ''' </summary>
  451. Public NetTaskThreadLimit As Integer
  452. ''' <summary>
  453. ''' 速度下限。
  454. ''' </summary>
  455. Public NetTaskSpeedLimitLow As Long = 256 * 1024L '256K/s
  456. ''' <summary>
  457. ''' 速度上限。若无限制则为 -1。
  458. ''' </summary>
  459. Public NetTaskSpeedLimitHigh As Long = -1
  460. ''' <summary>
  461. ''' 基于限速,当前可以下载的剩余量。
  462. ''' </summary>
  463. Public NetTaskSpeedLimitLeft As Long = -1
  464. Private ReadOnly NetTaskSpeedLimitLeftLock As New Object
  465. Private NetTaskSpeedLimitLeftLast As Long
  466. ''' <summary>
  467. ''' 正在运行中的线程数。
  468. ''' </summary>
  469. Public NetTaskThreadCount As Integer = 0
  470. Private ReadOnly NetTaskThreadCountLock As New Object
  471. ''' <summary>
  472. ''' 下载源。
  473. ''' </summary>
  474. Public Class NetSource
  475. Public Id As Integer
  476. Public Url As String
  477. Public FailCount As Integer
  478. Public Ex As Exception
  479. Public Thread As NetThread
  480. Public IsFailed As Boolean
  481. Public Overrides Function ToString() As String
  482. Return Url
  483. End Function
  484. End Class
  485. ''' <summary>
  486. ''' 下载进度标示。
  487. ''' </summary>
  488. Public Enum NetState
  489. ''' <summary>
  490. ''' 尚未进行已存在检查。
  491. ''' </summary>
  492. WaitForCheck = -1
  493. ''' <summary>
  494. ''' 尚未开始。
  495. ''' </summary>
  496. WaitForDownload = 0
  497. ''' <summary>
  498. ''' 正在连接,尚未获取文件大小。
  499. ''' </summary>
  500. Connect = 1
  501. ''' <summary>
  502. ''' 已获取文件大小,尚未有有效下载。
  503. ''' </summary>
  504. [Get] = 2
  505. ''' <summary>
  506. ''' 正在下载。
  507. ''' </summary>
  508. Download = 3
  509. ''' <summary>
  510. ''' 正在合并文件。
  511. ''' </summary>
  512. Merge = 4
  513. ''' <summary>
  514. ''' 不进行下载,因为已发现现存的文件。
  515. ''' </summary>
  516. WaitForCopy = 5
  517. ''' <summary>
  518. ''' 已完成。
  519. ''' </summary>
  520. Finish = 6
  521. ''' <summary>
  522. ''' 已失败或中断。
  523. ''' </summary>
  524. [Error] = 7
  525. End Enum
  526. ''' <summary>
  527. ''' 预下载检查行为。
  528. ''' </summary>
  529. Public Enum NetPreDownloadBehaviour
  530. ''' <summary>
  531. ''' 当文件已存在时,显示提示以提醒用户是否继续下载。
  532. ''' </summary>
  533. HintWhileExists
  534. ''' <summary>
  535. ''' 当文件已存在或正在下载时,直接退出下载函数执行,不对用户进行提示。
  536. ''' </summary>
  537. ExitWhileExistsOrDownloading
  538. ''' <summary>
  539. ''' 不进行已存在检查。
  540. ''' </summary>
  541. IgnoreCheck
  542. End Enum
  543. ''' <summary>
  544. ''' 下载线程。
  545. ''' </summary>
  546. Public Class NetThread
  547. Implements IEnumerable(Of NetThread)
  548. ''' <summary>
  549. ''' 对应的下载任务。
  550. ''' </summary>
  551. Public Task As NetFile
  552. ''' <summary>
  553. ''' 对应的线程。
  554. ''' </summary>
  555. Public Thread As Thread
  556. ''' <summary>
  557. ''' 链表中的下一个线程。
  558. ''' </summary>
  559. Public NextThread As NetThread
  560. Private ReadOnly Iterator Property [Next]() As IEnumerable(Of NetThread)
  561. Get
  562. Dim CurrentChain As NetThread = Me
  563. While CurrentChain IsNot Nothing
  564. Yield CurrentChain
  565. CurrentChain = CurrentChain.NextThread
  566. End While
  567. End Get
  568. End Property
  569. Public Function GetEnumerator() As IEnumerator(Of NetThread) Implements IEnumerable(Of NetThread).GetEnumerator
  570. Return [Next].GetEnumerator()
  571. End Function
  572. Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
  573. Return [Next].GetEnumerator()
  574. End Function
  575. ''' <summary>
  576. ''' 分配给任务中每个线程(无论其是否失败)的编号。
  577. ''' </summary>
  578. Public Uuid As Integer
  579. ''' <summary>
  580. ''' 是否为第一个线程。
  581. ''' </summary>
  582. Public ReadOnly Property IsFirstThread As Boolean
  583. Get
  584. Return DownloadStart = 0 AndAlso Task.FileSize = -2
  585. End Get
  586. End Property
  587. ''' <summary>
  588. ''' 该线程的缓存文件。
  589. ''' </summary>
  590. Public Temp As String
  591. ''' <summary>
  592. ''' 线程下载起始位置。
  593. ''' </summary>
  594. Public DownloadStart As Long
  595. ''' <summary>
  596. ''' 线程下载结束位置。
  597. ''' </summary>
  598. Public ReadOnly Property DownloadEnd As Long
  599. Get
  600. SyncLock Task.LockChain
  601. If NextThread Is Nothing Then
  602. If Task.IsUnknownSize Then
  603. Return 5 * 1024 * 1024 * 1024L '5G
  604. Else
  605. Return Task.FileSize - 1
  606. End If
  607. Else
  608. Return NextThread.DownloadStart - 1
  609. End If
  610. End SyncLock
  611. End Get
  612. End Property
  613. ''' <summary>
  614. ''' 线程未下载的文件大小。
  615. ''' </summary>
  616. Public ReadOnly Property DownloadUndone As Long
  617. Get
  618. Return DownloadEnd - (DownloadStart + DownloadDone) + 1
  619. End Get
  620. End Property
  621. ''' <summary>
  622. ''' 线程已下载的文件大小。
  623. ''' </summary>
  624. Public DownloadDone As Long = 0
  625. ''' <summary>
  626. ''' 上次记速时的时间。
  627. ''' </summary>
  628. Private SpeedLastTime As Long = GetTimeTick()
  629. ''' <summary>
  630. ''' 上次记速时的已下载大小。
  631. ''' </summary>
  632. Private SpeedLastDone As Long = 0
  633. ''' <summary>
  634. ''' 当前的下载速度,单位为 Byte / 秒。
  635. ''' </summary>
  636. Public ReadOnly Property Speed As Long
  637. Get
  638. If GetTimeTick() - SpeedLastTime > 200 Then
  639. Dim DeltaTime As Long = GetTimeTick() - SpeedLastTime
  640. _Speed = (DownloadDone - SpeedLastDone) / (DeltaTime / 1000)
  641. SpeedLastDone = DownloadDone
  642. SpeedLastTime += DeltaTime
  643. End If
  644. Return _Speed
  645. End Get
  646. End Property
  647. Private _Speed As Long = 0
  648. ''' <summary>
  649. ''' 线程初始化时的时间。
  650. ''' </summary>
  651. Public InitTime As Long = GetTimeTick()
  652. ''' <summary>
  653. ''' 上次接受到有效数据的时间,-1 表示尚未有有效数据。
  654. ''' </summary>
  655. Public LastReceiveTime As Long = -1
  656. ''' <summary>
  657. ''' 当前线程的状态。
  658. ''' </summary>
  659. Public State As NetState = NetState.WaitForDownload
  660. ''' <summary>
  661. ''' 是否已经结束。
  662. ''' </summary>
  663. Public ReadOnly Property IsEnded As Boolean
  664. Get
  665. Return State = NetState.Finish OrElse State = NetState.Error
  666. End Get
  667. End Property
  668. ''' <summary>
  669. ''' 当前选取的是哪一个 Url。
  670. ''' </summary>
  671. Public Source As NetSource
  672. End Class
  673. ''' <summary>
  674. ''' 下载单个文件。
  675. ''' </summary>
  676. Public Class NetFile
  677. #Region "属性"
  678. ''' <summary>
  679. ''' 所属的文件列表任务。
  680. ''' </summary>
  681. Public Tasks As New SynchronizedCollection(Of LoaderDownload)
  682. ''' <summary>
  683. ''' 所有下载源。
  684. ''' </summary>
  685. Public Sources As NetSource()
  686. ''' <summary>
  687. ''' 用于在第一个线程出错时切换下载源。
  688. ''' </summary>
  689. Private FirstThreadSource As Integer = 0
  690. ''' <summary>
  691. ''' 所有已经被标记为失败的,但未完整尝试过的,不允许断点续传的下载源。
  692. ''' </summary>
  693. Public SourcesOnce As New List(Of NetSource)
  694. ''' <summary>
  695. ''' 获取从某个源开始,第一个可用的源。
  696. ''' </summary>
  697. Private Function GetSource(Optional Id As Integer = 0) As NetSource
  698. If Id >= Sources.Count OrElse Id < 0 Then Id = 0
  699. SyncLock LockSource
  700. If Not IsSourceFailed(False) Then
  701. '存在多线程可用源
  702. Dim CurrentSource As NetSource = Sources(Id)
  703. While CurrentSource.IsFailed
  704. Id += 1
  705. If Id >= Sources.Count Then Id = 0
  706. CurrentSource = Sources(Id)
  707. End While
  708. Return CurrentSource
  709. ElseIf SourcesOnce.Any Then
  710. '仅存在单线程可用源
  711. Return SourcesOnce(0)
  712. Else
  713. '没有可用源
  714. Return Nothing
  715. End If
  716. End SyncLock
  717. End Function
  718. ''' <summary>
  719. ''' 是否已经没有可用源了。
  720. ''' </summary>
  721. Public Function IsSourceFailed(Optional AllowOnceSource As Boolean = True) As Boolean
  722. If AllowOnceSource AndAlso SourcesOnce.Any Then Return False
  723. SyncLock LockSource
  724. For Each Source As NetSource In Sources
  725. If Not Source.IsFailed Then Return False
  726. Next
  727. End SyncLock
  728. Return True
  729. End Function
  730. ''' <summary>
  731. ''' 存储在本地的带文件名的地址。
  732. ''' </summary>
  733. Public LocalPath As String = Nothing
  734. ''' <summary>
  735. ''' 存储在本地的文件名。
  736. ''' </summary>
  737. Public LocalName As String = Nothing
  738. ''' <summary>
  739. ''' 当前的下载状态。
  740. ''' </summary>
  741. Public State As NetState = NetState.WaitForCheck
  742. ''' <summary>
  743. ''' 导致下载失败的原因。
  744. ''' </summary>
  745. Public Ex As New List(Of Exception)
  746. ''' <summary>
  747. ''' 作为文件组成部分的线程链表。
  748. ''' 如果没有线程,可以为 Nothing。
  749. ''' </summary>
  750. Public Threads As NetThread
  751. ''' <summary>
  752. ''' 文件的总大小。若为 -2 则为未获取,若为 -1 则为无法获取准确大小。
  753. ''' </summary>
  754. Public FileSize As Long = -2
  755. ''' <summary>
  756. ''' 该文件是否无法获取准确大小。
  757. ''' </summary>
  758. Public IsUnknownSize As Boolean = False
  759. ''' <summary>
  760. ''' 该文件是否不需要分割。
  761. ''' </summary>
  762. Public ReadOnly Property IsNoSplit As Boolean
  763. Get
  764. Return IsUnknownSize OrElse FileSize < FilePieceLimit
  765. End Get
  766. End Property
  767. ''' <summary>
  768. ''' 为不需要分割的小文件进行临时存储。
  769. ''' </summary>
  770. Private SmailFileCache As Queue(Of Byte)
  771. ''' <summary>
  772. ''' 文件的已下载大小。
  773. ''' </summary>
  774. Public DownloadDone As Long = 0
  775. Private ReadOnly LockDone As New Object
  776. ''' <summary>
  777. ''' 文件的校验规则。
  778. ''' </summary>
  779. Public Check As FileChecker
  780. ''' <summary>
  781. ''' 下载时是否添加浏览器 UA。
  782. ''' </summary>
  783. Public UseBrowserUserAgent As Boolean
  784. ''' <summary>
  785. ''' 上次记速时的时间。
  786. ''' </summary>
  787. Private SpeedLastTime As Long = GetTimeTick()
  788. ''' <summary>
  789. ''' 上次记速时的已下载大小。
  790. ''' </summary>
  791. Private SpeedLastDone As Long = 0
  792. ''' <summary>
  793. ''' 当前的下载速度,单位为 Byte / 秒。
  794. ''' </summary>
  795. Public ReadOnly Property Speed As Long
  796. Get
  797. If GetTimeTick() - SpeedLastTime > 200 Then
  798. Dim DeltaTime As Long = GetTimeTick() - SpeedLastTime
  799. _Speed = (DownloadDone - SpeedLastDone) / (DeltaTime / 1000)
  800. SpeedLastDone = DownloadDone
  801. SpeedLastTime += DeltaTime
  802. End If
  803. Return _Speed
  804. End Get
  805. End Property
  806. Private _Speed As Long = 0
  807. ''' <summary>
  808. ''' 该文件是否由本地文件直接拷贝完成。
  809. ''' </summary>
  810. Public IsCopy As Boolean = False
  811. ''' <summary>
  812. ''' 本文件的显示进度。
  813. ''' </summary>
  814. Public ReadOnly Property Progress As Double
  815. Get
  816. Select Case State
  817. Case NetState.WaitForCheck
  818. Return 0
  819. Case NetState.WaitForCopy
  820. Return 0.2
  821. Case NetState.WaitForDownload
  822. Return 0.01
  823. Case NetState.Connect
  824. Return 0.02
  825. Case NetState.Get
  826. Return 0.04
  827. Case NetState.Download
  828. '正在下载中,对应 5% ~ 98%
  829. Dim OriginalProgress As Double = If(IsUnknownSize, 0.5, DownloadDone / Math.Max(FileSize, 1))
  830. OriginalProgress = 1 - (1 - OriginalProgress) ^ 0.9
  831. Return OriginalProgress * 0.93 + 0.05
  832. Case NetState.Merge
  833. Return 0.99
  834. Case NetState.Finish, NetState.Error
  835. Return 1
  836. Case Else
  837. Throw New ArgumentOutOfRangeException("文件状态未知:" & State)
  838. End Select
  839. End Get
  840. End Property
  841. ''' <summary>
  842. ''' 各个线程建立连接成功的总次数。
  843. ''' </summary>
  844. Private ConnectCount As Integer = 0
  845. ''' <summary>
  846. ''' 各个线程建立连接成功的总时间。
  847. ''' </summary>
  848. Private ConnectTime As Long = 0
  849. ''' <summary>
  850. ''' 各个线程建立连接成功的平均时间,单位为毫秒,-1 代表尚未有成功连接。
  851. ''' </summary>
  852. Private ReadOnly Property ConnectAverage As Integer
  853. Get
  854. SyncLock LockCount
  855. Return If(ConnectCount = 0, -1, ConnectTime / ConnectCount)
  856. End SyncLock
  857. End Get
  858. End Property
  859. Private Const FilePieceLimit As Long = 256 * 1024
  860. Public ReadOnly LockCount As New Object
  861. Public ReadOnly LockState As New Object
  862. Public ReadOnly LockChain As New Object
  863. Public ReadOnly LockSource As New Object
  864. Public ReadOnly Uuid As Integer = GetUuid()
  865. Public Overrides Function Equals(obj As Object) As Boolean
  866. Dim file = TryCast(obj, NetFile)
  867. Return file IsNot Nothing AndAlso Uuid = file.Uuid
  868. End Function
  869. #End Region
  870. ''' <summary>
  871. ''' 新建一个需要下载的文件。
  872. ''' </summary>
  873. ''' <param name="LocalPath">包含文件名的本地地址。</param>
  874. Public Sub New(Urls As IEnumerable(Of String), LocalPath As String, Optional Check As FileChecker = Nothing, Optional UseBrowserUserAgent As Boolean = False)
  875. Dim Sources As New List(Of NetSource)
  876. Dim Count As Integer = 0
  877. Urls = Urls.Distinct.ToArray
  878. For Each Source As String In Urls
  879. Sources.Add(New NetSource With {.FailCount = 0, .Url = SecretCdnSign(Source.Replace(vbCr, "").Replace(vbLf, "").Trim), .Id = Count, .IsFailed = False, .Ex = Nothing})
  880. Count += 1
  881. Next
  882. Me.Sources = Sources.ToArray
  883. Me.LocalPath = LocalPath
  884. Me.Check = Check
  885. Me.UseBrowserUserAgent = UseBrowserUserAgent
  886. Me.LocalName = GetFileNameFromPath(LocalPath)
  887. End Sub
  888. ''' <summary>
  889. ''' 尝试开始一个新的下载线程。
  890. ''' 如果失败,返回 Nothing。
  891. ''' </summary>
  892. Public Function TryBeginThread() As NetThread
  893. Try
  894. '条件检测
  895. If NetTaskThreadCount >= NetTaskThreadLimit OrElse IsSourceFailed() OrElse
  896. (IsNoSplit AndAlso Threads IsNot Nothing AndAlso Threads.State <> NetState.Error) Then Return Nothing
  897. If State >= NetState.Merge OrElse State = NetState.WaitForCheck Then Return Nothing
  898. SyncLock LockState
  899. If State < NetState.Connect Then State = NetState.Connect
  900. End SyncLock
  901. '初始化参数
  902. Dim StartPosition As Long, StartSource As NetSource = Nothing
  903. Dim Th As Thread, ThreadInfo As NetThread
  904. SyncLock LockChain
  905. '获取线程起点与下载源
  906. '不分割
  907. If IsNoSplit Then GoTo Capture
  908. '单线程
  909. If IsSourceFailed(False) Then
  910. '确认没有其他线程正使用此点
  911. If SourcesOnce(0).Thread IsNot Nothing AndAlso SourcesOnce(0).Thread.State <> NetState.Error Then Return Nothing
  912. '占用此点
  913. Capture:
  914. SmailFileCache = Nothing
  915. Threads = Nothing
  916. NetManager.DownloadDone -= DownloadDone
  917. SyncLock LockDone
  918. DownloadDone = 0
  919. End SyncLock
  920. SpeedLastDone = 0
  921. State = NetState.Get
  922. End If
  923. '首个开始点
  924. If Threads Is Nothing Then
  925. StartPosition = 0
  926. StartSource = GetSource(FirstThreadSource)
  927. FirstThreadSource = StartSource.Id + 1
  928. GoTo StartThread
  929. End If
  930. '寻找失败点
  931. For Each Thread As NetThread In Threads
  932. If Thread.State = NetState.Error AndAlso Thread.DownloadUndone > 0 Then
  933. StartPosition = Thread.DownloadStart + Thread.DownloadDone
  934. StartSource = GetSource(Thread.Source.Id + 1)
  935. GoTo StartThread
  936. End If
  937. Next
  938. '是否禁用多线程,以及规定碎片大小
  939. Dim TargetUrl As String = GetSource().Url
  940. If TargetUrl.Contains("pcl2-server") OrElse TargetUrl.Contains("gitcode.net") OrElse TargetUrl.Contains("github.com") OrElse TargetUrl.Contains("modrinth") Then Return Nothing
  941. '寻找最大碎片
  942. 'FUTURE: 下载引擎重做,计算下载源平均链接时间和线程下载速度,按最高时间节省来开启多线程
  943. Dim FilePieceMax As NetThread = Threads
  944. For Each Thread As NetThread In Threads
  945. If Thread.DownloadUndone > FilePieceMax.DownloadUndone Then FilePieceMax = Thread
  946. Next
  947. If FilePieceMax Is Nothing OrElse FilePieceMax.DownloadUndone < FilePieceLimit Then Return Nothing
  948. StartPosition = FilePieceMax.DownloadEnd - FilePieceMax.DownloadUndone * 0.4
  949. StartSource = GetSource()
  950. '开始线程
  951. StartThread:
  952. If (StartPosition > FileSize AndAlso FileSize >= 0 AndAlso Not IsUnknownSize) OrElse StartPosition < 0 OrElse IsNothing(StartSource) Then Return Nothing
  953. '构建线程
  954. Dim ThreadUuid As Integer = GetUuid()
  955. If Not Tasks.Any() Then Return Nothing '由于中断,已没有可用任务
  956. Th = New Thread(AddressOf Thread) With {.Name = $"NetTask {Tasks(0).Uuid}/{Uuid} Download {ThreadUuid}#", .Priority = ThreadPriority.BelowNormal}
  957. ThreadInfo = New NetThread With {.Uuid = ThreadUuid, .DownloadStart = StartPosition, .Thread = Th, .Source = StartSource, .Task = Me, .State = NetState.WaitForDownload}
  958. '链表处理
  959. If ThreadInfo.IsFirstThread OrElse Threads Is Nothing Then
  960. Threads = ThreadInfo
  961. Else
  962. Dim CurrentChain As NetThread = Threads
  963. While CurrentChain.DownloadEnd <= StartPosition
  964. CurrentChain = CurrentChain.NextThread
  965. End While
  966. ThreadInfo.NextThread = CurrentChain.NextThread
  967. CurrentChain.NextThread = ThreadInfo
  968. End If
  969. End SyncLock
  970. '开始线程
  971. SyncLock NetTaskThreadCountLock
  972. NetTaskThreadCount += 1
  973. End SyncLock
  974. SyncLock LockSource
  975. If IsSourceFailed(False) Then SourcesOnce(0).Thread = ThreadInfo
  976. End SyncLock
  977. Th.Start(ThreadInfo)
  978. Return ThreadInfo
  979. Catch ex As Exception
  980. Log(ex, "尝试开始下载线程失败(" & If(LocalName, "Nothing") & ")", LogLevel.Hint)
  981. Return Nothing
  982. End Try
  983. End Function
  984. ''' <summary>
  985. ''' 每个下载线程执行的代码。
  986. ''' </summary>
  987. Private Sub Thread(Info As NetThread)
  988. If ModeDebug OrElse Info.DownloadStart = 0 Then Log("[Download] " & LocalName & " " & Info.Uuid & "#:开始,起始点 " & Info.DownloadStart & "," & Info.Source.Url)
  989. Dim HttpRequest As HttpWebRequest
  990. Dim ResultStream As Stream = Nothing
  991. '部分下载源真的特别慢,并且只需要一个请求,例如 Ping 为 20s,如果增长太慢,就会造成类似 2.5s 5s 7.5s 10s 12.5s... 的极大延迟
  992. '延迟过长会导致某些特别慢的链接迟迟不被掐死
  993. Dim Timeout As Integer = Math.Min(Math.Max(ConnectAverage, 6000) * (1 + Info.Source.FailCount), 30000)
  994. Info.State = NetState.Connect
  995. Try
  996. Dim HttpDataCount As Integer = 0
  997. If SourcesOnce.Contains(Info.Source) AndAlso Not Info.Equals(Info.Source.Thread) Then GoTo SourceBreak
  998. '请求头
  999. HttpRequest = WebRequest.Create(Info.Source.Url)
  1000. If Info.Source.Url.StartsWithF("https", True) Then HttpRequest.ProtocolVersion = HttpVersion.Version11
  1001. 'HttpRequest.Proxy = Nothing 'new WebProxy(Ip, Port)
  1002. HttpRequest.Timeout = Timeout
  1003. HttpRequest.AddRange(Info.DownloadStart)
  1004. SecretHeadersSign(Info.Source.Url, HttpRequest, UseBrowserUserAgent)
  1005. Dim ContentLength As Long = 0
  1006. Using HttpResponse As HttpWebResponse = HttpRequest.GetResponse()
  1007. If State = NetState.Error Then GoTo SourceBreak '快速中断
  1008. If ModeDebug AndAlso HttpResponse.ResponseUri.OriginalString <> Info.Source.Url Then
  1009. Log($"[Download] {LocalName} {Info.Uuid}#:重定向至 {HttpResponse.ResponseUri.OriginalString}")
  1010. End If
  1011. ''从响应头获取文件名
  1012. 'If Info.IsFirstThread Then
  1013. ' Dim FileName As String = GetFileNameFromResponse(HttpResponse)
  1014. ' If ModeDebug Then Log($"[Download] {LocalName} {Info.Uuid}#:远程文件名:{If(FileName, "未提供")}")
  1015. ' If FileName IsNot Nothing AndAlso LocalName = "待定" Then
  1016. ' LocalName = FileName
  1017. ' Log($"[Download] {LocalName} {Info.Uuid}#:从响应头获取到文件名")
  1018. ' End If
  1019. 'End If
  1020. '文件大小校验
  1021. ContentLength = HttpResponse.ContentLength
  1022. If ContentLength = -1 Then
  1023. If FileSize > 1 Then
  1024. If Info.DownloadStart = 0 Then
  1025. Log($"[Download] {LocalName} {Info.Uuid}#:文件大小未知,但已从其他下载源获取,不作处理")
  1026. Else
  1027. Log($"[Download] {LocalName} {Info.Uuid}#:ContentLength 返回了 -1,无法确定是否支持分段下载,视作不支持")
  1028. GoTo NotSupportRange
  1029. End If
  1030. Else
  1031. FileSize = -1 : IsUnknownSize = True
  1032. Log($"[Download] {LocalName} {Info.Uuid}#:文件大小未知")
  1033. End If
  1034. ElseIf ContentLength < 0 Then
  1035. Throw New Exception("获取片大小失败,结果为 " & ContentLength & "。")
  1036. ElseIf Info.IsFirstThread Then
  1037. If Check IsNot Nothing Then
  1038. If ContentLength < Check.MinSize AndAlso Check.MinSize > 0 Then
  1039. Throw New Exception($"文件大小不足,获取结果为 {ContentLength},要求至少为 {Check.MinSize}。")
  1040. End If
  1041. If ContentLength <> Check.ActualSize AndAlso Check.ActualSize > 0 Then
  1042. Throw New Exception($"文件大小不一致,获取结果为 {ContentLength},要求必须为 {Check.ActualSize}。")
  1043. End If
  1044. End If
  1045. FileSize = ContentLength : IsUnknownSize = False
  1046. Log($"[Download] {LocalName} {Info.Uuid}#:文件大小 {ContentLength}({GetString(ContentLength)})")
  1047. '若文件大小大于 50 M,进行剩余磁盘空间校验
  1048. If ContentLength > 50 * 1024 * 1024 Then
  1049. For Each Drive As DriveInfo In DriveInfo.GetDrives
  1050. Dim DriveName As String = Drive.Name.First.ToString
  1051. Dim RequiredSpace = If(PathTemp.StartsWithF(DriveName), ContentLength * 1.1, 0) +
  1052. If(LocalPath.StartsWithF(DriveName), ContentLength + 5 * 1024 * 1024, 0)
  1053. If Drive.TotalFreeSpace < RequiredSpace Then
  1054. Throw New Exception(DriveName & " 盘空间不足,无法进行下载。" & vbCrLf & "需要至少 " & GetString(RequiredSpace) & " 空间,但当前仅剩余 " & GetString(Drive.TotalFreeSpace) & "。" &
  1055. If(PathTemp.StartsWithF(DriveName), vbCrLf & vbCrLf & "下载时需要与文件同等大小的空间存放缓存,你可以在设置中调整缓存文件夹的位置。", ""))
  1056. End If
  1057. Next
  1058. End If
  1059. ElseIf FileSize < 0 Then
  1060. Throw New Exception("非首线程运行时,尚未获取文件大小")
  1061. ElseIf Info.DownloadStart > 0 AndAlso ContentLength = FileSize Then
  1062. NotSupportRange:
  1063. SyncLock LockSource
  1064. If SourcesOnce.Contains(Info.Source) Then
  1065. GoTo SourceBreak
  1066. Else
  1067. SourcesOnce.Add(Info.Source)
  1068. End If
  1069. End SyncLock
  1070. Throw New WebException($"该下载源不支持分段下载:Range 起始于 {Info.DownloadStart},预期 ContentLength 为 {FileSize - Info.DownloadStart},返回 ContentLength 为 {ContentLength},总文件大小 {FileSize}")
  1071. ElseIf Not FileSize - Info.DownloadStart = ContentLength Then
  1072. Throw New WebException($"获取到的分段大小不一致:Range 起始于 {Info.DownloadStart},预期 ContentLength 为 {FileSize - Info.DownloadStart},返回 ContentLength 为 {ContentLength},总文件大小 {FileSize}")
  1073. End If
  1074. 'Log($"[Download] {LocalName} {Info.Uuid}#:通过大小检查,文件大小 {FileSize},起始点 {Info.DownloadStart},ContentLength {ContentLength}")
  1075. Info.State = NetState.Get
  1076. SyncLock LockState
  1077. If State < NetState.Get Then State = NetState.Get
  1078. End SyncLock
  1079. '创建缓存文件
  1080. If IsNoSplit Then
  1081. Info.Temp = Nothing
  1082. SmailFileCache = New Queue(Of Byte)
  1083. Else
  1084. Info.Temp = PathTemp & "Download\" & Uuid & "_" & Info.Uuid & "_" & RandomInteger(0, 999999) & ".tmp"
  1085. ResultStream = New FileStream(Info.Temp, FileMode.Create, FileAccess.Write, FileShare.Read)
  1086. End If
  1087. '开始下载
  1088. Using HttpStream = HttpResponse.GetResponseStream()
  1089. HttpStream.ReadTimeout = Timeout
  1090. If Setup.Get("SystemDebugDelay") Then Threading.Thread.Sleep(RandomInteger(50, 3000))
  1091. Dim HttpData As Byte() = New Byte(16384) {}
  1092. HttpDataCount = HttpStream.Read(HttpData, 0, 16384)
  1093. While (IsUnknownSize OrElse Info.DownloadUndone > 0) AndAlso '判断是否下载完成
  1094. HttpDataCount > 0 AndAlso Not IsProgramEnded AndAlso State < NetState.Merge AndAlso (Not Info.Source.IsFailed OrElse Info.Equals(Info.Source.Thread))
  1095. '限速
  1096. While NetTaskSpeedLimitHigh > 0 AndAlso NetTaskSpeedLimitLeft <= 0
  1097. Threading.Thread.Sleep(16)
  1098. End While
  1099. Dim RealDataCount As Integer = If(IsUnknownSize, HttpDataCount, Math.Min(HttpDataCount, Info.DownloadUndone))
  1100. SyncLock NetTaskSpeedLimitLeftLock
  1101. If NetTaskSpeedLimitHigh > 0 Then NetTaskSpeedLimitLeft -= RealDataCount
  1102. End SyncLock
  1103. Dim DeltaTime = GetTimeTick() - Info.LastReceiveTime
  1104. If DeltaTime > 1000000 Then DeltaTime = 1 '时间刻反转导致出现极大值
  1105. If RealDataCount > 0 Then
  1106. '有数据
  1107. If Info.DownloadDone = 0 Then
  1108. '第一次接受到数据
  1109. Info.State = NetState.Download
  1110. SyncLock LockState
  1111. If State < NetState.Download Then State = NetState.Download
  1112. End SyncLock
  1113. SyncLock LockCount
  1114. ConnectCount += 1
  1115. ConnectTime += GetTimeTick() - Info.InitTime
  1116. End SyncLock
  1117. End If
  1118. SyncLock LockCount
  1119. Info.Source.FailCount = 0
  1120. For Each Task In Tasks
  1121. Task.FailCount = 0
  1122. Next
  1123. End SyncLock
  1124. NetManager.DownloadDone += RealDataCount
  1125. SyncLock LockDone
  1126. DownloadDone += RealDataCount
  1127. End SyncLock
  1128. Info.DownloadDone += RealDataCount
  1129. If IsNoSplit Then
  1130. If HttpData.Count = RealDataCount Then
  1131. 'SmailFileCache.AddRange(HttpData)
  1132. For Each B In HttpData
  1133. SmailFileCache.Enqueue(B)
  1134. Next
  1135. Else
  1136. 'SmailFileCache.AddRange(HttpData.ToList.GetRange(0, RealDataCount))
  1137. For i = 0 To RealDataCount - 1
  1138. SmailFileCache.Enqueue(HttpData(i))
  1139. Next
  1140. End If
  1141. Else
  1142. ResultStream.Write(HttpData, 0, RealDataCount)
  1143. End If
  1144. '检查速度是否过慢
  1145. If DeltaTime > 1500 AndAlso DeltaTime > RealDataCount Then '数据包间隔大于 1.5s,且速度小于 1.5K/s
  1146. Throw New TimeoutException("由于速度过慢断开链接,下载 " & RealDataCount & " B,消耗 " & DeltaTime & " ms。")
  1147. End If
  1148. Info.LastReceiveTime = GetTimeTick()
  1149. '已完成
  1150. If Info.DownloadUndone = 0 AndAlso Not IsUnknownSize Then Exit While
  1151. ElseIf Info.LastReceiveTime > 0 AndAlso DeltaTime > Timeout Then
  1152. '无数据,且已超时
  1153. Throw New TimeoutException("操作超时,无数据。")
  1154. End If
  1155. HttpDataCount = HttpStream.Read(HttpData, 0, 16384)
  1156. End While
  1157. End Using
  1158. End Using
  1159. SourceBreak:
  1160. If State = NetState.Error OrElse Info.Source.IsFailed OrElse (Info.DownloadUndone > 0 AndAlso Not IsUnknownSize) Then
  1161. '被外部中断
  1162. Info.State = NetState.Error
  1163. Log($"[Download] {LocalName} {Info.Uuid}#:中断")
  1164. ElseIf HttpDataCount = 0 AndAlso Info.DownloadUndone > 0 AndAlso Not IsUnknownSize Then
  1165. '服务器无返回数据
  1166. Throw New Exception($"返回的 ContentLength 过多:ContentLength 为 {ContentLength},但获取到的总数据量仅为 {Info.DownloadDone}(全文件总数据量 {DownloadDone})")
  1167. Else
  1168. '本线程完成
  1169. Info.State = NetState.Finish
  1170. If ModeDebug Then Log($"[Download] {LocalName} {Info.Uuid}#:完成,已下载 {Info.DownloadDone}")
  1171. End If
  1172. Catch ex As Exception
  1173. '状态变更
  1174. SyncLock LockCount
  1175. Info.Source.FailCount += 1
  1176. For Each Task In Tasks
  1177. Task.FailCount += 1
  1178. Next
  1179. End SyncLock
  1180. Dim IsTimeoutString As String = GetExceptionSummary(ex).ToLower.Replace(" ", "")
  1181. Dim IsTimeout As Boolean = IsTimeoutString.Contains("由于连接方在一段时间后没有正确答复或连接的主机没有反应") OrElse
  1182. IsTimeoutString.Contains("超时") OrElse IsTimeoutString.Contains("timeout") OrElse IsTimeoutString.Contains("timedout")
  1183. Log("[Download] " & LocalName & " " & Info.Uuid & If(IsTimeout, "#:超时(" & (Timeout * 0.001) & "s)", "#:出错," & GetExceptionDetail(ex)))
  1184. Info.State = NetState.Error
  1185. ''使用该下载源的线程是否没有速度
  1186. ''下载超时也会导致没有速度,容易误判下载失败,所以已弃用此方法
  1187. 'Dim IsNoSpeed As Boolean = True
  1188. 'SyncLock LockChain
  1189. ' If Threads IsNot Nothing Then
  1190. ' For Each Thread As NetThread In Threads
  1191. ' If Thread.Source.Id = Info.Source.Id AndAlso Thread.Speed > 0 Then
  1192. ' IsNoSpeed = False
  1193. ' Exit For
  1194. ' End If
  1195. ' Next
  1196. ' End If
  1197. 'End SyncLock
  1198. Info.Source.Ex = ex
  1199. '根据情况判断,是否在多线程下禁用下载源(连续错误过多,或不支持断点续传)
  1200. If ex.Message.Contains("该下载源不支持") OrElse ex.Message.Contains("未能解析") OrElse ex.Message.Contains("(404)") OrElse
  1201. ex.Message.Contains("(502)") OrElse ex.Message.Contains("无返回数据") OrElse ex.Message.Contains("空间不足") OrElse ex.Message.Contains("获取到的分段大小不一致") OrElse
  1202. (ex.Message.Contains("(403)") AndAlso Not Info.Source.Url.ContainsF("bmclapi")) OrElse 'BMCLAPI 的部分源在高频率请求下会返回 403,所以不应因此禁用下载源
  1203. (Info.Source.FailCount >= MathClamp(NetTaskThreadLimit, 5, 30) AndAlso DownloadDone < 1) OrElse
  1204. Info.Source.FailCount > NetTaskThreadLimit + 2 Then
  1205. Dim IsThisFail As Boolean = False
  1206. SyncLock LockSource
  1207. If Info.Source.Thread IsNot Nothing AndAlso Info.Source.Thread.Equals(Info) Then
  1208. '单线程下,本线程出错
  1209. SourcesOnce.RemoveAt(0)
  1210. GoTo Wrong
  1211. ElseIf Not Info.Source.IsFailed Then
  1212. '多线程下,本线程出错
  1213. Wrong:
  1214. Info.Source.IsFailed = True
  1215. IsThisFail = True
  1216. End If
  1217. End SyncLock
  1218. '本线程引发下载源被禁用
  1219. If IsThisFail Then
  1220. Log($"[Download] {LocalName} {Info.Uuid}#:下载源被禁用({Info.Source.Id}):{Info.Source.Url}")
  1221. Log(ex, "下载源 " & Info.Source.Id & " 已被禁用", If(ex.Message.Contains("不支持分段下载") OrElse ex.Message.Contains("(404)") OrElse ex.Message.Contains("(416)"), LogLevel.Developer, LogLevel.Debug))
  1222. If IsSourceFailed() Then
  1223. '没有可用源
  1224. Log("[Download] 文件 " & LocalName & " 已无可用下载源")
  1225. Dim ExampleEx As Exception = Nothing
  1226. SyncLock LockSource
  1227. For Each Source As NetSource In Sources
  1228. Log("[Download] 已禁用的下载源:" & Source.Url)
  1229. If Source.Ex IsNot Nothing Then
  1230. ExampleEx = Source.Ex
  1231. Log(Source.Ex, "下载源禁用原因", LogLevel.Developer)
  1232. End If
  1233. Next
  1234. End SyncLock
  1235. Fail(ExampleEx)
  1236. ElseIf ex.Message.Contains("空间不足") Then
  1237. '没有空间
  1238. Fail(ex)
  1239. End If
  1240. End If
  1241. End If
  1242. '首线程错误
  1243. If FileSize = -2 Then
  1244. SyncLock LockChain
  1245. Threads = Nothing
  1246. End SyncLock
  1247. End If
  1248. Finally
  1249. If ResultStream IsNot Nothing Then ResultStream.Dispose()
  1250. SyncLock NetTaskThreadCountLock
  1251. NetTaskThreadCount -= 1
  1252. End SyncLock
  1253. '可能在没有下载完的时候开始合并文件了,这造成了大多数合并失败
  1254. If ((FileSize >= 0 AndAlso DownloadDone >= FileSize) OrElse (FileSize = -1 AndAlso DownloadDone > 0)) AndAlso State < NetState.Merge Then Merge()
  1255. End Try
  1256. End Sub
  1257. ''' <summary>
  1258. ''' 从 HTTP 响应头中获取文件名。
  1259. ''' 如果没有,返回 Nothing。
  1260. ''' </summary>
  1261. Private Function GetFileNameFromResponse(response As HttpWebResponse) As String
  1262. Dim header As String = response.Headers("Content-Disposition")
  1263. If String.IsNullOrEmpty(header) Then Return Nothing
  1264. 'attachment; filename="filename.ext"
  1265. If Not header.Contains("filename=") Then Return Nothing
  1266. Return header.After("filename=").Trim(""""c, " "c).Before(";")
  1267. End Function
  1268. '下载文件的最终收束事件
  1269. ''' <summary>
  1270. ''' 下载完成。合并文件。
  1271. ''' </summary>
  1272. Private Sub Merge()
  1273. '状态判断
  1274. SyncLock LockState
  1275. If State < NetState.Merge Then
  1276. State = NetState.Merge
  1277. Else
  1278. Exit Sub
  1279. End If
  1280. End SyncLock
  1281. Dim RetryCount As Integer = 0
  1282. Dim MergeFile As Stream = Nothing, AddWriter As BinaryWriter = Nothing
  1283. Try
  1284. Retry:
  1285. SyncLock LockChain
  1286. '创建文件夹
  1287. If File.Exists(LocalPath) Then File.Delete(LocalPath)
  1288. Dim Info As New FileInfo(LocalPath)
  1289. Info.Directory.Create()
  1290. '合并文件
  1291. If IsNoSplit Then
  1292. '仅有一个线程,从缓存中输出
  1293. If ModeDebug Then Log($"[Download] {LocalName}:下载结束,从缓存输出文件,长度:" & SmailFileCache.Count)
  1294. MergeFile = New FileStream(LocalPath, FileMode.Create)
  1295. AddWriter = New BinaryWriter(MergeFile)
  1296. AddWriter.Write(SmailFileCache.ToArray)
  1297. AddWriter.Dispose() : AddWriter = Nothing
  1298. MergeFile.Dispose() : MergeFile = Nothing
  1299. ElseIf Threads.DownloadDone = DownloadDone AndAlso Threads.Temp IsNot Nothing Then
  1300. '仅有一个文件,直接复制
  1301. If ModeDebug Then Log($"[Download] {LocalName}:下载结束,仅有一个文件,无需合并")
  1302. CopyFile(Threads.Temp, LocalPath)
  1303. Else
  1304. '有多个线程,合并
  1305. If ModeDebug Then Log($"[Download] {LocalName}:下载结束,开始合并文件")
  1306. MergeFile = New FileStream(LocalPath, FileMode.Create)
  1307. AddWriter = New BinaryWriter(MergeFile)
  1308. For Each Thread As NetThread In Threads
  1309. If Thread.DownloadDone = 0 OrElse Thread.Temp Is Nothing Then Continue For
  1310. Using fs As New FileStream(Thread.Temp, FileMode.Open, FileAccess.Read, FileShare.Read)
  1311. Using TempReader As New BinaryReader(fs)
  1312. AddWriter.Write(TempReader.ReadBytes(Thread.DownloadDone))
  1313. End Using
  1314. End Using
  1315. Next
  1316. AddWriter.Dispose() : AddWriter = Nothing
  1317. MergeFile.Dispose() : MergeFile = Nothing
  1318. End If
  1319. '写入大小要求
  1320. If Not IsUnknownSize AndAlso Check IsNot Nothing Then
  1321. If Check.ActualSize = -1 Then
  1322. Check.ActualSize = FileSize
  1323. ElseIf Check.ActualSize <> FileSize Then
  1324. Throw New Exception($"文件大小不一致:任务要求为 {Check.ActualSize} B,网络获取结果为 {FileSize}B")
  1325. End If
  1326. End If
  1327. '检查文件
  1328. Dim CheckResult As String = Check?.Check(LocalPath)
  1329. If CheckResult IsNot Nothing Then
  1330. Log($"[Download] {LocalName} 文件校验失败,下载线程细节:")
  1331. For Each Th As NetThread In Threads
  1332. Log($"[Download] {Th.Uuid}#,状态 {GetStringFromEnum(Th.State)},范围 {Th.DownloadStart}~{Th.DownloadStart + Th.DownloadDone},完成 {Th.DownloadDone},剩余 {Th.DownloadUndone}")
  1333. Next
  1334. Throw New Exception(CheckResult)
  1335. End If
  1336. '后处理
  1337. If IsNoSplit Then
  1338. SmailFileCache = Nothing
  1339. Else
  1340. For Each Thread As NetThread In Threads
  1341. If Thread.Temp IsNot Nothing Then File.Delete(Thread.Temp)
  1342. Next
  1343. End If
  1344. Finish()
  1345. End SyncLock
  1346. Catch ex As Exception
  1347. Log(ex, "合并文件出错(" & LocalName & ")")
  1348. If MergeFile IsNot Nothing Then
  1349. MergeFile.Dispose() : MergeFile = Nothing
  1350. End If
  1351. If AddWriter IsNot Nothing Then
  1352. AddWriter.Dispose() : AddWriter = Nothing
  1353. End If
  1354. '重试
  1355. If RetryCount <= 3 Then
  1356. Threading.Thread.Sleep(RandomInteger(500, 1000))
  1357. RetryCount += 1
  1358. GoTo Retry
  1359. End If
  1360. Fail(ex)
  1361. End Try
  1362. End Sub
  1363. ''' <summary>
  1364. ''' 下载失败。
  1365. ''' </summary>
  1366. Private Sub Fail(Optional RaiseEx As Exception = Nothing)
  1367. SyncLock LockState
  1368. If State >= NetState.Finish Then Exit Sub
  1369. If RaiseEx IsNot Nothing Then Ex.Add(RaiseEx)
  1370. '凉凉
  1371. State = NetState.Error
  1372. End SyncLock
  1373. InterruptAndDelete()
  1374. For Each Task In Tasks
  1375. Task.OnFileFail(Me)
  1376. Next
  1377. End Sub
  1378. ''' <summary>
  1379. ''' 下载中断。
  1380. ''' </summary>
  1381. Public Sub Abort(CausedByTask As LoaderDownload)
  1382. '从特定任务中移除,如果它还属于其他任务,则继续下载
  1383. Tasks.Remove(CausedByTask)
  1384. If Tasks.Any Then Exit Sub
  1385. '确认中断
  1386. SyncLock LockState
  1387. If State >= NetState.Finish Then Exit Sub
  1388. State = NetState.Error
  1389. End SyncLock
  1390. InterruptAndDelete()
  1391. End Sub
  1392. Private Sub InterruptAndDelete()
  1393. On Error Resume Next
  1394. If File.Exists(LocalPath) Then File.Delete(LocalPath)
  1395. SyncLock NetManager.LockRemain
  1396. NetManager.FileRemain -= 1
  1397. Log($"[Download] {LocalName}:状态 {State},剩余文件 {NetManager.FileRemain}")
  1398. End SyncLock
  1399. End Sub
  1400. '状态改变接口
  1401. ''' <summary>
  1402. ''' 将该文件设置为已下载完成。
  1403. ''' </summary>
  1404. Public Sub Finish(Optional PrintLog As Boolean = True)
  1405. SyncLock LockState
  1406. If State >= NetState.Finish Then Exit Sub
  1407. State = NetState.Finish
  1408. End SyncLock
  1409. SyncLock NetManager.LockRemain
  1410. NetManager.FileRemain -= 1
  1411. If PrintLog Then Log("[Download] " & LocalName & ":已完成,剩余文件 " & NetManager.FileRemain)
  1412. End SyncLock
  1413. For Each Task In Tasks
  1414. Task.OnFileFinish(Me)
  1415. Next
  1416. End Sub
  1417. End Class
  1418. ''' <summary>
  1419. ''' 下载一系列文件的加载器。
  1420. ''' </summary>
  1421. Public Class LoaderDownload
  1422. Inherits LoaderBase
  1423. #Region "属性"
  1424. ''' <summary>
  1425. ''' 需要下载的文件。
  1426. ''' </summary>
  1427. Public Files As List(Of NetFile)
  1428. Private ReadOnly FilesLock As New Object
  1429. ''' <summary>
  1430. ''' 剩余未完成的文件数。(用于减轻 FilesLock 的占用)
  1431. ''' </summary>
  1432. Private FileRemain As Integer
  1433. Private ReadOnly FileRemainLock As New Object
  1434. ''' <summary>
  1435. ''' 用于显示的百分比进度。
  1436. ''' </summary>
  1437. Public Overrides Property Progress As Double
  1438. Get
  1439. If State >= LoadState.Finished Then Return 1
  1440. SyncLock FilesLock
  1441. If Not Files.Any() Then Return 0 '必须返回 0,否则在获取列表的时候会错觉已经下载完了
  1442. End SyncLock
  1443. Return _Progress
  1444. End Get
  1445. Set(value As Double)
  1446. Throw New Exception("文件下载不允许指定进度")
  1447. End Set
  1448. End Property
  1449. Private _Progress As Double = 0
  1450. ''' <summary>
  1451. ''' 任务中的文件的连续失败计数。
  1452. ''' </summary>
  1453. Public Property FailCount As Integer
  1454. Get
  1455. Return _FailCount
  1456. End Get
  1457. Set(value As Integer)
  1458. _FailCount = value
  1459. If State = LoadState.Loading AndAlso value >= Math.Min(10000, Math.Max(FileRemain * 5.5, NetTaskThreadLimit * 5.5 + 3)) Then
  1460. Log("[Download] 由于同加载器中失败次数过多引发强制失败:连续失败了 " & value & " 次", LogLevel.Debug)
  1461. On Error Resume Next
  1462. Dim ExList As New List(Of Exception)
  1463. SyncLock FilesLock
  1464. For Each File In Files
  1465. For Each Source In File.Sources
  1466. If Source.Ex IsNot Nothing Then
  1467. ExList.Add(Source.Ex)
  1468. If ExList.Count > 10 Then GoTo FinishExCatch
  1469. End If
  1470. Next
  1471. Next
  1472. End SyncLock
  1473. FinishExCatch:
  1474. OnFail(ExList)
  1475. End If
  1476. End Set
  1477. End Property
  1478. Private _FailCount As Integer = 0
  1479. #End Region
  1480. ''' <summary>
  1481. ''' 刷新公开属性。由 NetManager 每 0.1 秒调用一次。
  1482. ''' </summary>
  1483. Public Sub RefreshStat()
  1484. '计算进度
  1485. Dim NewProgress As Double = 0
  1486. Dim TotalProgress As Double = 0
  1487. SyncLock FilesLock
  1488. For Each File In Files
  1489. If File.IsCopy Then
  1490. NewProgress += File.Progress * 0.2
  1491. TotalProgress += 0.2
  1492. Else
  1493. NewProgress += File.Progress
  1494. TotalProgress += 1
  1495. End If
  1496. Next
  1497. End SyncLock
  1498. If TotalProgress > 0 Then NewProgress /= TotalProgress
  1499. '刷新进度
  1500. If NewProgress < 1 AndAlso NewProgress > 0 Then NewProgress = 2 * (NewProgress ^ 3) - 3 * (NewProgress ^ 2) + 2 * NewProgress '2x^3-3x^2+2x,模拟开头和结尾更慢的情况
  1501. _Progress = NewProgress
  1502. End Sub
  1503. Public Sub New(Name As String, FileTasks As List(Of NetFile))
  1504. Me.Name = Name
  1505. Files = FileTasks
  1506. End Sub
  1507. Public Overrides Sub Start(Optional Input As Object = Nothing, Optional IsForceRestart As Boolean = False)
  1508. SyncLock FilesLock
  1509. If Input IsNot Nothing Then Files = Input
  1510. '去重
  1511. Dim ResultArray As New List(Of NetFile)
  1512. For i = 0 To Files.Count - 1
  1513. For ii = i + 1 To Files.Count - 1
  1514. If Files(i).LocalPath = Files(ii).LocalPath Then GoTo NextElement
  1515. Next
  1516. ResultArray.Add(Files(i))
  1517. NextElement:
  1518. Next i
  1519. Files = ResultArray
  1520. '设置剩余文件数
  1521. SyncLock FileRemainLock
  1522. For Each File In Files
  1523. If File.State <> NetState.Finish Then FileRemain += 1
  1524. Next
  1525. End SyncLock
  1526. End SyncLock
  1527. State = LoadState.Loading
  1528. '开始执行
  1529. RunInNewThread(
  1530. Sub()
  1531. Try
  1532. '输入检测
  1533. If Not Files.Any() Then
  1534. OnFinish()
  1535. Exit Sub
  1536. End If
  1537. For Each File As NetFile In Files
  1538. If File Is Nothing Then Throw New ArgumentException("存在空文件请求!")
  1539. For Each Source As NetSource In File.Sources
  1540. If Not (Source.Url.StartsWithF("https://", True) OrElse Source.Url.StartsWithF("http://", True)) Then
  1541. Source.Ex = New ArgumentException("输入的下载链接不正确!")
  1542. Source.IsFailed = True
  1543. End If
  1544. Next
  1545. If File.IsSourceFailed() Then Throw New ArgumentException("输入的下载链接不正确!")
  1546. If Not File.LocalPath.ToLower.Contains(":\") Then Throw New ArgumentException("输入的本地文件地址不正确!")
  1547. If File.LocalPath.EndsWithF("\") Then Throw New ArgumentException("请输入含文件名的完整文件路径!")
  1548. '文件夹检测
  1549. Dim DirPath As String = New FileInfo(File.LocalPath).Directory.FullName
  1550. If Not Directory.Exists(DirPath) Then Directory.CreateDirectory(DirPath)
  1551. Next
  1552. '接入下载管理器
  1553. NetManager.Start(Me)
  1554. '将文件分配给多个线程以进行已存在查找
  1555. Dim Folders As New List(Of String) '可能会用于已存在查找的文件夹列表
  1556. Dim FoldersFinal As New List(Of String) '最终用于查找的列表
  1557. If Not Setup.Get("SystemDebugSkipCopy") Then '在设置中禁用复制
  1558. Folders.Add(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\") '总是添加官启文件夹,因为 HMCL 会把所有文件存在这里
  1559. For Each Folder In McFolderList
  1560. Folders.Add(Folder.Path)
  1561. Next
  1562. Folders = Folders.Distinct.ToList
  1563. For Each Folder In Folders
  1564. If Folder <> PathMcFolder AndAlso Directory.Exists(Folder) Then FoldersFinal.Add(Folder)
  1565. Next
  1566. End If
  1567. '最多 5 个线程,最少每个线程分配 10 个文件
  1568. SyncLock FilesLock
  1569. Dim FilesPerThread As Integer = Math.Max(5, Files.Count / 10 + 1)
  1570. Dim FilesInThread As New List(Of NetFile)
  1571. For Each File In Files
  1572. FilesInThread.Add(File)
  1573. If FilesInThread.Count = FilesPerThread Then
  1574. Dim FilesToRun As New List(Of NetFile)
  1575. FilesToRun.AddRange(FilesInThread)
  1576. RunInNewThread(Sub() StartCopy(FilesToRun, FoldersFinal), "NetTask FileCopy " & Uuid)
  1577. FilesInThread.Clear()
  1578. End If
  1579. Next
  1580. If FilesInThread.Any Then
  1581. Dim FilesToRun As New List(Of NetFile)
  1582. FilesToRun.AddRange(FilesInThread)
  1583. RunInNewThread(Sub() StartCopy(FilesToRun, FoldersFinal), "NetTask FileCopy " & Uuid)
  1584. FilesInThread.Clear()
  1585. End If
  1586. End SyncLock
  1587. Catch ex As Exception
  1588. OnFail(New List(Of Exception) From {ex})
  1589. End Try
  1590. End Sub, "NetTask " & Uuid & " Main")
  1591. End Sub
  1592. Private Sub StartCopy(Files As List(Of NetFile), FolderList As List(Of String))
  1593. Try
  1594. If ModeDebug Then Log("[Download] 检查线程分配文件数:" & Files.Count & ",线程名:" & Thread.CurrentThread.Name)
  1595. '试图从已存在的 Minecraft 文件夹中寻找目标文件
  1596. Dim ExistFiles As New List(Of KeyValuePair(Of NetFile, String)) '{NetFile, Target As String}
  1597. For Each File As NetFile In Files
  1598. Dim ExistFilePath As String = Nothing
  1599. '判断是否有已存在的文件
  1600. If File.Check IsNot Nothing AndAlso McFolderList IsNot Nothing AndAlso PathMcFolder IsNot Nothing AndAlso File.Check.CanUseExistsFile AndAlso File.LocalPath.StartsWithF(PathMcFolder) Then
  1601. Dim Relative = File.LocalPath.Replace(PathMcFolder, "")
  1602. For Each Folder In FolderList
  1603. Dim Target = Folder & Relative
  1604. If File.Check.Check(Target) Is Nothing Then
  1605. ExistFilePath = Target
  1606. Exit For
  1607. End If
  1608. Next
  1609. End If
  1610. '若存在,则改变状态
  1611. SyncLock LockState
  1612. If ExistFilePath IsNot Nothing Then
  1613. File.State = NetState.WaitForCopy
  1614. File.IsCopy = True
  1615. ExistFiles.Add(New KeyValuePair(Of NetFile, String)(File, ExistFilePath))
  1616. Else
  1617. File.State = NetState.WaitForDownload
  1618. File.IsCopy = False
  1619. End If
  1620. End SyncLock
  1621. Next
  1622. '复制已存在的文件
  1623. For Each FileToken In ExistFiles
  1624. Dim File As NetFile = FileToken.Key
  1625. SyncLock LockState
  1626. If File.State > NetState.WaitForCopy Then Exit Sub
  1627. End SyncLock
  1628. Dim LocalPath As String = FileToken.Value
  1629. Dim RetryCount As Integer = 0
  1630. Retry:
  1631. Try
  1632. Log("[Download] 复制已存在的文件(" & LocalPath & ")")
  1633. CopyFile(LocalPath, File.LocalPath)
  1634. File.Finish(False)
  1635. Catch ex As Exception
  1636. RetryCount += 1
  1637. Log(ex, String.Format("复制已存在的文件失败,重试第 {2} 次({0} -> {1})", LocalPath, File.LocalPath, RetryCount))
  1638. If RetryCount < 3 Then
  1639. Thread.Sleep(200)
  1640. GoTo Retry
  1641. End If
  1642. File.State = NetState.WaitForDownload
  1643. File.IsCopy = False
  1644. End Try
  1645. Next
  1646. Catch ex As Exception
  1647. Log(ex, "下载已存在文件查找失败", LogLevel.Feedback)
  1648. End Try
  1649. End Sub
  1650. Public Sub OnFileFinish(File As NetFile)
  1651. '要求全部文件完成
  1652. SyncLock FileRemainLock
  1653. FileRemain -= 1
  1654. If FileRemain > 0 Then Exit Sub
  1655. End SyncLock
  1656. OnFinish()
  1657. End Sub
  1658. Public Sub OnFinish()
  1659. RaisePreviewFinish()
  1660. SyncLock LockState
  1661. If State > LoadState.Loading Then Exit Sub
  1662. State = LoadState.Finished
  1663. End SyncLock
  1664. End Sub
  1665. Public Sub OnFileFail(File As NetFile)
  1666. '将下载源的错误加入主错误列表
  1667. For Each Source In File.Sources
  1668. If Not IsNothing(Source.Ex) Then File.Ex.Add(Source.Ex)
  1669. Next
  1670. OnFail(File.Ex)
  1671. End Sub
  1672. Public Sub OnFail(ExList As List(Of Exception))
  1673. SyncLock LockState
  1674. If State > LoadState.Loading Then Exit Sub
  1675. If ExList Is Nothing OrElse Not ExList.Any() Then ExList = New List(Of Exception) From {New Exception("未知错误!")}
  1676. '寻找第一个不是 404 的下载源
  1677. Dim UsefulExs = ExList.Where(Function(e) Not e.Message.Contains("(404)")).ToList
  1678. [Error] = If(UsefulExs.Any, UsefulExs(0), ExList(0))
  1679. '获取实际失败的文件
  1680. SyncLock FilesLock
  1681. For Each File In Files
  1682. If File.State = NetState.Error Then
  1683. [Error] = New Exception("文件下载失败:" & File.LocalPath & vbCrLf & Join(
  1684. File.Sources.Select(Function(s) If(s.Ex Is Nothing, s.Url, s.Ex.Message & "(" & s.Url & ")")), vbCrLf), [Error])
  1685. Exit For
  1686. End If
  1687. Next
  1688. End SyncLock
  1689. '在设置 Error 对象后再更改为失败,避免 WaitForExit 无法捕获错误
  1690. State = LoadState.Failed
  1691. End SyncLock
  1692. '中断所有文件
  1693. SyncLock FilesLock
  1694. For Each TaskFile In Files
  1695. If TaskFile.State < NetState.Merge Then TaskFile.State = NetState.Error
  1696. Next
  1697. End SyncLock
  1698. '在退出同步锁后再进行日志输出
  1699. Dim ErrOutput As New List(Of String)
  1700. For Each Ex As Exception In ExList
  1701. ErrOutput.Add(GetExceptionDetail(Ex))
  1702. Next
  1703. Log("[Download] " & Join(ErrOutput.Distinct.ToArray, vbCrLf))
  1704. End Sub
  1705. Public Overrides Sub Abort()
  1706. SyncLock LockState
  1707. If State >= LoadState.Finished Then Exit Sub
  1708. State = LoadState.Aborted
  1709. End SyncLock
  1710. Log("[Download] " & Name & " 已取消!")
  1711. '中断所有文件
  1712. SyncLock FilesLock
  1713. For Each TaskFile In Files
  1714. TaskFile.Abort(Me)
  1715. Next
  1716. End SyncLock
  1717. End Sub
  1718. End Class
  1719. Public NetManager As New NetManagerClass
  1720. ''' <summary>
  1721. ''' 下载文件管理。
  1722. ''' </summary>
  1723. Public Class NetManagerClass
  1724. #Region "属性"
  1725. ''' <summary>
  1726. ''' 需要下载的文件。为“本地地址 - 文件对象”键值对。
  1727. ''' </summary>
  1728. Public Files As New Dictionary(Of String, NetFile)
  1729. Public ReadOnly LockFiles As New Object
  1730. ''' <summary>
  1731. ''' 当前的所有下载任务。
  1732. ''' </summary>
  1733. Public Tasks As New List(Of LoaderDownload)
  1734. Private ReadOnly LockTasks As New Object
  1735. ''' <summary>
  1736. ''' 已下载完成的大小。
  1737. ''' </summary>
  1738. Public Property DownloadDone As Long
  1739. Get
  1740. Return _DownloadDone
  1741. End Get
  1742. Set(value As Long)
  1743. SyncLock LockDone
  1744. _DownloadDone = value
  1745. End SyncLock
  1746. End Set
  1747. End Property
  1748. Private _DownloadDone As Long = 0
  1749. Private ReadOnly LockDone As New Object
  1750. ''' <summary>
  1751. ''' 尚未完成下载的文件数。
  1752. ''' </summary>
  1753. Public FileRemain As Integer = 0
  1754. Public ReadOnly LockRemain As New Object
  1755. ''' <summary>
  1756. ''' 上次记速时的已下载大小。
  1757. ''' </summary>
  1758. Private SpeedLastDone As Long = 0
  1759. ''' <summary>
  1760. ''' 至多最近 30 次下载速度的记录,较新的在前面。
  1761. ''' </summary>
  1762. Private SpeedLast As New List(Of Long)
  1763. '这些属性由 RefreshStat 刷新
  1764. ''' <summary>
  1765. ''' 当前的全局下载速度,单位为 Byte / 秒。
  1766. ''' </summary>
  1767. Public Speed As Long = 0
  1768. Public ReadOnly Uuid As Integer = GetUuid()
  1769. #End Region
  1770. ''' <summary>
  1771. ''' 进度与下载速度由下载管理线程每隔约 0.1 秒刷新一次。
  1772. ''' </summary>
  1773. Private Sub RefreshStat()
  1774. Try
  1775. Dim DeltaTime As Long = GetTimeTick() - RefreshStatLast
  1776. RefreshStatLast += DeltaTime
  1777. #Region "刷新整体速度"
  1778. '计算瞬时速度
  1779. Dim ActualSpeed As Double = Math.Max(0, (DownloadDone - SpeedLastDone) / (DeltaTime / 1000))
  1780. SpeedLast.Insert(0, ActualSpeed)
  1781. If SpeedLast.Count >= 31 Then SpeedLast.RemoveAt(30)
  1782. SpeedLastDone = DownloadDone
  1783. '计算用于显示的速度
  1784. Dim SpeedSum As Long = 0, SpeedDiv As Long = 0, Weight = SpeedLast.Count
  1785. For Each SpeedRecord In SpeedLast
  1786. SpeedSum += SpeedRecord * Weight
  1787. SpeedDiv += Weight
  1788. Weight -= 1
  1789. Next
  1790. Speed = If(SpeedDiv > 0, SpeedSum / SpeedDiv, 0)
  1791. '计算新的速度下限
  1792. Dim Limit As Long = 0
  1793. If SpeedLast.Count >= 10 Then Limit = SpeedLast.Take(10).Average * 0.85 '取近 1 秒的平均速度的 85%
  1794. If Limit > NetTaskSpeedLimitLow Then
  1795. NetTaskSpeedLimitLow = Limit
  1796. Log("[Download] " & "速度下限已提升到 " & GetString(Limit))
  1797. End If
  1798. #End Region
  1799. #Region "刷新下载任务属性"
  1800. SyncLock LockTasks
  1801. For Each Task In Tasks
  1802. Task.RefreshStat()
  1803. Next
  1804. End SyncLock
  1805. #End Region
  1806. Catch ex As Exception
  1807. Log(ex, "刷新下载公开属性失败")
  1808. End Try
  1809. End Sub
  1810. Private RefreshStatLast As Long
  1811. ''' <summary>
  1812. ''' 启动监控线程,用于新增下载线程。
  1813. ''' </summary>
  1814. Private Sub StartManager()
  1815. If IsManagerStarted Then Exit Sub
  1816. IsManagerStarted = True
  1817. Dim ThreadStarter =
  1818. Sub(Id As Integer) '0 或 1
  1819. Try
  1820. While True
  1821. Thread.Sleep(20)
  1822. '获取文件列表
  1823. Dim AllFiles As List(Of NetFile)
  1824. SyncLock LockFiles
  1825. If Id = 0 AndAlso FileRemain = 0 AndAlso Files.Any() Then Files.Clear() '若已完成,则清空
  1826. AllFiles = Files.Values.ToList()
  1827. End SyncLock
  1828. Dim WaitingFiles As New List(Of NetFile)
  1829. Dim OngoingFiles As New List(Of NetFile)
  1830. For Each File As NetFile In AllFiles
  1831. If File.Uuid Mod 2 = Id Then Continue For
  1832. If File.State = NetState.WaitForDownload Then
  1833. WaitingFiles.Add(File)
  1834. ElseIf File.State < NetState.Merge Then
  1835. OngoingFiles.Add(File)
  1836. End If
  1837. Next
  1838. '为等待中的文件开始线程
  1839. For Each File As NetFile In WaitingFiles
  1840. If NetTaskThreadCount >= NetTaskThreadLimit Then Continue While '最大线程数检查
  1841. Dim NewThread = File.TryBeginThread()
  1842. If NewThread IsNot Nothing AndAlso NewThread.Source.Url.Contains("bmclapi") Then Thread.Sleep(70) '减少 BMCLAPI 请求频率,避免 Too Many Requests
  1843. Next
  1844. '为进行中的文件追加线程
  1845. If Speed >= NetTaskSpeedLimitLow Then Continue While '下载速度足够,无需新增
  1846. For Each File As NetFile In OngoingFiles
  1847. If NetTaskThreadCount >= NetTaskThreadLimit Then Continue While '最大线程数检查
  1848. '线程种类计数
  1849. Dim PreparingCount = 0, DownloadingCount = 0
  1850. If File.Threads IsNot Nothing Then
  1851. For Each Thread As NetThread In File.Threads.ToList
  1852. If Thread.State < NetState.Download Then
  1853. PreparingCount += 1
  1854. ElseIf Thread.State = NetState.Download Then
  1855. DownloadingCount += 1
  1856. End If
  1857. Next
  1858. End If
  1859. '新增线程
  1860. If PreparingCount > DownloadingCount Then Continue For '准备中的线程已多于下载中的线程,不再新增
  1861. Dim NewThread = File.TryBeginThread()
  1862. If NewThread IsNot Nothing AndAlso NewThread.Source.Url.Contains("bmclapi") Then Thread.Sleep(70) '减少 BMCLAPI 请求频率,避免 Too Many Requests
  1863. Next
  1864. End While
  1865. Catch ex As Exception
  1866. Log(ex, $"下载管理启动线程 {Id} 出错", LogLevel.Assert)
  1867. End Try
  1868. End Sub
  1869. RunInNewThread(Sub() ThreadStarter(0), "NetManager ThreadStarter 0")
  1870. RunInNewThread(Sub() ThreadStarter(1), "NetManager ThreadStarter 1")
  1871. RunInNewThread(
  1872. Sub()
  1873. Try
  1874. Dim LastLoopTime As Long
  1875. NetTaskSpeedLimitLeftLast = GetTimeTick()
  1876. While True
  1877. Dim TimeNow = GetTimeTick()
  1878. LastLoopTime = TimeNow
  1879. '增加限速余量
  1880. If NetTaskSpeedLimitHigh > 0 Then NetTaskSpeedLimitLeft = NetTaskSpeedLimitHigh / 1000 * (TimeNow - NetTaskSpeedLimitLeftLast)
  1881. NetTaskSpeedLimitLeftLast = TimeNow
  1882. '刷新公开属性
  1883. RefreshStat()
  1884. '等待直至 80 ms
  1885. Do While GetTimeTick() - LastLoopTime < 80
  1886. Thread.Sleep(10)
  1887. Loop
  1888. End While
  1889. Catch ex As Exception
  1890. Log(ex, "下载管理刷新线程出错", LogLevel.Assert)
  1891. End Try
  1892. End Sub, "NetManager StatRefresher")
  1893. End Sub
  1894. Private IsManagerStarted As Boolean = False
  1895. 'Public FileRemainList As New List(Of String)
  1896. Private IsDownloadCacheCleared As Boolean = False
  1897. ''' <summary>
  1898. ''' 开始一个下载任务。
  1899. ''' </summary>
  1900. Public Sub Start(Task As LoaderDownload)
  1901. StartManager()
  1902. '清理缓存
  1903. If Not IsDownloadCacheCleared Then
  1904. Try
  1905. DeleteDirectory(PathTemp & "Download")
  1906. Catch ex As Exception
  1907. Log(ex, "清理下载缓存失败")
  1908. End Try
  1909. IsDownloadCacheCleared = True
  1910. End If
  1911. Directory.CreateDirectory(PathTemp & "Download")
  1912. '文件处理
  1913. SyncLock LockFiles
  1914. '添加每个文件
  1915. For i = 0 To Task.Files.Count - 1
  1916. Dim File = Task.Files(i)
  1917. If Files.ContainsKey(File.LocalPath) Then
  1918. '已有该文件
  1919. If Files(File.LocalPath).State >= NetState.Finish Then
  1920. '该文件已经下载过一次,且下载完成
  1921. '将已下载的文件替换成当前文件,重新下载
  1922. File.Tasks.Add(Task)
  1923. Files(File.LocalPath) = File
  1924. SyncLock LockRemain
  1925. FileRemain += 1
  1926. If ModeDebug Then Log("[Download] " & File.LocalName & ":已替换列表,剩余文件 " & FileRemain)
  1927. 'FileRemainList.Add(File.LocalPath)
  1928. End SyncLock
  1929. Else
  1930. '该文件正在下载中
  1931. '将当前文件替换成下载中的文件,即两个任务指向同一个文件
  1932. File = Files(File.LocalPath)
  1933. File.Tasks.Add(Task)
  1934. End If
  1935. Else
  1936. '没有该文件
  1937. File.Tasks.Add(Task)
  1938. Files.Add(File.LocalPath, File)
  1939. SyncLock LockRemain
  1940. FileRemain += 1
  1941. If ModeDebug Then Log("[Download] " & File.LocalName & ":已加入列表,剩余文件 " & FileRemain)
  1942. 'FileRemainList.Add(File.LocalPath)
  1943. End SyncLock
  1944. End If
  1945. Task.Files(i) = File '回设
  1946. Next
  1947. End SyncLock
  1948. SyncLock LockTasks
  1949. Tasks.Add(Task)
  1950. End SyncLock
  1951. End Sub
  1952. End Class
  1953. ''' <summary>
  1954. ''' 是否有正在进行中、需要在下载管理页面显示的下载任务?
  1955. ''' </summary>
  1956. Public Function HasDownloadingTask(Optional IgnoreCustomDownload As Boolean = False) As Boolean
  1957. For Each Task In LoaderTaskbar.ToList()
  1958. If (Task.Show AndAlso Task.State = LoadState.Loading) AndAlso
  1959. (Not IgnoreCustomDownload OrElse Not Task.Name.ToString.Contains("自定义下载")) Then
  1960. Return True
  1961. End If
  1962. Next
  1963. Return False
  1964. End Function
  1965. End Module