|
@@ -12,6 +12,7 @@
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
*/
|
|
*/
|
|
|
namespace Workerman\Events\React;
|
|
namespace Workerman\Events\React;
|
|
|
|
|
+use Workerman\Events\EventInterface;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Class ExtEventLoop
|
|
* Class ExtEventLoop
|
|
@@ -34,6 +35,90 @@ class ExtEventLoop extends \React\EventLoop\ExtEventLoop
|
|
|
protected $_signalEvents = array();
|
|
protected $_signalEvents = array();
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * @var array
|
|
|
|
|
+ */
|
|
|
|
|
+ protected $_timerIdMap = array();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @var int
|
|
|
|
|
+ */
|
|
|
|
|
+ protected $_timerIdIndex = 0;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Add event listener to event loop.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param $fd
|
|
|
|
|
+ * @param $flag
|
|
|
|
|
+ * @param $func
|
|
|
|
|
+ * @param array $args
|
|
|
|
|
+ * @return bool
|
|
|
|
|
+ */
|
|
|
|
|
+ public function add($fd, $flag, $func, $args = array())
|
|
|
|
|
+ {
|
|
|
|
|
+ $args = (array)$args;
|
|
|
|
|
+ switch ($flag) {
|
|
|
|
|
+ case EventInterface::EV_READ:
|
|
|
|
|
+ return $this->addReadStream($fd, $func);
|
|
|
|
|
+ case EventInterface::EV_WRITE:
|
|
|
|
|
+ return $this->addWriteStream($fd, $func);
|
|
|
|
|
+ case EventInterface::EV_SIGNAL:
|
|
|
|
|
+ return $this->addSignal($fd, $func);
|
|
|
|
|
+ case EventInterface::EV_TIMER:
|
|
|
|
|
+ $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
|
|
|
|
|
+ call_user_func_array($func, $args);
|
|
|
|
|
+ });
|
|
|
|
|
+ $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
|
|
|
|
|
+ return $this->_timerIdIndex;
|
|
|
|
|
+ case EventInterface::EV_TIMER_ONCE:
|
|
|
|
|
+ $timer_obj = $this->addTimer($fd, function() use ($func, $args) {
|
|
|
|
|
+ call_user_func_array($func, $args);
|
|
|
|
|
+ });
|
|
|
|
|
+ $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
|
|
|
|
|
+ return $this->_timerIdIndex;
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Remove event listener from event loop.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param mixed $fd
|
|
|
|
|
+ * @param int $flag
|
|
|
|
|
+ * @return bool
|
|
|
|
|
+ */
|
|
|
|
|
+ public function del($fd, $flag)
|
|
|
|
|
+ {
|
|
|
|
|
+ switch ($flag) {
|
|
|
|
|
+ case EventInterface::EV_READ:
|
|
|
|
|
+ return $this->removeReadStream($fd);
|
|
|
|
|
+ case EventInterface::EV_WRITE:
|
|
|
|
|
+ return $this->removeWriteStream($fd);
|
|
|
|
|
+ case EventInterface::EV_SIGNAL:
|
|
|
|
|
+ return $this->removeSignal($fd);
|
|
|
|
|
+ case EventInterface::EV_TIMER:
|
|
|
|
|
+ case EventInterface::EV_TIMER_ONCE;
|
|
|
|
|
+ if (isset($this->_timerIdMap[$fd])){
|
|
|
|
|
+ $timer_obj = $this->_timerIdMap[$fd];
|
|
|
|
|
+ unset($this->_timerIdMap[$fd]);
|
|
|
|
|
+ $this->cancelTimer($timer_obj);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Main loop.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ public function loop()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->run();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* Construct
|
|
* Construct
|
|
|
*/
|
|
*/
|
|
|
public function __construct()
|
|
public function __construct()
|
|
@@ -73,4 +158,16 @@ class ExtEventLoop extends \React\EventLoop\ExtEventLoop
|
|
|
unset($this->_signalEvents[$signal]);
|
|
unset($this->_signalEvents[$signal]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Destroy loop.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ public function destroy()
|
|
|
|
|
+ {
|
|
|
|
|
+ foreach ($this->_signalEvents as $event) {
|
|
|
|
|
+ $event->del();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|