| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- declare(strict_types=1);
- namespace Workerman\Events;
- use RuntimeException;
- use Swow\Coroutine;
- use Swow\Signal;
- use Swow\SignalException;
- use Throwable;
- use function Swow\Sync\waitAll;
- class Swow implements EventInterface
- {
- /**
- * All listeners for read timer
- * @var array
- */
- protected array $eventTimer = [];
- /**
- * All listeners for read event.
- * @var array<Coroutine>
- */
- protected array $readEvents = [];
- /**
- * All listeners for write event.
- * @var array<Coroutine>
- */
- protected array $writeEvents = [];
- /**
- * All listeners for signal.
- * @var array<Coroutine>
- */
- protected array $signalListener = [];
- /**
- * @var ?callable
- */
- protected $errorHandler = null;
- /**
- * Get timer count.
- *
- * @return integer
- */
- public function getTimerCount(): int
- {
- return count($this->eventTimer);
- }
- /**
- * {@inheritdoc}
- * @throws Throwable
- */
- public function delay(float $delay, callable $func, array $args = []): int
- {
- $t = (int)($delay * 1000);
- $t = max($t, 1);
- $that = $this;
- $coroutine = Coroutine::run(function () use ($t, $func, $args, $that): void {
- msleep($t);
- unset($this->eventTimer[Coroutine::getCurrent()->getId()]);
- try {
- $func(...$args);
- } catch (Throwable $e) {
- $that->error($e);
- }
- });
- $timerId = $coroutine->getId();
- $this->eventTimer[$timerId] = $timerId;
- return $timerId;
- }
- /**
- * {@inheritdoc}
- * @throws Throwable
- */
- public function repeat(float $interval, callable $func, array $args = []): int
- {
- $t = (int)($interval * 1000);
- $t = max($t, 1);
- $that = $this;
- $coroutine = Coroutine::run(static function () use ($t, $func, $args, $that): void {
- // @phpstan-ignore-next-line While loop condition is always true.
- while (true) {
- msleep($t);
- try {
- $func(...$args);
- } catch (Throwable $e) {
- $that->error($e);
- }
- }
- });
- $timerId = $coroutine->getId();
- $this->eventTimer[$timerId] = $timerId;
- return $timerId;
- }
- /**
- * {@inheritdoc}
- */
- public function offDelay(int $timerId): bool
- {
- if (isset($this->eventTimer[$timerId])) {
- try {
- (Coroutine::getAll()[$timerId])->kill();
- return true;
- } finally {
- unset($this->eventTimer[$timerId]);
- }
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public function offRepeat(int $timerId): bool
- {
- return $this->offDelay($timerId);
- }
- /**
- * {@inheritdoc}
- */
- public function deleteAllTimer(): void
- {
- foreach ($this->eventTimer as $timerId) {
- $this->offDelay($timerId);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function onReadable($stream, callable $func): void
- {
- $fd = (int)$stream;
- if (isset($this->readEvents[$fd])) {
- $this->offReadable($stream);
- }
- Coroutine::run(function () use ($stream, $func, $fd): void {
- try {
- $this->readEvents[$fd] = Coroutine::getCurrent();
- while (true) {
- if (!is_resource($stream)) {
- $this->offReadable($stream);
- break;
- }
- $rEvent = stream_poll_one($stream, STREAM_POLLIN | STREAM_POLLHUP);
- if (!isset($this->readEvents[$fd]) || $this->readEvents[$fd] !== Coroutine::getCurrent()) {
- break;
- }
- if ($rEvent !== STREAM_POLLNONE) {
- $func($stream);
- }
- if ($rEvent !== STREAM_POLLIN) {
- $this->offReadable($stream);
- break;
- }
- }
- } catch (RuntimeException) {
- $this->offReadable($stream);
- }
- });
- }
- /**
- * {@inheritdoc}
- */
- public function offReadable($stream): bool
- {
- // 在当前协程执行 $coroutine->kill() 会导致不可预知问题,所以没有使用$coroutine->kill()
- $fd = (int)$stream;
- if (isset($this->readEvents[$fd])) {
- unset($this->readEvents[$fd]);
- return true;
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public function onWritable($stream, callable $func): void
- {
- $fd = (int)$stream;
- if (isset($this->writeEvents[$fd])) {
- $this->offWritable($stream);
- }
- Coroutine::run(function () use ($stream, $func, $fd): void {
- try {
- $this->writeEvents[$fd] = Coroutine::getCurrent();
- while (true) {
- $rEvent = stream_poll_one($stream, STREAM_POLLOUT | STREAM_POLLHUP);
- if (!isset($this->writeEvents[$fd]) || $this->writeEvents[$fd] !== Coroutine::getCurrent()) {
- break;
- }
- if ($rEvent !== STREAM_POLLNONE) {
- $func($stream);
- }
- if ($rEvent !== STREAM_POLLOUT) {
- $this->offWritable($stream);
- break;
- }
- }
- } catch (RuntimeException) {
- $this->offWritable($stream);
- }
- });
- }
- /**
- * {@inheritdoc}
- */
- public function offWritable($stream): bool
- {
- $fd = (int)$stream;
- if (isset($this->writeEvents[$fd])) {
- unset($this->writeEvents[$fd]);
- return true;
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public function onSignal(int $signal, callable $func): void
- {
- Coroutine::run(function () use ($signal, $func): void {
- $this->signalListener[$signal] = Coroutine::getCurrent();
- while (1) {
- try {
- Signal::wait($signal);
- if (!isset($this->signalListener[$signal]) ||
- $this->signalListener[$signal] !== Coroutine::getCurrent()) {
- break;
- }
- $func($signal);
- } catch (SignalException) {
- }
- }
- });
- }
- /**
- * {@inheritdoc}
- */
- public function offSignal(int $signal): bool
- {
- if (!isset($this->signalListener[$signal])) {
- return false;
- }
- unset($this->signalListener[$signal]);
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function run(): void
- {
- waitAll();
- }
- /**
- * Destroy loop.
- *
- * @return void
- */
- public function stop(): void
- {
- Coroutine::killAll();
- }
- /**
- * {@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
- {
- if (!$this->errorHandler) {
- throw new $e;
- }
- ($this->errorHandler)($e);
- }
- }
|