-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
184 lines (156 loc) · 3.08 KB
/
service.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
package smux
import (
"errors"
"sync"
)
var (
ErrServiceClosed = errors.New("aioService is closed. ")
)
type Event byte
// Event poller 返回事件值
const (
EV_READ Event = 1 << 1
EV_WRITE Event = 1 << 2
EV_ERROR Event = 1 << 3
)
func (e Event) Readable() bool {
return e&EV_READ != 0 || e&EV_ERROR != 0
}
func (e Event) Writable() bool {
return e&EV_WRITE != 0 || e&EV_ERROR != 0
}
type AIOService struct {
mux *MuxSession
fd2Callback map[uint16]func(event Event)
fdLock sync.RWMutex
taskQueue chan *task
die chan struct{}
dieOnce sync.Once
}
type task struct {
fd uint16
event Event
}
func (this *AIOService) appendEvent(fd uint16, event Event) {
this.fdLock.RLock()
defer this.fdLock.RUnlock()
if _, ok := this.fd2Callback[fd]; ok {
t := &task{fd: fd, event: event}
this.taskQueue <- t
}
}
func (this *AIOService) trigger(fd uint16) {
stream := this.mux.GetMuxConn(fd)
if stream != nil {
var event Event
if _, ok := stream.Readable(); ok {
event |= EV_READ
}
if _, ok := stream.Writable(); ok {
event |= EV_WRITE
}
if event != 0 {
this.appendEvent(fd, event)
}
}
}
func (this *AIOService) Watch(fd uint16, callback func(event Event)) error {
if this.isClosed() {
return ErrServiceClosed
}
this.fdLock.Lock()
if _, ok := this.fd2Callback[fd]; !ok {
this.fd2Callback[fd] = callback
this.fdLock.Unlock()
this.trigger(fd)
} else {
this.fdLock.Unlock()
}
return nil
}
func (this *AIOService) Unwatch(fd uint16) {
this.fdLock.Lock()
defer this.fdLock.Unlock()
delete(this.fd2Callback, fd)
}
func (this *AIOService) Close() {
this.dieOnce.Do(func() {
close(this.die)
this.mux.aioServiceLocker.Lock()
this.mux.aioService = nil
this.mux.aioServiceLocker.Unlock()
this.mux = nil
})
}
func (this *AIOService) close() {
this.dieOnce.Do(func() {
close(this.die)
this.mux.aioService = nil
this.mux = nil
})
}
func (this *AIOService) isClosed() bool {
select {
case <-this.die:
return true
default:
return false
}
}
func OpenAIOService(mux *MuxSession, worker int) *AIOService {
mux.aioServiceLocker.Lock()
defer mux.aioServiceLocker.Unlock()
s := &AIOService{
mux: mux,
fd2Callback: map[uint16]func(event Event){},
fdLock: sync.RWMutex{},
taskQueue: make(chan *task, 1024),
}
mux.aioService = s
if mux.IsClosed() {
s.close()
return s
}
if worker <= 0 {
worker = 1
}
for i := 0; i < worker; i++ {
go func() {
for {
select {
case <-s.die:
return
case t := <-s.taskQueue:
s.fdLock.Lock()
callback, ok := s.fd2Callback[t.fd]
if ok {
s.fdLock.Unlock()
callback(t.event)
} else {
s.fdLock.Unlock()
}
}
}
}()
}
return s
}
func (this *MuxSession) preparseCmd(fd uint16, cmd byte) {
this.aioServiceLocker.Lock()
defer this.aioServiceLocker.Unlock()
if this.aioService == nil || this.aioService.isClosed() {
return
}
var event Event
switch cmd {
case cmdPSH:
event = EV_READ
case cmdCFM:
event = EV_WRITE
case cmdFIN:
event = EV_ERROR
default:
return
}
this.aioService.appendEvent(fd, event)
}