forked from jonaz/gombus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
78 lines (67 loc) · 1.68 KB
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gombus
import (
"bufio"
"fmt"
"io"
)
/*
// Does not handle timeout! the new one in tcp.go does
// perhaps use io.ReadCloser and close after timeout?
func ReadLongFrame(r io.Reader) (LongFrame, error) {
buf := bufio.NewReader(r)
// TODO if this should be used again must implement whole frame detection from conn.ReadLongFrame
msg, err := buf.ReadBytes(0x16)
if err != nil {
return nil, err
}
return LongFrame(msg), nil
}
*/
var ErrNoLongFrameFound = fmt.Errorf("no long frame found")
func ReadLongFrame(conn io.Reader) (LongFrame, error) {
buf := make([]byte, 4096)
tmp := make([]byte, 4096)
// foundStart := false
length := 0
globalN := -1
for {
n, err := conn.Read(tmp)
if err != nil {
return LongFrame{}, fmt.Errorf("error reading from connection: %w", err)
}
for _, b := range tmp[:n] {
globalN++
buf[globalN] = b
if globalN > 256 {
return LongFrame{}, ErrNoLongFrameFound
}
// look for end byte after length +C+A+CI+checksum
if length != 0 && globalN > length+4 && b == 0x16 {
return LongFrame(buf[:globalN+1]), nil
}
// look for start sequence 68 LL LL 68
if length == 0 && buf[0] == 0x68 && buf[3] == 0x68 && buf[1] == buf[2] {
length = int(buf[1])
}
}
}
}
func ReadSingleCharFrame(r io.Reader) (LongFrame, error) {
buf := bufio.NewReader(r)
msg, err := buf.ReadBytes(SingleCharacterFrame)
if err != nil {
return nil, err
}
return LongFrame(msg), nil
}
// ReadAnyAndPrint is used for debugging.
func ReadAnyAndPrint(r io.Reader) error {
tmp := make([]byte, 256) // using small tmo buffer for demonstrating
for {
n, err := r.Read(tmp)
if err != nil {
return err
}
fmt.Printf("% x\n", tmp[:n])
}
}