11local socket = require " bee.socket"
2+ local select = require " bee.select"
3+ local selector = select .create ()
4+ local SELECT_READ <const> = select .SELECT_READ
5+ local SELECT_WRITE <const> = select .SELECT_WRITE
26
3- local readfds = {}
4- local writefds = {}
5- local map = {}
6-
7- local function FD_SET (set , fd )
8- for i = 1 , # set do
9- if fd == set [i ] then
10- return
11- end
7+ local function fd_set_read (s )
8+ if s ._flags & SELECT_READ ~= 0 then
9+ return
1210 end
13- set [# set + 1 ] = fd
11+ s ._flags = s ._flags | SELECT_READ
12+ selector :event_mod (s ._fd , s ._flags )
1413end
1514
16- local function FD_CLR (set , fd )
17- for i = 1 , # set do
18- if fd == set [i ] then
19- set [i ] = set [# set ]
20- set [# set ] = nil
21- return
22- end
15+ local function fd_clr_read (s )
16+ if s ._flags & SELECT_READ == 0 then
17+ return
2318 end
19+ s ._flags = s ._flags & (~SELECT_READ )
20+ selector :event_mod (s ._fd , s ._flags )
2421end
2522
26- local function fd_set_read (fd )
27- FD_SET (readfds , fd )
28- end
29-
30- local function fd_clr_read (fd )
31- FD_CLR (readfds , fd )
32- end
33-
34- local function fd_set_write (fd )
35- FD_SET (writefds , fd )
23+ local function fd_set_write (s )
24+ if s ._flags & SELECT_WRITE ~= 0 then
25+ return
26+ end
27+ s ._flags = s ._flags | SELECT_WRITE
28+ selector :event_mod (s ._fd , s ._flags )
3629end
3730
38- local function fd_clr_write (fd )
39- FD_CLR (writefds , fd )
31+ local function fd_clr_write (s )
32+ if s ._flags & SELECT_WRITE == 0 then
33+ return
34+ end
35+ s ._flags = s ._flags & (~SELECT_WRITE )
36+ selector :event_mod (s ._fd , s ._flags )
4037end
4138
4239local function on_event (self , name , ...)
4946local function close (self )
5047 local fd = self ._fd
5148 on_event (self , " close" )
49+ selector :event_del (fd )
5250 fd :close ()
53- map [fd ] = nil
5451end
5552
5653local stream_mt = {}
@@ -69,7 +66,7 @@ function stream:write(data)
6966 return
7067 end
7168 if self ._writebuf == " " then
72- fd_set_write (self . _fd )
69+ fd_set_write (self )
7370 end
7471 self ._writebuf = self ._writebuf .. data
7572end
7976function stream :close ()
8077 if not self .shutdown_r then
8178 self .shutdown_r = true
82- fd_clr_read (self . _fd )
79+ fd_clr_read (self )
8380 end
8481 if self .shutdown_w or self ._writebuf == " " then
8582 self .shutdown_w = true
86- fd_clr_write (self . _fd )
83+ fd_clr_write (self )
8784 close (self )
8885 end
8986end
90- function stream :update (timeout )
91- local fd = self ._fd
92- local r = {fd }
93- local w = r
94- if self ._writebuf == " " then
95- w = nil
96- end
97- local rd , wr = socket .select (r , w , timeout or 0 )
98- if rd then
99- if # rd > 0 then
100- self :select_r ()
101- end
102- if # wr > 0 then
103- self :select_w ()
104- end
105- end
106- end
10787local function close_write (self )
108- fd_clr_write (self . _fd )
88+ fd_clr_write (self )
10989 if self .shutdown_r then
110- fd_clr_read (self ._fd )
11190 close (self )
11291 end
11392end
@@ -133,26 +112,43 @@ function stream:select_w()
133112 end
134113 end
135114end
115+ local function update_stream (s , event )
116+ if event & SELECT_READ ~= 0 then
117+ s :select_r ()
118+ end
119+ if event & SELECT_WRITE ~= 0 then
120+ s :select_w ()
121+ end
122+ end
136123
137124local function accept_stream (fd )
138- local self = setmetatable ({
125+ local s = setmetatable ({
139126 _fd = fd ,
127+ _flags = SELECT_READ ,
140128 _event = {},
141129 _writebuf = " " ,
142130 shutdown_r = false ,
143131 shutdown_w = false ,
144132 }, stream_mt )
145- map [fd ] = self
146- fd_set_read (fd )
147- return self
148- end
149- local function connect_stream (self )
150- setmetatable (self , stream_mt )
151- fd_set_read (self ._fd )
152- if self ._writebuf ~= " " then
153- self :select_w ()
133+ selector :event_add (fd , SELECT_READ , function (event )
134+ update_stream (s , event )
135+ end )
136+ return s
137+ end
138+ local function connect_stream (s )
139+ setmetatable (s , stream_mt )
140+ selector :event_del (s ._fd )
141+ if s ._writebuf ~= " " then
142+ s ._flags = SELECT_READ | SELECT_WRITE
143+ selector :event_add (s ._fd , SELECT_READ | SELECT_WRITE , function (event )
144+ update_stream (s , event )
145+ end )
146+ s :select_w ()
154147 else
155- fd_clr_write (self ._fd )
148+ s ._flags = SELECT_READ
149+ selector :event_add (s ._fd , SELECT_READ , function (event )
150+ update_stream (s , event )
151+ end )
156152 end
157153end
158154
@@ -170,35 +166,32 @@ function listen:is_closed()
170166end
171167function listen :close ()
172168 self .shutdown_r = true
173- fd_clr_read (self ._fd )
174169 close (self )
175170end
176- function listen :update (timeout )
177- local fd = self ._fd
178- local r = {fd }
179- local rd = socket .select (r , nil , timeout or 0 )
180- if rd then
181- if # rd > 0 then
182- self :select_r ()
183- end
184- end
185- end
186- function listen :select_r ()
187- local newfd = self ._fd :accept ()
188- if newfd :status () then
189- local news = accept_stream (newfd )
190- on_event (self , " accept" , news )
191- end
192- end
193171local function new_listen (fd )
194172 local s = {
195173 _fd = fd ,
174+ _flags = SELECT_READ ,
196175 _event = {},
197176 shutdown_r = false ,
198177 shutdown_w = true ,
199178 }
200- map [fd ] = s
201- fd_set_read (fd )
179+ selector :event_add (fd , SELECT_READ , function ()
180+ local newfd , err = fd :accept ()
181+ if not newfd then
182+ on_event (s , " error" , err )
183+ return
184+ end
185+ local ok , err = newfd :status ()
186+ if not ok then
187+ on_event (s , " error" , err )
188+ return
189+ end
190+ if newfd :status () then
191+ local news = accept_stream (newfd )
192+ on_event (s , " accept" , news )
193+ end
194+ end )
202195 return setmetatable (s , listen_mt )
203196end
204197
@@ -221,39 +214,27 @@ function connect:is_closed()
221214end
222215function connect :close ()
223216 self .shutdown_w = true
224- fd_clr_write (self ._fd )
225217 close (self )
226218end
227- function connect :update (timeout )
228- local fd = self ._fd
229- local w = {fd }
230- local rd , wr = socket .select (nil , w , timeout or 0 )
231- if rd then
232- if # wr > 0 then
233- self :select_w ()
234- end
235- end
236- end
237- function connect :select_w ()
238- local ok , err = self ._fd :status ()
239- if ok then
240- connect_stream (self )
241- on_event (self , " connect" )
242- else
243- on_event (self , " error" , err )
244- self :close ()
245- end
246- end
247219local function new_connect (fd )
248220 local s = {
249221 _fd = fd ,
222+ _flags = SELECT_WRITE ,
250223 _event = {},
251224 _writebuf = " " ,
252225 shutdown_r = false ,
253226 shutdown_w = false ,
254227 }
255- map [fd ] = s
256- fd_set_write (fd )
228+ selector :event_add (fd , SELECT_WRITE , function ()
229+ local ok , err = fd :status ()
230+ if ok then
231+ connect_stream (s )
232+ on_event (s , " connect" )
233+ else
234+ on_event (s , " error" , err )
235+ s :close ()
236+ end
237+ end )
257238 return setmetatable (s , connect_mt )
258239end
259240
@@ -293,18 +274,8 @@ function m.connect(protocol, ...)
293274end
294275
295276function m .update (timeout )
296- local rd , wr = socket .select (readfds , writefds , timeout or 0 )
297- if rd then
298- for i = 1 , # rd do
299- local fd = rd [i ]
300- local s = map [fd ]
301- s :select_r ()
302- end
303- for i = 1 , # wr do
304- local fd = wr [i ]
305- local s = map [fd ]
306- s :select_w ()
307- end
277+ for func , event in selector :wait (timeout or 0 ) do
278+ func (event )
308279 end
309280end
310281
0 commit comments