2017-01-05 19:57:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Loop;
|
|
|
|
|
2017-03-10 23:03:41 +01:00
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Promise;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Internal\Watcher;
|
2017-03-11 07:13:03 +01:00
|
|
|
use React\Promise\PromiseInterface as ReactPromise;
|
2017-03-15 17:12:49 +01:00
|
|
|
use function Amp\Promise\rethrow;
|
2017-01-05 19:57:35 +01:00
|
|
|
|
2017-03-12 11:21:44 +01:00
|
|
|
class EventDriver extends Driver {
|
2017-03-10 21:58:46 +01:00
|
|
|
/** @var \Event[]|null */
|
|
|
|
private static $activeSignals;
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var \EventBase */
|
2017-01-05 19:57:35 +01:00
|
|
|
private $handle;
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var \Event[] */
|
2017-01-05 19:57:35 +01:00
|
|
|
private $events = [];
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var callable */
|
2017-01-05 19:57:35 +01:00
|
|
|
private $ioCallback;
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var callable */
|
2017-01-05 19:57:35 +01:00
|
|
|
private $timerCallback;
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var callable */
|
2017-01-05 19:57:35 +01:00
|
|
|
private $signalCallback;
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var \Event[] */
|
2017-01-06 00:32:03 +01:00
|
|
|
private $signals = [];
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-05 19:57:35 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->handle = new \EventBase;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
if (self::$activeSignals === null) {
|
|
|
|
self::$activeSignals = &$this->signals;
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-05 19:57:35 +01:00
|
|
|
$this->ioCallback = function ($resource, $what, Watcher $watcher) {
|
2017-03-14 06:20:05 +01:00
|
|
|
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 23:03:41 +01:00
|
|
|
}
|
2017-01-05 19:57:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$this->timerCallback = function ($resource, $what, Watcher $watcher) {
|
|
|
|
if ($watcher->type & Watcher::DELAY) {
|
|
|
|
$this->cancel($watcher->id);
|
|
|
|
}
|
2017-03-10 23:03:41 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
try {
|
|
|
|
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
2017-03-12 18:03:13 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-10 23:03:41 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
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 23:03:41 +01:00
|
|
|
}
|
2017-01-05 19:57:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$this->signalCallback = function ($signum, $what, Watcher $watcher) {
|
2017-03-14 06:20:05 +01:00
|
|
|
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 23:03:41 +01:00
|
|
|
}
|
2017-01-05 19:57:35 +01:00
|
|
|
};
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-03-10 21:58:46 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function cancel(string $watcherId) {
|
|
|
|
parent::cancel($watcherId);
|
|
|
|
|
|
|
|
if (isset($this->events[$watcherId])) {
|
|
|
|
$this->events[$watcherId]->free();
|
|
|
|
unset($this->events[$watcherId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 11:25:21 +01:00
|
|
|
public static function isSupported(): bool {
|
2017-03-10 21:58:46 +01:00
|
|
|
return \extension_loaded("event");
|
|
|
|
}
|
|
|
|
|
2017-01-05 19:57:35 +01:00
|
|
|
public function __destruct() {
|
|
|
|
foreach ($this->events as $event) {
|
|
|
|
$event->free();
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function run() {
|
|
|
|
$active = self::$activeSignals;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
foreach ($active as $event) {
|
|
|
|
$event->del();
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
self::$activeSignals = &$this->signals;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
foreach ($this->signals as $event) {
|
|
|
|
$event->add();
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
try {
|
|
|
|
parent::run();
|
|
|
|
} finally {
|
|
|
|
foreach ($this->signals as $event) {
|
|
|
|
$event->del();
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
self::$activeSignals = &$active;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
foreach ($active as $event) {
|
|
|
|
$event->add();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 19:57:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function stop() {
|
|
|
|
$this->handle->stop();
|
|
|
|
parent::stop();
|
|
|
|
}
|
|
|
|
|
2017-03-10 21:58:46 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-03-10 22:46:12 +01:00
|
|
|
public function getHandle(): \EventBase {
|
2017-03-10 21:58:46 +01:00
|
|
|
return $this->handle;
|
|
|
|
}
|
|
|
|
|
2017-01-05 19:57:35 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-03-10 22:46:12 +01:00
|
|
|
protected function dispatch(bool $blocking) {
|
2017-01-05 19:57:35 +01:00
|
|
|
$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,
|
2017-02-17 06:39:11 +01:00
|
|
|
\Event::TIMEOUT | \Event::PERSIST,
|
2017-01-05 19:57:35 +01:00
|
|
|
$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:
|
2017-03-14 17:44:19 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
throw new \Error("Unknown watcher type");
|
|
|
|
// @codeCoverageIgnoreEnd
|
2017-01-05 19:57:35 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-05 19:57:35 +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
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
case Watcher::SIGNAL:
|
|
|
|
$this->signals[$id] = $this->events[$id];
|
2017-03-10 21:58:46 +01:00
|
|
|
// No break
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-05 19:57:35 +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
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
if ($watcher->type === Watcher::SIGNAL) {
|
|
|
|
unset($this->signals[$id]);
|
|
|
|
}
|
2017-01-05 19:57:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|