/
header.go
118 lines (110 loc) · 5.78 KB
/
header.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
package smgp
import (
"encoding/binary"
"fmt"
)
type MessageHeader struct {
PacketLength uint32
RequestId uint32
SequenceId uint32
}
func (header *MessageHeader) Encode() []byte {
if header.PacketLength < HeadLength {
header.PacketLength = HeadLength
}
frame := make([]byte, header.PacketLength)
binary.BigEndian.PutUint32(frame[0:4], header.PacketLength)
binary.BigEndian.PutUint32(frame[4:8], header.RequestId)
binary.BigEndian.PutUint32(frame[8:12], header.SequenceId)
return frame
}
func (header *MessageHeader) Decode(frame []byte) error {
if len(frame) < HeadLength {
return ErrorPacket
}
header.PacketLength = binary.BigEndian.Uint32(frame[0:4])
header.RequestId = binary.BigEndian.Uint32(frame[4:8])
header.SequenceId = binary.BigEndian.Uint32(frame[8:12])
return nil
}
func (header *MessageHeader) String() string {
return fmt.Sprintf("{ PacketLength: %d, RequestId: %s, SequenceId: %d }", header.PacketLength, CommandMap[header.RequestId], header.SequenceId)
}
const (
HeadLength = 12 // 报文头长度
CmdLogin = uint32(0x00000001) // 客户端登录
CmdLoginResp = uint32(0x80000001) // 客户端登录应答
CmdSubmit = uint32(0x00000002) // 提交短消息
CmdSubmitResp = uint32(0x80000002) // 提交短消息应答
CmdDeliver = uint32(0x00000003) // 下发短消息
CmdDeliverResp = uint32(0x80000003) // 下发短消息应答
CmdActiveTest = uint32(0x00000004) // 链路检测
CmdActiveTestResp = uint32(0x80000004) // 链路检测应答
CmdExit = uint32(0x00000006) // 退出请求
CmdExitResp = uint32(0x80000006) // 退出应答
// Forward = uint32(0x00000005) // 短消息前转
// Forward_Resp = uint32(0x80000005) // 短消息前转应答
// Query = uint32(0x00000007) // SP统计查询
// Query_Resp = uint32(0x80000007) // SP统计查询应答
// Query_TE_Route = uint32(0x00000008) // 查询TE路由
// Query_TE_Route_Resp = uint32(0x80000008) // 查询TE路由应答
// Query_SP_Route = uint32(0x00000009) // 查询SP路由
// Query_SP_Route_Resp = uint32(0x80000009) // 查询SP路由应答
// Payment_Request = uint32(0x0000000A) // 扣款请求(用于预付费系统,参见增值业务计费方案)
// Payment_Request_Resp = uint32(0x8000000A) // 扣款请求响应(用于预付费系统,参见增值业务计费方案,下同)
// Payment_Affirm = uint32(0x0000000B) // 扣款确认(用于预付费系统,参见增值业务计费方案)
// Payment_Affirm_Resp = uint32(0x8000000B) // 扣款确认响应(用于预付费系统,参见增值业务计费方案)
// Query_UserState = uint32(0x0000000C) // 查询用户状态(用于预付费系统,参见增值业务计费方案)
// Query_UserState_Resp = uint32(0x8000000C) // 查询用户状态响应(用于预付费系统,参见增值业务计费方案)
// Get_All_TE_Route = uint32(0x0000000D) // 获取所有终端路由
// Get_All_TE_Route_Resp = uint32(0x8000000D) // 获取所有终端路由应答
// Get_All_SP_Route = uint32(0x0000000E) // 获取所有SP路由
// Get_All_SP_Route_Resp = uint32(0x8000000E) // 获取所有SP路由应答
// Update_TE_Route = uint32(0x0000000F) // SMGW向GNS更新终端路由
// Update_TE_Route_Resp = uint32(0x8000000F) // SMGW向GNS更新终端路由应答
// Update_SP_Route = uint32(0x00000010) // SMGW向GNS更新SP路由
// Update_SP_Route_Resp = uint32(0x80000010) // SMGW向GNS更新SP路由应答
// Push_Update_TE_Route = uint32(0x00000011) // GNS向SMGW更新终端路由
// Push_Update_TE_Route_Resp = uint32(0x80000011) // GNS向SMGW更新终端路由应答
// Push_Update_SP_Route = uint32(0x00000012) // GNS向SMGW更新SP路由
// Push_Update_SP_Route_Resp = uint32(0x80000012) // GNS向SMGW更新SP路由应答
)
var CommandMap = make(map[uint32]string)
func init() {
CommandMap[CmdLogin] = "Login"
CommandMap[CmdLoginResp] = "Login_Resp"
CommandMap[CmdSubmit] = "Submit"
CommandMap[CmdSubmitResp] = "Submit_Resp"
CommandMap[CmdDeliver] = "Deliver"
CommandMap[CmdDeliverResp] = "Deliver_Resp"
CommandMap[CmdActiveTest] = "Active_Test"
CommandMap[CmdActiveTestResp] = "Active_Test_Resp"
CommandMap[CmdExit] = "Exit"
CommandMap[CmdExitResp] = "Exit_Resp"
// CommandMap[Forward] = "Forward"
// CommandMap[Forward_Resp] = "Forward_Resp"
// CommandMap[Query] = "Query"
// CommandMap[Query_Resp] = "Query_Resp"
// CommandMap[Query_TE_Route] = "Query_TE_Route"
// CommandMap[Query_TE_Route_Resp] = "Query_TE_Route_Resp"
// CommandMap[Query_SP_Route] = "Query_SP_Route"
// CommandMap[Query_SP_Route_Resp] = "Query_SP_Route_Resp"
// CommandMap[Payment_Request] = "Payment_Request"
// CommandMap[Payment_Request_Resp] = "Payment_Request_Resp"
// CommandMap[Payment_Affirm] = "Payment_Affirm"
// CommandMap[Payment_Affirm_Resp] = "Payment_Affirm_Resp"
// CommandMap[Query_UserState] = "Query_UserState"
// CommandMap[Query_UserState_Resp] = "Query_UserState_Resp"
// CommandMap[Get_All_TE_Route] = "Get_All_TE_Route"
// CommandMap[Get_All_TE_Route_Resp] = "Get_All_TE_Route_Resp"
// CommandMap[Get_All_SP_Route] = "Get_All_SP_Route"
// CommandMap[Get_All_SP_Route_Resp] = "Get_All_SP_Route_Resp"
// CommandMap[Update_TE_Route] = "Update_TE_Route"
// CommandMap[Update_TE_Route_Resp] = "Update_TE_Route_Resp"
// CommandMap[Update_SP_Route] = "Update_SP_Route"
// CommandMap[Update_SP_Route_Resp] = "Update_SP_Route_Resp"
// CommandMap[Push_Update_TE_Route] = "Push_Update_TE_Route"
// CommandMap[Push_Update_TE_Route_Resp] = "Push_Update_TE_Route_Resp"
// CommandMap[Push_Update_SP_Route] = "Push_Update_SP_Route"
// CommandMap[Push_Update_SP_Route_Resp] = "Push_Update_SP_Route_Resp"
}