-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.go
More file actions
44 lines (40 loc) · 729 Bytes
/
packet.go
File metadata and controls
44 lines (40 loc) · 729 Bytes
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
package main
import (
"bufio"
)
func readPacket(r *bufio.Reader, buf *[7]byte) int {
for {
b := readByte(r)
for b == 0 {
b = readByte(r)
}
if b == 0x80 {
continue // Synchronization packet
}
if b&0x03 == 0 {
// Protocol packet
return readProtoPayload(r, buf, b)
} else {
// Source packet
return readSourcePayload(r, buf, b)
}
}
}
func readProtoPayload(r *bufio.Reader, buf *[7]byte, b byte) int {
var n int
for {
buf[n] = b
if n++; n == len(buf) || b&0x80 == 0 {
return n
}
b = readByte(r)
}
}
func readSourcePayload(r *bufio.Reader, buf *[7]byte, b byte) int {
pktlen := 1 + 1<<(b&3-1)
buf[0] = b
for n := 1; n < pktlen; n++ {
buf[n] = readByte(r)
}
return pktlen
}