forked from MTK358/TuxBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventloop.lua
138 lines (122 loc) · 4.03 KB
/
eventloop.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
local socket = require 'socket'
local EventLoop = {}
EventLoop.__index = EventLoop
setmetatable(EventLoop, {__call = function (_)
local self = {}
setmetatable(self, EventLoop)
self._readable_handlers = {}
setmetatable(self._readable_handlers, {__mode='v'})
self._writable_handlers = {}
setmetatable(self._writable_handlers, {__mode='v'})
self._timers = {}
-- timers always eventually run out, so no risk of memory leaks.
return self
end})
local timer_mt = {
cancel = function (self)
self._running = false
end,
isrunning = function (self)
return self._running
end,
}
timer_mt.__index = timer_mt
function EventLoop:timer(timeout, cb)
local tbl = {
_time = socket.gettime() + timeout,
_running = true,
}
setmetatable(tbl, timer_mt)
self._timers[tbl] = cb
return tbl
end
function EventLoop:add_readable_handler(sock, callback)
self._readable_handlers[{sock}] = callback
end
function EventLoop:add_writable_handler(sock, callback)
self._writable_handlers[{sock}] = callback
end
function EventLoop:remove_readable_handler(sock, callback)
for k, v in pairs(self._readable_handlers) do
if k[1] == sock and v == callback then self._readable_handlers[k] = nil end
end
end
function EventLoop:remove_writable_handler(sock, callback)
for k, v in pairs(self._writable_handlers) do
if k[1] == sock and v == callback then self._writable_handlers[k] = nil end
end
end
function EventLoop:remove_socket_handlers(sock, callback)
self:remove_readable_handler(sock, callback)
self:remove_writable_handler(sock, callback)
end
function EventLoop:step()
local rsockets, wsockets = {}, {}
for k, v in pairs(self._readable_handlers) do
rsockets[#rsockets+1] = k[1]
end
for k, v in pairs(self._writable_handlers) do
wsockets[#wsockets+1] = k[1]
end
local r, w, _ = socket.select(rsockets,
wsockets,
self:_seconds_to_next_timer())
local readable_handlers, writable_handlers = {}, {}
for k, v in pairs(self._readable_handlers) do readable_handlers[k] = v end
for k, v in pairs(self._writable_handlers) do writable_handlers[k] = v end
for _, sock in pairs(r) do
for k, v in pairs(readable_handlers) do
if k[1] == sock then
local success, errmsg = pcall(v)
if not success then
io.stderr:write(('error in socket readable event handler: %s\n'):format(tostring(errmsg)))
self._writable_handlers[k] = nil
else
if not errmsg then self._readable_handlers[k] = nil end
end
end
end
end
for _, sock in pairs(w) do
for k, v in pairs(writable_handlers) do
if k[1] == sock then
local success, errmsg = pcall(v)
if not success then
io.stderr:write(('error in socket writable event handler: %s\n'):format(tostring(errmsg)))
self._writable_handlers[k] = nil
else
if not errmsg then self._writable_handlers[k] = nil end
end
end
end
end
for k, v in pairs(self._timers) do
if k._time <= socket.gettime() then
if k._running then
local success, errmsg = pcall(v)
if not success then
io.stderr:write(('error in timer event handler: %s\n'):format(tostring(errmsg)))
end
end
self._timers[k] = nil
end
end
end
function EventLoop:run()
self._exit = false
repeat self:step() until self._exit
end
function EventLoop:exit()
self._exit = true
end
function EventLoop:_seconds_to_next_timer()
local t = socket.gettime() + 5
for k, v in pairs(self._timers) do
if k._time < t then
t = k._time
end
end
t = t - socket.gettime()
return t < 0 and 0 or t
end
return EventLoop