forked from go-netty/go-netty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
260 lines (213 loc) · 5.11 KB
/
context.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
/*
* Copyright 2019 the go-netty project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package netty
type (
// HandlerContext defines a base handler context
HandlerContext interface {
Channel() Channel
Handler() Handler
Write(message Message)
Trigger(event Event)
Close(err error)
Attachment() Attachment
SetAttachment(Attachment)
}
// ActiveContext defines an active handler
ActiveContext interface {
HandlerContext
HandleActive()
}
// InboundContext defines an inbound handler
InboundContext interface {
HandlerContext
HandleRead(message Message)
}
// OutboundContext defines an outbound handler
OutboundContext interface {
HandlerContext
HandleWrite(message Message)
}
// ExceptionContext defines an exception handler
ExceptionContext interface {
HandlerContext
HandleException(ex Exception)
}
// InactiveContext defines an inactive handler
InactiveContext interface {
HandlerContext
HandleInactive(ex Exception)
}
// EventContext defines an event handler
EventContext interface {
HandlerContext
HandleEvent(event Event)
}
)
// handlerContext impl HandlerContext
type handlerContext struct {
pipeline Pipeline
handler Handler
prev *handlerContext
next *handlerContext
cast2Active ActiveHandler
cast2Inbound InboundHandler
cast2Outbound OutboundHandler
cast2Exception ExceptionHandler
cast2Inactive InactiveHandler
cast2Event EventHandler
}
func newHandlerContext(p Pipeline, handler Handler, prev, next *handlerContext) *handlerContext {
hc := &handlerContext{
pipeline: p,
handler: handler,
prev: prev,
next: next,
}
hc.cast2Active, _ = handler.(ActiveHandler)
hc.cast2Inbound, _ = handler.(InboundHandler)
hc.cast2Outbound, _ = handler.(OutboundHandler)
hc.cast2Exception, _ = handler.(ExceptionHandler)
hc.cast2Inactive, _ = handler.(InactiveHandler)
hc.cast2Event, _ = handler.(EventHandler)
return hc
}
func (hc *handlerContext) prevContext() *handlerContext {
return hc.prev
}
func (hc *handlerContext) nextContext() *handlerContext {
return hc.next
}
func (hc *handlerContext) Write(message Message) {
defer func() {
if err := recover(); nil != err {
hc.Channel().Pipeline().FireChannelException(AsException(err))
}
}()
var next = hc
for {
if next = next.prevContext(); nil == next {
break
}
if handler := next.cast2Outbound; nil != handler {
handler.HandleWrite(next, message)
break
}
}
}
func (hc *handlerContext) Trigger(event Event) {
defer func() {
if err := recover(); nil != err {
hc.Channel().Pipeline().FireChannelException(AsException(err))
}
}()
var next = hc
for {
if next = next.nextContext(); nil == next {
break
}
if handler := next.cast2Event; nil != handler {
handler.HandleEvent(next, event)
break
}
}
}
func (hc *handlerContext) Close(err error) {
hc.Channel().Close(err)
}
func (hc *handlerContext) Channel() Channel {
return hc.pipeline.Channel()
}
func (hc *handlerContext) Handler() Handler {
return hc.handler
}
func (hc *handlerContext) Attachment() Attachment {
return hc.Channel().Attachment()
}
func (hc *handlerContext) SetAttachment(v Attachment) {
hc.Channel().SetAttachment(v)
}
func (hc *handlerContext) HandleActive() {
var next = hc
for {
if next = next.nextContext(); nil == next {
break
}
if handler := next.cast2Active; nil != handler {
handler.HandleActive(next)
break
}
}
}
func (hc *handlerContext) HandleRead(message Message) {
var next = hc
for {
if next = next.nextContext(); nil == next {
break
}
if handler := next.cast2Inbound; nil != handler {
handler.HandleRead(next, message)
break
}
}
}
func (hc *handlerContext) HandleWrite(message Message) {
var prev = hc
for {
if prev = prev.prevContext(); nil == prev {
break
}
if handler := prev.cast2Outbound; nil != handler {
handler.HandleWrite(prev, message)
break
}
}
}
func (hc *handlerContext) HandleException(ex Exception) {
var next = hc
for {
if next = next.nextContext(); nil == next {
break
}
if handler := next.cast2Exception; nil != handler {
handler.HandleException(next, ex)
break
}
}
}
func (hc *handlerContext) HandleInactive(ex Exception) {
var next = hc
for {
if next = next.nextContext(); nil == next {
break
}
if handler := next.cast2Inactive; nil != handler {
handler.HandleInactive(next, ex)
break
}
}
}
func (hc *handlerContext) HandleEvent(event Event) {
var next = hc
for {
if next = next.nextContext(); nil == next {
break
}
if handler := next.cast2Event; nil != handler {
handler.HandleEvent(next, event)
break
}
}
}