-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmachine.go
340 lines (303 loc) · 7.43 KB
/
machine.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
package machine
import (
"fmt"
"time"
"sync"
"errors"
"net/http"
"github.com/gorilla/websocket"
"github.com/aglyzov/log15"
)
var Log = log15.New("pkg", "machine")
type State byte
type Command byte
type (
Machine struct {
URL string
Headers http.Header
Input <-chan []byte
Output chan<- []byte
Status <-chan Status
Command chan<- Command
}
Status struct {
State State
Error error
}
)
const (
// states
DISCONNECTED State = iota
CONNECTING
CONNECTED
WAITING
)
const (
// commands
QUIT Command = 16 + iota
PING
USE_TEXT
USE_BINARY
)
func init() {
// disable the logger by default
Log.SetHandler(log15.DiscardHandler())
}
func (s State) String() string {
switch s {
case DISCONNECTED: return "DISCONNECTED"
case CONNECTING: return "CONNECTING"
case CONNECTED: return "CONNECTED"
case WAITING: return "WAITING"
}
return fmt.Sprintf("UNKNOWN STATUS %v", s)
}
func (c Command) String() string {
switch c {
case QUIT: return "QUIT"
case PING: return "PING"
case USE_TEXT: return "USE_TEXT"
case USE_BINARY: return "USE_BINARY"
}
return fmt.Sprintf("UNKNOWN COMMAND %v", c)
}
func New(url string, headers http.Header) *Machine {
inp_ch := make(chan []byte, 8)
out_ch := make(chan []byte, 8)
sts_ch := make(chan Status, 2)
cmd_ch := make(chan Command, 2)
con_return_ch := make(chan *websocket.Conn, 1)
con_cancel_ch := make(chan bool, 1)
r_error_ch := make(chan error, 1)
w_error_ch := make(chan error, 1)
w_control_ch := make(chan Command, 1)
io_event_ch := make(chan bool, 2)
var wg sync.WaitGroup
connect := func() {
wg.Add(1)
defer wg.Done()
Log.Debug("connect has started")
for {
sts_ch <- Status{State:CONNECTING}
dialer := websocket.Dialer{HandshakeTimeout: 5*time.Second}
conn, _, err := dialer.Dial(url, headers)
if err == nil {
conn.SetPongHandler(func(string) error {io_event_ch <- true; return nil})
con_return_ch <- conn
sts_ch <- Status{State:CONNECTED}
return
} else {
Log.Debug("connect error", "err", err)
sts_ch <- Status{DISCONNECTED, err}
}
sts_ch <- Status{State:WAITING}
select {
case <- time.After(34*time.Second):
case <- con_cancel_ch:
sts_ch <- Status{DISCONNECTED, errors.New("cancelled")}
return
}
}
}
keep_alive := func() {
wg.Add(1)
defer wg.Done()
Log.Debug("keep_alive has started")
dur := 34 * time.Second
timer := time.NewTimer(dur)
timer.Stop()
loop:
for {
select {
case _, ok := <-io_event_ch:
if ok {
timer.Reset(dur)
} else {
timer.Stop()
break loop
}
case <-timer.C:
timer.Reset(dur)
// non-blocking PING request
select {
case w_control_ch <- PING:
default:
}
}
}
}
read := func(conn *websocket.Conn) {
wg.Add(1)
defer wg.Done()
Log.Debug("read has started")
for {
if _, msg, err := conn.ReadMessage(); err == nil {
Log.Debug("received message", "msg", string(msg))
io_event_ch <- true
inp_ch <- msg
} else {
Log.Debug("read error", "err", err)
r_error_ch <- err
break
}
}
}
write := func(conn *websocket.Conn, msg_type int) {
wg.Add(1)
defer wg.Done()
Log.Debug("write has started")
loop:
for {
select {
case msg, ok := <-out_ch:
if ok {
io_event_ch <- true
if err := conn.SetWriteDeadline(time.Now().Add(3*time.Second)); err != nil {
w_error_ch <- err
break loop
}
if err := conn.WriteMessage(msg_type, msg); err != nil {
w_error_ch <- err
break loop
}
conn.SetWriteDeadline(time.Time{}) // reset write deadline
} else {
Log.Debug("write error", "err", "out_ch closed")
w_error_ch <- errors.New("out_ch closed")
break loop
}
case cmd, ok := <-w_control_ch:
if !ok {
w_error_ch <- errors.New("w_control_ch closed")
break loop
} else {
switch cmd {
case QUIT:
Log.Debug("write received QUIT command")
w_error_ch <- errors.New("cancelled")
break loop
case PING:
if err := conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(3*time.Second)); err != nil {
Log.Debug("ping error", "err", err)
w_error_ch <- errors.New("cancelled")
break loop
}
case USE_TEXT:
msg_type = websocket.TextMessage
case USE_BINARY:
msg_type = websocket.BinaryMessage
}
}
}
}
}
go func() {
// local state
var conn *websocket.Conn
reading := false
writing := false
msg_type := websocket.BinaryMessage // use Binary messages by default
defer func() {
Log.Debug("cleanup has started")
if conn != nil {conn.Close()} // this also makes reader to exit
// close local output channels
close(con_cancel_ch) // this makes connect to exit
close(w_control_ch) // this makes write to exit
close(io_event_ch) // this makes keep_alive to exit
// drain input channels
<-time.After(50*time.Millisecond) // small pause to let things react
drain_loop:
for {
select {
case _, ok := <-out_ch:
if !ok {out_ch = nil}
case _, ok := <-cmd_ch:
if !ok {inp_ch = nil}
case conn, ok := <-con_return_ch:
if conn != nil {conn.Close()}
if !ok {con_return_ch = nil}
case _, ok := <-r_error_ch:
if !ok {r_error_ch = nil}
case _, ok := <-w_error_ch:
if !ok {w_error_ch = nil}
default:
break drain_loop
}
}
// wait for all goroutines to stop
wg.Wait()
// close output channels
close(inp_ch)
close(sts_ch)
}()
Log.Debug("main loop has started")
go connect()
go keep_alive()
main_loop:
for {
select {
case conn = <-con_return_ch:
if conn == nil {
break main_loop
}
Log.Debug("connected", "local", conn.LocalAddr(), "remote", conn.RemoteAddr())
reading = true
writing = true
go read(conn)
go write(conn, msg_type)
case err := <-r_error_ch:
reading = false
if writing {
// write goroutine is still active
Log.Debug("read error -> stopping write")
w_control_ch <- QUIT // ask write to exit
sts_ch <- Status{DISCONNECTED, err}
} else {
// both read and write goroutines have exited
Log.Debug("read error -> starting connect()")
if conn != nil {
conn.Close()
conn = nil
}
go connect()
}
case err := <-w_error_ch:
// write goroutine has exited
writing = false
if reading {
// read goroutine is still active
Log.Debug("write error -> stopping read")
if conn != nil {
conn.Close() // this also makes read to exit
conn = nil
}
sts_ch <- Status{DISCONNECTED, err}
} else {
// both read and write goroutines have exited
Log.Debug("write error -> starting connect()")
go connect()
}
case cmd, ok := <-cmd_ch:
if ok {
Log.Debug("received command", "cmd", cmd)
}
switch {
case !ok || cmd == QUIT:
if reading || writing || conn != nil {sts_ch <- Status{DISCONNECTED, nil}}
break main_loop // defer should clean everything up
case cmd == PING:
if conn != nil && writing {w_control_ch <- cmd}
case cmd == USE_TEXT:
msg_type = websocket.TextMessage
if writing {w_control_ch <- cmd}
case cmd == USE_BINARY:
msg_type = websocket.BinaryMessage
if writing {w_control_ch <- cmd}
default:
panic(fmt.Sprintf("unsupported command: %v", cmd))
}
}
}
}()
return & Machine{url, headers, inp_ch, out_ch, sts_ch, cmd_ch}
}