update workerman to 3.3.4

This commit is contained in:
walkor
2016-09-20 21:27:41 +08:00
parent a3d823d04a
commit 58dc935eb0
25 changed files with 4159 additions and 2500 deletions
+173
View File
@@ -0,0 +1,173 @@
<?php
/**
* 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 有个鬼<42765633@qq.com>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Workerman\Events;
use Workerman\Worker;
/**
* ev eventloop
*/
class Ev implements EventInterface
{
/**
* All listeners for read/write event.
*
* @var array
*/
protected $_allEvents = array();
/**
* Event listeners of signal.
*
* @var array
*/
protected $_eventSignal = array();
/**
* All timer event listeners.
* [func, args, event, flag, time_interval]
*
* @var array
*/
protected $_eventTimer = array();
/**
* Timer id.
*
* @var int
*/
protected static $_timerId = 1;
/**
* Add a timer.
* {@inheritdoc}
*/
public function add($fd, $flag, $func, $args = null)
{
$callback = function ($event, $socket) use ($fd, $func) {
try {
call_user_func($func, $fd);
} catch (\Exception $e) {
Worker::log($e);
exit(250);
} catch (\Error $e) {
Worker::log($e);
exit(250);
}
};
switch ($flag) {
case self::EV_SIGNAL:
$event = new \EvSignal($fd, $callback);
$this->_eventSignal[$fd] = $event;
return true;
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
$repeat = $flag == self::EV_TIMER_ONCE ? 0 : $fd;
$param = array($func, (array)$args, $flag, $fd, self::$_timerId);
$event = new \EvTimer($fd, $repeat, array($this, 'timerCallback'), $param);
$this->_eventTimer[self::$_timerId] = $event;
return self::$_timerId++;
default :
$fd_key = (int)$fd;
$real_flag = $flag === self::EV_READ ? \Ev::READ : \Ev::WRITE;
$event = new \EvIo($fd, $real_flag, $callback);
$this->_allEvents[$fd_key][$flag] = $event;
return true;
}
}
/**
* Remove a timer.
* {@inheritdoc}
*/
public function del($fd, $flag)
{
switch ($flag) {
case self::EV_READ:
case self::EV_WRITE:
$fd_key = (int)$fd;
if (isset($this->_allEvents[$fd_key][$flag])) {
$this->_allEvents[$fd_key][$flag]->stop();
unset($this->_allEvents[$fd_key][$flag]);
}
if (empty($this->_allEvents[$fd_key])) {
unset($this->_allEvents[$fd_key]);
}
break;
case self::EV_SIGNAL:
$fd_key = (int)$fd;
if (isset($this->_eventSignal[$fd_key])) {
$this->_allEvents[$fd_key][$flag]->stop();
unset($this->_eventSignal[$fd_key]);
}
break;
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
if (isset($this->_eventTimer[$fd])) {
$this->_eventTimer[$fd]->stop();
unset($this->_eventTimer[$fd]);
}
break;
}
return true;
}
/**
* Timer callback.
*
* @param \EvWatcher $event
*/
public function timerCallback($event)
{
$param = $event->data;
$timer_id = $param[4];
if ($param[2] === self::EV_TIMER_ONCE) {
$this->_eventTimer[$timer_id]->stop();
unset($this->_eventTimer[$timer_id]);
}
try {
call_user_func_array($param[0], $param[1]);
} catch (\Exception $e) {
Worker::log($e);
exit(250);
} catch (\Error $e) {
Worker::log($e);
exit(250);
}
}
/**
* Remove all timers.
*
* @return void
*/
public function clearAllTimer()
{
foreach ($this->_eventTimer as $event) {
$event->stop();
}
$this->_eventTimer = array();
}
/**
* Main loop.
*
* @see EventInterface::loop()
*/
public function loop()
{
\Ev::run();
}
}
+188
View File
@@ -0,0 +1,188 @@
<?php
/**
* 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 有个鬼<42765633@qq.com>
* @copyright 有个鬼<42765633@qq.com>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Workerman\Events;
use Workerman\Worker;
/**
* libevent eventloop
*/
class Event implements EventInterface
{
/**
* Event base.
* @var object
*/
protected $_eventBase = null;
/**
* All listeners for read/write event.
* @var array
*/
protected $_allEvents = array();
/**
* Event listeners of signal.
* @var array
*/
protected $_eventSignal = array();
/**
* All timer event listeners.
* [func, args, event, flag, time_interval]
* @var array
*/
protected $_eventTimer = array();
/**
* Timer id.
* @var int
*/
protected static $_timerId = 1;
/**
* construct
* @return void
*/
public function __construct()
{
$this->_eventBase = new \EventBase();
}
/**
* @see EventInterface::add()
*/
public function add($fd, $flag, $func, $args=array())
{
switch ($flag) {
case self::EV_SIGNAL:
$fd_key = (int)$fd;
$event = \Event::signal($this->_eventBase, $fd, $func);
if (!$event||!$event->add()) {
return false;
}
$this->_eventSignal[$fd_key] = $event;
return true;
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
$param = array($func, (array)$args, $flag, $fd, self::$_timerId);
$event = new \Event($this->_eventBase, -1, \Event::TIMEOUT|\Event::PERSIST, array($this, "timerCallback"), $param);
if (!$event||!$event->addTimer($fd)) {
return false;
}
$this->_eventTimer[self::$_timerId] = $event;
return self::$_timerId++;
default :
$fd_key = (int)$fd;
$real_flag = $flag === self::EV_READ ? \Event::READ | \Event::PERSIST : \Event::WRITE | \Event::PERSIST;
$event = new \Event($this->_eventBase, $fd, $real_flag, $func, $fd);
if (!$event||!$event->add()) {
return false;
}
$this->_allEvents[$fd_key][$flag] = $event;
return true;
}
}
/**
* @see Events\EventInterface::del()
*/
public function del($fd, $flag)
{
switch ($flag) {
case self::EV_READ:
case self::EV_WRITE:
$fd_key = (int)$fd;
if (isset($this->_allEvents[$fd_key][$flag])) {
$this->_allEvents[$fd_key][$flag]->del();
unset($this->_allEvents[$fd_key][$flag]);
}
if (empty($this->_allEvents[$fd_key])) {
unset($this->_allEvents[$fd_key]);
}
break;
case self::EV_SIGNAL:
$fd_key = (int)$fd;
if (isset($this->_eventSignal[$fd_key])) {
$this->_allEvents[$fd_key][$flag]->del();
unset($this->_eventSignal[$fd_key]);
}
break;
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
if (isset($this->_eventTimer[$fd])) {
$this->_eventTimer[$fd]->del();
unset($this->_eventTimer[$fd]);
}
break;
}
return true;
}
/**
* Timer callback.
* @param null $fd
* @param int $what
* @param int $timer_id
*/
public function timerCallback($fd, $what, $param)
{
$timer_id = $param[4];
if ($param[2] === self::EV_TIMER_ONCE) {
$this->_eventTimer[$timer_id]->del();
unset($this->_eventTimer[$timer_id]);
}
try {
call_user_func_array($param[0], $param[1]);
} catch (\Exception $e) {
Worker::log($e);
exit(250);
} catch (\Error $e) {
Worker::log($e);
exit(250);
}
}
/**
* @see Events\EventInterface::clearAllTimer()
* @return void
*/
public function clearAllTimer()
{
foreach ($this->_eventTimer as $event) {
$event->del();
}
$this->_eventTimer = array();
}
/**
* @see EventInterface::loop()
*/
public function loop()
{
$this->_eventBase->loop();
}
}
+44 -22
View File
@@ -1,63 +1,85 @@
<?php
/**
* 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
*/
namespace Workerman\Events;
interface EventInterface
{
/**
* 读事件
* Read event.
*
* @var int
*/
const EV_READ = 1;
/**
* 写事件
* Write event.
*
* @var int
*/
const EV_WRITE = 2;
/**
* 信号事件
* Signal event.
*
* @var int
*/
const EV_SIGNAL = 4;
/**
* 连续的定时事件
* Timer event.
*
* @var int
*/
const EV_TIMER = 8;
/**
* 定时一次
* @var int
* Timer once event.
*
* @var int
*/
const EV_TIMER_ONCE = 16;
/**
* 添加事件回调
* @param resource $fd
* @param int $flag
* Add event listener to event loop.
*
* @param mixed $fd
* @param int $flag
* @param callable $func
* @param mixed $args
* @return bool
*/
public function add($fd, $flag, $func, $args = null);
/**
* 删除事件回调
* @param resource $fd
* @param int $flag
* Remove event listener from event loop.
*
* @param mixed $fd
* @param int $flag
* @return bool
*/
public function del($fd, $flag);
/**
* 清除所有定时器
* Remove all timers.
*
* @return void
*/
public function clearAllTimer();
/**
* 事件循环
* Main loop.
*
* @return void
*/
public function loop();
+90 -92
View File
@@ -1,145 +1,146 @@
<?php
namespace Workerman\Events;
<?php
/**
* libevent
* @author walkor <walkor@workerman.net>
* 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
*/
namespace Workerman\Events;
use Workerman\Worker;
/**
* libevent eventloop
*/
class Libevent implements EventInterface
{
/**
* eventBase
* @var object
* Event base.
*
* @var resource
*/
protected $_eventBase = null;
/**
* 所有的事件
* All listeners for read/write event.
*
* @var array
*/
protected $_allEvents = array();
/**
* 所有的信号事件
* Event listeners of signal.
*
* @var array
*/
protected $_eventSignal = array();
/**
* 所有的定时事件
* All timer event listeners.
* [func, args, event, flag, time_interval]
*
* @var array
*/
protected $_eventTimer = array();
/**
* 构造函数
* @return void
* construct
*/
public function __construct()
{
$this->_eventBase = event_base_new();
}
/**
* 添加事件
* @see EventInterface::add()
* {@inheritdoc}
*/
public function add($fd, $flag, $func, $args=null)
public function add($fd, $flag, $func, $args = array())
{
switch($flag)
{
switch ($flag) {
case self::EV_SIGNAL:
$fd_key = (int)$fd;
$real_flag = EV_SIGNAL | EV_PERSIST;
$fd_key = (int)$fd;
$real_flag = EV_SIGNAL | EV_PERSIST;
$this->_eventSignal[$fd_key] = event_new();
if(!event_set($this->_eventSignal[$fd_key], $fd, $real_flag, $func, null))
{
if (!event_set($this->_eventSignal[$fd_key], $fd, $real_flag, $func, null)) {
return false;
}
if(!event_base_set($this->_eventSignal[$fd_key], $this->_eventBase))
{
if (!event_base_set($this->_eventSignal[$fd_key], $this->_eventBase)) {
return false;
}
if(!event_add($this->_eventSignal[$fd_key]))
{
if (!event_add($this->_eventSignal[$fd_key])) {
return false;
}
return true;
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
$event = event_new();
$event = event_new();
$timer_id = (int)$event;
if(!event_set($event, 0, EV_TIMEOUT, array($this, 'timerCallback'), $timer_id))
{
if (!event_set($event, 0, EV_TIMEOUT, array($this, 'timerCallback'), $timer_id)) {
return false;
}
if(!event_base_set($event, $this->_eventBase))
{
if (!event_base_set($event, $this->_eventBase)) {
return false;
}
$time_interval = $fd*1000000;
if(!event_add($event, $time_interval))
{
$time_interval = $fd * 1000000;
if (!event_add($event, $time_interval)) {
return false;
}
$this->_eventTimer[$timer_id] = array($func, (array)$args, $event, $flag, $time_interval);
return $timer_id;
default :
$fd_key = (int)$fd;
$real_flag = $flag == self::EV_READ ? EV_READ | EV_PERSIST : EV_WRITE | EV_PERSIST;
$fd_key = (int)$fd;
$real_flag = $flag === self::EV_READ ? EV_READ | EV_PERSIST : EV_WRITE | EV_PERSIST;
$event = event_new();
if(!event_set($event, $fd, $real_flag, $func, null))
{
if (!event_set($event, $fd, $real_flag, $func, null)) {
return false;
}
if(!event_base_set($event, $this->_eventBase))
{
if (!event_base_set($event, $this->_eventBase)) {
return false;
}
if(!event_add($event))
{
if (!event_add($event)) {
return false;
}
$this->_allEvents[$fd_key][$flag] = $event;
return true;
}
}
/**
* 删除事件
* @see Events\EventInterface::del()
* {@inheritdoc}
*/
public function del($fd ,$flag)
public function del($fd, $flag)
{
switch($flag)
{
switch ($flag) {
case self::EV_READ:
case self::EV_WRITE:
$fd_key = (int)$fd;
if(isset($this->_allEvents[$fd_key][$flag]))
{
if (isset($this->_allEvents[$fd_key][$flag])) {
event_del($this->_allEvents[$fd_key][$flag]);
unset($this->_allEvents[$fd_key][$flag]);
}
if(empty($this->_allEvents[$fd_key]))
{
if (empty($this->_allEvents[$fd_key])) {
unset($this->_allEvents[$fd_key]);
}
break;
case self::EV_SIGNAL:
$fd_key = (int)$fd;
if(isset($this->_eventSignal[$fd_key]))
{
if (isset($this->_eventSignal[$fd_key])) {
event_del($this->_eventSignal[$fd_key]);
unset($this->_eventSignal[$fd_key]);
}
@@ -147,8 +148,7 @@ class Libevent implements EventInterface
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
// 这里 fd 为timerid
if(isset($this->_eventTimer[$fd]))
{
if (isset($this->_eventTimer[$fd])) {
event_del($this->_eventTimer[$fd][2]);
unset($this->_eventTimer[$fd]);
}
@@ -156,48 +156,46 @@ class Libevent implements EventInterface
}
return true;
}
/**
* 定时器回调
* @param null $_null
* @param null $_null
* @param int $timer_id
* Timer callback.
*
* @param mixed $_null1
* @param int $_null2
* @param mixed $timer_id
*/
protected function timerCallback($_null, $_null, $timer_id)
protected function timerCallback($_null1, $_null2, $timer_id)
{
// 如果是连续的定时任务,再把任务加进去
if($this->_eventTimer[$timer_id][3] == self::EV_TIMER)
{
if ($this->_eventTimer[$timer_id][3] === self::EV_TIMER) {
event_add($this->_eventTimer[$timer_id][2], $this->_eventTimer[$timer_id][4]);
}
try
{
// 执行任务
try {
call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
} catch (\Exception $e) {
Worker::log($e);
exit(250);
} catch (\Error $e) {
Worker::log($e);
exit(250);
}
catch(\Exception $e)
{
echo $e;
if (isset($this->_eventTimer[$timer_id]) && $this->_eventTimer[$timer_id][3] === self::EV_TIMER_ONCE) {
$this->del($timer_id, self::EV_TIMER_ONCE);
}
}
/**
* 删除所有定时器
* @return void
* {@inheritdoc}
*/
public function clearAllTimer()
{
foreach($this->_eventTimer as $task_data)
{
foreach ($this->_eventTimer as $task_data) {
event_del($task_data[2]);
}
$this->_eventTimer = array();
}
/**
* 事件循环
* @see EventInterface::loop()
* {@inheritdoc}
*/
public function loop()
{
+125 -129
View File
@@ -1,141 +1,163 @@
<?php
/**
* 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
*/
namespace Workerman\Events;
/**
* select eventloop
*/
class Select implements EventInterface
{
/**
* 所有的事件
* All listeners for read/write event.
*
* @var array
*/
public $_allEvents = array();
/**
* 所有信号事件
* Event listeners of signal.
*
* @var array
*/
public $_signalEvents = array();
/**
* 监听这些描述符的读事件
* Fds waiting for read event.
*
* @var array
*/
protected $_readFds = array();
/**
* 监听这些描述符的写事件
* Fds waiting for write event.
*
* @var array
*/
protected $_writeFds = array();
/**
* 任务调度器,最大堆
* Timer scheduler.
* {['data':timer_id, 'priority':run_timestamp], ..}
* @var SplPriorityQueue
*
* @var \SplPriorityQueue
*/
protected $_scheduler = null;
/**
* 定时任务
* All timer event listeners.
* [[func, args, flag, timer_interval], ..]
*
* @var array
*/
protected $_task = array();
/**
* 定时器id
* Timer id.
*
* @var int
*/
protected $_timerId = 1;
/**
* select超时时间,单位:微妙
* Select timeout.
*
* @var int
*/
protected $_selectTimeout = 100000000;
/**
* 构造函数
* @return void
* Paired socket channels
*
* @var array
*/
protected $channel = array();
/**
* Construct.
*/
public function __construct()
{
// 创建一个管道,放入监听读的描述符集合中,避免空轮询
// Create a pipeline and put into the collection of the read to read the descriptor to avoid empty polling.
$this->channel = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
if($this->channel)
{
if ($this->channel) {
stream_set_blocking($this->channel[0], 0);
$this->_readFds[0] = $this->channel[0];
}
// 初始化优先队列(最大堆)
// Init SplPriorityQueue.
$this->_scheduler = new \SplPriorityQueue();
$this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
}
/**
* 添加事件及处理函数
* @see Events\EventInterface::add()
* {@inheritdoc}
*/
public function add($fd, $flag, $func, $args = null)
public function add($fd, $flag, $func, $args = array())
{
switch ($flag)
{
switch ($flag) {
case self::EV_READ:
$fd_key = (int)$fd;
$fd_key = (int)$fd;
$this->_allEvents[$fd_key][$flag] = array($func, $fd);
$this->_readFds[$fd_key] = $fd;
$this->_readFds[$fd_key] = $fd;
break;
case self::EV_WRITE:
$fd_key = (int)$fd;
$fd_key = (int)$fd;
$this->_allEvents[$fd_key][$flag] = array($func, $fd);
$this->_writeFds[$fd_key] = $fd;
$this->_writeFds[$fd_key] = $fd;
break;
case self::EV_SIGNAL:
$fd_key = (int)$fd;
$fd_key = (int)$fd;
$this->_signalEvents[$fd_key][$flag] = array($func, $fd);
pcntl_signal($fd, array($this, 'signalHandler'));
break;
case self::EV_TIMER:
case self::EV_TIMER_ONCE:
// $fd 为 定时的时间间隔,单位为秒,支持小数,能精确到0.001秒
$run_time = microtime(true)+$fd;
$run_time = microtime(true) + $fd;
$this->_scheduler->insert($this->_timerId, -$run_time);
$this->_task[$this->_timerId] = array($func, $args, $flag, $fd);
$this->_task[$this->_timerId] = array($func, (array)$args, $flag, $fd);
$this->tick();
return $this->_timerId++;
}
return true;
}
/**
* 信号处理函数
* Signal handler.
*
* @param int $signal
*/
public function signalHandler($signal)
{
call_user_func_array($this->_signalEvents[$signal][self::EV_SIGNAL][0], array($signal));
}
/**
* 删除某个描述符的某类事件的监听
* @see Events\EventInterface::del()
* {@inheritdoc}
*/
public function del($fd ,$flag)
public function del($fd, $flag)
{
$fd_key = (int)$fd;
switch ($flag)
{
switch ($flag) {
case self::EV_READ:
unset($this->_allEvents[$fd_key][$flag], $this->_readFds[$fd_key]);
if(empty($this->_allEvents[$fd_key]))
{
if (empty($this->_allEvents[$fd_key])) {
unset($this->_allEvents[$fd_key]);
}
return true;
case self::EV_WRITE:
unset($this->_allEvents[$fd_key][$flag], $this->_writeFds[$fd_key]);
if(empty($this->_allEvents[$fd_key]))
{
if (empty($this->_allEvents[$fd_key])) {
unset($this->_allEvents[$fd_key]);
}
return true;
@@ -145,67 +167,51 @@ class Select implements EventInterface
break;
case self::EV_TIMER:
case self::EV_TIMER_ONCE;
// $fd_key为要删除的定时器id,即timerId
unset($this->_task[$fd_key]);
return true;
}
return false;;
return false;
}
/**
* 检查是否有可执行的定时任务,有的话执行
* Tick for timer.
*
* @return void
*/
protected function tick()
{
while(!$this->_scheduler->isEmpty())
{
$scheduler_data = $this->_scheduler->top();
$timer_id = $scheduler_data['data'];
$next_run_time = -$scheduler_data['priority'];
$time_now = microtime(true);
if($time_now >= $next_run_time)
{
while (!$this->_scheduler->isEmpty()) {
$scheduler_data = $this->_scheduler->top();
$timer_id = $scheduler_data['data'];
$next_run_time = -$scheduler_data['priority'];
$time_now = microtime(true);
$this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
if ($this->_selectTimeout <= 0) {
$this->_scheduler->extract();
// 如果任务不存在,则是对应的定时器已经删除
if(!isset($this->_task[$timer_id]))
{
if (!isset($this->_task[$timer_id])) {
continue;
}
// 任务数据[func, args, flag, timer_interval]
// [func, args, flag, timer_interval]
$task_data = $this->_task[$timer_id];
// 如果是持续的定时任务,再把任务加到定时队列
if($task_data[2] == self::EV_TIMER)
{
$next_run_time = $time_now+$task_data[3];
if ($task_data[2] === self::EV_TIMER) {
$next_run_time = $time_now + $task_data[3];
$this->_scheduler->insert($timer_id, -$next_run_time);
}
// 尝试执行任务
try
{
call_user_func_array($task_data[0], $task_data[1]);
}
catch(\Exception $e)
{
echo $e;
call_user_func_array($task_data[0], $task_data[1]);
if (isset($this->_task[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
$this->del($timer_id, self::EV_TIMER_ONCE);
}
continue;
}
else
{
// 设定超时时间
$this->_selectTimeout = ($next_run_time - $time_now)*1000000;
return;
}
return;
}
$this->_selectTimeout = 100000000;
}
/**
* 删除所有定时器
* @return void
* {@inheritdoc}
*/
public function clearAllTimer()
{
@@ -213,55 +219,45 @@ class Select implements EventInterface
$this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
$this->_task = array();
}
/**
* 主循环
* @see Events\EventInterface::loop()
* {@inheritdoc}
*/
public function loop()
{
$e = null;
while (1)
{
// 如果有信号,尝试执行信号处理函数
while (1) {
// Calls signal handlers for pending signals
pcntl_signal_dispatch();
$read = $this->_readFds;
$read = $this->_readFds;
$write = $this->_writeFds;
// 等待可读或者可写事件
@stream_select($read, $write, $e, 0, $this->_selectTimeout);
// 这些描述符可读,执行对应描述符的读回调函数
if($read)
{
foreach($read as $fd)
{
$fd_key = (int) $fd;
if(isset($this->_allEvents[$fd_key][self::EV_READ]))
{
call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0], array($this->_allEvents[$fd_key][self::EV_READ][1]));
}
}
}
// 这些描述符可写,执行对应描述符的写回调函数
if($write)
{
foreach($write as $fd)
{
$fd_key = (int) $fd;
if(isset($this->_allEvents[$fd_key][self::EV_WRITE]))
{
call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0], array($this->_allEvents[$fd_key][self::EV_WRITE][1]));
}
}
}
// 尝试执行定时任务
if(!$this->_scheduler->isEmpty())
{
// Waiting read/write/signal/timeout events.
$ret = @stream_select($read, $write, $e, 0, $this->_selectTimeout);
if (!$this->_scheduler->isEmpty()) {
$this->tick();
}
if (!$ret) {
continue;
}
foreach ($read as $fd) {
$fd_key = (int)$fd;
if (isset($this->_allEvents[$fd_key][self::EV_READ])) {
call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0],
array($this->_allEvents[$fd_key][self::EV_READ][1]));
}
}
foreach ($write as $fd) {
$fd_key = (int)$fd;
if (isset($this->_allEvents[$fd_key][self::EV_WRITE])) {
call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0],
array($this->_allEvents[$fd_key][self::EV_WRITE][1]));
}
}
}
}
}