-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_pkg_simple.go
219 lines (182 loc) · 4.3 KB
/
io_pkg_simple.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
package io
import (
"bufio"
"fmt"
"github.com/injoyai/base/g"
"github.com/injoyai/conv"
"io"
)
/*
简易封装包
帧头 1字节 0x68
长度 2字节
控制 1字节
数据 n字节
校验 1字节
控制码:
.=======================================================================================.
|bit7 |bit6 |bit5~0 |
|---------------------------------------------------------------------------------------|
|数据方向0请求,1响应 |响应是否有错误 |1读取,2写入,3订阅 |
^=======================================================================================^
数据域:
数据类型 1字节
长度 1字节
数据 n字节
...
*/
type SimpleControl struct {
IsResponse bool
IsErr bool
Type uint8
}
func (this SimpleControl) Byte() uint8 {
b := this.Type
if this.IsResponse {
b |= 0x80
}
if this.IsErr {
b |= 0x40
}
return b
}
type Simple struct {
Control SimpleControl //控制码,基本信息,方向,错误等 类型,1读,2写,3订阅,4通知
MsgID uint8 //消息序号
Data SimpleData //数据
}
func (this *Simple) Resp(data SimpleData, err ...error) *Simple {
if data == nil {
data = SimpleData{}
}
this.Control.IsResponse = true
if len(err) > 0 && err[0] != nil {
this.Control.IsErr = true
data[FliedError] = conv.Bytes(err[0])
}
this.Data = data
return this
}
func (this *Simple) Bytes() g.Bytes {
bs := []byte{0x68}
data := this.Data.Bytes()
length := uint16(len(data) + 3) // 1(报文头)+2(长度)
bs = append(bs, conv.Bytes(length)...) //后续数据长度
bs = append(bs, this.Control.Byte()) //控制码
bs = append(bs, this.MsgID) //消息id
bs = append(bs, data...) //数据
bs = append(bs, this.sum(bs)) //校验
return bs
}
func (this *Simple) sum(bs []byte) byte {
var sum byte
for _, v := range bs {
sum += v
}
return sum
}
// SimpleData key和value的长度不能超过255
type SimpleData map[string][]byte
func (this SimpleData) Bytes() g.Bytes {
data := []byte(nil)
for k, v := range this {
data = append(data, byte(len(k)))
data = append(data, k...)
data = append(data, byte(len(v)))
data = append(data, v...)
}
return data
}
func (this SimpleData) SMap() map[string]string {
data := map[string]string{}
for k, v := range this {
data[k] = string(v)
}
return data
}
func NewSimplePing() *Simple {
return &Simple{
Control: SimpleControl{
Type: OprPing,
},
}
}
func NewSimple(control SimpleControl, data SimpleData, msgID ...uint8) *Simple {
return &Simple{
Control: control,
Data: data,
MsgID: conv.GetDefaultUint8(0, msgID...),
}
}
func DecodeSimple(bs []byte) (*Simple, error) {
if len(bs) < 7 {
return nil, fmt.Errorf("数据长度小于(%d)", 7)
}
if bs[0] != 0x68 {
return nil, fmt.Errorf("帧头错误,预期(0x68),得到(%x)", bs[0])
}
length := conv.Int(bs[1:3])
if len(bs) != length+3 {
return nil, fmt.Errorf("数据总长度错误,预期(%d),得到(%d)", length+3, len(bs))
}
p := &Simple{
Control: SimpleControl{
IsResponse: bs[3]&0x80 == 0x80,
IsErr: bs[3]&0x40 == 0x40,
Type: bs[3] & 0x3F,
},
MsgID: bs[4],
Data: map[string][]byte{},
}
sum := p.sum(bs[:len(bs)-1])
if sum != bs[len(bs)-1] {
return nil, fmt.Errorf("数据校验错误,预期(%x),得到(%x)", sum, bs[len(bs)-1])
}
data := bs[5 : len(bs)-1]
for len(data) > 0 {
keyLen := data[0]
if len(data) < int(1+keyLen) {
break
}
k := string(data[1 : 1+keyLen])
valLen := data[1+keyLen]
if len(data) < int(1+keyLen+1+valLen) {
break
}
v := data[1+keyLen+1 : 1+keyLen+1+valLen]
p.Data[k] = v
data = data[1+keyLen+1+valLen:]
}
return p, nil
}
func WriteWithSimple(bs []byte) ([]byte, error) {
return bs, nil
}
func ReadWithSimple(r *bufio.Reader) ([]byte, error) {
for {
b, err := r.ReadByte()
if err != nil {
return nil, err
}
if b == 0x68 {
result := []byte{0x68}
buf := make([]byte, 2)
_, err = io.ReadAtLeast(r, buf, 2)
if err != nil {
return nil, err
}
result = append(result, buf...)
length := conv.Int(buf)
buf = make([]byte, length)
_, err = io.ReadAtLeast(r, buf, length)
if err != nil {
return nil, err
}
result = append(result, buf...)
//校验
if _, err = DecodeSimple(result); err == nil {
return result, nil
}
}
}
}