forked from linxGnu/gosmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiverBase.go
208 lines (166 loc) · 4.85 KB
/
ReceiverBase.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
package gosmpp
import (
"fmt"
"time"
"github.com/linxGnu/gosmpp/Data"
"github.com/linxGnu/gosmpp/Exception"
"github.com/linxGnu/gosmpp/PDU"
"github.com/linxGnu/gosmpp/Utils"
)
type IReceiver interface {
IRoutineProcess
ReceiveAsync()
TryReceivePDU(IConnection, PDU.IPDU) (PDU.IPDU, *Exception.Exception)
}
type ReceiverBase struct {
RoutineProcess
receiveTimeout int64
messageIncompleteRetryCount byte
receiver IReceiver
}
func NewReceiverBase() *ReceiverBase {
a := &ReceiverBase{}
a.Construct()
return a
}
func (c *ReceiverBase) Construct() {
c.receiveTimeout = Data.RECEIVER_TIMEOUT
c.messageIncompleteRetryCount = 0
}
func (c *ReceiverBase) RegisterReceiver(rec IReceiver) {
c.receiver = rec
c.RegisterProcessUnit(rec)
}
func (c *ReceiverBase) GetReceiveTimeout() int64 {
return c.receiveTimeout
}
func (c *ReceiverBase) SetReceiveTimeout(timeout int64) {
c.receiveTimeout = timeout
}
/**
* This is an implementation of <code>ProcessingThread</code>'s
* <code>process</code> method, which is method called in loop from
* the <code>run</code> method.<br>
* This simply calls <code>receiveAsync</code>.
*/
func (c *ReceiverBase) Process() {
if c.receiver != nil {
c.receiver.ReceiveAsync()
}
}
func (c *ReceiverBase) canContinueReceiving(deadLine time.Time, timeout int64) bool {
if timeout == Data.RECEIVE_BLOCKING {
return true
}
return time.Now().Before(deadLine)
}
func (c *ReceiverBase) tryReceivePDUWithTimeout(conn IConnection, expected PDU.IPDU) (PDU.IPDU, *Exception.Exception) {
return c.tryReceivePDUWithCustomTimeout(conn, expected, c.GetReceiveTimeout())
}
func (c *ReceiverBase) tryReceivePDUWithCustomTimeout(conn IConnection, expectedPDU PDU.IPDU, timeout int64) (pduResult PDU.IPDU, expc *Exception.Exception) {
defer func() {
if errs := recover(); errs != nil {
pduResult = nil
expc = Exception.NewException(fmt.Errorf("%v\n", errs))
}
}()
if c.receiver == nil {
return nil, Exception.NewExceptionFromStr("Receiver not initialized")
}
if timeout <= 0 {
return c.receiver.TryReceivePDU(conn, expectedPDU)
}
deadLine := time.Now().Add(time.Duration(timeout) * time.Millisecond)
var pdu PDU.IPDU
for pdu == nil && c.canContinueReceiving(deadLine, timeout) {
_pdu, err := c.receiver.TryReceivePDU(conn, expectedPDU)
if err != nil {
return nil, err
}
pdu = _pdu
if pdu == nil {
time.Sleep(50 * time.Millisecond)
}
}
return pdu, nil
}
func (c *ReceiverBase) ReceivePDUFromConnection(conn IConnection, unprocessed *Utils.Unprocessed) (PDU.IPDU, *Exception.Exception) {
if unprocessed == nil {
return nil, nil
}
var pdu PDU.IPDU // nil by default
var unprocessedBuf *Utils.ByteBuffer
if unprocessed.GetHasUnprocessed() {
unprocessedBuf = unprocessed.GetUnprocessed()
_pdu, err := c.tryGetUnprocessedPDU(unprocessed)
if err != nil {
return _pdu, err
}
pdu = _pdu
}
if pdu == nil {
buffer, err := conn.Receive()
if err != nil {
return pdu, err
}
unprocessedBuf = unprocessed.GetUnprocessed()
if buffer.Len() != 0 {
_, err := unprocessedBuf.Write(buffer.Bytes())
if err != nil {
return pdu, Exception.NewException(err)
}
unprocessed.SetLastTimeReceivedCurTime()
_pdu, e := c.tryGetUnprocessedPDU(unprocessed)
if e != nil {
if e == Exception.UnknownCommandIdException {
unprocessed.Reset()
}
return pdu, e
}
pdu = _pdu
} else {
timeout := c.GetReceiveTimeout()
if unprocessedBuf.Len() > 0 && time.Now().UnixNano() > timeout*int64(1000000)+unprocessed.GetLastTimeReceived() {
unprocessed.Reset()
return pdu, Exception.TimeoutException
}
}
}
return pdu, nil
}
func (c *ReceiverBase) tryGetUnprocessedPDU(unproc *Utils.Unprocessed) (PDU.IPDU, *Exception.Exception) {
unprocBuffer := unproc.GetUnprocessed()
pdu, err, header := PDU.CreatePDU(unprocBuffer)
if err == Exception.HeaderIncompleteException {
unproc.SetHasUnprocessed(false)
unproc.SetExpected(Data.PDU_HEADER_SIZE)
// incomplete message header, will wait for the rest.
return nil, nil
} else if err == Exception.MessageIncompleteException {
if c.messageIncompleteRetryCount > 5 {
// Giving up on incomplete messages - probably garbage in unprocessed buffer. Flushing unprocessed buffer.
c.messageIncompleteRetryCount = 0
unproc.Reset()
}
unproc.SetHasUnprocessed(false)
unproc.SetExpected(Data.PDU_HEADER_SIZE)
c.messageIncompleteRetryCount++
// incomplete message, will wait for the rest.
return nil, nil
} else if err == Exception.UnknownCommandIdException {
if int(header.GetCommandLength()) <= unprocBuffer.Len() {
_, err1 := unprocBuffer.Read_Bytes(int(header.GetCommandId()))
if err1 != nil {
return nil, err1
}
unproc.Check()
}
return pdu, err
} else if err != nil {
unproc.Check()
return pdu, err
}
unproc.Check()
c.messageIncompleteRetryCount = 0
return pdu, nil
}