constants: add several error codes. and add simple(?) http server example

This commit is contained in:
Shuhei Tanuma 2012-07-23 22:50:06 +09:00
parent 9a55c5e270
commit 4bf5f591ed
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,68 @@
<?php
$address = "::1";
$port = 8888;
$banner = <<<EOF
# # ##### ##### ##### ##### ##### ##### # # ##### #####
# # # # # # # # # # # # # # #
###### # # # # ##### ##### ##### ##### # # ##### #####
# # # # ##### # # # # # # # # #
# # # # # # # # # ## # # # # ##
# # # # # ##### ##### # # # ##### # #
http server started on port $port
EOF;
echo $banner;
$server = uv_tcp_init();
uv_tcp_bind6($server, uv_ip6_addr($address, $port));
$clients = array();
$parsers = array();
uv_listen($server, 511, function($server_stream) use (&$parsers, &$clients){
$client = uv_tcp_init();
uv_accept($server_stream, $client);
$clients[(int)$client] = $client;
$parsers[(int)$client] = uv_http_parser_init();
uv_read_start($client, function($client, $nread, $buffer) use (&$parsers, &$clients){
if ($nread < 0) {
uv_shutdown($client, function($client){
uv_close($client, function($client) use (&$parsers, &$clients){
unset($parsers[(int)$client]);
unset($clients[(int)$client]);
});
});
} 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]);
});
});
}
} 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]);
});
});
}
}
});
});
//require "debug_timer.php";
uv_run(uv_default_loop());

4
uv.c
View File

@ -118,6 +118,10 @@ static int php_uv_class_init(TSRMLS_D)
zend_declare_class_constant_long(uv_class_entry, "PROCESS_WINDOWS_VERBATIM_ARGUMENTS", sizeof("PROCESS_WINDOWS_VERBATIM_ARGUMENTS")-1, UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS TSRMLS_CC);
zend_declare_class_constant_long(uv_class_entry, "PROCESS_DETACHED", sizeof("PROCESS_DETACHED")-1, UV_PROCESS_DETACHED TSRMLS_CC);
#define PHP_UV_ERRNO_GEN(code_notused, name, msg_notused) zend_declare_class_constant_long(uv_class_entry, #name, sizeof(#name)-1, UV_##name TSRMLS_CC);
UV_ERRNO_MAP(PHP_UV_ERRNO_GEN)
#undef PHP_UV_ERRNO_GEN
return 0;
}