15 KiB
php-uv
interface to libuv for php (experimental). also supports http-parser.
Experimental
This extension is experimental, its functions may change their names or move to extension all together so do not rely to much on them you have been warned!
Install
git clone https://github.com/chobie/php-uv.git --recursive
cd php-uv
(cd libuv && make)
phpize
./configure
make
make install
# add `extension=uv.so` to your php.ini
Examples
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();
Community
Check out #php-uv on irc.freenode.net.
Author
- Shuhei Tanuma
License
PHP License
Documents
this documents was generated by examples/docgen.php
.
void uv_unref(resource $uv_t)
long uv_last_error([resource $uv_loop])
string uv_err_name(long $error_code)
string uv_strerror(long $error_code)
void uv_update_time(resource $uv_loop)
void uv_ref(resource $uv_handle)
void uv_run([resource $uv_loop])
void uv_run_once([resource $uv_loop])
void uv_loop_delete(resource $uv_loop)
long uv_now(resource $uv_loop)
void uv_tcp_bind(resource $uv_sockaddr)
void uv_tcp_bind6(resource $uv_sockaddr)
void uv_write(resource $handle, string $data, callable $callback)
Description
send buffer to speicified uv resource.
Parameters
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)
Return Value
void:
Example
void uv_write2(resource $handle, string $data, resource $send, callable $callback)
void uv_tcp_nodelay(resource $handle, bool $flag)
void uv_accept(resource $server, resource $client)
void uv_shutdown(resource $handle, callable $callback)
Description
shutdown uv handle.
Parameters
resource $handle: uv resources (uv_tcp, uv_udp, uv_pipe ...etc.) callable $callback: callable variables. this callback expects (resource $handle, long $status)
Return Value
void:
Example
void uv_close(resource $handle, callable $callback)
Description
close uv handle.
Parameters
resource $handle: uv resources (uv_tcp, uv_udp, uv_pipe ...etc.) callable $callback: callable variables. this callback expects (resource $handle, long $status)
Return Value
void:
Example
void uv_read_start(resource $handle, callable $callback)
Description
starts read callback for uv resources.
Parameters
resource $handle: uv resources (uv_tcp, uv_udp, uv_pipe ...etc.) callable $callback: callable variables. this callback expects (resource $handle, long $nread, string buffer)
Return Value
void:
Example
Note
You have to handle erorrs correctly. otherwise this will leak.
void uv_read2_start(resource $handle, callable $callback)
void uv_read_stop(resource $handle)
resource uv_ip4_addr(string $ipv4_addr, long $port)
resource uv_ip6_addr(string $ipv6_addr, long $port)
void uv_listen(resource $handle, long $backlog, callable $callback)
void uv_tcp_connect(resource $handle, resource $ipv4_addr, callable $callback)
Description
connect to specified ip address and port.
Parameters
resource $handle: requires uv_tcp_init()
resource.
resource $ipv4_addr: requires uv_sockaddr resource.
callable $callback: callable variables.
Return Value
void:
Example
<?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();
void uv_tcp_connect6(resource $handle, resource $ipv6_addr, callable $callback)
Description
connect to specified ip address and port.
Parameters
resource $handle: requires uv_tcp_init()
resource.
resource $ipv4_addr: requires uv_sockaddr resource.
callable $callback: callable variables.
Return Value
void:
Example
<?php
$tcp = uv_tcp_init();
uv_tcp_connect($tcp, uv_ip6_addr("::1",8080), function($tcp_handle, $status){
uv_close($tcp_handle);
});
uv_run();
resource uv_timer_init([resource $loop])
Description
initialize timer handle.
Parameters
resource $loop: uv_loop resource.
Return Value
resource $timer: initialized timer resource.
Example
<?php
$timer = uv_timer_init();
void uv_timer_start(resource $timer, long $timeout, long $repeat, callable $callback)
Description
initialize timer handle.
Parameters
resource $loop: uv_loop resource.
Return Value
resource $timer: initialized timer resource.
Example
<?php
$timer = uv_timer_init();
uv_timer_start($timer, 100, 100, function($timer, $status){
echo "Hello\n";
});
uv_run();
void uv_timer_stop(resource $timer)
Description
stop specified timer.
Parameters
resource $timer: uv timer resource.
Return Value
void:
Example
<?php
$timer = uv_timer_init();
uv_timer_start($timer, 100, 100, function($timer, $status){
echo "Hello\n";
uv_timer_stop($timer);
});
uv_run();
void uv_timer_again(resource $timer)
Description
restart timer.
Parameters
resource $timer: uv_timer resource.
Return Value
void:
Example
void uv_timer_set_repeat(resource $timer, long $repeat)
long uv_timer_get_repeat(resource $timer)
void uv_idle_start(resource $idle, callable $callback)
Description
start idle callback.
Parameters
resource $idle: uv_idle resource. callable $callback: idle callback.
Return Value
void:
Example
<?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)
void uv_idle_stop(resource $idle)
Description
stop idle callback.
Parameters
resource $idle: uv_idle resource.
Return Value
void:
Example
<?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();
resource uv_tcp_init([resource $loop])
Description
create a tcp socket.
Parameters
resource $loop: loop resource or null. if not specified loop resource then use uv_default_loop resource.
Return Value
resource php_uv: uv resource which initialized for tcp.
Example
<?php
$tcp = uv_tcp_init();
resource uv_idle_init([resource $loop])
Description
initialize uv idle handle.
Parameters
resource $loop: uv_loop resource.
Return Value
resource $idle: initialized idle handle.
Example
<?php
$loop = uv_default_loop();
$idle = uv_idle_init($loop);
resource uv_default_loop()
Description
return default loop handle.
Parameters
Return Value
resource $loop:
Example
<?php
$loop = uv_default_loop();
resource uv_loop_new()
Description
create a new loop handle.
Parameters
Return Value
resource $loop:
Example
<?php
$loop = uv_loop_new();
resource uv_udp_init([resource $loop])
Description
create a udp socket.
Parameters
resource $loop: loop resource or null. if not specified loop resource then use uv_default_loop resource.
Return Value
resource php_uv: uv resource which initialized for udp.
Example
<?php
$udp = uv_udp_init();
void uv_udp_bind(resource $resource, resource $address, long $flags)
void uv_udp_bind6(resource $resource, resource $address, long $flags)
void uv_udp_recv_start(resource $handle, callable $callback)
void uv_udp_recv_stop(resource $handle)
long uv_udp_set_membership(resource $handle, string $multicast_addr, string $interface_addr, long $membership)
void uv_udp_set_multicast_loop(resource $handle, long $enabled)
void uv_udp_set_multicast_ttl(resource $handle, long $ttl)
void uv_udp_set_broadcast(resource $handle, bool $enabled)
void uv_udp_send(resource $handle, string $data, resource $uv_addr, callable $callback)
void uv_udp_send6(resource $handle, string $data, resource $uv_addr6, callable $callback)
bool uv_is_active(resource $handle)
bool uv_is_readable(resource $handle)
bool uv_is_writable(resource $handle)
bool uv_walk(resource $loop, callable $closure[, array $opaque])
resource uv_pipe_init(resource $loop, long $ipc)
void uv_pipe_open(resource $handle, long $pipe)
long uv_pipe_bind(resource $handle, string $name)
void uv_pipe_connect(resource $handle, string $path, callable $callback)
void uv_pipe_pending_instances(resource $handle, long $count)
resource uv_ares_init_options(resource $loop, array $options, long $optmask)
void ares_gethostbyname(resource $handle, string $name, long $flag, callable $callback)
array uv_loadavg(void)
double uv_uptime(void)
long uv_get_free_memory(void)
long uv_get_total_memory(void)
long uv_hrtime(void)
string uv_exepath(void)
string uv_cwd(void)
array uv_cpu_info(void)
array uv_interface_addresses(void)
resource uv_spawn(resource $loop, string $command, array $args, array $context, callable $callback)
void uv_process_kill(resource $handle, long $signal)
TODO: