Comparison

net/server_epoll.lua @ 7573:0f0a6febbc5a

net.server_epoll: Add some comments
author Kim Alvefur <zash@zash.se>
date Thu, 18 Aug 2016 01:52:18 +0200
parent 7564:f95ffd24e042
child 7574:97b8506118a9
comparison
equal deleted inserted replaced
7564:f95ffd24e042 7573:0f0a6febbc5a
38 local function closetimer(t) 38 local function closetimer(t)
39 t[1] = 0; 39 t[1] = 0;
40 t[2] = noop; 40 t[2] = noop;
41 end 41 end
42 42
43 -- Set to true when timers have changed
43 local resort_timers = false; 44 local resort_timers = false;
45
46 -- Add absolute timer
44 local function at(time, f) 47 local function at(time, f)
45 local timer = { time, f, close = closetimer }; 48 local timer = { time, f, close = closetimer };
46 t_insert(timers, timer); 49 t_insert(timers, timer);
47 resort_timers = true; 50 resort_timers = true;
48 return timer; 51 return timer;
49 end 52 end
53
54 -- Add relative timer
50 local function addtimer(timeout, f) 55 local function addtimer(timeout, f)
51 return at(gettime() + timeout, f); 56 return at(gettime() + timeout, f);
52 end 57 end
53 58
59 -- Run callbacks of expired timers
60 -- Return time until next timeout
54 local function runtimers() 61 local function runtimers()
55 if resort_timers then 62 if resort_timers then
56 -- Sort earliest timers to the end 63 -- Sort earliest timers to the end
57 t_sort(timers, function (a, b) return a[1] > b[1]; end); 64 t_sort(timers, function (a, b) return a[1] > b[1]; end);
58 resort_timers = false; 65 resort_timers = false;
74 for i = #timers, 1, -1 do 81 for i = #timers, 1, -1 do
75 local timer = timers[i]; 82 local timer = timers[i];
76 local t, f = timer[1], timer[2]; 83 local t, f = timer[1], timer[2];
77 local now = gettime(); -- inside or before the loop? 84 local now = gettime(); -- inside or before the loop?
78 if t > now then 85 if t > now then
86 -- This timer should not fire yet
79 local diff = t - now; 87 local diff = t - now;
80 if diff < next_delay then 88 if diff < next_delay then
81 next_delay = diff; 89 next_delay = diff;
82 end 90 end
83 break; 91 break;
90 t_remove(timers, i); 98 t_remove(timers, i);
91 end 99 end
92 end 100 end
93 if resort_timers or next_delay < 1e-6 then 101 if resort_timers or next_delay < 1e-6 then
94 -- Timers may be added from within a timer callback. 102 -- Timers may be added from within a timer callback.
95 -- Those would not be considered for next_dela, 103 -- Those would not be considered for next_delay,
96 -- and we might sleep for too long, so instead 104 -- and we might sleep for too long, so instead
97 -- we return a shorter timeout so we can 105 -- we return a shorter timeout so we can
98 -- properly sort all new timers. 106 -- properly sort all new timers.
99 next_delay = 1e-6; 107 next_delay = 1e-6;
100 end 108 end
220 if not ok then return ok, err end 228 if not ok then return ok, err end
221 self._flags = flags; 229 self._flags = flags;
222 return true; 230 return true;
223 end 231 end
224 232
233 -- Called when socket is readable
225 function interface:onreadable() 234 function interface:onreadable()
226 local data, err, partial = self.conn:receive(self._pattern); 235 local data, err, partial = self.conn:receive(self._pattern);
227 if data or partial then 236 if data or partial then
228 self.listeners.onincoming(self, data or partial, err); 237 self.listeners.onincoming(self, data or partial, err);
229 end 238 end
240 if self.conn:dirty() then 249 if self.conn:dirty() then
241 self:pausefor(cfg.read_retry_delay); 250 self:pausefor(cfg.read_retry_delay);
242 end 251 end
243 end 252 end
244 253
254 -- Called when socket is writable
245 function interface:onwriteable() 255 function interface:onwriteable()
246 local buffer = self.writebuffer; 256 local buffer = self.writebuffer;
247 local data = t_concat(buffer); 257 local data = t_concat(buffer);
248 local ok, err, partial = self.conn:send(data); 258 local ok, err, partial = self.conn:send(data);
249 if ok then 259 if ok then