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