2 client curl user handler
13 class UserHandler implements http\Client\Curl\User
22 function __construct(http\Client $client, EventBase $evbase) {
23 $this->evbase = $evbase;
24 $this->client = $client;
31 function timer(int $timeout_ms) {
33 if (isset($this->timeout)) {
34 $this->timeout->add($timeout_ms/1000);
36 $this->timeout = Event::timer($this->evbase, function() {
37 if (!call_user_func($this->run, $this->client)) {
39 $this->timeout->del();
40 $this->timeout = null;
44 $this->timeout->add($timeout_ms/1000);
48 function socket($socket, int $action) {
54 case self::POLL_REMOVE:
55 if (isset($this->ios[(int) $socket])) {
57 $this->ios[(int) $socket]->del();
58 unset($this->ios[(int) $socket]);
64 if ($action & self::POLL_IN) {
67 if ($action & self::POLL_OUT) {
70 if (isset($this->ios[(int) $socket])) {
71 $this->ios[(int) $socket]->set($this->evbase,
72 $socket, $ev, $this->onEvent($socket));
74 $this->ios[(int) $socket] = new Event($this->evbase,
75 $socket, $ev, $this->onEvent($socket));
81 function onEvent($socket) {
82 return function($watcher, $events) use($socket) {
84 if ($events & Ev::READ) {
85 $action |= self::POLL_IN;
87 if ($events & Ev::WRITE) {
88 $action |= self::POLL_OUT;
90 if (!call_user_func($this->run, $this->client, $socket, $action)) {
92 $this->timeout->del();
93 $this->timeout = null;
100 throw new BadMethodCallException("this test uses EventBase::loop()");
103 function wait(int $timeout_ms = null) {
104 throw new BadMethodCallException("this test uses EventBase::loop()");
108 throw new BadMethodCallException("this test uses EventBase::loop()");
113 include "helper/server.inc";
115 server("proxy.inc", function($port) {
116 $evbase = new EventBase;
117 $client = new http\Client;
119 "use_eventloop" => new UserHandler($client, $evbase)
121 $client->enqueue(new http\Client\Request("GET", "http://localhost:$port/"), function($r) {
122 var_dump($r->getResponseCode());
131 T*[ST]+U+T*int\(200\)