Skip to content

Commit 8a4d754

Browse files
committed
refactor(web): 将 WebBinaryData 改用引用池模式
WebBinaryData 改为工厂方法 Create() + Clear() 模式, WebManager.Binary 中 Dispose() 替换为 ReferencePool.Release()。 GameFrameX/com.gameframex.unity#5
1 parent 883cc37 commit 8a4d754

2 files changed

Lines changed: 64 additions & 20 deletions

File tree

Runtime/Web/WebManager.Binary.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace GameFrameX.Web.Runtime
1515
/// <summary>
1616
/// Web请求管理器的ProtoBuf部分实现
1717
/// </summary>
18-
public partial class WebManager : GameFrameworkModule, IWebManager
18+
public partial class WebManager
1919
{
2020
/// <summary>
2121
/// 等待处理的Binary请求队列
@@ -63,15 +63,15 @@ private void ShutdownBinary()
6363
while (m_WaitingBinaryQueue.Count > 0)
6464
{
6565
var webData = m_WaitingBinaryQueue.Dequeue();
66-
webData.Dispose();
66+
ReferencePool.Release(webData);
6767
}
6868

6969
m_WaitingBinaryQueue.Clear();
7070
while (m_SendingBinaryList.Count > 0)
7171
{
7272
var webData = m_SendingBinaryList[0];
7373
m_SendingBinaryList.RemoveAt(0);
74-
webData.Dispose();
74+
ReferencePool.Release(webData);
7575
}
7676

7777
m_SendingBinaryList.Clear();
@@ -119,10 +119,12 @@ private async void MakeBinaryBytesRequest(WebBinaryData webData)
119119
if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError || unityWebRequest.error != null)
120120
{
121121
webData.Task.TrySetException(new Exception(unityWebRequest.error));
122+
ReferencePool.Release(webData);
122123
return;
123124
}
124125

125126
webData.Task.SetResult(new WebBufferResult(webData.UserData, unityWebRequest.downloadHandler.data));
127+
ReferencePool.Release(webData);
126128
};
127129
#else
128130
try
@@ -189,6 +191,7 @@ private async void MakeBinaryBytesRequest(WebBinaryData webData)
189191
finally
190192
{
191193
m_SendingBinaryList.Remove(webData);
194+
ReferencePool.Release(webData);
192195
}
193196

194197
#endif
@@ -210,7 +213,7 @@ public Task<WebBufferResult> PostToBytes(string url, byte[] from, Dictionary<str
210213
queryString = MergeQueryString(queryString);
211214
header = MergeHeader(header);
212215
url = UrlHandler(url, queryString);
213-
var webData = new WebBinaryData(url, header, from, uniTaskCompletionSource, userData);
216+
var webData = WebBinaryData.Create(url, header, from, uniTaskCompletionSource, userData);
214217
m_SendingBinaryList.Add(webData);
215218
MakeBinaryBytesRequest(webData);
216219
return uniTaskCompletionSource.Task;

Runtime/Web/WebManager.WebBinaryData.cs

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,77 @@ public partial class WebManager
1111
/// </summary>
1212
private sealed class WebBinaryData : WebData
1313
{
14+
private Dictionary<string, string> m_Header;
15+
1416
/// <summary>
1517
/// 获取请求任务的完成源,用于异步操作的控制和结果返回
1618
/// </summary>
17-
public readonly TaskCompletionSource<WebBufferResult> Task;
19+
public TaskCompletionSource<WebBufferResult> Task { get; private set; }
1820

1921
/// <summary>
20-
/// 获取要发送的Protocol Buffer序列化后的字节数组数据
22+
/// 获取要发送的字节数组数据
2123
/// </summary>
22-
public readonly byte[] SendData;
24+
public byte[] SendData { get; private set; }
2325

2426
/// <summary>
2527
/// 获取请求头信息
2628
/// </summary>
27-
public readonly Dictionary<string, string> Header;
29+
public Dictionary<string, string> Header { get { return m_Header; } }
30+
31+
/// <summary>
32+
/// 无参构造函数(ReferencePool 需要new约束)
33+
/// </summary>
34+
public WebBinaryData() { }
2835

2936
/// <summary>
30-
/// 初始化Web Binary请求数据
37+
/// 创建Web Binary请求数据
3138
/// </summary>
32-
/// <param name="url">请求URL</param>
33-
/// <param name="header">请求头信息</param>
34-
/// <param name="sendData">要发送的Binary序列化数据</param>
35-
/// <param name="task">请求任务的完成源</param>
36-
/// <param name="userData">用户自定义数据</param>
37-
public WebBinaryData(string url, Dictionary<string, string> header, byte[] sendData, TaskCompletionSource<WebBufferResult> task, object userData) : base(false, url, userData)
39+
public static WebBinaryData Create(string url, Dictionary<string, string> header, byte[] sendData, TaskCompletionSource<WebBufferResult> task, object userData)
3840
{
39-
task.CheckNull(nameof(task));
40-
SendData = sendData;
41-
Task = task;
42-
Header = header;
41+
var data = ReferencePool.Acquire<WebBinaryData>();
42+
data.URL = url;
43+
data.IsGet = false;
44+
data.UserData = userData;
45+
data.Task = task;
46+
data.SendData = sendData;
47+
48+
if (header != null && header.Count > 0)
49+
{
50+
if (data.m_Header == null)
51+
{
52+
data.m_Header = new Dictionary<string, string>();
53+
}
54+
else
55+
{
56+
data.m_Header.Clear();
57+
}
58+
59+
foreach (var kv in header)
60+
{
61+
data.m_Header[kv.Key] = kv.Value;
62+
}
63+
}
64+
65+
return data;
66+
}
67+
68+
public override void Clear()
69+
{
70+
if (Task != null)
71+
{
72+
Task.TrySetCanceled();
73+
Task = null;
74+
}
75+
76+
SendData = null;
77+
78+
if (m_Header != null)
79+
{
80+
m_Header.Clear();
81+
}
82+
83+
base.Clear();
4384
}
4485
}
4586
}
46-
}
87+
}

0 commit comments

Comments
 (0)