int CServerLinker::Monitor(void) { CServerLinkerMonitor *pMonitor = NULL; pMonitor = new(std::nothrow) CServerLinkerMonitor(); if (NULL == pMonitor) { Q_Printf("%s", Q_EXCEPTION_ALLOCMEMORY); return Q_ERROR_ALLOCMEMORY; } pMonitor->setServerLinker(this); try { CThread objThread; //等待线程启动 m_objMutex.Lock(); objThread.Execute(pMonitor); m_objCond.Wait(&m_objMutex, 1000); m_objMutex.unLock(); } catch (CException &e) { Q_SafeDelete(pMonitor); Q_Printf("exception code %d message %s", e.getErrorCode(), e.getErrorMsg()); return e.getErrorCode(); } return Q_RTN_OK; }
bool CDisposeEvent::onSerciveStartUp(void) { try { CSessionManager::getSingletonPtr()->setLua(m_pLua); luabridge::getGlobal(m_pLua, LUA_EVENT_ONSTARTUP)(CSessionManager::getSingletonPtr(), CSessionManager::getSingletonPtr()->getNetBinary(), CSessionManager::getSingletonPtr()->getSerializeBinary(), CEncrypt::getSingletonPtr()); } catch(luabridge::LuaException &e) { Q_Printf("%s", e.what()); Q_LOG(LOGLV_ERROR, "%s", e.what()); return false; } catch(CQException &e) { Q_Printf("exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); Q_LOG(LOGLV_ERROR, "exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); return false; } return true; }
int CSockPairEvent::initTimerEvent(void) { int iRtn = Q_RTN_OK; m_pTimerBev = bufferevent_socket_new(getBase(), m_objTimerSockPair.getReadFD(), BEV_OPT_CLOSE_ON_FREE); if (NULL == m_pTimerBev) { Q_Printf("%s", "bufferevent_socket_new error."); return Q_RTN_FAILE; } iRtn = m_objTimerBuffer.setBuffer(m_pTimerBev); if (Q_RTN_OK != iRtn) { Q_Printf("%s", "set main buffer error."); return iRtn; } bufferevent_setcb(m_pTimerBev, timerReadCB, NULL, eventCB, this); iRtn = bufferevent_enable(m_pTimerBev, EV_READ); if (Q_RTN_OK != iRtn) { Q_Printf("%s", "bufferevent_enable error."); return iRtn; } return Q_RTN_OK; }
int CSockPairEvent::initExitMonitor(unsigned int uiMS) { timeval tVal; evutil_timerclear(&tVal); if (uiMS >= 1000) { tVal.tv_sec = (uiMS / 1000); tVal.tv_usec = ((uiMS % 1000) * (1000)); } else { tVal.tv_usec = (uiMS * 1000); } m_pExitEvent = event_new(getBase(), -1, EV_PERSIST, exitMonitorCB, this); if (NULL == m_pExitEvent) { Q_Printf("%s", "event_new error"); return Q_RTN_FAILE; } if (Q_RTN_OK != event_add(m_pExitEvent, &tVal)) { Q_Printf("%s", "event_add error"); event_free(m_pExitEvent); m_pExitEvent = NULL; return Q_RTN_FAILE; } return Q_RTN_OK; }
void CWorkThreadEvent::addServerLinker(struct event_base *pMainBase, CSessionManager *pSessionManager, OrderMsg &stOrderMsg) { struct bufferevent *pBev = NULL; CSession *pSession = NULL; CServerLinker *pServerLinker = (CServerLinker *)stOrderMsg.pHandle; if (NULL == pServerLinker) { Q_Printf("%s", Q_EXCEPTION_NULLPOINTER); return; } if (Q_INVALID_SOCK == pServerLinker->getSock()) { Q_Printf("%s", "invalid socket"); return; } (void)evutil_make_socket_nonblocking(pServerLinker->getSock()); pBev = bufferevent_socket_new(pMainBase, pServerLinker->getSock(), BEV_OPT_CLOSE_ON_FREE); if (NULL == pBev) { Q_Printf("%s", "bufferevent_socket_new error."); return; } if (Q_RTN_OK != pSessionManager->addSession(pBev)) { bufferevent_free(pBev); Q_Printf("%s", "add session error."); return; } bufferevent_setcb(pBev, workThreadReadCB, NULL, workThreadEventCB, pSessionManager); if (Q_RTN_OK != bufferevent_enable(pBev, EV_READ | EV_WRITE)) { pSessionManager->dellSession(pBev); bufferevent_free(pBev); pBev = NULL; Q_Printf("%s", "bufferevent_enable error."); return; } pSessionManager->addServerLinker(pServerLinker->getLinkerName(), pBev); pSession = pSessionManager->getSession(pBev); pSession->setHandle(pServerLinker); pSession->setServerLinker(true); pServerLinker->setLinked(true); pSessionManager->getInterface()->onLinkedServer(pSession); }
void CInitBase::Init(void) { m_pCfg = event_config_new(); if (NULL == m_pCfg) { Q_EXCEPTION(Q_RTN_FAILE, "%s", "event_config_new error."); } #ifdef Q_IOCP evthread_use_windows_threads(); event_config_set_flag(m_pCfg, EVENT_BASE_FLAG_STARTUP_IOCP); #endif m_pBase = event_base_new_with_config(m_pCfg); if (NULL == m_pBase) { Q_EXCEPTION(Q_RTN_FAILE, "%s", "event_base_new error."); } #ifdef Q_IOCP Q_Printf("event version %s, using %s", event_get_version(), "IOCP"); #else Q_Printf("event version %s, using %s", event_get_version(), event_base_get_method(m_pBase)); #endif }
void CSockPairEvent::exitMonitorCB(evutil_socket_t, short, void *arg) { CSockPairEvent *pParam = (CSockPairEvent*)arg; switch(pParam->getRunStatus()) { case RUNSTATUS_STOPPING: { if (!pParam->getRunOnStop()) { Q_Printf("ready stop thread %u.", Q_ThreadID()); pParam->onStop(); pParam->setRunOnStop(true); } } break; case RUNSTATUS_STOPPED: { Q_Printf("stop thread %u successfully.", Q_ThreadID()); event_base_loopbreak(pParam->getBase()); } break; default: break; } }
void Run(void) { int iRtn = Q_RTN_OK; m_pLinker->setMonitorRun(true); m_pLinker->getMutex()->Lock(); m_pLinker->getCond()->Signal(); m_pLinker->getMutex()->unLock(); while(!(m_pLinker->getStop())) { if (!m_pLinker->getLinked()) { Q_Printf("try link %s on port %d...", m_pLinker->getIp(), m_pLinker->getPort()); iRtn = TryLink(); if (Q_RTN_OK != iRtn) { Q_Printf("try link %s on port %d error.", m_pLinker->getIp(), m_pLinker->getPort()); } } m_pLinker->getCloseMutex()->Lock(); m_pLinker->getCloseCond()->Wait(m_pLinker->getCloseMutex(), 5 * 1000); m_pLinker->getCloseMutex()->unLock(); } m_pLinker->setMonitorRun(false); m_pLinker->getMutex()->Lock(); m_pLinker->getCond()->Signal(); m_pLinker->getMutex()->unLock(); };
Q_SOCK CLog::addLoger(CLoger *pLoger) { if (NULL == pLoger) { return Q_INVALID_SOCK; } if (!getIsRun()) { Q_Printf("%s", "loger not run."); Q_SafeDelete(pLoger); return Q_INVALID_SOCK; } LogerInfo objLogInfo; objLogInfo.pLoger = pLoger; objLogInfo.pPair = new(std::nothrow) CSockPair(); if (NULL == objLogInfo.pPair) { Q_Printf("%s", Q_EXCEPTION_ALLOCMEMORY); objLogInfo.FreeAll(); return Q_INVALID_SOCK; } objLogInfo.pBuffer = new(std::nothrow) CEventBuffer(); if (NULL == objLogInfo.pBuffer) { Q_Printf("%s", Q_EXCEPTION_ALLOCMEMORY); objLogInfo.FreeAll(); return Q_INVALID_SOCK; } m_objMutex.Lock(); int iRtn = sendMainMsg((const char*)(&objLogInfo), sizeof(objLogInfo)); m_objMutex.unLock(); if (Q_RTN_OK != iRtn) { Q_Printf("%s", "send message error"); objLogInfo.FreeAll(); return Q_INVALID_SOCK; } return objLogInfo.pPair->getWriteFD(); }
void CWorkThreadEvent::onMainRead(struct SockPairEventParam *pParam) { struct bufferevent *pBev = NULL; Q_SOCK iFD = Q_INVALID_SOCK; CSessionManager *pSessionManager = (CSessionManager *)(pParam->pUserDate); while(Q_GetEventValue<Q_SOCK>(pParam->pEventBuf, iFD)) { if (Q_INVALID_SOCK == iFD) { Q_Printf("%s", "invalid socket"); continue; } (void)evutil_make_socket_nonblocking(iFD); pBev = bufferevent_socket_new(pParam->pMainBase, iFD, BEV_OPT_CLOSE_ON_FREE); if (NULL == pBev) { evutil_closesocket(iFD); Q_Printf("%s", "bufferevent_socket_new error."); continue; } if (Q_RTN_OK != pSessionManager->addSession(pBev)) { evutil_closesocket(iFD); bufferevent_free(pBev); Q_Printf("%s", "add session error."); continue; } bufferevent_setcb(pBev, workThreadReadCB, NULL, workThreadEventCB, pSessionManager); if (Q_RTN_OK != bufferevent_enable(pBev, EV_READ | EV_WRITE)) { evutil_closesocket(iFD); pSessionManager->dellSession(pBev); bufferevent_free(pBev); Q_Printf("%s", "bufferevent_enable error."); continue; } } }
int CDisposeEvent::initLua(const std::string &strLua) { m_pLua = luaL_newstate(); if (NULL == m_pLua) { return Q_ERROR_ALLOCMEMORY; } luaL_openlibs(m_pLua); m_objReg2Lua.setLState(m_pLua); m_objReg2Lua.Register(); int iRtn = luaL_dofile(m_pLua, strLua.c_str()); if (Q_RTN_OK != iRtn) { const char *pError = lua_tostring(m_pLua, -1); std::string strLuaError = ((NULL == pError) ? "" : pError); if (NULL != m_pLua) { lua_close(m_pLua); m_pLua = NULL; } Q_Printf("%s", strLuaError.c_str()); } return iRtn; }
bool CHttpParser::setHttpRequest(struct evhttp_request *req) { if (NULL != m_pEventBuf) { evbuffer_free(m_pEventBuf); m_pEventBuf = NULL; } m_strPostMsg.clear(); m_strQuery.clear(); m_pEventBuf = evbuffer_new(); if (NULL == m_pEventBuf) { Q_Printf("%s", "evbuffer_new error."); return false; } m_Req = req; struct evbuffer *pBuf = evhttp_request_get_input_buffer(m_Req); size_t iLens = evbuffer_get_length(pBuf); if (iLens > 0) { m_strPostMsg.append((const char *)evbuffer_pullup(pBuf, (ev_ssize_t)iLens), iLens); evbuffer_drain(pBuf, iLens); } const struct evhttp_uri *pUri = evhttp_request_get_evhttp_uri(m_Req); const char *pszQuery = evhttp_uri_get_query(pUri); m_strQuery = (NULL == pszQuery ? "" : pszQuery); return true; }
void CSockPairEvent::eventCB(struct bufferevent *, short event, void *arg) { CSockPairEvent *pParam = (CSockPairEvent*)arg; int iSockError = EVUTIL_SOCKET_ERROR(); if (event & BEV_EVENT_TIMEOUT) { return; } if (event & BEV_EVENT_ERROR) { #ifdef Q_OS_WIN if (WSA_IO_PENDING == iSockError) // WSAEWOULDBLOCK { return; } #else if (EAGAIN == iSockError) { return; } #endif } pParam->setRunStatus(RUNSTATUS_STOPPING); Q_Printf("an error happend, event is %d. error code %d, message %s exit loop", event, iSockError, evutil_socket_error_to_string(iSockError)); }
CDisposeEvent::~CDisposeEvent(void) { if (NULL != m_pLua) { Q_Printf("%s", "close lua vm..."); lua_close(m_pLua); m_pLua = NULL; } }
int CWorkThreadEvent::setTimer(unsigned int uiMS) { if (Q_INIT_NUMBER == uiMS) { return Q_RTN_OK; } timeval tVal; evutil_timerclear(&tVal); if (uiMS >= 1000) { tVal.tv_sec = uiMS / 1000; tVal.tv_usec = (uiMS % 1000) * (1000); } else { tVal.tv_usec = (uiMS * 1000); } m_pEvent = event_new(getBase(), -1, EV_PERSIST, workThreadTimerCB, &m_objSessionManager); if (NULL == m_pEvent) { Q_Printf("%s", "event_new error"); return Q_RTN_FAILE; } (void)event_priority_set(m_pEvent, Priority_Hight); if (Q_RTN_OK != event_add(m_pEvent, &tVal)) { Q_Printf("%s", "event_add error"); event_free(m_pEvent); m_pEvent = NULL; return Q_RTN_FAILE; } m_objSessionManager.setTimer(uiMS); return Q_RTN_OK; }
int CSockPair::creatListener(Q_SOCK &Listener) const { CNETAddr objListen_addr; int iRtn = Q_RTN_OK; iRtn = objListen_addr.setAddr("127.0.0.1", 0); if (Q_RTN_OK != iRtn) { Q_Printf("set socket addr error. error code %d.", iRtn); return iRtn; } Listener = socket(AF_INET, SOCK_STREAM, 0); if (Q_INVALID_SOCK == Listener) { iRtn = Q_SockError(); Q_Printf("create socket error. error code %d, message %s ", iRtn, Q_SockError2Str(iRtn)); return Q_RTN_FAILE; } if (Q_RTN_FAILE == bind(Listener, objListen_addr.getAddr(), (int)objListen_addr.getAddrSize())) { iRtn = Q_SockError(); Q_Printf("bind port error. error code %d, message %s ", iRtn, Q_SockError2Str(iRtn)); evutil_closesocket(Listener); return Q_RTN_FAILE; } if (Q_RTN_FAILE == listen(Listener, 1)) { iRtn = Q_SockError(); Q_Printf("listen error. error code %d, message %s ", iRtn, Q_SockError2Str(iRtn)); evutil_closesocket(Listener); return Q_RTN_FAILE; } return Q_RTN_OK; }
void CIniFile::readInfo(void) { m_lstNodeInfo.clear(); std::fstream inStream(m_strFile.c_str(), std::ios::in); if (!inStream.good()) { Q_Printf("open file %s error.", m_strFile.c_str()); inStream.close(); return; } char pBuffer[Q_ONEK]; std::string strTmp; std::string strNode; while(inStream.good()) { Q_Zero(pBuffer, sizeof(pBuffer)); inStream.getline(pBuffer, (std::streamsize)(sizeof(pBuffer) - 1)); strTmp = std::string(pBuffer); strTmp = Q_Trim(strTmp); if (strTmp.empty() || isNote(strTmp)) { continue; } //去掉注释 removeNote(strTmp); strTmp = Q_Trim(strTmp); if (strTmp.empty()) { continue; } if (isNode(strTmp)) { strNode = getNode(strTmp); continue; } if (isKey(strTmp)) { setStringValue(strNode.c_str(), getKey(strTmp).c_str(), getVal(strTmp).c_str()); } } inStream.close(); return; }
void CDisposeEvent::onLinkedOther(class CSession *pSession) { try { luabridge::getGlobal(m_pLua, LUA_EVENT_ONLINKEDOTHER)(pSession); } catch(luabridge::LuaException &e) { Q_Printf("%s", e.what()); Q_LOG(LOGLV_ERROR, "%s", e.what()); } catch(CQException &e) { Q_Printf("exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); Q_LOG(LOGLV_ERROR, "exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); } }
void CDisposeEvent::onHttpRead(class CHttpParser *pBuffer) { try { luabridge::getGlobal(m_pLua, LUA_EVENT_ONHTTPREAD)(pBuffer); } catch(luabridge::LuaException &e) { Q_Printf("%s", e.what()); Q_LOG(LOGLV_ERROR, "%s", e.what()); } catch(CQException &e) { Q_Printf("exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); Q_LOG(LOGLV_ERROR, "exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); } }
void CDisposeEvent::onSerciveShutDown(void) { try { luabridge::getGlobal(m_pLua, LUA_EVENT_ONSHUTDOWN)(); } catch(luabridge::LuaException &e) { Q_Printf("%s", e.what()); Q_LOG(LOGLV_ERROR, "%s", e.what()); } catch(CQException &e) { Q_Printf("exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); Q_LOG(LOGLV_ERROR, "exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); } }
void CDisposeEvent::onDebug(const char *pszMsg, const size_t &iLens) { try { CSessionManager::getSingletonPtr()->getNetBinary()->setBuffer(pszMsg, iLens); luabridge::getGlobal(m_pLua, LUA_EVENT_ONDEBUG)(); } catch(luabridge::LuaException &e) { Q_Printf("%s", e.what()); Q_LOG(LOGLV_ERROR, "%s", e.what()); } catch(CQException &e) { Q_Printf("exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); Q_LOG(LOGLV_ERROR, "exception. code %d, message %s", e.getErrorCode(), e.getErrorMsg()); } }
void CLog::onMainRead(SockPairEventParam *pParam) { LogerInfo *pLogerInfo = NULL; while(NULL != (pLogerInfo = Q_GetEventValue<LogerInfo>(pParam->pEventBuf))) { //Ìí¼Ó½øÑ»· pLogerInfo->pBev = bufferevent_socket_new(pParam->pMainBase, pLogerInfo->pPair->getReadFD(), BEV_OPT_CLOSE_ON_FREE); if (NULL == pLogerInfo->pBev) { Q_Printf("%s", "bufferevent_socket_new error."); pLogerInfo->FreeAll(); Q_SafeDelete(pLogerInfo); continue; } if (Q_RTN_OK != pLogerInfo->pBuffer->setBuffer(pLogerInfo->pBev)) { Q_Printf("%s", "set buffer error."); pLogerInfo->FreeAll(); Q_SafeDelete(pLogerInfo); continue; } bufferevent_setcb(pLogerInfo->pBev, LogerReadCB, NULL, NULL, pLogerInfo); if (Q_RTN_OK != bufferevent_enable(pLogerInfo->pBev, EV_READ)) { Q_Printf("%s", "bufferevent_enable error."); pLogerInfo->FreeAll(); Q_SafeDelete(pLogerInfo); continue; } ((std::list<LogerInfo *>*)(pParam->pUserDate))->push_back(pLogerInfo); } }
int CEncrypt::setRSAKey(const char *pszPubKeyFile, const char *pszPriKeyFile, const char *pszRandKeyFile) { if ((NULL != pszPubKeyFile) && (0 != strlen(pszPubKeyFile))) { if (Q_RTN_OK != m_objRSAKey.loadPublicKey(pszPubKeyFile)) { Q_Printf("load rsa public key from file %s error.", pszPubKeyFile); return Q_RTN_FAILE; } } if ((NULL != pszPriKeyFile) && (0 != strlen(pszPriKeyFile))) { if (Q_RTN_OK != m_objRSAKey.loadPrivateKey(pszPriKeyFile)) { Q_Printf("load rsa private key from file %s error.", pszPriKeyFile); return Q_RTN_FAILE; } } if ((NULL != pszRandKeyFile) && (0 != strlen(pszRandKeyFile))) { if (Q_RTN_OK != m_objRSAKey.loadRandom(pszRandKeyFile)) { Q_Printf("load rsa random key from file %s error.", pszRandKeyFile); return Q_RTN_FAILE; } } m_objRSA.setKey(&m_objRSAKey); return Q_RTN_OK; }
int CServerLinker::Link(void) { closeSock(); int iRtn = Q_RTN_OK; CNETAddr objAddr; int iKeepAlive = 1; iRtn = objAddr.setAddr(m_strDesIp.c_str(), m_usDesPort); if (Q_RTN_OK != iRtn) { return iRtn; } m_Sock = socket(AF_INET, SOCK_STREAM, 0); if (Q_INVALID_SOCK == m_Sock) { iRtn = Q_SockError(); Q_Printf("create socket error. error code %d, message %s", iRtn, Q_SockError2Str(iRtn)); return iRtn; } iRtn = connect(m_Sock, objAddr.getAddr(), objAddr.getAddrSize()); if (Q_RTN_OK != iRtn) { iRtn = Q_SockError(); Q_Printf("connect %s on port %d error. error code %d, message %s", objAddr.getIp().c_str(), objAddr.getPort(), iRtn, Q_SockError2Str(iRtn)); return iRtn; } (void)setsockopt(m_Sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&iKeepAlive, sizeof(iKeepAlive)); return Q_RTN_OK; }
bool CLuaBinary::setVal(const char *pszBuf, const size_t iLens) { try { m_objWritBuffer.pushBuff(pszBuf, iLens); } catch (CQException &e) { Q_Printf("%s", e.getErrorMsg()); return false; } return true; }
bool CDBLoger::Link(void) { try { m_objLinker.open(m_objUrl); } catch (CQException &e) { Q_Printf("%s", e.getErrorMsg()); return false; } return true; }
bool CDBLoger::createTable(const std::string &strName) { try { (void)m_objLinker.execDML(Q_FormatStr("CREATE TABLE IF NOT EXISTS %s like %s;", strName.c_str(), LOG_TABLE).c_str()); } catch(CQException &e) { Q_Printf("%s", e.getErrorMsg()); return false; } return true; }
int CSockInit::Init(void) const { #ifdef Q_OS_WIN WORD wVersionReq; WSAData wsData; wVersionReq = MAKEWORD(2, 2); int iRtn = WSAStartup(wVersionReq, &wsData); if (Q_RTN_OK != iRtn) { iRtn = Q_SockError(); Q_Printf("WSAStartup error. error code %d, message %s", iRtn, Q_SockError2Str(iRtn)); return iRtn; } #endif return Q_RTN_OK; }
/************************************************************************ * Function name:Q_SockWrite * Description :向socket中写入发送的内容 * IN :fd --socket句柄 pBuf --要发送的数据 iLens --数据长度 * OUT :NONE * Return :Q_RTN_OK --成功 其他 --失败 * Make By :lqf/[email protected] * Date Time :2014/04/30 * Modification * ......record :first program ************************************************************************/ int Q_SockWrite(const Q_SOCK &fd, const char *pBuf, const size_t &iLens) { int iSendSize = Q_INIT_NUMBER; size_t iSendTotalSize = Q_INIT_NUMBER; do { iSendSize = send(fd, pBuf + iSendTotalSize, (int)(iLens - iSendTotalSize), 0); if (iSendSize <= 0) { int iRtn = Q_SockError(); Q_Printf("send error. error code %d, message %s ", iRtn, Q_SockError2Str(iRtn)); return Q_RTN_FAILE; } iSendTotalSize += (size_t)iSendSize; } while (iLens > iSendTotalSize); return Q_RTN_OK; }
/************************************************************************ * Function name:Q_SockRead * Description :从socket中读取内容 * IN :fd --socket句柄 iLens --读取长度 * OUT :pBuf --读取数据存放地 * Return :Q_RTN_OK --成功 其他 --失败 * Make By :lqf/[email protected] * Date Time :2014/04/30 * Modification * ......record :first program ************************************************************************/ int Q_SockRead(const Q_SOCK &fd, char *pBuf, const size_t &iLens) { int iRecvSize = Q_INIT_NUMBER; size_t iRecvTotalSize = Q_INIT_NUMBER; do { iRecvSize = recv(fd, pBuf + iRecvTotalSize, (int)(iLens - iRecvTotalSize), 0); if (iRecvSize <= 0) { int iRtn = Q_SockError(); Q_Printf("recv error. error code %d, message %s ", iRtn, Q_SockError2Str(iRtn)); return Q_RTN_FAILE; } iRecvTotalSize += (size_t)iRecvSize; } while (iRecvTotalSize < iLens); return Q_RTN_OK; }