class Libevent
{
/**
* base event
*
* @var null
*/
public $baseEvent = NULL;
public function __construct()
{
$this->baseEvent = event_base_new();
}
public function add($fd, $flag = EV_READ, $callback, $args = [])
{
$event = event_new();
if (!$event) {
exit("event_new faild\n");
}
$flag = $flag == EV_READ ? EV_READ | EV_PERSIST : EV_WRITE | EV_PERSIST;
if (!event_set($event, $fd, $flag, $callback, null)) {
exit("event_set faild\n");
}
if (!event_base_set($event, $this->baseEvent)) {
exit("event_base_set faild\n");
}
if (!event_add($event)) {
exit("event_add faild\n");
}
//$this->arr[] = $event;
}
public function loop()
{
$ret = event_base_loop($this->baseEvent);
if ($ret == 1) {
exit("no events were registered\n");
} else if ($ret == -1) {
exit("event_base_loop faild\n");
}
}
}
function read($fd)
{
$buffer = fread($fd, 65536);
$buffer = trim($buffer);
echo $buffer ."\n";
}
$libevent = new Libevent();
$libevent->add(STDIN, EV_READ, 'read');
$libevent->loop();
@walkor 大大, 我对workerman 的libevent 进行了拆解, 测试,
我运行中,注释掉 $this->arr[] = $event; 这行, 命令行会提示 no events were registered,
当我不注释时正常运行, 请问下, 这是怎么一回事, 求解答。
php 的版本是 php5.5.34的
$event 变量是函数内部局部变量,出了函数作用域就释放了,所以有问题。
加上$this->arr[] = $event; 这句将局部变量存储了起来,就不会被释放,所以没问题
@1 , event_base_set($event, $this->baseEvent), 这句话不是将 $event 关联到$this->baseEvent么, 为啥外面的变量还能影响的到 libevent的执行?
这个要看event扩展源码是怎么处理的