2016-06-08 16:12:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Loop;
|
|
|
|
|
|
|
|
use Amp\Loop\Internal\Watcher;
|
|
|
|
|
|
|
|
class EvLoop extends Loop {
|
|
|
|
/**
|
|
|
|
* @var \EvLoop
|
|
|
|
*/
|
|
|
|
private $handle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \EvWatcher[]
|
|
|
|
*/
|
|
|
|
private $events = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $ioCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $timerCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $signalCallback;
|
2017-01-06 00:32:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \EvSignal[]
|
|
|
|
*/
|
|
|
|
private $signals = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \EvSignal[]|null
|
|
|
|
*/
|
|
|
|
private static $activeSignals;
|
2016-06-08 16:12:42 +02:00
|
|
|
|
2016-06-26 17:35:25 +02:00
|
|
|
public static function supported() {
|
2016-06-08 16:12:42 +02:00
|
|
|
return \extension_loaded("ev");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->handle = new \EvLoop;
|
2017-01-06 00:32:03 +01:00
|
|
|
|
|
|
|
if (self::$activeSignals === null) {
|
|
|
|
self::$activeSignals = &$this->signals;
|
|
|
|
}
|
2016-06-08 19:08:00 +02:00
|
|
|
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->ioCallback = function (\EvIO $event) {
|
|
|
|
/** @var \Amp\Loop\Internal\Watcher $watcher */
|
|
|
|
$watcher = $event->data;
|
|
|
|
|
|
|
|
$callback = $watcher->callback;
|
|
|
|
$callback($watcher->id, $watcher->value, $watcher->data);
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->timerCallback = function (\EvTimer $event) {
|
|
|
|
/** @var \Amp\Loop\Internal\Watcher $watcher */
|
|
|
|
$watcher = $event->data;
|
|
|
|
|
|
|
|
if ($watcher->type & Watcher::DELAY) {
|
|
|
|
$this->cancel($watcher->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$callback = $watcher->callback;
|
|
|
|
$callback($watcher->id, $watcher->data);
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->signalCallback = function (\EvSignal $event) {
|
|
|
|
/** @var \Amp\Loop\Internal\Watcher $watcher */
|
|
|
|
$watcher = $event->data;
|
|
|
|
|
|
|
|
$callback = $watcher->callback;
|
2017-01-05 19:57:35 +01:00
|
|
|
$callback($watcher->id, $watcher->value, $watcher->data);
|
2016-06-08 16:12:42 +02:00
|
|
|
};
|
|
|
|
}
|
2017-01-06 00:38:33 +01:00
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
foreach ($this->events as $event) {
|
|
|
|
$event->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function run() {
|
|
|
|
$active = self::$activeSignals;
|
|
|
|
|
|
|
|
foreach ($active as $event) {
|
|
|
|
$event->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$activeSignals = &$this->signals;
|
|
|
|
|
|
|
|
foreach ($this->signals as $event) {
|
|
|
|
$event->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
parent::run();
|
|
|
|
} finally {
|
|
|
|
foreach ($this->signals as $event) {
|
|
|
|
$event->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$activeSignals = &$active;
|
|
|
|
|
|
|
|
foreach ($active as $event) {
|
|
|
|
$event->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function stop() {
|
|
|
|
$this->handle->stop();
|
|
|
|
parent::stop();
|
|
|
|
}
|
2016-06-08 23:03:37 +02:00
|
|
|
|
2016-06-08 19:08:00 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-06-08 16:12:42 +02:00
|
|
|
protected function dispatch($blocking) {
|
|
|
|
$this->handle->run($blocking ? \Ev::RUN_ONCE : \Ev::RUN_ONCE | \Ev::RUN_NOWAIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Watcher::WRITABLE:
|
|
|
|
$this->events[$id] = $this->handle->io($watcher->value, \Ev::WRITE, $this->ioCallback, $watcher);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Watcher::DELAY:
|
|
|
|
case Watcher::REPEAT:
|
|
|
|
$interval = $watcher->value / self::MILLISEC_PER_SEC;
|
|
|
|
$this->events[$id] = $this->handle->timer(
|
|
|
|
$interval,
|
2016-06-08 19:08:00 +02:00
|
|
|
$watcher->type & Watcher::REPEAT ? $interval : 0,
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->timerCallback,
|
|
|
|
$watcher
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Watcher::SIGNAL:
|
|
|
|
$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \DomainException("Unknown watcher type");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->events[$id]->start();
|
|
|
|
}
|
2017-01-06 00:32:03 +01:00
|
|
|
|
|
|
|
if ($watcher->type === Watcher::SIGNAL) {
|
|
|
|
$this->signals[$id] = $this->events[$id];
|
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-01-05 19:39:10 +01:00
|
|
|
protected function deactivate(Watcher $watcher) {
|
2016-06-08 16:12:42 +02:00
|
|
|
if (isset($this->events[$id = $watcher->id])) {
|
|
|
|
$this->events[$id]->stop();
|
2017-01-06 00:32:03 +01:00
|
|
|
if ($watcher->type === Watcher::SIGNAL) {
|
|
|
|
unset($this->signals[$id]);
|
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 23:03:37 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-06-08 16:12:42 +02:00
|
|
|
public function cancel($watcherIdentifier) {
|
|
|
|
parent::cancel($watcherIdentifier);
|
|
|
|
unset($this->events[$watcherIdentifier]);
|
|
|
|
}
|
2016-06-09 19:59:18 +02:00
|
|
|
|
2016-06-08 16:12:42 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getHandle() {
|
|
|
|
return $this->handle;
|
|
|
|
}
|
|
|
|
}
|