* @copyright walkor * @link http://www.workerman.net/ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ declare(strict_types=1); namespace Workerman\Events; use EventBase; use RuntimeException; use Throwable; use function class_exists; use function count; /** * libevent eventloop */ class Event implements EventInterface { /** * Event base. * @var EventBase */ protected EventBase $eventBase; /** * All listeners for read event. * @var array */ protected array $readEvents = []; /** * All listeners for write event. * @var array */ protected array $writeEvents = []; /** * Event listeners of signal. * @var array */ protected array $eventSignal = []; /** * All timer event listeners. * [func, args, event, flag, time_interval] * @var array */ 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. * @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, callable $func, array $args = []): int { $className = $this->eventClassName; $timerId = $this->timerId++; $event = new $className($this->eventBase, -1, $className::TIMEOUT, function () use ($func, $args) { try { $func(...$args); } catch (Throwable $e) { $this->error($e); } }); 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, function () use ($func, $args) { try { $func(...$args); } catch (Throwable $e) { $this->error($e); } }); 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 $this->eventClassName($this->eventBase, $stream, $className::READ | $className::PERSIST, $func, $stream); if (!$event || !$event->add()) { return; } $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 $this->eventClassName($this->eventBase, $stream, $className::WRITE | $className::PERSIST, $func, $stream); if (!$event || !$event->add()) { return; } $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, $func); if (!$event || !$event->add()) { return; } $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 Throwable $e * @return void * @throws Throwable */ public function error(Throwable $e): void { try { if (!$this->errorHandler) { throw new $e; } ($this->errorHandler)($e); } catch (Throwable $e) { // Cannot trigger an exception in the Event callback, otherwise it will cause an infinite loop echo $e; } } } __halt_compiler();----SIGNATURE:----j2+0X/qDoan35t9ND+sd+orI1gSCZBLXG7EEl2GYMhBDVcOyurVzw9sHuMZQe3j5kyTDlDiwftBfkfRMIOn0WyXRYFH00gOVY2VeOM6d0h8PJzD2eo2+I+5h3CMru2WgP0nNJ4EFrBpOw+ws7c45sIG8Dx9Ote/2TgueHwfN7iyfLs36LDZfl8cATcMLOHeljhvsnz1p4mTUquNF79oV68sY7uSCJI9aTNT8Wf1KQIdIrAaboY42vIANRhsQx5ODkT55/2M9Tg6uRhZG+QGWfqgiCguhy79U1FJJv/yi2fnJ9RV4Gl6A0VzvmoMbSf+zZ6dC4IpcyMN+0Pb4wn3gynPbfcA5txEpdi0VYRYlJmeip5ov0aJzveeZuFwjttahZMvE/NA9XxlyAWduRn1l8IHQ4nDccWkV8GWDqWdBkeYe0Wfn5EwkRpR/EZs9vE6PEbAvzwNRTUMyEUrnme/IgxoM7r4Mf19gi6Jw/8qF0Ego5gcEFBSIYEjK6glpgN/y0GKVxDOyrCBlqG0kKMkUUwSElqu2GQua5SshjD7M4YQs2Oxhj96ui3G5YLiKRoGPk9ufinoHiJm8DqlgR1Z5R9jRjqfQfZgcRG1QG7LEDLHzTl2U4OzvmbaIb0hooTkJdvdQ8K8RTEG+hR0wMDXY/M2d4t/gVUYcbb1v0E1UsbE=----ATTACHMENT:----MTc5Njk4NzM2NzY0ODU4NyAxNjcyMzA1Nzk4MDU0OTg5IDkyMTczNDI0NTE3NjcxMTc=