|
| 1 | +--TEST-- |
| 2 | +stream_socket_server() and stream_socket_client() SO_RCVBUF and SO_SNDBUF context options test |
| 3 | +--EXTENSIONS-- |
| 4 | +sockets |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +function buffers($stream): array { |
| 8 | + $sock = socket_import_stream($stream); |
| 9 | + return [ |
| 10 | + socket_get_option($sock, SOL_SOCKET, SO_RCVBUF), |
| 11 | + socket_get_option($sock, SOL_SOCKET, SO_SNDBUF), |
| 12 | + ]; |
| 13 | +} |
| 14 | + |
| 15 | +// Shrinking is always honoured, while growing may be capped by the system maximum. |
| 16 | +function context(int $rcvbuf, int $sndbuf) { |
| 17 | + return stream_context_create(['socket' => [ |
| 18 | + 'so_rcvbuf' => intdiv($rcvbuf, 4), |
| 19 | + 'so_sndbuf' => intdiv($sndbuf, 4), |
| 20 | + ]]); |
| 21 | +} |
| 22 | + |
| 23 | +function port($server): int { |
| 24 | + return (int)substr(strrchr(stream_socket_get_name($server, false), ':'), 1); |
| 25 | +} |
| 26 | + |
| 27 | +$control = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, |
| 28 | + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN); |
| 29 | + |
| 30 | +if (!$control) { |
| 31 | + die('Unable to create server'); |
| 32 | +} |
| 33 | + |
| 34 | +[$rcvbuf, $sndbuf] = buffers($control); |
| 35 | + |
| 36 | +$server = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, |
| 37 | + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, context($rcvbuf, $sndbuf)); |
| 38 | + |
| 39 | +if (!$server) { |
| 40 | + die('Unable to create server'); |
| 41 | +} |
| 42 | + |
| 43 | +echo "Listen buffers\n"; |
| 44 | +[$listen_rcvbuf, $listen_sndbuf] = buffers($server); |
| 45 | +var_dump($listen_rcvbuf < $rcvbuf); |
| 46 | +var_dump($listen_sndbuf < $sndbuf); |
| 47 | + |
| 48 | +// A connection is compared against another connection: some systems size the |
| 49 | +// receive buffer of a connected socket on their own. |
| 50 | +$control_client = stream_socket_client("tcp://127.0.0.1:" . port($server), $errno, $errstr, 30); |
| 51 | + |
| 52 | +if (!$control_client) { |
| 53 | + die('Unable to create client'); |
| 54 | +} |
| 55 | + |
| 56 | +$control_accepted = stream_socket_accept($server, 1); |
| 57 | + |
| 58 | +if (!$control_accepted) { |
| 59 | + die('Unable to accept connection'); |
| 60 | +} |
| 61 | + |
| 62 | +[, $client_sndbuf] = buffers($control_client); |
| 63 | + |
| 64 | +$client = stream_socket_client("tcp://127.0.0.1:" . port($server), $errno, $errstr, 30, |
| 65 | + STREAM_CLIENT_CONNECT, context($client_sndbuf, $client_sndbuf)); |
| 66 | + |
| 67 | +if (!$client) { |
| 68 | + die('Unable to create client'); |
| 69 | +} |
| 70 | + |
| 71 | +$accepted = stream_socket_accept($server, 1); |
| 72 | + |
| 73 | +if (!$accepted) { |
| 74 | + die('Unable to accept connection'); |
| 75 | +} |
| 76 | + |
| 77 | +echo "Client buffers\n"; |
| 78 | +[, $client_sndbuf2] = buffers($client); |
| 79 | +var_dump($client_sndbuf2 < $client_sndbuf); |
| 80 | + |
| 81 | +fclose($accepted); |
| 82 | +fclose($control_accepted); |
| 83 | +fclose($client); |
| 84 | +fclose($control_client); |
| 85 | +fclose($server); |
| 86 | +fclose($control); |
| 87 | + |
| 88 | +?> |
| 89 | +--EXPECT-- |
| 90 | +Listen buffers |
| 91 | +bool(true) |
| 92 | +bool(true) |
| 93 | +Client buffers |
| 94 | +bool(true) |
0 commit comments