static PHP_METHOD(swoole_http2_client, __construct) { char *host; zend_size_t host_len; long port = 80; zend_bool ssl = SW_FALSE; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &host, &host_len, &port, &ssl) == FAILURE) { return; } if (host_len <= 0) { zend_throw_exception(swoole_exception_class_entry_ptr, "host is empty.", SW_ERROR_INVALID_PARAMS TSRMLS_CC); RETURN_FALSE; } http2_client_property *hcc; hcc = (http2_client_property*) emalloc(sizeof(http2_client_property)); bzero(hcc, sizeof(http2_client_property)); swoole_set_property(getThis(), HTTP2_CLIENT_PROPERTY_INDEX, hcc); hcc->requests = swLinkedList_new(0, http2_client_request_free); hcc->stream_requests = swLinkedList_new(0, http2_client_request_free); hcc->streams = swHashMap_new(8, http2_client_stream_free); hcc->stream_id = 1; zval *ztype; SW_MAKE_STD_ZVAL(ztype); long type = SW_FLAG_ASYNC | SW_SOCK_TCP; if (ssl) { type |= SW_SOCK_SSL; hcc->ssl = 1; } ZVAL_LONG(ztype, type); zval *zobject = getThis(); zval *retval = NULL; sw_zend_call_method_with_1_params(&zobject, swoole_client_class_entry_ptr, NULL, "__construct", &retval, ztype); if (retval) { sw_zval_ptr_dtor(&retval); } sw_zval_ptr_dtor(&ztype); hcc->host = estrndup(host, host_len); hcc->host_len = host_len; hcc->port = port; }
/* * suspend current coroutine */ static PHP_METHOD(swoole_coroutine_util, yield) { int cid = sw_get_current_cid(); if (cid < 0) { swoole_php_fatal_error(E_ERROR, "can not yield outside coroutine"); RETURN_FALSE; } swLinkedList *coros_list = swHashMap_find_int(defer_coros, cid); if (coros_list == NULL) { coros_list = swLinkedList_new(2, NULL); if (coros_list == NULL) { RETURN_FALSE; } if (swHashMap_add_int(defer_coros, cid, coros_list) == SW_ERR) { swLinkedList_free(coros_list); RETURN_FALSE; } } php_context *context = emalloc(sizeof(php_context)); coro_save(context); if (swLinkedList_append(coros_list, (void *) context) == SW_ERR) { efree(context); RETURN_FALSE; } coro_yield(); }
static PHP_METHOD(swoole_coroutine_util, suspend) { char *id; int id_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",&id, &id_len) == FAILURE) { return; } swLinkedList *coros_list = swHashMap_find(defer_coros, id, id_len); if (coros_list == NULL) { coros_list = swLinkedList_new(2, NULL); if (coros_list == NULL) { RETURN_FALSE; } if (swHashMap_add(defer_coros, id, id_len, coros_list) == SW_ERR) { swLinkedList_free(coros_list); RETURN_FALSE; } } php_context *context = emalloc(sizeof(php_context)); coro_save(context); if (swLinkedList_append(coros_list, (void *)context) == SW_ERR) { efree(context); RETURN_FALSE; } coro_yield(); }