Interface to libuv for php.
git clone https://github.com/bwoebi/php-uv.git
cd php-uv
phpize
./configure
make
make install
# add `extension=uv.so` to your php.ini
Windows builds for stable PHP versions are available from PECL.
see examples and tests directory.
<?php
$tcp = uv_tcp_init();
uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0',8888));
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){
var_dump($buffer);
uv_close($socket);
});
});
$c = uv_tcp_init();
uv_tcp_connect($c, uv_ip4_addr('0.0.0.0',8888), function($stream, $stat){
if ($stat == 0) {
uv_write($stream,"Hello",function($stream, $stat){
uv_close($stream);
});
}
});
uv_run();
- Shuhei Tanuma
- Bob Weinand
PHP License
decrement reference
resource $uv_t: uv resource handle.
*void *:
<?php
$tcp = uv_tcp_init();
uv_unref($tcp);
get last error code.
resource $uv_loop: uv loop handle
long $error_code: error code
<?php
$err = uv_last_error();
var_dump($err);
get error code name.
long $error_code: libuv error code
string $erorr_name: error name
<?php
$err = uv_last_error();
var_dump(uv_err_name($err));
get error message.
long $error_code: libuv error code
string $erorr_message: error message
<?php
$err = uv_last_error();
var_dump(uv_strerror($err));
increment reference count
resource $uv_handle: uv resource.
void:
<?php
$tcp = uv_tcp_init();
uv_ref($tcp);
- support uv_loop resource
run event loop
resource $uv_loopg: uv_loop resource
void:
<?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();
delete specified loop resource.
resource $uv_loop: uv_loop resource
void:
binds a name to a socket.
resource $uv_tcp: uv_tcp resource
resource $uv_sockaddr: uv sockaddr4 resource.
void:
<?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);
});
});
binds a name to a socket.
resource $uv_tcp: uv_tcp resource
resource $uv_sockaddr: uv sockaddr6 resource.
void:
<?php
$tcp = uv_tcp_init();
uv_tcp_bind6($tcp, uv_ip6_addr('::1',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);
});
});
send buffer to speicified uv resource.
resource $handle: uv resources (uv_tcp, uv_udp, uv_pipe ...etc.) string $data: buffer. callable $callback: callable variables. this callback expects (resource $handle, long $status)
void:
set Nagel's flags for specified tcp resource.
resource $handle: libuv tcp resource
bool $enable: true means enabled. false means disabled.
void:
<?php
$tcp = uv_tcp_init();
uv_tcp_nodelay($tcp, true);
accepts a connection on a socket.
resource $uv: uv_tcp or uv_pipe server resource
resource $uv: uv_tcp or uv_pipe client resource.
void:
<?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);
});
});
shutdown uv handle.
resource $handle: uv resources (uv_tcp, uv_udp, uv_pipe ...etc.) callable $callback: callable variables. this callback expects (resource $handle, long $status)
void:
close uv handle.
resource $handle: uv resources (uv_tcp, uv_udp, uv_pipe ...etc.) callable $callback: callable variables. this callback expects (resource $handle, long $status)
void:
starts read callback for uv resources.
resource $handle: uv resources (uv_tcp, uv_udp, uv_pipe ...etc.)
callable $callback: callable variables. this callback parameter expects (resource $handle, long $nread, string buffer)
void:
- You have to handle erorrs correctly. otherwise this will leak.
- if you want to use PHP's stream or socket resource. see uv_fs_poll_init and uv_fs_read.
stop read callback
resource $uv: uv resource handle which started uv_read.
void:
<?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){
uv_read_stop($socket);
var_dump($buffer);
uv_close($socket);
uv_close($server);
});
});
create a ipv4 sockaddr.
string $ipv4_addr: ipv4 address
long $port: port number.
resource $uv_sockaddr: sockaddr resource
<?php
$sockaddr = uv_ip4_addr("127.0.0.1", 8080);
- check passed ip address is valid.
- check port number is valid
create a ipv6 sockaddr.
string $ipv6_addr: ipv6 address
long $port: port number.
resource $uv_sockaddr: sockaddr resource
<?php
$sockaddr = uv_ip6_addr("::1", 8080);
- check passed ip address is valid.
- check port number is valid
listens for a connection on a uv handle.
resource $handle: uv resource handle (tcp, udp and pipe)
long $backlog: backlog
callable $callback: this callback parameter expects (resource $connection, long $status)
*void *:
<?php
$tcp = uv_tcp_init();
uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0',9999));
uv_listen($tcp,100, function($server, $status){
$client = uv_tcp_init();
uv_accept($server, $client);
uv_read_start($client, function($socket, $nread, $buffer) use ($server){
var_dump($buffer);
uv_close($socket);
uv_close($server);
});
});
uv_run();
connect to specified ip address and port.
resource $handle: requires uv_tcp_init()
resource.
resource $ipv4_addr: requires uv_sockaddr resource.
callable $callback: callable variables.
void:
<?php
$tcp = uv_tcp_init();
uv_tcp_connect($tcp, uv_ip4_addr("127.0.0.1",8080), function($tcp_handle, $status){
uv_close($tcp_handle);
});
uv_run();
connect to specified ip address and port.
resource $handle: requires uv_tcp_init()
resource.
resource $ipv4_addr: requires uv_sockaddr resource.
callable $callback: callable variables.
void:
<?php
$tcp = uv_tcp_init();
uv_tcp_connect($tcp, uv_ip6_addr("::1",8080), function($tcp_handle, $status){
uv_close($tcp_handle);
});
uv_run();
initialize timer handle.
resource $loop: uv_loop resource.
resource $timer: initialized timer resource.
<?php
$timer = uv_timer_init();
initialize timer handle.
resource $loop: uv_loop resource.
long $timeout: periodical event starts when after this timeout. 1000 is 1 sec.
long $repeat: repeat interval. 1000 is 1 sec.
*void:
<?php
$timer = uv_timer_init();
$after_1_second = 1000;
$period_is_1_second = 1000;
uv_timer_start($timer, $after_1_seconds, $period_is_1_second, function($timer, $status){
echo "Hello\n";
});
uv_run();
stop specified timer.
resource $timer: uv timer resource.
long $retval:
<?php
$timer = uv_timer_init();
uv_timer_start($timer, 100, 100, function($timer, $status){
echo "Hello\n";
uv_timer_stop($timer);
});
uv_run();
restart timer.
resource $timer: uv_timer resource.
void:
set repeat count.
resource $uv_timer: uv_timer resource
long $repeat: repeat count
void:
returns repeat interval.
resource $uv_timer: uv_timer resource
long $repeat_time:
initialize uv idle handle.
resource $loop: uv_loop resource.
resource $idle: initialized idle handle.
<?php
$loop = uv_default_loop();
$idle = uv_idle_init($loop);
start idle callback.
resource $idle: uv_idle resource. callable $callback: idle callback.
long result:
<?php
$loop = uv_default_loop();
$idle = uv_idle_init();
$i = 0;
uv_idle_start($idle, function($idle_handle, $stat) use (&$i){
echo "count: {$i}" . PHP_EOL;
$i++;
if ($i > 3) {
uv_idle_stop($idle);
}
sleep(1);
});
uv_run();
stop idle callback.
resource $idle: uv_idle resource.
long result:
<?php
$loop = uv_default_loop();
$idle = uv_idle_init();
$i = 0;
uv_idle_start($idle, function($idle_handle, $stat) use (&$i){
echo "count: {$i}" . PHP_EOL;
$i++;
if ($i > 3) {
uv_idle_stop($idle);
}
sleep(1);
});
uv_run();
void uv_getaddrinfo(resource $loop, callable $callback, string $node, string $service, array $hints)
create a tcp socket.
resource $loop: loop resource or null. if not specified loop resource then use uv_default_loop resource.
resource php_uv: uv resource which initialized for tcp.
<?php
$tcp = uv_tcp_init();
return default loop handle.
resource $loop:
<?php
$loop = uv_default_loop();
create a new loop handle.
resource $loop:
<?php
$loop = uv_loop_new();
create a udp socket.
resource $loop: loop resource or null. if not specified loop resource then use uv_default_loop resource.
resource php_uv: uv resource which initialized for udp.
<?php
$udp = uv_udp_init();
listens for a connection on a uv udp handle.
resource $handle: uv resource handle (udp)
resource $uv_ip_addr: uv sockaddr(ipv4) resource.
long $flags: unused.
*void *:
<?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();
listens for a connection on a uv udp handle.
resource $handle: uv resource handle (udp)
resource $uv_ip_addr: uv sockaddr(ipv6) resource.
long $flags: Should be 0 or UV::UDP_IPV6ONLY
*void *:
<?php
$udp = uv_udp_init();
var_dump($udp);
uv_udp_bind6($udp, uv_ip6_addr('::1',10000));
uv_udp_recv_start($udp,function($stream, $nread, $buffer){
echo "recv:" . $buffer;
uv_close($stream);
});
$uv = uv_udp_init();
uv_udp_send6($uv, "Hello", uv_ip6_addr("::1",10000),function($uv, $s){
echo "success" . PHP_EOL;
uv_close($uv);
});
uv_run();
start receive callback.
resource $handle: uv resource handle (udp)
callable $callback: this callback parameter expects (resource $stream, long $nread, string $buffer).
*void *:
<?php
$udp = uv_udp_init();
var_dump($udp);
uv_udp_bind6($udp, uv_ip6_addr('::1',10000));
uv_udp_recv_start($udp,function($stream, $nread, $buffer){
echo "recv:" . $buffer;
uv_close($stream);
});
$uv = uv_udp_init();
uv_udp_send6($uv, "Hello", uv_ip6_addr("::1",10000),function($uv, $s){
echo "success" . PHP_EOL;
uv_close($uv);
});
uv_run();
stop receive callback.
resource $handle: uv resource handle (udp)
*void *:
long uv_udp_set_membership(resource $handle, string $multicast_addr, string $interface_addr, long $membership)
join or leave udp muticast group..
resource $handle: uv resource handle (udp)
string $multicast_addr: multicast address
string $interface_addr: interface address
long $membership: UV::JOIN_GROUP or UV::LEAVE_GROUP
*long *: result code
set multicast loop
resource $handle: uv resource handle (udp)
long $enabled:
void:
set multicast ttl
resource $handle: uv resource handle (udp)
long $ttl: multicast ttl
void:
set udp broadcast
resource $handle: uv resource handle (udp)
long $enabled:
void:
send buffer to specified address.
resource $handle: uv resource handle (udp)
string $data: data
resource uv_addr: uv_ip4_addr
callable $callback: this callback parameter expects (resource $stream, long $status).
*void *:
<?php
$udp = uv_udp_init();
var_dump($udp);
uv_udp_bind($udp, uv_ip4_addr('::1',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("::1",10000),function($uv, $s){
echo "success" . PHP_EOL;
uv_close($uv);
});
uv_run();
send buffer to specified address.
resource $handle: uv resource handle (udp)
string $data: data
resource uv_addr: uv_ip6_addr
callable $callback: this callback parameter expects (resource $stream, long $status).
*void *:
<?php
$udp = uv_udp_init();
var_dump($udp);
uv_udp_bind6($udp, uv_ip6_addr('::1',10000));
uv_udp_recv_start($udp,function($stream, $nread, $buffer){
echo "recv:" . $buffer;
uv_close($stream);
});
$uv = uv_udp_init();
uv_udp_send6($uv, "Hello", uv_ip6_addr("::1",10000),function($uv, $s){
echo "success" . PHP_EOL;
uv_close($uv);
});
uv_run();
- implement this.
returns current uv type. (this is not libuv function. util for php-uv)
resource $uv_handle: uv_handle
long $handle_type: should return UV::IS_UV_* constatns. e.g) UV::IS_UV_TCP
<?php
$tcp = uv_tcp_init();
var_dump(uv_handle_type($tcp));
- this may change.
initialize pipe resource
resource $uv_loop: uv_loop resource
bool $ipc: when this pipe use for ipc, please set true otherwise false.
resource $uv_pipe:
<?php
$pipe = uv_pipe_init(uv_default_loop(), true);
open a pipe resource.
resource $uv_handle: uv pipe handle
*long $pipe: dunnno. maybe file descriptor.
void:
create a named pipe.
resource $uv_handle: uv pipe handle
*long $pipe: dunnno. maybe file descriptor.
void:
connect to named pipe.
resource $uv_handle: uv pipe handle
*string $path: named pipe path
*callable $callback: this callback parameter expects (resource $pipe, long $status)
void:
<?php
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();
retunrs current loadaverage.
array $loadaverage:
<?php
var_dump(uv_loadavg());
//array(3) {
// [0]=>
// float(1.7421875)
// [1]=>
// float(1.427734375)
// [2]=>
// float(1.3955078125)
//}
returns array on windows box. (does not support load average on windows)
returns current uptime.
long $uptime:
<?php
var_dump(uv_uptime());
//float(1247516)
returns current free memory size.
long $free:
<?php
var_dump(uv_get_free_memory());
//int(135860224)
returns total memory size.
long $free:
<?php
var_dump(uv_get_total_memory());
//int(8589934592)
check implmentation
returns current exepath. basically this will returns current php path.
string $exepath:
<?php
var_dump(uv_exepath());
//string(53) "/Users/chobie/.phpenv/versions/5.4.1-zts-goto/bin/php"
```
### string uv_cwd(void)
##### *Description*
returns current working directory.
##### *Parameters*
##### *Return Value*
*string $cwd*:
##### *Example*
````php
<?php
var_dump(uv_cwd());
//string(24) "/Users/chobie/src/php-uv"
returns current cpu informations
.
array $cpu_info:
<?php
var_dump(uv_cpu_info());
//array(8) {
// [0]=>
// array(3) {
// ["model"]=>
// string(13) "MacBookPro8,2"
// ["speed"]=>
// int(2200)
// ["times"]=>
// array(5) {
// ["sys"]=>
// int(69952140)
// ["user"]=>
// int(38153450)
// ["idle"]=>
// int(776709120)
// ["irq"]=>
// int(0)
// ["nice"]=>
// int(0)
// }
// }...
resource uv_spawn(resource $loop, string $command, array $args, array $stdio, string $cwd, array $env = array(), callable $callback [,long $flags, array $options])
send signal to specified uv process resource.
resource $handle: uv resource handle (process)
long $signal:
void:
send signal to specified pid.
long $pid: process id
long $signal:
void:
change working directory.
string $directory:
*bool *:
initialize rwlock resource
resource $rwlock: returns uv rwlock resource
set read lock
resource $handle: uv resource handle (uv rwlock)
*void *:
- implemnt this correctly
unlock read lock
resource $handle: uv resource handle (uv rwlock)
void:
set write lock
resource $handle: uv resource handle (uv rwlock)
*void *:
- implement this correctly
unlock write lock
resource $handle: uv resource handle (uv rwlock)
void:
initialize mutex resource
resource $uv_mutex: uv mutex resource
lock mutex
resource $handle: uv resource handle (uv mutex)
void:
- implement this correctly
initialize semaphore resource
resource $uv_sem:
post semaphore
resource $handle: uv resource handle (uv sem)
void:
- implemnt this correctly
- implment this correctly
initialize prepare resource
resource $loop: uv loop handle
resource $uv_prepare:
<?php
$prepare = uv_prepare_init(uv_default_loop());
setup prepare loop callback. (pre loop callback)
resource $handle: uv resource handle (prepare)
callable $callback: this callback parameter expects (resource $prepare, long $status).
*long *:
<?php
$loop = uv_default_loop();
$prepare = uv_prepare_init($loop);
uv_prepare_start($prepare, function($rsc, $status){
echo "Hello";
uv_unref($rsc);
});
uv_run();
stop prepare callback
resource $prepare: uv resource handle (prepare)
long:
setup check resource
resource $loop: uv loop handle
resource uv_check:
<?php
$check = uv_check_init(uv_default_loop());
stats check loop callback. (after loop callback)
resource $handle: uv resource handle (check)
callable $callback: this callback parameter expects (resource $check, long $status).
*long *:
<?php
$loop = uv_default_loop();
$check = uv_check_init($loop);
$idle = uv_idle_init();
$i = 0;
uv_idle_start($idle, function($stat) use (&$i, $idle, $loop){
echo "count: {$i}" . PHP_EOL;
$i++;
if ($i > 3) {
uv_idle_stop($idle);
}
sleep(1);
});
uv_check_start($check, function($check, $status){
echo "Hello";
uv_check_stop($check);
});
uv_run();
stop check callback
resource $check: uv resource handle (check)
*void *:
setup async callback
resource $loop: uv loop resource
callback $callback:
*resource *: uv async resource
send async callback immidiately
resource $handle: uv async handle
void:
execute callbacks in another thread (requires Thread Safe enabled PHP)
open specified file
resource $loop: uv_loop resource.
string $path: file path
long $flag: file flag. this should be UV::O_RDONLY and some constants flag.
long $mode: mode flag. this should be UV::S_IRWXU and some mode flag.
callable $calback: this callback parameter expects (resource $stream)
void:
<?php
uv_fs_open(uv_default_loop(),"/tmp/hello",
UV::O_WRONLY | UV::O_CREAT | UV::O_APPEND,
UV::S_IRWXU | UV::S_IRUSR,
function($r){
uv_fs_write(uv_default_loop(),$r,"hello",0, function($a) use ($r){
uv_fs_fdatasync(uv_default_loop(),$r,function(){
echo "fsync finished";
});
});
});
uv_run();
async read.
resource $loop: uv loop handle
zval $fd: this expects long $fd, resource $php_stream or resource $php_socket.
long $offset: the offset position in the file at which reading should commence.
long $length: the length in bytes that should be read starting at position $offset.
resource $callback: this callback parameter expects (zval $fd, long $nread, string $buffer).
*void *:
close specified file descriptor.
resource $loop: uv_loop resource.
zval $fd: file descriptor. this expects long $fd, resource $php_stream or resource $php_socket.
callable $calback: this callback parameter expects (resource $stream)
void:
- handling PHP's stream and socket correctly.
write buffer to specified file descriptor.
resource $loop: uv_loop resource.
zval $fd: file descriptor. this expects long $fd, resource $php_stream or resource $php_socket.
string $buffer: buffer.
callable $calback: this callback parameter expects (resource $stream, long $result)
void:
async fsync
resource $handle: uv loop handle
zval $fd:
callable $callback:
void:
async fdatasync
resource $handle: uv loop handle
zval $fd:
callable $callback:
void:
async ftruncate
resource $handle: uv loop handle
zval $fd:
long $offset:
callable $callback:
void:
async mkdir
resource $handle: uv loop handle
string $path:
long $mode:
callable $callback:
void:
async rmdir
resource $handle: uv loop handle
string $path:
callable $callback:
void:
async unlink
resource $handle: uv loop handle
string $path:
callable $callback:
void:
async rename
resource $handle: uv loop handle
string $from:
string $to:
callable $callback:
void:
async utime
resource $handle: uv loop handle
string $path:
long $utime:
long $atime:
callable $callback:
void:
async futime
resource $handle: uv loop handle
zval $fd:
long $utime:
long $atime:
callable $callback:
void:
async chmod
resource $handle: uv loop handle
string $path:
long $mode:
callable $callback:
void:
async fchmod
resource $handle: uv loop handle
zval $fd:
long $mode:
callable $callback:
void:
async chown
resource $handle: uv loop handle
string $paht:
long $uid:
long $gid:
callable $callback:
void:
async fchown
resource $handle: uv loop handle
zval $fd:
long $uid:
long $gid:
callable $callback:
void:
async link
resource $handle: uv loop handle
string $from:
string $to:
callable $callback:
void:
async symlink
resource $handle: uv loop handle
string $from:
string $to:
long $flags:
callable $callback:
void:
async readlink
resource $handle: uv loop handle
string $path:
callable $callback:
void:
async stat
resource $handle: uv loop handle
string $path:
callable $callback: this callback parameter expects (resource $stream, array $stat)
void:
async lstat
resource $handle: uv loop handle
string $path:
callable $callback:
void:
async fstat
resource $handle: uv loop handle
zval $fd:
callable $callback:
void:
async readdir
resource $handle: uv loop handle
string $path:
long $flags:
callable $callback:
void:
void uv_fs_sendfile(resource $loop, zval $in_fd, zval $out_fd, long $offset, long $length, callable $callback)
async sendfile
resource $handle: uv loop handle
zval $in_fd:
zval $out_fd:
long $offset:
long $length:
callable $callback:
void:
initialize fs event.
resource $handle: uv loop handle
string $path:
callable $callback:
void:
initialize tty resource. you have to open tty your hand.
resource $handle: uv loop handle
zval $fd:
long $readable:
resource $uv_tty:
initialize poll
resource $uv_loop: uv_loop resource.
mixed $fd: this expects long fd, PHP's stream or PHP's socket resource.
resource uv: uv resource which initialized poll.
<?php
$fd = fopen("php://stdout","w+");
$poll = uv_poll_init(uv_default_loop(), $fd);
- some platform doesn't support file descriptor on these method.
start polling
resource $poll: uv poll resource.
long $events: UV::READBLE and UV::WRITABLE flags.
callable $callback: this callback parameter expects (resource $poll, long $status, long $events, mixed $connection). the connection parameter passes uv_poll_init'd fd.
void:
<?php
$fd = fopen("php://stdout","w+");
$poll = uv_poll_init(uv_default_loop(), $fd);
uv_poll_start($poll, UV::WRITABLE, function($poll, $stat, $ev, $conn){
fwrite($conn, "Hello");
fclose($conn);
uv_poll_stop($poll);
});
uv_run();
- if you want to use a socket. please use uv_poll_init_socket instead of this. Windows can't handle socket with this function.
resource $uv_loop: uv loop handle
void:
resource $uv_loop: uv loop handle
resource:
resource $sig_handle:
callable $callable: signal callback
int $sig_num: signal
void:
resource $sig_handle:
int $sig_num: