Browse Source
- Added socket_import_stream().
- Added socket_import_stream().
- Fixed socket_strerror.phpt - Made php_set_sock_blocking return FAILURE on fcntl error. - Made socket_set_block()/socket_set_nonblock() emit warning on error.experimental/with_scalar_types
9 changed files with 406 additions and 17 deletions
-
10ext/sockets/php_sockets.h
-
165ext/sockets/sockets.c
-
26ext/sockets/tests/socket_import_stream-1.phpt
-
49ext/sockets/tests/socket_import_stream-2.phpt
-
46ext/sockets/tests/socket_import_stream-3.phpt
-
98ext/sockets/tests/socket_import_stream-4.phpt
-
23ext/sockets/tests/socket_import_stream-5.phpt
-
2ext/sockets/tests/socket_strerror.phpt
-
4main/network.c
@ -0,0 +1,26 @@ |
|||
--TEST-- |
|||
socket_import_stream: Basic test |
|||
--SKIPIF-- |
|||
<?php |
|||
if (!extension_loaded('sockets')) { |
|||
die('SKIP sockets extension not available.'); |
|||
} |
|||
|
|||
--FILE-- |
|||
<?php |
|||
|
|||
$domain = (strtoupper(substr(PHP_OS, 0, 3) == 'WIN') ? STREAM_PF_INET : STREAM_PF_UNIX); |
|||
$s = stream_socket_pair($domain, STREAM_SOCK_STREAM, 0); |
|||
|
|||
$s0 = reset($s); |
|||
$s1 = next($s); |
|||
|
|||
$sock = socket_import_stream($s0); |
|||
var_dump($sock); |
|||
socket_write($sock, "test message"); |
|||
socket_close($sock); |
|||
|
|||
var_dump(stream_get_contents($s1)); |
|||
--EXPECTF-- |
|||
resource(%d) of type (Socket) |
|||
string(12) "test message" |
|||
@ -0,0 +1,49 @@ |
|||
--TEST-- |
|||
socket_import_stream: Bad arguments |
|||
--SKIPIF-- |
|||
<?php |
|||
if (!extension_loaded('sockets')) { |
|||
die('SKIP sockets extension not available.'); |
|||
} |
|||
|
|||
--FILE-- |
|||
<?php |
|||
|
|||
var_dump(socket_import_stream()); |
|||
var_dump(socket_import_stream(1, 2)); |
|||
var_dump(socket_import_stream(1)); |
|||
var_dump(socket_import_stream(new stdclass)); |
|||
var_dump(socket_import_stream(fopen(__FILE__, "rb"))); |
|||
var_dump(socket_import_stream(socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))); |
|||
$s = stream_socket_server("udp://127.0.0.1:58392", $errno, $errstr, STREAM_SERVER_BIND); |
|||
var_dump($s); |
|||
var_dump(fclose($s)); |
|||
var_dump(socket_import_stream($s)); |
|||
|
|||
|
|||
echo "Done."; |
|||
--EXPECTF-- |
|||
Warning: socket_import_stream() expects exactly 1 parameter, 0 given in %s on line %d |
|||
NULL |
|||
|
|||
Warning: socket_import_stream() expects exactly 1 parameter, 2 given in %s on line %d |
|||
NULL |
|||
|
|||
Warning: socket_import_stream() expects parameter 1 to be resource, integer given in %s on line %d |
|||
NULL |
|||
|
|||
Warning: socket_import_stream() expects parameter 1 to be resource, object given in %s on line %d |
|||
NULL |
|||
|
|||
Warning: socket_import_stream(): cannot represent a stream of type STDIO as a Socket Descriptor in %s on line %d |
|||
bool(false) |
|||
|
|||
Warning: socket_import_stream(): supplied resource is not a valid stream resource in %s on line %d |
|||
bool(false) |
|||
resource(%d) of type (stream) |
|||
bool(true) |
|||
|
|||
Warning: socket_import_stream(): %d is not a valid stream resource in %s on line %d |
|||
bool(false) |
|||
Done. |
|||
|
|||
@ -0,0 +1,46 @@ |
|||
--TEST-- |
|||
socket_import_stream: Test with multicasting |
|||
--SKIPIF-- |
|||
<?php |
|||
if (!extension_loaded('sockets')) { |
|||
die('SKIP sockets extension not available.'); |
|||
} |
|||
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
|||
$br = socket_bind($s, '0.0.0.0', 58381); |
|||
if ($br === false) |
|||
die("SKIP IPv4/port 58381 not available"); |
|||
$so = socket_set_option($s, IPPROTO_IP, MCAST_JOIN_GROUP, array( |
|||
"group" => '224.0.0.23', |
|||
"interface" => "lo", |
|||
)); |
|||
if ($br === false) |
|||
die("SKIP joining group 224.0.0.23 on interface lo failed"); |
|||
--FILE-- |
|||
<?php |
|||
|
|||
$stream = stream_socket_server("udp://0.0.0.0:58381", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock = socket_import_stream($stream); |
|||
var_dump($sock); |
|||
$so = socket_set_option($sock, IPPROTO_IP, MCAST_JOIN_GROUP, array( |
|||
"group" => '224.0.0.23', |
|||
"interface" => "lo", |
|||
)); |
|||
var_dump($so); |
|||
|
|||
$sendsock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
|||
var_dump($sendsock); |
|||
$br = socket_bind($sendsock, '127.0.0.1'); |
|||
$so = socket_sendto($sendsock, $m = "my message", strlen($m), 0, "224.0.0.23", 58381); |
|||
var_dump($so); |
|||
|
|||
stream_set_blocking($stream, 0); |
|||
var_dump(fread($stream, strlen($m))); |
|||
echo "Done.\n"; |
|||
--EXPECTF-- |
|||
resource(%d) of type (Socket) |
|||
bool(true) |
|||
resource(%d) of type (Socket) |
|||
int(10) |
|||
string(10) "my message" |
|||
Done. |
|||
|
|||
@ -0,0 +1,98 @@ |
|||
--TEST-- |
|||
socket_import_stream: effects of closing |
|||
--SKIPIF-- |
|||
<?php |
|||
if (!extension_loaded('sockets')) { |
|||
die('SKIP sockets extension not available.'); |
|||
} |
|||
|
|||
--FILE-- |
|||
<?php |
|||
|
|||
function test($stream, $sock) { |
|||
if ($stream !== null) { |
|||
echo "stream_set_blocking "; |
|||
print_r(stream_set_blocking($stream, 0)); |
|||
echo "\n"; |
|||
} |
|||
if ($sock !== null) { |
|||
echo "socket_set_block "; |
|||
print_r(socket_set_block($sock)); |
|||
echo "\n"; |
|||
echo "socket_get_option "; |
|||
print_r(socket_get_option($sock, SOL_SOCKET, SO_TYPE)); |
|||
echo "\n"; |
|||
} |
|||
echo "\n"; |
|||
} |
|||
|
|||
echo "normal\n"; |
|||
$stream0 = stream_socket_server("udp://0.0.0.0:58380", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock0 = socket_import_stream($stream0); |
|||
test($stream0, $sock0); |
|||
|
|||
echo "\nunset stream\n"; |
|||
$stream1 = stream_socket_server("udp://0.0.0.0:58381", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock1 = socket_import_stream($stream1); |
|||
unset($stream1); |
|||
test(null, $sock1); |
|||
|
|||
echo "\nunset socket\n"; |
|||
$stream2 = stream_socket_server("udp://0.0.0.0:58382", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock2 = socket_import_stream($stream2); |
|||
unset($sock2); |
|||
test($stream2, null); |
|||
|
|||
echo "\nclose stream\n"; |
|||
$stream3 = stream_socket_server("udp://0.0.0.0:58383", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock3 = socket_import_stream($stream3); |
|||
fclose($stream3); |
|||
test($stream3, $sock3); |
|||
|
|||
echo "\nclose socket\n"; |
|||
$stream4 = stream_socket_server("udp://0.0.0.0:58384", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock4 = socket_import_stream($stream4); |
|||
socket_close($sock4); |
|||
test($stream4, $sock4); |
|||
|
|||
echo "Done.\n"; |
|||
--EXPECTF-- |
|||
normal |
|||
stream_set_blocking 1 |
|||
socket_set_block 1 |
|||
socket_get_option 2 |
|||
|
|||
|
|||
unset stream |
|||
socket_set_block 1 |
|||
socket_get_option 2 |
|||
|
|||
|
|||
unset socket |
|||
stream_set_blocking 1 |
|||
|
|||
|
|||
close stream |
|||
stream_set_blocking |
|||
Warning: stream_set_blocking(): %d is not a valid stream resource in %s on line %d |
|||
|
|||
socket_set_block |
|||
Warning: socket_set_block(): unable to set blocking mode [%d]: %s in %s on line %d |
|||
|
|||
socket_get_option |
|||
Warning: socket_get_option(): unable to retrieve socket option [%d]: %s in %s on line %d |
|||
|
|||
|
|||
|
|||
close socket |
|||
stream_set_blocking |
|||
Warning: stream_set_blocking(): %d is not a valid stream resource in %s on line %d |
|||
|
|||
socket_set_block |
|||
Warning: socket_set_block(): %d is not a valid Socket resource in %s on line %d |
|||
|
|||
socket_get_option |
|||
Warning: socket_get_option(): %d is not a valid Socket resource in %s on line %d |
|||
|
|||
|
|||
Done. |
|||
@ -0,0 +1,23 @@ |
|||
--TEST-- |
|||
socket_import_stream: effects of leaked handles |
|||
--SKIPIF-- |
|||
<?php |
|||
if (!extension_loaded('sockets')) { |
|||
die('SKIP sockets extension not available.'); |
|||
} |
|||
if (!function_exists('leak_variable')) |
|||
die('SKIP only for debug builds') |
|||
--FILE-- |
|||
<?php |
|||
|
|||
$stream0 = stream_socket_server("udp://0.0.0.0:58380", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock0 = socket_import_stream($stream0); |
|||
leak_variable($stream0, true); |
|||
|
|||
$stream1 = stream_socket_server("udp://0.0.0.0:58381", $errno, $errstr, STREAM_SERVER_BIND); |
|||
$sock1 = socket_import_stream($stream1); |
|||
leak_variable($sock1, true); |
|||
|
|||
echo "Done.\n"; |
|||
--EXPECT-- |
|||
Done. |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue