14
14
package frame
15
15
16
16
import (
17
+ "fmt"
17
18
"io"
18
19
19
20
"github.com/antlabs/wsutil/enum"
@@ -22,15 +23,46 @@ import (
22
23
23
24
func ReadFrameFromReaderV2 (r io.Reader , headArray * [enum .MaxFrameHeaderSize ]byte , buf * []byte ) (f Frame2 , err error ) {
24
25
h , _ , err := ReadHeader (r , headArray )
26
+ if err != nil {
27
+ return f , fmt .Errorf ("ReadFrameFromReaderV2:%w" , err )
28
+ }
29
+
30
+ if cap (* buf ) < int (h .PayloadLen ) {
31
+ // TODO sync.Pool 处理
32
+ * buf = make ([]byte , h .PayloadLen )
33
+ }
34
+ * buf = (* buf )[:h .PayloadLen ]
35
+ n1 , err := io .ReadFull (r , * buf )
25
36
if err != nil {
26
37
return f , err
27
38
}
39
+ if n1 != int (h .PayloadLen ) {
40
+ return f , io .ErrUnexpectedEOF
41
+ }
42
+ f .Payload = buf
43
+ f .FrameHeader = h
44
+ if h .Mask {
45
+ mask .Mask (* f .Payload , h .MaskKey )
46
+ }
47
+
48
+ return f , nil
49
+ }
50
+
51
+ func ReadFrameFromReaderV3 (r io.Reader , lr io.Reader , headArray * [enum .MaxFrameHeaderSize ]byte , buf * []byte ) (f Frame2 , err error ) {
52
+ h , _ , err := ReadHeader (r , headArray )
53
+ if err != nil {
54
+ return f , fmt .Errorf ("ReadFrameFromReaderV2:%w" , err )
55
+ }
28
56
29
57
if cap (* buf ) < int (h .PayloadLen ) {
30
58
// TODO sync.Pool 处理
31
59
* buf = make ([]byte , h .PayloadLen )
32
60
}
33
61
* buf = (* buf )[:h .PayloadLen ]
62
+ if lr != nil {
63
+ r = lr
64
+ }
65
+
34
66
n1 , err := io .ReadFull (r , * buf )
35
67
if err != nil {
36
68
return f , err
0 commit comments