From 2a57961b590c624e6bc6b889667aedc7699d7a3b Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 30 Jun 2021 21:22:57 +0800 Subject: [PATCH] support error handler callback --- src/sf_global.c | 2 +- src/sf_global.h | 11 +++++++++++ src/sf_proto.h | 2 +- src/sf_types.h | 2 ++ src/sf_util.c | 1 - src/sf_util.h | 7 ++++++- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/sf_global.c b/src/sf_global.c index c96022a..3f45cbc 100644 --- a/src/sf_global.c +++ b/src/sf_global.c @@ -39,7 +39,7 @@ SFGlobalVariables g_sf_global_vars = { SF_DEF_MAX_PACKAGE_SIZE, SF_DEF_MIN_BUFF_SIZE, SF_DEF_MAX_BUFF_SIZE, 0, SF_DEF_THREAD_STACK_SIZE, 0, 0, 0, {'\0'}, {'\0'}, {SF_DEF_SYNC_LOG_BUFF_INTERVAL, false}, - {0, 0} + {0, 0}, NULL, {NULL, 0} }; SFContext g_sf_context = { diff --git a/src/sf_global.h b/src/sf_global.h index 529dbac..5f10157 100644 --- a/src/sf_global.h +++ b/src/sf_global.h @@ -54,6 +54,8 @@ typedef struct sf_global_variables { SFLogConfig error_log; SFConnectionStat connection_stat; + sf_error_handler_callback error_handler; + string_t empty; } SFGlobalVariables; typedef struct sf_context_ini_config { @@ -84,6 +86,9 @@ extern SFContext g_sf_context; #define SF_G_CONN_CURRENT_COUNT g_sf_global_vars.connection_stat.current_count #define SF_G_CONN_MAX_COUNT g_sf_global_vars.connection_stat.max_count +#define SF_G_ERROR_HANDLER g_sf_global_vars.error_handler +#define SF_G_EMPTY_STRING g_sf_global_vars.empty + #define SF_WORK_THREADS(sf_context) sf_context.work_threads #define SF_ALIVE_THREAD_COUNT(sf_context) sf_context.thread_count #define SF_THREAD_INDEX(sf_context, tdata) (int)(tdata - sf_context.thread_data) @@ -207,6 +212,12 @@ static inline void sf_set_global_base_path(const char *base_path) SF_G_BASE_PATH_INITED = true; } +static inline void sf_set_error_handler( + sf_error_handler_callback error_handler) +{ + SF_G_ERROR_HANDLER = error_handler; +} + #ifdef __cplusplus } #endif diff --git a/src/sf_proto.h b/src/sf_proto.h index 134610e..45d0121 100644 --- a/src/sf_proto.h +++ b/src/sf_proto.h @@ -103,7 +103,7 @@ if (client_ctx->auth.enabled) { \ out_bytes += FCFS_AUTH_SESSION_ID_LEN; \ memcpy(the_req_start, client_ctx->auth.ctx-> \ - session_id, FCFS_AUTH_SESSION_ID_LEN); \ + session.id, FCFS_AUTH_SESSION_ID_LEN); \ the_req_start += FCFS_AUTH_SESSION_ID_LEN; \ } \ if (the_req_id > 0) { \ diff --git a/src/sf_types.h b/src/sf_types.h index 4a2bb5f..69d37e3 100644 --- a/src/sf_types.h +++ b/src/sf_types.h @@ -43,6 +43,8 @@ typedef int (*sf_recv_timeout_callback)(struct fast_task_info *task); /* calback for release iovec buffer */ typedef void (*sf_release_buffer_callback)(struct fast_task_info *task); +typedef int (*sf_error_handler_callback)(const int errnum); + typedef struct sf_context { char name[64]; struct nio_thread_data *thread_data; diff --git a/src/sf_util.c b/src/sf_util.c index 99c696f..f8eb7f1 100644 --- a/src/sf_util.c +++ b/src/sf_util.c @@ -22,7 +22,6 @@ #include #include #include -#include "sf_global.h" #include "sf_define.h" #include "sf_util.h" diff --git a/src/sf_util.h b/src/sf_util.h index fc3591f..164f2ce 100644 --- a/src/sf_util.h +++ b/src/sf_util.h @@ -23,6 +23,7 @@ #include "fastcommon/sched_thread.h" #include "sf_define.h" #include "sf_types.h" +#include "sf_global.h" #ifdef DEBUG_FLAG /*only for format check*/ @@ -127,8 +128,12 @@ static inline int sf_unify_errno(const int errnum) } } -static inline int sf_localize_errno(const int errnum) +static inline int sf_localize_errno(int errnum) { + if (SF_G_ERROR_HANDLER != NULL) { + errnum = SF_G_ERROR_HANDLER(errnum); + } + switch (errnum) { case SF_ERROR_EBUSY: return EBUSY;