@@ -395,80 +395,22 @@ private static async Task HandleClientAsync(TcpClient client)
395395 using ( client )
396396 using ( NetworkStream stream = client . GetStream ( ) )
397397 {
398- const int MaxMessageBytes = 64 * 1024 * 1024 ; // 64 MB safety cap
399398 byte [ ] buffer = new byte [ 8192 ] ;
400399 while ( isRunning )
401400 {
402401 try
403402 {
404- // Read message with optional length prefix (8-byte big-endian)
405- bool usedFraming = false ;
406- string commandText = null ;
407-
408- // First, attempt to read an 8-byte header
409- byte [ ] header = new byte [ 8 ] ;
410- int headerFilled = 0 ;
411- while ( headerFilled < 8 )
403+ int bytesRead = await stream . ReadAsync ( buffer , 0 , buffer . Length ) ;
404+ if ( bytesRead == 0 )
412405 {
413- int r = await stream . ReadAsync ( header , headerFilled , 8 - headerFilled ) ;
414- if ( r == 0 )
415- {
416- // Disconnected
417- return ;
418- }
419- headerFilled += r ;
420- }
421-
422- // Interpret header as big-endian payload length, with plausibility check
423- ulong payloadLen = ReadUInt64BigEndian ( header ) ;
424- if ( payloadLen > 0 && payloadLen <= ( ulong ) MaxMessageBytes )
425- {
426- // Framed message path
427- usedFraming = true ;
428- byte [ ] payload = await ReadExactAsync ( stream , ( int ) payloadLen ) ;
429- commandText = System . Text . Encoding . UTF8 . GetString ( payload ) ;
430- }
431- else
432- {
433- // Legacy path: treat header bytes as the beginning of a JSON/plain message and read until we have a full JSON
434- usedFraming = false ;
435- using var ms = new MemoryStream ( ) ;
436- ms . Write ( header , 0 , header . Length ) ;
437-
438- // Read available data in chunks; stop when we have valid JSON or ping, or when no more data available for now
439- while ( true )
440- {
441- // If we already have enough text, try to interpret
442- string currentText = System . Text . Encoding . UTF8 . GetString ( ms . ToArray ( ) ) ;
443- string trimmed = currentText . Trim ( ) ;
444- if ( trimmed == "ping" )
445- {
446- commandText = trimmed ;
447- break ;
448- }
449- if ( IsValidJson ( trimmed ) )
450- {
451- commandText = trimmed ;
452- break ;
453- }
454-
455- // Read next chunk
456- int r = await stream . ReadAsync ( buffer , 0 , buffer . Length ) ;
457- if ( r == 0 )
458- {
459- // Disconnected mid-message; fall back to whatever we have
460- commandText = currentText ;
461- break ;
462- }
463- ms . Write ( buffer , 0 , r ) ;
464-
465- if ( ms . Length > MaxMessageBytes )
466- {
467- throw new IOException ( $ "Incoming message exceeded { MaxMessageBytes } bytes cap") ;
468- }
469- }
406+ break ; // Client disconnected
470407 }
471408
409+ string commandText = System . Text . Encoding . UTF8 . GetString (
410+ buffer ,
411+ 0 ,
412+ bytesRead
413+ ) ;
472414 string commandId = Guid . NewGuid ( ) . ToString ( ) ;
473415 TaskCompletionSource < string > tcs = new ( ) ;
474416
@@ -480,14 +422,6 @@ private static async Task HandleClientAsync(TcpClient client)
480422 /*lang=json,strict*/
481423 "{\" status\" :\" success\" ,\" result\" :{\" message\" :\" pong\" }}"
482424 ) ;
483-
484- if ( usedFraming )
485- {
486- // Mirror framing for response
487- byte [ ] outHeader = new byte [ 8 ] ;
488- WriteUInt64BigEndian ( outHeader , ( ulong ) pingResponseBytes . Length ) ;
489- await stream . WriteAsync ( outHeader , 0 , outHeader . Length ) ;
490- }
491425 await stream . WriteAsync ( pingResponseBytes , 0 , pingResponseBytes . Length ) ;
492426 continue ;
493427 }
@@ -499,12 +433,6 @@ private static async Task HandleClientAsync(TcpClient client)
499433
500434 string response = await tcs . Task ;
501435 byte [ ] responseBytes = System . Text . Encoding . UTF8 . GetBytes ( response ) ;
502- if ( usedFraming )
503- {
504- byte [ ] outHeader = new byte [ 8 ] ;
505- WriteUInt64BigEndian ( outHeader , ( ulong ) responseBytes . Length ) ;
506- await stream . WriteAsync ( outHeader , 0 , outHeader . Length ) ;
507- }
508436 await stream . WriteAsync ( responseBytes , 0 , responseBytes . Length ) ;
509437 }
510438 catch ( Exception ex )
@@ -516,55 +444,6 @@ private static async Task HandleClientAsync(TcpClient client)
516444 }
517445 }
518446
519- // Read exactly count bytes or throw if stream closes prematurely
520- private static async Task < byte [ ] > ReadExactAsync ( NetworkStream stream , int count )
521- {
522- byte [ ] data = new byte [ count ] ;
523- int offset = 0 ;
524- while ( offset < count )
525- {
526- int r = await stream . ReadAsync ( data , offset , count - offset ) ;
527- if ( r == 0 )
528- {
529- throw new IOException ( "Connection closed before reading expected bytes" ) ;
530- }
531- offset += r ;
532- }
533- return data ;
534- }
535-
536- private static ulong ReadUInt64BigEndian ( byte [ ] buffer )
537- {
538- if ( buffer == null || buffer . Length < 8 )
539- {
540- return 0UL ;
541- }
542- return ( ( ulong ) buffer [ 0 ] << 56 )
543- | ( ( ulong ) buffer [ 1 ] << 48 )
544- | ( ( ulong ) buffer [ 2 ] << 40 )
545- | ( ( ulong ) buffer [ 3 ] << 32 )
546- | ( ( ulong ) buffer [ 4 ] << 24 )
547- | ( ( ulong ) buffer [ 5 ] << 16 )
548- | ( ( ulong ) buffer [ 6 ] << 8 )
549- | buffer [ 7 ] ;
550- }
551-
552- private static void WriteUInt64BigEndian ( byte [ ] dest , ulong value )
553- {
554- if ( dest == null || dest . Length < 8 )
555- {
556- throw new ArgumentException ( "Destination buffer too small for UInt64" ) ;
557- }
558- dest [ 0 ] = ( byte ) ( value >> 56 ) ;
559- dest [ 1 ] = ( byte ) ( value >> 48 ) ;
560- dest [ 2 ] = ( byte ) ( value >> 40 ) ;
561- dest [ 3 ] = ( byte ) ( value >> 32 ) ;
562- dest [ 4 ] = ( byte ) ( value >> 24 ) ;
563- dest [ 5 ] = ( byte ) ( value >> 16 ) ;
564- dest [ 6 ] = ( byte ) ( value >> 8 ) ;
565- dest [ 7 ] = ( byte ) ( value ) ;
566- }
567-
568447 private static void ProcessCommands ( )
569448 {
570449 List < string > processedIds = new ( ) ;
0 commit comments