-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
371 lines (315 loc) · 8.2 KB
/
client.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package socket
import (
"errors"
"fmt"
"github.com/omzlo/clog"
"github.com/omzlo/go-sscp"
"sync"
"time"
)
const (
HELLO_MAJOR = 2
HELLO_MINOR = 0
)
type EventCallback func(*EventConn, Eventer) error
/****************************************************************************/
type EventRequest struct {
ResponseCallback func(*EventConn, error) error
CreatedAt time.Time
}
// EventConn
//
//
type EventConn struct {
Conn *sscp.Conn
Addr string
ClientName string
AuthToken string
Connected bool
AutoRedial bool
MsgId uint16
Callbacks map[EventId]EventCallback
processConnect func(*EventConn) error
Mutex sync.Mutex
pendingRequests map[uint16]*EventRequest
endOfReadChannel chan error
terminationChannel chan error
eventChannel chan Eventer
dialCount int
}
/* Some key logic in EventConn:
1) There is only one goroutine that reads messages from an EventConn.
- This is either the dial() function or the conn.readEventLoop() goroutine called from dial()
- dial() may be called several times if auto-redial is enabled.
- However, there is always a call to Close() between calls to dial()
- Close() always waits for conn.readEventLoop() to end.
- Hence there are never 2 conn.readEventLoop() goroutines running in parallel.
2) There is only one goroutine that can writes messages to an EventConn at a time.
- dial() and SendAsync() are the only functions that write to an EventConn.
- SendAsync() uses a mutex to avoid concurrent writes to a conn
- SendAsync() does not allow writes when conn.Connect is false
- dial() will perform a write when conn.Connect is false
- Hence dial() and SendAsync() do not overlap
*/
func defaultProcessConnect(conn *EventConn) error {
return nil
}
var Terminate = errors.New("Client termination request")
var TimeoutError = errors.New("Event response timeout")
func NewEventConn(addr string, client_name string, auth string) *EventConn {
if addr == "" {
addr = ":4242"
}
return &EventConn{Addr: addr,
ClientName: client_name,
AuthToken: auth,
Connected: false,
AutoRedial: false,
MsgId: 1,
Callbacks: make(map[EventId]EventCallback),
processConnect: defaultProcessConnect,
pendingRequests: make(map[uint16]*EventRequest),
endOfReadChannel: make(chan error, 1),
terminationChannel: make(chan error, 1),
eventChannel: make(chan Eventer, 32),
dialCount: 0,
}
}
func (conn *EventConn) dial() error {
sscp_conn, err := sscp.Dial("tcp", conn.Addr, []byte(conn.ClientName), []byte(conn.AuthToken))
if err != nil {
return err
}
conn.Conn = sscp_conn
conn.MsgId = 1
event := NewClientHelloEvent(conn.ClientName, HELLO_MAJOR, HELLO_MINOR)
event.SetMsgId(conn.MsgId)
if err = EncodeEvent(conn.Conn, event); err != nil {
conn.Close()
return err
}
conn.MsgId++
response, err := DecodeEvent(conn.Conn)
if err != nil {
conn.Close()
return err
}
if response.Id() != ServerHelloEventId {
conn.Close()
return fmt.Errorf("Expected ServerHelloEvent, got %d (%s)", response.Id(), response.Id())
}
if response.MsgId() != event.MsgId() {
conn.Close()
return fmt.Errorf("Message Id mismatch on ServerHelloEvent %d != %d", response.MsgId(), event.MsgId())
}
conn.Connected = true
go conn.readEventLoop()
conn.dialCount++
if conn.dialCount == 1 {
go conn.processEventLoop()
}
clog.Debug("Opened connection %d to %s from %s", conn.dialCount, conn.Conn.RemoteAddr(), conn.Conn.LocalAddr())
return conn.processConnect(conn)
}
func (conn *EventConn) OnEvent(eid EventId, cb EventCallback) {
if cb == nil {
delete(conn.Callbacks, eid)
} else {
conn.Callbacks[eid] = cb
}
}
func (conn *EventConn) OnConnect(cb func(*EventConn) error) {
if cb == nil {
conn.processConnect = defaultProcessConnect
} else {
conn.processConnect = cb
}
}
func (conn *EventConn) SendAsync(request Eventer, cb func(*EventConn, error) error) {
conn.Mutex.Lock()
defer conn.Mutex.Unlock()
if !conn.Connected {
err := errors.New("Sending event on a closed connection.")
cb(conn, err)
return
}
if conn.MsgId == 0 {
conn.MsgId = 1
}
request.SetMsgId(conn.MsgId)
conn.pendingRequests[conn.MsgId] = &EventRequest{ResponseCallback: cb, CreatedAt: time.Now()}
if err := EncodeEvent(conn.Conn, request); err != nil {
delete(conn.pendingRequests, request.MsgId())
cb(conn, err)
return
}
conn.MsgId++
}
func (conn *EventConn) Send(request Eventer) error {
msg_id := request.MsgId()
conn.SendAsync(request, func(econn *EventConn, ee error) error {
return ee
})
for {
e_id, err := conn.processNextEvent()
if e_id == msg_id || err != nil {
return err
}
}
}
func (conn *EventConn) WaitTermination(duration time.Duration) error {
if duration == 0 {
err := <-conn.terminationChannel
if err != Terminate {
return err
}
return nil
}
timeout := time.NewTimer(duration)
select {
case response := <-conn.terminationChannel:
if !timeout.Stop() {
<-timeout.C
}
if response != Terminate {
return response
}
return nil
case <-timeout.C:
return TimeoutError
}
}
func (conn *EventConn) Terminate() error {
conn.AutoRedial = false
return conn.Close()
}
func (conn *EventConn) Close() error {
// TODO: clean pending requests
//clog.DebugXX("Closing connection %d to %s", conn.dialCount, conn.Conn.RemoteAddr())
conn.Connected = false
conn.Conn.Close()
// Wait for read-side close
select {
case res := <-conn.endOfReadChannel:
clog.Debug("Closed connection %d to %s: %s", conn.dialCount, conn.Conn.RemoteAddr(), res)
return res
case <-time.After(5 * time.Second):
clog.Error("Stalled while waiting for connection %d to close on read side. Please report this error.", conn.dialCount)
return fmt.Errorf("Stalled while waiting for connection to close on the read side")
}
}
func (conn *EventConn) Connect() error {
return conn.dial()
}
func (conn *EventConn) EnableAutoRedial() *EventConn {
conn.AutoRedial = true
return conn
}
/*
func (conn *EventConn) cancelRequest(request Eventer) {
er := conn.pendingRequests[request.MsgId()]
if er != nil {
}
}
*/
func (conn *EventConn) readEventLoop() {
for {
event, err := DecodeEvent(conn.Conn)
if err != nil {
conn.endOfReadChannel <- err
return
}
conn.eventChannel <- event
}
}
func (conn *EventConn) processNextEvent() (uint16, error) {
event, more := <-conn.eventChannel
if !more {
return 0, nil
}
msg_id := event.MsgId()
if msg_id == 0 {
cb := conn.Callbacks[event.Id()]
if cb != nil {
err := cb(conn, event)
if err != nil {
return 0, err
}
}
return 0, nil
}
conn.Mutex.Lock()
er := conn.pendingRequests[msg_id]
if er != nil {
delete(conn.pendingRequests, msg_id)
}
conn.Mutex.Unlock()
if er == nil {
return msg_id, fmt.Errorf("Unexpected sequence number %d in response event", msg_id)
}
if err := er.ResponseCallback(conn, nil); err != nil {
return msg_id, err
}
conn.Mutex.Lock()
var expired_id uint16 = 0
var expired_er *EventRequest = nil
for event_id, er := range conn.pendingRequests {
if time.Since(er.CreatedAt) > 3*time.Second {
delete(conn.pendingRequests, event_id)
expired_er = er
expired_id = event_id
break
}
}
conn.Mutex.Unlock()
if expired_er != nil {
if err := er.ResponseCallback(conn, TimeoutError); err != nil {
return expired_id, err
}
}
return 0, nil
}
func (conn *EventConn) processEventLoop() {
var err error
backoff := 1
for {
/* Reconnect automatically if needed. Auto-redial defines the behaviour */
if !conn.Connected {
err = conn.dial()
if err != nil {
if !conn.AutoRedial {
break
}
time.Sleep(time.Duration(backoff) * time.Second)
if backoff < 8 {
backoff *= 2
}
continue
}
backoff = 1
}
/* Process events */
_, err = conn.processNextEvent()
if err != nil {
conn.Close()
if err == Terminate {
break
}
if !conn.AutoRedial {
break
}
continue
}
}
clog.Debug("Ended event loop")
conn.terminationChannel <- err
}
func ReturnErrorOrContinue(c *EventConn, e error) error {
return e
}
func ReturnErrorOrTerminate(c *EventConn, e error) error {
if e == nil {
return Terminate
}
return e
}