-
Notifications
You must be signed in to change notification settings - Fork 62
/
session.go
183 lines (156 loc) · 4.33 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
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
package gosmpp
import (
"errors"
"fmt"
"github.com/linxGnu/gosmpp/pdu"
"sync/atomic"
"time"
)
var (
ErrWindowSizeEqualZero = errors.New("request window size cannot be 0")
ErrExpireCheckTimerNotSet = errors.New("ExpireCheckTimer cannot be 0 if PduExpireTimeOut is set")
ErrStoreAccessTimeOutEqualZero = errors.New("StoreAccessTimeOut window size cannot be 0")
ErrWindowSizeNotAvailableOnReceiverBinds = errors.New("window size not available on receiver binds")
)
// 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
requestStore RequestStore
}
type SessionOption func(session *Session)
// 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, opts ...SessionOption) (session *Session, err error) {
// Loop through each option
if settings.ReadTimeout <= 0 || settings.ReadTimeout <= settings.EnquireLink {
return nil, fmt.Errorf("invalid settings: ReadTimeout must greater than max(0, EnquireLink)")
}
var requestStore RequestStore = nil
if settings.WindowedRequestTracking != nil {
requestStore = NewDefaultStore()
if settings.MaxWindowSize == 0 {
return nil, ErrWindowSizeEqualZero
}
if settings.StoreAccessTimeOut == 0 {
return nil, ErrStoreAccessTimeOutEqualZero
}
if settings.PduExpireTimeOut > 0 && settings.ExpireCheckTimer == 0 {
return nil, ErrExpireCheckTimerNotSet
}
}
conn, err := c.Connect()
if err == nil {
session = &Session{
c: c,
rebindingInterval: rebindingInterval,
originalOnClosed: settings.OnClosed,
requestStore: requestStore,
}
for _, opt := range opts {
opt(session)
}
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
trans := newTransceivable(conn, session.settings, session.requestStore)
trans.start()
session.trx.Store(trans)
}
return
}
func WithRequestStore(store RequestStore) SessionOption {
return func(s *Session) {
s.requestStore = store
}
}
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()
}
func (s *Session) GetWindowSize() (int, error) {
if s.c.GetBindType() == pdu.Transmitter || s.c.GetBindType() == pdu.Transceiver {
size, err := s.bound().GetWindowSize()
if err != nil {
return 0, err
}
return size, nil
}
return 0, ErrWindowSizeNotAvailableOnReceiverBinds
}
// 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
trans := newTransceivable(conn, s.settings, s.requestStore)
trans.start()
s.trx.Store(trans)
// reset rebinding state
atomic.StoreInt32(&s.rebinding, 0)
if s.settings.OnRebind != nil {
s.settings.OnRebind()
}
return
}
}
}
}