fixed Numerics::numToInt method && tidied up code
[PHP-P10.git] / Tools / timer.inc.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)                       *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      *
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  *
20  * Tools/timer.inc.php
21  *
22  *  timer functions
23  */
24 $timers['id']=0;
25
26 function timer_loop() {
27         global $timers;
28         $mtime = microtime(true);
29         $ret = false;
30         foreach ($timers as $id => $timer) {
31                 if(($timer['expire'] - 0.00019) <= $mtime) { //we expire a timer 0,19 ms before (to reduce timer desyncs)
32                         if((is_array($timer['function']) && method_exists($timer['function'][0],$timer['function'][1])) || (!is_array($timer['function']) && function_exists($timer['function']))) {
33                                 call_user_func_array($timer['function'],$timer['params']);
34                         }
35                         $ret = true;
36                         unset($timers[$id]);
37                 }
38         }
39         return $ret;
40 }
41
42 function timer($seconds,$command,$parameter) {
43         global $timers;
44         $new['expire'] = microtime(true) + $seconds;
45         $new['function'] = $command;
46         $new['params'] = $parameter;
47         if(!array_key_exists('id', $timers)) {
48                 $timers['id'] = 0;
49         }
50         while(isset($timers[$timers['id']])|| !isset($timers['id'])) {
51                 $timers['id']++;
52                 if($timers['id'] > 9999999) $timers['id'] = 0;
53         }
54         $timers[$timers['id']] = $new;
55         return $timers['id'];
56 }
57
58 function utimer($seconds,$command,$parameter) {
59         global $timers;
60         $new['expire'] = microtime(true) + ($seconds / 1000);
61         $new['function'] = $command;
62         $new['params'] = $parameter;
63         while($timers[$timers['id']] || !$timers['id']) {
64                 $timers['id']++;
65                 if($timers['id'] > 9999999) $timers['id'] = 0;
66         }
67         $timers[$timers['id']] = $new;
68         return $timers['id'];
69 }
70
71 function kill_timer($id) {
72         global $timers;
73         unset($timers[$id]);
74 }
75
76 $timers['id']=0;
77
78 ?>