Last active
September 2, 2019 09:13
-
-
Save zhaoyanliang2/ded89c60251c6320278c9313dbd93f52 to your computer and use it in GitHub Desktop.
socket support for php-thread
This file contains 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
#include "ext/standard/file.h" | |
PHP_FUNCTION(stream_fd_fetch) | |
{ | |
zval *res; | |
zend_bool detach = TRUE; | |
php_stream *stream; | |
php_netstream_data_t *sock; | |
php_socket_t socket; | |
const char *resource_type; | |
ZEND_PARSE_PARAMETERS_START(1, 2) | |
Z_PARAM_RESOURCE(res) | |
Z_PARAM_OPTIONAL | |
Z_PARAM_BOOL(detach) | |
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); | |
resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(res)); | |
if ( strcmp(resource_type, "stream") != 0 ) { | |
zend_throw_error(NULL, "Only stream resource can be detached"); | |
return; | |
} | |
php_stream_from_zval(stream, res); | |
sock = (php_netstream_data_t*)stream->abstract; | |
socket = sock->socket; | |
if ( detach ) { | |
sock->socket = SOCK_ERR; | |
} | |
RETURN_LONG(socket); | |
} | |
PHP_FUNCTION(stream_fd_attach) | |
{ | |
php_socket_t socket; | |
zend_string *protocol = 0; | |
char *fake_name; | |
php_stream *stream; | |
php_netstream_data_t *sock; | |
ZEND_PARSE_PARAMETERS_START(1, 2) | |
Z_PARAM_LONG(socket) | |
Z_PARAM_OPTIONAL | |
Z_PARAM_STR(protocol) | |
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); | |
if ( protocol ) { | |
fake_name = emalloc(ZSTR_LEN(protocol) + sizeof("://attached")); | |
memcpy(fake_name, ZSTR_VAL(protocol), ZSTR_LEN(protocol)); | |
memcpy(fake_name + ZSTR_LEN(protocol), "://attached", sizeof("://attached")); | |
} else { | |
fake_name = "tcp://attached"; | |
} | |
stream = php_stream_xport_create(fake_name, 7, 0, STREAM_XPORT_CLIENT, | |
NULL, NULL, NULL, NULL, NULL); | |
sock = (php_netstream_data_t*)stream->abstract; | |
sock->socket = (php_socket_t)socket; | |
php_stream_to_zval(stream, return_value); | |
if ( protocol ) { | |
efree(fake_name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment