Comparison

util/timer.lua @ 4808:07d0a3a75c8a

net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
author Matthew Wild <mwild1@gmail.com>
date Thu, 03 May 2012 16:48:34 +0100
parent 4751:0c7ae4bfc835
child 4812:5bcdc384e485
comparison
equal deleted inserted replaced
4807:2999f0fd1347 4808:07d0a3a75c8a
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 9 local server = require "net.server";
10 local ns_addtimer = require "net.server".addtimer;
11 local event = require "net.server".event;
12 local event_base = require "net.server".event_base;
13
14 local math_min = math.min 10 local math_min = math.min
15 local math_huge = math.huge 11 local math_huge = math.huge
16 local get_time = require "socket".gettime; 12 local get_time = require "socket".gettime;
17 local t_insert = table.insert; 13 local t_insert = table.insert;
18 local pairs = pairs; 14 local pairs = pairs;
22 local new_data = {}; 18 local new_data = {};
23 19
24 module "timer" 20 module "timer"
25 21
26 local _add_task; 22 local _add_task;
27 if not event then 23 if not server.event then
28 function _add_task(delay, callback) 24 function _add_task(delay, callback)
29 local current_time = get_time(); 25 local current_time = get_time();
30 delay = delay + current_time; 26 delay = delay + current_time;
31 if delay >= current_time then 27 if delay >= current_time then
32 t_insert(new_data, {delay, callback}); 28 t_insert(new_data, {delay, callback});
36 return _add_task(r, callback); 32 return _add_task(r, callback);
37 end 33 end
38 end 34 end
39 end 35 end
40 36
41 ns_addtimer(function() 37 server._addtimer(function()
42 local current_time = get_time(); 38 local current_time = get_time();
43 if #new_data > 0 then 39 if #new_data > 0 then
44 for _, d in pairs(new_data) do 40 for _, d in pairs(new_data) do
45 t_insert(data, d); 41 t_insert(data, d);
46 end 42 end
62 end 58 end
63 end 59 end
64 return next_time; 60 return next_time;
65 end); 61 end);
66 else 62 else
63 local event = require "net.server".event;
64 local event_base = require "net.server".event_base;
67 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1; 65 local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
66
68 function _add_task(delay, callback) 67 function _add_task(delay, callback)
69 local event_handle; 68 local event_handle;
70 event_handle = event_base:addevent(nil, 0, function () 69 event_handle = event_base:addevent(nil, 0, function ()
71 local ret = callback(); 70 local ret = callback();
72 if ret then 71 if ret then