-
Notifications
You must be signed in to change notification settings - Fork 8.1k
streams: add so_rcvbuf and so_sndbuf socket context options. #23406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devnexen
wants to merge
2
commits into
php:master
Choose a base branch
from
devnexen:stream_stf_so_buf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| --TEST-- | ||
| stream_socket_server() and stream_socket_client() SO_RCVBUF and SO_SNDBUF context options test | ||
| --EXTENSIONS-- | ||
| sockets | ||
| --FILE-- | ||
| <?php | ||
| function buffers($stream): array { | ||
| $sock = socket_import_stream($stream); | ||
| return [ | ||
| socket_get_option($sock, SOL_SOCKET, SO_RCVBUF), | ||
| socket_get_option($sock, SOL_SOCKET, SO_SNDBUF), | ||
| ]; | ||
| } | ||
|
|
||
| // Shrinking is always honoured, while growing may be capped by the system maximum. | ||
| function context(int $rcvbuf, int $sndbuf) { | ||
| return stream_context_create(['socket' => [ | ||
| 'so_rcvbuf' => intdiv($rcvbuf, 4), | ||
| 'so_sndbuf' => intdiv($sndbuf, 4), | ||
| ]]); | ||
| } | ||
|
|
||
| function port($server): int { | ||
| return (int)substr(strrchr(stream_socket_get_name($server, false), ':'), 1); | ||
| } | ||
|
|
||
| $control = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, | ||
| STREAM_SERVER_BIND | STREAM_SERVER_LISTEN); | ||
|
|
||
| if (!$control) { | ||
| die('Unable to create server'); | ||
| } | ||
|
|
||
| [$rcvbuf, $sndbuf] = buffers($control); | ||
|
|
||
| $server = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, | ||
| STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, context($rcvbuf, $sndbuf)); | ||
|
|
||
| if (!$server) { | ||
| die('Unable to create server'); | ||
| } | ||
|
|
||
| echo "Listen buffers\n"; | ||
| [$listen_rcvbuf, $listen_sndbuf] = buffers($server); | ||
| var_dump($listen_rcvbuf < $rcvbuf); | ||
| var_dump($listen_sndbuf < $sndbuf); | ||
|
|
||
| // A connection is compared against another connection: some systems size the | ||
| // receive buffer of a connected socket on their own. | ||
| $control_client = stream_socket_client("tcp://127.0.0.1:" . port($server), $errno, $errstr, 30); | ||
|
|
||
| if (!$control_client) { | ||
| die('Unable to create client'); | ||
| } | ||
|
|
||
| $control_accepted = stream_socket_accept($server, 1); | ||
|
|
||
| if (!$control_accepted) { | ||
| die('Unable to accept connection'); | ||
| } | ||
|
|
||
| [, $client_sndbuf] = buffers($control_client); | ||
|
|
||
| $client = stream_socket_client("tcp://127.0.0.1:" . port($server), $errno, $errstr, 30, | ||
| STREAM_CLIENT_CONNECT, context($client_sndbuf, $client_sndbuf)); | ||
|
|
||
| if (!$client) { | ||
| die('Unable to create client'); | ||
| } | ||
|
|
||
| $accepted = stream_socket_accept($server, 1); | ||
|
|
||
| if (!$accepted) { | ||
| die('Unable to accept connection'); | ||
| } | ||
|
|
||
| echo "Client buffers\n"; | ||
| [, $client_sndbuf2] = buffers($client); | ||
| var_dump($client_sndbuf2 < $client_sndbuf); | ||
|
|
||
| fclose($accepted); | ||
| fclose($control_accepted); | ||
| fclose($client); | ||
| fclose($control_client); | ||
| fclose($server); | ||
| fclose($control); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Listen buffers | ||
| bool(true) | ||
| bool(true) | ||
| Client buffers | ||
| bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --TEST-- | ||
| SO_RCVBUF and SO_SNDBUF context options reject invalid values | ||
| --FILE-- | ||
| <?php | ||
| foreach (['so_rcvbuf', 'so_sndbuf'] as $option) { | ||
| foreach ([0, -1, 'abc'] as $value) { | ||
| $context = stream_context_create(['socket' => [$option => $value]]); | ||
| try { | ||
| @stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, | ||
| STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); | ||
| } catch (ValueError $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $context = stream_context_create(['socket' => ['so_rcvbuf' => 0]]); | ||
| try { | ||
| @stream_socket_client("tcp://127.0.0.1:1", $errno, $errstr, 1, | ||
| STREAM_CLIENT_CONNECT, $context); | ||
| } catch (ValueError $e) { | ||
| echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| ValueError: stream context option 'so_rcvbuf' must be between 1 and 2147483647 | ||
| ValueError: stream context option 'so_rcvbuf' must be between 1 and 2147483647 | ||
| ValueError: stream context option 'so_rcvbuf' must be between 1 and 2147483647 | ||
| ValueError: stream context option 'so_sndbuf' must be between 1 and 2147483647 | ||
| ValueError: stream context option 'so_sndbuf' must be between 1 and 2147483647 | ||
| ValueError: stream context option 'so_sndbuf' must be between 1 and 2147483647 | ||
| ValueError: stream context option 'so_rcvbuf' must be between 1 and 2147483647 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --TEST-- | ||
| stream_socket_server() SO_RCVBUF and SO_SNDBUF context options test with UDP | ||
| --EXTENSIONS-- | ||
| sockets | ||
| --FILE-- | ||
| <?php | ||
| function buffers($stream): array { | ||
| $sock = socket_import_stream($stream); | ||
| return [ | ||
| socket_get_option($sock, SOL_SOCKET, SO_RCVBUF), | ||
| socket_get_option($sock, SOL_SOCKET, SO_SNDBUF), | ||
| ]; | ||
| } | ||
|
|
||
| $control = stream_socket_server("udp://127.0.0.1:0", $errno, $errstr, STREAM_SERVER_BIND); | ||
|
|
||
| if (!$control) { | ||
| die('Unable to create server'); | ||
| } | ||
|
|
||
| [$rcvbuf, $sndbuf] = buffers($control); | ||
|
|
||
| // Shrinking is always honoured, while growing may be capped by the system maximum. | ||
| $context = stream_context_create(['socket' => [ | ||
| 'so_rcvbuf' => intdiv($rcvbuf, 4), | ||
| 'so_sndbuf' => intdiv($sndbuf, 4), | ||
| ]]); | ||
|
|
||
| $server = stream_socket_server("udp://127.0.0.1:0", $errno, $errstr, STREAM_SERVER_BIND, $context); | ||
|
|
||
| if (!$server) { | ||
| die('Unable to create server'); | ||
| } | ||
|
|
||
| [$server_rcvbuf, $server_sndbuf] = buffers($server); | ||
| echo "Server buffers\n"; | ||
| var_dump($server_rcvbuf < $rcvbuf); | ||
| var_dump($server_sndbuf < $sndbuf); | ||
|
|
||
| fclose($server); | ||
| fclose($control); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Server buffers | ||
| bool(true) | ||
| bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.