@@ -213,23 +213,32 @@ private async Task HandleClientAsync(TcpClientWrapper client, CancellationToken
213213 {
214214 var requestBytes = new List < byte > ( ) ;
215215
216+ // Waiting for next request
217+ byte [ ] headerBytes = await stream . ReadExpectedBytesAsync ( 6 , cancellationToken ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
218+ requestBytes . AddRange ( headerBytes ) ;
219+
220+ ushort length = headerBytes
221+ . Skip ( 4 ) . Take ( 2 ) . ToArray ( )
222+ . GetBigEndianUInt16 ( ) ;
223+
224+ // Waiting for the remaining required data
216225 using ( var cts = new CancellationTokenSource ( ReadWriteTimeout ) )
217226 using ( cancellationToken . Register ( cts . Cancel ) )
218227 {
219- byte [ ] headerBytes = await stream . ReadExpectedBytesAsync ( 6 , cts . Token ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
220- requestBytes . AddRange ( headerBytes ) ;
221-
222- ushort length = headerBytes
223- . Skip ( 4 ) . Take ( 2 ) . ToArray ( )
224- . GetBigEndianUInt16 ( ) ;
225-
226228 byte [ ] bodyBytes = await stream . ReadExpectedBytesAsync ( length , cts . Token ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
227229 requestBytes . AddRange ( bodyBytes ) ;
228230 }
229231
230232 byte [ ] responseBytes = await HandleRequestAsync ( [ .. requestBytes ] , cancellationToken ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
231233 if ( responseBytes != null )
232- await stream . WriteAsync ( responseBytes , 0 , responseBytes . Length , cancellationToken ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
234+ {
235+ // Write response when available
236+ using ( var cts = new CancellationTokenSource ( ReadWriteTimeout ) )
237+ using ( cancellationToken . Register ( cts . Cancel ) )
238+ {
239+ await stream . WriteAsync ( responseBytes , 0 , responseBytes . Length , cts . Token ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
240+ }
241+ }
233242 }
234243 }
235244 catch
0 commit comments