-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
84 lines (71 loc) · 1.58 KB
/
protocol.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
79
80
81
82
83
84
package bilidanmaku
import (
"encoding/binary"
"net"
)
// [body len] [head+ver] [action ] [param ]
// [0 0 1 78] [0 16 0 0] [0 0 0 5] [0 0 0 0]
const (
_HeadLength = 16
_HeartLength = 4
_Body = 4
_Head = 2
_Version = 2
_Action = 4
_Param = 4
)
var (
_WsHeart = string("[object Object]")
)
type liveMsg struct {
Hlen int
Blen int
Version int
Action int
Param int
Head []byte
Data []byte
}
func (v *liveMsg) isZlib() bool {
return v.Version == 2
}
func (v *liveMsg) isHeart() bool {
return v.Version == 1
}
func (v *liveMsg) isRawJson() bool {
return v.Version == 0
}
func b4int(src []byte) int {
return int(binary.BigEndian.Uint32(src))
}
func b2int(src []byte) int {
raw := make([]byte, 2)
raw = append(raw, src...)
return int(binary.BigEndian.Uint32(raw))
}
func parseHead(data []byte) (res *liveMsg) {
res = &liveMsg{}
res.Head = data[:_Body+_Head+_Version+_Action+_Param]
pos := 0
// 前4个为长度 0 0 1 78
// 后12个为标识 0 16 0 0 0 0 0 5 0 0 0 0
res.Blen = b4int(data[pos : pos+_Body])
pos += _Body
res.Hlen = b2int(data[pos : pos+_Head])
pos += _Head
res.Version = b2int(data[pos : pos+_Version])
pos += _Version
res.Action = b4int(data[pos : pos+_Action])
pos += _Param
res.Param = b4int(data[pos : pos+_Param])
return res
}
func parse(data []byte) (res *liveMsg, surplus []byte) {
res = parseHead(data[:_Body+_Head+_Version+_Action+_Param])
res.Data = data[_Body+_Head+_Version+_Action+_Param : res.Blen]
surplus = data[res.Blen:]
return
}
func ioParse(conn net.Conn) (res *liveMsg, err error) {
return nil, nil
}