2013-08-05 22:05:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Alert;
|
|
|
|
|
2014-01-22 16:10:04 +01:00
|
|
|
class LibeventReactor implements Reactor {
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
private $base;
|
|
|
|
private $watchers = [];
|
2013-11-29 18:35:13 +01:00
|
|
|
private $lastWatcherId;
|
2013-08-05 22:05:08 +02:00
|
|
|
private $resolution = 1000000;
|
2014-02-23 22:26:28 +01:00
|
|
|
private $isRunning = false;
|
|
|
|
private $isGCScheduled = false;
|
2013-08-05 22:05:08 +02:00
|
|
|
private $gcEvent;
|
|
|
|
private $stopException;
|
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
private static $TYPE_STREAM = 0;
|
|
|
|
private static $TYPE_ONCE = 1;
|
|
|
|
private static $TYPE_REPEATING = 2;
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function __construct() {
|
2013-11-29 18:35:13 +01:00
|
|
|
$this->lastWatcherId = PHP_INT_MAX * -1;
|
2013-09-11 19:00:43 +02:00
|
|
|
$this->base = event_base_new();
|
2013-08-05 22:05:08 +02:00
|
|
|
$this->gcEvent = event_new();
|
|
|
|
event_timer_set($this->gcEvent, [$this, 'collectGarbage']);
|
|
|
|
event_base_set($this->gcEvent, $this->base);
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function run(callable $onStart = NULL) {
|
2014-01-20 14:23:28 +01:00
|
|
|
if ($this->isRunning) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($onStart) {
|
|
|
|
$this->immediately($onStart);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
2014-01-20 14:23:28 +01:00
|
|
|
|
|
|
|
$this->doRun();
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function tick() {
|
2013-08-05 22:05:08 +02:00
|
|
|
if (!$this->isRunning) {
|
|
|
|
$this->doRun(EVLOOP_ONCE | EVLOOP_NONBLOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doRun($flags = 0) {
|
2014-02-23 22:26:28 +01:00
|
|
|
$this->isRunning = true;
|
2013-08-05 22:05:08 +02:00
|
|
|
event_base_loop($this->base, $flags);
|
2014-02-23 22:26:28 +01:00
|
|
|
$this->isRunning = false;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
if ($this->stopException) {
|
|
|
|
$e = $this->stopException;
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function stop() {
|
2013-08-05 22:05:08 +02:00
|
|
|
event_base_loopexit($this->base);
|
|
|
|
}
|
2013-08-29 08:38:13 +02:00
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function at(callable $callback, $timeString) {
|
2013-08-05 22:05:08 +02:00
|
|
|
$now = time();
|
|
|
|
$executeAt = @strtotime($timeString);
|
2013-08-29 08:38:13 +02:00
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
if ($executeAt === false && $executeAt <= $now) {
|
2013-08-05 22:05:08 +02:00
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'Valid future time string (parsable by strtotime()) required'
|
|
|
|
);
|
|
|
|
}
|
2013-08-29 08:38:13 +02:00
|
|
|
|
2013-08-05 22:05:08 +02:00
|
|
|
$delay = $executeAt - $now;
|
2013-08-29 08:38:13 +02:00
|
|
|
|
2013-08-05 22:05:08 +02:00
|
|
|
return $this->once($callback, $delay);
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function immediately(callable $callback) {
|
2013-08-05 22:05:08 +02:00
|
|
|
return $this->once($callback, $delay = 0);
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function once(callable $callback, $delay) {
|
2013-11-29 18:35:13 +01:00
|
|
|
$watcherId = ++$this->lastWatcherId;
|
2013-09-11 19:00:43 +02:00
|
|
|
$eventResource = event_new();
|
2013-08-05 22:05:08 +02:00
|
|
|
$delay = ($delay > 0) ? ($delay * $this->resolution) : 0;
|
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
$watcher = new LibeventWatcher;
|
|
|
|
$watcher->id = $watcherId;
|
|
|
|
$watcher->type = self::$TYPE_ONCE;
|
|
|
|
$watcher->eventResource = $eventResource;
|
|
|
|
$watcher->interval = $delay;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
|
|
|
|
$watcher->wrapper = $this->wrapOnceCallback($watcher);
|
|
|
|
|
|
|
|
$this->watchers[$watcherId] = $watcher;
|
|
|
|
|
|
|
|
event_timer_set($eventResource, $watcher->wrapper);
|
|
|
|
event_base_set($eventResource, $this->base);
|
|
|
|
event_add($eventResource, $delay);
|
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function wrapOnceCallback(LibeventWatcher $watcher) {
|
|
|
|
$callback = $watcher->callback;
|
|
|
|
$watcherId = $watcher->id;
|
|
|
|
|
|
|
|
return function() use ($callback, $watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
try {
|
2013-09-08 15:46:25 +02:00
|
|
|
$callback($watcherId, $this);
|
2013-08-05 22:05:08 +02:00
|
|
|
$this->cancel($watcherId);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->stopException = $e;
|
|
|
|
$this->stop();
|
|
|
|
}
|
|
|
|
};
|
2013-09-11 19:00:43 +02:00
|
|
|
}
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function repeat(callable $callback, $interval) {
|
2013-11-29 18:35:13 +01:00
|
|
|
$watcherId = ++$this->lastWatcherId;
|
2013-09-11 19:00:43 +02:00
|
|
|
$interval = ($interval > 0) ? ($interval * $this->resolution) : 0;
|
|
|
|
$eventResource = event_new();
|
|
|
|
|
|
|
|
$watcher = new LibeventWatcher;
|
|
|
|
$watcher->id = $watcherId;
|
|
|
|
$watcher->type = self::$TYPE_REPEATING;
|
|
|
|
$watcher->eventResource = $eventResource;
|
|
|
|
$watcher->interval = $interval;
|
|
|
|
$watcher->callback = $callback;
|
2014-02-23 22:26:28 +01:00
|
|
|
$watcher->isRepeating = true;
|
2013-11-27 17:56:29 +01:00
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
$watcher->wrapper = $this->wrapRepeatingCallback($watcher);
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
|
|
|
|
|
|
|
event_timer_set($eventResource, $watcher->wrapper);
|
|
|
|
event_base_set($eventResource, $this->base);
|
|
|
|
event_add($eventResource, $interval);
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
private function wrapRepeatingCallback(LibeventWatcher $watcher) {
|
|
|
|
$callback = $watcher->callback;
|
|
|
|
$watcherId = $watcher->id;
|
|
|
|
$eventResource = $watcher->eventResource;
|
|
|
|
$interval = $watcher->interval;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
return function() use ($callback, $eventResource, $interval, $watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
try {
|
2013-09-08 15:46:25 +02:00
|
|
|
$callback($watcherId, $this);
|
2013-09-11 19:00:43 +02:00
|
|
|
event_add($eventResource, $interval);
|
2013-08-05 22:05:08 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->stopException = $e;
|
|
|
|
$this->stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function onReadable($stream, callable $callback, $enableNow = true) {
|
2013-08-05 22:05:08 +02:00
|
|
|
return $this->watchIoStream($stream, EV_READ | EV_PERSIST, $callback, $enableNow);
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function onWritable($stream, callable $callback, $enableNow = true) {
|
2013-08-05 22:05:08 +02:00
|
|
|
return $this->watchIoStream($stream, EV_WRITE | EV_PERSIST, $callback, $enableNow);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function watchIoStream($stream, $flags, callable $callback, $enableNow) {
|
2013-11-29 18:35:13 +01:00
|
|
|
$watcherId = ++$this->lastWatcherId;
|
2013-09-11 19:00:43 +02:00
|
|
|
$eventResource = event_new();
|
|
|
|
|
|
|
|
$watcher = new LibeventWatcher;
|
|
|
|
$watcher->id = $watcherId;
|
|
|
|
$watcher->type = self::$TYPE_STREAM;
|
|
|
|
$watcher->stream = $stream;
|
|
|
|
$watcher->streamFlags = $flags;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
$watcher->wrapper = $this->wrapStreamCallback($watcher);
|
|
|
|
$watcher->isEnabled = (bool) $enableNow;
|
|
|
|
$watcher->eventResource = $eventResource;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
$this->watchers[$watcherId] = $watcher;
|
|
|
|
|
|
|
|
event_set($eventResource, $stream, $flags, $watcher->wrapper);
|
|
|
|
event_base_set($eventResource, $this->base);
|
|
|
|
|
|
|
|
if ($enableNow) {
|
|
|
|
event_add($eventResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function wrapStreamCallback(LibeventWatcher $watcher) {
|
|
|
|
$callback = $watcher->callback;
|
|
|
|
$watcherId = $watcher->id;
|
|
|
|
|
|
|
|
return function($stream) use ($callback, $watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
try {
|
2013-09-08 15:46:25 +02:00
|
|
|
$callback($watcherId, $stream, $this);
|
2013-08-05 22:05:08 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->stopException = $e;
|
|
|
|
$this->stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function cancel($watcherId) {
|
2013-09-11 19:00:43 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
|
|
|
return;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
2013-09-11 19:00:43 +02:00
|
|
|
|
|
|
|
$watcher = $this->watchers[$watcherId];
|
|
|
|
event_del($watcher->eventResource);
|
|
|
|
$this->garbage[] = $watcher;
|
|
|
|
$this->scheduleGarbageCollection();
|
|
|
|
unset($this->watchers[$watcherId]);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function disable($watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
$watcher = $this->watchers[$watcherId];
|
|
|
|
if ($watcher->isEnabled) {
|
|
|
|
event_del($watcher->eventResource);
|
2014-02-23 22:26:28 +01:00
|
|
|
$watcher->isEnabled = false;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function enable($watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
if (!isset($this->watchers[$watcherId])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
$watcher = $this->watchers[$watcherId];
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
if (!$watcher->isEnabled) {
|
|
|
|
event_add($watcher->eventResource, $watcher->interval);
|
2014-02-23 22:26:28 +01:00
|
|
|
$watcher->isEnabled = true;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function scheduleGarbageCollection() {
|
|
|
|
if (!$this->isGCScheduled) {
|
|
|
|
event_add($this->gcEvent, 0);
|
2014-02-23 22:26:28 +01:00
|
|
|
$this->isGCScheduled = true;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function collectGarbage() {
|
|
|
|
$this->garbage = [];
|
2014-02-23 22:26:28 +01:00
|
|
|
$this->isGCScheduled = false;
|
2013-08-05 22:05:08 +02:00
|
|
|
event_del($this->gcEvent);
|
|
|
|
}
|
2013-08-29 08:38:13 +02:00
|
|
|
|
2013-09-11 19:00:43 +02:00
|
|
|
}
|