返回 DeepSeek-Reasonix
framing.go
根目录 / internal / extension / rpcwire / framing.go
1 package rpcwire
2
3 import (
4 "bufio"
5 "errors"
6 )
7
8 func readLine(br *bufio.Reader, maxBytes int) ([]byte, error) {
9 var buf []byte
10 for {
11 chunk, err := br.ReadSlice('\n')
12 buf = append(buf, chunk...)
13 if maxBytes > 0 && len(buf) > maxBytes {
14 return nil, &FrameTooLargeError{Direction: "inbound", Size: len(buf), Limit: maxBytes}
15 }
16 if errors.Is(err, bufio.ErrBufferFull) {
17 continue
18 }
19 n := len(buf)
20 for n > 0 && (buf[n-1] == '\n' || buf[n-1] == '\r') {
21 n--
22 }
23 return trimSpace(buf[:n]), err
24 }
25 }
26
27 func trimSpace(b []byte) []byte {
28 i, j := 0, len(b)
29 for i < j && isSpace(b[i]) {
30 i++
31 }
32 for j > i && isSpace(b[j-1]) {
33 j--
34 }
35 return b[i:j]
36 }
37
38 func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' }
39
39 lines GO