ModNet.vb 98 KB

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