2 client curl user handler
14 class UserHandler implements http\Client\Curl\User
22 function __construct(http\Client $client) {
23 $this->client = $client;
26 function init(callable $run) {
30 function timer($timeout_ms) {
32 if (isset($this->timeout)) {
33 $this->timeout->set($timeout_ms/1000, 0);
34 $this->timeout->start();
36 $this->timeout = new EvTimer($timeout_ms/1000, 0, function() {
37 if (!call_user_func($this->run, $this->client)) {
39 $this->timeout->stop();
40 $this->timeout = null;
47 function socket($socket, $action) {
53 case self::POLL_REMOVE:
54 if (isset($this->ios[(int) $socket])) {
56 $this->ios[(int) $socket]->stop();
57 unset($this->ios[(int) $socket]);
63 if ($action & self::POLL_IN) {
66 if ($action & self::POLL_OUT) {
69 if (isset($this->ios[(int) $socket])) {
70 $this->ios[(int) $socket]->set($socket, $ev);
72 $this->ios[(int) $socket] = new EvIo($socket, $ev, function($watcher, $events) use($socket) {
74 if ($events & Ev::READ) {
75 $action |= self::POLL_IN;
77 if ($events & Ev::WRITE) {
78 $action |= self::POLL_OUT;
80 if (!call_user_func($this->run, $this->client, $socket, $action)) {
82 $this->timeout->stop();
83 $this->timeout = null;
93 throw new BadMethodCallException("this test uses Ev::run()");
96 function wait($timeout_ms = null) {
97 throw new BadMethodCallException("this test uses Ev::run()");
101 throw new BadMethodCallException("this test uses Ev::run()");
106 include "helper/server.inc";
108 server("proxy.inc", function($port) {
109 $client = new http\Client;
111 "use_eventloop" => new UserHandler($client)
113 $client->enqueue(new http\Client\Request("GET", "http://localhost:$port/"), function($r) {
114 var_dump($r->getResponseCode());