forked from linxGnu/gosmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
130 lines (109 loc) · 2.77 KB
/
session.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
package gosmpp
import (
"fmt"
"sync/atomic"
"time"
)
// Session represents session for TX, RX, TRX.
type Session struct {
c Connector
originalOnClosed func(State)
settings Settings
rebindingInterval time.Duration
trx atomic.Value // transceivable
state int32
rebinding int32
}
// NewSession creates new session for TX, RX, TRX.
//
// Session will `non-stop`, automatically rebind (create new and authenticate connection with SMSC) when
// unexpected error happened.
//
// `rebindingInterval` indicates duration that Session has to wait before rebinding again.
//
// Setting `rebindingInterval <= 0` will disable `auto-rebind` functionality.
func NewSession(c Connector, settings Settings, rebindingInterval time.Duration) (session *Session, err error) {
if settings.ReadTimeout <= 0 || settings.ReadTimeout <= settings.EnquireLink {
return nil, fmt.Errorf("invalid settings: ReadTimeout must greater than max(0, EnquireLink)")
}
conn, err := c.Connect()
if err == nil {
session = &Session{
c: c,
rebindingInterval: rebindingInterval,
originalOnClosed: settings.OnClosed,
}
if rebindingInterval > 0 {
newSettings := settings
newSettings.OnClosed = func(state State) {
switch state {
case ExplicitClosing:
return
default:
if session.originalOnClosed != nil {
session.originalOnClosed(state)
}
session.rebind()
}
}
session.settings = newSettings
} else {
session.settings = settings
}
// bind to session
session.trx.Store(newTransceivable(conn, session.settings))
}
return
}
func (s *Session) bound() *transceivable {
r, _ := s.trx.Load().(*transceivable)
return r
}
// Transmitter returns bound Transmitter.
func (s *Session) Transmitter() Transmitter {
return s.bound()
}
// Receiver returns bound Receiver.
func (s *Session) Receiver() Receiver {
return s.bound()
}
// Transceiver returns bound Transceiver.
func (s *Session) Transceiver() Transceiver {
return s.bound()
}
// Close session.
func (s *Session) Close() (err error) {
if atomic.CompareAndSwapInt32(&s.state, Alive, Closed) {
err = s.close()
}
return
}
func (s *Session) close() (err error) {
if b := s.bound(); b != nil {
err = b.Close()
}
return
}
func (s *Session) rebind() {
if atomic.CompareAndSwapInt32(&s.rebinding, 0, 1) {
_ = s.close()
for atomic.LoadInt32(&s.state) == Alive {
conn, err := s.c.Connect()
if err != nil {
if s.settings.OnRebindingError != nil {
s.settings.OnRebindingError(err)
}
time.Sleep(s.rebindingInterval)
} else {
// bind to session
s.trx.Store(newTransceivable(conn, s.settings))
// reset rebinding state
atomic.StoreInt32(&s.rebinding, 0)
if s.settings.OnRebind != nil {
s.settings.OnRebind()
}
return
}
}
}
}