6 * Asynchronnous resource handling, optionally (ab)using ticks
13 * $conn = new \pq\Connection;
14 * $conn->execAsync("SELECT * FROM foo", function ($rs) {
18 * $ticker = new \atick\Ticker;
19 * $ticker->register();
20 * $ticker->read($conn->socket, function($fd) use ($conn) {
29 * while (count($ticker));
33 * And an example without ticks:
36 * $conn = new \pq\Connection;
37 * $conn->execAsync("SELECT * FROM foo", function ($r) {
41 * $ticker = new \atick\Ticker;
42 * $ticker->read($conn->socket, function($fd) use ($conn) {
55 class Ticker
implements \Countable
60 protected $read = array();
65 protected $write = array();
68 * Register the ticker as tick function
69 * @return \atick\Ticker
72 register_tick_function(array($this, "__invoke"));
77 * Unregister the ticker as tick function
78 * @return \atick\Ticker
80 function unregister() {
81 unregister_tick_function(array($this, "__invoke"));
86 * The tick handler; calls atick\Ticker::wait(0)
89 function __invoke($timeout = 0) {
90 return $this->wait($timeout);
94 * Wait for read/write readiness on the watched fds
95 * @param float $timeout
96 * @return int count of wached fds
98 function wait($timeout = 1) {
99 $r = $w = $e = array();
101 foreach ($this->read
as $s) {
102 is_resource($s[0]) and $r[] = $s[0];
105 foreach ($this->write
as $s) {
106 is_resource($s[0]) and $w[] = $s[0];
110 $u = (int) (($timeout - $t) * 1000000);
112 if (($r ||
$w) && stream_select($r, $w, $e, $t, $u)) {
114 $this->read
[(int)$s][1]($s);
117 $this->write
[(int)$s][1]($s);
121 return $this->count();
125 * Returns the count of watched fds
126 * @implements \Countable
130 foreach ($this->read
as $i => $s) {
131 list($fd,,$verify) = $s;
133 unset($this->read
[$i]);
137 foreach ($this->write
as $i => $s) {
138 list($fd,,$verify) = $s;
140 unset($this->write
[$i]);
144 return count($this->read
) +
count($this->write
);
148 * Attach a read handler
149 * @param resource $fd
150 * @param callable $onread void($fd) the descriptor is readable, read data, now!
151 * @param callable $verify bool($fd) wheter the fd is still valid and should be watched
152 * @return \atick\Ticker
154 function read($fd, callable
$onread, callable
$verify = null) {
155 $this->read
[(int)$fd] = array($fd, $onread, $verify ?
: function($fd) {
156 return is_resource($fd) && !feof($fd);
162 * Attach a write handler
163 * @param resource $fd
164 * @param callable $onwrite void($fd) the descriptor is writable, write data.
165 * @param callable $verify bool($fd) wheter the fd is still valid and should be watched
166 * @return \atick\Ticker
168 function write($fd, callable
$onwrite, callable
$verify = null) {
169 $this->write
[(int)$fd] = array($fd, $onwrite, $verify ?
: function($fd) {
170 return is_resource($fd) && !feof($fd);