static PHP_METHOD(swoole_server_port, __destruct) { swoole_port_callbacks *callbacks = swoole_get_property(getThis(), 0); efree(callbacks); swoole_set_property(getThis(), 0, NULL); swoole_set_object(getThis(), NULL); }
static PHP_METHOD(swoole_server_port, __destruct) { swoole_server_port_property *property = swoole_get_property(getThis(), 0); efree(property); swoole_set_property(getThis(), 0, NULL); swoole_set_object(getThis(), NULL); }
static PHP_METHOD(swoole_ringqueue, __construct) { long len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &len) == FAILURE) { RETURN_FALSE; } if (len < 0) { len = SW_RINGQUEUE_LEN; } swRingQueue *queue = emalloc(sizeof(swRingQueue)); if (queue == NULL) { zend_throw_exception(swoole_exception_class_entry_ptr, "failed to create ringqueue.", SW_ERROR_MALLOC_FAIL TSRMLS_CC); RETURN_FALSE; } if (swRingQueue_init(queue, len)) { zend_throw_exception(swoole_exception_class_entry_ptr, "failed to init ringqueue.", SW_ERROR_MALLOC_FAIL TSRMLS_CC); RETURN_FALSE; } swoole_set_object(getThis(), queue); }
PHP_METHOD(swoole_lock, __construct) { long type = SW_MUTEX; char *filelock; int filelock_len = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &type, &filelock, &filelock_len) == FAILURE) { RETURN_FALSE; } swLock *lock = emalloc(sizeof(swLock)); if (lock == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "alloc failed."); RETURN_FALSE; } switch(type) { #ifdef HAVE_RWLOCK case SW_RWLOCK: ret = swRWLock_create(lock, 1); break; #endif case SW_FILELOCK: if (filelock_len <= 0) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "filelock require lock file name."); RETURN_FALSE; } int fd; if ((fd = open(filelock, O_RDWR | O_CREAT, 0666)) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "open file[%s] failed. Error: %s [%d]", filelock, strerror(errno), errno); RETURN_FALSE; } ret = swFileLock_create(lock, fd); break; case SW_SEM: ret = swSem_create(lock, IPC_PRIVATE); break; #ifdef HAVE_SPINLOCK case SW_SPINLOCK: ret = swSpinLock_create(lock, 1); break; #endif case SW_MUTEX: default: ret = swMutex_create(lock, 1); break; } if (ret < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "create lock failed"); RETURN_FALSE; } swoole_set_object(getThis(), lock); RETURN_TRUE; }
static PHP_METHOD(swoole_coroutine_util, listCoroutines) { object_init_ex(return_value, swoole_coroutine_iterator_class_entry_ptr); coroutine_iterator *i = emalloc(sizeof(coroutine_iterator)); bzero(i, sizeof(coroutine_iterator)); swoole_set_object(return_value, i); }
static PHP_METHOD(swoole_mysql, __destruct) { mysql_client *client = swoole_get_object(getThis()); if (!client) { swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_mysql."); RETURN_FALSE; } else if (client->state != SW_MYSQL_STATE_CLOSED && client->cli) { zval *retval = NULL; zval *zobject = getThis(); client->cli->destroyed = 1; sw_zend_call_method_with_0_params(&zobject, swoole_mysql_class_entry_ptr, NULL, "close", &retval); if (retval) { sw_zval_ptr_dtor(&retval); } } //release buffer memory if (client->buffer) { swString_free(client->buffer); } efree(client); swoole_set_object(getThis(), NULL); }
static PHP_METHOD(swoole_buffer, __construct) { long size = SW_STRING_BUFFER_DEFAULT; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &size) == FAILURE) { RETURN_FALSE; } if (size < 1) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "buffer size cannot be less than 0"); RETURN_FALSE; } else if (size > SW_STRING_BUFFER_MAXLEN) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "buffer size must not exceed %d", SW_STRING_BUFFER_MAXLEN); RETURN_FALSE; } swString *buffer = swString_new(size); if (buffer == NULL) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "malloc(%ld) failed.", size); RETURN_FALSE; } swoole_set_object(getThis(), buffer); zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("capacity"), size TSRMLS_CC); }
PHP_METHOD(swoole_atomic_long, __construct) { zend_long value = 0; #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_LONG(value) ZEND_PARSE_PARAMETERS_END(); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &value) == FAILURE) { RETURN_FALSE; } #endif sw_atomic_long_t *atomic = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(sw_atomic_long_t)); if (atomic == NULL) { zend_throw_exception(swoole_exception_class_entry_ptr, "global memory allocation failure.", SW_ERROR_MALLOC_FAIL TSRMLS_CC); RETURN_FALSE; } *atomic = (sw_atomic_long_t) value; swoole_set_object(getThis(), (void*) atomic); RETURN_TRUE; }
static PHP_METHOD(swoole_msgqueue, __destruct) { SW_PREVENT_USER_DESTRUCT; swMsgQueue *queue = swoole_get_object(getThis()); efree(queue); swoole_set_object(getThis(), NULL); }
static PHP_METHOD(swoole_process, __construct) { zend_bool redirect_stdin_and_stdout = 0; long create_pipe = 1; zval *callback; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bl", &callback, &redirect_stdin_and_stdout, &create_pipe) == FAILURE) { RETURN_FALSE; } char *func_name = NULL; if (!sw_zend_is_callable(callback, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "function '%s' is not callable", func_name); efree(func_name); RETURN_FALSE; } efree(func_name); swWorker *process = emalloc(sizeof(swWorker)); bzero(process, sizeof(swWorker)); process->id = php_swoole_worker_round_id++; if (php_swoole_worker_round_id == 0) { php_swoole_worker_round_id = 1; } if (redirect_stdin_and_stdout) { process->redirect_stdin = 1; process->redirect_stdout = 1; process->redirect_stderr = 1; create_pipe = 1; } if (create_pipe > 0) { swPipe *_pipe = emalloc(sizeof(swWorker)); int socket_type = create_pipe == 1 ? SOCK_STREAM : SOCK_DGRAM; if (swPipeUnsock_create(_pipe, 1, socket_type) < 0) { RETURN_FALSE; } process->pipe_object = _pipe; process->pipe_master = _pipe->getFd(_pipe, SW_PIPE_MASTER); process->pipe_worker = _pipe->getFd(_pipe, SW_PIPE_WORKER); process->pipe = process->pipe_master; zend_update_property_long(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("pipe"), process->pipe_master TSRMLS_CC); } swoole_set_object(getThis(), process); zend_update_property(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("callback"), callback TSRMLS_CC); }
PHP_METHOD(swoole_lock, __destruct) { swLock *lock = swoole_get_object(getThis()); if (lock) { lock->free(lock); swoole_set_object(getThis(), NULL); } }
static PHP_METHOD(swoole_redis, __destruct) { swRedisClient *redis = swoole_get_object(getThis()); if (redis && redis->context && redis->state != SWOOLE_REDIS_STATE_CLOSED) { redisAsyncDisconnect(redis->context); } swoole_set_object(getThis(), NULL); efree(redis); }
static PHP_METHOD(swoole_redis, __construct) { swRedisClient *redis = emalloc(sizeof(swRedisClient)); bzero(redis, sizeof(swRedisClient)); redis->object = getThis(); sw_copy_to_stack(redis->object, redis->_object); swoole_set_object(getThis(), redis); }
static PHP_METHOD(swoole_redis, __construct) { swRedisClient *redis = emalloc(sizeof(swRedisClient)); bzero(redis, sizeof(swRedisClient)); #if PHP_MAJOR_VERSION < 7 redis->object = getThis(); #else redis->object = &redis->_object; memcpy(redis->object, getThis(), sizeof(zval)); #endif swoole_set_object(getThis(), redis); }
PHP_METHOD(swoole_atomic, __construct) { long value = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &value) == FAILURE) { RETURN_FALSE; } sw_atomic_t *atomic = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(sw_atomic_t)); *atomic = (sw_atomic_t) value; swoole_set_object(getThis(), (void*) atomic); RETURN_TRUE; }
PHP_METHOD(swoole_table, __construct) { long table_size; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &table_size) == FAILURE) { RETURN_FALSE; } if (table_size < 1) { RETURN_FALSE; } swTable *table = swTable_new(table_size); swoole_set_object(getThis(), table); }
static PHP_METHOD(swoole_mysql, __construct) { if (!mysql_request_buffer) { mysql_request_buffer = swString_new(SW_MYSQL_QUERY_INIT_SIZE); if (!mysql_request_buffer) { swoole_php_fatal_error(E_ERROR, "[1] swString_new(%d) failed.", SW_HTTP_RESPONSE_INIT_SIZE); RETURN_FALSE; } } mysql_client *client = emalloc(sizeof(mysql_client)); bzero(client, sizeof(mysql_client)); swoole_set_object(getThis(), client); }
/** * @zobject: swoole_http_client object */ static void http_client_coro_onError(swClient *cli) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *zdata; zval *retval = NULL; SW_MAKE_STD_ZVAL(zdata); //return false ZVAL_BOOL(zdata, 0); zval *zobject = cli->object; php_context *sw_current_context = swoole_get_property(zobject, 1); zend_update_property_long(swoole_http_client_coro_class_entry_ptr, zobject, ZEND_STRL("errCode"), SwooleG.error TSRMLS_CC); if (cli->timeout_id > 0) { php_swoole_clear_timer_coro(cli->timeout_id TSRMLS_CC); cli->timeout_id=0; } if (!cli->released) { http_client_free(zobject TSRMLS_CC); } swoole_set_object(zobject, NULL); http_client_property *hcc = swoole_get_property(zobject, 0); if(hcc->defer && hcc->defer_status != HTTP_CLIENT_STATE_DEFER_WAIT){ hcc->defer_status = HTTP_CLIENT_STATE_DEFER_DONE; hcc->defer_result = 0; goto free_zdata; } hcc->defer_status = HTTP_CLIENT_STATE_DEFER_INIT; int ret = coro_resume(sw_current_context, zdata, &retval); if (ret > 0) { goto free_zdata; } if (retval != NULL) { sw_zval_ptr_dtor(&retval); } free_zdata: sw_zval_ptr_dtor(&zdata); }
static void http_client_coro_onTimeout(php_context *ctx) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *zdata; zval *retval = NULL; SW_MAKE_STD_ZVAL(zdata); ZVAL_BOOL(zdata, 0); //return false #if PHP_MAJOR_VERSION < 7 zval *zobject = (zval *)ctx->coro_params; #else zval _zobject = ctx->coro_params; zval *zobject = & _zobject; #endif //define time out RETURN ERROR 110 zend_update_property_long(swoole_http_client_coro_class_entry_ptr, zobject, ZEND_STRL("errCode"), 110 TSRMLS_CC); http_client *http = swoole_get_object(zobject); http->cli->released = 1; http_client_free(zobject TSRMLS_CC); swoole_set_object(zobject, NULL); http_client_property *hcc = swoole_get_property(zobject, 0); if(hcc->defer && hcc->defer_status != HTTP_CLIENT_STATE_DEFER_WAIT){ hcc->defer_status = HTTP_CLIENT_STATE_DEFER_DONE; hcc->defer_result = 0; goto free_zdata; } hcc->defer_status = HTTP_CLIENT_STATE_DEFER_INIT; int ret = coro_resume(ctx, zdata, &retval); if (ret > 0) { goto free_zdata; } if (retval != NULL) { sw_zval_ptr_dtor(&retval); } free_zdata: sw_zval_ptr_dtor(&zdata); }
static PHP_METHOD(swoole_mysql, __destruct) { mysql_client *client = swoole_get_object(getThis()); if (!client) { swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_mysql."); RETURN_FALSE; } // if (client->state != SW_MYSQL_STATE_CLOSED) // { // zval *retval; // sw_zend_call_method_with_0_params(&getThis(), swoole_mysql_class_entry_ptr, NULL, "close", &retval); // if (retval) // { // sw_zval_ptr_dtor(&retval); // } // } efree(client); swoole_set_object(getThis(), NULL); }
PHP_METHOD(swoole_table, __construct) { long table_size; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &table_size) == FAILURE) { RETURN_FALSE; } if (table_size < 1) { RETURN_FALSE; } swTable *table = swTable_new(table_size); if (table == NULL) { swoole_php_fatal_error(E_ERROR, "alloc global memory failed."); RETURN_FALSE; } swoole_set_object(getThis(), table); }
static PHP_METHOD(swoole_msgqueue, __construct) { long key; long perms = 0; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &key, &perms) == FAILURE) { RETURN_FALSE; } swMsgQueue *queue = emalloc(sizeof(swMsgQueue)); if (queue == NULL) { zend_throw_exception(swoole_exception_class_entry_ptr, "failed to create MsgQueue.", SW_ERROR_MALLOC_FAIL); RETURN_FALSE; } if (swMsgQueue_create(queue, 1, key, perms)) { zend_throw_exception(swoole_exception_class_entry_ptr, "failed to init MsgQueue.", SW_ERROR_MALLOC_FAIL); RETURN_FALSE; } swoole_set_object(getThis(), queue); }
static PHP_METHOD(swoole_redis, __construct) { zval *zset = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zset) == FAILURE) { return; } swRedisClient *redis = emalloc(sizeof(swRedisClient)); bzero(redis, sizeof(swRedisClient)); redis->object = getThis(); redis->timeout = SW_REDIS_CONNECT_TIMEOUT; redis->database = -1; if (zset && !ZVAL_IS_NULL(zset)) { php_swoole_array_separate(zset); zend_update_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("setting"), zset TSRMLS_CC); sw_zval_ptr_dtor(&zset); HashTable *vht; zval *ztmp; vht = Z_ARRVAL_P(zset); /** * timeout */ if (php_swoole_array_get_value(vht, "timeout", ztmp)) { convert_to_double(ztmp); redis->timeout = (double) Z_DVAL_P(ztmp); } /** * password */ if (php_swoole_array_get_value(vht, "password", ztmp)) { convert_to_string(ztmp); if (Z_STRLEN_P(ztmp) >= 1 << 8) { swoole_php_fatal_error(E_WARNING, "redis password is too long."); } else if (Z_STRLEN_P(ztmp) > 0) { redis->password = estrdup(Z_STRVAL_P(ztmp)); redis->password_len = Z_STRLEN_P(ztmp); } } /** * database */ if (php_swoole_array_get_value(vht, "database", ztmp)) { convert_to_long(ztmp); if (Z_LVAL_P(ztmp) > 1 << 8) { swoole_php_fatal_error(E_WARNING, "redis database number is too big."); } else { redis->database = (int8_t) Z_LVAL_P(ztmp); } } } sw_copy_to_stack(redis->object, redis->_object); swoole_set_object(getThis(), redis); }
static PHP_METHOD(swoole_mysql, __construct) { if (!mysql_request_buffer) { mysql_request_buffer = swString_new(SW_MYSQL_QUERY_INIT_SIZE); if (!mysql_request_buffer) { swoole_php_fatal_error(E_ERROR, "[1] swString_new(%d) failed.", SW_HTTP_RESPONSE_INIT_SIZE); RETURN_FALSE; } } char *unixsocket = NULL; zend_size_t unixsocket_len = 0; mysql_connector connector; connector.port = SW_MYSQL_DEFAULT_PORT; if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "ssss|ls", &connector.host, &connector.host_len, &connector.user, &connector.user_len, &connector.password, &connector.password_len, &connector.database, &connector.database_len, &connector.port, &unixsocket, &unixsocket_len) == FAILURE) { RETURN_FALSE; } swClient *cli = emalloc(sizeof(swClient)); int type = SW_SOCK_TCP; if (unixsocket) { type = SW_SOCK_UNIX_STREAM; connector.host = unixsocket; connector.host_len = unixsocket_len; } if (swClient_create(cli, type, 0) < 0) { zend_throw_exception(swoole_mysql_exception_class_entry, "swClient_create failed.", 1 TSRMLS_CC); RETURN_FALSE; } if (cli->connect(cli, connector.host, connector.port, SW_MYSQL_CONNECT_TIMEOUT, 0) < 0) { zend_throw_exception(swoole_mysql_exception_class_entry, "connect to mysql server[%s:%d] failed.", 2 TSRMLS_CC); RETURN_FALSE; } int tcp_nodelay = 1; if (setsockopt(cli->socket->fd, IPPROTO_TCP, TCP_NODELAY, (const void *) &tcp_nodelay, sizeof(int)) == -1) { swoole_php_sys_error(E_WARNING, "setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) failed.", cli->socket->fd); } char buf[2048]; int n = cli->recv(cli, buf, sizeof(buf), 0); if (n < 0) { zend_throw_exception(swoole_mysql_exception_class_entry, "recvfrom mysql server failed.", 3 TSRMLS_CC); RETURN_FALSE; } if (mysql_handshake(&connector, buf, n) == SW_ERR) { zend_throw_exception(swoole_mysql_exception_class_entry, "handshake with mysql server failed.", 4 TSRMLS_CC); RETURN_FALSE; } if (cli->send(cli, connector.buf, connector.packet_length + 4, 0) < 0) { zend_throw_exception(swoole_mysql_exception_class_entry, "sendto mysql server failed.", 5 TSRMLS_CC); RETURN_FALSE; } if (cli->recv(cli, buf, sizeof(buf), 0) < 0) { zend_throw_exception(swoole_mysql_exception_class_entry, "recvfrom mysql server failed.", 6 TSRMLS_CC); RETURN_FALSE; } mysql_client *client = emalloc(sizeof(mysql_client)); bzero(client, sizeof(mysql_client)); client->buffer = swString_new(SW_BUFFER_SIZE_BIG); client->fd = cli->socket->fd; client->object = getThis(); client->cli = cli; sw_copy_to_stack(client->object, client->_object); zend_update_property_bool(swoole_mysql_class_entry_ptr, getThis(), ZEND_STRL("connected"), 1 TSRMLS_CC); swoole_set_object(getThis(), client); php_swoole_check_reactor(); swSetNonBlock(cli->socket->fd); if (!isset_event_callback) { SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_MYSQL | SW_EVENT_READ, swoole_mysql_onRead); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_MYSQL | SW_EVENT_ERROR, swoole_mysql_onError); } swConnection *socket = swReactor_get(SwooleG.main_reactor, cli->socket->fd); socket->active = 1; socket->object = client; }
static PHP_METHOD(swoole_redis, connect) { char *host; zend_size_t host_len; long port; zval *callback; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz", &host, &host_len, &port, &callback) == FAILURE) { RETURN_FALSE; } if (host_len <= 0) { swoole_php_error(E_WARNING, "host is empty."); RETURN_FALSE; } if (port <= 1 || port > 65535) { swoole_php_error(E_WARNING, "port is invalid."); RETURN_FALSE; } swRedisClient *redis = emalloc(sizeof(swRedisClient)); bzero(redis, sizeof(swRedisClient)); #if PHP_MAJOR_VERSION < 7 redis->object = getThis(); #else redis->object = &redis->_object; memcpy(redis->object, getThis(), sizeof(zval)); #endif sw_zval_add_ref(&redis->object); swoole_set_object(getThis(), redis); redisAsyncContext *context = redisAsyncConnect(host, (int) port); if (context->err) { swoole_php_error(E_WARNING, "connect to redis-server[%s:%d] failed, Erorr: %s[%d]", host, (int) port, context->errstr, context->err); RETURN_FALSE; } php_swoole_check_reactor(); if (!isset_event_callback) { SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_READ, swoole_redis_onRead); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_WRITE, swoole_redis_onWrite); isset_event_callback = 1; } redisAsyncSetConnectCallback(context, swoole_redis_onConnect); redisAsyncSetDisconnectCallback(context, swoole_redis_onClose); #if PHP_MAJOR_VERSION < 7 redis->connect_callback = callback; #else redis->connect_callback = &redis->_connect_callback; memcpy(redis->connect_callback, callback, sizeof(zval)); #endif sw_zval_add_ref(&redis->connect_callback); redis->context = context; context->ev.addRead = swoole_redis_event_AddRead; context->ev.delRead = swoole_redis_event_DelRead; context->ev.addWrite = swoole_redis_event_AddWrite; context->ev.delWrite = swoole_redis_event_DelWrite; context->ev.cleanup = swoole_redis_event_Cleanup; context->ev.data = redis; zend_update_property_string(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("host"), host TSRMLS_CC); zend_update_property_long(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("port"), port TSRMLS_CC); if (SwooleG.main_reactor->add(SwooleG.main_reactor, redis->context->c.fd, PHP_SWOOLE_FD_REDIS | SW_EVENT_WRITE) < 0) { swoole_php_fatal_error(E_WARNING, "swoole_event_add failed. Erorr: %s[%d].", redis->context->errstr, redis->context->err); RETURN_FALSE; } swConnection *conn = swReactor_get(SwooleG.main_reactor, redis->context->c.fd); conn->object = redis; }
static PHP_METHOD(swoole_ringqueue, __destruct) { swRingQueue *queue = swoole_get_object(getThis()); efree(queue); swoole_set_object(getThis(), NULL); }
static PHP_METHOD(swoole_coroutine_iterator, __destruct) { coroutine_iterator *i = swoole_get_object(getThis()); efree(i); swoole_set_object(getThis(), NULL); }