///连接关闭 int CwxProProxyApp::onConnClosed(CwxAppHandler4Msg& , CwxAppHandler4Msg& conn, CwxMsgHead const& , bool& ) { CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(conn.getConnInfo().getSvrId()); pBlock->event().setHostId(conn.getConnInfo().getHostId()); pBlock->event().setConnId(conn.getConnInfo().getConnId()); pBlock->event().setEvent(CwxEventInfo::CONN_CLOSED); m_mqThreadPool->append(pBlock); return 0; }
///分发mq channel的队列消息函数。返回值:0:正常;-1:队列停止 int CwxMqApp::dealMqFetchThreadQueueMsg(CwxMsgQueue* queue, CwxMqApp* app, CwxAppChannel* channel) { int iRet = 0; CwxMsgBlock* block = NULL; CwxAppHandler4Channel* handler = NULL; while (!queue->isEmpty()) { do { iRet = queue->dequeue(block); if (-1 == iRet) return -1; if ((block->event().getEvent() == CwxEventInfo::CONN_CREATED) && (block->event().getSvrId() == SVR_TYPE_QUEUE)) { if (channel->isRegIoHandle(block->event().getIoHandle())) { CWX_ERROR(("Handler[%] is register", block->event().getIoHandle())); break; } if (block->event().getSvrId() == SVR_TYPE_QUEUE) { handler = new CwxMqQueueHandler(app, channel); } else { CWX_ERROR(("Invalid svr_type[%d], close handle[%d]", block->event().getSvrId(), block->event().getIoHandle())); ::close(block->event().getIoHandle()); } handler->setHandle(block->event().getIoHandle()); if (0 != handler->open()) { CWX_ERROR(("Failure to register handler[%d]", handler->getHandle())); delete handler; break; } } else { CWX_ASSERT(block->event().getEvent() == CwxEventInfo::TIMEOUT_CHECK); CWX_ASSERT(block->event().getSvrId() == SVR_TYPE_QUEUE); app->getQueueMgr()->timeout(app->getCurTime()); } } while (0); CwxMsgBlockAlloc::free(block); block = NULL; } if (queue->isDeactived()) return -1; return 0; }
///连接关闭 int CwxMqApp::onConnClosed(CwxAppHandler4Msg& conn) { if ((SVR_TYPE_RECV == conn.getConnInfo().getSvrId()) || (SVR_TYPE_MASTER == conn.getConnInfo().getSvrId())) { CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(conn.getConnInfo().getSvrId()); pBlock->event().setHostId(conn.getConnInfo().getHostId()); pBlock->event().setConnId(conn.getConnInfo().getConnId()); ///设置事件类型 pBlock->event().setEvent(CwxEventInfo::CONN_CLOSED); m_recvThreadPool->append(pBlock); } else if (SVR_TYPE_MONITOR == conn.getConnInfo().getSvrId()) { ///若是监控的连接关闭,则必须释放先前所创建的string对象。 if (conn.getConnInfo().getUserData()) { delete (string*) conn.getConnInfo().getUserData(); conn.getConnInfo().setUserData(NULL); } } else { CWX_ASSERT(0); } return 0; }
int UnistorHandler4Trans::onConnClosed() { list<CwxTaskBoardTask*> tasks; CwxTss* pThrEnv = CwxTss::instance(); CwxMsgBlock* msg = CwxMsgBlockAlloc::malloc(0); msg->event().setHostId(0); msg->event().setConnId(m_uiConnId); m_pApp->getTaskBoard().noticeConnClosed(msg, pThrEnv, tasks); if (!tasks.empty()) { list<CwxTaskBoardTask*>::iterator iter = tasks.begin(); while(iter != tasks.end()){ (*iter)->execute(pThrEnv); iter++; } tasks.clear(); } m_handlers->erase(m_uiConnId); m_bRebuildConn = true; m_bCanTrans = false; return -1; }
///连接建立 int CwxMqApp::onConnCreated(CwxAppHandler4Msg& conn, bool&, bool&) { if ((SVR_TYPE_RECV == conn.getConnInfo().getSvrId()) || (SVR_TYPE_MASTER == conn.getConnInfo().getSvrId())) { CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(conn.getConnInfo().getSvrId()); pBlock->event().setHostId(conn.getConnInfo().getHostId()); pBlock->event().setConnId(conn.getConnInfo().getConnId()); ///设置事件类型 pBlock->event().setEvent(CwxEventInfo::CONN_CREATED); ///将事件添加到消息队列 m_recvThreadPool->append(pBlock); } else if (SVR_TYPE_MONITOR == conn.getConnInfo().getSvrId()) { ///如果是监控的连接建立,则建立一个string的buf,用于缓存不完整的命令 string* buf = new string(); conn.getConnInfo().setUserData(buf); } else { CWX_ASSERT(0); } return 0; }
int CwxMqApp::onConnCreated(CWX_UINT32 uiSvrId, CWX_UINT32 uiHostId, CWX_HANDLE handle, bool&) { CwxMsgBlock* msg = CwxMsgBlockAlloc::malloc(0); msg->event().setSvrId(uiSvrId); msg->event().setHostId(uiHostId); msg->event().setConnId(CWX_APP_INVALID_CONN_ID); msg->event().setIoHandle(handle); msg->event().setEvent(CwxEventInfo::CONN_CREATED); ///将消息放到线程池队列中,有内部的线程调用其处理handle来处理 if (SVR_TYPE_DISP == uiSvrId) { if (m_dispThreadPool->append(msg) <= 1) m_dispChannel->notice(); } else if (SVR_TYPE_QUEUE == uiSvrId) { if (m_mqThreadPool->append(msg) <= 1) m_mqChannel->notice(); } else { CWX_ASSERT(SVR_TYPE_MONITOR == uiSvrId); } return 0; }
void CwxEchoThriftIf::Echo(echo_thrift::EchoData& _return, const std::string& echo_data) { EchoTss* tss = (EchoTss*)CwxTss::instance(); CwxMsgBlock* msg = CwxMsgBlockAlloc::malloc(echo_data.length()); memcpy(msg->wr_ptr(), echo_data.c_str(), echo_data.length()); msg->wr_ptr(echo_data.length()); msg->event().setSvrId(CwxEchoApp::SVR_TYPE_ECHO); msg->event().setEvent(CwxEventInfo::RECV_MSG); msg->event().setConnUserData(&tss->m_queue); tss->m_curId++; msg->event().m_ullArg = tss->m_curId; m_app->GetThreadPool()->append(msg); while(true) { if (tss->m_queue.dequeue(msg) != -1) { if (msg->event().m_ullArg == tss->m_curId) { _return.data.assign(msg->rd_ptr(), msg->length()); CwxMsgBlockAlloc::free(msg); return; } CwxMsgBlockAlloc::free(msg); } else { _return.data = ""; } } }
///时钟函数 void CwxProProxyApp::onTime(CwxTimeValue const& current) { ///调用基类的onTime函数 CwxAppFramework::onTime(current); m_ttCurTime = current.sec(); ///检查超时 static CWX_UINT32 ttTimeBase = 0; ///<时钟回跳的base时钟 static CWX_UINT32 ttLastTime = 0; ///<上次检查时间 bool bClockBack = isClockBack(ttTimeBase, m_ttCurTime); if (bClockBack || (m_ttCurTime >= ttLastTime + 1)) { ttLastTime = m_ttCurTime; if (m_zkThreadPool) { ///发送超时检查到zk线程 CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(SVR_PRO_TYPE_ZK); pBlock->event().setEvent(CwxEventInfo::TIMEOUT_CHECK); m_zkThreadPool->append(pBlock); } if (m_mqThreadPool) { ///发送超时检查到mq线程 CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(SVR_PRO_TYPE_RECV); pBlock->event().setEvent(CwxEventInfo::TIMEOUT_CHECK); m_mqThreadPool->append(pBlock); } } }
///时钟函数 void CwxMqApp::onTime(CwxTimeValue const& current) { ///调用基类的onTime函数 CwxAppFramework::onTime(current); m_ttCurTime = current.sec(); ///检查超时 static CWX_UINT32 ttTimeBase = 0; ///<时钟回跳的base时钟 static CWX_UINT32 ttLastTime = m_ttCurTime; ///<上一次检查的时间 bool bClockBack = isClockBack(ttTimeBase, m_ttCurTime); if (bClockBack || (m_ttCurTime >= ttLastTime + 1)) { ttLastTime = m_ttCurTime; if (m_config.getCommon().m_bMaster) { CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(SVR_TYPE_RECV); pBlock->event().setEvent(CwxEventInfo::TIMEOUT_CHECK); //将超时检查事件,放入事件队列 m_recvThreadPool->append(pBlock); } else { CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(SVR_TYPE_MASTER); pBlock->event().setEvent(CwxEventInfo::TIMEOUT_CHECK); //将超时检查事件,放入事件队列 m_recvThreadPool->append(pBlock); } //若存在分发线程池,则往分发线程池append TIMEOUT_CHECK if (m_dispThreadPool) { CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(SVR_TYPE_DISP); pBlock->event().setEvent(CwxEventInfo::TIMEOUT_CHECK); //将超时检查事件,放入事件队列 m_dispThreadPool->append(pBlock); } //若存在mq程池,则往mq线程池append TIMEOUT_CHECK if (m_mqThreadPool) { CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(0); pBlock->event().setSvrId(SVR_TYPE_QUEUE); pBlock->event().setEvent(CwxEventInfo::TIMEOUT_CHECK); //将超时检查事件,放入事件队列 m_mqThreadPool->append(pBlock); } } }
///往master转发消息 bool UnistorHandler4Trans::transMsg(UnistorTss* , CWX_UINT32 uiTaskId, CwxMsgBlock* msg){ CwxMsgBlock* block = NULL; ///如果没有完成认证的连接,则失败 if (!m_uiAuthConnNum) return false; ///往master转发消息 block = CwxMsgBlockAlloc::malloc(CwxMsgHead::MSG_HEAD_LEN + msg->length()); CwxMsgHead header(msg->event().getMsgHeader()); header.setDataLen(msg->length()); header.setTaskId(uiTaskId); CWX_UINT8 ucAttr=header.getAttr(); header.setAttr(UnistorPoco::clearFromMaster(ucAttr)); memcpy(block->wr_ptr(), header.toNet(), CwxMsgHead::MSG_HEAD_LEN); memcpy(block->wr_ptr() + CwxMsgHead::MSG_HEAD_LEN, msg->rd_ptr(), msg->length()); block->wr_ptr( CwxMsgHead::MSG_HEAD_LEN + msg->length()); block->event().setTaskId(uiTaskId); block->send_ctrl().setMsgAttr(CwxMsgSendCtrl::FAIL_FINISH_NOTICE); if (!m_authConn[msg->event().getConnId()%m_uiAuthConnNum]->putMsg(block)){ CwxMsgBlockAlloc::free(block); return false; } return true; }