2 client curl user handler
12 class UserHandler implements http\Client\Curl\User
22 private $timeout = 1000;
24 function __construct(http\Client $client) {
25 $this->client = $client;
32 function timer(int $timeout_ms) {
34 $this->timeout = $timeout_ms;
37 function socket($socket, int $action) {
43 case self::POLL_REMOVE:
44 if (false !== ($r = array_search($socket, $this->fds["R"], true))) {
46 unset($this->fds["R"][$r]);
49 if (false !== ($w = array_search($socket, $this->fds["W"], true))) {
51 unset($this->fds["W"][$w]);
57 if ($action & self::POLL_IN) {
58 if (!in_array($socket, $this->fds["R"], true)) {
59 $this->fds["R"][] = $socket;
62 if ($action & self::POLL_OUT) {
63 if (!in_array($socket, $this->fds["W"], true)) {
64 $this->fds["W"][] = $socket;
74 foreach ($this->W as $w) {
75 call_user_func($this->run, $this->client, $w, self::POLL_OUT);
77 foreach ($this->R as $r) {
78 call_user_func($this->run, $this->client, $r, self::POLL_IN);
80 return count($this->client);
83 function wait(int $timeout_ms = null) {
86 if ($timeout_ms === null) {
87 $timeout_ms = $this->timeout;
89 $ts = floor($timeout_ms / 1000);
90 $tu = ($timeout_ms % 1000) * 1000;
94 if (($wfds = count($R) + count($W))) {
95 $nfds = stream_select($R, $W, $E, $ts, $tu);
99 $this->R = (array) $R;
100 $this->W = (array) $W;
102 if ($nfds === false) {
108 time_nanosleep($ts, $tu*1000);
110 call_user_func($this->run, $this->client);
123 include "helper/server.inc";
125 server("proxy.inc", function($port) {
126 $client = new http\Client;
127 $client->configure(array(
128 "use_eventloop" => new UserHandler($client)
130 $client->enqueue(new http\Client\Request("GET", "http://localhost:$port/"), function($r) {
131 var_dump($r->getResponseCode());
140 T[WST]*(O[WST]*)+U+int\(200\)