forked from CodeMonkeyKevin/smpp34
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmitter.go
253 lines (199 loc) · 5.4 KB
/
transmitter.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
package smpp34
import (
"crypto/tls"
"time"
)
type Transmitter struct {
Smpp
eLTicker *time.Ticker // Enquire Link ticker
eLCheckTimer *time.Timer // Enquire Link Check timer
eLDuration int // Enquire Link Duration
Err error // Errors generated in go routines that lead to conn close
}
// NewTransmitter creates and initializes a new Transmitter.
// The eli parameter is for EnquireLink interval, in seconds.
func NewTransmitter(host string, port int, eli int, bindParams Params) (*Transmitter, error) {
return newTransmitter(host, port, eli, bindParams, nil)
}
// NewTransmitterTLs creates and initializes a new Transmitter using TLS.
// The eli parameter is for EnquireLink interval, in seconds.
func NewTransmitterTLS(host string, port int, eli int, bindParams Params, config *tls.Config) (*Transmitter, error) {
if config == nil {
config = &tls.Config{}
}
return newTransmitter(host, port, eli, bindParams, config)
}
// eli = EnquireLink Interval in Seconds
func newTransmitter(host string, port int, eli int, bindParams Params, config *tls.Config) (*Transmitter, error) {
tx := &Transmitter{}
var err error
if config == nil {
err = tx.Connect(host, port)
} else {
err = tx.ConnectTLS(host, port, config)
}
if err != nil {
return nil, err
}
sysId := bindParams[SYSTEM_ID].(string)
pass := bindParams[PASSWORD].(string)
if err := tx.Bind(sysId, pass, &bindParams); err != nil {
return nil, err
}
// EnquireLinks should not be less 10seconds
if eli < 10 {
eli = 10
}
tx.eLDuration = eli
go tx.startEnquireLink(eli)
return tx, nil
}
func (t *Transmitter) Bind(system_id string, password string, params *Params) error {
pdu, err := t.Smpp.Bind(BIND_TRANSMITTER, system_id, password, params)
if err := t.Write(pdu); err != nil {
return err
}
// If BindResp NOT received in 5secs close connection
go t.bindCheck()
// Read (blocking)
pdu, err = t.Smpp.Read()
if err != nil {
return err
}
if pdu.GetHeader().Id != BIND_TRANSMITTER_RESP {
return SmppBindRespErr
}
if !pdu.Ok() {
return SmppBindAuthErr("Bind auth failed. " + pdu.GetHeader().Status.Error())
}
t.Bound = true
return nil
}
func (t *Transmitter) SubmitSm(source_addr, destination_addr, short_message string, params *Params) (seq uint32, err error) {
p, err := t.Smpp.SubmitSm(source_addr, destination_addr, short_message, params)
if err != nil {
return 0, err
}
if err := t.Write(p); err != nil {
return 0, err
}
return p.GetHeader().Sequence, nil
}
func (t *Transmitter) SubmitSmEncoded(source_addr, destination_addr string, short_message []byte, params *Params) (seq uint32, err error) {
p, err := t.Smpp.SubmitSmEncoded(source_addr, destination_addr, short_message, params)
if err != nil {
return 0, err
}
if err := t.Write(p); err != nil {
return 0, err
}
return p.GetHeader().Sequence, nil
}
func (t *Transmitter) QuerySm(message_id, source_addr string, params *Params) (seq uint32, err error) {
p, err := t.Smpp.QuerySm(message_id, source_addr, params)
if err != nil {
return 0, err
}
if err := t.Write(p); err != nil {
return 0, err
}
return p.GetHeader().Sequence, nil
}
func (t *Transmitter) DeliverSmResp(seq, status uint32) error {
return SmppPduErr
}
func (t *Transmitter) Unbind() error {
p, _ := t.Smpp.Unbind()
if err := t.Write(p); err != nil {
return err
}
return nil
}
func (t *Transmitter) UnbindResp(seq uint32) error {
p, _ := t.Smpp.UnbindResp(seq)
if err := t.Write(p); err != nil {
return err
}
t.Bound = false
return nil
}
func (t *Transmitter) bindCheck() {
// Block
<-time.After(time.Duration(5 * time.Second))
if !t.Bound {
// send error to t.err? So it can be read before closing.
t.Err = SmppBindRespErr
t.Close()
}
}
func (t *Transmitter) startEnquireLink(eli int) {
t.eLTicker = time.NewTicker(time.Duration(eli) * time.Second)
// check delay is half the time of enquire link intervel
d := time.Duration(eli/2) * time.Second
t.eLCheckTimer = time.NewTimer(d)
t.eLCheckTimer.Stop()
for {
select {
case <-t.eLTicker.C:
p, _ := t.EnquireLink()
if err := t.Write(p); err != nil {
t.Err = SmppELWriteErr
t.Close()
return
}
t.eLCheckTimer.Reset(d)
case <-t.eLCheckTimer.C:
t.Err = SmppELRespErr
t.Close()
return
}
}
}
func (t *Transmitter) Read() (Pdu, error) {
pdu, err := t.Smpp.Read()
if err != nil {
if _, ok := err.(PduCmdIdErr); ok {
// Invalid PDU Command ID, should send back GenericNack
t.GenericNack(uint32(0), ESME_RINVCMDID)
} else if SmppPduLenErr == err {
// Invalid PDU, PDU read or Len error
t.GenericNack(uint32(0), ESME_RINVCMDLEN)
}
return nil, err
}
switch pdu.GetHeader().Id {
case SUBMIT_SM_RESP:
return pdu, nil
case QUERY_SM_RESP:
return pdu, nil
case ENQUIRE_LINK:
p, _ := t.Smpp.EnquireLinkResp(pdu.GetHeader().Sequence)
if err := t.Write(p); err != nil {
return nil, err
}
case ENQUIRE_LINK_RESP:
// Stop EnquireLink Check.
t.eLCheckTimer.Stop()
case UNBIND:
t.UnbindResp(pdu.GetHeader().Sequence)
t.Close()
default:
// Should not have received these PDUs on a TX bind
return nil, SmppPduErr
}
return pdu, nil
}
func (t *Transmitter) Close() {
// Check timers exists incase we Close() before timers are created
if t.eLCheckTimer != nil {
t.eLCheckTimer.Stop()
}
if t.eLTicker != nil {
t.eLTicker.Stop()
}
t.Smpp.Close()
}
func (t *Transmitter) Write(p Pdu) error {
err := t.Smpp.Write(p)
return err
}