-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmsg.go
359 lines (331 loc) · 7.32 KB
/
msg.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
package goStrongswanVici
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"strconv"
)
type segmentType byte
const (
stCMD_REQUEST segmentType = 0
stCMD_RESPONSE = 1
stCMD_UNKNOWN = 2
stEVENT_REGISTER = 3
stEVENT_UNREGISTER = 4
stEVENT_CONFIRM = 5
stEVENT_UNKNOWN = 6
stEVENT = 7
)
func (t segmentType) hasName() bool {
switch t {
case stCMD_REQUEST, stEVENT_REGISTER, stEVENT_UNREGISTER, stEVENT:
return true
}
return false
}
func (t segmentType) isValid() bool {
switch t {
case stCMD_REQUEST, stCMD_RESPONSE, stCMD_UNKNOWN, stEVENT_REGISTER,
stEVENT_UNREGISTER, stEVENT_CONFIRM, stEVENT_UNKNOWN, stEVENT:
return true
}
return false
}
func (t segmentType) hasMsg() bool {
switch t {
case stCMD_REQUEST, stCMD_RESPONSE, stEVENT:
return true
}
return false
}
type elementType byte
const (
etSECTION_START elementType = 1
etSECTION_END = 2
etKEY_VALUE = 3
etLIST_START = 4
etLIST_ITEM = 5
etLIST_END = 6
)
type segment struct {
typ segmentType
name string
msg map[string]interface{}
}
//msg 在内部以下列3种类型表示(降低复杂度)
// string
// map[string]interface{}
// []string
func writeSegment(w io.Writer, msg segment) (err error) {
if !msg.typ.isValid() {
return fmt.Errorf("[writeSegment] msg.typ %d not defined", msg.typ)
}
buf := &bytes.Buffer{}
buf.WriteByte(byte(msg.typ))
//name
if msg.typ.hasName() {
err = writeString1(buf, msg.name)
if err != nil {
fmt.Printf("error returned from writeString1i \n")
return
}
}
if msg.typ.hasMsg() {
err = writeMap(buf, msg.msg)
if err != nil {
fmt.Printf("error retruned from writeMap \n")
return
}
}
//写长度
err = binary.Write(w, binary.BigEndian, uint32(buf.Len()))
if err != nil {
fmt.Printf("[writeSegment] error writing to binary \n")
return
}
_, err = buf.WriteTo(w)
if err != nil {
fmt.Printf("[writeSegment] error writing to buffer \n")
return
}
return nil
}
func readSegment(inR io.Reader) (msg segment, err error) {
//长度
var length uint32
err = binary.Read(inR, binary.BigEndian, &length)
if err != nil {
return
}
r := bufio.NewReader(&io.LimitedReader{
R: inR,
N: int64(length),
})
//类型
c, err := r.ReadByte()
if err != nil {
return
}
msg.typ = segmentType(c)
if !msg.typ.isValid() {
return msg, fmt.Errorf("[readSegment] msg.typ %d not defined", msg.typ)
}
if msg.typ.hasName() {
msg.name, err = readString1(r)
if err != nil {
return
}
}
if msg.typ.hasMsg() {
msg.msg, err = readMap(r, true)
if err != nil {
return
}
}
return
}
//一个字节长度的字符串
func writeString1(w *bytes.Buffer, s string) (err error) {
length := len(s)
if length > 255 {
return fmt.Errorf("[writeString1] length>255")
}
w.WriteByte(byte(length))
w.WriteString(s)
return
}
func readString1(r *bufio.Reader) (s string, err error) {
length, err := r.ReadByte()
if err != nil {
return
}
buf := make([]byte, length)
_, err = io.ReadFull(r, buf)
if err != nil {
return
}
return string(buf), nil
}
//两个字节长度的字符串
func writeString2(w *bytes.Buffer, s string) (err error) {
length := len(s)
if length > 65535 {
return fmt.Errorf("[writeString2] length>65535")
}
binary.Write(w, binary.BigEndian, uint16(length))
w.WriteString(s)
return
}
func readString2(r io.Reader) (s string, err error) {
var length uint16
err = binary.Read(r, binary.BigEndian, &length)
if err != nil {
return
}
buf := make([]byte, length)
_, err = io.ReadFull(r, buf)
if err != nil {
return
}
return string(buf), nil
}
func writeKeyMap(w *bytes.Buffer, name string, msg map[string]interface{}) (err error) {
w.WriteByte(byte(etSECTION_START))
err = writeString1(w, name)
if err != nil {
return
}
writeMap(w, msg)
w.WriteByte(byte(etSECTION_END))
return nil
}
func writeKeyList(w *bytes.Buffer, name string, msg []string) (err error) {
w.WriteByte(byte(etLIST_START))
err = writeString1(w, name)
if err != nil {
return
}
for _, s := range msg {
w.WriteByte(byte(etLIST_ITEM))
err = writeString2(w, s)
if err != nil {
return
}
}
w.WriteByte(byte(etLIST_END))
return nil
}
func writeKeyString(w *bytes.Buffer, name string, msg string) (err error) {
w.WriteByte(byte(etKEY_VALUE))
err = writeString1(w, name)
if err != nil {
return
}
err = writeString2(w, msg)
return
}
func writeMap(w *bytes.Buffer, msg map[string]interface{}) (err error) {
for k, v := range msg {
switch t := v.(type) {
case map[string]interface{}:
writeKeyMap(w, k, t)
case []string:
writeKeyList(w, k, t)
case string:
writeKeyString(w, k, t)
case []interface{}:
str := make([]string, len(t))
for i := range t {
str[i] = t[i].(string)
}
writeKeyList(w, k, str)
default:
return fmt.Errorf("[writeMap] can not write type %T right now", msg)
}
}
return nil
}
//SECTION_START has been read already.
func readKeyMap(r *bufio.Reader) (key string, msg map[string]interface{}, err error) {
key, err = readString1(r)
if err != nil {
return
}
msg, err = readMap(r, false)
return
}
//LIST_START has been read already.
func readKeyList(r *bufio.Reader) (key string, msg []string, err error) {
key, err = readString1(r)
if err != nil {
return
}
msg = []string{}
for {
var c byte
c, err = r.ReadByte()
if err != nil {
return
}
switch elementType(c) {
case etLIST_ITEM:
value, err := readString2(r)
if err != nil {
return "", nil, err
}
msg = append(msg, value)
case etLIST_END: //end of outer list
return key, msg, nil
default:
return "", nil, fmt.Errorf("[readKeyList] protocol error 2")
}
}
return
}
//KEY_VALUE has been read already.
func readKeyString(r *bufio.Reader) (key string, msg string, err error) {
key, err = readString1(r)
if err != nil {
return
}
msg, err = readString2(r)
if err != nil {
return
}
return
}
// Since the original key chosen can have duplicates,
// this function is used to map the original key to a new one
// to make them unique.
func getNewKeyToHandleDuplicates(key string, msg map[string]interface{}) string {
if _, ok := msg[key]; !ok {
return key
}
for i := 0; ; i++ {
newKey := key + "##" + strconv.Itoa(i)
if _, ok := msg[newKey]; !ok {
return newKey
}
}
}
//SECTION_START has been read already.
func readMap(r *bufio.Reader, isRoot bool) (msg map[string]interface{}, err error) {
msg = map[string]interface{}{}
for {
c, err := r.ReadByte()
if err == io.EOF && isRoot { //may be root section
return msg, nil
}
if err != nil {
return nil, err
}
switch elementType(c) {
case etSECTION_START:
key, value, err := readKeyMap(r)
if err != nil {
return nil, err
}
msg[getNewKeyToHandleDuplicates(key, msg)] = value
case etLIST_START:
key, value, err := readKeyList(r)
if err != nil {
return nil, err
}
msg[getNewKeyToHandleDuplicates(key, msg)] = value
case etKEY_VALUE:
key, value, err := readKeyString(r)
if err != nil {
return nil, err
}
msg[getNewKeyToHandleDuplicates(key, msg)] = value
case etSECTION_END: //end of outer section
return msg, nil
default:
panic(fmt.Errorf("[readMap] protocol error 1, %d %#v", c, msg))
//return nil, fmt.Errorf("[readMap] protocol error 1, %d",c)
}
}
return
}