1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . IO ;
44using System . Net ;
@@ -62,20 +62,16 @@ private void ShutdownProtoBuf()
6262 while ( m_WaitingProtoBufQueue . Count > 0 )
6363 {
6464 var webData = m_WaitingProtoBufQueue . Dequeue ( ) ;
65- webData . Dispose ( ) ;
65+ ReferencePool . Release ( webData ) ;
6666 }
6767
6868 m_WaitingProtoBufQueue . Clear ( ) ;
69- while ( m_SendingProtoBufList . Count > 0 )
69+ for ( int i = 0 ; i < m_SendingProtoBufList . Count ; i ++ )
7070 {
71- var webData = m_SendingProtoBufList [ 0 ] ;
72- m_SendingProtoBufList . RemoveAt ( 0 ) ;
73- webData . Dispose ( ) ;
71+ ReferencePool . Release ( m_SendingProtoBufList [ i ] ) ;
7472 }
7573
7674 m_SendingProtoBufList . Clear ( ) ;
77-
78- m_MemoryStream . Dispose ( ) ;
7975 }
8076
8177 /// <summary>
@@ -96,23 +92,29 @@ private async void MakeProtoBufBytesRequest(WebProtoBufData webData)
9692 }
9793
9894 unityWebRequest . timeout = ( int ) RequestTimeout . TotalSeconds ;
99- {
100- unityWebRequest . SetRequestHeader ( "Content-Type" , ProtoBufContentType ) ;
101- byte [ ] postData = webData . SendData ;
102- unityWebRequest . uploadHandler = new UnityEngine . Networking . UploadHandlerRaw ( postData ) ;
103- }
95+ unityWebRequest . SetRequestHeader ( "Content-Type" , ProtoBufContentType ) ;
96+ byte [ ] postData = webData . SendData ;
97+ unityWebRequest . uploadHandler = new UnityEngine . Networking . UploadHandlerRaw ( postData ) ;
10498
10599 var asyncOperation = unityWebRequest . SendWebRequest ( ) ;
106100 asyncOperation . completed += ( asyncOperation2 ) =>
107101 {
108- m_SendingProtoBufList . Remove ( webData ) ;
109- if ( unityWebRequest . isNetworkError || unityWebRequest . isHttpError || unityWebRequest . error != null )
102+ try
110103 {
111- webData . Task . TrySetException ( new Exception ( unityWebRequest . error ) ) ;
112- return ;
113- }
104+ m_SendingProtoBufList . Remove ( webData ) ;
105+ if ( unityWebRequest . isNetworkError || unityWebRequest . isHttpError || unityWebRequest . error != null )
106+ {
107+ webData . Task . TrySetException ( new Exception ( unityWebRequest . error ) ) ;
108+ return ;
109+ }
114110
115- webData . Task . SetResult ( new WebBufferResult ( webData . UserData , unityWebRequest . downloadHandler . data ) ) ;
111+ webData . Task . SetResult ( new WebBufferResult ( webData . UserData , unityWebRequest . downloadHandler . data ) ) ;
112+ }
113+ finally
114+ {
115+ unityWebRequest . Dispose ( ) ;
116+ ReferencePool . Release ( webData ) ;
117+ }
116118 } ;
117119#else
118120 try
@@ -132,10 +134,11 @@ private async void MakeProtoBufBytesRequest(WebProtoBufData webData)
132134 {
133135 using ( Stream responseStream = response . GetResponseStream ( ) )
134136 {
135- m_MemoryStream . SetLength ( response . ContentLength ) ;
136- m_MemoryStream . Position = 0 ;
137- await responseStream . CopyToAsync ( m_MemoryStream ) ;
138- webData . Task . SetResult ( new WebBufferResult ( webData . UserData , m_MemoryStream . ToArray ( ) ) ) ; // 将流的内容复制到内存流中并转换为byte数组
137+ using ( var ms = new MemoryStream ( ) )
138+ {
139+ await responseStream . CopyToAsync ( ms ) ;
140+ webData . Task . SetResult ( new WebBufferResult ( webData . UserData , ms . ToArray ( ) ) ) ;
141+ }
139142 }
140143 }
141144 }
@@ -161,6 +164,7 @@ private async void MakeProtoBufBytesRequest(WebProtoBufData webData)
161164 finally
162165 {
163166 m_SendingProtoBufList . Remove ( webData ) ;
167+ ReferencePool . Release ( webData ) ;
164168 }
165169#endif
166170 }
@@ -219,7 +223,7 @@ private Task<WebBufferResult> PostInner(string url, MessageObject message, objec
219223 Body = SerializerHelper . Serialize ( message ) ,
220224 } ;
221225 var sendData = SerializerHelper . Serialize ( messageHttpObject ) ;
222- var webData = new WebProtoBufData ( url , sendData , uniTaskCompletionSource , userData ) ;
226+ var webData = WebProtoBufData . Create ( url , sendData , uniTaskCompletionSource , userData ) ;
223227 m_WaitingProtoBufQueue . Enqueue ( webData ) ;
224228 return uniTaskCompletionSource . Task ;
225229 }
@@ -240,4 +244,4 @@ private void DebugSendLog(MessageObject messageObject)
240244#endif
241245 }
242246 }
243- }
247+ }
0 commit comments