1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 17:37:34 +01:00
amp/lib/Loop/EventDriver.php

259 lines
7.1 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Loop;
use Amp\Coroutine;
use Amp\Promise;
use Amp\Internal\Watcher;
use React\Promise\PromiseInterface as ReactPromise;
use function Amp\Promise\rethrow;
class EventDriver extends Driver {
/** @var \Event[]|null */
private static $activeSignals;
2017-02-17 05:36:32 +01:00
/** @var \EventBase */
private $handle;
2017-02-17 05:36:32 +01:00
/** @var \Event[] */
private $events = [];
2017-02-17 05:36:32 +01:00
/** @var callable */
private $ioCallback;
2017-02-17 05:36:32 +01:00
/** @var callable */
private $timerCallback;
2017-02-17 05:36:32 +01:00
/** @var callable */
private $signalCallback;
2017-02-17 05:36:32 +01:00
/** @var \Event[] */
private $signals = [];
2017-03-10 19:19:32 +01:00
public function __construct() {
$this->handle = new \EventBase;
2017-03-10 19:19:32 +01:00
if (self::$activeSignals === null) {
self::$activeSignals = &$this->signals;
}
2017-03-10 19:19:32 +01:00
$this->ioCallback = function ($resource, $what, Watcher $watcher) {
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
$this->timerCallback = function ($resource, $what, Watcher $watcher) {
if ($watcher->type & Watcher::DELAY) {
$this->cancel($watcher->id);
}
try {
$result = ($watcher->callback)($watcher->id, $watcher->data);
2017-03-12 18:03:13 +01:00
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
$this->signalCallback = function ($signum, $what, Watcher $watcher) {
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
}
2017-03-10 19:19:32 +01:00
/**
* {@inheritdoc}
*/
public function cancel(string $watcherId) {
parent::cancel($watcherId);
if (isset($this->events[$watcherId])) {
$this->events[$watcherId]->free();
unset($this->events[$watcherId]);
}
}
public static function isSupported(): bool {
return \extension_loaded("event");
}
public function __destruct() {
foreach ($this->events as $event) {
$event->free();
}
}
2017-03-10 19:19:32 +01:00
/**
* {@inheritdoc}
*/
public function run() {
$active = self::$activeSignals;
2017-03-10 19:19:32 +01:00
foreach ($active as $event) {
$event->del();
}
2017-03-10 19:19:32 +01:00
self::$activeSignals = &$this->signals;
2017-03-10 19:19:32 +01:00
foreach ($this->signals as $event) {
$event->add();
}
2017-03-10 19:19:32 +01:00
try {
parent::run();
} finally {
foreach ($this->signals as $event) {
$event->del();
}
2017-03-10 19:19:32 +01:00
self::$activeSignals = &$active;
2017-03-10 19:19:32 +01:00
foreach ($active as $event) {
$event->add();
}
}
}
/**
* {@inheritdoc}
*/
public function stop() {
$this->handle->stop();
parent::stop();
}
/**
* {@inheritdoc}
*/
public function getHandle(): \EventBase {
return $this->handle;
}
/**
* {@inheritdoc}
*/
protected function dispatch(bool $blocking) {
$this->handle->loop($blocking ? \EventBase::LOOP_ONCE : \EventBase::LOOP_ONCE | \EventBase::LOOP_NONBLOCK);
}
/**
* {@inheritdoc}
*/
protected function activate(array $watchers) {
foreach ($watchers as $watcher) {
if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) {
case Watcher::READABLE:
$this->events[$id] = new \Event(
$this->handle,
$watcher->value,
\Event::READ | \Event::PERSIST,
$this->ioCallback,
$watcher
);
break;
case Watcher::WRITABLE:
$this->events[$id] = new \Event(
$this->handle,
$watcher->value,
\Event::WRITE | \Event::PERSIST,
$this->ioCallback,
$watcher
);
break;
case Watcher::DELAY:
case Watcher::REPEAT:
$this->events[$id] = new \Event(
$this->handle,
-1,
\Event::TIMEOUT | \Event::PERSIST,
$this->timerCallback,
$watcher
);
break;
case Watcher::SIGNAL:
$this->events[$id] = new \Event(
$this->handle,
$watcher->value,
\Event::SIGNAL | \Event::PERSIST,
$this->signalCallback,
$watcher
);
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
// @codeCoverageIgnoreEnd
}
}
2017-03-10 19:19:32 +01:00
switch ($watcher->type) {
case Watcher::DELAY:
case Watcher::REPEAT:
$this->events[$id]->add($watcher->value / self::MILLISEC_PER_SEC);
break;
2017-03-10 19:19:32 +01:00
case Watcher::SIGNAL:
$this->signals[$id] = $this->events[$id];
// No break
2017-03-10 19:19:32 +01:00
default:
$this->events[$id]->add();
break;
}
}
}
/**
* {@inheritdoc}
*/
protected function deactivate(Watcher $watcher) {
if (isset($this->events[$id = $watcher->id])) {
$this->events[$id]->del();
2017-03-10 19:19:32 +01:00
if ($watcher->type === Watcher::SIGNAL) {
unset($this->signals[$id]);
}
}
}
}