forked from go-routeros/routeros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.go
85 lines (73 loc) · 1.47 KB
/
async.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
package routeros
import "github.com/go-routeros/routeros/proto"
type sentenceProcessor interface {
processSentence(sen *proto.Sentence) (bool, error)
}
type replyCloser interface {
close(err error)
}
// Async starts asynchronous mode and returns immediately.
func (c *Client) Async() <-chan error {
c.mu.Lock()
defer c.mu.Unlock()
errC := make(chan error, 1)
if c.async {
errC <- errAlreadyAsync
close(errC)
return errC
}
c.async = true
c.tags = make(map[string]sentenceProcessor)
go c.asyncLoopChan(errC)
return errC
}
func (c *Client) asyncLoopChan(errC chan<- error) {
defer close(errC)
// If c.Close() has been called, c.closing will be true, and
// err will be “use of closed network connection”. Ignore that error.
err := c.asyncLoop()
if err != nil {
c.mu.Lock()
closing := c.closing
c.mu.Unlock()
if !closing {
errC <- err
}
}
}
func (c *Client) asyncLoop() error {
for {
sen, err := c.r.ReadSentence()
if err != nil {
c.closeTags(err)
return err
}
c.mu.Lock()
r, ok := c.tags[sen.Tag]
c.mu.Unlock()
if !ok {
continue
}
done, err := r.processSentence(sen)
if done || err != nil {
c.mu.Lock()
delete(c.tags, sen.Tag)
c.mu.Unlock()
closeReply(r, err)
}
}
}
func (c *Client) closeTags(err error) {
c.mu.Lock()
defer c.mu.Unlock()
for _, r := range c.tags {
closeReply(r, err)
}
c.tags = nil
}
func closeReply(r sentenceProcessor, err error) {
rr, ok := r.(replyCloser)
if ok {
rr.close(err)
}
}