-
Notifications
You must be signed in to change notification settings - Fork 1
/
DODownload.cs
252 lines (216 loc) · 8.45 KB
/
DODownload.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Runtime.InteropServices;
namespace DODownloader
{
/// <summary>
/// Utility class provides to manage a DO download. A download manages only one file.
/// </summary>
internal class DODownload
{
// COM object representing this download in DO client
public IDODownload ClientDownload { get; private set; }
// Unique ID for this dowload in DO client
public Guid Id { get; private set; }
// Status callback receiver
public DODownloadCallback Handler { get; private set; }
// The file that is being downloaded
public DOFile File { get; private set; }
public DODownload(DOFile file, IDODownload downloadObj)
{
ClientDownload = downloadObj;
ClientDownload.GetProperty(DODownloadProperty.Id, out object valId);
Id = new Guid((string)valId);
File = file;
SetHandler(new DODownloadCallback((string)valId));
}
public void SetCostFlags(DODownloadCostPolicy policy)
{
ClientDownload.SetProperty(DODownloadProperty.CostPolicy, (uint)policy);
}
public void SetNoCallbacks()
{
ClientDownload.SetProperty(DODownloadProperty.CallbackInterface, null);
ResetHandlerState();
}
public void SetUri(string newUri)
{
ClientDownload.SetProperty(DODownloadProperty.Uri, newUri);
}
/// <summary>
/// Set ForegroundPriority to true. Downloads default to background.
/// </summary>
public void SetForeground()
{
ClientDownload.SetProperty(DODownloadProperty.ForegroundPriority, true);
}
/// <summary>
/// Set ForegroundPriority to false. Downloads default to background.
/// </summary>
public void SetBackground()
{
ClientDownload.SetProperty(DODownloadProperty.ForegroundPriority, false);
}
public bool IsForeground()
{
ClientDownload.GetProperty(DODownloadProperty.ForegroundPriority, out object value);
return (bool)value;
}
public bool IsBackground()
{
return !IsForeground();
}
public void SetNoProgressTimeout(uint timeoutSecs)
{
ClientDownload.SetProperty(DODownloadProperty.NoProgressTimeoutSeconds, timeoutSecs);
}
public uint GetNoProgressTimeout()
{
ClientDownload.GetProperty(DODownloadProperty.NoProgressTimeoutSeconds, out var val);
return (uint)val;
}
public ulong GetTotalSizeBytes()
{
ClientDownload.GetProperty(DODownloadProperty.TotalSizeBytes, out var val);
return Convert.ToUInt64(val);
}
public void SetHandler(DODownloadCallback handler)
{
Handler = handler;
// Without UnknownWrapper dosvc receives VT_DISPATCH and returns E_INVALIDARG
ClientDownload.SetProperty(DODownloadProperty.CallbackInterface, (handler != null) ? new UnknownWrapper(handler) : null);
}
public DODownloadState GetState()
{
return GetStatus().State;
}
public DO_DOWNLOAD_STATUS GetStatus()
{
ClientDownload.GetStatus(out DO_DOWNLOAD_STATUS status);
return status;
}
public void Start()
{
Start(new DODownloadRanges());
}
public void Start(DODownloadRanges rangesInfo)
{
if (rangesInfo == null)
{
ClientDownload.Start(IntPtr.Zero);
}
else
{
// Marshal the ranges in place by hand-assembling a DO_DOWNLOAD_RANGES_INFO structure
// in CoTaskMemAlloc'd memory. We need to take into account struct member alignment here.
// The first member RangeCount is a UInt32 which will occupy 4bytes on x86 but 8bytes on x64 (4bytes padding).
// After RangeCount is an array of DO_DOWNLOAD_RANGE structs which is nothing but 2 UInt64 numbers
// so they are naturally aligned on both x86 and x64 machines as long as RangeCount is constructed properly.
int elemSize = Marshal.SizeOf<DO_DOWNLOAD_RANGE>();
int ptrSize = Marshal.SizeOf<IntPtr>(); // platform specific size for the first member (RangeCount)
IntPtr coRangesInfo = Marshal.AllocCoTaskMem(ptrSize + (elemSize * rangesInfo.Count));
IntPtr coRanges = coRangesInfo + ptrSize;
try
{
Marshal.WriteInt32(coRangesInfo, rangesInfo.Count);
for (int i = 0; i < rangesInfo.Count; i++)
{
Marshal.StructureToPtr(rangesInfo.Collection[i], coRanges + (elemSize * i), false);
}
Console.WriteLine($"Download {Id}: starting with {rangesInfo.Count} ranges");
ClientDownload.Start(coRangesInfo);
}
finally
{
Marshal.FreeCoTaskMem(coRangesInfo);
}
}
}
public void Resume()
{
Console.WriteLine($"Download {Id}: resuming");
ResetHandlerState();
ClientDownload.Start(IntPtr.Zero);
}
public void Pause()
{
Console.WriteLine($"Download {Id}: pausing");
ClientDownload.Pause();
}
public void Abort()
{
Console.WriteLine($"Download {Id}: aborting");
ClientDownload.Abort();
}
// 'Finalize' conflicts with destructor invocation, hence the '2' suffix
public void Finalize2()
{
Console.WriteLine($"Download {Id}: finalizing");
ClientDownload.Finalize2();
}
public void StartAndWaitUntilTransferred(DODownloadRanges rangeInfo, int completionTimeSecs)
{
Start(rangeInfo);
WaitUntilTransferred(completionTimeSecs);
}
public void StartAndWaitUntilTransferred(int completionTimeSecs)
{
Start();
WaitUntilTransferred(completionTimeSecs);
}
public void ResumeAndWaitUntilTransferred(int waitTimeSecs)
{
Resume();
WaitUntilTransferred(waitTimeSecs);
}
public void StartAndWaitUntilTransferring(int waitTimeSecs = 15, DODownloadRanges rangeInfo = null)
{
var ranges = !DODownloadRanges.IsNullOrEmpty(rangeInfo) ? rangeInfo : new DODownloadRanges();
Start(ranges);
WaitUntilTransferring(waitTimeSecs);
}
/// <summary>
/// Wait until the download is in transferring state.
/// </summary>
/// <param name="waitTimeSecs">How long we should wait</param>
/// <param name="isFromPaused">Whether we started from a paused state. If true, we won't bail out if the state is paused.</param>
public void WaitUntilTransferring(int waitTimeSecs, bool isFromPaused = false)
{
Handler.WaitForState(DODownloadState.Transferring, waitTimeSecs, this,
(isFromPaused ? new DODownloadState[] { } : new[] { DODownloadState.Paused }));
}
public void WaitUntilTransferred(int waitTimeSecs)
{
Console.WriteLine($"Download {Id}: waiting until transferred state");
Handler.WaitForState(DODownloadState.Transferred, waitTimeSecs, this,
new DODownloadState[] { DODownloadState.Paused });
}
/// <summary>
/// Use this to clear any state from the callback handler. Useful if we need to clear out
/// any reported errors before resuming a job.
/// </summary>
public void ResetHandlerState()
{
Handler.Reset();
}
public int ErrorCode()
{
return Handler.GetLastDownloadStatus().Error;
}
public int ExtendedErrorCode()
{
return Handler.GetLastDownloadStatus().ExtendedError;
}
public bool IsStatusComplete()
{
return Handler.GetDownloadCompleteStatus();
}
public bool IsStatusError()
{
return (ErrorCode() != 0);
}
public bool IsStatusExtendedError()
{
return (ExtendedErrorCode() != 0);
}
}
}