-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathdeliver.go
314 lines (251 loc) · 8.55 KB
/
deliver.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
// 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 deliver request and response packets.
const (
Cmpp2DeliverReqPktMaxLen uint32 = 12 + 233 //245d, 0xf5
Cmpp2DeliverRspPktLen uint32 = 12 + 8 + 1 //21d, 0x15
Cmpp3DeliverReqPktMaxLen uint32 = 12 + 257 //269d, 0x10d
Cmpp3DeliverRspPktLen uint32 = 12 + 8 + 4 //24d, 0x18
)
// Errors for result in deliver resp.
var (
ErrnoDeliverInvalidStruct uint8 = 1
ErrnoDeliverInvalidCommandId uint8 = 2
ErrnoDeliverInvalidSequence uint8 = 3
ErrnoDeliverInvalidMsgLength uint8 = 4
ErrnoDeliverInvalidFeeCode uint8 = 5
ErrnoDeliverExceedMaxMsgLength uint8 = 6
ErrnoDeliverInvalidServiceId uint8 = 7
ErrnoDeliverNotPassFlowControl uint8 = 8
ErrnoDeliverOtherError uint8 = 9
DeliverRspResultErrMap = map[uint8]error{
ErrnoDeliverInvalidStruct: errDeliverInvalidStruct,
ErrnoDeliverInvalidCommandId: errDeliverInvalidCommandId,
ErrnoDeliverInvalidSequence: errDeliverInvalidSequence,
ErrnoDeliverInvalidMsgLength: errDeliverInvalidMsgLength,
ErrnoDeliverInvalidFeeCode: errDeliverInvalidFeeCode,
ErrnoDeliverExceedMaxMsgLength: errDeliverExceedMaxMsgLength,
ErrnoDeliverInvalidServiceId: errDeliverInvalidServiceId,
ErrnoDeliverNotPassFlowControl: errDeliverNotPassFlowControl,
ErrnoDeliverOtherError: errDeliverOtherError,
}
errDeliverInvalidStruct = errors.New("deliver response status: invalid protocol structure")
errDeliverInvalidCommandId = errors.New("deliver response status: invalid command id")
errDeliverInvalidSequence = errors.New("deliver response status: invalid message sequence")
errDeliverInvalidMsgLength = errors.New("deliver response status: invalid message length")
errDeliverInvalidFeeCode = errors.New("deliver response status: invalid fee code")
errDeliverExceedMaxMsgLength = errors.New("deliver response status: exceed max message length")
errDeliverInvalidServiceId = errors.New("deliver response status: invalid service id")
errDeliverNotPassFlowControl = errors.New("deliver response status: not pass the flow control")
errDeliverOtherError = errors.New("deliver response status: other error")
)
type Cmpp2DeliverReqPkt struct {
MsgId uint64
DestId string
ServiceId string
TpPid uint8
TpUdhi uint8
MsgFmt uint8
SrcTerminalId string
RegisterDelivery uint8
MsgLength uint8
MsgContent string
Reserve string
//session info
SeqId uint32
}
type Cmpp2DeliverRspPkt struct {
MsgId uint64
Result uint8
//session info
SeqId uint32
}
type Cmpp3DeliverReqPkt struct {
MsgId uint64
DestId string
ServiceId string
TpPid uint8
TpUdhi uint8
MsgFmt uint8
SrcTerminalId string
SrcTerminalType uint8
RegisterDelivery uint8
MsgLength uint8
MsgContent string
LinkId string
//session info
SeqId uint32
}
type Cmpp3DeliverRspPkt struct {
MsgId uint64
Result uint32
//session info
SeqId uint32
}
// Pack packs the Cmpp2DeliverReqPkt to bytes stream for client side.
func (p *Cmpp2DeliverReqPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen uint32 = CMPP_HEADER_LEN + 65 + uint32(p.MsgLength) + 8
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_DELIVER)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteInt(binary.BigEndian, p.MsgId)
w.WriteFixedSizeString(p.DestId, 21)
w.WriteFixedSizeString(p.ServiceId, 10)
w.WriteByte(p.TpPid)
w.WriteByte(p.TpUdhi)
w.WriteByte(p.MsgFmt)
w.WriteFixedSizeString(p.SrcTerminalId, 21)
w.WriteByte(p.RegisterDelivery)
w.WriteByte(p.MsgLength)
w.WriteString(p.MsgContent)
w.WriteFixedSizeString(p.Reserve, 8)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp2DeliverReqPkt variable.
// After unpack, you will get all value of fields in
// Cmpp2DeliverReqPkt struct.
func (p *Cmpp2DeliverReqPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
// Body
r.ReadInt(binary.BigEndian, &p.MsgId)
destId := r.ReadCString(21)
p.DestId = string(destId)
serviceId := r.ReadCString(10)
p.ServiceId = string(serviceId)
p.TpPid = r.ReadByte()
p.TpUdhi = r.ReadByte()
p.MsgFmt = r.ReadByte()
srcTerminalId := r.ReadCString(21)
p.SrcTerminalId = string(srcTerminalId)
p.RegisterDelivery = r.ReadByte()
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 Cmpp2DeliverRspPkt to bytes stream for client side.
func (p *Cmpp2DeliverRspPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen uint32 = Cmpp2DeliverRspPktLen
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_DELIVER_RESP)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteInt(binary.BigEndian, p.MsgId)
w.WriteByte(p.Result)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp2DeliverRspPkt variable.
// After unpack, you will get all value of fields in
// Cmpp2DeliverRspPkt struct.
func (p *Cmpp2DeliverRspPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
r.ReadInt(binary.BigEndian, &p.MsgId)
p.Result = r.ReadByte()
return r.Error()
}
// Pack packs the Cmpp3DeliverReqPkt to bytes stream for client side.
func (p *Cmpp3DeliverReqPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen uint32 = CMPP_HEADER_LEN + 77 + uint32(p.MsgLength) + 20
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_DELIVER)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteInt(binary.BigEndian, p.MsgId)
w.WriteFixedSizeString(p.DestId, 21)
w.WriteFixedSizeString(p.ServiceId, 10)
w.WriteByte(p.TpPid)
w.WriteByte(p.TpUdhi)
w.WriteByte(p.MsgFmt)
w.WriteFixedSizeString(p.SrcTerminalId, 32)
w.WriteByte(p.SrcTerminalType)
w.WriteByte(p.RegisterDelivery)
w.WriteByte(p.MsgLength)
w.WriteString(p.MsgContent)
w.WriteFixedSizeString(p.LinkId, 20)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp3DeliverReqPkt variable.
// After unpack, you will get all value of fields in
// Cmpp3DeliverReqPkt struct.
func (p *Cmpp3DeliverReqPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
// Body
r.ReadInt(binary.BigEndian, &p.MsgId)
destId := r.ReadCString(21)
p.DestId = string(destId)
serviceId := r.ReadCString(10)
p.ServiceId = string(serviceId)
p.TpPid = r.ReadByte()
p.TpUdhi = r.ReadByte()
p.MsgFmt = r.ReadByte()
srcTerminalId := r.ReadCString(32)
p.SrcTerminalId = string(srcTerminalId)
p.SrcTerminalType = r.ReadByte()
p.RegisterDelivery = 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 Cmpp3DeliverRspPkt to bytes stream for client side.
func (p *Cmpp3DeliverRspPkt) Pack(seqId uint32) ([]byte, error) {
var pktLen uint32 = Cmpp3DeliverRspPktLen
var w = newPacketWriter(pktLen)
// Pack header
w.WriteInt(binary.BigEndian, pktLen)
w.WriteInt(binary.BigEndian, CMPP_DELIVER_RESP)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// Pack Body
w.WriteInt(binary.BigEndian, p.MsgId)
w.WriteInt(binary.BigEndian, p.Result)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp3DeliverRspPkt variable.
// After unpack, you will get all value of fields in
// Cmpp3DeliverRspPkt struct.
func (p *Cmpp3DeliverRspPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
r.ReadInt(binary.BigEndian, &p.MsgId)
r.ReadInt(binary.BigEndian, &p.Result)
return r.Error()
}