Exemplo n.º 1
0
PVoid hawk_EchoClientRoutine(void* pArg)
{
	HawkSession session;
	if (session.Init(g_IpAddr))
	{
		while (g_Running)
		{
			//更新
			if (!session.Tick(PROTO_TIME))
				break;

			//发送协议
			Int32 iType = HawkRand::RandInt(PROTO_A, PROTO_C);
			if (iType == PROTO_A)
			{
				ProtoA sCmd;
				session.SendProtocol(&sCmd);
			}
			else if (iType == PROTO_B)
			{
				ProtoB sCmd;
				session.SendProtocol(&sCmd);
			}
			else if (iType == PROTO_C)
			{
				ProtoC sCmd;
				session.SendProtocol(&sCmd);
			}			

			//接收协议
			Protocol* pProto = 0;
			while (session.DecodeProtocol(pProto) && pProto)
			{
#if (PRINT_PROTO == 1)
				ProtoType iType = pProto->GetType();
				if (iType == PROTO_A)
				{
					ProtoA* pA = (ProtoA*)pProto;
					HawkFmtPrint("Msg: %s",pA->m_sMsg.c_str());
				}
				else if (iType == PROTO_B)
				{
					ProtoB* pB = (ProtoB*)pProto;
					HawkFmtPrint("Size: %d, Time: %d",pB->m_iSize, pB->m_iTime);
				}
				else if (iType == PROTO_C)
				{
					ProtoC* pC = (ProtoC*)pProto;
					HawkFmtPrint("X: %f, Y: %f, Z: %f",pC->m_fX, pC->m_fY, pC->m_fZ);
				}
#endif
				P_ProtocolManager->ReleaseProto(pProto);
			}

			HawkSleep(PROTO_TIME);
		}
	}

	return 0;
}
Exemplo n.º 2
0
	Bool HawkLogServer::Run()
	{
		if (!m_pLogFile && !m_pLogDB)
			return false;

		if (m_bRunning || !m_pRecvBuf || !m_pLogCache)
			return false;

		m_bRunning = true;
		UInt32 iFlushTime = HawkOSOperator::GetTickCount();
			
		while(m_bRunning)
		{
			UInt32 iCurTime = HawkOSOperator::GetTickCount();
			if (!m_sSocket.UpdateEvent(HEVENT_READ))
			{
				if (iCurTime - iFlushTime >= (UInt32)m_iCacheTime)
				{
					FlushLogs();
					iFlushTime = iCurTime;
				}
				else
				{
					HawkSleep(DEFAULT_SLEEP);
				}
				continue;
			}

			m_pRecvBuf->Clear();
			Size_t iRecv = (Size_t)m_pRecvBuf->Capacity();

			SocketAddr sAddr;
			if(!m_sSocket.ReceiveFrom(m_pRecvBuf->Begin(), iRecv, sAddr))
				continue;

			m_pRecvBuf->Resize((UInt32)iRecv);
			Protocol* pProto = 0;
			try
			{
				pProto = P_ProtocolManager->Decode(*m_pRecvBuf);
				if (!pProto) continue;
			}
			catch (HawkException& rhsExcep)
			{
#ifdef _DEBUG
				HawkFmtPrint("Exception: %s", rhsExcep.GetMsg().c_str());
#endif
				continue;
			}

			ProtoType eType = pProto->GetType();
			if(eType == SysProtocol::SYS_LOG_MSG)
			{
				SysProtocol::Sys_LogMsg* pCmd = (SysProtocol::Sys_LogMsg*)pProto;
				CacheLogs(pCmd->m_iLogId, pCmd->m_iType, pCmd->m_sKey, pCmd->m_sMsg);
			}

			P_ProtocolManager->ReleaseProto(pProto);
		}

		FlushLogs();
		return true;
	}
Exemplo n.º 3
0
	Bool HawkProfiler::RunLoop()
	{
		UInt32 iCalcTime = HawkOSOperator::GetTickCount();
		
		while(m_bRunning)
		{
			UInt32 iCurTime = HawkOSOperator::GetTickCount();

			//定期计算CPU和内存使用情况
			if (iCurTime -  iCalcTime >= 1000)
			{
				GetCpuUsage();
				GetMemUsage();
				iCalcTime = iCurTime;
			}

			if (!m_sSocket.UpdateEvent(HEVENT_READ, DEFAULT_SLEEP))
				continue;

			m_pRecvBuf->Clear();
			Size_t iRecv = (Size_t)m_pRecvBuf->Capacity();

			SocketAddr sAddr;
			if(!m_sSocket.ReceiveFrom(m_pRecvBuf->Begin(), iRecv, sAddr))
				continue;

			m_pRecvBuf->Resize((UInt32)iRecv);
			Protocol* pProto = 0;
			try
			{
				pProto = P_ProtocolManager->Decode(*m_pRecvBuf);
				if (!pProto) continue;
			}
			catch (HawkException& rhsExcep)
			{
				HawkFmtPrint("Exception: %s", rhsExcep.GetMsg().c_str());
				continue;
			}

			ProtoType eType = pProto->GetType();
			if(eType == SysProtocol::SYS_PROF_REQ)
			{
				SysProtocol::Sys_ProfInfo sInfo;
				sInfo.m_iTimeStamp	 = HawkOSOperator::GetTickCount();
				sInfo.m_iCpuCount	 = m_iCpuCount;
				sInfo.m_iTotalMem	 = m_iTotalMem;
				sInfo.m_iCpuUsage	 = m_iCpuUsage;
				sInfo.m_iMemUsage	 = m_iMemUsage;
				sInfo.m_iConnect	 = m_iConnect;
				sInfo.m_iRecvProto	 = m_iRecvProto;
				sInfo.m_iRecvSize	 = m_iRecvSize;
				sInfo.m_iSendProto	 = m_iSendProto;
				sInfo.m_iSendSize	 = m_iSendSize;

				m_pRecvBuf->Clear();
				sInfo.Encode(*m_pRecvBuf);
				Size_t iSize = (Size_t)m_pRecvBuf->Size();
				m_sSocket.SendTo(m_pRecvBuf->Begin(), iSize, sAddr);
			}

			P_ProtocolManager->ReleaseProto(pProto);
		}
		return true;
	}