Get socket resource by fd

Issue #55 resolved
Jonny Big created an issue

Hi.

Can we have an option to convert $fd arg. (in callbacks) back to socket resource.

something like:

/* {{{ proto resource EventUtil::getSocketByFd([mixed fd = null]);
 *
 * Returns socket. */
PHP_METHOD(EventUtil, getSocketByFd)
{
    zval                  *pzfd = NULL;
    php_socket            *php_sock;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &pzfd) == FAILURE) {
        return;
    }

    if (pzfd) {
        evutil_socket_t fd = (evutil_socket_t)php_event_zval_to_fd(pzfd);

        if (fd < 0) {
            RETURN_FALSE;
        }

        php_sock = php_create_socket();
        php_sock->bsd_socket = fd;
        RETVAL_RES(zend_register_resource(php_sock, php_sockets_le_socket()));

    } else {
        RETURN_FALSE;
    }
}
/* }}} */

Comments (1)

  1. Ruslan Osmanov repo owner

    There is already a method for this: EventUtil::createSocket(). The method is available when the ext is configured with --enable-event-sockets option.

  2. Log in to comment