2016-09-20 21:27:41 +08:00
|
|
|
<?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
|
|
|
|
*/
|
2015-04-04 21:46:31 +08:00
|
|
|
namespace Workerman\Events;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
use Workerman\Worker;
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* libevent eventloop
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
class Libevent implements EventInterface
|
|
|
|
{
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Event base.
|
|
|
|
*
|
|
|
|
* @var resource
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
protected $_eventBase = 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
|
|
|
* All listeners for read/write event.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_allEvents = array();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Event listeners of signal.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_eventSignal = array();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* All timer event listeners.
|
2015-04-04 21:46:31 +08:00
|
|
|
* [func, args, event, flag, time_interval]
|
2016-09-20 21:27:41 +08:00
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_eventTimer = array();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* construct
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->_eventBase = event_base_new();
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* {@inheritdoc}
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function add($fd, $flag, $func, $args = array())
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
switch ($flag) {
|
2015-04-04 21:46:31 +08:00
|
|
|
case self::EV_SIGNAL:
|
2016-09-20 21:27:41 +08:00
|
|
|
$fd_key = (int)$fd;
|
|
|
|
$real_flag = EV_SIGNAL | EV_PERSIST;
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->_eventSignal[$fd_key] = event_new();
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!event_set($this->_eventSignal[$fd_key], $fd, $real_flag, $func, null)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!event_base_set($this->_eventSignal[$fd_key], $this->_eventBase)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!event_add($this->_eventSignal[$fd_key])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case self::EV_TIMER:
|
|
|
|
case self::EV_TIMER_ONCE:
|
2016-09-20 21:27:41 +08:00
|
|
|
$event = event_new();
|
2015-04-04 21:46:31 +08:00
|
|
|
$timer_id = (int)$event;
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!event_set($event, 0, EV_TIMEOUT, array($this, 'timerCallback'), $timer_id)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
if (!event_base_set($event, $this->_eventBase)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
$time_interval = $fd * 1000000;
|
|
|
|
if (!event_add($event, $time_interval)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->_eventTimer[$timer_id] = array($func, (array)$args, $event, $flag, $time_interval);
|
|
|
|
return $timer_id;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
default :
|
2016-09-20 21:27:41 +08:00
|
|
|
$fd_key = (int)$fd;
|
|
|
|
$real_flag = $flag === self::EV_READ ? EV_READ | EV_PERSIST : EV_WRITE | EV_PERSIST;
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
$event = event_new();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
if (!event_set($event, $fd, $real_flag, $func, null)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
if (!event_base_set($event, $this->_eventBase)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
if (!event_add($event)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->_allEvents[$fd_key][$flag] = $event;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
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
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* {@inheritdoc}
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function del($fd, $flag)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
switch ($flag) {
|
2015-04-04 21:46:31 +08:00
|
|
|
case self::EV_READ:
|
|
|
|
case self::EV_WRITE:
|
|
|
|
$fd_key = (int)$fd;
|
2016-09-20 21:27:41 +08:00
|
|
|
if (isset($this->_allEvents[$fd_key][$flag])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
event_del($this->_allEvents[$fd_key][$flag]);
|
|
|
|
unset($this->_allEvents[$fd_key][$flag]);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (empty($this->_allEvents[$fd_key])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
unset($this->_allEvents[$fd_key]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::EV_SIGNAL:
|
|
|
|
$fd_key = (int)$fd;
|
2016-09-20 21:27:41 +08:00
|
|
|
if (isset($this->_eventSignal[$fd_key])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
event_del($this->_eventSignal[$fd_key]);
|
|
|
|
unset($this->_eventSignal[$fd_key]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::EV_TIMER:
|
|
|
|
case self::EV_TIMER_ONCE:
|
|
|
|
// 这里 fd 为timerid
|
2016-09-20 21:27:41 +08:00
|
|
|
if (isset($this->_eventTimer[$fd])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
event_del($this->_eventTimer[$fd][2]);
|
|
|
|
unset($this->_eventTimer[$fd]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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
|
|
|
* Timer callback.
|
|
|
|
*
|
|
|
|
* @param mixed $_null1
|
|
|
|
* @param int $_null2
|
|
|
|
* @param mixed $timer_id
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
protected function timerCallback($_null1, $_null2, $timer_id)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->_eventTimer[$timer_id][3] === self::EV_TIMER) {
|
2015-04-04 21:46:31 +08:00
|
|
|
event_add($this->_eventTimer[$timer_id][2], $this->_eventTimer[$timer_id][4]);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
try {
|
2015-04-04 21:46:31 +08:00
|
|
|
call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
|
2016-09-20 21:27:41 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (isset($this->_eventTimer[$timer_id]) && $this->_eventTimer[$timer_id][3] === self::EV_TIMER_ONCE) {
|
|
|
|
$this->del($timer_id, self::EV_TIMER_ONCE);
|
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
|
|
|
* {@inheritdoc}
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public function clearAllTimer()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
foreach ($this->_eventTimer as $task_data) {
|
2015-04-04 21:46:31 +08:00
|
|
|
event_del($task_data[2]);
|
|
|
|
}
|
|
|
|
$this->_eventTimer = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* {@inheritdoc}
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public function loop()
|
|
|
|
{
|
|
|
|
event_base_loop($this->_eventBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|