2014-06-11 18:33:15 +02:00
|
|
|
<?php
|
|
|
|
|
2014-09-23 04:38:32 +02:00
|
|
|
namespace Amp;
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2015-03-19 16:14:21 +01:00
|
|
|
class UvReactor implements SignalReactor {
|
2014-06-11 18:33:15 +02:00
|
|
|
private $loop;
|
2015-03-19 16:14:21 +01:00
|
|
|
private $lastWatcherId = "a";
|
2014-08-06 20:17:23 +02:00
|
|
|
private $watchers;
|
2014-12-02 00:08:49 +01:00
|
|
|
private $enabledWatcherCount = 0;
|
2014-11-11 22:07:15 +01:00
|
|
|
private $streamIdPollMap = [];
|
2014-08-06 05:45:33 +02:00
|
|
|
private $isRunning = false;
|
2014-06-11 18:33:15 +02:00
|
|
|
private $stopException;
|
|
|
|
private $resolution = 1000;
|
2014-08-06 05:45:33 +02:00
|
|
|
private $isWindows;
|
2014-08-08 22:39:11 +02:00
|
|
|
private $immediates = [];
|
2014-12-02 00:08:49 +01:00
|
|
|
private $onError;
|
2015-04-03 17:56:16 +02:00
|
|
|
private $onCoroutineResolution;
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
/* Pre-PHP7 closure GC hack vars */
|
|
|
|
private $garbage;
|
|
|
|
private $gcWatcher;
|
|
|
|
private $gcCallback;
|
|
|
|
|
2014-11-26 22:27:30 +01:00
|
|
|
private static $instanceCount = 0;
|
2014-11-11 18:00:01 +01:00
|
|
|
|
2014-12-01 21:39:57 +01:00
|
|
|
public function __construct() {
|
2015-01-06 03:01:07 +01:00
|
|
|
if (!extension_loaded('uv')) {
|
2015-04-03 17:56:16 +02:00
|
|
|
throw new \RuntimeException('php-uv extension is required to use the UvReactor.');
|
2015-01-06 03:01:07 +01:00
|
|
|
}
|
|
|
|
|
2014-12-01 21:39:57 +01:00
|
|
|
$this->loop = uv_loop_new();
|
2014-08-06 05:45:33 +02:00
|
|
|
$this->isWindows = (stripos(PHP_OS, 'win') === 0);
|
2015-04-30 19:41:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prior to PHP7 we can't cancel closure watchers inside their own callbacks
|
|
|
|
* because PHP will fatal. In legacy versions we schedule manual GC workarounds.
|
|
|
|
*
|
|
|
|
* @link https://bugs.php.net/bug.php?id=62452
|
|
|
|
*/
|
|
|
|
if (PHP_MAJOR_VERSION < 7) {
|
|
|
|
$this->garbage = [];
|
|
|
|
$this->gcWatcher = uv_timer_init($this->loop);
|
|
|
|
$this->gcCallback = function() {
|
|
|
|
$this->garbage = [];
|
|
|
|
$this->isGcScheduled = false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->onCoroutineResolution = function($e = null, $r = null) {
|
2015-04-30 19:41:14 +02:00
|
|
|
if ($e) {
|
|
|
|
$this->onCallbackError($e);
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
};
|
2015-04-30 19:41:14 +02:00
|
|
|
|
2014-11-26 22:27:30 +01:00
|
|
|
self::$instanceCount++;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2014-08-06 05:45:33 +02:00
|
|
|
public function run(callable $onStart = null) {
|
2014-06-11 18:33:15 +02:00
|
|
|
if ($this->isRunning) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-06 06:17:03 +02:00
|
|
|
$this->isRunning = true;
|
2014-08-08 20:15:23 +02:00
|
|
|
if ($onStart) {
|
2014-10-23 00:14:01 +02:00
|
|
|
$this->immediately($onStart);
|
2014-08-08 20:15:23 +02:00
|
|
|
}
|
2014-08-08 22:39:11 +02:00
|
|
|
|
|
|
|
while ($this->isRunning) {
|
|
|
|
if ($this->immediates && !$this->doImmediates()) {
|
|
|
|
break;
|
|
|
|
}
|
2014-12-02 00:08:49 +01:00
|
|
|
if (empty($this->enabledWatcherCount)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-01-21 21:39:12 +01:00
|
|
|
uv_run($this->loop, \UV::RUN_DEFAULT | (empty($this->immediates) ? \UV::RUN_ONCE : \UV::RUN_NOWAIT));
|
2014-08-08 22:39:11 +02:00
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
|
|
|
|
if ($this->stopException) {
|
|
|
|
$e = $this->stopException;
|
2014-08-06 05:45:33 +02:00
|
|
|
$this->stopException = null;
|
2014-06-11 18:33:15 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:39:11 +02:00
|
|
|
private function doImmediates() {
|
|
|
|
$immediates = $this->immediates;
|
2015-04-03 17:56:16 +02:00
|
|
|
foreach ($immediates as $watcherId => $watcher) {
|
2014-12-02 00:08:49 +01:00
|
|
|
try {
|
|
|
|
$this->enabledWatcherCount--;
|
|
|
|
unset(
|
|
|
|
$this->immediates[$watcherId],
|
|
|
|
$this->watchers[$watcherId]
|
|
|
|
);
|
2015-04-30 19:41:14 +02:00
|
|
|
$result = call_user_func($watcher->callback, $this, $watcherId, $watcher->callbackData);
|
2014-12-02 00:08:49 +01:00
|
|
|
if ($result instanceof \Generator) {
|
2015-04-03 17:56:16 +02:00
|
|
|
resolve($result, $this)->when($this->onCoroutineResolution);
|
2014-12-02 00:08:49 +01:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2015-04-30 19:41:14 +02:00
|
|
|
$this->onCallbackError($e);
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
2014-12-02 00:08:49 +01:00
|
|
|
|
2014-08-08 22:39:11 +02:00
|
|
|
if (!$this->isRunning) {
|
|
|
|
// If a watcher stops the reactor break out of the loop
|
2014-12-02 00:08:49 +01:00
|
|
|
return false;
|
2014-08-08 22:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isRunning;
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:33:15 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function tick($noWait = false) {
|
2014-08-08 22:39:11 +02:00
|
|
|
if ($this->isRunning) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
$noWait = (bool) $noWait;
|
2014-08-06 06:17:03 +02:00
|
|
|
$this->isRunning = true;
|
2014-08-08 22:39:11 +02:00
|
|
|
|
|
|
|
if (empty($this->immediates) || $this->doImmediates()) {
|
2015-01-21 21:39:12 +01:00
|
|
|
$flags = $noWait || !empty($this->immediates) ? (\UV::RUN_NOWAIT | \UV::RUN_ONCE) : \UV::RUN_ONCE;
|
2014-11-26 19:42:36 +01:00
|
|
|
uv_run($this->loop, $flags);
|
2014-08-08 22:39:11 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 05:45:33 +02:00
|
|
|
$this->isRunning = false;
|
2014-06-11 18:33:15 +02:00
|
|
|
|
|
|
|
if ($this->stopException) {
|
|
|
|
$e = $this->stopException;
|
2014-08-06 05:45:33 +02:00
|
|
|
$this->stopException = null;
|
2014-06-11 18:33:15 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
|
|
|
public function stop() {
|
|
|
|
uv_stop($this->loop);
|
2014-08-08 22:39:11 +02:00
|
|
|
$this->isRunning = false;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function immediately(callable $callback, array $options = []) {
|
|
|
|
$watcher = new \StdClass;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->id = $watcherId = $this->lastWatcherId++;
|
2014-11-26 22:27:30 +01:00
|
|
|
$watcher->type = Watcher::IMMEDIATE;
|
2014-08-08 22:39:11 +02:00
|
|
|
$watcher->callback = $callback;
|
2015-05-20 21:21:40 +02:00
|
|
|
$watcher->callbackData = @$options["cb_data"];
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-04-03 17:56:16 +02:00
|
|
|
|
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
$this->enabledWatcherCount++;
|
|
|
|
$this->immediates[$watcherId] = $watcher;
|
|
|
|
}
|
2014-08-08 22:39:11 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
2014-08-08 22:39:11 +02:00
|
|
|
|
|
|
|
return $watcherId;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function once(callable $callback, $msDelay, array $options = []) {
|
2015-04-03 17:56:16 +02:00
|
|
|
assert(($msDelay >= 0), "\$msDelay at Argument 2 expects integer >= 0");
|
|
|
|
return $this->registerTimer($callback, $msDelay, $msInterval = -1, $options);
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function repeat(callable $callback, $msInterval, array $options = []) {
|
2015-04-03 17:56:16 +02:00
|
|
|
assert(($msInterval >= 0), "\$msInterval at Argument 2 expects integer >= 0");
|
2015-04-30 19:41:14 +02:00
|
|
|
$msDelay = isset($options["ms_delay"]) ? $options["ms_delay"] : $msInterval;
|
|
|
|
assert(($msDelay >= 0), "ms_delay option expects integer >= 0");
|
2015-04-03 17:56:16 +02:00
|
|
|
|
|
|
|
// libuv interprets a zero interval as "non-repeating." Because we support
|
|
|
|
// zero-time repeat intervals in our other event reactors we hack in support
|
|
|
|
// for this by assigning a 1ms interval when zero is passed by the user.
|
|
|
|
if ($msInterval === 0) {
|
|
|
|
$msInterval = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->registerTimer($callback, $msDelay, $msInterval, $options);
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function registerTimer(callable $callback, $msDelay, $msInterval, array $options) {
|
2015-04-03 17:56:16 +02:00
|
|
|
$isRepeating = ($msInterval !== -1);
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher = new \StdClass;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->id = $watcherId = $this->lastWatcherId++;
|
2015-05-25 18:47:04 +02:00
|
|
|
$watcher->type = ($isRepeating) ? Watcher::TIMER_REPEAT : Watcher::TIMER_ONCE;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->uvHandle = uv_timer_init($this->loop);
|
2014-06-11 18:33:15 +02:00
|
|
|
$watcher->callback = $this->wrapTimerCallback($watcher, $callback);
|
2015-05-20 21:21:40 +02:00
|
|
|
$watcher->callbackData = @$options["cb_data"];
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->msDelay = $msDelay;
|
|
|
|
$watcher->msInterval = $isRepeating ? $msInterval : 0;
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
if ($watcher->isEnabled) {
|
2015-04-30 19:41:14 +02:00
|
|
|
$this->enabledWatcherCount++;
|
2015-04-03 17:56:16 +02:00
|
|
|
uv_timer_start($watcher->uvHandle, $watcher->msDelay, $watcher->msInterval, $watcher->callback);
|
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
return $watcherId;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function wrapTimerCallback($watcher, $callback) {
|
2014-06-11 18:33:15 +02:00
|
|
|
return function() use ($watcher, $callback) {
|
|
|
|
try {
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcherId = $watcher->id;
|
2015-04-30 19:41:14 +02:00
|
|
|
$result = call_user_func($callback, $this, $watcherId, $watcher->callbackData);
|
2014-09-22 22:47:48 +02:00
|
|
|
if ($result instanceof \Generator) {
|
2015-04-03 17:56:16 +02:00
|
|
|
resolve($result, $this)->when($this->onCoroutineResolution);
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
2015-01-19 02:13:08 +01:00
|
|
|
// The isset() check is necessary because the "once" timer
|
|
|
|
// callback may have cancelled itself when it was invoked.
|
2015-04-03 17:56:16 +02:00
|
|
|
if ($watcher->type === Watcher::TIMER_ONCE && isset($this->watchers[$watcherId])) {
|
|
|
|
$this->clearWatcher($watcherId);
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2015-04-30 19:41:14 +02:00
|
|
|
$this->onCallbackError($e);
|
2014-12-02 00:08:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function onCallbackError(\Exception $e) {
|
|
|
|
if (empty($this->onError)) {
|
|
|
|
$this->stopException = $e;
|
|
|
|
$this->stop();
|
|
|
|
} else {
|
|
|
|
$this->tryUserErrorCallback($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 18:47:04 +02:00
|
|
|
private function tryUserErrorCallback(\Exception $e) {
|
2014-12-02 00:08:49 +01:00
|
|
|
try {
|
2015-04-30 19:41:14 +02:00
|
|
|
call_user_func($this->onError, $e);
|
2014-12-02 00:08:49 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->stopException = $e;
|
|
|
|
$this->stop();
|
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function onReadable($stream, callable $callback, array $options = []) {
|
2015-04-03 17:56:16 +02:00
|
|
|
return $this->watchStream($stream, $callback, Watcher::IO_READER, $options);
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function onWritable($stream, callable $callback, array $options = []) {
|
2015-04-03 17:56:16 +02:00
|
|
|
return $this->watchStream($stream, $callback, Watcher::IO_WRITER, $options);
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function watchStream($stream, callable $callback, $type, array $options) {
|
|
|
|
$watcherId = $this->lastWatcherId++;
|
|
|
|
$watcher = new \StdClass;
|
|
|
|
$watcher->id = $watcherId;
|
2014-11-26 22:27:30 +01:00
|
|
|
$watcher->type = $type;
|
2014-11-11 22:07:15 +01:00
|
|
|
$watcher->callback = $callback;
|
2015-05-20 21:21:40 +02:00
|
|
|
$watcher->callbackData = @$options["cb_data"];
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->stream = $stream;
|
|
|
|
$watcher->streamId = $streamId = (int) $stream;
|
|
|
|
$watcher->poll = $poll = isset($this->streamIdPollMap[$streamId])
|
|
|
|
? $this->streamIdPollMap[$streamId]
|
|
|
|
: $this->makePollHandle($stream);
|
2014-11-11 22:07:15 +01:00
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
if (!$watcher->isEnabled) {
|
2014-11-11 22:07:15 +01:00
|
|
|
$poll->disable[$watcherId] = $watcher;
|
2015-04-03 17:56:16 +02:00
|
|
|
// If the poll is disabled we don't need to do anything else
|
2014-11-11 22:07:15 +01:00
|
|
|
return $watcherId;
|
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->enabledWatcherCount++;
|
|
|
|
|
2014-11-26 22:27:30 +01:00
|
|
|
if ($type === Watcher::IO_READER) {
|
2014-11-11 22:07:15 +01:00
|
|
|
$poll->readers[$watcherId] = $watcher;
|
2014-06-11 18:33:15 +02:00
|
|
|
} else {
|
2014-11-11 22:07:15 +01:00
|
|
|
$poll->writers[$watcherId] = $watcher;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 19:17:08 +01:00
|
|
|
$newFlags = 0;
|
2014-11-11 22:07:15 +01:00
|
|
|
if ($poll->readers) {
|
2014-11-12 19:17:08 +01:00
|
|
|
$newFlags |= \UV::READABLE;
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
|
|
|
if ($poll->writers) {
|
2014-11-12 19:17:08 +01:00
|
|
|
$newFlags |= \UV::WRITABLE;
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
2014-11-12 19:17:08 +01:00
|
|
|
if ($newFlags != $poll->flags) {
|
|
|
|
$poll->flags = $newFlags;
|
|
|
|
uv_poll_start($poll->handle, $newFlags, $poll->callback);
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2014-11-11 22:07:15 +01:00
|
|
|
return $watcherId;
|
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2014-11-11 22:07:15 +01:00
|
|
|
private function makePollHandle($stream) {
|
|
|
|
// Windows needs the socket-specific init function, so make sure we use
|
|
|
|
// it when dealing with tcp/ssl streams.
|
|
|
|
$pollInitFunc = $this->isWindows
|
|
|
|
? $this->chooseWindowsPollingFunction($stream)
|
|
|
|
: 'uv_poll_init';
|
2014-06-11 18:33:15 +02:00
|
|
|
|
2014-11-11 22:07:15 +01:00
|
|
|
$streamId = (int) $stream;
|
2015-05-25 18:47:04 +02:00
|
|
|
|
|
|
|
$poll = new \StdClass;
|
|
|
|
$poll->readers = [];
|
|
|
|
$poll->writers = [];
|
|
|
|
$poll->disable = [];
|
|
|
|
$poll->flags = 0;
|
|
|
|
$poll->handle = \call_user_func($pollInitFunc, $this->loop, $stream);
|
|
|
|
$poll->callback = function($uvHandle, $stat, $events) use ($poll) {
|
|
|
|
if ($events & \UV::READABLE) {
|
|
|
|
foreach ($poll->readers as $watcher) {
|
|
|
|
$this->invokePollWatcher($watcher);
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
2015-05-25 18:47:04 +02:00
|
|
|
}
|
|
|
|
if ($events & \UV::WRITABLE) {
|
|
|
|
foreach ($poll->writers as $watcher) {
|
|
|
|
$this->invokePollWatcher($watcher);
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-25 18:47:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return $this->streamIdPollMap[$streamId] = $poll;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function chooseWindowsPollingFunction($stream) {
|
2014-11-11 17:08:46 +01:00
|
|
|
$streamType = stream_get_meta_data($stream)['stream_type'];
|
|
|
|
|
|
|
|
return ($streamType === 'tcp_socket/ssl' || $streamType === 'tcp_socket')
|
2014-08-06 05:45:33 +02:00
|
|
|
? 'uv_poll_init_socket'
|
|
|
|
: 'uv_poll_init';
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
private function invokePollWatcher($watcher) {
|
2014-11-11 22:07:15 +01:00
|
|
|
try {
|
2015-04-30 19:41:14 +02:00
|
|
|
$result = call_user_func($watcher->callback, $this, $watcher->id, $watcher->stream, $watcher->callbackData);
|
2014-11-11 22:07:15 +01:00
|
|
|
if ($result instanceof \Generator) {
|
2015-04-03 17:56:16 +02:00
|
|
|
resolve($result, $this)->when($this->onCoroutineResolution);
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
2014-11-11 22:07:15 +01:00
|
|
|
} catch (\Exception $e) {
|
2015-04-30 19:41:14 +02:00
|
|
|
$this->onCallbackError($e);
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 06:17:03 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-06 06:17:03 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function onSignal($signo, callable $func, array $options = []) {
|
|
|
|
$watcher = new \StdClass;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->id = $watcherId = $this->lastWatcherId++;
|
2014-11-26 22:27:30 +01:00
|
|
|
$watcher->type = Watcher::SIGNAL;
|
2015-03-19 16:14:21 +01:00
|
|
|
$watcher->callback = $this->wrapSignalCallback($watcher, $func);
|
2015-05-20 21:21:40 +02:00
|
|
|
$watcher->callbackData = @$options["cb_data"];
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->signo = $signo;
|
|
|
|
$watcher->uvHandle = uv_signal_init($this->loop);
|
2014-08-06 06:17:03 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
$this->enabledWatcherCount++;
|
|
|
|
uv_signal_start($watcher->uvHandle, $watcher->callback, $watcher->signo);
|
|
|
|
}
|
2014-08-06 06:17:03 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
2014-08-06 06:17:03 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
return $watcherId;
|
2014-08-06 06:17:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function wrapSignalCallback($watcher, $callback) {
|
2014-08-06 06:17:03 +02:00
|
|
|
return function() use ($watcher, $callback) {
|
|
|
|
try {
|
2015-04-30 19:41:14 +02:00
|
|
|
$result = call_user_func($callback, $this, $watcher->id, $watcher->signo, $watcher->callbackData);
|
2014-09-22 22:47:48 +02:00
|
|
|
if ($result instanceof \Generator) {
|
2015-04-03 17:56:16 +02:00
|
|
|
resolve($result, $this)->when($this->onCoroutineResolution);
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
2014-08-06 06:17:03 +02:00
|
|
|
} catch (\Exception $e) {
|
2015-04-30 19:41:14 +02:00
|
|
|
$this->onCallbackError($e);
|
2014-08-06 06:17:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:33:15 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function cancel($watcherId) {
|
2014-06-11 18:33:15 +02:00
|
|
|
if (isset($this->watchers[$watcherId])) {
|
|
|
|
$this->clearWatcher($watcherId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function clearWatcher($watcherId) {
|
2014-06-11 18:33:15 +02:00
|
|
|
$watcher = $this->watchers[$watcherId];
|
|
|
|
unset($this->watchers[$watcherId]);
|
|
|
|
if ($watcher->isEnabled) {
|
2014-12-02 00:08:49 +01:00
|
|
|
$this->enabledWatcherCount--;
|
2014-11-26 22:27:30 +01:00
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::IO_READER:
|
2014-11-11 22:07:15 +01:00
|
|
|
// fallthrough
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::IO_WRITER:
|
2014-11-11 22:07:15 +01:00
|
|
|
$this->clearPollFromWatcher($watcher);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::SIGNAL:
|
2015-04-03 17:56:16 +02:00
|
|
|
uv_signal_stop($watcher->uvHandle);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::IMMEDIATE:
|
2014-08-08 22:39:11 +02:00
|
|
|
unset($this->immediates[$watcherId]);
|
|
|
|
break;
|
2015-02-03 06:21:07 +01:00
|
|
|
case Watcher::TIMER_ONCE:
|
|
|
|
// we don't have to actually stop once timers
|
|
|
|
break;
|
2014-08-06 06:17:03 +02:00
|
|
|
default:
|
2015-04-03 17:56:16 +02:00
|
|
|
uv_timer_stop($watcher->uvHandle);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
2015-04-30 19:41:14 +02:00
|
|
|
|
|
|
|
if (PHP_MAJOR_VERSION < 7) {
|
|
|
|
$this->garbage[] = $watcher;
|
|
|
|
if (!$this->isGcScheduled) {
|
|
|
|
uv_timer_start($this->gcWatcher, 250, 0, $this->gcCallback);
|
|
|
|
$this->isGcScheduled = true;
|
|
|
|
}
|
|
|
|
}
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
private function clearPollFromWatcher($watcher) {
|
2014-11-11 22:07:15 +01:00
|
|
|
$poll = $watcher->poll;
|
|
|
|
$watcherId = $watcher->id;
|
|
|
|
|
|
|
|
unset(
|
|
|
|
$poll->readers[$watcherId],
|
|
|
|
$poll->writers[$watcherId],
|
|
|
|
$poll->disable[$watcherId]
|
|
|
|
);
|
|
|
|
|
|
|
|
// If any watchers are still enabled for this stream we're finished here
|
|
|
|
$hasEnabledWatchers = ((int) $poll->readers) + ((int) $poll->writers);
|
|
|
|
if ($hasEnabledWatchers) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always stop polling if no enabled watchers remain
|
|
|
|
uv_poll_stop($poll->handle);
|
|
|
|
|
|
|
|
// If all watchers are disabled we can pull out here
|
|
|
|
$hasDisabledWatchers = (bool) $poll->disable;
|
|
|
|
if ($hasDisabledWatchers) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise there are no watchers left for this poll and we should clear it
|
|
|
|
$streamId = (int) $watcher->stream;
|
|
|
|
unset($this->streamIdPollMap[$streamId]);
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:33:15 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function disable($watcherId) {
|
2014-06-11 18:33:15 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$watcher = $this->watchers[$watcherId];
|
2014-08-06 06:17:03 +02:00
|
|
|
if (!$watcher->isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-26 22:27:30 +01:00
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::IO_READER:
|
2014-11-11 22:07:15 +01:00
|
|
|
// fallthrough
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::IO_WRITER:
|
2014-11-11 22:07:15 +01:00
|
|
|
$this->disablePollFromWatcher($watcher);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::SIGNAL:
|
2015-04-03 17:56:16 +02:00
|
|
|
uv_signal_stop($watcher->uvHandle);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::IMMEDIATE:
|
2015-04-03 17:56:16 +02:00
|
|
|
unset($this->immediates[$watcherId]);
|
2014-08-08 22:39:11 +02:00
|
|
|
break;
|
2015-04-03 17:56:16 +02:00
|
|
|
case Watcher::TIMER_ONCE:
|
|
|
|
// fallthrough
|
|
|
|
case Watcher::TIMER_REPEAT:
|
|
|
|
uv_timer_stop($watcher->uvHandle);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
2015-04-03 17:56:16 +02:00
|
|
|
default:
|
2015-04-30 19:41:14 +02:00
|
|
|
throw new \RuntimeException("Unexpected Watcher type encountered");
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
2014-08-06 06:17:03 +02:00
|
|
|
|
|
|
|
$watcher->isEnabled = false;
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->enabledWatcherCount--;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
private function disablePollFromWatcher($watcher) {
|
2014-11-11 22:07:15 +01:00
|
|
|
$poll = $watcher->poll;
|
|
|
|
$watcherId = $watcher->id;
|
|
|
|
|
2014-11-12 19:17:08 +01:00
|
|
|
unset(
|
|
|
|
$poll->readers[$watcherId],
|
|
|
|
$poll->writers[$watcherId]
|
|
|
|
);
|
2014-11-11 22:07:15 +01:00
|
|
|
|
|
|
|
$poll->disable[$watcherId] = $watcher;
|
|
|
|
|
2014-11-12 19:17:08 +01:00
|
|
|
if (!($poll->readers || $poll->writers)) {
|
2014-11-11 22:07:15 +01:00
|
|
|
uv_poll_stop($poll->handle);
|
2014-11-12 19:17:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're still here we may need to update the polling flags
|
|
|
|
$newFlags = 0;
|
|
|
|
if ($poll->readers) {
|
|
|
|
$newFlags |= \UV::READABLE;
|
|
|
|
}
|
|
|
|
if ($poll->writers) {
|
|
|
|
$newFlags |= \UV::WRITABLE;
|
|
|
|
}
|
|
|
|
if ($poll->flags != $newFlags) {
|
|
|
|
$poll->flags = $newFlags;
|
|
|
|
uv_poll_start($poll->handle, $newFlags, $poll->callback);
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:33:15 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-06-11 18:33:15 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function enable($watcherId) {
|
2014-06-11 18:33:15 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$watcher = $this->watchers[$watcherId];
|
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-26 22:27:30 +01:00
|
|
|
switch ($watcher->type) {
|
2015-04-03 17:56:16 +02:00
|
|
|
case Watcher::TIMER_ONCE: // fallthrough
|
|
|
|
case Watcher::TIMER_REPEAT:
|
|
|
|
uv_timer_start($watcher->uvHandle, $watcher->msDelay, $watcher->msInterval, $watcher->callback);
|
|
|
|
break;
|
|
|
|
case Watcher::IO_READER: // fallthrough
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::IO_WRITER:
|
2014-11-11 22:07:15 +01:00
|
|
|
$this->enablePollFromWatcher($watcher);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::SIGNAL:
|
2015-04-03 17:56:16 +02:00
|
|
|
uv_signal_start($watcher->uvHandle, $watcher->callback, $watcher->signo);
|
2014-08-06 06:17:03 +02:00
|
|
|
break;
|
2014-11-26 22:27:30 +01:00
|
|
|
case Watcher::IMMEDIATE:
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->immediates[$watcherId] = $watcher;
|
2014-08-08 22:39:11 +02:00
|
|
|
break;
|
2014-08-06 06:17:03 +02:00
|
|
|
default:
|
2015-04-30 19:41:14 +02:00
|
|
|
throw new \RuntimeException("Unexpected Watcher type encountered");
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 06:17:03 +02:00
|
|
|
$watcher->isEnabled = true;
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->enabledWatcherCount++;
|
2014-06-11 18:33:15 +02:00
|
|
|
}
|
2014-08-19 20:20:04 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
private function enablePollFromWatcher($watcher) {
|
2014-11-11 22:07:15 +01:00
|
|
|
$poll = $watcher->poll;
|
|
|
|
$watcherId = $watcher->id;
|
|
|
|
|
|
|
|
unset($poll->disable[$watcherId]);
|
|
|
|
|
2014-11-26 22:27:30 +01:00
|
|
|
if ($watcher->type === Watcher::IO_READER) {
|
2014-11-11 22:07:15 +01:00
|
|
|
$poll->flags |= \UV::READABLE;
|
|
|
|
$poll->readers[$watcherId] = $watcher;
|
|
|
|
} else {
|
|
|
|
$poll->flags |= \UV::WRITABLE;
|
|
|
|
$poll->writers[$watcherId] = $watcher;
|
|
|
|
}
|
|
|
|
|
2015-01-22 02:15:08 +01:00
|
|
|
@uv_poll_start($poll->handle, $poll->flags, $poll->callback);
|
2014-11-11 22:07:15 +01:00
|
|
|
}
|
|
|
|
|
2014-08-19 20:20:04 +02:00
|
|
|
/**
|
|
|
|
* Access the underlying php-uv extension loop resource
|
|
|
|
*
|
|
|
|
* This method exists outside the base Reactor API. It provides access to the underlying php-uv
|
|
|
|
* event loop resource for code that wishes to interact with lower-level php-uv extension
|
|
|
|
* functionality.
|
|
|
|
*
|
|
|
|
* @return resource
|
|
|
|
*/
|
|
|
|
public function getUnderlyingLoop() {
|
|
|
|
return $this->loop;
|
|
|
|
}
|
2014-12-02 00:08:49 +01:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-12-02 00:08:49 +01:00
|
|
|
*/
|
2015-03-19 16:14:21 +01:00
|
|
|
public function onError(callable $func) {
|
|
|
|
$this->onError = $func;
|
2014-12-02 00:08:49 +01:00
|
|
|
}
|
2014-12-02 07:06:36 +01:00
|
|
|
|
2014-11-26 22:27:30 +01:00
|
|
|
public function __destruct() {
|
|
|
|
self::$instanceCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __debugInfo() {
|
|
|
|
$immediates = $timers = $readers = $writers = $signals = $disabled = 0;
|
|
|
|
foreach ($this->watchers as $watcher) {
|
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::IMMEDIATE:
|
|
|
|
$immediates++;
|
|
|
|
break;
|
|
|
|
case Watcher::TIMER_ONCE:
|
|
|
|
case Watcher::TIMER_REPEAT:
|
|
|
|
$timers++;
|
|
|
|
break;
|
|
|
|
case Watcher::IO_READER:
|
|
|
|
$readers++;
|
|
|
|
break;
|
|
|
|
case Watcher::IO_WRITER:
|
|
|
|
$writers++;
|
|
|
|
break;
|
|
|
|
case Watcher::SIGNAL:
|
|
|
|
$signals++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \DomainException(
|
|
|
|
"Unexpected watcher type: {$watcher->type}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$disabled += !$watcher->isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'timers' => $timers,
|
|
|
|
'immediates' => $immediates,
|
|
|
|
'io_readers' => $readers,
|
|
|
|
'io_writers' => $writers,
|
|
|
|
'signals' => $signals,
|
|
|
|
'disabled' => $disabled,
|
|
|
|
'last_watcher_id' => $this->lastWatcherId,
|
|
|
|
'instances' => self::$instanceCount,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|