@@ -573,42 +573,45 @@ public IEnumerator<byte> GetEnumerator ()
573573
574574 public static WsFrame Parse ( byte [ ] src )
575575 {
576- return Parse ( src , true ) ;
576+ return Parse ( src , true , null ) ;
577577 }
578578
579579 public static WsFrame Parse ( Stream stream )
580580 {
581- return Parse ( stream , true ) ;
581+ return Parse ( stream , true , null ) ;
582582 }
583583
584- public static WsFrame Parse ( byte [ ] src , bool unmask )
584+ public static WsFrame Parse ( byte [ ] src , Action < Exception > error )
585585 {
586- using ( var stream = new MemoryStream ( src ) )
587- {
588- return Parse ( stream , unmask ) ;
589- }
586+ return Parse ( src , true , error ) ;
587+ }
588+
589+ public static WsFrame Parse ( Stream stream , Action < Exception > error )
590+ {
591+ return Parse ( stream , true , error ) ;
590592 }
591593
592- public static WsFrame Parse ( Stream stream , bool unmask )
594+ public static WsFrame Parse ( byte [ ] src , bool unmask , Action < Exception > error )
593595 {
594- return Parse ( stream , unmask , null ) ;
596+ using ( var stream = new MemoryStream ( src ) )
597+ {
598+ return Parse ( stream , unmask , error ) ;
599+ }
595600 }
596601
597602 public static WsFrame Parse ( Stream stream , bool unmask , Action < Exception > error )
598603 {
599604 WsFrame frame = null ;
600- try
601- {
605+ try {
602606 var header = stream . ReadBytes ( 2 ) ;
603607 frame = header . Length == 2
604608 ? parse ( header , stream , unmask )
605609 : CreateCloseFrame (
606610 Mask . UNMASK ,
607611 CloseStatusCode . ABNORMAL ,
608- "'Header' of a frame cannot be read from the data stream." ) ;
612+ "The header part of a frame cannot be read from the ' stream' ." ) ;
609613 }
610- catch ( Exception ex )
611- {
614+ catch ( Exception ex ) {
612615 if ( error != null )
613616 error ( ex ) ;
614617 }
@@ -629,43 +632,25 @@ public static void ParseAsync (Stream stream, Action<WsFrame> completed, Action<
629632 public static void ParseAsync (
630633 Stream stream , bool unmask , Action < WsFrame > completed , Action < Exception > error )
631634 {
632- var header = new byte [ 2 ] ;
633- AsyncCallback callback = ar =>
634- {
635- WsFrame frame = null ;
636- try
635+ stream . ReadBytesAsync (
636+ 2 ,
637+ header =>
637638 {
638- var readLen = stream . EndRead ( ar ) ;
639- if ( readLen == 1 )
640- {
641- var tmp = stream . ReadByte ( ) ;
642- if ( tmp > - 1 )
643- {
644- header [ 1 ] = ( byte ) tmp ;
645- readLen ++ ;
646- }
647- }
648-
649- frame = readLen == 2
650- ? parse ( header , stream , unmask )
651- : CreateCloseFrame (
652- Mask . UNMASK ,
653- CloseStatusCode . ABNORMAL ,
654- "'Header' of a frame cannot be read from the data stream." ) ;
655- }
656- catch ( Exception ex )
639+ var frame = header . Length == 2
640+ ? parse ( header , stream , unmask )
641+ : CreateCloseFrame (
642+ Mask . UNMASK ,
643+ CloseStatusCode . ABNORMAL ,
644+ "The header part of a frame cannot be read from the 'stream'." ) ;
645+
646+ if ( completed != null )
647+ completed ( frame ) ;
648+ } ,
649+ ex =>
657650 {
658651 if ( error != null )
659652 error ( ex ) ;
660- }
661- finally
662- {
663- if ( completed != null )
664- completed ( frame ) ;
665- }
666- } ;
667-
668- stream . BeginRead ( header , 0 , 2 , callback , null ) ;
653+ } ) ;
669654 }
670655
671656 public void Print ( bool dumped )
0 commit comments