-
Notifications
You must be signed in to change notification settings - Fork 1
/
scr.lua
398 lines (344 loc) · 9.46 KB
/
scr.lua
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: scr.lua $
--
-- Author: Markus Stenberg <markus [email protected]>
--
-- Copyright (c) 2013 cisco Systems, Inc.
--
-- Created: Thu Apr 25 10:13:25 2013 mstenber
-- Last modified: Sat Oct 19 01:46:38 2013 mstenber
-- Edit time: 164 min
--
-- coroutine event reactor - coroutine based handling of file
-- descriptors (or something else). All we care about is that the
-- 'blocking' returns a set of functions, which return true (at some
-- point).
-- Using (say) select or something on the side to handle when to call
-- scr:poll() is advisable, of course.
-- Basic idea:
-- - coroutines are started to 'do stuff'
-- - coroutines can yield at any time, with yield value consisting of
-- callbacks
-- - coroutine is resumed with the return value of callback(s), when
-- one of them returns non-nil
require 'mst'
require 'ssloop'
require 'scbtcp'
module(..., package.seeall)
-- main reactor class
scr = mst.create_class{class='scr'}
function scr:init()
-- pending is just list of coroutine objects we need to call
self.pending = {}
-- blocked consists of co, <block criteria>
self.blocked = {}
end
function scr:repr_data()
return mst.repr{pending=#self.pending,
blocked=#self.blocked}
end
function scr:run(f, ...)
local co = coroutine.create(f)
table.insert(self.pending, {co, ...})
return co
end
function scr:resume_pending()
local i = #self.pending
if i == 0
then
return
end
local a = table.remove(self.pending, i)
self:d('resuming', a)
local co = a[1]
local nargs = {coroutine.resume(co, unpack(a, 2))}
mst.a(nargs[1] or not nargs[2],
'error encountered', nargs[2])
if nargs[1] and #nargs>1
then
nargs[1] = co
self:d('adding to blocked', nargs)
table.insert(self.blocked, nargs)
else
-- drop it - no point in 'just kidding, not really interested in wait'
mst.d('dropping', co, nargs)
-- final exit also looks like this.. so we treat is as exit
--self:a(not nargs[1], 'yield without parameters not supported by reactor')
end
-- tail recursion - we keep this up while pending is non-empty
self:resume_pending()
end
function scr:check_blocked()
local i = 1
while i <= #self.blocked
do
local a = self.blocked[i]
local co = a[1]
local ra
for i, v in ipairs(a)
do
if i > 1
then
local r = v()
if r
then
ra = ra or {}
ra[i] = r
end
end
end
if ra
then
ra[1] = co
-- make ra real array of length #b
for i=2,#a
do
if ra[i] == nil
then
ra[i] = false
end
end
self:a(#ra == #a)
table.remove(self.blocked, i)
table.insert(self.pending, ra)
else
i = i + 1
end
end
end
function scr:poll()
-- we iteratively repeat following:
-- - 'busy' coroutines
-- - 'blocked' coroutines
-- - until nothing happens
self:resume_pending()
self:check_blocked()
self:resume_pending()
end
-- socket wrapper - can do reads+writes, magic happens as appropriate,
-- using scr (+global ssloop)
-- happy assumption: scrsocket is used _only_ within coroutine that is
-- already within scr (=yield+resume works as advertised)
-- also, another happy assumption is that we can use global event loop
-- as we see fit..
scrsocket = mst.create_class{class='scrsocket',
mandatory={'s'}}
function scrsocket:init()
end
function scrsocket:uninit()
self.s:close()
self:clear_ssloop()
end
function scrsocket:clear_ssloop()
if self.ro
then
self.ro:done()
self.ro = nil
end
if self.wo
then
self.wo:done()
self.wo = nil
end
if self.to
then
self.to:done()
self.to = nil
end
end
function scrsocket:get_loop()
return self.loop or ssloop.loop()
end
function scrsocket:get_io(reader)
local actionable
local loop = self:get_loop()
local f = reader and loop.new_reader or loop.new_writer
local o = f(loop, self.s,
function ()
actionable = true
end)
o:start()
local function done()
return actionable
end
return done, o
end
function get_timeout(timeout, loop)
if not timeout then return end
loop = loop or ssloop.loop()
local elapsed
local o = loop:new_timeout_delta(timeout,
function ()
elapsed = true
end)
o:start()
return function ()
return elapsed
end, o
end
function sleep(timeout, loop)
if not timeout then return end
-- just wait for timeout to expire
local t, to = get_timeout(timeout, loop)
coroutine.yield(t)
to:done()
end
function scrsocket:get_timeout(timeout)
return get_timeout(timeout, self:get_loop())
end
function scrsocket:io_with_timeout(fun, readable, timeout)
local r, ro = self:get_io(readable)
self.ro = ro
local t, to = self:get_timeout(timeout)
self.to = to
local rr, rt = coroutine.yield(r, t)
self:clear_ssloop()
-- if we're busy _and_ we have result, it's probably more sensible
-- to process the result than the timeout (hopefully this logic is
-- sensible). otherwise, someone may re-send request to us again..
if not rr
then
self:d('io timed out', timeout)
return nil, 'timeout[io]'
end
self:d('io finished')
return fun()
end
-- tcp
function scrsocket:accept(timeout)
return self:io_with_timeout(function ()
self:d('accept')
local s, err = self.s:accept()
if s
then
s:settimeout(0)
s = wrap_socket(s)
end
return s, err
end, true, timeout)
end
function scrsocket:connect(ip, port, timeout)
mst.d('connect', ip, port)
self:a(ip and port, 'ip or port not specified')
local r, e = self.s:connect(ip, port)
self:d('got from connect', r, e)
if r
then
return r
end
if err == scbtcp.ERR_ALREADY_CONNECTED
then
return 1
end
-- blocking connect call -> wait for it to finish
return self:io_with_timeout(function ()
self:d('[callback] connect', ip, port)
local r, err = self.s:connect(ip, port)
self:d('connect result', r, err)
if not r and err == scbtcp.ERR_ALREADY_CONNECTED
then
r = true
err = nil
end
return r, err
end, false, timeout)
end
function scrsocket:receive(timeout)
return self:io_with_timeout(function ()
self:d('receive')
local r, err, partial = self.s:receive(2^10)
local s = r or partial
s = #s>0 and s
return s, err
end, true, timeout)
end
function scrsocket:send(d, timeout)
local sent = 0
local w, wo = self:get_io(false)
self.wo = wo
local t, to = self:get_timeout(timeout)
self.to = to
while sent < #d
do
-- make sure socket is writable
local rw, rt = coroutine.yield(w, t)
if rt
then
self:clear_ssloop()
return nil, 'timeout[send]'
end
sent, err = self.s:send(d, sent+1)
if not sent or sent == 0 then return nil, err end
end
self:clear_ssloop()
wo:done()
return true
end
-- udp
function scrsocket:receivefrom(timeout)
return self:io_with_timeout(function ()
self:d('receivefrom')
return self.s:receivefrom()
end, true, timeout)
end
function scrsocket:sendto(...)
--self:d('sendto', ...)
return self.s:sendto(...)
end
-- shared
function scrsocket:close()
-- just proxied
self.s:close()
end
-- general utility API
function get_scr()
-- get the scr instance (which lives in global event loop by default)
local l = ssloop.loop()
mst.a(l)
if not l.scr
then
local s = scr:new{}
s.get_timeout = function ()
-- just run poll - but pretend not to have timeouts
s:poll()
end
l:add_timeout(s)
l.scr = s
end
return l.scr
end
function clear_scr()
local l = ssloop.loop()
mst.a(l)
if l.scr
then
l:remove_timeout(l.scr)
l.scr = nil
return true
end
end
function run(f, ...)
return get_scr():run(f, ...)
end
function timeouted_run_async_call(timeout, fun, ...)
local done
local args = {...}
run(function ()
done = {fun(unpack(args))}
end)
local r = ssloop.loop():loop_until(function ()
return done
end, timeout)
if not r then return end
return unpack(done)
end
function wrap_socket(s)
mst.a(s, 'no socket provided')
if scrsocket:is_instance(s)
then
return s
end
return scrsocket:new{s=s}
end