Skip to content

Commit 466d772

Browse files
author
Jiang Yin
committed
下载模块支持下载超过2GB的文件
1 parent 22fb200 commit 466d772

11 files changed

+74
-68
lines changed

GameFramework/Download/DownloadAgentHelperCompleteEventArgs.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public sealed class DownloadAgentHelperCompleteEventArgs : GameFrameworkEventArg
1717
/// </summary>
1818
public DownloadAgentHelperCompleteEventArgs()
1919
{
20-
Length = 0;
20+
Length = 0L;
2121
}
2222

2323
/// <summary>
2424
/// 获取下载的数据大小。
2525
/// </summary>
26-
public int Length
26+
public long Length
2727
{
2828
get;
2929
private set;
@@ -34,9 +34,9 @@ public int Length
3434
/// </summary>
3535
/// <param name="length">下载的数据大小。</param>
3636
/// <returns>创建的下载代理辅助器完成事件。</returns>
37-
public static DownloadAgentHelperCompleteEventArgs Create(int length)
37+
public static DownloadAgentHelperCompleteEventArgs Create(long length)
3838
{
39-
if (length < 0)
39+
if (length < 0L)
4040
{
4141
throw new GameFrameworkException("Length is invalid.");
4242
}
@@ -51,7 +51,7 @@ public static DownloadAgentHelperCompleteEventArgs Create(int length)
5151
/// </summary>
5252
public override void Clear()
5353
{
54-
Length = 0;
54+
Length = 0L;
5555
}
5656
}
5757
}

GameFramework/Download/DownloadAgentHelperUpdateLengthEventArgs.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public sealed class DownloadAgentHelperUpdateLengthEventArgs : GameFrameworkEven
1717
/// </summary>
1818
public DownloadAgentHelperUpdateLengthEventArgs()
1919
{
20-
DeltaLength = 0;
20+
DeltaLength = 0L;
2121
}
2222

2323
/// <summary>
2424
/// 获取下载的增量数据大小。
2525
/// </summary>
26-
public int DeltaLength
26+
public long DeltaLength
2727
{
2828
get;
2929
private set;
@@ -34,9 +34,9 @@ public int DeltaLength
3434
/// </summary>
3535
/// <param name="deltaLength">下载的增量数据大小。</param>
3636
/// <returns>创建的下载代理辅助器更新数据大小事件。</returns>
37-
public static DownloadAgentHelperUpdateLengthEventArgs Create(int deltaLength)
37+
public static DownloadAgentHelperUpdateLengthEventArgs Create(long deltaLength)
3838
{
39-
if (deltaLength <= 0)
39+
if (deltaLength <= 0L)
4040
{
4141
throw new GameFrameworkException("Delta length is invalid.");
4242
}
@@ -51,7 +51,7 @@ public static DownloadAgentHelperUpdateLengthEventArgs Create(int deltaLength)
5151
/// </summary>
5252
public override void Clear()
5353
{
54-
DeltaLength = 0;
54+
DeltaLength = 0L;
5555
}
5656
}
5757
}

GameFramework/Download/DownloadManager.DownloadAgent.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ private sealed class DownloadAgent : ITaskAgent<DownloadTask>, IDisposable
2222
private FileStream m_FileStream;
2323
private int m_WaitFlushSize;
2424
private float m_WaitTime;
25-
private int m_StartLength;
26-
private int m_DownloadedLength;
27-
private int m_SavedLength;
25+
private long m_StartLength;
26+
private long m_DownloadedLength;
27+
private long m_SavedLength;
2828
private bool m_Disposed;
2929

3030
public GameFrameworkAction<DownloadAgent> DownloadAgentStart;
31-
public GameFrameworkAction<DownloadAgent, int> DownloadAgentUpdate;
32-
public GameFrameworkAction<DownloadAgent, int> DownloadAgentSuccess;
31+
public GameFrameworkAction<DownloadAgent, long> DownloadAgentUpdate;
32+
public GameFrameworkAction<DownloadAgent, long> DownloadAgentSuccess;
3333
public GameFrameworkAction<DownloadAgent, string> DownloadAgentFailure;
3434

3535
/// <summary>
@@ -48,9 +48,9 @@ public DownloadAgent(IDownloadAgentHelper downloadAgentHelper)
4848
m_FileStream = null;
4949
m_WaitFlushSize = 0;
5050
m_WaitTime = 0f;
51-
m_StartLength = 0;
52-
m_DownloadedLength = 0;
53-
m_SavedLength = 0;
51+
m_StartLength = 0L;
52+
m_DownloadedLength = 0L;
53+
m_SavedLength = 0L;
5454
m_Disposed = false;
5555

5656
DownloadAgentStart = null;
@@ -84,7 +84,7 @@ public float WaitTime
8484
/// <summary>
8585
/// 获取开始下载时已经存在的大小。
8686
/// </summary>
87-
public int StartLength
87+
public long StartLength
8888
{
8989
get
9090
{
@@ -95,7 +95,7 @@ public int StartLength
9595
/// <summary>
9696
/// 获取本次已经下载的大小。
9797
/// </summary>
98-
public int DownloadedLength
98+
public long DownloadedLength
9999
{
100100
get
101101
{
@@ -106,7 +106,7 @@ public int DownloadedLength
106106
/// <summary>
107107
/// 获取当前的大小。
108108
/// </summary>
109-
public int CurrentLength
109+
public long CurrentLength
110110
{
111111
get
112112
{
@@ -117,7 +117,7 @@ public int CurrentLength
117117
/// <summary>
118118
/// 获取已经存盘的大小。
119119
/// </summary>
120-
public int SavedLength
120+
public long SavedLength
121121
{
122122
get
123123
{
@@ -190,9 +190,9 @@ public StartTaskStatus Start(DownloadTask task)
190190
if (File.Exists(downloadFile))
191191
{
192192
m_FileStream = File.OpenWrite(downloadFile);
193-
m_FileStream.Seek(0, SeekOrigin.End);
194-
m_StartLength = m_SavedLength = (int)m_FileStream.Length;
195-
m_DownloadedLength = 0;
193+
m_FileStream.Seek(0L, SeekOrigin.End);
194+
m_StartLength = m_SavedLength = m_FileStream.Length;
195+
m_DownloadedLength = 0L;
196196
}
197197
else
198198
{
@@ -203,15 +203,15 @@ public StartTaskStatus Start(DownloadTask task)
203203
}
204204

205205
m_FileStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write);
206-
m_StartLength = m_SavedLength = m_DownloadedLength = 0;
206+
m_StartLength = m_SavedLength = m_DownloadedLength = 0L;
207207
}
208208

209209
if (DownloadAgentStart != null)
210210
{
211211
DownloadAgentStart(this);
212212
}
213213

214-
if (m_StartLength > 0)
214+
if (m_StartLength > 0L)
215215
{
216216
m_Helper.Download(m_Task.DownloadUri, m_StartLength, m_Task.UserData);
217217
}
@@ -247,9 +247,9 @@ public void Reset()
247247
m_Task = null;
248248
m_WaitFlushSize = 0;
249249
m_WaitTime = 0f;
250-
m_StartLength = 0;
251-
m_DownloadedLength = 0;
252-
m_SavedLength = 0;
250+
m_StartLength = 0L;
251+
m_DownloadedLength = 0L;
252+
m_SavedLength = 0L;
253253
}
254254

255255
/// <summary>

GameFramework/Download/DownloadManager.DownloadCounter.DownloadCounterNode.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ private sealed partial class DownloadCounter
1313
{
1414
private sealed class DownloadCounterNode : IReference
1515
{
16-
private int m_DownloadedLength;
16+
private long m_DeltaLength;
1717
private float m_ElapseSeconds;
1818

1919
public DownloadCounterNode()
2020
{
21-
m_DownloadedLength = 0;
21+
m_DeltaLength = 0L;
2222
m_ElapseSeconds = 0f;
2323
}
2424

25-
public int DownloadedLength
25+
public long DeltaLength
2626
{
2727
get
2828
{
29-
return m_DownloadedLength;
29+
return m_DeltaLength;
3030
}
3131
}
3232

@@ -38,26 +38,24 @@ public float ElapseSeconds
3838
}
3939
}
4040

41-
public static DownloadCounterNode Create(int downloadedLength)
41+
public static DownloadCounterNode Create()
4242
{
43-
DownloadCounterNode downloadCounterNode = ReferencePool.Acquire<DownloadCounterNode>();
44-
downloadCounterNode.m_DownloadedLength = downloadedLength;
45-
return downloadCounterNode;
43+
return ReferencePool.Acquire<DownloadCounterNode>();
4644
}
4745

4846
public void Update(float elapseSeconds, float realElapseSeconds)
4947
{
5048
m_ElapseSeconds += realElapseSeconds;
5149
}
5250

53-
public void AddDownloadedLength(int downloadedLength)
51+
public void AddDeltaLength(long deltaLength)
5452
{
55-
m_DownloadedLength += downloadedLength;
53+
m_DeltaLength += deltaLength;
5654
}
5755

5856
public void Clear()
5957
{
60-
m_DownloadedLength = 0;
58+
m_DeltaLength = 0L;
6159
m_ElapseSeconds = 0f;
6260
}
6361
}

GameFramework/Download/DownloadManager.DownloadCounter.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,38 @@ public void Update(float elapseSeconds, float realElapseSeconds)
124124

125125
if (m_TimeLeft <= 0f)
126126
{
127-
int totalDownloadLength = 0;
127+
long totalDeltaLength = 0L;
128128
foreach (DownloadCounterNode downloadCounterNode in m_DownloadCounterNodes)
129129
{
130-
totalDownloadLength += downloadCounterNode.DownloadedLength;
130+
totalDeltaLength += downloadCounterNode.DeltaLength;
131131
}
132132

133-
m_CurrentSpeed = m_Accumulator > 0f ? totalDownloadLength / m_Accumulator : 0f;
133+
m_CurrentSpeed = m_Accumulator > 0f ? totalDeltaLength / m_Accumulator : 0f;
134134
m_TimeLeft += m_UpdateInterval;
135135
}
136136
}
137137

138-
public void RecordDownloadedLength(int downloadedLength)
138+
public void RecordDeltaLength(long deltaLength)
139139
{
140-
if (downloadedLength <= 0)
140+
if (deltaLength <= 0L)
141141
{
142142
return;
143143
}
144144

145+
DownloadCounterNode downloadCounterNode = null;
145146
if (m_DownloadCounterNodes.Count > 0)
146147
{
147-
DownloadCounterNode downloadCounterNode = m_DownloadCounterNodes.Last.Value;
148+
downloadCounterNode = m_DownloadCounterNodes.Last.Value;
148149
if (downloadCounterNode.ElapseSeconds < m_UpdateInterval)
149150
{
150-
downloadCounterNode.AddDownloadedLength(downloadedLength);
151+
downloadCounterNode.AddDeltaLength(deltaLength);
151152
return;
152153
}
153154
}
154155

155-
m_DownloadCounterNodes.AddLast(DownloadCounterNode.Create(downloadedLength));
156+
downloadCounterNode = DownloadCounterNode.Create();
157+
downloadCounterNode.AddDeltaLength(deltaLength);
158+
m_DownloadCounterNodes.AddLast(downloadCounterNode);
156159
}
157160

158161
private void Reset()

GameFramework/Download/DownloadManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ private void OnDownloadAgentStart(DownloadAgent sender)
349349
}
350350
}
351351

352-
private void OnDownloadAgentUpdate(DownloadAgent sender, int lastDownloadedLength)
352+
private void OnDownloadAgentUpdate(DownloadAgent sender, long deltaLength)
353353
{
354-
m_DownloadCounter.RecordDownloadedLength(lastDownloadedLength);
354+
m_DownloadCounter.RecordDeltaLength(deltaLength);
355355
if (m_DownloadUpdateEventHandler != null)
356356
{
357357
DownloadUpdateEventArgs downloadUpdateEventArgs = DownloadUpdateEventArgs.Create(sender.Task.SerialId, sender.Task.DownloadPath, sender.Task.DownloadUri, sender.CurrentLength, sender.Task.UserData);
@@ -360,7 +360,7 @@ private void OnDownloadAgentUpdate(DownloadAgent sender, int lastDownloadedLengt
360360
}
361361
}
362362

363-
private void OnDownloadAgentSuccess(DownloadAgent sender, int lastDownloadedLength)
363+
private void OnDownloadAgentSuccess(DownloadAgent sender, long length)
364364
{
365365
if (m_DownloadSuccessEventHandler != null)
366366
{

GameFramework/Download/DownloadStartEventArgs.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public DownloadStartEventArgs()
2020
SerialId = 0;
2121
DownloadPath = null;
2222
DownloadUri = null;
23-
CurrentLength = 0;
23+
CurrentLength = 0L;
2424
UserData = null;
2525
}
2626

@@ -54,7 +54,7 @@ public string DownloadUri
5454
/// <summary>
5555
/// 获取当前大小。
5656
/// </summary>
57-
public int CurrentLength
57+
public long CurrentLength
5858
{
5959
get;
6060
private set;
@@ -78,7 +78,7 @@ public object UserData
7878
/// <param name="currentLength">当前大小。</param>
7979
/// <param name="userData">用户自定义数据。</param>
8080
/// <returns>创建的下载开始事件。</returns>
81-
public static DownloadStartEventArgs Create(int serialId, string downloadPath, string downloadUri, int currentLength, object userData)
81+
public static DownloadStartEventArgs Create(int serialId, string downloadPath, string downloadUri, long currentLength, object userData)
8282
{
8383
DownloadStartEventArgs downloadStartEventArgs = ReferencePool.Acquire<DownloadStartEventArgs>();
8484
downloadStartEventArgs.SerialId = serialId;
@@ -97,7 +97,7 @@ public override void Clear()
9797
SerialId = 0;
9898
DownloadPath = null;
9999
DownloadUri = null;
100-
CurrentLength = 0;
100+
CurrentLength = 0L;
101101
UserData = null;
102102
}
103103
}

GameFramework/Download/DownloadSuccessEventArgs.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public DownloadSuccessEventArgs()
2020
SerialId = 0;
2121
DownloadPath = null;
2222
DownloadUri = null;
23-
CurrentLength = 0;
23+
CurrentLength = 0L;
2424
UserData = null;
2525
}
2626

@@ -54,7 +54,7 @@ public string DownloadUri
5454
/// <summary>
5555
/// 获取当前大小。
5656
/// </summary>
57-
public int CurrentLength
57+
public long CurrentLength
5858
{
5959
get;
6060
private set;
@@ -78,7 +78,7 @@ public object UserData
7878
/// <param name="currentLength">当前大小。</param>
7979
/// <param name="userData">用户自定义数据。</param>
8080
/// <returns>创建的下载成功事件。</returns>
81-
public static DownloadSuccessEventArgs Create(int serialId, string downloadPath, string downloadUri, int currentLength, object userData)
81+
public static DownloadSuccessEventArgs Create(int serialId, string downloadPath, string downloadUri, long currentLength, object userData)
8282
{
8383
DownloadSuccessEventArgs downloadSuccessEventArgs = ReferencePool.Acquire<DownloadSuccessEventArgs>();
8484
downloadSuccessEventArgs.SerialId = serialId;
@@ -97,7 +97,7 @@ public override void Clear()
9797
SerialId = 0;
9898
DownloadPath = null;
9999
DownloadUri = null;
100-
CurrentLength = 0;
100+
CurrentLength = 0L;
101101
UserData = null;
102102
}
103103
}

0 commit comments

Comments
 (0)