Skip to content

Commit 4f2b06c

Browse files
hujiaming.0927hujm2023
hujiaming.0927
authored andcommitted
feature:1.Optimize PDU implement;2.Add SGIP/SMGP protocol implementation
1 parent aad2321 commit 4f2b06c

File tree

107 files changed

+7552
-1701
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+7552
-1701
lines changed

.DS_Store

6 KB
Binary file not shown.

cmpp/cmpp20/consts.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cmpp20
22

3+
import "errors"
4+
35
const (
46
HeaderLength = 4 + 4 + 4 // cmpp2 PDU Header的长度
57

@@ -16,3 +18,16 @@ const (
1618
MaxQueryLength = HeaderLength + 8 + 1 + 10 + 8
1719
MaxQueryRespLength = HeaderLength + 8 + 1 + 10 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4
1820
)
21+
const (
22+
DELIVERED = "DELIVRD" // 成功送达
23+
UNDELIVERABLE = "UNDELIV" // 无法送达
24+
EXPIRED = "EXPIRED"
25+
DELETED = "DELETED"
26+
ACCEPTED = "ACCEPTD"
27+
UNKNOWN = "UNKNOWN"
28+
REJECTED = "REJECTD"
29+
)
30+
31+
var (
32+
ErrInvalidPudLength = errors.New("invalid pdu length")
33+
)

cmpp/cmpp20/pdu_activetest.go

+59-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmpp20
22

33
import (
4+
sms "github.com/hujm2023/go-sms-protocol"
45
"github.com/hujm2023/go-sms-protocol/cmpp"
56
"github.com/hujm2023/go-sms-protocol/packet"
67
)
@@ -10,17 +11,18 @@ type PduActiveTest struct {
1011
}
1112

1213
func (p *PduActiveTest) IEncode() ([]byte, error) {
13-
buf := packet.NewPacketWriter()
14+
p.TotalLength = MaxActiveTestLength
15+
buf := packet.NewPacketWriter(MaxActiveTestLength)
1416
defer buf.Release()
1517

16-
cmpp.WriteHeaderNoLength(p.Header, buf)
18+
buf.WriteBytes(p.Header.Bytes())
1719

18-
return buf.BytesWithLength()
20+
return buf.Bytes()
1921
}
2022

2123
func (p *PduActiveTest) IDecode(data []byte) error {
2224
if len(data) < cmpp.MinCMPPPduLength {
23-
return cmpp.ErrInvalidPudLength
25+
return ErrInvalidPudLength
2426
}
2527

2628
buf := packet.NewPacketReader(data)
@@ -31,10 +33,36 @@ func (p *PduActiveTest) IDecode(data []byte) error {
3133
return buf.Error()
3234
}
3335

36+
func (p *PduActiveTest) GetSequenceID() uint32 {
37+
return p.Header.SequenceID
38+
}
39+
3440
func (p *PduActiveTest) SetSequenceID(sid uint32) {
3541
p.Header.SequenceID = sid
3642
}
3743

44+
func (p *PduActiveTest) GetCommand() sms.ICommander {
45+
return cmpp.CommandActiveTest
46+
}
47+
48+
func (p *PduActiveTest) GenEmptyResponse() sms.PDU {
49+
return &PduActiveTestResp{
50+
Header: cmpp.Header{
51+
CommandID: cmpp.CommandActiveTestResp,
52+
SequenceID: p.GetSequenceID(),
53+
},
54+
}
55+
}
56+
57+
func (p *PduActiveTest) String() string {
58+
w := packet.NewPDUStringer()
59+
defer w.Release()
60+
61+
w.Write("Header", p.Header)
62+
63+
return w.String()
64+
}
65+
3866
// --------------------------------------------------------------------
3967

4068
type PduActiveTestResp struct {
@@ -45,18 +73,19 @@ type PduActiveTestResp struct {
4573
}
4674

4775
func (pr *PduActiveTestResp) IEncode() ([]byte, error) {
48-
buf := packet.NewPacketWriter()
76+
pr.TotalLength = MaxActiveTestRespLength
77+
buf := packet.NewPacketWriter(MaxActiveTestRespLength)
4978
defer buf.Release()
5079

51-
cmpp.WriteHeaderNoLength(pr.Header, buf)
80+
buf.WriteBytes(pr.Header.Bytes())
5281
buf.WriteUint8(pr.Reserved)
5382

54-
return buf.BytesWithLength()
83+
return buf.Bytes()
5584
}
5685

5786
func (pr *PduActiveTestResp) IDecode(data []byte) error {
5887
if len(data) < cmpp.MinCMPPPduLength {
59-
return cmpp.ErrInvalidPudLength
88+
return ErrInvalidPudLength
6089
}
6190

6291
buf := packet.NewPacketReader(data)
@@ -68,6 +97,28 @@ func (pr *PduActiveTestResp) IDecode(data []byte) error {
6897
return buf.Error()
6998
}
7099

100+
func (pr *PduActiveTestResp) GetSequenceID() uint32 {
101+
return pr.Header.SequenceID
102+
}
103+
71104
func (pr *PduActiveTestResp) SetSequenceID(sid uint32) {
72105
pr.Header.SequenceID = sid
73106
}
107+
108+
func (p *PduActiveTestResp) GetCommand() sms.ICommander {
109+
return cmpp.CommandActiveTestResp
110+
}
111+
112+
func (p *PduActiveTestResp) GenEmptyResponse() sms.PDU {
113+
return nil
114+
}
115+
116+
func (p *PduActiveTestResp) String() string {
117+
w := packet.NewPDUStringer()
118+
defer w.Release()
119+
120+
w.Write("Header", p.Header)
121+
w.Write("Reserved", p.Reserved)
122+
123+
return w.String()
124+
}

cmpp/cmpp20/pdu_activetest_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ func TestPduActiveTest(t *testing.T) {
1919
encoded, err := a.IEncode()
2020
assert.Nil(t, err)
2121
assert.True(t, bytes.EqualFold(dataExpected, encoded))
22+
23+
t.Log(a.String())
2224
}

cmpp/cmpp20/pdu_connect.go

+61-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package cmpp20
22

33
import (
4-
"fmt"
5-
4+
sms "github.com/hujm2023/go-sms-protocol"
65
"github.com/hujm2023/go-sms-protocol/cmpp"
76
"github.com/hujm2023/go-sms-protocol/packet"
87
)
@@ -24,21 +23,22 @@ type PduConnect struct {
2423
}
2524

2625
func (p *PduConnect) IEncode() ([]byte, error) {
27-
buf := packet.NewPacketWriter()
26+
p.TotalLength = MaxConnectLength
27+
buf := packet.NewPacketWriter(MaxConnectLength)
2828
defer buf.Release()
2929

30-
cmpp.WriteHeaderNoLength(p.Header, buf)
30+
buf.WriteBytes(p.Header.Bytes())
3131
buf.WriteFixedLenString(p.SourceAddr, 6)
3232
buf.WriteFixedLenString(p.AuthenticatorSource, 16)
3333
buf.WriteUint8(p.Version)
3434
buf.WriteUint32(p.Timestamp)
3535

36-
return buf.BytesWithLength()
36+
return buf.Bytes()
3737
}
3838

3939
func (p *PduConnect) IDecode(data []byte) error {
4040
if len(data) < cmpp.MinCMPPPduLength {
41-
return cmpp.ErrInvalidPudLength
41+
return ErrInvalidPudLength
4242
}
4343

4444
buf := packet.NewPacketReader(data)
@@ -53,10 +53,40 @@ func (p *PduConnect) IDecode(data []byte) error {
5353
return buf.Error()
5454
}
5555

56+
func (p *PduConnect) GetSequenceID() uint32 {
57+
return p.Header.SequenceID
58+
}
59+
5660
func (p *PduConnect) SetSequenceID(sid uint32) {
5761
p.Header.SequenceID = sid
5862
}
5963

64+
func (p *PduConnect) GetCommand() sms.ICommander {
65+
return cmpp.CommandConnect
66+
}
67+
68+
func (p *PduConnect) GenEmptyResponse() sms.PDU {
69+
return &PduConnectResp{
70+
Header: cmpp.Header{
71+
CommandID: cmpp.CommandConnectResp,
72+
SequenceID: p.GetSequenceID(),
73+
},
74+
}
75+
}
76+
77+
func (p *PduConnect) String() string {
78+
w := packet.NewPDUStringer()
79+
defer w.Release()
80+
81+
w.Write("Header", p.Header)
82+
w.Write("SourceAddr", p.SourceAddr)
83+
w.WriteWithBytes("AuthenticatorSource", p.AuthenticatorSource)
84+
w.Write("Version", p.Version)
85+
w.Write("Timestamp", p.Timestamp)
86+
87+
return w.String()
88+
}
89+
6090
// --------------------------------------------------------------------
6191

6292
type PduConnectResp struct {
@@ -73,20 +103,21 @@ type PduConnectResp struct {
73103
}
74104

75105
func (pr *PduConnectResp) IEncode() ([]byte, error) {
76-
buf := packet.NewPacketWriter()
106+
pr.TotalLength = MaxConnectRespLength
107+
buf := packet.NewPacketWriter(MaxConnectRespLength)
77108
defer buf.Release()
78109

79-
cmpp.WriteHeaderNoLength(pr.Header, buf)
110+
buf.WriteBytes(pr.Header.Bytes())
80111
buf.WriteUint8(pr.Status)
81112
buf.WriteFixedLenString(pr.AuthenticatorISMG, 16)
82113
buf.WriteUint8(pr.Version)
83114

84-
return buf.BytesWithLength()
115+
return buf.Bytes()
85116
}
86117

87118
func (pr *PduConnectResp) IDecode(data []byte) error {
88119
if len(data) < cmpp.MinCMPPPduLength {
89-
return cmpp.ErrInvalidPudLength
120+
return ErrInvalidPudLength
90121
}
91122

92123
buf := packet.NewPacketReader(data)
@@ -98,23 +129,30 @@ func (pr *PduConnectResp) IDecode(data []byte) error {
98129
return buf.Error()
99130
}
100131

132+
func (pr *PduConnectResp) GetSequenceID() uint32 {
133+
return pr.Header.SequenceID
134+
}
135+
101136
func (pr *PduConnectResp) SetSequenceID(sid uint32) {
102137
pr.Header.SequenceID = sid
103138
}
104139

105-
// 1 字节,状态:0正确 1消息结构错 2非法源地址 3认证错 4版本太高 >5其他错误
106-
var connectRespStatus = map[uint8]string{
107-
0: "正确",
108-
1: "消息结构错",
109-
2: "非法源地址",
110-
3: "认证错",
111-
4: "版本太高",
112-
5: "其他错误",
140+
func (p *PduConnectResp) GetCommand() sms.ICommander {
141+
return cmpp.CommandConnectResp
113142
}
114143

115-
func ConnectRespResultString(r uint8) string {
116-
if s, ok := connectRespStatus[r]; ok {
117-
return s
118-
}
119-
return fmt.Sprintf("未知错误代码 %d", r)
144+
func (p *PduConnectResp) GenEmptyResponse() sms.PDU {
145+
return nil
146+
}
147+
148+
func (p *PduConnectResp) String() string {
149+
w := packet.NewPDUStringer()
150+
defer w.Release()
151+
152+
w.Write("Header", p.Header)
153+
w.Write("Status", p.Status)
154+
w.WriteWithBytes("AuthenticatorISMG", p.AuthenticatorISMG)
155+
w.Write("Version", p.Version)
156+
157+
return w.String()
120158
}

cmpp/cmpp20/pdu_connect_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"bytes"
55
"testing"
66

7-
"github.com/hujm2023/go-sms-protocol/cmpp"
87
"github.com/stretchr/testify/assert"
8+
9+
"github.com/hujm2023/go-sms-protocol/cmpp"
910
)
1011

1112
func TestPduConnect(t *testing.T) {
@@ -28,4 +29,6 @@ func TestPduConnect(t *testing.T) {
2829
encoded, err := c.IEncode()
2930
assert.Nil(t, err)
3031
assert.True(t, bytes.Equal(encoded, dataExpected))
32+
33+
t.Log(c.String())
3134
}

0 commit comments

Comments
 (0)