phpsocks5/Workerman/Lib/Timer.php

177 lines
4.3 KiB
PHP
Raw Normal View History

2015-04-04 21:46:31 +08:00
<?php
2016-09-20 21:27:41 +08:00
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
2015-04-04 21:46:31 +08:00
namespace Workerman\Lib;
2016-09-20 21:27:41 +08:00
use Workerman\Events\EventInterface;
use Exception;
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Timer.
*
* example:
2015-04-04 21:46:31 +08:00
* Workerman\Lib\Timer::add($time_interval, callback, array($arg1, $arg2..));
*/
2016-09-20 21:27:41 +08:00
class Timer
2015-04-04 21:46:31 +08:00
{
/**
2016-09-20 21:27:41 +08:00
* Tasks that based on ALARM signal.
2015-04-04 21:46:31 +08:00
* [
* run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
* run_time => [[$func, $args, $persistent, time_interval],[$func, $args, $persistent, time_interval],..]],
2016-09-20 21:27:41 +08:00
* ..
2015-04-04 21:46:31 +08:00
* ]
2016-09-20 21:27:41 +08:00
*
2015-04-04 21:46:31 +08:00
* @var array
*/
protected static $_tasks = array();
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
* event
2016-09-20 21:27:41 +08:00
*
* @var \Workerman\Events\EventInterface
2015-04-04 21:46:31 +08:00
*/
protected static $_event = null;
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Init.
*
* @param \Workerman\Events\EventInterface $event
2015-04-04 21:46:31 +08:00
* @return void
*/
public static function init($event = null)
{
2016-09-20 21:27:41 +08:00
if ($event) {
2015-04-04 21:46:31 +08:00
self::$_event = $event;
2016-09-20 21:27:41 +08:00
} else {
2015-04-04 21:46:31 +08:00
pcntl_signal(SIGALRM, array('\Workerman\Lib\Timer', 'signalHandle'), false);
}
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* ALARM signal handler.
*
2015-04-04 21:46:31 +08:00
* @return void
*/
public static function signalHandle()
{
2016-09-20 21:27:41 +08:00
if (!self::$_event) {
2015-04-04 21:46:31 +08:00
pcntl_alarm(1);
self::tick();
}
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Add a timer.
*
* @param int $time_interval
2015-04-04 21:46:31 +08:00
* @param callback $func
2016-09-20 21:27:41 +08:00
* @param mixed $args
* @param bool $persistent
* @return bool
2015-04-04 21:46:31 +08:00
*/
public static function add($time_interval, $func, $args = array(), $persistent = true)
{
2016-09-20 21:27:41 +08:00
if ($time_interval <= 0) {
2015-04-04 21:46:31 +08:00
echo new Exception("bad time_interval");
return false;
}
2016-09-20 21:27:41 +08:00
if (self::$_event) {
return self::$_event->add($time_interval,
$persistent ? EventInterface::EV_TIMER : EventInterface::EV_TIMER_ONCE, $func, $args);
2015-04-04 21:46:31 +08:00
}
2016-09-20 21:27:41 +08:00
if (!is_callable($func)) {
2015-04-04 21:46:31 +08:00
echo new Exception("not callable");
return false;
}
2016-09-20 21:27:41 +08:00
if (empty(self::$_tasks)) {
2015-04-04 21:46:31 +08:00
pcntl_alarm(1);
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
$time_now = time();
$run_time = $time_now + $time_interval;
2016-09-20 21:27:41 +08:00
if (!isset(self::$_tasks[$run_time])) {
2015-04-04 21:46:31 +08:00
self::$_tasks[$run_time] = array();
}
2016-09-20 21:27:41 +08:00
self::$_tasks[$run_time][] = array($func, (array)$args, $persistent, $time_interval);
2015-04-04 21:46:31 +08:00
return true;
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Tick.
*
2015-04-04 21:46:31 +08:00
* @return void
*/
public static function tick()
{
2016-09-20 21:27:41 +08:00
if (empty(self::$_tasks)) {
2015-04-04 21:46:31 +08:00
pcntl_alarm(0);
return;
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
$time_now = time();
2016-09-20 21:27:41 +08:00
foreach (self::$_tasks as $run_time => $task_data) {
if ($time_now >= $run_time) {
foreach ($task_data as $index => $one_task) {
$task_func = $one_task[0];
$task_args = $one_task[1];
$persistent = $one_task[2];
2015-04-04 21:46:31 +08:00
$time_interval = $one_task[3];
2016-09-20 21:27:41 +08:00
try {
2015-04-04 21:46:31 +08:00
call_user_func_array($task_func, $task_args);
2016-09-20 21:27:41 +08:00
} catch (\Exception $e) {
2015-04-04 21:46:31 +08:00
echo $e;
}
2016-09-20 21:27:41 +08:00
if ($persistent) {
2015-04-04 21:46:31 +08:00
self::add($time_interval, $task_func, $task_args);
}
}
unset(self::$_tasks[$run_time]);
}
}
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Remove a timer.
*
* @param mixed $timer_id
* @return bool
2015-04-04 21:46:31 +08:00
*/
public static function del($timer_id)
{
2016-09-20 21:27:41 +08:00
if (self::$_event) {
2015-04-04 21:46:31 +08:00
return self::$_event->del($timer_id, EventInterface::EV_TIMER);
}
2016-09-20 21:27:41 +08:00
return false;
2015-04-04 21:46:31 +08:00
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Remove all timers.
*
* @return void
2015-04-04 21:46:31 +08:00
*/
public static function delAll()
{
self::$_tasks = array();
pcntl_alarm(0);
2016-09-20 21:27:41 +08:00
if (self::$_event) {
2015-04-04 21:46:31 +08:00
self::$_event->clearAllTimer();
}
}
}