企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] * * * * * ## 1 事件文件 ~~~ Workerman\Events\ EventInterface.php ;事件接口 Ev.php ;EV事件机制 Event.php ;Event事件机制 Libvent.php ;Libevent事件机制 Select.php ;Select事件机制 ~~~ ## 2 事件接口(EventInterface.php) >[info] 成员变量 ~~~ ;读事件,写事件,信号事件,周期事件,定时事件 const EV_READ = 1; const EV_WRITE = 2; const EV_SIGNAL = 4; const EV_TIMER = 8; const EV_TIMER_ONCE = 16; ~~~ >[info] 成员方法 ### add()注册事件 public function add($fd, $flag, $func, $args = null); > $fd:文件描述符 > $flag:事件类型 > $func:事件处理接口 > $args:参数 * * * * * ### del():注销事件 public function del($fd, $flag); > $fd:文件描述符 > $flag:事件类型 ### clearAllTimer():移除所有定时器事件 public function clearAllTimer(); ### loop():开启事件循环 public function loop(); ## 3 Ev事件机制(Ev.php) >[info] 成员变量 ~~~ ;所有读写事件,信号事件,定时器事件 protected $_allEvents = array(); protected $_eventSignal = array(); protected $_eventTimer = array(); ;定时器id protected static $_timerId = 1; ~~~ >[info] 成员方法 ### add($fd, $flag, $func, $args = null)注册事件 `public function add($fd, $flag, $func, $args = null)` > $fd:文件描述符 > $flag:事件类型 > $func:事件回调函数 > $args:参数 ### del($fd, $flag) 注销事件 `public function del($fd, $flag)` > $fd:文件描述符 > $flag:事件类型 ### timerCallback($event) 定时器回调接口 `public function timerCallback($event)` > $event:事件参数 ### clearAllTimer() 移除所有定时器 `public function clearAllTimer()` ### loop() 开启事件循环 `public function loop()` ## 4 Event事件机制(Event.php) >[info] 成员变量 ~~~ ;事件对象 protected $_eventBase = null; ;读写事件,信号事件,定时器事件 protected $_allEvents = array(); protected $_eventSignal = array(); protected $_eventTimer = array(); ;定时器id protected static $_timerId = 1; ~~~ >[info] 成员方法 ### __construct():构造函数 `public function __construct()` ### add($fd, $flag, $func, $args=array()):注册事件 `public function add($fd, $flag, $func, $args=array())` ### del($fd, $flag):注销事件 `public function del($fd, $flag)` ### timerCallback($fd, $what, $param):定时器回调 `public function timerCallback($fd, $what, $param)` ### clearAllTimer():清除所有定时 public function clearAllTimer() ### loop():开启事件循环 `public function loop()` ## 5 Livevent事件机制 >[info] 成员变量 ~~~ ;事件对象 protected $_eventBase = null; ;读写事件,信号事件,定时器事件 protected $_allEvents = array(); protected $_eventSignal = array(); protected $_eventTimer = array(); ~~~ >[info] 成员方法 ### __construct():构造函数 ` public function __construct()` ### add($fd, $flag, $func, $args = array()):注册事件 `public function add($fd, $flag, $func, $args = array())` ### del($fd, $flag:注销事件 `public function del($fd, $flag)` ### timerCallback($_null1, $_null2, $timer_id):定时器回调 `protected function timerCallback($_null1, $_null2, $timer_id)` ### clearAllTimer():清除定时器事件 `public function clearAllTimer()` ### loop():开启事件循环 `public function loop()` ## 6 Select事件机制 >[info] 成员变量 ~~~ ;读写事件,信号事件 public $_allEvents = array(); public $_signalEvents = array(); ;读描述符,写描述符 protected $_readFds = array(); protected $_writeFds = array(); ;定时器调度栈,定时器监听, protected $_scheduler = null; protected $_task = array(); ;定时器id protected $_timerId = 1; ;延时 protected $_selectTimeout = 100000000; ;socket管道 protected $channel = array(); ~~~ >[info] 成员方法 ### __construct():构造函数 public function __construct() ### add($fd, $flag, $func, $args = array()):注册事件 public function add($fd, $flag, $func, $args = array()) ### signalHandler($signal):信号处理接口 public function signalHandler($signal) ### del($fd, $flag):注销事件 public function del($fd, $flag) ### tick():定时器调度 protected function tick() ### clearAllTimer():清除定时器 public function clearAllTimer() ### loop():开启事件循环 public function loop() ### 7 事件总结 事件机制实现对socket描述的事件回调处理 EventInterface.php 事件接口 Ev.php libev事件接口 Event.php Event扩展事件接口 Libevent.php Livevent扩展事件接口 Select.php Select事件接口 有关事件扩展机制 见基础原理 事件循环