From b83d6e03340a9b6df5f1a67c32c7bf2585ae6f08 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 18 Dec 2022 14:23:50 +0800 Subject: [PATCH] 1 --- f8/msgqueue.cc | 133 ++++++++++++++++++++++--------------------------- 1 file changed, 59 insertions(+), 74 deletions(-) diff --git a/f8/msgqueue.cc b/f8/msgqueue.cc index 95340db..b78df6f 100644 --- a/f8/msgqueue.cc +++ b/f8/msgqueue.cc @@ -20,7 +20,7 @@ namespace f8 struct IMMsgNode { - unsigned short msgid; + int msgid; const a8::Args args; IMMsgNode* next = nullptr; @@ -50,8 +50,24 @@ namespace f8 } } - #if 0 - void ProcessMsg(int msgid, const a8::XParams& param) + void Update() + { + if (!im_work_node_ && im_top_node_) { + im_msg_mutex_.lock(); + im_work_node_ = im_top_node_; + im_top_node_ = nullptr; + im_bot_node_ = nullptr; + im_msg_mutex_.unlock(); + } + while (im_work_node_) { + IMMsgNode *pdelnode = im_work_node_; + ProcessMsg(pdelnode->msgid, pdelnode->args); + im_work_node_ = im_work_node_->next; + delete pdelnode; + } + } + + void ProcessMsg(int msgid, const a8::Args& args) { auto itr = msg_handlers.find(msgid); if (itr != msg_handlers.end()) { @@ -59,11 +75,10 @@ namespace f8 struct MsgQueueNode *node = nullptr; struct MsgQueueNode *tmp = nullptr; list_for_each_entry_safe(node, tmp, head, entry) { - node->func(param); + node->func(args); } } } - #endif CallBackHandle RegisterCallBack(int msgid, MsgHandleFunc handle_func) { @@ -82,6 +97,42 @@ namespace f8 return &node->entry; } + void PostMsg(int msgid, const a8::Args args) + { + IMMsgNode *p = new IMMsgNode(args); + p->msgid = msgid; + p->next = nullptr; + im_msg_mutex_.lock(); + if (im_bot_node_) { + im_bot_node_->next = p; + im_bot_node_ = p; + } else { + im_top_node_ = p; + im_bot_node_ = p; + } + im_msg_mutex_.unlock(); +#if 0 + NotifyLoopCond(); +#endif + } + + bool HasMsg() + { + if (!im_work_node_) { + im_msg_mutex_.lock(); + if (!im_work_node_ && im_top_node_) { + im_work_node_ = im_top_node_; + im_top_node_ = nullptr; + im_bot_node_ = nullptr; + } + im_msg_mutex_.unlock(); + } + if (im_work_node_) { + return true; + } + return false; + } + }; void MsgQueue::Init() @@ -118,63 +169,12 @@ namespace f8 void MsgQueue::Update() { -#if 0 - if (!im_work_node_ && im_top_node_) { - im_msg_mutex_->lock(); - im_work_node_ = im_top_node_; - im_top_node_ = nullptr; - im_bot_node_ = nullptr; - im_msg_mutex_->unlock(); - } - while (im_work_node_) { - IMMsgNode *pdelnode = im_work_node_; - switch (im_work_node_->msgid) { - case f8::IM_SysMsgQueue: - { - const a8::XParams* param = (const a8::XParams*)pdelnode->params.param1.GetUserData(); - f8::MsgQueue::Instance()->ProcessMsg(pdelnode->params.sender.GetInt(), - *param - ); - delete param; - } - break; - case IM_ClientSocketDisconnect: - { - PlayerMgr::Instance()->OnClientDisconnect(pdelnode->params); - } - break; - case IM_ExecGM: - { - HandlerMgr::Instance()->ProcGMMsg(pdelnode->params.param3, - pdelnode->params.sender, - pdelnode->params.param1.GetString(), - pdelnode->params.param2.GetString() - ); - } - break; - } - im_work_node_ = im_work_node_->next; - delete pdelnode; - } -#endif + imp_->Update(); } bool MsgQueue::HasMsg() { - #if 0 - if (!im_work_node_) { - im_msg_mutex_.lock(); - if (!im_work_node_ && im_top_node_) { - im_work_node_ = im_top_node_; - im_top_node_ = nullptr; - im_bot_node_ = nullptr; - } - im_msg_mutex_.unlock(); - } - if (im_work_node_) { - return true; - } - #endif + return imp_->HasMsg(); } void MsgQueue::RemoveCallBack(CallBackHandle handle) @@ -198,22 +198,7 @@ namespace f8 void MsgQueue::PostMsg(int msgid, const a8::Args args) { - #if 0 - IMMsgNode *p = new IMMsgNode; - p->msgid = imcmd; - p->params = params; - p->next = nullptr; - im_msg_mutex_->lock(); - if (im_bot_node_) { - im_bot_node_->next = p; - im_bot_node_ = p; - } else { - im_top_node_ = p; - im_bot_node_ = p; - } - im_msg_mutex_->unlock(); - NotifyLoopCond(); - #endif + imp_->PostMsg(msgid, std::move(args)); } }