2013-08-05 22:05:08 +02:00
|
|
|
<?php
|
|
|
|
|
2014-09-23 04:38:32 +02:00
|
|
|
namespace Amp;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-03-16 20:00:10 +01:00
|
|
|
class NativeReactor implements Reactor {
|
2015-07-30 05:23:53 +02:00
|
|
|
use Struct;
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
private $watchers = [];
|
2014-08-08 20:23:00 +02:00
|
|
|
private $immediates = [];
|
2015-04-03 17:56:16 +02:00
|
|
|
private $timerOrder = [];
|
2013-08-05 22:05:08 +02:00
|
|
|
private $readStreams = [];
|
|
|
|
private $writeStreams = [];
|
2015-04-03 17:56:16 +02:00
|
|
|
private $readWatchers = [];
|
|
|
|
private $writeWatchers = [];
|
2015-08-04 17:06:49 +02:00
|
|
|
private $timersEnabled = false;
|
|
|
|
private $isTimerSortNeeded = false;
|
|
|
|
private $state = self::STOPPED;
|
2015-04-03 17:56:16 +02:00
|
|
|
private $onCoroutineResolution;
|
2015-07-29 07:15:43 +02:00
|
|
|
private $hasExtPcntl;
|
|
|
|
private $signalState;
|
|
|
|
private $signalHandler;
|
2015-07-31 07:21:21 +02:00
|
|
|
private $keepAliveCount = 0;
|
2014-11-26 22:27:30 +01:00
|
|
|
|
2014-09-22 22:47:48 +02:00
|
|
|
public function __construct() {
|
2015-07-29 07:15:43 +02:00
|
|
|
$this->hasExtPcntl = \extension_loaded("pcntl");
|
|
|
|
$this->signalState = $signalState = new \StdClass;
|
|
|
|
$this->signalState->shouldDispatch = false;
|
|
|
|
$this->signalState->handlers = [];
|
|
|
|
$this->signalHandler = static function($signo) use ($signalState) {
|
|
|
|
if (empty($signalState->handlers[$signo])) {
|
2014-12-02 00:08:49 +01:00
|
|
|
return;
|
2015-07-29 07:15:43 +02:00
|
|
|
}
|
|
|
|
foreach ($signalState->handlers[$signo] as $watcherId => $watcher) {
|
2015-07-31 07:21:21 +02:00
|
|
|
$out = \call_user_func($watcher->callback, $watcherId, $signo, $watcher->cbData);
|
2015-07-29 07:15:43 +02:00
|
|
|
if ($out instanceof \Generator) {
|
|
|
|
resolve($out)->when($this->onCoroutineResolution);
|
|
|
|
}
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
};
|
2015-07-29 07:15:43 +02:00
|
|
|
$this->onCoroutineResolution = static function ($error = null, $result = null) {
|
|
|
|
if ($error) {
|
|
|
|
throw $error;
|
2015-04-03 17:56:16 +02:00
|
|
|
}
|
2015-07-29 07:15:43 +02:00
|
|
|
};
|
2014-11-26 22:27:30 +01:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2014-08-07 07:35:05 +02:00
|
|
|
public function run(callable $onStart = null) {
|
2015-08-04 17:06:49 +02:00
|
|
|
if ($this->state !== self::STOPPED) {
|
|
|
|
throw new \LogicException(
|
|
|
|
"Cannot run() recursively; event reactor already active"
|
|
|
|
);
|
2014-01-20 14:23:28 +01:00
|
|
|
}
|
2015-08-04 17:06:49 +02:00
|
|
|
|
2014-01-20 14:23:28 +01:00
|
|
|
if ($onStart) {
|
2015-08-04 17:06:49 +02:00
|
|
|
$this->state = self::STARTING;
|
|
|
|
$watcherId = $this->immediately($onStart);
|
|
|
|
if (!$this->tryImmediate($this->watchers[$watcherId]) || empty($this->keepAliveCount)) {
|
2015-07-31 07:21:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-08-04 17:06:49 +02:00
|
|
|
} else {
|
|
|
|
$this->state = self::RUNNING;
|
2014-01-20 14:23:28 +01:00
|
|
|
}
|
2015-08-04 17:06:49 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->enableTimers();
|
2015-08-04 17:06:49 +02:00
|
|
|
|
|
|
|
while ($this->state > self::STOPPED) {
|
|
|
|
$this->doTick($noWait = false);
|
2015-07-31 07:21:21 +02:00
|
|
|
if (empty($this->keepAliveCount)) {
|
|
|
|
break;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
2015-08-04 17:06:49 +02:00
|
|
|
|
2015-08-10 19:36:05 +02:00
|
|
|
\gc_collect_cycles();
|
|
|
|
|
2015-08-05 19:13:16 +02:00
|
|
|
$this->timersEnabled = false;
|
|
|
|
$this->state = self::STOPPED;
|
2015-08-04 17:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function unload() {
|
|
|
|
if ($this->watchers) {
|
2015-08-05 05:58:49 +02:00
|
|
|
foreach (\array_keys($this->watchers) as $watcherId) {
|
2015-08-04 17:06:49 +02:00
|
|
|
$this->cancel($watcherId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->timersEnabled = false;
|
|
|
|
$this->state = self::STOPPED;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 07:21:21 +02:00
|
|
|
private function tryImmediate($watcher) {
|
|
|
|
try {
|
2015-08-05 05:11:52 +02:00
|
|
|
unset(
|
|
|
|
$this->watchers[$watcher->id],
|
|
|
|
$this->immediates[$watcher->id]
|
|
|
|
);
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount -= $watcher->keepAlive;
|
|
|
|
$out = \call_user_func($watcher->callback, $watcher->id, $watcher->cbData);
|
|
|
|
if ($out instanceof \Generator) {
|
|
|
|
resolve($out)->when($this->onCoroutineResolution);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// @TODO Remove coverage ignore block once PHP5 support is no longer required
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
\call_user_func($this->onCoroutineResolution, $e);
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// @TODO Remove this catch block once PHP5 support is no longer required
|
|
|
|
\call_user_func($this->onCoroutineResolution, $e);
|
|
|
|
}
|
|
|
|
|
2015-08-04 17:06:49 +02:00
|
|
|
return $this->state;
|
2015-07-31 07:21:21 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
private function enableTimers() {
|
2014-02-23 22:26:28 +01:00
|
|
|
$now = microtime(true);
|
2015-04-03 17:56:16 +02:00
|
|
|
foreach ($this->watchers as $watcherId => $watcher) {
|
|
|
|
if (!($watcher->type & Watcher::TIMER) || isset($watcher->nextExecutionAt) || !$watcher->isEnabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$watcher->nextExecutionAt = $now + $watcher->msDelay;
|
|
|
|
$this->timerOrder[$watcherId] = $watcher->nextExecutionAt;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
2015-08-04 17:06:49 +02:00
|
|
|
$this->timersEnabled = true;
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->isTimerSortNeeded = true;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2014-02-23 22:26:28 +01:00
|
|
|
public function stop() {
|
2015-08-04 17:06:49 +02:00
|
|
|
if ($this->state !== self::STOPPED) {
|
|
|
|
$this->state = self::STOPPED;
|
|
|
|
$this->timersEnabled = false;
|
|
|
|
} else {
|
|
|
|
throw new \LogicException(
|
|
|
|
"Cannot stop(); event reactor not currently active"
|
|
|
|
);
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function tick($noWait = false) {
|
2015-08-04 17:06:49 +02:00
|
|
|
if ($this->state) {
|
|
|
|
throw new \LogicException(
|
|
|
|
"Cannot tick() recursively; event reactor already active"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->state = self::TICKING;
|
|
|
|
$this->doTick((bool) $noWait);
|
|
|
|
$this->state = self::STOPPED;
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
throw $e;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doTick($noWait) {
|
|
|
|
if (empty($this->timersEnabled)) {
|
2015-07-29 07:15:43 +02:00
|
|
|
$this->enableTimers();
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-07-31 07:21:21 +02:00
|
|
|
$immediates = $this->immediates;
|
|
|
|
foreach ($immediates as $watcher) {
|
|
|
|
if (!$this->tryImmediate($watcher)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-07-29 07:15:43 +02:00
|
|
|
}
|
|
|
|
if ($this->signalState->shouldDispatch) {
|
|
|
|
\pcntl_signal_dispatch();
|
|
|
|
}
|
2014-08-08 20:23:00 +02:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
// If an immediately watcher called stop() we pull out here
|
2015-08-04 17:06:49 +02:00
|
|
|
if ($this->state <= self::STOPPING) {
|
2015-07-29 07:15:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-12-02 07:09:51 +01:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
if ($this->immediates || $noWait) {
|
|
|
|
$timeToNextAlarm = 0;
|
|
|
|
} elseif (empty($this->timerOrder)) {
|
|
|
|
$timeToNextAlarm = 1;
|
|
|
|
} else {
|
|
|
|
if ($this->isTimerSortNeeded) {
|
|
|
|
\asort($this->timerOrder);
|
|
|
|
$this->isTimerSortNeeded = false;
|
2014-12-02 00:08:49 +01:00
|
|
|
}
|
2015-07-31 07:21:21 +02:00
|
|
|
|
|
|
|
// Don't block the run() loop if no keep-alive timers exist
|
|
|
|
// @TODO Instead of iterating all timers hunting for a keep-alive
|
|
|
|
// we should just use a specific counter to cache the number
|
|
|
|
// of keep-alive timers in use at any given time
|
|
|
|
$timeToNextAlarm = 0;
|
|
|
|
foreach ($this->timerOrder as $watcherId => $time) {
|
|
|
|
if ($this->watchers[$watcherId]->keepAlive) {
|
|
|
|
// The reset() is important because the foreach modifies
|
|
|
|
// the internal array pointer.
|
|
|
|
$nextTimerAt = \reset($this->timerOrder);
|
|
|
|
$timeToNextAlarm = \round($nextTimerAt - \microtime(true), 4);
|
|
|
|
$timeToNextAlarm = ($timeToNextAlarm > 0) ? $timeToNextAlarm : 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 07:15:43 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 07:21:21 +02:00
|
|
|
if (empty($this->keepAliveCount)) {
|
|
|
|
return;
|
|
|
|
} elseif ($this->readStreams || $this->writeStreams) {
|
2015-07-29 07:15:43 +02:00
|
|
|
$this->selectActionableStreams($timeToNextAlarm);
|
|
|
|
} elseif (!($this->timerOrder || $this->immediates || $this->signalState->handlers)) {
|
|
|
|
$this->stop();
|
|
|
|
} elseif ($timeToNextAlarm > 0) {
|
|
|
|
\usleep($timeToNextAlarm * 1000000);
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
if ($this->timerOrder || $this->immediates) {
|
|
|
|
$this->executeTimers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 01:18:50 +01:00
|
|
|
private function selectActionableStreams($timeout) {
|
2013-11-15 01:13:24 +01:00
|
|
|
$r = $this->readStreams;
|
|
|
|
$w = $this->writeStreams;
|
2014-08-07 07:35:05 +02:00
|
|
|
$e = null;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2013-11-15 01:18:50 +01:00
|
|
|
if ($timeout <= 0) {
|
|
|
|
$sec = 0;
|
|
|
|
$usec = 0;
|
|
|
|
} else {
|
|
|
|
$sec = floor($timeout);
|
2015-04-03 17:56:16 +02:00
|
|
|
$usec = ($timeout - $sec) * 1000000;
|
2013-11-15 01:18:50 +01:00
|
|
|
}
|
2015-07-29 07:15:43 +02:00
|
|
|
if (!@stream_select($r, $w, $e, $sec, $usec)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($r as $stream) {
|
|
|
|
$streamId = (int) $stream;
|
|
|
|
foreach ($this->readWatchers[$streamId] as $watcherId => $watcher) {
|
|
|
|
$this->doIoCallback($watcherId, $watcher, $stream);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
2015-07-29 07:15:43 +02:00
|
|
|
}
|
|
|
|
foreach ($w as $stream) {
|
|
|
|
$streamId = (int) $stream;
|
|
|
|
foreach ($this->writeWatchers[$streamId] as $watcherId => $watcher) {
|
|
|
|
$this->doIoCallback($watcherId, $watcher, $stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doIoCallback($watcherId, $watcher, $stream) {
|
|
|
|
try {
|
2015-07-31 07:21:21 +02:00
|
|
|
$result = \call_user_func($watcher->callback, $watcherId, $stream, $watcher->cbData);
|
2015-07-29 07:15:43 +02:00
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
resolve($result)->when($this->onCoroutineResolution);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
2015-07-29 07:15:43 +02:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
\call_user_func($this->onCoroutineResolution, $e);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\call_user_func($this->onCoroutineResolution, $e);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
private function executeTimers() {
|
2014-02-23 22:26:28 +01:00
|
|
|
$now = microtime(true);
|
2015-04-03 17:56:16 +02:00
|
|
|
if ($this->isTimerSortNeeded) {
|
2015-07-29 07:15:43 +02:00
|
|
|
\asort($this->timerOrder);
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->isTimerSortNeeded = false;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
foreach ($this->timerOrder as $watcherId => $executionCutoff) {
|
2014-09-22 22:47:48 +02:00
|
|
|
if ($executionCutoff > $now) {
|
2013-08-05 22:05:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-06-10 21:31:30 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
|
|
|
unset($this->timerOrder[$watcherId]);
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher = $this->watchers[$watcherId];
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-07-31 07:21:21 +02:00
|
|
|
$result = \call_user_func($watcher->callback, $watcherId, $watcher->cbData);
|
2015-04-03 17:56:16 +02:00
|
|
|
if ($result instanceof \Generator) {
|
2015-07-29 07:15:43 +02:00
|
|
|
resolve($result)->when($this->onCoroutineResolution);
|
2015-04-03 17:56:16 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 08:18:25 +02:00
|
|
|
if ($watcher->type === Watcher::TIMER_ONCE) {
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount -= $watcher->keepAlive;
|
2014-09-22 22:47:48 +02:00
|
|
|
unset(
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->watchers[$watcherId],
|
|
|
|
$this->timerOrder[$watcherId]
|
2014-09-22 22:47:48 +02:00
|
|
|
);
|
2015-05-26 08:18:25 +02:00
|
|
|
continue;
|
|
|
|
} elseif ($watcher->isEnabled) {
|
|
|
|
$this->isTimerSortNeeded = true;
|
|
|
|
$watcher->nextExecutionAt = $now + $watcher->msInterval;
|
|
|
|
$this->timerOrder[$watcherId] = $watcher->nextExecutionAt;
|
|
|
|
} else {
|
|
|
|
unset($this->timerOrder[$watcherId]);
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function immediately(callable $callback, array $options = []) {
|
2015-07-30 05:23:53 +02:00
|
|
|
$watcher = new \StdClass;
|
2015-07-30 04:10:24 +02:00
|
|
|
$watcher->id = $watcherId = \spl_object_hash($watcher);
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->type = Watcher::IMMEDIATE;
|
|
|
|
$watcher->callback = $callback;
|
2015-08-18 14:46:34 +02:00
|
|
|
$watcher->cbData = isset($options["cb_data"]) ? $options["cb_data"] : null;
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->keepAlive = isset($options["keep_alive"]) ? (bool) $options["keep_alive"] : true;
|
2015-04-03 17:56:16 +02:00
|
|
|
|
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
$this->watchers[$watcherId] = $this->immediates[$watcherId] = $watcher;
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount += $watcher->keepAlive;
|
2015-04-03 17:56:16 +02:00
|
|
|
}
|
2014-08-08 20:23:00 +02:00
|
|
|
|
|
|
|
return $watcherId;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function once(callable $callback, $msDelay, array $options = []) {
|
|
|
|
$msDelay = (int) $msDelay;
|
2015-04-03 17:56:16 +02:00
|
|
|
assert(($msDelay >= 0), "\$msDelay at Argument 2 expects integer >= 0");
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
/* In the php7 branch we use an anonymous class with Struct for this.
|
|
|
|
* Using a stdclass isn't terribly readable and it's prone to error but
|
|
|
|
* it's the easiest way to minimize the distance between 5.x and 7 code
|
|
|
|
* and keep maintenance simple.
|
|
|
|
*/
|
|
|
|
$watcher = new \StdClass;
|
2015-07-30 04:10:24 +02:00
|
|
|
$watcher->id = $watcherId = \spl_object_hash($watcher);
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->type = Watcher::TIMER_ONCE;
|
|
|
|
$watcher->callback = $callback;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->cbData = @$options["cb_data"];
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->keepAlive = isset($options["keep_alive"]) ? (bool) $options["keep_alive"] : true;
|
2015-05-10 08:36:39 +02:00
|
|
|
$watcher->msDelay = round(($msDelay / 1000), 3);
|
|
|
|
$watcher->nextExecutionAt = null;
|
2015-04-03 17:56:16 +02:00
|
|
|
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount += ($watcher->keepAlive && $watcher->isEnabled);
|
|
|
|
|
2015-08-04 17:06:49 +02:00
|
|
|
if ($watcher->isEnabled && $this->state > self::STOPPED) {
|
2015-05-10 08:36:39 +02:00
|
|
|
$nextExecutionAt = microtime(true) + $watcher->msDelay;
|
|
|
|
$watcher->nextExecutionAt = $nextExecutionAt;
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->timerOrder[$watcherId] = $nextExecutionAt;
|
|
|
|
$this->isTimerSortNeeded = true;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function repeat(callable $callback, $msInterval, array $options = []) {
|
|
|
|
$msInterval = (int) $msInterval;
|
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"] : (int) $msInterval;
|
|
|
|
assert(($msDelay >= 0), "ms_delay option expects integer >= 0");
|
2015-07-23 07:32:52 +02:00
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
/* In the php7 branch we use an anonymous class with Struct for this.
|
|
|
|
* Using a stdclass isn't terribly readable and it's prone to error but
|
|
|
|
* it's the easiest way to minimize the distance between 5.x and 7 code
|
|
|
|
* and keep maintenance simple.
|
|
|
|
*/
|
|
|
|
$watcher = new \StdClass;
|
2015-07-30 04:10:24 +02:00
|
|
|
$watcher->id = $watcherId = \spl_object_hash($watcher);
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->type = Watcher::TIMER_REPEAT;
|
|
|
|
$watcher->callback = $callback;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->cbData = @$options["cb_data"];
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->keepAlive = isset($options["keep_alive"]) ? (bool) $options["keep_alive"] : true;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->msInterval = round(($msInterval / 1000), 3);
|
|
|
|
$watcher->msDelay = round(($msDelay / 1000), 3);
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->nextExecutionAt = null; // only needed for php5.x
|
2015-04-03 17:56:16 +02:00
|
|
|
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount += ($watcher->keepAlive && $watcher->isEnabled);
|
|
|
|
|
2015-08-04 17:06:49 +02:00
|
|
|
if ($watcher->isEnabled && $this->state > self::STOPPED) {
|
2015-05-10 08:36:39 +02:00
|
|
|
$increment = (isset($watcher->msDelay) ? $watcher->msDelay : $watcher->msInterval);
|
|
|
|
$nextExecutionAt = microtime(true) + $increment;
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->timerOrder[$watcherId] = $watcher->nextExecutionAt = $nextExecutionAt;
|
|
|
|
$this->isTimerSortNeeded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->watchers[$watcherId] = $watcher;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +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->registerIoWatcher($stream, $callback, $options, Watcher::IO_READER);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +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->registerIoWatcher($stream, $callback, $options, Watcher::IO_WRITER);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:41:14 +02:00
|
|
|
private function registerIoWatcher($stream, $callback, $options, $type) {
|
|
|
|
/* In the php7 branch we use an anonymous class with Struct for this.
|
|
|
|
* Using a stdclass isn't terribly readable and it's prone to error but
|
|
|
|
* it's the easiest way to minimize the distance between 5.x and 7 code
|
|
|
|
* and keep maintenance simple.
|
|
|
|
*/
|
|
|
|
$watcher = new \StdClass;
|
2015-07-30 04:10:24 +02:00
|
|
|
$watcher->id = $watcherId = \spl_object_hash($watcher);
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->type = $type;
|
|
|
|
$watcher->callback = $callback;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->cbData = @$options["cb_data"];
|
2015-04-30 19:41:14 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->keepAlive = isset($options["keep_alive"]) ? (bool) $options["keep_alive"] : true;
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->stream = $stream;
|
|
|
|
$watcher->streamId = $streamId = (int) $stream;
|
|
|
|
|
|
|
|
if ($watcher->isEnabled) {
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount += $watcher->keepAlive;
|
2015-04-03 17:56:16 +02:00
|
|
|
if ($type === Watcher::IO_READER) {
|
|
|
|
$this->readStreams[$streamId] = $stream;
|
|
|
|
$this->readWatchers[$streamId][$watcherId] = $watcher;
|
|
|
|
} else {
|
|
|
|
$this->writeStreams[$streamId] = $stream;
|
|
|
|
$this->writeWatchers[$streamId][$watcherId] = $watcher;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
return $watcherId;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-07-29 07:15:43 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @throws \RuntimeException if ext/pcntl unavailable or signal handler registration fails
|
|
|
|
*/
|
|
|
|
public function onSignal($signo, callable $func, array $options = []) {
|
|
|
|
if (empty($this->hasExtPcntl)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Cannot react to signals; ext/pcntl not loaded"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$signo = (int) $signo;
|
|
|
|
|
|
|
|
$watcher = new \StdClass;
|
2015-07-30 04:10:24 +02:00
|
|
|
$watcher->id = $watcherId = \spl_object_hash($watcher);
|
2015-07-29 07:15:43 +02:00
|
|
|
$watcher->type = Watcher::SIGNAL;
|
|
|
|
$watcher->callback = $func;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->cbData = isset($options["cb_data"]) ? $options["cb_data"] : null;
|
2015-07-29 07:15:43 +02:00
|
|
|
$watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
|
2015-07-31 07:21:21 +02:00
|
|
|
$watcher->keepAlive = isset($options["keep_alive"]) ? (bool) $options["keep_alive"] : true;
|
2015-07-29 07:15:43 +02:00
|
|
|
$watcher->signo = $signo;
|
|
|
|
|
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
if (empty($this->signalState->handlers[$signo]) && !@\pcntl_signal($signo, $this->signalHandler)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed registering signal handler"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$this->signalState->shouldDispatch = true;
|
|
|
|
$this->signalState->handlers[$signo][$watcherId] = $watcher;
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount += $watcher->keepAlive;
|
2015-07-29 07:15:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->watchers[$watcherId] = $watcher;
|
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function onError(callable $func) {
|
|
|
|
$this->onCoroutineResolution = static function($e = null, $r = null) use ($func) {
|
|
|
|
if ($e) {
|
|
|
|
\call_user_func($func, $e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function cancel($watcherId) {
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->disable($watcherId);
|
2015-05-26 08:18:25 +02:00
|
|
|
unset(
|
|
|
|
$this->watchers[$watcherId],
|
|
|
|
$this->timerOrder[$watcherId]
|
|
|
|
);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function enable($watcherId) {
|
2015-04-03 17:56:16 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
2013-08-05 22:05:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher = $this->watchers[$watcherId];
|
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
// If the watcher is already enabled we're finished here
|
|
|
|
return;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->isEnabled = true;
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount += $watcher->keepAlive;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::TIMER_ONCE:
|
|
|
|
case Watcher::TIMER_REPEAT:
|
|
|
|
if (!isset($watcher->nextExecutionAt)) {
|
|
|
|
$watcher->nextExecutionAt = microtime(true) + $watcher->msDelay;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
2015-04-03 17:56:16 +02:00
|
|
|
$this->isTimerSortNeeded = true;
|
|
|
|
$this->timerOrder[$watcherId] = $watcher->nextExecutionAt;
|
2013-08-05 22:05:08 +02:00
|
|
|
break;
|
2015-04-03 17:56:16 +02:00
|
|
|
case Watcher::IO_READER:
|
|
|
|
$streamId = (int) $watcher->stream;
|
|
|
|
$this->readStreams[$streamId] = $watcher->stream;
|
|
|
|
$this->readWatchers[$streamId][$watcherId] = $watcher;
|
2013-08-05 22:05:08 +02:00
|
|
|
break;
|
2015-04-03 17:56:16 +02:00
|
|
|
case Watcher::IO_WRITER:
|
|
|
|
$streamId = (int) $watcher->stream;
|
|
|
|
$this->writeStreams[$streamId] = $watcher->stream;
|
|
|
|
$this->writeWatchers[$streamId][$watcherId] = $watcher;
|
2013-08-05 22:05:08 +02:00
|
|
|
break;
|
2015-04-03 17:56:16 +02:00
|
|
|
case Watcher::IMMEDIATE:
|
|
|
|
$this->immediates[$watcherId] = $watcher;
|
2014-08-08 20:23:00 +02:00
|
|
|
break;
|
2015-07-29 07:15:43 +02:00
|
|
|
case Watcher::SIGNAL:
|
|
|
|
$signo = $watcher->signo;
|
|
|
|
if (empty($this->signalState->handlers[$signo])) {
|
|
|
|
\pcntl_signal($signo, $this->signalHandler);
|
|
|
|
}
|
|
|
|
$this->signalState->handlers[$signo][$watcherId] = $watcher;
|
|
|
|
$this->signalState->shouldDispatch = true;
|
|
|
|
break;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:26:08 +02:00
|
|
|
/**
|
2015-03-19 16:14:21 +01:00
|
|
|
* {@inheritDoc}
|
2014-08-08 18:26:08 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function disable($watcherId) {
|
2015-04-03 17:56:16 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher = $this->watchers[$watcherId];
|
|
|
|
if (!$watcher->isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
$watcher->isEnabled = false;
|
2015-07-31 07:21:21 +02:00
|
|
|
$this->keepAliveCount -= $watcher->keepAlive;
|
2014-08-08 20:23:00 +02:00
|
|
|
|
2015-04-03 17:56:16 +02:00
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::TIMER_ONCE:
|
|
|
|
case Watcher::TIMER_REPEAT:
|
|
|
|
unset($this->timerOrder[$watcherId]);
|
|
|
|
break;
|
|
|
|
case Watcher::IO_READER:
|
|
|
|
$streamId = $watcher->streamId;
|
|
|
|
unset($this->readWatchers[$streamId][$watcherId]);
|
|
|
|
if (empty($this->readWatchers[$streamId])) {
|
|
|
|
unset($this->readStreams[$streamId]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Watcher::IO_WRITER:
|
|
|
|
$streamId = $watcher->streamId;
|
|
|
|
unset($this->writeWatchers[$streamId][$watcherId]);
|
|
|
|
if (empty($this->writeWatchers[$streamId])) {
|
|
|
|
unset($this->writeStreams[$streamId]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Watcher::IMMEDIATE:
|
|
|
|
unset($this->immediates[$watcherId]);
|
|
|
|
break;
|
2015-07-29 07:15:43 +02:00
|
|
|
case Watcher::SIGNAL:
|
|
|
|
$signo = $watcher->signo;
|
|
|
|
unset($this->signalState->handlers[$signo][$watcherId]);
|
|
|
|
if (empty($this->signalState->handlers[$signo])) {
|
|
|
|
unset($this->signalState->handlers[$signo]);
|
|
|
|
\pcntl_signal($signo, \SIG_DFL);
|
|
|
|
if (empty($this->signalState->handlers)) {
|
|
|
|
$this->signalState->shouldDispatch = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
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-07-29 07:15:43 +02:00
|
|
|
public function info() {
|
|
|
|
$once = $repeat = $immediately = $onReadable = $onWritable = $onSignal = [
|
|
|
|
"enabled" => 0,
|
|
|
|
"disabled" => 0,
|
|
|
|
];
|
|
|
|
foreach ($this->watchers as $watcher) {
|
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::IMMEDIATE: $arr =& $immediately; break;
|
|
|
|
case Watcher::TIMER_ONCE: $arr =& $once; break;
|
|
|
|
case Watcher::TIMER_REPEAT: $arr =& $repeat; break;
|
|
|
|
case Watcher::IO_READER: $arr =& $onReadable; break;
|
|
|
|
case Watcher::IO_WRITER: $arr =& $onWritable; break;
|
|
|
|
case Watcher::SIGNAL: $arr =& $onSignal; break;
|
|
|
|
}
|
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
$arr["enabled"] += 1;
|
|
|
|
} else {
|
|
|
|
$arr["disabled"] += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
"immediately" => $immediately,
|
|
|
|
"once" => $once,
|
|
|
|
"repeat" => $repeat,
|
|
|
|
"on_readable" => $onReadable,
|
|
|
|
"on_writable" => $onWritable,
|
|
|
|
"on_signal" => $onSignal,
|
2015-07-31 07:21:21 +02:00
|
|
|
"keep_alive" => $this->keepAliveCount,
|
2015-08-05 05:47:20 +02:00
|
|
|
"state" => $this->state,
|
2015-07-29 07:15:43 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __debugInfo() {
|
|
|
|
return $this->info();
|
2014-12-02 00:08:49 +01:00
|
|
|
}
|
2015-07-29 07:15:43 +02:00
|
|
|
}
|