Skip to content

New function: stream_socket_listen() #431

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_socket_server, 0, 0, 1)
ZEND_ARG_INFO(0, context)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_stream_socket_listen, 0)
ZEND_ARG_INFO(0, serverstream)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_socket_accept, 0, 0, 1)
ZEND_ARG_INFO(0, serverstream)
ZEND_ARG_INFO(0, timeout)
Expand Down Expand Up @@ -3125,6 +3129,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(stream_filter_remove, arginfo_stream_filter_remove)
PHP_FE(stream_socket_client, arginfo_stream_socket_client)
PHP_FE(stream_socket_server, arginfo_stream_socket_server)
PHP_FE(stream_socket_listen, arginfo_stream_socket_listen)
PHP_FE(stream_socket_accept, arginfo_stream_socket_accept)
PHP_FE(stream_socket_get_name, arginfo_stream_socket_get_name)
PHP_FE(stream_socket_recvfrom, arginfo_stream_socket_recvfrom)
Expand Down
34 changes: 34 additions & 0 deletions ext/standard/streamsfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,40 @@ PHP_FUNCTION(stream_socket_server)
}
/* }}} */

/* {{{ proto bool stream_socket_listen(resource serverstream)
Listen for client connections on a socket previously bound via stream_socket_server() */
PHP_FUNCTION(stream_socket_listen)
{
php_stream *stream = NULL;
zval *zstream = NULL, **zbacklog = NULL;
int backlog = 32;
char *error_text = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) == FAILURE) {
RETURN_FALSE;
}

php_stream_from_zval(stream, &zstream);

if (stream->context && php_stream_context_get_option(stream->context, "socket", "backlog", &zbacklog) == SUCCESS) {
zval *ztmp = *zbacklog;
convert_to_long_ex(&ztmp);
backlog = Z_LVAL_P(ztmp);
if (ztmp != *zbacklog) {
zval_ptr_dtor(&ztmp);
}
}

if (0 != php_stream_xport_listen(stream, backlog, &error_text TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, error_text == NULL ? "Unknown error" : error_text);
RETURN_FALSE;
} else {
php_stream_to_zval(stream, return_value);
RETURN_TRUE;
}
}
/* }}} */

/* {{{ proto resource stream_socket_accept(resource serverstream, [ double timeout [, string &peername ]])
Accept a client connection from a server socket */
PHP_FUNCTION(stream_socket_accept)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/streamsfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

PHP_FUNCTION(stream_socket_client);
PHP_FUNCTION(stream_socket_server);
PHP_FUNCTION(stream_socket_listen);
PHP_FUNCTION(stream_socket_accept);
PHP_FUNCTION(stream_socket_get_name);
PHP_FUNCTION(stream_socket_recvfrom);
Expand Down
40 changes: 40 additions & 0 deletions ext/standard/tests/streams/stream_socket_listen.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Tests that stream_socket_listen() listens on a previously bound socket
--FILE--
<?php
$server = stream_socket_server('tcp://0.0.0.0:0', $errno, $errstr, STREAM_SERVER_BIND);
var_dump(stream_socket_listen($server));

$port = explode(':', stream_socket_get_name($server, FALSE), 2)[1];

$clientAddr = "tcp://127.0.0.1:{$port}";
$clientFlags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
$client = stream_socket_client($clientAddr, $errno, $errstr, $timeout = 0, $clientFlags);

while (TRUE) {
$r = [$server];
$w = $e = NULL;
if (stream_select($r, $w, $e, $timeout = 1)) {
$serverClient = stream_socket_accept($server, 1);
break;
}
}

fwrite($serverClient, "foo\n");
$sockData = trim(fgets($client));

var_dump($server, $client, $serverClient, $sockData);

fclose($server);
fclose($client);
fclose($serverClient);
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
bool(true)
resource(%d) of type (stream)
resource(%d) of type (stream)
resource(%d) of type (stream)
string(3) "foo"
===DONE===
3 changes: 3 additions & 0 deletions main/streams/transports.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ PHPAPI int php_stream_xport_listen(php_stream *stream, int backlog, char **error
}

return param.outputs.returncode;

} else if (ret == PHP_STREAM_OPTION_RETURN_NOTIMPL) {
*error_text = "listening not supported on this stream";
}

return ret;
Expand Down