Skip to content

Commit 84a3356

Browse files
committed
fix(web): 修复并发请求时 MemoryStream 数据覆盖问题
移除共享的 m_MemoryStream 字段,改为每个请求使用局部 MemoryStream, 避免多个并发请求在 await 点交叉时互相覆盖响应数据。 GameFrameX/com.gameframex.unity#5
1 parent d5107b1 commit 84a3356

2 files changed

Lines changed: 10 additions & 24 deletions

File tree

Runtime/Web/WebManager.Binary.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ private void ShutdownBinary()
7575
}
7676

7777
m_SendingBinaryList.Clear();
78-
79-
m_MemoryStream.Dispose();
8078
}
8179

8280
/// <summary>
@@ -152,20 +150,12 @@ private async void MakeBinaryBytesRequest(WebBinaryData webData)
152150
{
153151
using (Stream responseStream = response.GetResponseStream())
154152
{
155-
var transferEncoding = response.Headers.Get("Transfer-Encoding");
156-
if (string.IsNullOrWhiteSpace(transferEncoding))
157-
{
158-
m_MemoryStream.SetLength(response.ContentLength);
159-
}
160-
else
153+
using (var ms = new MemoryStream())
161154
{
162-
m_MemoryStream.SetLength(0);
155+
await responseStream.CopyToAsync(ms);
156+
var resultData = ms.ToArray();
157+
webData.TaskSource.SetResult(new WebBufferResult(webData.UserData, resultData)); // 将流的内容复制到内存流中并转换为byte数组
163158
}
164-
165-
m_MemoryStream.Position = 0;
166-
await responseStream.CopyToAsync(m_MemoryStream);
167-
var resultData = m_MemoryStream.ToArray();
168-
webData.TaskSource.SetResult(new WebBufferResult(webData.UserData, resultData)); // 将流的内容复制到内存流中并转换为byte数组
169159
}
170160
}
171161
}

Runtime/Web/WebManager.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ public partial class WebManager : GameFrameworkModule, IWebManager
2727
// 正在处理的普通请求列表
2828
private readonly List<WebJsonData> m_SendingNormalList = new List<WebJsonData>(16);
2929

30-
// 用于存储请求和响应数据的内存流
31-
private readonly MemoryStream m_MemoryStream;
32-
3330
// JSON内容类型常量
3431
private const string JsonContentType = "application/json; charset=utf-8";
3532

@@ -47,7 +44,6 @@ public partial class WebManager : GameFrameworkModule, IWebManager
4744
public WebManager()
4845
{
4946
MaxConnectionPerServer = 8;
50-
m_MemoryStream = new MemoryStream();
5147
Timeout = 5f;
5248
}
5349

@@ -126,7 +122,6 @@ protected override void Shutdown()
126122

127123
m_SendingNormalList.Clear();
128124
ShutdownBinary();
129-
m_MemoryStream.Dispose();
130125
}
131126

132127
/// <summary>
@@ -419,14 +414,15 @@ private async void MakeJsonBytesRequest(WebJsonData webJsonData)
419414
{
420415
using (Stream responseStream = response.GetResponseStream())
421416
{
422-
m_MemoryStream.SetLength(responseStream.Length);
423-
m_MemoryStream.Position = 0;
424-
await responseStream.CopyToAsync(m_MemoryStream);
425-
var resultData = m_MemoryStream.ToArray();
417+
using (var ms = new MemoryStream())
418+
{
419+
await responseStream.CopyToAsync(ms);
420+
var resultData = ms.ToArray();
426421
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
427422
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {resultData}");
428423
#endif
429-
webJsonData.UniTaskCompletionBytesSource.SetResult(new WebBufferResult(webJsonData.UserData, resultData)); // 将流的内容复制到内存流中并转换为byte数组
424+
webJsonData.UniTaskCompletionBytesSource.SetResult(new WebBufferResult(webJsonData.UserData, resultData)); // 将流的内容复制到内存流中并转换为byte数组
425+
}
430426
}
431427
}
432428
}

0 commit comments

Comments
 (0)