1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00

Add libevent onSignal handler

This commit is contained in:
Daniel Lowrey 2014-04-22 20:45:20 -04:00
parent 7b351454f9
commit 62c3503abb

View File

@ -16,6 +16,7 @@ class LibeventReactor implements Reactor {
private static $TYPE_STREAM = 0;
private static $TYPE_ONCE = 1;
private static $TYPE_REPEATING = 2;
private static $TYPE_SIGNAL = 3;
public function __construct() {
$this->lastWatcherId = PHP_INT_MAX * -1;
@ -204,6 +205,40 @@ class LibeventReactor implements Reactor {
};
}
public function onSignal($signal, callable $callback) {
$watcherId = ++$this->lastWatcherId;
$eventResource = event_new();
$watcher = new LibeventWatcher;
$watcher->id = $watcherId;
$watcher->type = self::$TYPE_SIGNAL;
$watcher->eventResource = $eventResource;
$watcher->callback = $callback;
$watcher->wrapper = $this->wrapSignalCallback($watcher);
$this->watchers[$watcherId] = $watcher;
event_set($eventResource, $signal, EV_SIGNAL | EV_PERSIST, $watcher->wrapper);
event_base_set($eventResource, $this->base);
event_add($eventResource);
return $watcherId;
}
private function wrapSignalCallback(LibeventWatcher $watcher) {
$callback = $watcher->callback;
$watcherId = $watcher->id;
return function() use ($callback, $watcherId) {
try {
$callback($watcherId, $this);
} catch (\Exception $e) {
$this->stopException = $e;
$this->stop();
}
};
}
public function cancel($watcherId) {
if (!isset($this->watchers[$watcherId])) {
return;