From a09180d909183c4caaa429464f02aaac19efe230 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 8 Jul 2019 17:22:35 +0800 Subject: [PATCH] 1 --- cpp/dbpool.cc | 8 +++++++- cpp/httpclientpool.cc | 2 +- cpp/msgqueue.cc | 17 +++++++++++++++-- cpp/msgqueue.h | 4 +++- cpp/types.h | 2 ++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cpp/dbpool.cc b/cpp/dbpool.cc index 254173f..8e5aafe 100644 --- a/cpp/dbpool.cc +++ b/cpp/dbpool.cc @@ -319,8 +319,14 @@ namespace f8 void Init() { + auto free_custom_msg = [] (const a8::XParams& params) + { + a8::XParams* param = (a8::XParams*)params.param1.GetUserData(); + DataSet* data_set = (DataSet*)param->param2.GetUserData(); + delete data_set; + }; curr_seqid = 1000001; - exec_async_query_msgid = MsgQueue::Instance()->AllocIMMsgId(); + exec_async_query_msgid = MsgQueue::Instance()->AllocIMMsgId(free_custom_msg); } void UnInit() diff --git a/cpp/httpclientpool.cc b/cpp/httpclientpool.cc index ce95b4d..9703304 100644 --- a/cpp/httpclientpool.cc +++ b/cpp/httpclientpool.cc @@ -276,7 +276,7 @@ namespace f8 void Init() { curr_seqid = 1000001; - exec_async_http_msgid = MsgQueue::Instance()->AllocIMMsgId(); + exec_async_http_msgid = MsgQueue::Instance()->AllocIMMsgId(nullptr); } void UnInit() diff --git a/cpp/msgqueue.cc b/cpp/msgqueue.cc index 480cb63..c6ec471 100644 --- a/cpp/msgqueue.cc +++ b/cpp/msgqueue.cc @@ -18,6 +18,7 @@ namespace f8 public: int curr_im_msgid = 10000; std::map msg_handlers; + std::map custom_free_funcs; ~MsgQueueImp() { @@ -109,9 +110,21 @@ namespace f8 return imp_->RegisterCallBack(msgid, handle_func); } - int MsgQueue::AllocIMMsgId() + int MsgQueue::AllocIMMsgId(CustomIMMsgFreeFunc free_func) { - return ++imp_->curr_im_msgid; + int custom_im_msgid = ++imp_->curr_im_msgid; + if (free_func) { + imp_->custom_free_funcs[custom_im_msgid] = free_func; + } + return custom_im_msgid; + } + + void MsgQueue::FreeCustomIMMsg(a8::XParams& param) + { + auto itr = imp_->custom_free_funcs.find(param.sender.GetInt()); + if (itr != imp_->custom_free_funcs.end()) { + itr->second(param); + } } void MsgQueue::ProcessMsg(int msgid, const a8::XParams& param) diff --git a/cpp/msgqueue.h b/cpp/msgqueue.h index b4d39d6..e22ebf4 100644 --- a/cpp/msgqueue.h +++ b/cpp/msgqueue.h @@ -3,6 +3,7 @@ namespace f8 { typedef std::function MsgHandleFunc; + typedef void CustomIMMsgFreeFunc(const a8::XParams& param); typedef list_head* CallBackHandle; class MsgQueueImp; @@ -21,7 +22,8 @@ namespace f8 void AddDelayMsg(int msgid, a8::XParams param, int milli_seconds); void RemoveCallBack(CallBackHandle handle); CallBackHandle RegisterCallBack(int msgid, MsgHandleFunc handle_func); - int AllocIMMsgId(); + int AllocIMMsgId(CustomIMMsgFreeFunc free_func); + void FreeCustomIMMsg(a8::XParams& param); void ProcessMsg(int msgid, const a8::XParams& param); //线程安全版本 diff --git a/cpp/types.h b/cpp/types.h index 8c31a57..2b8ca7b 100644 --- a/cpp/types.h +++ b/cpp/types.h @@ -12,6 +12,8 @@ namespace f8 bool pending = false; unsigned long saddr = 0; int socket_handle = 0; + time_t create_time = 0; + time_t handle_time = 0; std::string query_str; a8::XObject request; a8::MutableXObject* resp_xobj = nullptr;