-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_pkg.go
364 lines (310 loc) · 8.5 KB
/
io_pkg.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package io
import (
"bufio"
"compress/gzip"
"fmt"
"github.com/injoyai/base/bytes"
"github.com/injoyai/conv"
"github.com/injoyai/logs"
"hash/crc32"
"io"
"time"
)
/*
通用封装包
包构成(大端):
.===================================.
|构成 |字节 |类型 |说明 |
|-----------------------------------|
|帧头 |2字节 |Byte |固定0x8888 |
|-----------------------------------|
|帧长 |4字节 |HEX |总字节长度 |
|-----------------------------------|
|帧类型 |2字节 |BIN |详见帧类型 |
|-----------------------------------|
|消息号 |1字节 |Byte |消息id |
|-----------------------------------|
|内容 |可变 |Byte |数据内容 |
|-----------------------------------|
|校验和 |4字节 |Byte |crc IEEE |
|-----------------------------------|
|帧尾 |2字节 |Byte |固定0x8989 |
^===================================^
包类型:
.=======================================================================================================.
|bit15 |bit14 |bit13~11 |bit10 |bit9 |bit8 |
|-------------------------------------------------------------------------------------------------------|
|数据方向0请求,1响应 |预留 |压缩方式,0无,1gzip |预留 |
^=======================================================================================================^
|bit7 |功能码 |
|-------------------------------------------------------------------------------------------------------|
|数据的读写0读/订阅/接收,1写/发布/发送 | |
^=======================================================================================================^
*/
var (
pkgStart = []byte{0x88, 0x88} //帧头
pkgEnd = []byte{0x89, 0x89} //帧尾
)
const (
pkgBaseLength = 15
ControlCall uint8 = 0x00
ControlBack uint8 = 0x80
ControlGzip uint8 = 0x10
)
const (
// 内置功能码,待定
FunctionRead uint8 = 0x00
FunctionWrite uint8 = 0x80
FunctionCustom uint8 = 0x0 //自定义
FunctionPing uint8 = 0x1 //测试连接,无数据
FunctionTime uint8 = 0x2 //时间(时间戳),同步时间
FunctionSubscribe uint8 = 0x3 //订阅
FunctionIMEI uint8 = 0x4 //imei
FunctionICCID uint8 = 0x5 //iccid
FunctionIMSI uint8 = 0x6 //imsi
FunctionReload uint8 = 0x7 //重新加载
FunctionReboot uint8 = 0x8 //重启设备
FunctionLinkAddress uint8 = 0x9 //新连接地址
FunctionSlave uint8 = 0xA //设置从站地址,主节点开始,后续一次增加长度,例如主节点是 "1" 分配给子节点就是 "1.1","1.2","1.13"
)
func NewPkgPing() []byte {
//00000001
return (&Pkg{Control: ControlCall, Function: FunctionPing}).Bytes()
}
func NewPkgPong() []byte {
//10000001
return (&Pkg{Control: ControlBack, Function: FunctionPing}).Bytes()
}
func NewPkg(msgID uint8, data []byte) *Pkg {
return NewCallPkg(msgID, data)
}
func NewCallPkg(msgID uint8, data []byte) *Pkg {
return &Pkg{
Control: ControlCall,
Function: FunctionCustom,
MsgID: msgID,
Data: data,
}
}
func NewBackPkg(msgID uint8, data []byte) *Pkg {
return NewCallPkg(msgID, data).Resp(data)
}
type Pkg struct {
Control uint8 //控制码
Function uint8 //功能码
MsgID uint8 //消息id
Data []byte //数据内容
}
// SetCompress 设置压缩方式,1是gzip,其他不压缩
func (this *Pkg) SetCompress(n uint8) *Pkg {
this.Control &= 0xCF
this.Control |= n
return this
}
func (this *Pkg) String() string {
return this.Bytes().HEX()
}
func (this *Pkg) encodeData() []byte {
data := this.Data
switch this.Control & 0x30 {
case ControlGzip:
// Gzip 压缩字节
buf := bytes.NewBuffer(nil)
gzipWriter := gzip.NewWriter(buf)
gzipWriter.Write(data)
gzipWriter.Close()
data = buf.Bytes()
default:
}
return data
}
func (this *Pkg) decodeData() error {
switch this.Control & 0x30 {
case ControlGzip:
// Gzip 解压字节
reader := bytes.NewReader(this.Data)
gzipReader, err := gzip.NewReader(reader)
if err != nil {
return err
}
defer gzipReader.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(gzipReader)
if err != nil {
logs.Err(err)
return err
}
this.Data = buf.Bytes()
default:
}
return nil
}
func (this *Pkg) Bytes() bytes.Entity {
data := []byte(nil)
data = append(data, pkgStart...)
dataBytes := this.encodeData()
length := uint32(len(dataBytes) + pkgBaseLength)
data = append(data, conv.Bytes(length)...)
data = append(data, this.Control, this.Function)
data = append(data, this.MsgID)
data = append(data, dataBytes...)
data = append(data, conv.Bytes(crc32.ChecksumIEEE(data))...)
data = append(data, pkgEnd...)
return data
}
// Resp 生成响应包
func (this *Pkg) Resp(bs []byte) *Pkg {
this.Control |= ControlBack
this.Data = bs
return this
}
// IsCall 是否请求数据
func (this *Pkg) IsCall() bool {
return this.Control&0x80 == 0
}
// IsBack 是否是响应数据
func (this *Pkg) IsBack() bool {
return this.Control&0x80 == 0x80
}
// IsPing 是否是ping,需要响应pong
func (this *Pkg) IsPing() bool {
return this.IsCall() && this.Function&0x7F == FunctionPing
}
// IsPong 是否是pong,不需要处理
func (this *Pkg) IsPong() bool {
return this.IsBack() && this.Function&0x7F == FunctionPing
}
func (this *Pkg) IsRead() bool {
return this.Function&0x80 == 0
}
func (this *Pkg) IsWrite() bool {
return this.Function&0x80 == 0x80
}
func (this *Pkg) GetFunction() uint8 {
return this.Function & 0x7F
}
func (this *Pkg) ReadWriteTag(c *Client, key string) error {
if this.IsCall() {
if this.IsRead() {
if _, err := c.Write(this.Resp(conv.Bytes(c.Tag().GetString(key))).Bytes()); err != nil {
return err
}
}
if this.IsWrite() {
c.Tag().Set(key, string(this.Data))
if _, err := c.Write(this.Resp(nil).Bytes()); err != nil {
return err
}
}
}
return nil
}
// DecodePkg 按自定义的包解析
func DecodePkg(bs []byte) (*Pkg, error) {
//校验基础数据长度
if len(bs) <= pkgBaseLength {
return nil, fmt.Errorf("数据长度小于(%d)", pkgBaseLength)
}
//校验帧头
if bs[0] != pkgStart[0] && bs[1] != pkgStart[1] {
return nil, fmt.Errorf("帧头错误,预期(%x),得到(%x)", pkgStart, bs[:2])
}
//获取总数据长度
length := conv.Int(bs[2:6])
//校验总长度
if len(bs) != length {
return nil, fmt.Errorf("数据总长度错误,预期(%d),得到(%d)", length, len(bs))
}
//校验crc32
if crc1, crc2 := crc32.ChecksumIEEE(bs[:length-6]), conv.Uint32(bs[length-6:length-2]); crc1 != crc2 {
return nil, fmt.Errorf("数据CRC校验错误,预期(%x),得到(%x)", crc1, crc2)
}
//校验帧尾
if bs[length-2] != pkgEnd[0] && bs[length-1] != bs[1] {
return nil, fmt.Errorf("帧尾错误,预期(%x),得到(%x)", pkgEnd, bs[length-2:])
}
p := &Pkg{
Control: bs[6],
Function: bs[7],
MsgID: bs[8],
Data: bs[9 : length-6],
}
return p, p.decodeData()
}
func DealWithPkg(c *Client, bs Message) (*Pkg, error) {
p, err := DecodePkg(bs)
if err != nil {
return nil, err
}
switch p.GetFunction() {
case FunctionPing:
if p.IsCall() {
if _, err := c.Write(p.Resp(nil).Bytes()); err != nil {
return nil, err
}
}
case FunctionTime:
if p.IsCall() && p.IsRead() {
now := conv.Bytes(time.Now().UnixMilli())
if _, err := c.Write(p.Resp(now).Bytes()); err != nil {
return nil, err
}
}
case FunctionIMEI:
if err := p.ReadWriteTag(c, "imei"); err != nil {
return nil, err
}
case FunctionICCID:
if err := p.ReadWriteTag(c, "iccid"); err != nil {
return nil, err
}
case FunctionIMSI:
if err := p.ReadWriteTag(c, "imsi"); err != nil {
return nil, err
}
case FunctionSlave:
if err := p.ReadWriteTag(c, "salve"); err != nil {
return nil, err
}
}
return p, nil
}
func WriteWithPkg(req []byte) ([]byte, error) {
return NewPkg(0, req).Bytes(), nil
}
func ReadWithPkg(buf *bufio.Reader) (result []byte, err error) {
var bs []byte
for {
bs = make([]byte, 2)
n, err := buf.Read(bs)
if err != nil {
return result, err
}
if n == 2 && bs[0] == pkgStart[0] && bs[1] == pkgStart[1] {
//帧头
result = append(result, bs...)
bs = make([]byte, 4)
n, err = buf.Read(bs)
if err != nil {
return result, err
}
if n == 4 {
//长度
length := conv.Int(bs)
if length > pkgBaseLength {
result = append(result, bs...)
length -= 6
bs = make([]byte, length)
_, err = io.ReadAtLeast(buf, bs, length)
result = append(result, bs...)
//return result, nil
p, err := DecodePkg(result)
if err != nil {
return nil, err
}
return p.Data, nil
}
}
}
}
}