From e25f262a0571b9cc2c622ec5a02292840a00e513 Mon Sep 17 00:00:00 2001 From: matyhtf Date: Tue, 12 May 2020 15:14:17 +0800 Subject: [PATCH] add swoole_set_last_error/swoole_get_last_error --- core-tests/src/core/log.cpp | 2 +- include/error.h | 3 --- include/swoole.h | 13 +++++++++++++ swoole.cc | 2 +- swoole_server.cc | 12 ++++++------ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/core-tests/src/core/log.cpp b/core-tests/src/core/log.cpp index bef9c9d4148..028bcf10913 100644 --- a/core-tests/src/core/log.cpp +++ b/core-tests/src/core/log.cpp @@ -58,7 +58,7 @@ TEST(log, date_format_long_string) int retval = swLog_set_date_format(str->str); ASSERT_EQ(retval, SW_ERR); - ASSERT_EQ(SwooleG.error, SW_ERROR_INVALID_PARAMS); + ASSERT_EQ(swoole_get_last_error(), SW_ERROR_INVALID_PARAMS); } TEST(log, date_with_microseconds) diff --git a/include/error.h b/include/error.h index 45f58845b5c..a347594ddde 100644 --- a/include/error.h +++ b/include/error.h @@ -153,7 +153,4 @@ enum swErrorCode SW_ERROR_END }; -const char* swoole_strerror(int code); -void swoole_throw_error(int code); - #endif /* SW_ERRNO_H_ */ diff --git a/include/swoole.h b/include/swoole.h index 4d736042c93..8414058801c 100644 --- a/include/swoole.h +++ b/include/swoole.h @@ -2590,6 +2590,19 @@ extern __thread swThreadGlobal_t SwooleTG; //Thread Global Variable #define SW_CPU_NUM (SwooleG.cpu_num) +static sw_inline void swoole_set_last_error(int error) +{ + SwooleG.error = error; +} + +static sw_inline int swoole_get_last_error() +{ + return SwooleG.error; +} + +SW_API const char* swoole_strerror(int code); +SW_API void swoole_throw_error(int code); + //----------------------------------------------- //OS Feature #if defined(HAVE_KQUEUE) || !defined(HAVE_SENDFILE) diff --git a/swoole.cc b/swoole.cc index f8affc7c2fd..227be6b51c4 100644 --- a/swoole.cc +++ b/swoole.cc @@ -855,7 +855,7 @@ static PHP_FUNCTION(swoole_hashcode) PHP_FUNCTION(swoole_last_error) { - RETURN_LONG(SwooleG.error); + RETURN_LONG(swoole_get_last_error()); } PHP_FUNCTION(swoole_cpu_num) diff --git a/swoole_server.cc b/swoole_server.cc index 64983bc4301..37656a11951 100644 --- a/swoole_server.cc +++ b/swoole_server.cc @@ -1872,7 +1872,7 @@ void php_swoole_onClose(swServer *serv, swDataHead *info) { php_coro_context *context = coros_list->front(); coros_list->pop_front(); - SwooleG.error = ECONNRESET; + swoole_set_last_error(ECONNRESET); zval_ptr_dtor(&context->coro_params); ZVAL_NULL(&context->coro_params); php_swoole_server_send_resume(serv, context, info->fd); @@ -1923,7 +1923,7 @@ static void php_swoole_onSendTimeout(swTimer *timer, swTimer_node *tnode) zval result; zval *retval = NULL; - SwooleG.error = ETIMEDOUT; + swoole_set_last_error(ETIMEDOUT); ZVAL_FALSE(&result); int fd = (int) (long) context->private_data; @@ -1977,7 +1977,7 @@ static enum swReturn_code php_swoole_server_send_resume(swServer *serv, php_coro goto _fail; } int ret = serv->send(serv, fd, data, length); - if (ret < 0 && SwooleG.error == SW_ERROR_OUTPUT_SEND_YIELD && serv->send_yield) + if (ret < 0 && swoole_get_last_error() == SW_ERROR_OUTPUT_SEND_YIELD && serv->send_yield) { return SW_CONTINUE; } @@ -3149,7 +3149,7 @@ static PHP_METHOD(swoole_server, send) RETURN_FALSE; } ret = serv->send(serv, fd, data, length); - if (ret < 0 && SwooleG.error == SW_ERROR_OUTPUT_SEND_YIELD) + if (ret < 0 && swoole_get_last_error() == SW_ERROR_OUTPUT_SEND_YIELD) { zval_add_ref(zdata); php_swoole_server_send_yield(serv, fd, zdata, return_value); @@ -3657,7 +3657,7 @@ static PHP_METHOD(swoole_server, taskWaitMulti) if (n_task == 0) { - SwooleG.error = SW_ERROR_TASK_DISPATCH_FAIL; + swoole_set_last_error(SW_ERROR_TASK_DISPATCH_FAIL); RETURN_FALSE; } @@ -3803,7 +3803,7 @@ static PHP_METHOD(swoole_server, taskCo) if (n_task == 0) { - SwooleG.error = SW_ERROR_TASK_DISPATCH_FAIL; + swoole_set_last_error(SW_ERROR_TASK_DISPATCH_FAIL); RETURN_FALSE; }