-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
95 lines (88 loc) · 1.71 KB
/
handle.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
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"bytes"
"fmt"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/transform"
"log"
"strconv"
)
func handleNotify(b []byte) (r sBProtocolResult) {
var err error
if len(b) < 2 {
return
}
r.rqType, err = strconv.Atoi(string(b[:2]))
if err != nil {
return
}
switch r.rqType {
case SBC_PACKET_NIGHT_GETDAYS:
if len(b) < 14 {
return
}
r.byteData = b[2:14]
case SBC_PACKET_NIGHT_GETDAYS2:
if len(b) < 11 {
return
}
r.byteData = b[2:11]
case SBC_PACKET_GLOBAL_CONFIRMUID:
if len(b) < 20 {
return
}
r.uid = b[2]
r.byteData = b[4:20]
case SBC_PACKET_HOLIDAY_GET:
if !decodeIntArr(b, r) {
return
}
case SBC_PACKET_GLOBAL_FIRSTLOG:
if !decodeIntArr(b, r) {
return
}
case SBC_PACKET_GLOBAL_NEXTLOG:
if !decodeIntArr(b, r) {
return
}
}
b = b[2:]
b = b[:bytes.IndexByte(b, 0)]
e, err := ianaindex.MIME.Encoding("US-ASCII")
if err != nil {
log.Println(err)
}
str, _, err := transform.String(e.NewDecoder(), string(b))
if err != nil || len(str) == 0 {
return
}
r.stringData = str
if name, ok := topics[r.rqType]; ok {
r.rqName = name
} else {
log.Println("unknown rqType:", r.rqType)
}
if f, ok := packetTypes[r.rqType]; ok {
f(&r)
} else {
log.Println("unknown parser", r.rqName)
}
fmt.Printf("received: %s: %d | %d | % X | %q | %q | %q\n", r.rqName, r.rqType, r.uid, r.byteData, r.byteData, r.stringData, r.result)
return r
}
func decodeIntArr(b []byte, r sBProtocolResult) bool {
if len(b) < 18 {
return false
}
i0, err := strconv.Atoi(string(b[2:8]))
if err != nil {
return false
}
i1, err := strconv.Atoi(string(b[10:18]))
if err != nil {
return false
}
r.intArrData[0] = i0
r.intArrData[1] = i1
return true
}