forked from fiorix/go-smpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.go
262 lines (220 loc) · 6.24 KB
/
receiver.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
// Copyright 2015 go-smpp authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package smpp
import (
"bytes"
"crypto/tls"
"fmt"
"sync"
"time"
"github.com/Zensey/go-smpp/smpp/pdu"
"github.com/Zensey/go-smpp/smpp/pdu/pdufield"
)
// Receiver implements an SMPP client receiver.
type Receiver struct {
Addr string
User string
Passwd string
SystemType string
EnquireLink time.Duration
EnquireLinkTimeout time.Duration // Time after last EnquireLink response when connection considered down
BindInterval time.Duration // Binding retry interval
MergeInterval time.Duration // Time in which Receiver waits for the parts of the long messages
MergeCleanupInterval time.Duration // How often to cleanup expired message parts
TLS *tls.Config
Handler HandlerFunc
chanClose chan struct{}
// struct which holds the map of MergeHolders for the merging of the long incoming messages.
// It is used only if the incoming PDU holds UDH data and Receiver has MergeInterval > 0.
mg struct {
mergeHolders map[int]*MergeHolder
sync.Mutex
}
cl struct {
*client
sync.Mutex
}
}
// HandlerFunc is the handler function that a Receiver calls
// when a new PDU arrives.
type HandlerFunc func(p pdu.Body)
// MergeHolder is a struct which holds the slice of MessageParts for the merging of a long incoming message.
type MergeHolder struct {
MessageID int
MessageParts []*MessagePart // Slice with the parts of the message
PartsCount int
LastWriteTime time.Time
}
// MessagePart is a struct which holds the data of the part of a long incoming message.
type MessagePart struct {
PartID int
Data *bytes.Buffer
}
// Bind starts the Receiver. It creates a persistent connection
// to the server, update its status via the returned channel,
// and calls the registered Handler when new PDU arrives.
//
// Bind implements the ClientConn interface.
func (r *Receiver) Bind() <-chan ConnStatus {
r.cl.Lock()
defer r.cl.Unlock()
r.chanClose = make(chan struct{})
if r.cl.client != nil {
return r.cl.Status
}
c := &client{
Addr: r.Addr,
TLS: r.TLS,
EnquireLink: r.EnquireLink,
EnquireLinkTimeout: r.EnquireLinkTimeout,
Status: make(chan ConnStatus, 1),
BindFunc: r.bindFunc,
BindInterval: r.BindInterval,
}
r.cl.client = c
c.init()
go c.Bind()
// Set up message merging if requested
if r.MergeInterval > 0 {
if r.MergeCleanupInterval == 0 {
r.MergeCleanupInterval = 1 * time.Second
}
r.mg.mergeHolders = make(map[int]*MergeHolder)
go r.mergeCleaner()
}
return c.Status
}
func (r *Receiver) bindFunc(c Conn) error {
p := pdu.NewBindReceiver()
f := p.Fields()
f.Set(pdufield.SystemID, r.User)
f.Set(pdufield.Password, r.Passwd)
f.Set(pdufield.SystemType, r.SystemType)
resp, err := bind(c, p)
if err != nil {
return err
}
if resp.Header().ID != pdu.BindReceiverRespID {
return fmt.Errorf("unexpected response for BindReceiver: %s",
resp.Header().ID)
}
// Clean the map in case of rebind, because message id numbering resets after reconnection
// and older IDs are no longer valid
if r.MergeInterval > 0 {
r.mg.Lock()
r.mg.mergeHolders = make(map[int]*MergeHolder)
r.mg.Unlock()
}
if r.Handler != nil {
go r.handlePDU()
}
return nil
}
func (r *Receiver) handlePDU() {
var (
ok bool
sm *pdufield.SM
udhList *pdufield.UDHList
msgID, partsCount int
mh *MergeHolder
orderedBodies []*bytes.Buffer
)
loop:
for {
p, err := r.cl.Read()
if err != nil {
break
}
if p.Header().ID == pdu.DeliverSMID { // Send DeliverSMResp
pResp := pdu.NewDeliverSMRespSeq(p.Header().Seq)
r.cl.Write(pResp)
}
if r.MergeInterval == 0 { // Handle the PDU if merging is not needed
r.Handler(p)
continue
}
sm, ok = p.Fields()[pdufield.ShortMessage].(*pdufield.SM)
if !ok {
// PDU is malformed, do not process
continue
}
udhList, ok = p.Fields()[pdufield.GSMUserData].(*pdufield.UDHList)
if !ok { // Check if GSMUserData is present inside the PDU, do not try to merge if it's not
r.Handler(p)
continue
}
for _, udh := range udhList.Data {
switch udh.IEI.Data {
case 0x00: // Concatenated short messages, 8-bit reference number
if int(udh.IELength.Data) != 3 { // Contains message ID, parts count and part number
// PDU is malformed, do not process
break
}
// Get message ID and total count of its parts
msgID = int(udh.IEData.Data[0])
partsCount = int(udh.IEData.Data[1])
// Check if message part was already added to a MergeHolder
r.mg.Lock()
if mh, ok = r.mg.mergeHolders[msgID]; !ok {
mh = &MergeHolder{
MessageID: msgID,
PartsCount: partsCount,
}
r.mg.mergeHolders[msgID] = mh
}
r.mg.Unlock()
// Add current part of the message to the slice
mh.MessageParts = append(mh.MessageParts, &MessagePart{
PartID: int(udh.IEData.Data[2]),
Data: bytes.NewBuffer(sm.Data),
})
mh.LastWriteTime = time.Now()
// Check if we have all the parts of the message
if len(mh.MessageParts) != mh.PartsCount {
continue loop
}
// Order up PDUs
orderedBodies = make([]*bytes.Buffer, partsCount)
for _, mp := range mh.MessageParts {
orderedBodies[mp.PartID-1] = mp.Data
}
// Merge PDUs
var buf bytes.Buffer
for _, body := range orderedBodies {
buf.Write(body.Bytes())
}
p.Fields().Set(pdufield.ShortMessage, buf.Bytes())
// Handle
r.Handler(p)
}
}
}
}
func (r *Receiver) mergeCleaner() {
timer := time.NewTimer(r.MergeCleanupInterval)
for {
select {
case <-timer.C:
r.mg.Lock()
for _, mHolder := range r.mg.mergeHolders {
if time.Since(mHolder.LastWriteTime) > r.MergeInterval { // Message has expired, remove
delete(r.mg.mergeHolders, mHolder.MessageID)
}
}
r.mg.Unlock()
case <-r.chanClose:
return
}
}
}
// Close implements the ClientConn interface.
func (r *Receiver) Close() error {
r.cl.Lock()
defer r.cl.Unlock()
if r.cl.client == nil {
return ErrNotConnected
}
close(r.chanClose)
return r.cl.Close()
}