public function __construct() { $system = $this; //set up the event handler set_error_handler(function ($severity, $message, $filePath, $line) use(&$system) { $error = new Error($severity, $message, $filePath, $line); $system->emit("system.error", $error); }); //set up exception handler set_exception_handler(function ($exception) use(&$system) { $system->emit("system.exception", $exception); }); //on shutdown, run the event loop register_shutdown_function(function () { uv_run(); }); // start the event loop $this->eventLoop = uv_default_loop(); //set up the core module map $modulePrefix = "\\Phastlight\\Module\\"; $this->moduleMap = array('console' => $modulePrefix . 'Console\\Main', 'process' => $modulePrefix . 'Process\\Main', 'os' => $modulePrefix . 'OS\\Main', 'http' => $modulePrefix . 'HTTP\\Main', 'timer' => $modulePrefix . 'Timer\\Main', 'util' => $modulePrefix . 'Util\\Main', 'fs' => $modulePrefix . 'FileSystem\\Main', 'net' => $modulePrefix . 'NET\\Main', 'child_process' => $modulePrefix . 'ChildProcess\\Main', 'cluster' => $modulePrefix . 'Cluster\\Main'); $this->modules = array(); }
<?php uv_getaddrinfo(uv_default_loop(), function ($s, $names) { var_dump($names); }, "yahoo.com", NULL, array("ai_family" => UV::AF_UNSPEC)); uv_run();
/** * Create a new uv loop. * * @param LoopConfig $config */ public function __construct(LoopConfig $config = null) { parent::__construct($config); // $this->loop = $loop ?? \uv_loop_new(); // TODO: Investigate why new uv loops cannot be deleted (segfault) and leak file descriptors... $this->loop = \uv_default_loop(); $this->dispose = new \SplQueue(); $this->dispose->setIteratorMode(\SplQueue::IT_MODE_FIFO | \SplQueue::IT_MODE_DELETE); $this->config->setHostResolver(new Resolver($this)); $this->config->setFilesystem(new Filesystem($this, $this->config->getFilesystemObserver())); if (\defined('PHP_ZTS') && \PHP_ZTS) { // TODO: Implement a pool based on uv_queue_work() if ZTS is enabled (unusable: "zend_mm_heap corrupted"). } $this->delayCallback = function ($event) { $watcher = $this->timerWatchers[(int) $event] ?? null; if ($watcher) { $this->info[Watcher::TYPE_DELAY]--; unset($this->timerWatchers[(int) $event], $this->watchers[$watcher->id], $this->enable[$watcher->id]); if ($watcher->referenced) { $this->watchersReferenced--; } $watcher->event = null; $this->dispose->enqueue($event); ($watcher->callback)($watcher->id, $watcher->data); } }; $this->repeatCallback = function ($event) { $watcher = $this->timerWatchers[(int) $event] ?? null; if ($watcher) { ($watcher->callback)($watcher->id, $watcher->data); } }; $this->streamCallback = function ($event, int $status, int $events) { $id = (int) $event; if (($events === 0 || $events & \UV::READABLE) && isset($this->readWatchers[$id])) { foreach ($this->readWatchers[$id] as $watcher) { ($watcher->callback)($watcher->id, $watcher->stream, $watcher->data); } } if (($events === 0 || $events & \UV::WRITABLE) && isset($this->writeWatchers[$id])) { foreach ($this->writeWatchers[$id] as $watcher) { ($watcher->callback)($watcher->id, $watcher->stream, $watcher->data); } } }; $this->signalCallback = function ($event, int $signo) { if (isset($this->signalWatchers[$signo])) { foreach ($this->signalWatchers[$signo] as $watcher) { ($watcher->callback)($watcher->id, $watcher->signo, $watcher->data); } } }; }
<?php uv_fs_readdir(uv_default_loop(), ".", 0, function ($result, $da) { var_dump($da); }); uv_run();
<?php $loop = uv_default_loop(); $timer = uv_timer_init(); $i = 0; uv_timer_start($timer, 1000, 1000, function ($stat) use(&$i, $timer, $loop) { echo "count: {$i}" . PHP_EOL; $i++; if ($i > 3) { uv_timer_stop($timer); uv_unref($timer); } }); uv_run(); echo "finished";
<?php uv_fs_utime(uv_default_loop(), __FILE__, time(), time(), function () { echo "Finished"; }); uv_run();
<?php uv_fs_open(uv_default_loop(), __FILE__, UV::O_RDONLY, 0, function ($r) { uv_fs_fstat(uv_default_loop(), $r, function ($result, $da) { var_dump($da); }); }); uv_run();
<?php uv_fs_chmod(uv_default_loop(), "moe", 0777, function ($result) { var_dump($result); }); uv_run();
<?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();
<?php uv_fs_event_init(uv_default_loop(), "/tmp/", function ($rsc, $name, $event, $stat) { var_dump($name); var_dump($event); }, 0); uv_run();
<?php uv_fs_open(uv_default_loop(), __FILE__, UV::O_RDONLY, 0, function ($read_fd) { $stdout = 0; uv_fs_sendfile(uv_default_loop(), $stdout, $read_fd, 0, 6, function ($result) { }); }); uv_run();
<?php uv_fs_readlink(uv_default_loop(), "li", function ($result, $buffer) { var_dump($result); var_dump($buffer); }); uv_run();
<?php uv_fs_open(uv_default_loop(), "/dev/tty", UV::O_RDONLY, 0, function ($r) { $tty = uv_tty_init(uv_default_loop(), $r, 1); var_dump(uv_tty_get_winsize($tty, $width, $height)); var_dump($width, $height); }); uv_run();
public function __construct() { $this->eventBase = uv_default_loop(); }
<?php uv_fs_unlink(uv_default_loop(), "moe", function ($result) { var_dump($result); }); uv_run();
}); }); 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());
<?php uv_fs_mkdir(uv_default_loop(), "hoge", 0644, function ($result) { var_dump($result); }); uv_run();
<?php uv_fs_open(uv_default_loop(), "./tmp", UV::O_WRONLY, UV::S_IRWXU | UV::S_IRUSR, function ($r) { var_dump($r); uv_fs_ftruncate(uv_default_loop(), $r, 0, function () use($r) { uv_fs_close(uv_default_loop(), $r, function () { }); }); }); uv_run();
<?php $socket = stream_socket_server("tcp://0.0.0.0:9999", $errno, $errstr); stream_set_blocking($socket, 0); $poll = uv_poll_init(uv_default_loop(), $socket); uv_poll_start($poll, UV::READABLE, function ($poll, $stat, $ev, $socket) { echo "parent poll:\n"; var_dump($stat); $conn = stream_socket_accept($socket); $pp = uv_poll_init(uv_default_loop(), $conn); uv_poll_start($pp, UV::WRITABLE, function ($poll, $stat, $ev, $conn) { echo " cb"; var_dump($stat); var_dump($conn); uv_poll_stop($poll); uv_fs_write(uv_default_loop(), $conn, "Hello World", -1, function ($conn, $nwrite) { var_dump($conn); fclose($conn); }); }); }); uv_run();
<?php $uv = uv_ares_init_options(uv_default_loop(), array("servers" => array("8.8.8.8"), "port" => 53), null); ares_gethostbyname($uv, "google.com", AF_INET, function ($name, $addr) { var_dump($name); var_dump($addr); }); uv_run();
<?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();
<?php uv_fs_rename(uv_default_loop(), "moe", "moe2", function ($result) { var_dump($result); }); uv_run();
<?php uv_fs_lstat(uv_default_loop(), __FILE__, function ($result, $da) { var_dump($da); }); uv_run();
<?php uv_fs_open(uv_default_loop(), __FILE__, UV::O_RDONLY, 0, function ($r) { uv_fs_read(uv_default_loop(), $r, function ($stream, $nread, $data) { if ($nread <= 0) { if ($nread < 0) { throw new Exception("read error"); } uv_fs_close(uv_default_loop(), $stream, function () { }); } else { echo $data; } }); }); uv_run();
<?php $poll = uv_fs_poll_init(uv_default_loop()); uv_fs_poll_start($poll, function ($rsc, $stat, $p) { var_dump(1); var_dump($p); //uv_fs_poll_stop($rsc); }, "/tmp/hoge", 1); uv_run();
/** * Stops the loop (mainly in case the loop would have been run infinitely) * * @param int|null $signal * @return LoopInterface */ public function stop($signal = null) { if ($this->loop !== \uv_default_loop()) { \uv_loop_delete($this->loop); } return $this; }
<?php uv_fs_open(uv_default_loop(), "./tmp", UV::O_WRONLY | UV::O_CREAT | UV::O_APPEND, UV::S_IRWXU | UV::S_IRUSR, function ($r) { var_dump($r); uv_fs_write(uv_default_loop(), $r, "hello", 0, function ($a) use($r) { var_dump($a); var_dump("ok"); uv_fs_fdatasync(uv_default_loop(), $r, function () { echo "fsync finished"; }); }); }); uv_run();
public function listen($port) { uv_tcp_nodelay($this->server, 1); uv_tcp_bind6($this->server, uv_ip6_addr("::1", $port)); uv_listen($this->server, 511, array($this, "onConnect")); uv_run(uv_default_loop()); }
public function listen($port) { printf("# server listened at {$port}\n"); uv_tcp_nodelay($this->server, 1); uv_tcp_bind($this->server, uv_ip4_addr("0.0.0.0", $port)); uv_listen($this->server, 511, array($this, "onConnect")); uv_run(uv_default_loop()); }
<?php uv_fs_rmdir(uv_default_loop(), "hoge", function ($result) { var_dump($result); }); uv_run();