int main(int argc, Char* argv[]) { HawkUtil::Init(); AString sAddr = "*:8080"; if (argc >= 2) sAddr = argv[1]; AString sLocalDir = "Files/"; if (argc >= 3) sLocalDir = argv[2]; HawkFileAcceptor sAcceptor; if (sAcceptor.Init(sAddr, sLocalDir)) { HawkFmtPrint("FileAcceptor Is Running......"); sAcceptor.Run(); } else { HawkFmtError("Run FileAcceptor Failed, Address: %s", sAddr.c_str()); } HawkUtil::Stop(); HawkUtil::Release(); return 0; }
HawkProtocol* HawkProtocolManager::CreateProtocol(ProtoType iType) { ProtocolMap::iterator it = m_mRegister.find(iType); if (it == m_mRegister.end()) { HawkFmtError("Protocol Unregister, Type: %d", iType); T_Exception("Protocol Unregister."); return 0; } HawkProtocol* pProto = m_mRegister[iType]->Clone(); return pProto; }
Bool HawkGateProxy::RecvProtocol(GateMsgHeader& sHeader, HawkProtocol*& pProto, Int32 iTimeout) { if (m_pProxyZmq && m_pOctets) { if (m_pProxyZmq->PollEvent(HEVENT_READ, iTimeout)) { //提取消息头 m_pOctets->Clear(); Size_t iSize = (Size_t)m_pOctets->Capacity(); if (!m_pProxyZmq->Recv(m_pOctets->Begin(), iSize)) return false; sHeader = *((GateMsgHeader*)m_pOctets->Begin()); Bool bRecvMore = m_pProxyZmq->IsWaitRecv(); HawkAssert(iSize == sizeof(sHeader) && bRecvMore); if (iSize != sizeof(sHeader) || !bRecvMore) return false; //提取协议内容 m_pOctets->Clear(); iSize = (Size_t)m_pOctets->Capacity(); if (!m_pProxyZmq->Recv(m_pOctets->Begin(), iSize)) return false; //协议解析 m_pOctets->Resize(iSize); try { pProto = P_ProtocolManager->Decode(*m_pOctets); } catch (HawkException& rhsExcep) { //协议解析异常退出 HawkFmtError("Session Decode Protocol Error, Msg: %s", rhsExcep.GetMsg().c_str()); //释放协议 P_ProtocolManager->ReleaseProto(pProto); pProto = 0; return false; } HawkAssert(!m_pProxyZmq->IsWaitRecv()); return pProto != 0; } } return false; }
Bool HawkSqlite::Open(const AString& sDBName) { if (!sqlite3_threadsafe()) { HawkFmtError("Sqlite3 Cannot Support MultiThread."); } if (m_pDBHandle) sqlite3_close((sqlite3*)m_pDBHandle); HawkOSOperator::MakeSureFileName(sDBName); Long lHdlAddr = (Long)&m_pDBHandle; if(sqlite3_open_v2(sDBName.c_str(), reinterpret_cast<sqlite3**>(lHdlAddr),SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,0) != SQLITE_OK) return false; return true; }
Bool HawkGateProxy::RegisterThreads(const vector<UInt32>& vThreads) { Char szCollectAddr[DEFAULT_SIZE] = {0}; Long lCollectId = (Long)m_pCollectZmq->GetHandle(); sprintf(szCollectAddr, "inproc://hawk-gateproxy-%ld", lCollectId); for (Size_t i=0;i<vThreads.size();i++) { UInt32 iThreadId = vThreads[i]; //支持多次调用 if (m_mThreadProxy.find(iThreadId) != m_mThreadProxy.end()) { HawkFmtError("GateProxy Thread Register Duplicate, ThreadId: %u", iThreadId); continue; } HawkZmq* pZmq = P_ZmqManager->CreateZmq(HawkZmq::HZMQ_PUSH); UInt64 iZmqId = HawkZmq::GenValidZid(iThreadId); if (!pZmq->SetIdentity(&iZmqId, (Int32)sizeof(iZmqId))) { P_ZmqManager->CloseZmq(pZmq); return false; } if (!pZmq->Connect(szCollectAddr)) { P_ZmqManager->CloseZmq(pZmq); return false; } m_mThreadProxy[iThreadId] = pZmq; } return true; }