forked from egametang/ET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityWebRequestRenewalAsync.cs
More file actions
360 lines (305 loc) · 10.6 KB
/
Copy pathUnityWebRequestRenewalAsync.cs
File metadata and controls
360 lines (305 loc) · 10.6 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace ET
{
public class UnityWebRequestRenewalUpdateSystem: UpdateSystem<UnityWebRequestRenewalAsync>
{
public override void Update(UnityWebRequestRenewalAsync self)
{
self.Update();
}
}
/// <summary>
/// 断点续传实现
/// </summary>
public class UnityWebRequestRenewalAsync: Entity
{
public class AcceptAllCertificate: CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
return true;
}
}
public static AcceptAllCertificate certificateHandler = new AcceptAllCertificate();
public UnityWebRequest headRequest;
public bool isCancel;
public ETTask tcs;
//请求资源类型
private class RequestType
{
public const int None = 0; //未开始
public const int Head = 1; //文件头
public const int Data = 2; //文件数据
}
//当前请求资源类型
private int requestType = RequestType.None;
//多任务同时请求
private readonly List<UnityWebRequest> dataRequests = new List<UnityWebRequest>();
//当前下载的文件流 边下边存文件流
private FileStream fileStream;
//资源地址
private string Url;
//每个下载包长度
private int packageLength = 1000000; //1M
//同时开启任务最大个数
private int maxCount = 20;
//已经写入文件的大小
private long byteWrites;
//文件总大小
private long totalBytes;
//当前请求的位置
private long downloadIndex;
//文件下载是否出错
private string dataError
{
get
{
foreach (UnityWebRequest webRequest in this.dataRequests)
{
if (!string.IsNullOrEmpty(webRequest.error))
{
return webRequest.error;
}
}
return "";
}
}
//批量开启任务下载
private void DownloadPackages()
{
if (dataRequests.Count >= maxCount || this.downloadIndex == totalBytes - 1)
{
return;
}
//开启一个下载任务
void DownloadPackage(long start, long end)
{
this.downloadIndex = end;
Log.Debug($"Request Data ({start}~{end}):{Url}");
UnityWebRequest request = UnityWebRequest.Get(Url);
dataRequests.Add(request);
request.certificateHandler = certificateHandler;
request.SetRequestHeader("Range", $"bytes={start}-{end}");
request.SendWebRequest();
}
//开启批量下载
for (int i = dataRequests.Count; i < maxCount; i++)
{
long start = this.byteWrites + i * packageLength;
long end = this.byteWrites + (i + 1) * packageLength - 1;
if (end > this.totalBytes)
{
end = this.totalBytes - 1;
}
DownloadPackage(start, end);
if (end == this.totalBytes - 1)
{
break;
}
}
}
//一次批量下载完成后写文件
private void WritePackages()
{
//写入单个包
void WritePackage(UnityWebRequest webRequest)
{
byte[] buff = webRequest.downloadHandler.data;
if (buff != null && buff.Length > 0)
{
this.fileStream.Write(buff, 0, buff.Length);
this.byteWrites += buff.Length;
}
Log.Debug($"write file Length:{byteWrites}");
}
//从第一个开始顺序写入
while (this.dataRequests.Count > 0 && dataRequests[0].isDone)
{
UnityWebRequest first = dataRequests[0];
dataRequests.RemoveAt(0);
WritePackage(first);
first.Dispose();
}
}
//更新文件体下载
private void UpdatePackages()
{
if (this.isCancel)
{
this.tcs.SetException(new Exception($"request data error: {dataError}"));
return;
}
if (!string.IsNullOrEmpty(dataError))
{
this.tcs.SetException(new Exception($"request data error: {dataError}"));
return;
}
this.WritePackages();
if (this.byteWrites == this.totalBytes)
{
this.tcs.SetResult();
}
else
{
this.DownloadPackages();
}
}
//更新文件头下载
private void UpdateHead()
{
if (this.isCancel)
{
this.tcs.SetException(new Exception($"request error: {this.headRequest.error}"));
return;
}
if (!this.headRequest.isDone)
{
return;
}
if (!string.IsNullOrEmpty(this.headRequest.error))
{
this.tcs.SetException(new Exception($"request error: {this.headRequest.error}"));
return;
}
this.tcs.SetResult();
}
//检测是不是同一个文件
private bool CheckSameFile(string modifiedTime)
{
string cacheValue = PlayerPrefs.GetString(Url);
string currentValue = this.totalBytes + "|" + modifiedTime;
if (cacheValue == currentValue)
{
return true;
}
PlayerPrefs.SetString(Url, currentValue);
PlayerPrefs.Save();
Log.Debug($"断点续传下载一个新的文件:{Url} cacheValue:{cacheValue} currentValue:{currentValue}");
return false;
}
/// <summary>
/// 断点续传入口
/// </summary>
/// <param name="url">文件下载地址</param>
/// <param name="filePath">文件写入路径</param>
/// <param name="packageLength">单个任务包体字节大小</param>
/// <param name="maxCount">同时开启最大任务个数</param>
/// <returns></returns>
public async ETTask DownloadAsync(string url, string filePath, int packageLength = 1000000, int maxCount = 20)
{
try
{
url = url.Replace(" ", "%20");
this.Url = url;
this.packageLength = packageLength;
this.maxCount = maxCount;
Log.Debug("Web Request:" + url);
#region Download File Header
this.requestType = RequestType.Head;
//下载文件头
Log.Debug($"Request Head: {Url}");
this.tcs = ETTask.Create(true);
this.headRequest = UnityWebRequest.Head(Url);
this.headRequest.SendWebRequest();
await this.tcs;
this.totalBytes = long.Parse(this.headRequest.GetResponseHeader("Content-Length"));
string modifiedTime = this.headRequest.GetResponseHeader("Last-Modified");
Log.Debug($"totalBytes: {this.totalBytes}");
this.headRequest?.Dispose();
this.headRequest = null;
#endregion
#region Check Local File
//打开或创建
fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
//获取已下载长度
this.byteWrites = fileStream.Length;
//通过本地缓存的服务器文件修改时间和文件总长度检测服务器是否是同一个文件 不是同一个从头开始写入
if (!CheckSameFile(modifiedTime))
{
this.byteWrites = 0;
}
Log.Debug($"byteWrites: {this.byteWrites}");
if (this.byteWrites == this.totalBytes)
{
Log.Debug("已经下载完成2");
return;
}
//设置开始写入位置
fileStream.Seek(this.byteWrites, SeekOrigin.Begin);
#endregion
#region Download File Data
//下载文件数据
requestType = RequestType.Data;
Log.Debug($"Request Data: {Url}");
this.tcs = ETTask.Create(true);
this.DownloadPackages();
await this.tcs;
#endregion
}
catch (Exception e)
{
Log.Error($"下载:{Url} Exception:{e}");
throw;
}
}
//下载进度
public float Progress
{
get
{
if (this.totalBytes == 0)
{
return 0;
}
return (float) ((this.byteWrites + ByteDownloaded) / (double) this.totalBytes);
}
}
//当前任务已经下载的长度
public long ByteDownloaded
{
get
{
long length = 0;
foreach (UnityWebRequest dataRequest in this.dataRequests)
{
length += dataRequest.downloadHandler.data.Length;
}
return length;
}
}
public void Update()
{
if (this.requestType == RequestType.Head)
{
this.UpdateHead();
}
if (this.requestType == RequestType.Data)
{
this.UpdatePackages();
}
}
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
headRequest?.Dispose();
headRequest = null;
foreach (UnityWebRequest dataRequest in this.dataRequests)
{
dataRequest.Dispose();
}
dataRequests.Clear();
this.fileStream?.Close();
this.fileStream?.Dispose();
this.fileStream = null;
this.isCancel = false;
}
}
}