update workerman to 3.3.4
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user