-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol_handle_stream.go
247 lines (209 loc) · 5.97 KB
/
protocol_handle_stream.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
package tentacle
import (
"sync/atomic"
"time"
)
const (
serviceProtocolInit uint = iota
serviceProtocolConnected
serviceProtocolDisconnected
serviceProtocolReceived
serviceProtocolSetNotify
serviceProtocolRemoveNotify
serviceProtocolNotify
)
// As a firm believer in the type system, this is the last stubborn stand against the Go type!
type serviceProtocolEvent struct {
tag uint
event interface{}
}
type serviceProtocolConnectedInner struct {
context *SessionContext
version string
}
type serviceProtocolReceivedInner struct {
id SessionID
data []byte
}
type protocolSetNotifyInner struct {
interval time.Duration
token uint64
}
type serviceProtocolStream struct {
handle ServiceProtocol
handleContext ProtocolContext
sessions map[SessionID]*SessionContext
notifys map[uint64]time.Duration
shutdown *atomic.Value
eventReceiver <-chan serviceProtocolEvent
notifyChan chan uint64
reportChan chan<- sessionEvent
}
func (s *serviceProtocolStream) run() {
// In theory, this value will not appear, but if it does, it means that the channel was accidentally closed.
closed := func(ok bool) bool {
if !ok {
s.shutdown.Store(true)
return true
}
return false
}
for {
if s.shutdown.Load().(bool) {
defer protectRun(func() { close(s.notifyChan) }, nil)
break
}
select {
case event, ok := <-s.eventReceiver:
if closed(ok) {
continue
}
s.handleEvent(event)
case token, ok := <-s.notifyChan:
if closed(ok) {
continue
}
s.handleEvent(serviceProtocolEvent{tag: serviceProtocolNotify, event: token})
}
}
}
func (s *serviceProtocolStream) handleEvent(event serviceProtocolEvent) {
reportFn := func(sid SessionID) func() {
return func() {
s.reportChan <- sessionEvent{tag: protocolHandleError, event: protocolHandleErrorInner{pid: s.handleContext.Pid, sid: sid}}
}
}
switch event.tag {
case serviceProtocolInit:
protectRun(func() { s.handle.Init(&s.handleContext) }, reportFn(SessionID(0)))
case serviceProtocolConnected:
sessioninfo := event.event.(serviceProtocolConnectedInner)
protectRun(func() { s.handle.Connected(s.handleContext.toRef(sessioninfo.context), sessioninfo.version) }, reportFn(sessioninfo.context.Sid))
s.sessions[sessioninfo.context.Sid] = sessioninfo.context
case serviceProtocolDisconnected:
id := event.event.(SessionID)
sessionctx, ok := s.sessions[id]
if !ok {
return
}
protectRun(func() { s.handle.Disconnected(s.handleContext.toRef(sessionctx)) }, reportFn(sessionctx.Sid))
delete(s.sessions, id)
case serviceProtocolReceived:
data := event.event.(serviceProtocolReceivedInner)
sessionctx, ok := s.sessions[data.id]
if !ok {
return
}
protectRun(func() { s.handle.Received(s.handleContext.toRef(sessionctx), data.data) }, reportFn(sessionctx.Sid))
case serviceProtocolNotify:
token := event.event.(uint64)
protectRun(func() { s.handle.Notify(&s.handleContext, token) }, reportFn(SessionID(0)))
s.setNotify(token)
case serviceProtocolSetNotify:
notify := event.event.(protocolSetNotifyInner)
s.notifys[notify.token] = notify.interval
s.setNotify(notify.token)
case serviceProtocolRemoveNotify:
token := event.event.(uint64)
delete(s.notifys, token)
}
}
func (s *serviceProtocolStream) setNotify(token uint64) {
interval, ok := s.notifys[token]
if !ok {
return
}
go func() {
<-time.After(interval)
protectRun(func() { s.notifyChan <- token }, nil)
}()
}
const (
sessionProtocolOpened uint = iota
sessionProtocolClosed
sessionProtocolDisconnected
sessionProtocolReceived
sessionProtocolSetNotify
sessionProtocolRemoveNotify
sessionProtocolNotify
)
// As a firm believer in the type system, this is the last stubborn stand against the Go type!
type sessionProtocolEvent struct {
tag uint
event interface{}
}
type sessionProtocolStream struct {
handle SessionProtocol
handleContext ProtocolContext
context *SessionContext
notifys map[uint64]time.Duration
shutdown bool
eventReceiver <-chan sessionProtocolEvent
notifyChan chan uint64
reportChan chan<- sessionEvent
}
func (s *sessionProtocolStream) run() {
// In theory, this value will not appear, but if it does, it means that the channel was accidentally closed.
closed := func(ok bool) bool {
if !ok {
s.shutdown = true
return true
}
return false
}
for {
if s.shutdown || s.context.closed.Load().(bool) {
break
}
select {
case event, ok := <-s.eventReceiver:
if closed(ok) {
continue
}
s.handleEvent(event)
case token, ok := <-s.notifyChan:
if closed(ok) {
continue
}
s.handleEvent(sessionProtocolEvent{tag: sessionProtocolNotify, event: token})
}
}
}
func (s *sessionProtocolStream) handleEvent(event sessionProtocolEvent) {
reportFn := func() {
s.reportChan <- sessionEvent{tag: protocolHandleError, event: protocolHandleErrorInner{sid: s.context.Sid, pid: s.handleContext.Pid}}
}
switch event.tag {
case sessionProtocolOpened:
version := event.event.(string)
protectRun(func() { s.handle.Connected(s.handleContext.toRef(s.context), version) }, reportFn)
case sessionProtocolClosed:
protectRun(func() { s.handle.Disconnected(s.handleContext.toRef(s.context)) }, reportFn)
case sessionProtocolDisconnected:
s.shutdown = true
case sessionProtocolReceived:
data := event.event.([]byte)
protectRun(func() { s.handle.Received(s.handleContext.toRef(s.context), data) }, reportFn)
case sessionProtocolNotify:
token := event.event.(uint64)
s.handle.Notify(s.handleContext.toRef(s.context), token)
s.setNotify(token)
case sessionProtocolSetNotify:
notify := event.event.(protocolSetNotifyInner)
s.notifys[notify.token] = notify.interval
s.setNotify(notify.token)
case sessionProtocolRemoveNotify:
token := event.event.(uint64)
delete(s.notifys, token)
}
}
func (s *sessionProtocolStream) setNotify(token uint64) {
interval, ok := s.notifys[token]
if !ok {
return
}
go func() {
<-time.After(interval)
protectRun(func() { s.notifyChan <- token }, nil)
}()
}