| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?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
- */
- namespace Workerman\Events;
- use Workerman\Worker;
- /**
- * libevent eventloop
- */
- class Event implements EventInterface
- {
- /**
- * Event base.
- * @var object
- */
- protected $eventBase = null;
- /**
- * All listeners for read event.
- * @var array
- */
- protected $readEvents = [];
- /**
- * All listeners for write event.
- * @var array
- */
- protected $writeEvents = [];
- /**
- * Event listeners of signal.
- * @var array
- */
- protected $eventSignal = [];
- /**
- * All timer event listeners.
- * [func, args, event, flag, time_interval]
- * @var array
- */
- protected $eventTimer = [];
- /**
- * Timer id.
- * @var int
- */
- protected $timerId = 0;
- /**
- * Event class name.
- * @var string
- */
- protected $eventClassName = '';
- /**
- * Construct.
- * @return void
- */
- public function __construct()
- {
- if (\class_exists('\\\\Event', false)) {
- $className = '\\\\Event';
- } else {
- $className = '\Event';
- }
- $this->eventClassName = $className;
- if (\class_exists('\\\\EventBase', false)) {
- $className = '\\\\EventBase';
- } else {
- $className = '\EventBase';
- }
- $this->eventBase = new $className();
- }
- /**
- * {@inheritdoc}
- */
- public function delay(float $delay, $func, $args = [])
- {
- $className = $this->eventClassName;
- $timerId = $this->timerId++;
- $event = new $className($this->eventBase, -1, $className::TIMEOUT, function () use ($func, $args, $timerId) {
- try {
- $this->offDelay($timerId);
- $func(...$args);
- } catch (\Throwable $e) {
- Worker::stopAll(250, $e);
- }
- });
- if (!$event || !$event->addTimer($delay)) {
- return false;
- }
- $this->eventTimer[$timerId] = $event;
- return $timerId;
- }
- /**
- * {@inheritdoc}
- */
- public function offDelay($timerId)
- {
- if (isset($this->eventTimer[$timerId])) {
- $this->eventTimer[$timerId]->del();
- unset($this->eventTimer[$timerId]);
- return true;
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public function offRepeat($timerId)
- {
- return $this->offDelay($timerId);
- }
- /**
- * {@inheritdoc}
- */
- public function repeat(float $interval, $func, $args = [])
- {
- $className = $this->eventClassName;
- $timerId = $this->timerId++;
- $event = new $className($this->eventBase, -1, $className::TIMEOUT | $className::PERSIST, function () use ($func, $args) {
- try {
- $func(...$args);
- } catch (\Throwable $e) {
- Worker::stopAll(250, $e);
- }
- });
- if (!$event || !$event->addTimer($interval)) {
- return false;
- }
- $this->eventTimer[$timerId] = $event;
- return $timerId;
- }
- /**
- * {@inheritdoc}
- */
- public function onReadable($stream, $func)
- {
- $className = $this->eventClassName;
- $fdKey = (int)$stream;
- $event = new $this->eventClassName($this->eventBase, $stream, $className::READ | $className::PERSIST, $func, $stream);
- if (!$event || !$event->add()) {
- return false;
- }
- $this->writeEvents[$fdKey] = $event;
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function offReadable($stream)
- {
- $fdKey = (int)$stream;
- if (isset($this->readEvents[$fdKey])) {
- $this->readEvents[$fdKey]->del();
- unset($this->readEvents[$fdKey]);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function onWritable($stream, $func)
- {
- $className = $this->eventClassName;
- $fdKey = (int)$stream;
- $event = new $this->eventClassName($this->eventBase, $stream, $className::WRITE | $className::PERSIST, $func, $stream);
- if (!$event || !$event->add()) {
- return false;
- }
- $this->writeEvents[$fdKey] = $event;
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function offWritable($stream)
- {
- $fdKey = (int)$stream;
- if (isset($this->writeEvents[$fdKey])) {
- $this->writeEvents[$fdKey]->del();
- unset($this->writeEvents[$fdKey]);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function onSignal($signal, $func)
- {
- $className = $this->eventClassName;
- $fdKey = (int)$signal;
- $event = $className::signal($this->eventBase, $signal, $func);
- if (!$event || !$event->add()) {
- return false;
- }
- $this->eventSignal[$fdKey] = $event;
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function offSignal($signal)
- {
- $fdKey = (int)$signal;
- if (isset($this->eventSignal[$fdKey])) {
- $this->eventSignal[$fdKey]->del();
- unset($this->eventSignal[$fdKey]);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function deleteAllTimer()
- {
- foreach ($this->eventTimer as $event) {
- $event->del();
- }
- $this->eventTimer = [];
- }
- /**
- * {@inheritdoc}
- */
- public function run()
- {
- $this->eventBase->loop();
- }
- /**
- * {@inheritdoc}
- */
- public function stop()
- {
- $this->eventBase->exit();
- }
- /**
- * {@inheritdoc}
- */
- public function getTimerCount()
- {
- return \count($this->eventTimer);
- }
- }
|