static PHP_METHOD(swoole_coroutine_util, sleep) { coro_check(TSRMLS_C); double seconds; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", & seconds) == FAILURE) { return; } int ms = (int) (seconds * 1000); if (SwooleG.serv && swIsMaster()) { swoole_php_fatal_error(E_WARNING, "cannot use timer in master process."); return; } if (ms > SW_TIMER_MAX_VALUE) { swoole_php_fatal_error(E_WARNING, "The given parameters is too big."); return; } if (ms <= 0) { swoole_php_fatal_error(E_WARNING, "Timer must be greater than 0"); return; } php_swoole_check_reactor(); php_swoole_check_timer(ms); swoole_coroutine_sleep(seconds); RETURN_TRUE; }
long php_swoole_add_timer(int ms, zval *callback, zval *param, int is_tick TSRMLS_DC) { if (ms > 86400000) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "The given parameters is too big."); return SW_ERR; } 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 SW_ERR; } efree(func_name); if (SwooleGS->start > 0 && swIsTaskWorker()) { swoole_php_error(E_WARNING, "cannot use swoole_server->after in task worker."); } swTimer_callback *cb = emalloc(sizeof(swTimer_callback)); cb->data = param; cb->callback = callback; if (is_tick) { cb->type = SW_TIMER_TICK; } else { cb->type = SW_TIMER_AFTER; } php_swoole_check_reactor(); php_swoole_check_timer(ms); sw_zval_add_ref(&cb->callback); if (cb->data) { sw_zval_add_ref(&cb->data); } return SwooleG.timer.add(&SwooleG.timer, ms, is_tick, cb); }
static PHP_METHOD(swoole_coroutine_util, sleep) { coro_check(TSRMLS_C); double seconds; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", & seconds) == FAILURE) { return; } int ms = (int) (seconds * 1000); if (SwooleG.serv && swIsMaster()) { swoole_php_fatal_error(E_WARNING, "cannot use timer in master process."); return; } if (ms > SW_TIMER_MAX_VALUE) { swoole_php_fatal_error(E_WARNING, "The given parameters is too big."); return; } if (ms <= 0) { swoole_php_fatal_error(E_WARNING, "Timer must be greater than 0"); return; } php_context *context = emalloc(sizeof(php_context)); context->onTimeout = NULL; context->state = SW_CORO_CONTEXT_RUNNING; php_swoole_check_reactor(); php_swoole_check_timer(ms); if (SwooleG.timer.add(&SwooleG.timer, ms, 0, context, php_coroutine_sleep_timeout) == NULL) { RETURN_FALSE; } coro_save(context); coro_yield(); }
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, "redis server host is empty."); RETURN_FALSE; } swRedisClient *redis = swoole_get_object(getThis()); redisAsyncContext *context; if (strncasecmp(host, ZEND_STRL("unix:/")) == 0) { context = redisAsyncConnectUnix(host + 5); } else { if (port <= 1 || port > 65535) { swoole_php_error(E_WARNING, "redis server port is invalid."); RETURN_FALSE; } context = redisAsyncConnect(host, (int) port); } if (context->err) { swoole_php_error(E_WARNING, "failed to connect to the redis-server[%s:%d], Erorr: %s[%d]", host, (int) port, context->errstr, context->err); RETURN_FALSE; } php_swoole_check_reactor(); if (!swReactor_handle_isset(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS)) { 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); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_ERROR, swoole_redis_onError); } redisAsyncSetConnectCallback(context, swoole_redis_onConnect); redisAsyncSetDisconnectCallback(context, swoole_redis_onClose); zend_update_property_long(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("sock"), context->c.fd TSRMLS_CC); zend_update_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("onConnect"), callback TSRMLS_CC); 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; } if (redis->timeout > 0) { php_swoole_check_timer((int) (redis->timeout * 1000)); redis->timer = SwooleG.timer.add(&SwooleG.timer, (int) (redis->timeout * 1000), 0, redis, swoole_redis_onTimeout); } sw_zval_add_ref(&redis->object); swConnection *conn = swReactor_get(SwooleG.main_reactor, redis->context->c.fd); conn->object = redis; }
long php_swoole_add_timer(int ms, zval *callback, zval *param, int persistent TSRMLS_DC) { if (ms > SW_TIMER_MAX_VALUE) { swoole_php_fatal_error(E_WARNING, "The given parameters is too big."); return SW_ERR; } if (ms <= 0) { swoole_php_fatal_error(E_WARNING, "Timer must be greater than 0"); return SW_ERR; } char *func_name = NULL; zend_fcall_info_cache *func_cache = emalloc(sizeof(zend_fcall_info_cache)); if (!sw_zend_is_callable_ex(callback, NULL, 0, &func_name, NULL, func_cache, NULL TSRMLS_CC)) { efree(func_cache); efree(func_name); swoole_php_fatal_error(E_ERROR, "Function '%s' is not callable", func_name); return SW_ERR; } efree(func_name); if (!swIsTaskWorker()) { php_swoole_check_reactor(); } php_swoole_check_timer(ms); swTimer_callback *cb = emalloc(sizeof(swTimer_callback)); cb->data = &cb->_data; cb->callback = &cb->_callback; memcpy(cb->callback, callback, sizeof(zval)); if (param) { memcpy(cb->data, param, sizeof(zval)); } else { cb->data = NULL; } if (SwooleG.enable_coroutine) { cb->func_cache = func_cache; } else { efree(func_cache); } swTimerCallback timer_func; if (persistent) { cb->type = SW_TIMER_TICK; timer_func = php_swoole_onInterval; } else { cb->type = SW_TIMER_AFTER; timer_func = php_swoole_onTimeout; } sw_zval_add_ref(&cb->callback); if (cb->data) { sw_zval_add_ref(&cb->data); } swTimer_node *tnode = SwooleG.timer.add(&SwooleG.timer, ms, persistent, cb, timer_func); if (tnode == NULL) { swoole_php_fatal_error(E_WARNING, "add timer failed."); return SW_ERR; } else { tnode->type = SW_TIMER_TYPE_PHP; return tnode->id; } }