-
Notifications
You must be signed in to change notification settings - Fork 0
/
iofilter.go
278 lines (234 loc) · 5.24 KB
/
iofilter.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
// File IoFilter
// @Author: yandaren1220@126.com
// @Date: 2016-08-23
package gonetio
import (
"bytes"
)
type IoFilter struct {
name string // the name of the filter
handler IoHandler // the handler
next *IoFilter // the next filter
prev *IoFilter // the pre filter
conn *Tcpcon // the tcp connection
}
// set tcp con
func (flt *IoFilter) SetCon(con *Tcpcon) {
flt.conn = con
}
// get tcp con
func (flt *IoFilter) GetCon() *Tcpcon {
return flt.conn
}
// Find next in bound filter
func (flt *IoFilter) findNextInBoundFilter() *IoFilter {
next := flt.getNext()
for {
if next != nil && !next.getHandler().IsInBound() {
next = next.getNext()
} else {
break
}
}
return next
}
// Find next out bound filter
func (flt *IoFilter) findNextOutBoundFilter() *IoFilter {
prev := flt.getPrev()
for {
if prev != nil && !prev.getHandler().IsOutBound() {
prev = prev.getPrev()
} else {
break
}
}
return prev
}
// get the handler of the filter
func (flt *IoFilter) getHandler() IoHandler {
return flt.handler
}
// set the next filter
func (flt *IoFilter) setNext(filter *IoFilter) {
flt.next = filter
}
// get the next filter
func (flt *IoFilter) getNext() *IoFilter {
return flt.next
}
// set the prev filter
func (flt *IoFilter) setPrev(filter *IoFilter) {
flt.prev = filter
}
// get the previous filter
func (flt *IoFilter) getPrev() *IoFilter {
return flt.prev
}
// Connection opened
// The event fired when the server accept a new connection
// or the client client server
func (flt *IoFilter) ConnOpened() {
next := flt.findNextInBoundFilter()
if next != nil {
next.getHandler().ConnOpened(next)
}
}
// Connection closed
func (flt *IoFilter) ConnClosed() {
next := flt.findNextInBoundFilter()
if next != nil {
next.getHandler().ConnClosed(next)
}
}
// The event fired when receive message from the connection
func (flt *IoFilter) MessageReceived(obj BaseObject) {
next := flt.findNextInBoundFilter()
if next != nil {
next.getHandler().MessageReceived(next, obj)
}
}
// The event fire write
func (flt *IoFilter) FireWrite(obj BaseObject) {
next := flt.findNextOutBoundFilter()
if next != nil {
next.getHandler().FireWrite(next, obj)
}
}
// the head filter
type HeadHandler struct {
IoHandlerImp
}
// new head handler
func newHeadHandler() *HeadHandler {
handler := &HeadHandler{}
handler.SetBoundType(OutBound)
return handler
}
// Fire Write
func (hh *HeadHandler) FireWrite(filter *IoFilter, obj BaseObject) {
buffer := obj.(*bytes.Buffer)
filter.GetCon().Flush(buffer, 0)
}
// Clone
func (hh *HeadHandler) Clone() IoHandler {
return newHeadHandler()
}
// the tail filter
type TailHandler struct {
IoHandlerImp
}
// new tail handler
func newTailHandler() *TailHandler {
handler := &TailHandler{}
handler.SetBoundType(InBound)
return handler
}
// The event fired when receive message from the connection
func (th *TailHandler) MessageReceived(filter *IoFilter, obj BaseObject) {
// todo clear the buffer
buffer := obj.(*bytes.Buffer)
buffer.Reset()
}
// Clone
func (th *TailHandler) Clone() IoHandler {
return newTailHandler()
}
type IoFilterChain struct {
conn *Tcpcon // the connection
head *IoFilter // the head io filter
tail *IoFilter // the tail io filter
}
// New IoFilter Chain Instance
func NewIoFilterChain(con *Tcpcon) *IoFilterChain {
headFilter := &IoFilter{
name: "head",
handler: newHeadHandler(),
next: nil,
prev: nil,
conn: con,
}
tailFilter := &IoFilter{
name: "tail",
handler: newTailHandler(),
next: nil,
prev: nil,
conn: con,
}
headFilter.setNext(tailFilter)
tailFilter.setPrev(headFilter)
return &IoFilterChain{
conn: con,
head: headFilter,
tail: tailFilter,
}
}
// Clone
func (fc *IoFilterChain) NewInstanceAndClone(con *Tcpcon) *IoFilterChain {
chain := NewIoFilterChain(con)
filter := fc.head.getNext()
for {
if filter != fc.tail {
chain.AddLast(filter.name, filter.getHandler().Clone())
filter = filter.getNext()
} else {
break
}
}
return chain
}
// get head
func (fc *IoFilterChain) GetHeadFilter() *IoFilter {
return fc.head
}
// get tail
func (fc *IoFilterChain) GetTailFilter() *IoFilter {
return fc.tail
}
// get the connection
func (fc *IoFilterChain) getCon() *Tcpcon {
return fc.conn
}
// set the connction
func (fc *IoFilterChain) setCon(con *Tcpcon) {
filter := fc.head
for {
if filter != nil {
filter.SetCon(con)
filter = filter.getNext()
} else {
break
}
}
fc.conn = con
}
// add last
// add the filter to the last
func (fc *IoFilterChain) AddLast(filterName string, ioHandler IoHandler) {
filter := &IoFilter{
name: filterName,
handler: ioHandler,
next: nil,
prev: nil,
conn: fc.conn,
}
filter.setPrev(fc.tail.getPrev())
filter.setNext(fc.tail)
fc.tail.getPrev().setNext(filter)
fc.tail.setPrev(filter)
}
// fire the connection opened event at the chain
func (fc *IoFilterChain) FireConnOpened() {
fc.head.ConnOpened()
}
// Connection closed
func (fc *IoFilterChain) FireConnClosed() {
fc.head.ConnClosed()
}
// The event fired when receive message from the connection
func (fc *IoFilterChain) FireMessageReceived(obj BaseObject) {
fc.head.MessageReceived(obj)
}
// Fire Write
func (fc *IoFilterChain) FireWrite(obj BaseObject) {
fc.tail.FireWrite(obj)
}