-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathfwd.go
489 lines (409 loc) · 13.2 KB
/
fwd.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2015 Tony Bai.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package cmpp
import (
"encoding/binary"
"errors"
)
// Packet length const for cmpp fwd request and response packets.
const (
Cmpp2FwdReqPktMaxLen uint32 = 12 + 2379 //2277d, 0x957
Cmpp2FwdRspPktLen uint32 = 12 + 8 + 1 + 1 + 1 //23d, 0x17
Cmpp3FwdReqPktMaxLen uint32 = 12 + 2491 //2503d, 0x9c7
Cmpp3FwdRspPktLen uint32 = 12 + 8 + 1 + 1 + 4 //26d, 0x1a
)
// Errors for result in fwd resp.
var (
ErrnoFwdInvalidStruct uint8 = 1
ErrnoFwdInvalidCommandId uint8 = 2
ErrnoFwdInvalidSequence uint8 = 3
ErrnoFwdInvalidMsgLength uint8 = 4
ErrnoFwdInvalidFeeCode uint8 = 5
ErrnoFwdExceedMaxMsgLength uint8 = 6
ErrnoFwdInvalidServiceId uint8 = 7
ErrnoFwdNotPassFlowControl uint8 = 8
ErrnoFwdNoPrivilege uint8 = 9
FwdRspResultErrMap = map[uint8]error{
ErrnoFwdInvalidStruct: errFwdInvalidStruct,
ErrnoFwdInvalidCommandId: errFwdInvalidCommandId,
ErrnoFwdInvalidSequence: errFwdInvalidSequence,
ErrnoFwdInvalidMsgLength: errFwdInvalidMsgLength,
ErrnoFwdInvalidFeeCode: errFwdInvalidFeeCode,
ErrnoFwdExceedMaxMsgLength: errFwdExceedMaxMsgLength,
ErrnoFwdInvalidServiceId: errFwdInvalidServiceId,
ErrnoFwdNotPassFlowControl: errFwdNotPassFlowControl,
ErrnoFwdNoPrivilege: errFwdNoPrivilege,
}
errFwdInvalidStruct = errors.New("fwd response status: invalid protocol structure")
errFwdInvalidCommandId = errors.New("fwd response status: invalid command id")
errFwdInvalidSequence = errors.New("fwd response status: invalid message sequence")
errFwdInvalidMsgLength = errors.New("fwd response status: invalid message length")
errFwdInvalidFeeCode = errors.New("fwd response status: invalid fee code")
errFwdExceedMaxMsgLength = errors.New("fwd response status: exceed max message length")
errFwdInvalidServiceId = errors.New("fwd response status: invalid service id")
errFwdNotPassFlowControl = errors.New("fwd response status: not pass the flow control")
errFwdNoPrivilege = errors.New("fwd response status: msg has no fwd privilege")
)
type Cmpp2FwdReqPkt struct {
SourceId string
DestinationId string
NodesCount uint8
MsgFwdType uint8
MsgId uint64
PkTotal uint8
PkNumber uint8
RegisteredDelivery uint8
MsgLevel uint8
ServiceId string
FeeUserType uint8
FeeTerminalId string
TpPid uint8
TpUdhi uint8
MsgFmt uint8
MsgSrc string
FeeType string
FeeCode string
ValidTime string
AtTime string
SrcId string
DestUsrTl uint8
DestId []string
MsgLength uint8
MsgContent string
Reserve string
// session info
SeqId uint32
}
type Cmpp2FwdRspPkt struct {
MsgId uint64
PkTotal uint8
PkNumber uint8
Result uint8
// session info
SeqId uint32
}
type Cmpp3FwdReqPkt struct {
SourceId string
DestinationId string
NodesCount uint8
MsgFwdType uint8
MsgId uint64
PkTotal uint8
PkNumber uint8
RegisteredDelivery uint8
MsgLevel uint8
ServiceId string
FeeUserType uint8
FeeTerminalId string
FeeTerminalPseudo string
FeeTerminalUserType uint8
TpPid uint8
TpUdhi uint8
MsgFmt uint8
MsgSrc string
FeeType string
FeeCode string
ValidTime string
AtTime string
SrcId string
SrcPseudo string
SrcUserType uint8
SrcType uint8
DestUsrTl uint8
DestId []string
DestPseudo string
DestUserType uint8
MsgLength uint8
MsgContent string
LinkId string
// session info
SeqId uint32
}
type Cmpp3FwdRspPkt struct {
MsgId uint64
PkTotal uint8
PkNumber uint8
Result uint32
// session info
SeqId uint32
}
// Pack packs the Cmpp2FwdReqPkt to bytes stream for client side.
// Before calling Pack, you should initialize a Cmpp2FwdReqPkt variable
// with correct field value.
func (p *Cmpp2FwdReqPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen uint32 = CMPP_HEADER_LEN + 131 + uint32(p.DestUsrTl)*21 + 1 + uint32(p.MsgLength) + 8
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_FWD)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteFixedSizeString(p.SourceId, 6)
w.WriteFixedSizeString(p.DestinationId, 6)
w.WriteByte(p.NodesCount)
w.WriteByte(p.MsgFwdType)
w.WriteInt(binary.BigEndian, p.MsgId)
if p.PkTotal == 0 && p.PkNumber == 0 {
p.PkTotal, p.PkNumber = 1, 1
}
w.WriteByte(p.PkTotal)
w.WriteByte(p.PkNumber)
w.WriteByte(p.RegisteredDelivery)
w.WriteByte(p.MsgLevel)
w.WriteFixedSizeString(p.ServiceId, 10)
w.WriteByte(p.FeeUserType)
w.WriteFixedSizeString(p.FeeTerminalId, 21)
w.WriteByte(p.TpPid)
w.WriteByte(p.TpUdhi)
w.WriteByte(p.MsgFmt)
w.WriteFixedSizeString(p.MsgSrc, 6)
w.WriteFixedSizeString(p.FeeType, 2)
w.WriteFixedSizeString(p.FeeCode, 6)
w.WriteFixedSizeString(p.ValidTime, 17)
w.WriteFixedSizeString(p.AtTime, 17)
w.WriteFixedSizeString(p.SrcId, 21)
w.WriteByte(p.DestUsrTl)
for _, d := range p.DestId {
w.WriteFixedSizeString(d, 21)
}
w.WriteByte(p.MsgLength)
w.WriteString(p.MsgContent)
w.WriteFixedSizeString(p.Reserve, 8)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp2FwdReqPkt variable.
// After unpack, you will get all value of fields in Cmpp2FwdReqPkt struct.
func (p *Cmpp2FwdReqPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
sourceId := r.ReadCString(6)
p.SourceId = string(sourceId)
destinationId := r.ReadCString(6)
p.DestinationId = string(destinationId)
p.NodesCount = r.ReadByte()
p.MsgFwdType = r.ReadByte()
r.ReadInt(binary.BigEndian, &p.MsgId)
p.PkTotal = r.ReadByte()
p.PkNumber = r.ReadByte()
p.RegisteredDelivery = r.ReadByte()
p.MsgLevel = r.ReadByte()
serviceId := r.ReadCString(10)
p.ServiceId = string(serviceId)
p.FeeUserType = r.ReadByte()
feeTerminalId := r.ReadCString(21)
p.FeeTerminalId = string(feeTerminalId)
p.TpPid = r.ReadByte()
p.TpUdhi = r.ReadByte()
p.MsgFmt = r.ReadByte()
msgSrc := r.ReadCString(6)
p.MsgSrc = string(msgSrc)
feeType := make([]byte, 2)
r.ReadBytes(feeType)
p.FeeType = string(feeType)
feeCode := r.ReadCString(6)
p.FeeCode = string(feeCode)
validTime := r.ReadCString(17)
p.ValidTime = string(validTime)
atTime := r.ReadCString(17)
p.AtTime = string(atTime)
srcId := r.ReadCString(21)
p.SrcId = string(srcId)
p.DestUsrTl = r.ReadByte()
for i := 0; i < int(p.DestUsrTl); i++ {
destId := r.ReadCString(21)
p.DestId = append(p.DestId, string(destId))
}
p.MsgLength = r.ReadByte()
msgContent := make([]byte, p.MsgLength)
r.ReadBytes(msgContent)
p.MsgContent = string(msgContent)
reserve := r.ReadCString(8)
p.Reserve = string(reserve)
return r.Error()
}
// Pack packs the Cmpp2FwdRspPkt to bytes stream for server side.
// Before calling Pack, you should initialize a Cmpp2FwdRspPkt variable
// with correct field value.
func (p *Cmpp2FwdRspPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen = Cmpp2FwdRspPktLen
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_FWD_RESP)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteInt(binary.BigEndian, p.MsgId)
w.WriteByte(p.PkTotal)
w.WriteByte(p.PkNumber)
w.WriteByte(p.Result)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp2FwdRspPkt variable.
// After unpack, you will get all value of fields in Cmpp2FwdRspPkt struct.
func (p *Cmpp2FwdRspPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
r.ReadInt(binary.BigEndian, &p.MsgId)
p.PkTotal = r.ReadByte()
p.PkNumber = r.ReadByte()
p.Result = r.ReadByte()
return r.Error()
}
// Pack packs the Cmpp3FwdReqPkt to bytes stream for client side.
// Before calling Pack, you should initialize a Cmpp3FwdReqPkt variable
// with correct field value.
func (p *Cmpp3FwdReqPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen uint32 = CMPP_HEADER_LEN + 198 + uint32(p.DestUsrTl)*21 + 32 + 1 + 1 + uint32(p.MsgLength) + 20
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_FWD)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteFixedSizeString(p.SourceId, 6)
w.WriteFixedSizeString(p.DestinationId, 6)
w.WriteByte(p.NodesCount)
w.WriteByte(p.MsgFwdType)
w.WriteInt(binary.BigEndian, p.MsgId)
if p.PkTotal == 0 && p.PkNumber == 0 {
p.PkTotal, p.PkNumber = 1, 1
}
w.WriteByte(p.PkTotal)
w.WriteByte(p.PkNumber)
w.WriteByte(p.RegisteredDelivery)
w.WriteByte(p.MsgLevel)
w.WriteFixedSizeString(p.ServiceId, 10)
w.WriteByte(p.FeeUserType)
w.WriteFixedSizeString(p.FeeTerminalId, 21)
w.WriteFixedSizeString(p.FeeTerminalPseudo, 32)
w.WriteByte(p.FeeTerminalUserType)
w.WriteByte(p.TpPid)
w.WriteByte(p.TpUdhi)
w.WriteByte(p.MsgFmt)
w.WriteFixedSizeString(p.MsgSrc, 6)
w.WriteFixedSizeString(p.FeeType, 2)
w.WriteFixedSizeString(p.FeeCode, 6)
w.WriteFixedSizeString(p.ValidTime, 17)
w.WriteFixedSizeString(p.AtTime, 17)
w.WriteFixedSizeString(p.SrcId, 21)
w.WriteFixedSizeString(p.SrcPseudo, 32)
w.WriteByte(p.SrcUserType)
w.WriteByte(p.SrcType)
w.WriteByte(p.DestUsrTl)
for _, d := range p.DestId {
w.WriteFixedSizeString(d, 21)
}
w.WriteFixedSizeString(p.DestPseudo, 32)
w.WriteByte(p.DestUserType)
w.WriteByte(p.MsgLength)
w.WriteString(p.MsgContent)
w.WriteFixedSizeString(p.LinkId, 20)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp3FwdReqPkt variable.
// After unpack, you will get all value of fields in Cmpp3FwdReqPkt struct.
func (p *Cmpp3FwdReqPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
// Body
sourceId := r.ReadCString(6)
p.SourceId = string(sourceId)
destinationId := r.ReadCString(6)
p.DestinationId = string(destinationId)
p.NodesCount = r.ReadByte()
p.MsgFwdType = r.ReadByte()
r.ReadInt(binary.BigEndian, &p.MsgId)
p.PkTotal = r.ReadByte()
p.PkNumber = r.ReadByte()
p.RegisteredDelivery = r.ReadByte()
p.MsgLevel = r.ReadByte()
serviceId := r.ReadCString(10)
p.ServiceId = string(serviceId)
p.FeeUserType = r.ReadByte()
feeTerminalId := r.ReadCString(21)
p.FeeTerminalId = string(feeTerminalId)
feeTerminalPseudo := r.ReadCString(32)
p.FeeTerminalPseudo = string(feeTerminalPseudo)
p.FeeTerminalUserType = r.ReadByte()
p.TpPid = r.ReadByte()
p.TpUdhi = r.ReadByte()
p.MsgFmt = r.ReadByte()
msgSrc := r.ReadCString(6)
p.MsgSrc = string(msgSrc)
feeType := make([]byte, 2)
r.ReadBytes(feeType)
p.FeeType = string(feeType)
feeCode := r.ReadCString(6)
p.FeeCode = string(feeCode)
validTime := r.ReadCString(17)
p.ValidTime = string(validTime)
atTime := r.ReadCString(17)
p.AtTime = string(atTime)
srcId := r.ReadCString(21)
p.SrcId = string(srcId)
srcPseudo := r.ReadCString(32)
p.SrcPseudo = string(srcPseudo)
p.SrcUserType = r.ReadByte()
p.SrcType = r.ReadByte()
p.DestUsrTl = r.ReadByte()
for i := 0; i < int(p.DestUsrTl); i++ {
destId := r.ReadCString(21)
p.DestId = append(p.DestId, string(destId))
}
destPseudo := r.ReadCString(32)
p.DestPseudo = string(destPseudo)
p.DestUserType = r.ReadByte()
p.MsgLength = r.ReadByte()
msgContent := make([]byte, p.MsgLength)
r.ReadBytes(msgContent)
p.MsgContent = string(msgContent)
linkId := r.ReadCString(20)
p.LinkId = string(linkId)
return r.Error()
}
// Pack packs the Cmpp3FwdRspPkt to bytes stream for server side.
// Before calling Pack, you should initialize a Cmpp3FwdRspPkt variable
// with correct field value.
func (p *Cmpp3FwdRspPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen = Cmpp3FwdRspPktLen
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_FWD_RESP)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteInt(binary.BigEndian, p.MsgId)
w.WriteByte(p.PkTotal)
w.WriteByte(p.PkNumber)
w.WriteInt(binary.BigEndian, p.Result)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp3FwdRspPkt variable.
// After unpack, you will get all value of fields in Cmpp3FwdRspPkt struct.
func (p *Cmpp3FwdRspPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
r.ReadInt(binary.BigEndian, &p.MsgId)
p.PkTotal = r.ReadByte()
p.PkNumber = r.ReadByte()
r.ReadInt(binary.BigEndian, &p.Result)
return r.Error()
}