Update examples

This commit is contained in:
Bob Weinand 2020-02-15 22:36:03 +01:00
parent 5be79e433d
commit 99312394a7
35 changed files with 92 additions and 153 deletions

View File

@ -5,7 +5,7 @@ $check = uv_check_init($loop);
$idle = uv_idle_init(); $idle = uv_idle_init();
$i = 0; $i = 0;
uv_idle_start($idle, function($stat) use (&$i, $idle, $loop){ uv_idle_start($idle, function($status) use (&$i, $idle, $loop){
echo "count: {$i}" . PHP_EOL; echo "count: {$i}" . PHP_EOL;
$i++; $i++;
@ -15,8 +15,8 @@ uv_idle_start($idle, function($stat) use (&$i, $idle, $loop){
sleep(1); sleep(1);
}); });
uv_check_start($check, function($check, $status){ uv_check_start($check, function($check) {
echo "Hello"; echo "Hello\n";
uv_check_stop($check); uv_check_stop($check);
}); });

View File

@ -1,6 +1,6 @@
<?php <?php
uv_fs_chmod(uv_default_loop(), "moe", 0777, function($result){ uv_fs_chmod(uv_default_loop(), "moe", 0777, function($result) {
var_dump($result); var_dump($result);
}); });

View File

@ -4,7 +4,9 @@ $timer = uv_timer_init();
$stat = array(); $stat = array();
$stat['begin'] = memory_get_usage(); $stat['begin'] = memory_get_usage();
uv_timer_start($timer, 10, 1000, function($stat, $timer) use (&$stat){ uv_timer_start($timer, 10, 1000, function($timer) use (&$stat) {
$stat["current"] = memory_get_usage(); $stat["current"] = memory_get_usage();
printf("memory: %d\n", $stat["current"] - $stat['begin']); printf("memory: %d\n", $stat["current"] - $stat['begin']);
}); });
uv_run();

View File

@ -15,10 +15,10 @@ function pad($str)
return str_pad($str, 20, ' ',STR_PAD_RIGHT); return str_pad($str, 20, ' ',STR_PAD_RIGHT);
} }
uv_listen($tcp,100, function($server) use ($users){ uv_listen($tcp, 100, function($server) use ($users) {
$client = uv_tcp_init(); $client = uv_tcp_init();
uv_accept($server, $client); uv_accept($server, $client);
uv_read_start($client, function($socket, $nread, $buffer) use ($users){ uv_read_start($client, function($socket, $buffer) use ($users){
$buffer = str_replace("/W","",$buffer); $buffer = str_replace("/W","",$buffer);
if ($buffer == "\r\n") { if ($buffer == "\r\n") {
$data = ""; $data = "";
@ -28,7 +28,7 @@ uv_listen($tcp,100, function($server) use ($users){
$data .= join("", array_map("pad",array_values($user))) . "\r\n"; $data .= join("", array_map("pad",array_values($user))) . "\r\n";
} }
uv_write($socket, $data, function($client, $stat){ uv_write($socket, $data, function($client, $stat) {
uv_close($client); uv_close($client);
}); });
} else { } else {
@ -38,4 +38,4 @@ uv_listen($tcp,100, function($server) use ($users){
}); });
}); });
uv_run(); uv_run();

View File

@ -1,7 +1,7 @@
<?php <?php
uv_fs_open(uv_default_loop(),__FILE__, UV::O_RDONLY, 0, function($r){ uv_fs_open(uv_default_loop(),__FILE__, UV::O_RDONLY, 0, function($r){
uv_fs_read(uv_default_loop(),$r,function($stream, $nread, $data) { uv_fs_read(uv_default_loop(),$r,function($stream, $data) {
if ($nread <= 0) { if (is_long($data)) {
if ($nread < 0) { if ($nread < 0) {
throw new Exception("read error"); throw new Exception("read error");
} }

View File

@ -1,11 +1,11 @@
<?php <?php
$poll = uv_fs_poll_init(uv_default_loop()); $poll = uv_fs_poll_init(uv_default_loop());
uv_fs_poll_start($poll,function($rsc,$stat,$p){ uv_fs_poll_start($poll,function($rsc,$stat,$p) {
var_dump(1); var_dump(1);
var_dump($p); var_dump($p);
//uv_fs_poll_stop($rsc); // uv_fs_poll_stop($rsc);
}, "/target/directory", 1);
}, "/tmp/hoge", 1);
uv_run(); uv_run();

View File

@ -2,12 +2,11 @@
uv_fs_open(uv_default_loop(),"./tmp", UV::O_WRONLY, uv_fs_open(uv_default_loop(),"./tmp", UV::O_WRONLY,
UV::S_IRWXU | UV::S_IRUSR, UV::S_IRWXU | UV::S_IRUSR,
function($r){ function($fd) {
var_dump($r); var_dump($fd);
uv_fs_ftruncate(uv_default_loop(),$r,0, function() use ($r){ uv_fs_ftruncate(uv_default_loop(), $fd, 0, function($fd) {
uv_fs_close(uv_default_loop(),$r,function(){}); uv_fs_close(uv_default_loop(), $fd, function(){});
}); });
}); });
uv_run(); uv_run();

View File

@ -1,8 +1,8 @@
<?php <?php
$fsevent = uv_fs_event_init(uv_default_loop(),"/tmp/",function($rsc,$name,$event,$stat){ $fsevent = uv_fs_event_init(uv_default_loop(), "/tmp/", function($rsc, $name, $event, $status) {
var_dump($name, $event); var_dump($name, $event);
print PHP_EOL; print PHP_EOL;
},0); }, 0);
uv_run(); uv_run();

View File

@ -1,8 +1,8 @@
<?php <?php
uv_fs_open(uv_default_loop(), __FILE__, UV::O_RDONLY, 0, function($r){ uv_fs_open(uv_default_loop(), __FILE__, UV::O_RDONLY, 0, function($file) {
uv_fs_fstat(uv_default_loop(), $r, function($result, $da){ uv_fs_fstat(uv_default_loop(), $file, function($file, $stat) {
var_dump($da); var_dump($stat);
}); });
}); });

View File

@ -1,17 +1,18 @@
<?php <?php
uv_fs_open(uv_default_loop(),"./tmp", UV::O_WRONLY | UV::O_CREAT | UV::O_APPEND, uv_fs_open(uv_default_loop(), "./tmp", UV::O_WRONLY | UV::O_CREAT | UV::O_APPEND,
UV::S_IRWXU | UV::S_IRUSR, UV::S_IRWXU | UV::S_IRUSR,
function($r){ function($fd) {
var_dump($r); var_dump($fd);
uv_fs_write(uv_default_loop(),$r,"hello",0, function($a) use ($r){ uv_fs_write(uv_default_loop(), $fd, "hello", 0, function($fd, $result) {
var_dump($a); var_dump($result);
var_dump("ok"); var_dump("ok");
uv_fs_fdatasync(uv_default_loop(),$r,function(){ uv_fs_fdatasync(uv_default_loop(), $fd, function(){
echo "fsync finished"; echo "fsync finished";
});
}); });
}); }
}); );
uv_run(); uv_run();

View File

@ -1,7 +1,7 @@
<?php <?php
uv_getaddrinfo(uv_default_loop(),function($s,$names){ uv_getaddrinfo(uv_default_loop(),function($s,$names){
var_dump($names); var_dump($names);
},"yahoo.com", NULL ,array( }, "yahoo.com", NULL ,array(
"ai_family" => UV::AF_UNSPEC "ai_family" => UV::AF_UNSPEC
)); ));

View File

@ -1,14 +0,0 @@
<?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();

View File

@ -3,7 +3,7 @@ $loop = uv_default_loop();
$idle = uv_idle_init(); $idle = uv_idle_init();
$i = 0; $i = 0;
uv_idle_start($idle, function($stat) use (&$i, $idle, $loop){ uv_idle_start($idle, function($stat) use (&$i, $idle, $loop) {
echo "count: {$i}" . PHP_EOL; echo "count: {$i}" . PHP_EOL;
$i++; $i++;

View File

@ -1,7 +1,7 @@
<?php <?php
uv_fs_lstat(uv_default_loop(), __FILE__, function($result, $da){ uv_fs_lstat(uv_default_loop(), __FILE__, function($stat) {
var_dump($da); var_dump($stat);
}); });
uv_run(); uv_run();

View File

@ -1,6 +1,6 @@
<?php <?php
uv_fs_mkdir(uv_default_loop(), "hoge", 0644, function($result){ uv_fs_mkdir(uv_default_loop(), "hoge", 0644, function($result) {
var_dump($result); var_dump($result);
}); });

View File

@ -4,9 +4,9 @@ $pipe = uv_pipe_init(uv_default_loop(), 0);
uv_pipe_open($pipe, 1); uv_pipe_open($pipe, 1);
//uv_pipe_bind($pipe,"/tmp/hoge.sock"); //uv_pipe_bind($pipe,"/tmp/hoge.sock");
uv_write($pipe, "Hello",function($b,$s){ uv_write($pipe, "Hello", function($pipe, $buf) {
echo 1; echo 1;
uv_close($b); uv_close($pipe);
}); });
uv_run(); uv_run();

View File

@ -1,13 +1,17 @@
<?php <?php
define("PIPE_PATH", dirname(__FILE__) . "/pipe_test.sock"); if (stripos(PHP_OS, "WIN") == 0) {
@unlink(PIPE_PATH); define("PIPE_PATH", "\\\\.\\pipe\\MyPipeName");
} else {
define("PIPE_PATH", dirname(__FILE__) . "/pipe_test.sock");
@unlink(PIPE_PATH);
}
$a = uv_pipe_init(uv_default_loop(), 0); $a = uv_pipe_init(uv_default_loop(), 0);
$ret = uv_pipe_bind($a,PIPE_PATH); $ret = uv_pipe_bind($a,PIPE_PATH);
uv_listen($a, 8192, function($stream){ uv_listen($a, 8192, function($stream) {
$pipe = uv_pipe_init(uv_default_loop(),0); $pipe = uv_pipe_init(uv_default_loop(),0);
uv_accept($stream,$pipe); uv_accept($stream,$pipe);
uv_read_start($pipe,function($pipe, $nread, $buffer) use ($stream){ uv_read_start($pipe,function($pipe, $buffer) use ($stream) {
echo $buffer; echo $buffer;
uv_read_stop($pipe); uv_read_stop($pipe);
uv_close($stream, function(){ uv_close($stream, function(){
@ -17,8 +21,8 @@ uv_listen($a, 8192, function($stream){
}); });
$b = uv_pipe_init(uv_default_loop(), 0); $b = uv_pipe_init(uv_default_loop(), 0);
uv_pipe_connect($b, PIPE_PATH, function($a,$b){ uv_pipe_connect($b, PIPE_PATH, function($pipe, $status) {
uv_write($b,"Hello", function($stream,$stat){ uv_write($pipe, "Hello", function($stream, $status) {
uv_close($stream); uv_close($stream);
}); });
}); });

View File

@ -5,23 +5,23 @@ stream_set_blocking($socket, 0);
$poll = uv_poll_init(uv_default_loop(), $socket); $poll = uv_poll_init(uv_default_loop(), $socket);
uv_poll_start($poll, UV::READABLE, function($poll, $stat, $ev, $socket){ uv_poll_start($poll, UV::READABLE, function($poll, $stat, $ev, $socket) {
echo "parent poll:\n"; echo "parent poll:\n";
var_dump($stat); var_dump($stat);
$conn = stream_socket_accept($socket); $conn = stream_socket_accept($socket);
$pp = uv_poll_init(uv_default_loop(), $conn); $pp = uv_poll_init(uv_default_loop(), $conn);
uv_poll_start($pp, UV::WRITABLE, function($poll, $stat, $ev, $conn){ uv_poll_start($pp, UV::WRITABLE, function($poll, $stat, $ev, $conn) {
echo " cb"; echo " cb";
var_dump($stat); var_dump($stat);
var_dump($conn); var_dump($conn);
uv_poll_stop($poll); uv_poll_stop($poll);
uv_fs_write(uv_default_loop(), $conn, "Hello World", -1, function($conn, $nwrite){ uv_fs_write(uv_default_loop(), $conn, "Hello World", -1, function($conn, $nwrite) {
var_dump($conn); var_dump($conn);
fclose($conn); fclose($conn);
}); });
}); });
}); });
uv_run(); uv_run();

View File

@ -2,9 +2,9 @@
$loop = uv_default_loop(); $loop = uv_default_loop();
$prepare = uv_prepare_init($loop); $prepare = uv_prepare_init($loop);
uv_prepare_start($prepare, function($rsc, $status){ uv_prepare_start($prepare, function($prepare) {
echo "Hello"; echo "Hello";
uv_unref($rsc); uv_unref($prepare);
}); });
uv_run(); uv_run();

View File

@ -1,7 +1,7 @@
<?php <?php
uv_fs_readdir(uv_default_loop(), ".", 0, function($result, $da){ uv_fs_scandir(uv_default_loop(), ".", function($contents) {
var_dump($da); var_dump($contents);
}); });
uv_run(); uv_run();

View File

@ -1,7 +1,6 @@
<?php <?php
uv_fs_readlink(uv_default_loop(), "li", function($result,$buffer){ uv_fs_readlink(uv_default_loop(), "linkPath", function($buffer){
var_dump($result);
var_dump($buffer); var_dump($buffer);
}); });

View File

@ -1,6 +1,6 @@
<?php <?php
uv_fs_rename(uv_default_loop(), "moe", "moe2", function($result){ uv_fs_rename(uv_default_loop(), "moe", "moe2", function($result) {
var_dump($result); var_dump($result);
}); });

View File

@ -1,48 +0,0 @@
<?php
$url = "http://yahoo.co.jp/";
$parts = parse_url($url);
$domain = $parts['host'];
$path = $parts['path'] . '?' . $parts['query'];
$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();

View File

@ -1,6 +1,6 @@
<?php <?php
uv_fs_rmdir(uv_default_loop(), "hoge", function($result){ uv_fs_rmdir(uv_default_loop(), "hoge", function($result) {
var_dump($result); var_dump($result);
}); });

View File

@ -1,9 +1,7 @@
<?php <?php
uv_fs_open(uv_default_loop(),__FILE__,UV::O_RDONLY,0, function($read_fd){ 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_fs_sendfile(uv_default_loop(),$stdout,$read_fd,0,6, function($result){
});
}); });
uv_run(); uv_run();

View File

@ -17,7 +17,7 @@ uv_spawn(uv_default_loop(), "php", array('-r','var_dump($_ENV);'), $stdio, "/usr
}, $flags); }, $flags);
uv_read2_start($out, function($out, $nread, $buffer, $stat){ uv_read_start($out, function($out, $buffer){
echo $buffer; echo $buffer;
uv_close($out,function(){}); uv_close($out,function(){});

View File

@ -11,12 +11,11 @@ $fp = fopen("php://stdout", "w");
$stdio[] = uv_stdio_new($fp, UV::INHERIT_FD | UV::WRITABLE_PIPE); $stdio[] = uv_stdio_new($fp, UV::INHERIT_FD | UV::WRITABLE_PIPE);
$flags = 0; $flags = 0;
uv_spawn(uv_default_loop(), "php", array('-r','var_dump($_ENV);'), $stdio, "/usr/bin/", uv_spawn(uv_default_loop(), "php", ['-r', 'var_dump($_ENV);'], $stdio, "/usr/bin/",
array("key"=>"hello"), ["key" => "hello"],
function($process, $stat, $signal){ function($process, $stat, $signal){
uv_close($process,function(){}); uv_close($process,function(){});
}, $flags); }, $flags);
uv_run();
uv_run();

View File

@ -1,7 +1,7 @@
<?php <?php
uv_fs_stat(uv_default_loop(), __FILE__, function($result, $da){ uv_fs_stat(uv_default_loop(), __FILE__, function($stat) {
var_dump($da); var_dump($stat);
}); });
uv_run(); uv_run();

View File

@ -3,12 +3,12 @@ $tcp = uv_tcp_init();
uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0',9999)); uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0',9999));
uv_listen($tcp,100, function($server){ uv_listen($tcp, 100, function($server){
$client = uv_tcp_init(); $client = uv_tcp_init();
uv_accept($server, $client); uv_accept($server, $client);
var_dump(uv_tcp_getsockname($server)); var_dump(uv_tcp_getsockname($server));
uv_read_start($client, function($socket, $nread, $buffer) use ($server){ uv_read_start($client, function($socket, $buffer) use ($server) {
var_dump($buffer); var_dump($buffer);
uv_close($socket); uv_close($socket);
uv_close($server); uv_close($server);
@ -16,9 +16,9 @@ uv_listen($tcp,100, function($server){
}); });
$c = uv_tcp_init(); $c = uv_tcp_init();
uv_tcp_connect($c, uv_ip4_addr('0.0.0.0',9999), function($client, $stat){ uv_tcp_connect($c, uv_ip4_addr('0.0.0.0',9999), function($client, $status) {
if ($stat == 0) { if ($status == 0) {
uv_write($client,"Hello",function($socket, $stat){ uv_write($client, "Hello", function($socket, $status) {
uv_close($socket); uv_close($socket);
}); });
} }

View File

@ -16,7 +16,7 @@ uv_listen($tcp,100, function($server){
}); });
$c = uv_tcp_init(); $c = uv_tcp_init();
uv_tcp_connect6($c, uv_ip6_addr('::1',9999), function($client, $stat){ uv_tcp_connect($c, uv_ip6_addr('::1',9999), function($client, $stat){
if ($stat == 0) { if ($stat == 0) {
uv_write($client,"Hello",function($socket, $stat){ uv_write($client,"Hello",function($socket, $stat){
uv_close($socket); uv_close($socket);

View File

@ -3,7 +3,7 @@ $loop = uv_default_loop();
$timer = uv_timer_init(); $timer = uv_timer_init();
$i = 0; $i = 0;
uv_timer_start($timer, 1000, 1000, function($stat) use (&$i, $timer, $loop){ uv_timer_start($timer, 1000, 1000, function($stat) use (&$i, $timer, $loop) {
echo "count: {$i}" . PHP_EOL; echo "count: {$i}" . PHP_EOL;
$i++; $i++;
@ -15,4 +15,4 @@ uv_timer_start($timer, 1000, 1000, function($stat) use (&$i, $timer, $loop){
uv_run(); uv_run();
echo "finished"; echo "finished\n";

View File

@ -1,7 +1,7 @@
<?php <?php
uv_fs_open(uv_default_loop(), "/dev/tty", UV::O_RDONLY, 0, function($r){ uv_fs_open(uv_default_loop(), "/dev/tty", UV::O_RDONLY, 0, function($fd) {
$tty = uv_tty_init(uv_default_loop(), $r, 1); $tty = uv_tty_init(uv_default_loop(), $fd, 1);
var_dump(uv_tty_get_winsize($tty, $width, $height)); var_dump(uv_tty_get_winsize($tty, $width, $height));
var_dump($width, $height); var_dump($width, $height);
}); });

View File

@ -2,15 +2,15 @@
$udp = uv_udp_init(); $udp = uv_udp_init();
var_dump($udp); var_dump($udp);
uv_udp_bind($udp, uv_ip4_addr('0.0.0.0',10000)); uv_udp_bind($udp, uv_ip4_addr('0.0.0.0', 10000));
uv_udp_recv_start($udp,function($stream, $nread, $buffer){ uv_udp_recv_start($udp,function($stream, $buffer){
echo "recv:" . $buffer; echo "recv:" . $buffer;
uv_close($stream); uv_close($stream);
}); });
$uv = uv_udp_init(); $uv = uv_udp_init();
uv_udp_send($uv, "Hello", uv_ip4_addr("0.0.0.0",10000),function($uv, $s){ uv_udp_send($uv, "Hello", uv_ip4_addr("0.0.0.0", 10000), function($uv, $status) {
echo "success" . PHP_EOL; echo "success" . PHP_EOL;
uv_close($uv); uv_close($uv);
}); });

View File

@ -1,6 +1,6 @@
<?php <?php
uv_fs_unlink(uv_default_loop(), "moe", function($result){ uv_fs_unlink(uv_default_loop(), "moe", function($result) {
var_dump($result); var_dump($result);
}); });

View File

@ -1,8 +1,7 @@
<?php <?php
uv_fs_utime(uv_default_loop(), __FILE__, time(), time(), function() {
uv_fs_utime(uv_default_loop(),__FILE__, time(), time(),function(){ echo "Finished\n";
echo "Finished";
}); });
uv_run(); uv_run();