Browse Source

修正多线程下载的bug

懒得勤快 3 years ago
parent
commit
195ec0273c

+ 1 - 1
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -3,7 +3,7 @@
         <TargetFrameworks>netstandard2.0;netstandard2.1;net461;net5;net6;net7</TargetFrameworks>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>2.5.8.2</Version>
+        <Version>2.5.8.3</Version>
         <Authors>懒得勤快</Authors>
         <Description>Masuit.Tools基础公共库,包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。</Description>
         <Copyright>懒得勤快,长空X</Copyright>

+ 10 - 10
Masuit.Tools.Abstractions/Net/MultiThreadDownloader.cs

@@ -194,16 +194,16 @@ namespace Masuit.Tools.Net
                 return;
             }
 
-            PartialDownloaderList.Sort((x, y) => y.RemainingBytes - x.RemainingBytes);
-            int rem = PartialDownloaderList[0].RemainingBytes;
+            PartialDownloaderList.Sort((x, y) => (int)(y.RemainingBytes - x.RemainingBytes));
+            var rem = PartialDownloaderList[0].RemainingBytes;
             if (rem < 50 * 1024)
             {
                 WaitOrResumeAll(PartialDownloaderList, false);
                 return;
             }
 
-            int from = PartialDownloaderList[0].CurrentPosition + rem / 2;
-            int to = PartialDownloaderList[0].To;
+            var from = PartialDownloaderList[0].CurrentPosition + rem / 2;
+            var to = PartialDownloaderList[0].To;
             if (from > to)
             {
                 WaitOrResumeAll(PartialDownloaderList, false);
@@ -314,10 +314,10 @@ namespace Masuit.Tools.Net
 
         private PartialDownloader CreateNew(int order, int parts, long contentLength)
         {
-            int division = (int)contentLength / parts;
-            int remaining = (int)contentLength % parts;
-            int start = division * order;
-            int end = start + division - 1;
+            var division = contentLength / parts;
+            var remaining = contentLength % parts;
+            var start = division * order;
+            var end = start + division - 1;
             end += order == parts - 1 ? remaining : 0;
             return new PartialDownloader(_url, TempFileDirectory, SnowFlake.NewId, start, end, true);
         }
@@ -413,8 +413,8 @@ namespace Masuit.Tools.Net
             {
                 if (PartialDownloaderList[i].Stopped)
                 {
-                    int from = PartialDownloaderList[i].CurrentPosition + 1;
-                    int to = PartialDownloaderList[i].To;
+                    var from = PartialDownloaderList[i].CurrentPosition + 1;
+                    var to = PartialDownloaderList[i].To;
                     if (from > to)
                     {
                         continue;

+ 11 - 10
Masuit.Tools.Abstractions/Net/PartialDownloader.cs

@@ -29,10 +29,10 @@ namespace Masuit.Tools.Net
         public event EventHandler DownloadPartStopped;
 
         private readonly AsyncOperation _aop = AsyncOperationManager.CreateOperation(null);
-        readonly int[] _lastSpeeds;
-        int _counter;
-        private int _to;
-        private int _totalBytesRead;
+        private readonly int[] _lastSpeeds;
+        private long _counter;
+        private long _to;
+        private long _totalBytesRead;
         private bool _wait;
 
         /// <summary>
@@ -83,7 +83,7 @@ namespace Masuit.Tools.Net
         /// <summary>
         /// to
         /// </summary>
-        public int To
+        public long To
         {
             get => _to;
             set
@@ -96,17 +96,17 @@ namespace Masuit.Tools.Net
         /// <summary>
         /// from
         /// </summary>
-        public int From { get; }
+        public long From { get; }
 
         /// <summary>
         /// 当前位置
         /// </summary>
-        public int CurrentPosition => From + _totalBytesRead - 1;
+        public long CurrentPosition => From + _totalBytesRead - 1;
 
         /// <summary>
         /// 剩余字节数
         /// </summary>
-        public int RemainingBytes => (int)(ContentLength - _totalBytesRead);
+        public long RemainingBytes => ContentLength - _totalBytesRead;
 
         /// <summary>
         /// 完整路径
@@ -139,7 +139,7 @@ namespace Masuit.Tools.Net
         /// <param name="from"></param>
         /// <param name="to"></param>
         /// <param name="rangeAllowed"></param>
-        public PartialDownloader(string url, string dir, string fileGuid, int from, int to, bool rangeAllowed)
+        public PartialDownloader(string url, string dir, string fileGuid, long from, long to, bool rangeAllowed)
         {
             From = from;
             _to = to;
@@ -150,7 +150,7 @@ namespace Masuit.Tools.Net
             _lastSpeeds = new int[10];
         }
 
-        void DownloadProcedure(Action<HttpWebRequest> config)
+        private void DownloadProcedure(Action<HttpWebRequest> config)
         {
             using (var file = new FileStream(FullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Delete))
             {
@@ -163,6 +163,7 @@ namespace Masuit.Tools.Net
                     req.ServicePoint.ConnectionLimit += 1;
                     req.ServicePoint.Expect100Continue = true;
                     req.ProtocolVersion = HttpVersion.Version11;
+                    req.Proxy = WebRequest.GetSystemWebProxy();
                     config(req);
                     if (RangeAllowed)
                     {

+ 1 - 1
Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj

@@ -17,7 +17,7 @@
         <Product>Masuit.Tools.AspNetCore</Product>
         <PackageId>Masuit.Tools.AspNetCore</PackageId>
         <LangVersion>latest</LangVersion>
-        <Version>1.1.8.2</Version>
+        <Version>1.1.8.3</Version>
         <RepositoryType></RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
         <FileVersion>$(Version)</FileVersion>

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -6,7 +6,7 @@
 官网教程:https://ldqk.org/55
 github:https://github.com/ldqk/Masuit.Tools
         </Description>
-        <Version>2.5.8.2</Version>
+        <Version>2.5.8.3</Version>
         <Copyright>Copyright © 懒得勤快</Copyright>
         <PackageProjectUrl>https://github.com/ldqk/Masuit.Tools</PackageProjectUrl>
         <PackageTags>Masuit.Tools,工具库,Utility,Crypt,Extensions</PackageTags>

+ 2 - 2
Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj

@@ -97,10 +97,10 @@
       <Version>4.18.3</Version>
     </PackageReference>
     <PackageReference Include="MSTest.TestAdapter">
-      <Version>2.2.10</Version>
+      <Version>3.0.0</Version>
     </PackageReference>
     <PackageReference Include="MSTest.TestFramework">
-      <Version>2.2.10</Version>
+      <Version>3.0.0</Version>
     </PackageReference>
     <PackageReference Include="NUnit">
       <Version>3.13.3</Version>