2016-01-20 12:01:40 +01:00
|
|
|
<?php
|
|
|
|
|
2016-05-26 17:05:17 +02:00
|
|
|
namespace Interop\Async\Loop;
|
2016-01-20 12:01:40 +01:00
|
|
|
|
2016-09-04 22:45:37 +02:00
|
|
|
/**
|
|
|
|
* Event loop driver which implements all basic operations to allow interoperability.
|
|
|
|
*/
|
2016-08-04 21:08:52 +02:00
|
|
|
abstract class Driver
|
2016-01-20 12:01:40 +01:00
|
|
|
{
|
2016-08-04 21:08:52 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $registry = [];
|
|
|
|
|
2016-02-17 16:25:39 +01:00
|
|
|
/**
|
2016-03-14 11:56:31 +01:00
|
|
|
* Start the event loop.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-20 12:49:31 +02:00
|
|
|
* The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an
|
|
|
|
* exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an
|
2016-05-22 19:36:13 +02:00
|
|
|
* error handler or exceptions that would be passed to an error handler but none exists to handle them.
|
2016-05-20 12:49:31 +02:00
|
|
|
*
|
2016-02-17 16:25:39 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function run();
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-03-14 11:56:31 +01:00
|
|
|
* Stop the event loop.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-20 12:49:31 +02:00
|
|
|
* When an event loop is stopped, it continues with its current tick and exits the loop afterwards. Multiple calls
|
|
|
|
* to stop MUST be ignored and MUST NOT raise an exception.
|
|
|
|
*
|
2016-02-17 16:25:39 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function stop();
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-03-14 11:56:31 +01:00
|
|
|
* Defer the execution of a callback.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-06-26 16:15:01 +02:00
|
|
|
* The deferred callable MUST be executed in the next tick of the event loop and before any other type of watcher.
|
|
|
|
* Order of enabling MUST be preserved when executing the callbacks.
|
2016-05-20 12:49:31 +02:00
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param callable(string $watcherId, mixed $data) $callback The callback to defer. The `$watcherId` will be
|
2016-06-26 16:15:01 +02:00
|
|
|
* invalidated before the callback call.
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-28 15:57:07 +02:00
|
|
|
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
2016-02-17 16:25:39 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function defer(callable $callback, $data = null);
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-20 12:49:31 +02:00
|
|
|
* Delay the execution of a callback.
|
|
|
|
*
|
2016-06-26 16:15:01 +02:00
|
|
|
* The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which
|
2016-09-25 13:22:01 +02:00
|
|
|
* timers expire first, but timers with the same expiration time MAY be executed in any order.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-12 12:28:46 +02:00
|
|
|
* @param int $delay The amount of time, in milliseconds, to delay the execution for.
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param callable(string $watcherId, mixed $data) $callback The callback to delay. The `$watcherId` will be
|
2016-06-26 16:15:01 +02:00
|
|
|
* invalidated before the callback call.
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-28 15:57:07 +02:00
|
|
|
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
2016-02-17 16:25:39 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function delay($delay, callable $callback, $data = null);
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-20 12:49:31 +02:00
|
|
|
* Repeatedly execute a callback.
|
|
|
|
*
|
2016-06-26 16:15:01 +02:00
|
|
|
* The interval between executions is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be
|
|
|
|
* determined by which timers expire first, but timers with the same expiration time may be executed in any order.
|
2016-05-23 20:29:10 +02:00
|
|
|
* The first execution is scheduled after the first interval period.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-12 12:28:46 +02:00
|
|
|
* @param int $interval The time interval, in milliseconds, to wait between executions.
|
2016-05-22 18:07:21 +02:00
|
|
|
* @param callable(string $watcherId, mixed $data) $callback The callback to repeat.
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-28 15:57:07 +02:00
|
|
|
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
2016-02-17 16:25:39 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function repeat($interval, callable $callback, $data = null);
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-09-23 16:02:23 +02:00
|
|
|
* Execute a callback when a stream resource becomes readable or is closed for reading.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-10-24 17:08:53 +02:00
|
|
|
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
|
|
|
* watcher when closing the resource locally. Drivers might choose to notify the user in a debug mode if there are
|
|
|
|
* watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed
|
|
|
|
* resources are therefore undefined behavior.
|
|
|
|
*
|
2016-06-26 16:15:01 +02:00
|
|
|
* Multiple watchers on the same stream may be executed in any order.
|
2016-06-04 18:27:30 +02:00
|
|
|
*
|
2016-03-14 11:56:31 +01:00
|
|
|
* @param resource $stream The stream to monitor.
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-28 15:57:07 +02:00
|
|
|
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
2016-02-17 16:25:39 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function onReadable($stream, callable $callback, $data = null);
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-09-23 16:02:23 +02:00
|
|
|
* Execute a callback when a stream resource becomes writable or is closed for writing.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-10-24 17:08:53 +02:00
|
|
|
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
|
|
|
|
* watcher when closing the resource locally. Drivers might choose to notify the user in a debug mode if there are
|
|
|
|
* watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed
|
|
|
|
* resources are therefore undefined behavior.
|
|
|
|
*
|
2016-06-26 16:15:01 +02:00
|
|
|
* Multiple watchers on the same stream may be executed in any order.
|
2016-06-04 18:27:30 +02:00
|
|
|
*
|
2016-03-14 11:56:31 +01:00
|
|
|
* @param resource $stream The stream to monitor.
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-28 15:57:07 +02:00
|
|
|
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
2016-02-17 16:25:39 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function onWritable($stream, callable $callback, $data = null);
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-03-14 11:56:31 +01:00
|
|
|
* Execute a callback when a signal is received.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-10-24 17:08:53 +02:00
|
|
|
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
|
2016-09-04 22:45:37 +02:00
|
|
|
* Implementations may try to detect this, if possible, but are not required to. This is due to technical
|
|
|
|
* limitations of the signals being registered globally per process.
|
2016-07-19 18:10:51 +02:00
|
|
|
*
|
2016-10-24 17:08:53 +02:00
|
|
|
* Multiple watchers on the same signal may be executed in any order.
|
|
|
|
*
|
2016-03-14 11:56:31 +01:00
|
|
|
* @param int $signo The signal number to monitor.
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
|
2016-05-15 00:13:35 +02:00
|
|
|
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-28 15:57:07 +02:00
|
|
|
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
|
2016-05-26 17:43:03 +02:00
|
|
|
*
|
2016-07-15 22:17:24 +02:00
|
|
|
* @throws UnsupportedFeatureException If signal handling is not supported.
|
2016-02-17 16:25:39 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function onSignal($signo, callable $callback, $data = null);
|
2016-02-17 16:25:39 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-15 00:19:05 +02:00
|
|
|
* Enable a watcher.
|
2016-07-15 22:17:24 +02:00
|
|
|
*
|
2016-06-26 16:15:01 +02:00
|
|
|
* Watchers (enabling or new watchers) MUST immediately be marked as enabled, but only be activated (i.e. callbacks
|
2016-09-25 13:22:01 +02:00
|
|
|
* can be called) right before the next tick. Callbacks of watchers MUST not be called in the tick they were
|
|
|
|
* enabled.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param string $watcherId The watcher identifier.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-03-01 01:52:59 +01:00
|
|
|
* @return void
|
2016-05-28 15:57:07 +02:00
|
|
|
*
|
2016-07-15 22:17:24 +02:00
|
|
|
* @throws InvalidWatcherException If the watcher identifier is invalid.
|
2016-02-17 16:25:39 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function enable($watcherId);
|
2016-03-01 01:52:59 +01:00
|
|
|
|
|
|
|
/**
|
2016-09-25 13:22:01 +02:00
|
|
|
* Disable a watcher.
|
|
|
|
*
|
|
|
|
* Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an
|
|
|
|
* invalid watcher.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param string $watcherId The watcher identifier.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-03-01 01:52:59 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function disable($watcherId);
|
2016-03-01 01:52:59 +01:00
|
|
|
|
|
|
|
/**
|
2016-09-25 13:22:01 +02:00
|
|
|
* Cancel a watcher.
|
|
|
|
*
|
|
|
|
* This will detatch the event loop from all resources that are associated to the watcher. After this operation the
|
|
|
|
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param string $watcherId The watcher identifier.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-03-01 01:52:59 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function cancel($watcherId);
|
2016-03-23 10:47:18 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-15 00:19:05 +02:00
|
|
|
* Reference a watcher.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-20 12:49:31 +02:00
|
|
|
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by
|
|
|
|
* default.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param string $watcherId The watcher identifier.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-03-23 10:47:18 +01:00
|
|
|
* @return void
|
2016-05-28 15:57:07 +02:00
|
|
|
*
|
2016-07-15 22:17:24 +02:00
|
|
|
* @throws InvalidWatcherException If the watcher identifier is invalid.
|
2016-03-23 10:47:18 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function reference($watcherId);
|
2016-03-23 10:47:18 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-15 00:19:05 +02:00
|
|
|
* Unreference a watcher.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-20 12:49:31 +02:00
|
|
|
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers
|
|
|
|
* are all referenced by default.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-05-19 17:21:26 +02:00
|
|
|
* @param string $watcherId The watcher identifier.
|
2016-05-12 17:57:02 +02:00
|
|
|
*
|
2016-03-23 10:47:18 +01:00
|
|
|
* @return void
|
2016-05-28 15:57:07 +02:00
|
|
|
*
|
2016-07-15 22:17:24 +02:00
|
|
|
* @throws InvalidWatcherException If the watcher identifier is invalid.
|
2016-03-23 10:47:18 +01:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function unreference($watcherId);
|
2016-07-15 22:17:24 +02:00
|
|
|
|
2016-05-27 17:45:45 +02:00
|
|
|
/**
|
2016-09-25 13:22:01 +02:00
|
|
|
* Stores information in the loop bound registry.
|
2016-05-27 17:45:45 +02:00
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* This can be used to store loop bound information. Stored information is package private. Packages MUST NOT
|
|
|
|
* retrieve the stored state of other packages. Packages MUST use the following prefix for keys: `vendor.package.`
|
2016-05-27 17:45:45 +02:00
|
|
|
*
|
2016-09-04 22:45:37 +02:00
|
|
|
* @param string $key The namespaced storage key.
|
|
|
|
* @param mixed $value The value to be stored.
|
2016-05-27 17:45:45 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-06 14:49:08 +02:00
|
|
|
final public function setState($key, $value)
|
2016-08-04 21:08:52 +02:00
|
|
|
{
|
|
|
|
if ($value === null) {
|
|
|
|
unset($this->registry[$key]);
|
|
|
|
} else {
|
|
|
|
$this->registry[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 17:45:45 +02:00
|
|
|
|
|
|
|
/**
|
2016-09-25 13:22:01 +02:00
|
|
|
* Gets information stored bound to the loop.
|
2016-05-27 17:45:45 +02:00
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
|
|
|
|
* MUST use the following prefix for keys: `vendor.package.`
|
2016-05-27 17:45:45 +02:00
|
|
|
*
|
2016-09-04 22:45:37 +02:00
|
|
|
* @param string $key The namespaced storage key.
|
2016-05-27 17:45:45 +02:00
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* @return mixed The previously stored value or `null` if it doesn't exist.
|
2016-05-27 17:45:45 +02:00
|
|
|
*/
|
2016-08-06 14:49:08 +02:00
|
|
|
final public function getState($key)
|
2016-08-04 21:08:52 +02:00
|
|
|
{
|
|
|
|
return isset($this->registry[$key]) ? $this->registry[$key] : null;
|
|
|
|
}
|
2016-05-13 23:41:54 +02:00
|
|
|
|
2016-05-18 13:12:42 +02:00
|
|
|
/**
|
|
|
|
* Set a callback to be executed when an error occurs.
|
|
|
|
*
|
2016-10-24 20:43:21 +02:00
|
|
|
* The callback receives the error as the first and only parameter. The return value of the callback gets ignored.
|
|
|
|
* If it can't handle the error, it MUST throw the error. Errors thrown by the callback or during its invocation
|
|
|
|
* MUST be thrown into the `run` loop and stop the driver.
|
|
|
|
*
|
2016-05-18 13:12:42 +02:00
|
|
|
* Subsequent calls to this method will overwrite the previous handler.
|
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* @param callable(\Throwable|\Exception $error)|null $callback The callback to execute. `null` will clear the
|
|
|
|
* current handler.
|
2016-05-18 13:12:42 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function setErrorHandler(callable $callback = null);
|
2016-05-18 13:12:42 +02:00
|
|
|
|
2016-05-21 11:00:01 +02:00
|
|
|
/**
|
|
|
|
* Retrieve an associative array of information about the event loop driver.
|
|
|
|
*
|
2016-07-15 22:17:24 +02:00
|
|
|
* The returned array MUST contain the following data describing the driver's currently registered watchers:
|
2016-05-21 11:00:01 +02:00
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* [
|
|
|
|
* "defer" => ["enabled" => int, "disabled" => int],
|
|
|
|
* "delay" => ["enabled" => int, "disabled" => int],
|
|
|
|
* "repeat" => ["enabled" => int, "disabled" => int],
|
|
|
|
* "on_readable" => ["enabled" => int, "disabled" => int],
|
|
|
|
* "on_writable" => ["enabled" => int, "disabled" => int],
|
|
|
|
* "on_signal" => ["enabled" => int, "disabled" => int],
|
|
|
|
* "watchers" => ["referenced" => int, "unreferenced" => int],
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* Implementations MAY optionally add more information in the array but at minimum the above `key => value` format
|
2016-07-15 22:17:24 +02:00
|
|
|
* MUST always be provided.
|
2016-05-21 11:00:01 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function info();
|
2016-05-21 11:00:01 +02:00
|
|
|
|
2016-05-19 01:12:56 +02:00
|
|
|
/**
|
|
|
|
* Get the underlying loop handle.
|
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* Example: the `uv_loop` resource for `libuv` or the `EvLoop` object for `libev` or `null` for a native driver.
|
2016-05-19 01:12:56 +02:00
|
|
|
*
|
2016-10-24 17:08:53 +02:00
|
|
|
* Note: This function is *not* exposed in the `Loop` class. Users shall access it directly on the respective loop
|
2016-07-15 22:17:24 +02:00
|
|
|
* instance.
|
2016-05-21 01:31:18 +02:00
|
|
|
*
|
2016-09-25 13:22:01 +02:00
|
|
|
* @return null|object|resource The loop handle the event loop operates on. `null` if there is none.
|
2016-05-19 01:12:56 +02:00
|
|
|
*/
|
2016-08-05 13:56:26 +02:00
|
|
|
abstract public function getHandle();
|
2016-01-20 12:01:40 +01:00
|
|
|
}
|