forked from linxGnu/gosmpp
/
transceivable.go
155 lines (125 loc) · 3.07 KB
/
transceivable.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
package gosmpp
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/linxGnu/gosmpp/pdu"
)
type transceivable struct {
settings Settings
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
conn *Connection
in *receivable
out *transmittable
aliveState int32
}
func newTransceivable(conn *Connection, settings Settings) *transceivable {
t := &transceivable{
settings: settings,
conn: conn,
}
t.ctx, t.cancel = context.WithCancel(context.Background())
t.out = newTransmittable(conn, Settings{
WriteTimeout: settings.WriteTimeout,
EnquireLink: settings.EnquireLink,
OnSubmitError: settings.OnSubmitError,
OnClosed: func(state State) {
switch state {
case ConnectionIssue:
// also close input
_ = t.in.close(ExplicitClosing)
if t.settings.OnClosed != nil {
t.settings.OnClosed(ConnectionIssue)
}
default:
return
}
},
RequestWindowConfig: settings.RequestWindowConfig,
})
t.in = newReceivable(conn, Settings{
ReadTimeout: settings.ReadTimeout,
OnPDU: settings.OnPDU,
OnAllPDU: settings.OnAllPDU,
OnReceivingError: settings.OnReceivingError,
OnClosed: func(state State) {
switch state {
case InvalidStreaming, UnbindClosing:
// also close output
_ = t.out.close(ExplicitClosing)
if t.settings.OnClosed != nil {
t.settings.OnClosed(state)
}
default:
return
}
},
RequestWindowConfig: settings.RequestWindowConfig,
response: func(p pdu.PDU) {
_ = t.Submit(p)
},
})
return t
}
func (t *transceivable) start() {
if t.settings.RequestWindowConfig != nil && t.settings.ExpireCheckTimer > 0 {
t.wg.Add(1)
go func() {
t.windowCleanup()
t.wg.Done()
}()
}
t.out.start()
t.in.start()
}
// SystemID returns tagged SystemID which is attached with bind_resp from SMSC.
func (t *transceivable) SystemID() string {
return t.conn.systemID
}
// Close transceiver and stop underlying daemons.
func (t *transceivable) Close() (err error) {
if atomic.CompareAndSwapInt32(&t.aliveState, Alive, Closed) {
// closing input and output
_ = t.out.close(StoppingProcessOnly)
_ = t.in.close(StoppingProcessOnly)
// close underlying conn
err = t.conn.Close()
// notify transceiver closed
if t.settings.OnClosed != nil {
t.settings.OnClosed(ExplicitClosing)
}
}
return
}
// Submit a PDU.
func (t *transceivable) Submit(p pdu.PDU) error {
return t.out.Submit(p)
}
func (t *transceivable) GetWindowSize() int {
return t.out.GetWindowSize()
}
func (t *transceivable) windowCleanup() {
ticker := time.NewTicker(t.settings.ExpireCheckTimer)
defer ticker.Stop()
for {
select {
case <-t.ctx.Done():
return
case <-ticker.C:
for _, request := range t.settings.RequestStore.List(context.TODO()) {
if time.Since(request.TimeSent) > t.settings.PduExpireTimeOut {
t.settings.RequestStore.Delete(context.TODO(), request.GetSequenceNumber())
if t.settings.OnExpiredPduRequest != nil {
bindClose := t.settings.OnExpiredPduRequest(request.PDU)
if bindClose {
_ = t.Close()
}
}
}
}
}
}
}