Example #1
0
 public function connect($port, $host, $connectionListener)
 {
     $self = $this;
     $this->connection = uv_tcp_init();
     $client = $this->connection;
     uv_tcp_connect($this->connection, uv_ip4_addr($host, $port), function ($stream, $stat) use($self, $connectionListener, &$client) {
         $self->setType("tcp4");
         if ($stat == 0) {
             $self->emit("connect");
             if (is_callable($connectionListener)) {
                 $connectionListener();
             }
             //start reading data from server...
             uv_read_start($client, function ($stream, $nread, $buffer) use($self) {
                 if ($nread > 0) {
                     //we got some data from server
                     $self->emit('data', $buffer);
                 }
                 if ($self->shouldClose) {
                     uv_close($stream);
                     $self->emit('close');
                 }
             });
         }
     });
     return $this;
 }
Example #2
0
 public function listen($port, $host = '127.0.0.1', $backlog = 100)
 {
     $this->port = $port;
     $this->host = $host;
     $self = $this;
     $system = $this->getSystem();
     $this->server = uv_tcp_init();
     $request = $self->getServerRequest();
     $response = $self->getServerResponse();
     $requestListener = $self->getRequestListener();
     uv_tcp_bind($this->server, uv_ip4_addr($host, $port));
     uv_listen($this->server, $backlog, function ($server) use($self, &$request, &$response, &$requestListener) {
         $client = uv_tcp_init();
         uv_accept($server, $client);
         uv_read_start($client, function ($socket, $nread, $buffer) use($self, &$request, &$response, &$requestListener) {
             $self->generateServerVariables($buffer, $socket);
             call_user_func_array($requestListener, array($request, $response));
             $status_code = $response->getStatusCode();
             $statusMessage = $response->getReasonPhrase();
             $headers = $response->getHeaders();
             $header = "";
             if (count($headers) > 0) {
                 foreach ($headers as $key => $val) {
                     $header .= $key . ": " . $val . "\r\n";
                 }
             }
             $output = $response->getData();
             $buffer = "HTTP/1.1 {$status_code} {$statusMessage}\r\n{$header}\r\n{$output}";
             uv_write($socket, $buffer);
             uv_close($socket);
         });
     });
 }
Example #3
0
 public function nextTick($callback)
 {
     $loop = $this->getSystem()->getEventLoop();
     $plugin = $this;
     $plugin->addTickCallback($callback);
     $tick = $this->tick;
     if ($tick == 1) {
         $f = function ($r, $status) use($plugin, &$tick) {
             $callback = $plugin->getTickCallback($tick);
             if (is_callable($callback)) {
                 call_user_func($callback);
                 $plugin->removeTickCallback($tick);
                 $tick++;
                 uv_async_send($r);
             } else {
                 uv_close($r);
             }
         };
         $r = uv_async_init($loop, $f);
         uv_async_send($r);
     }
 }
 /**
  * Closes the socket connection.
  *
  * @param callable $callback
  * @return ClientInterface
  */
 public function close(callable $callback = null)
 {
     $client = $this;
     $this->running = false;
     \uv_close($this->connection, function ($resource) use($client, $callback) {
         $client->emit(new Event('close'));
         if ($callback !== null) {
             $callback($client);
         }
     });
     return $this;
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function clear()
 {
     foreach ($this->polls as $poll) {
         \uv_close($poll);
     }
     foreach ($this->timers as $timer) {
         \uv_close($timer);
     }
     $this->polls = [];
     $this->timers = [];
     $this->sockets = [];
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function clear()
 {
     for ($this->timers->rewind(); $this->timers->valid(); $this->timers->next()) {
         \uv_close($this->timers->getInfo());
     }
     $this->timers = new ObjectStorage();
     $this->handles = [];
 }
Example #7
0
<?php

$pipe = uv_pipe_init(uv_default_loop(), 0);
uv_pipe_open($pipe, 1);
//uv_pipe_bind($pipe,"/tmp/hoge.sock");
uv_write($pipe, "Hello", function ($b, $s) {
    echo 1;
    uv_close($b);
});
uv_run();
Example #8
0
<?php

$tcp = uv_tcp_init();
uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0', 9999));
uv_listen($tcp, 100, function ($server) {
    $client = uv_tcp_init();
    uv_accept($server, $client);
    var_dump(uv_tcp_getsockname($server));
    uv_read_start($client, function ($socket, $nread, $buffer) use($server) {
        var_dump($buffer);
        uv_close($socket);
        uv_close($server);
    });
});
$c = uv_tcp_init();
uv_tcp_connect($c, uv_ip4_addr('0.0.0.0', 9999), function ($client, $stat) {
    if ($stat == 0) {
        uv_write($client, "Hello", function ($socket, $stat) {
            uv_close($socket);
        });
    }
});
uv_run();
 /**
  * @return ServerInterface
  */
 public function stop()
 {
     if ($this->registeredListener === true) {
         $server = $this;
         \uv_close($this->getBackend(), function () use($server) {
             $server->registeredListener = false;
         });
     }
     return $this;
 }
Example #10
0
<?php

$udp = uv_udp_init();
var_dump($udp);
uv_udp_bind($udp, uv_ip4_addr('0.0.0.0', 10000));
uv_udp_recv_start($udp, function ($stream, $nread, $buffer) {
    echo "recv:" . $buffer;
    uv_close($stream);
});
$uv = uv_udp_init();
uv_udp_send($uv, "Hello", uv_ip4_addr("0.0.0.0", 10000), function ($uv, $s) {
    echo "success" . PHP_EOL;
    uv_close($uv);
});
uv_run();
Example #11
0
$uv = uv_ares_init_options(uv_default_loop(), array("servers" => array("8.8.8.8"), "port" => 53), null);
ares_gethostbyname($uv, $domain, AF_INET, function ($name, $addr) use($path, $host) {
    $a = array_shift($addr);
    $address = uv_ip4_addr($a, "80");
    $tcp = uv_tcp_init();
    uv_tcp_connect($tcp, $address, function ($client, $stat) use($path, $host) {
        var_dump(uv_tcp_getpeername($client));
        $request = <<<EOF
GET {$path} HTTP/1.0
Host: {$host}


EOF;
        echo $request;
        var_dump($client);
        uv_write($client, $request, function ($client, $stat) {
            echo "write";
            if ($stat == 0) {
                uv_read_start($client, function ($client, $nread, $buffer) {
                    echo "\n1\n";
                    //var_dump($buffer);
                    uv_close($client);
                });
            } else {
                echo 2;
                uv_close($client);
            }
        });
    });
});
uv_run();
Example #12
0
<?php

$in = uv_pipe_init(uv_default_loop(), true);
echo "HELLO ";
$stdio = array();
$stdio[] = uv_stdio_new($in, UV::CREATE_PIPE | UV::READABLE_PIPE);
$fp = fopen("php://stdout", "w");
$stdio[] = uv_stdio_new($fp, UV::INHERIT_FD | UV::WRITABLE_PIPE);
$flags = 0;
uv_spawn(uv_default_loop(), "php", array('-r', 'var_dump($_ENV);'), $stdio, "/usr/bin/", array("key" => "hello"), function ($process, $stat, $signal) {
    uv_close($process, function () {
    });
}, $flags);
uv_run();
Example #13
0
<?php

$loop = uv_default_loop();
$async = uv_async_init($loop, function ($async, $status) {
    var_dump(1);
    uv_close($async);
});
uv_async_send($async);
uv_run();
Example #14
0
<?php

define("PIPE_PATH", dirname(__FILE__) . "/pipe_test.sock");
@unlink(PIPE_PATH);
$a = uv_pipe_init(uv_default_loop(), 0);
$ret = uv_pipe_bind($a, PIPE_PATH);
uv_listen($a, 8192, function ($stream) {
    $pipe = uv_pipe_init(uv_default_loop(), 0);
    uv_accept($stream, $pipe);
    uv_read_start($pipe, function ($pipe, $nread, $buffer) use($stream) {
        echo $buffer;
        uv_read_stop($pipe);
        uv_close($stream, function () {
            @unlink(PIPE_PATH);
        });
    });
});
$b = uv_pipe_init(uv_default_loop(), 0);
uv_pipe_connect($b, PIPE_PATH, function ($a, $b) {
    uv_write($b, "Hello", function ($stream, $stat) {
        uv_close($stream);
    });
});
uv_run();
exit;
Example #15
0
<?php

$in = uv_pipe_init(uv_default_loop(), true);
$out = uv_pipe_init(uv_default_loop(), true);
echo "HELLO ";
$stdio = array();
$stdio[] = uv_stdio_new($in, UV::CREATE_PIPE | UV::READABLE_PIPE);
$stdio[] = uv_stdio_new($out, UV::CREATE_PIPE | UV::WRITABLE_PIPE);
$flags = 0;
uv_spawn(uv_default_loop(), "php", array('-r', 'var_dump($_ENV);'), $stdio, "/usr/bin/", array("KEY" => "hello"), function ($process, $stat, $signal) {
    uv_close($process, function () {
    });
}, $flags);
uv_read2_start($out, function ($out, $nread, $buffer, $stat) {
    echo $buffer;
    uv_close($out, function () {
    });
});
uv_run();
Example #16
0
 /**
  * {@inheritdoc}
  */
 protected function disableSignalWatcher(SignalWatcher $watcher, bool $dispose = false)
 {
     if ($watcher->event === null) {
         return;
     }
     unset($this->signalWatchers[$watcher->signo][$watcher->id]);
     if (empty($this->signalWatchers[$watcher->signo])) {
         unset($this->signalWatchers[$watcher->signo]);
         if (\uv_is_active($watcher->event)) {
             \uv_signal_stop($watcher->event);
         }
     }
     if ($dispose) {
         try {
             if ($this->dispatching) {
                 $this->dispose->enqueue($watcher->event);
             } else {
                 \uv_close($watcher->event);
             }
         } finally {
             $watcher->event = null;
         }
     }
 }
Example #17
0
 public function onShutdown($handle, $status)
 {
     uv_close($handle, array($this, "onClose"));
 }
Example #18
0
                    unset($clients[(int) $client]);
                });
            });
            return;
        } else {
            if ($nread == 0) {
                if (uv_last_error() == UV::EOF) {
                    uv_shutdown($client, function ($client) use(&$parsers, &$clients) {
                        uv_close($client, function ($client) use(&$parsers, &$clients) {
                            unset($parsers[(int) $client]);
                            unset($clients[(int) $client]);
                        });
                    });
                    return;
                }
            } else {
                $result = array();
                if (uv_http_parser_execute($parsers[(int) $client], $buffer, $result)) {
                    $response = "HTTP/1.1 200 OK\r\n\r\nHello World";
                    uv_write($client, $response, function ($client) use(&$parsers, &$clients) {
                        uv_close($client, function ($client) use(&$parsers, &$clients) {
                            unset($parsers[(int) $client]);
                            unset($clients[(int) $client]);
                        });
                    });
                }
            }
        }
    });
});
uv_run(uv_default_loop());