@@ -63,6 +63,7 @@ public async Task<bool> UploadFile(Stream stream, int commandId, ReadOnlyMemory<
6363 ulong currentBlockOffset = offset ;
6464 var task = Task . Run ( async ( ) => // closure
6565 {
66+ var bufferSpan = buffer . AsSpan ( 0 , ( int ) payload ) ;
6667 int sequence = GetNewSequence ( ) ;
6768
6869 var head = new DataHighwayHead
@@ -81,7 +82,7 @@ public async Task<bool> UploadFile(Stream stream, int commandId, ReadOnlyMemory<
8182 DataOffset = currentBlockOffset ,
8283 DataLength = ( uint ) payload ,
8384 ServiceTicket = _ticket . Value . Item1 ,
84- Md5 = MD5 . HashData ( buffer . AsSpan ( 0 , ( int ) payload ) ) ,
85+ Md5 = MD5 . HashData ( bufferSpan ) ,
8586 FileMd5 = fileMd5 ,
8687 } ;
8788 var loginHead = new LoginSigHead
@@ -108,13 +109,12 @@ public async Task<bool> UploadFile(Stream stream, int commandId, ReadOnlyMemory<
108109 BinaryPrimitives . WriteUInt32BigEndian ( memory . Span [ 1 ..] , ( uint ) headProto . Length ) ;
109110 BinaryPrimitives . WriteUInt32BigEndian ( memory . Span [ 5 ..] , ( uint ) payload ) ;
110111 headProto . Span . CopyTo ( memory . Span [ 9 ..] ) ;
111- buffer . AsSpan ( 0 , ( int ) payload ) . CopyTo ( memory . Span [ ( 9 + headProto . Length ) ..] ) ;
112+ bufferSpan . CopyTo ( memory . Span [ ( 9 + headProto . Length ) ..] ) ;
112113 memory . Span [ ^ 1 ] = 0x29 ;
113114
114- var content = new ReadOnlyMemoryContent ( memory ) ;
115115 var request = new HttpRequestMessage ( HttpMethod . Post , $ "http://{ _url } ")
116116 {
117- Content = content , Headers = { { "Connection" , end ? "close" : "keep-alive" } }
117+ Content = new ReadOnlyMemoryContent ( memory ) , Headers = { { "Connection" , end ? "close" : "keep-alive" } }
118118 } ;
119119
120120 try
@@ -145,13 +145,15 @@ public async Task<bool> UploadFile(Stream stream, int commandId, ReadOnlyMemory<
145145 }
146146 finally
147147 {
148+ ArrayPool < byte > . Shared . Return ( buffer ) ;
148149 ArrayPool < byte > . Shared . Return ( upload ) ;
149150 request . Dispose ( ) ;
150- content . Dispose ( ) ;
151+ new ReadOnlyMemoryContent ( memory ) . Dispose ( ) ;
151152 }
152153
153154 return false ;
154155 } ) ;
156+ offset += payload ;
155157
156158 tasks . Add ( task ) ;
157159 if ( tasks . Count == ( _concurrent ) )
@@ -160,9 +162,7 @@ public async Task<bool> UploadFile(Stream stream, int commandId, ReadOnlyMemory<
160162 foreach ( bool t in successBlocks ) result &= t ;
161163 tasks . Clear ( ) ;
162164 }
163-
164- offset += payload ;
165-
165+
166166 if ( tasks . Count != 0 )
167167 {
168168 var finalBlocks = await Task . WhenAll ( tasks ) ;
0 commit comments