Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Commit 3ad5c52

Browse files
committed
bump 1.0.5;
Show thread count on history windows; Show left time on history windows; Fix bug
1 parent 23bd8c0 commit 3ad5c52

18 files changed

+233
-100
lines changed

BaiduPCS_NET.sln

+15-46
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,30 @@
11

2-
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.31101.0
2+
Microsoft Visual Studio Solution File, Format Version 14.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BaiduPCS_NET", "BaiduPCS_NET\BaiduPCS_NET.csproj", "{605B8AE1-834A-459B-9461-7BA72D0212BA}"
77
EndProject
8-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BaiduPCS_DLL", "..\BaiduPCS\pcs\BaiduPCS_DLL.vcxproj", "{6EDC18B3-E237-4C73-9A54-58B0BEB98998}"
9-
EndProject
10-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{FC85E053-C4CA-4E77-B958-740118D6A0C7}"
11-
EndProject
128
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample_0_FileExplorer", "Sample\Sample_0_FileExplorer\Sample_0_FileExplorer.csproj", "{EAC9410A-0903-431D-87ED-223BFB8653C4}"
139
EndProject
1410
Global
1511
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16-
Debug|Any CPU = Debug|Any CPU
17-
Debug|Mixed Platforms = Debug|Mixed Platforms
18-
Debug|Win32 = Debug|Win32
19-
Release|Any CPU = Release|Any CPU
20-
Release|Mixed Platforms = Release|Mixed Platforms
21-
Release|Win32 = Release|Win32
12+
Debug|x86 = Debug|x86
13+
Release|x86 = Release|x86
2214
EndGlobalSection
2315
GlobalSection(ProjectConfigurationPlatforms) = postSolution
24-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
26-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
27-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
28-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|Win32.ActiveCfg = Debug|Any CPU
29-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
30-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Release|Any CPU.Build.0 = Release|Any CPU
31-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
32-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Release|Mixed Platforms.Build.0 = Release|Any CPU
33-
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Release|Win32.ActiveCfg = Release|Any CPU
34-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Debug|Any CPU.ActiveCfg = Debug|Win32
35-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
36-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Debug|Mixed Platforms.Build.0 = Debug|Win32
37-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Debug|Win32.ActiveCfg = Debug|Win32
38-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Debug|Win32.Build.0 = Debug|Win32
39-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Release|Any CPU.ActiveCfg = Release|Win32
40-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Release|Mixed Platforms.ActiveCfg = Release|Win32
41-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Release|Mixed Platforms.Build.0 = Release|Win32
42-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Release|Win32.ActiveCfg = Release|Win32
43-
{6EDC18B3-E237-4C73-9A54-58B0BEB98998}.Release|Win32.Build.0 = Release|Win32
44-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
46-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
47-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
48-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|Win32.ActiveCfg = Debug|Any CPU
49-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
50-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Release|Any CPU.Build.0 = Release|Any CPU
51-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
52-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Release|Mixed Platforms.Build.0 = Release|Any CPU
53-
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Release|Win32.ActiveCfg = Release|Any CPU
16+
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|x86.ActiveCfg = Debug|x86
17+
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|x86.Build.0 = Debug|x86
18+
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Debug|x86.Deploy.0 = Debug|x86
19+
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Release|x86.ActiveCfg = Release|x86
20+
{605B8AE1-834A-459B-9461-7BA72D0212BA}.Release|x86.Build.0 = Release|x86
21+
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|x86.ActiveCfg = Debug|x86
22+
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|x86.Build.0 = Debug|x86
23+
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Debug|x86.Deploy.0 = Debug|x86
24+
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Release|x86.ActiveCfg = Release|x86
25+
{EAC9410A-0903-431D-87ED-223BFB8653C4}.Release|x86.Build.0 = Release|x86
5426
EndGlobalSection
5527
GlobalSection(SolutionProperties) = preSolution
5628
HideSolutionNode = FALSE
5729
EndGlobalSection
58-
GlobalSection(NestedProjects) = preSolution
59-
{EAC9410A-0903-431D-87ED-223BFB8653C4} = {FC85E053-C4CA-4E77-B958-740118D6A0C7}
60-
EndGlobalSection
6130
EndGlobal

BaiduPCS_NET/BaiduPCS_NET.csproj

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
77
<ProjectGuid>{605B8AE1-834A-459B-9461-7BA72D0212BA}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
@@ -13,24 +13,23 @@
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
1515
</PropertyGroup>
16-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
1717
<DebugSymbols>true</DebugSymbols>
18-
<DebugType>full</DebugType>
19-
<Optimize>false</Optimize>
2018
<OutputPath>bin\Debug\</OutputPath>
2119
<DefineConstants>DEBUG;TRACE</DefineConstants>
22-
<ErrorReport>prompt</ErrorReport>
23-
<WarningLevel>4</WarningLevel>
20+
<DebugType>full</DebugType>
2421
<PlatformTarget>x86</PlatformTarget>
22+
<ErrorReport>prompt</ErrorReport>
23+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
2524
</PropertyGroup>
26-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27-
<DebugType>pdbonly</DebugType>
28-
<Optimize>true</Optimize>
25+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
2926
<OutputPath>bin\Release\</OutputPath>
3027
<DefineConstants>TRACE</DefineConstants>
31-
<ErrorReport>prompt</ErrorReport>
32-
<WarningLevel>4</WarningLevel>
28+
<Optimize>true</Optimize>
29+
<DebugType>pdbonly</DebugType>
3330
<PlatformTarget>x86</PlatformTarget>
31+
<ErrorReport>prompt</ErrorReport>
32+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
3433
</PropertyGroup>
3534
<ItemGroup>
3635
<Reference Include="System" />
@@ -61,7 +60,8 @@
6160
<ItemGroup />
6261
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6362
<PropertyGroup>
64-
<PostBuildEvent>echo F | xcopy "$(SolutionDir)$(Configuration)\BaiduPCS.dll" "$(TargetDir)" /E /R /Y</PostBuildEvent>
63+
<PostBuildEvent>
64+
</PostBuildEvent>
6565
</PropertyGroup>
6666
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6767
Other similar extension points exist, see Microsoft.Common.targets.

BaiduPCS_NET/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.3.0")]
36-
[assembly: AssemblyFileVersion("1.0.3.0")]
35+
[assembly: AssemblyVersion("1.0.5.0")]
36+
[assembly: AssemblyFileVersion("1.0.5.0")]

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
1. git clone https://github.com/GangZhuo/BaiduPCS.git
2222
2. git clone https://github.com/GangZhuo/BaiduPCS_NET.git
2323
3. 按照 BaiduPCS 中 [编译-windows] 的说明,使 BaiduPCS 能够编译通过
24-
4. 打开 BaiduPCS_NET 目录下的 BaiduPCS_NET.sln。
25-
然后,先编译 BaiduPCS_DLL 项目, 然后编译其他项目。
24+
4. 分别复制 BaiduPCS 中编译的 BaiduPCS.dll 文件到 Sample_0_FileExplorer
25+
项目的 bin\Debug 和 bin\Release 目录下。
26+
4. 打开 BaiduPCS_NET 目录下的 BaiduPCS_NET.sln,执行编译。
2627

2728
## 使用
2829
* 1. 下载:软件中鼠标选中需要下载的文件或目录(可多选),然后右击,在弹出的菜单中选择 "Download",然后选择本地目录。

Sample/Sample_0_FileExplorer/Common/Utils.cs

+27
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ public static string HumanReadableSize(long size)
9090
}
9191
}
9292

93+
public static string LeftTime(long leftSize, long speed)
94+
{
95+
// 计算剩余时间
96+
string s;
97+
int second;
98+
if (speed == 0)
99+
return "";
100+
second = (int)(leftSize / speed);
101+
TimeSpan ts = new TimeSpan(0, 0, second);
102+
s = "";
103+
if (ts.TotalDays > 1)
104+
s += ((int)ts.TotalDays).ToString() + "day(s) ";
105+
if (ts.TotalHours > 1)
106+
s += ((int)ts.TotalHours).ToString().PadLeft(2, '0') + ":";
107+
else
108+
s += "00:";
109+
if (ts.TotalMinutes > 1)
110+
s += ((int)ts.TotalMinutes).ToString().PadLeft(2, '0') + ":";
111+
else
112+
s += "00:";
113+
if (ts.TotalSeconds > 1)
114+
s += ((int)ts.TotalSeconds).ToString().PadLeft(2, '0');
115+
else
116+
s += "00";
117+
return s;
118+
}
119+
93120
/// <summary>
94121
/// 获取 CPU 属性参数
95122
/// </summary>

Sample/Sample_0_FileExplorer/DU/BigFileHelper.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ public class BigFileHelper : IDisposable, IBigFileHelper
88
{
99
public string FileName { get; private set; }
1010
private MemoryMappedFile mmf;
11+
private long fileSize;
1112

12-
public BigFileHelper(string filename)
13+
public BigFileHelper(string filename, long filesize)
1314
{
1415
FileName = filename;
15-
mmf = MemoryMappedFile.CreateFromFile(FileName, FileMode.Open, Utils.md5(FileName), 0, MemoryMappedFileAccess.ReadWrite); //映射文件到内存
16+
this.fileSize = filesize;
17+
mmf = MemoryMappedFile.CreateFromFile(FileName, FileMode.Open, Utils.md5(FileName), filesize, MemoryMappedFileAccess.ReadWrite); //映射文件到内存
1618
}
1719

1820
~BigFileHelper()

Sample/Sample_0_FileExplorer/DU/Downloader.cs

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Downloader : ICancellable, IProgressable
2020
public event EventHandler<CompletedEventArgs> Completed;
2121
public event EventHandler<ProgressEventArgs> Progress;
2222
public event EventHandler<StateFileNameDecideEventArgs> StateFileNameDecide;
23+
public event EventHandler<ThreadCountChangedEventArgs> ThreadChanged;
2324

2425
public Downloader(BaiduPCS pcs, PcsFileInfo from, string to)
2526
{
@@ -45,6 +46,7 @@ public virtual void Download()
4546
CreateDirectory(to);
4647
stream = new FileStream(to, FileMode.Create, FileAccess.Write);
4748
pcs.WriteUserData = stream;
49+
fireThreadChanged(new ThreadCountChangedEventArgs(1, 1));
4850
PcsRes rc = pcs.download(from.path, 0, 0, 0);
4951
if (rc == PcsRes.PCS_OK)
5052
{
@@ -74,6 +76,7 @@ public virtual void Download()
7476
stream.Close();
7577
Downloading = false;
7678
fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error));
79+
fireThreadChanged(new ThreadCountChangedEventArgs(0, 1));
7780
}
7881

7982
public virtual void Cancel()
@@ -125,5 +128,11 @@ protected virtual void fireStateFileNameDecide(StateFileNameDecideEventArgs args
125128
if (StateFileNameDecide != null)
126129
StateFileNameDecide(this, args);
127130
}
131+
132+
protected virtual void fireThreadChanged(ThreadCountChangedEventArgs args)
133+
{
134+
if (ThreadChanged != null)
135+
ThreadChanged(this, args);
136+
}
128137
}
129138
}

Sample/Sample_0_FileExplorer/DU/IProgressable.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public interface IProgressable
88
event EventHandler<StateFileNameDecideEventArgs> StateFileNameDecide;
99
event EventHandler<ProgressEventArgs> Progress;
1010
event EventHandler<CompletedEventArgs> Completed;
11+
event EventHandler<ThreadCountChangedEventArgs> ThreadChanged;
1112
}
1213
}

Sample/Sample_0_FileExplorer/DU/MultiThreadDownloader.cs

+48-17
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ public override void Download()
6868
fireStateFileNameDecide(args);
6969
SliceFileName = args.SliceFileName;
7070
CreateOrRestoreSliceList(); // 创建或还原分片列表
71+
CreateDirectory(to); //创建目录
7172
CreateLocalFile(); // 如果需要则创建本地文件
72-
bigfile = new BigFileHelper(to); //映射文件到内存
73+
bigfile = new BigFileHelper(to, from.size); //映射文件到内存
7374
foreach (Slice slice in SliceList)
7475
{
7576
if (slice.status != SliceStatus.Successed)
@@ -127,13 +128,8 @@ private void CreateLocalFile()
127128
{
128129
if (!File.Exists(to))
129130
{
130-
CreateDirectory(to);
131131
//预先创建一个大文件
132-
using (FileStream fs = File.Create(to))
133-
{
134-
long offset = fs.Seek(from.size - 1, SeekOrigin.Begin);
135-
fs.WriteByte((byte)0);
136-
}
132+
using (FileStream fs = File.Create(to)) { }
137133
}
138134
}
139135

@@ -152,9 +148,20 @@ private void DownloadSliceList()
152148

153149
private void Wait()
154150
{
151+
int prev, running;
152+
153+
prev = (int)Interlocked.Read(ref runningThreadCount);
154+
fireThreadChanged(new ThreadCountChangedEventArgs(prev, ThreadCount));
155+
155156
while (true)
156157
{
157-
if (Interlocked.Read(ref runningThreadCount) > 0)
158+
running = (int)Interlocked.Read(ref runningThreadCount);
159+
if (running != prev)
160+
{
161+
prev = running;
162+
fireThreadChanged(new ThreadCountChangedEventArgs(running, ThreadCount));
163+
}
164+
if (running > 0)
158165
Thread.Sleep(100);
159166
else
160167
break;
@@ -191,6 +198,7 @@ private void downloadTask(object objTID)
191198
BaiduPCS pcs;
192199
Slice slice;
193200
PcsRes rc;
201+
string errmsg;
194202
pcs = this.pcs.clone();
195203
pcs.Write += onWrite;
196204
while (tid == Interlocked.Read(ref taskId))
@@ -200,15 +208,31 @@ private void downloadTask(object objTID)
200208
break;
201209
slice.tid = tid;
202210
pcs.WriteUserData = slice;
203-
rc = pcs.download(from.path, 0, slice.start, slice.totalSize);
204-
if (rc == PcsRes.PCS_OK || slice.status == SliceStatus.Successed)
205-
slice.status = SliceStatus.Successed;
206-
else if (slice.status == SliceStatus.Cancelled)
207-
Cancel();
208-
else
211+
do
209212
{
210-
slice.status = SliceStatus.Failed;
211-
lock (locker) Error = new Exception(pcs.getError());
213+
errmsg = null;
214+
if (slice.status == SliceStatus.Failed)
215+
slice.status = SliceStatus.Retrying;
216+
rc = pcs.download(from.path, 0,
217+
slice.start + slice.doneSize,
218+
slice.totalSize - slice.doneSize);
219+
if (rc == PcsRes.PCS_OK || slice.status == SliceStatus.Successed)
220+
slice.status = SliceStatus.Successed;
221+
else if (slice.status == SliceStatus.Cancelled)
222+
Cancel();
223+
else
224+
{
225+
slice.status = SliceStatus.Failed;
226+
errmsg = pcs.getError();
227+
}
228+
} while (slice.status == SliceStatus.Failed &&
229+
AppSettings.RetryWhenDownloadFailed &&
230+
errmsg != null &&
231+
errmsg.Contains("Timeout"));
232+
233+
if (slice.status != SliceStatus.Successed && slice.status != SliceStatus.Cancelled)
234+
{
235+
lock (locker) Error = new Exception(errmsg);
212236
StopAllDownloadThreads();
213237
}
214238
}
@@ -263,7 +287,14 @@ private uint onWrite(BaiduPCS sender, byte[] data, uint contentlength, object us
263287
size = slice.totalSize - slice.doneSize;
264288
if (size > 0)
265289
{
266-
bigfile.Update(slice.start + slice.doneSize, data, 0, (int)size);
290+
try
291+
{
292+
bigfile.Update(slice.start + slice.doneSize, data, 0, (int)size);
293+
}
294+
catch(Exception ex)
295+
{
296+
throw;
297+
}
267298
}
268299
slice.doneSize += size;
269300
lock (locker) DoneSize += size;

Sample/Sample_0_FileExplorer/DU/MultiThreadUploader.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,20 @@ private void UploadSliceList()
169169

170170
private void Wait()
171171
{
172+
int prev, running;
173+
174+
prev = (int)Interlocked.Read(ref runningThreadCount);
175+
fireThreadChanged(new ThreadCountChangedEventArgs(prev, ThreadCount));
176+
172177
while (true)
173178
{
174-
if (Interlocked.Read(ref runningThreadCount) > 0)
179+
running = (int)Interlocked.Read(ref runningThreadCount);
180+
if (running != prev)
181+
{
182+
prev = running;
183+
fireThreadChanged(new ThreadCountChangedEventArgs(running, ThreadCount));
184+
}
185+
if (running > 0)
175186
Thread.Sleep(100);
176187
else
177188
break;

Sample/Sample_0_FileExplorer/DU/SliceStatus.cs

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public enum SliceStatus
1414
/// </summary>
1515
Running,
1616

17+
/// <summary>
18+
/// 重试中
19+
/// </summary>
20+
Retrying,
21+
1722
/// <summary>
1823
/// 失败
1924
/// </summary>

0 commit comments

Comments
 (0)