void CwxEchoChannelEventHandler::replyMessage() { ///设置echo回复的消息类型,为请求的消息类型+1 m_recvMsgData->event().getMsgHeader().setMsgType(m_recvMsgData->event().getMsgHeader().getMsgType() + 1); ///设置echo回复的数据包长度 m_recvMsgData->event().getMsgHeader().setDataLen(m_recvMsgData->length()); ///创建回复的数据包 CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(m_recvMsgData->length() + CwxMsgHead::MSG_HEAD_LEN); ///拷贝数据包的包头 memcpy(pBlock->wr_ptr(), m_recvMsgData->event().getMsgHeader().toNet(), CwxMsgHead::MSG_HEAD_LEN); ///滑动block的写指针 pBlock->wr_ptr(CwxMsgHead::MSG_HEAD_LEN); ///拷贝数据包的数据 memcpy(pBlock->wr_ptr(), m_recvMsgData->rd_ptr(), m_recvMsgData->length()); ///滑动block的写指针 pBlock->wr_ptr(m_recvMsgData->length()); if (!putMsg(pBlock)) { CWX_ERROR(("Failure to put message")); CwxMsgBlockAlloc::free(pBlock); } m_ullMsgNum ++; if (m_ullMsgNum && !(m_ullMsgNum%10000)) { char szBuf[64]; CwxCommon::toString(m_ullMsgNum, szBuf, 10); CWX_INFO(("Recv echo message num:%s", szBuf)); } }
int CwxMqApp::monitorStats(char const* buf, CWX_UINT32 uiDataLen, CwxAppHandler4Msg& conn) { string* strCmd = (string*) conn.getConnInfo().getUserData(); strCmd->append(buf, uiDataLen); CwxMsgBlock* msg = NULL; string::size_type end = 0; do { CwxCommon::trim(*strCmd); end = strCmd->find('\n'); if (string::npos == end) { if (strCmd->length() > 10) { //无效的命令 strCmd->erase(); ///清空接受到的命令 ///回复信息 msg = CwxMsgBlockAlloc::malloc(1024); strcpy(msg->wr_ptr(), "ERROR\r\n"); msg->wr_ptr(strlen(msg->wr_ptr())); } else { return 0; } } else { if (memcmp(strCmd->c_str(), "stats", 5) == 0) { strCmd->erase(); ///清空接受到的命令 CWX_UINT32 uiLen = packMonitorInfo(); msg = CwxMsgBlockAlloc::malloc(uiLen); memcpy(msg->wr_ptr(), m_szBuf, uiLen); msg->wr_ptr(uiLen); } else if (memcmp(strCmd->c_str(), "quit", 4) == 0) { return -1; } else { //无效的命令 strCmd->erase(); ///清空接受到的命令 ///回复信息 msg = CwxMsgBlockAlloc::malloc(1024); strcpy(msg->wr_ptr(), "ERROR\r\n"); msg->wr_ptr(strlen(msg->wr_ptr())); } } } while (0); msg->send_ctrl().setConnId(conn.getConnInfo().getConnId()); msg->send_ctrl().setSvrId(CwxMqApp::SVR_TYPE_MONITOR); msg->send_ctrl().setHostId(0); msg->send_ctrl().setMsgAttr(CwxMsgSendCtrl::NONE); if (-1 == sendMsgByConn(msg)) { CWX_ERROR(("Failure to send monitor reply")); CwxMsgBlockAlloc::free(msg); return -1; } return 0; }
///echo请求的处理函数 int CwxEchoEventHandler::onRecvMsg(CwxMsgBlock*& msg, CwxTss* ) { ///设置echo回复的消息类型,为请求的消息类型+1 msg->event().getMsgHeader().setMsgType(msg->event().getMsgHeader().getMsgType() + 1); ///设置echo回复的数据包长度 msg->event().getMsgHeader().setDataLen(msg->length()); ///创建回复的数据包 CwxMsgBlock* pBlock = CwxMsgBlockAlloc::malloc(msg->length() + CwxMsgHead::MSG_HEAD_LEN); ///拷贝数据包的包头 memcpy(pBlock->wr_ptr(), msg->event().getMsgHeader().toNet(), CwxMsgHead::MSG_HEAD_LEN); ///滑动block的写指针 pBlock->wr_ptr(CwxMsgHead::MSG_HEAD_LEN); ///拷贝数据包的数据 memcpy(pBlock->wr_ptr(), msg->rd_ptr(), msg->length()); ///滑动block的写指针 pBlock->wr_ptr(msg->length()); ///设置回复消息的发送控制信息 pBlock->send_ctrl().reset(); ///设置回复消息对应连接的svr-id pBlock->send_ctrl().setSvrId(msg->event().getSvrId()); ///设置回复消息对应连接的host-id pBlock->send_ctrl().setHostId(msg->event().getHostId()); ///设置回复消息的连接id pBlock->send_ctrl().setConnId(msg->event().getConnId()); ///回复消息 if (0 != this->m_pApp->sendMsgByConn(pBlock)) { CWX_ERROR(("Failure to send msg")); return -1; } m_ullMsgNum ++; if (m_ullMsgNum && !(m_ullMsgNum%10000)) { char szBuf[64]; CwxCommon::toString(m_ullMsgNum, szBuf, 10); CWX_INFO(("Recv echo message num:%s", szBuf)); } return 1; }
///往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; }
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 = ""; } } }