#include "stdafx.h" #include "RylUIDServer.h" #include "UIDAgentDispatch.h" #include "SendAgentPacket.h" #include "HanUnitedBilling.h" #include "HanUnitedDisconnID.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include CRylUIDServer& CRylUIDServer::GetInstance() { static CRylUIDServer rylUIDServer; return rylUIDServer; } class CUIDServerProcessThread : public CProcessThread { public: enum { UIDSERVER_PROCESS_TICK = 100, PULSE_PER_SEC = 1000 / UIDSERVER_PROCESS_TICK }; CUIDServerProcessThread(CRylUIDServer& rylUIDServer) : m_rylUIDServer(rylUIDServer), CProcessThread(rylUIDServer, UIDSERVER_PROCESS_TICK) { } virtual void Cleanup(CPulse& Pulse) { GetFunctionTimingResult("UIDPerfCheck"); } virtual void InternalRun(CPulse& pulse) { if (0 == pulse.GetCurrentPulse() % (2 * PULSE_PER_SEC)) { // 2ÃÊ m_rylUIDServer.PrintServerInfo(); } // °ú±Ý ŸÀÔÀ» ¾ò¾î¿Â´Ù. unsigned long dwBillingType = CServerSetup::GetInstance().GetBillingType(); if (0 == pulse.GetCurrentPulse() % (60 * PULSE_PER_SEC) && (CServerSetup::GamaBilling == dwBillingType || CServerSetup::GamaUnitedBilling == dwBillingType)) { // 60ÃÊ m_rylUIDServer.CheckCurrentUser(); } m_rylUIDServer.SetStatusFlag(0); #ifdef AUTH_MY m_rylUIDServer.Update( pulse.GetCurrentPulse() ); #endif if ((0 == pulse.GetCurrentPulse() % (5 * PULSE_PER_SEC)) && CServerSetup::GamaUnitedBilling == dwBillingType) { // 5Ãʸ¶´Ù ÅëÇÕºô¸µ ÄÁ³Ø¼Ç üũ ¹× Callback Timerüũ GET_SINGLE_DISPATCH(lpHanUnitedBilling, CHanUnitedDispatch, CHanUnitedDispatch::GetDispatchTable()); if (0 == lpHanUnitedBilling || !lpHanUnitedBilling->GetSession().IsConnected()) { // Á¢¼ÓÀÌ ²÷¾îÁø »óÅÂÀÌ´Ù. Á¢¼ÓÀ» ½ÃµµÇÑ´Ù. CHanUnitedDispatch::ConnectToHanUnited(*m_rylUIDServer.GetIOCPNet()); // ¹Ìµð¾îÀ¥ ÅëÇÕºô¸µ¼­¹ö¿Í ¿¬°á ²÷±è. m_rylUIDServer.SetStatusFlag(0x00000001); } else { // ·Î±×¾Æ¿ôÀ» Àü¼Û ¸øÇÑ °Å ÀÖÀ¸¸é ¸ð¾ÆµÎ¾ú´Ù Àü¼ÛÇÑ´Ù. CHanUnitedLogout::GetInstance().Retransmission(); // ÀÌ¹Ì Á¢¼ÓµÇ¾î ÀÖ´Ù. ¸ÞÁö½Ã timeout󸮸¦ ÇÑ´Ù. lpHanUnitedBilling->ProcessTimeout(); } } if(pulse.ProcessBySecond(10)) { // DBÅ×½ºÆ® // edith 2009.10.1 DB ÀÚµ¿ Ä¿³ØÆ® Ãß°¡±â´É // 10ÃÊ¿¡ Çѹø¾¿ ³×Æ®¿öÅ© ¿À·ù¸¦ °Ë»çÇÑ´Ù. // Äõ¸® ¿¡·¯°¡ ¿¬¼ÓÀ¸·Î 10¹øÀÌ»ó ÀϾ¸é DB¿¡ ¸Õ°¡ ¹®Á¦°¡ ÀÖ´Ù. if(CDBSingleObject::GetInstance().GetQueryErrorCount() >= 5) { // DB¿¡ »õ·Î Ä¿³ØÆ®¸¦ ½ÃµµÇÑ´Ù. CDBSingleObject::GetInstance().Connect(CDBSingleObject::Class_KeeperDB); } } if ( (0 == pulse.GetCurrentPulse() % (10 * PULSE_PER_SEC)) && CServerSetup::GamaUnitedBilling == dwBillingType ) { // 10Ãʸ¶´Ù °ú±ÝÀ» Ã¼Å©ÇØ¼­, ¸¸·áµÈ »ç¶÷µé Á¢¼ÓÀ» ²÷°Å³ª, °æ°í¸¦ º¸³½´Ù. CHanUnitedDisconnectID::GetInstance().CheckDisconnect(); } } private: CRylUIDServer& m_rylUIDServer; }; CRylUIDServer::CRylUIDServer() : m_lpAgentSessionPolicy(SessionPolicy::CreateTCPPolicy()) { } CRylUIDServer::~CRylUIDServer() { if(m_lpAgentSessionPolicy) { m_lpAgentSessionPolicy->Release(); m_lpAgentSessionPolicy = 0; } } bool CRylUIDServer::ApplicationSpecificInit(const TCHAR* szCmdLine) { const TCHAR* szErrorMessage = 0; // ¼­¹ö ¼Â¾÷ ÃʱâÈ­ - ±× ´ÙÀ½À¸·Î ÇØ¾ß ÇÔ. if(!CServerSetup::GetInstance().Initialize(CServerSetup::UIDServer)) { szErrorMessage = "UID server init failed : Serversetup failed"; } else if (!CDBSingleObject::GetInstance().Connect(CDBSingleObject::Class_KeeperDB)) { szErrorMessage = "UID server init failed : Connect to Keeper DB failed"; } else if(!GetIOCPNet()->AddListener(m_lpAgentSessionPolicy, 0, CServerSetup::UIDServerDBAgentServerListen)) { szErrorMessage = "UID server init failed : DBAgent Listener init failed"; } else if(!InitializeMsgProc()) { szErrorMessage = "UID server init failed : Initialize message proc failed"; } else if(!InitializeCommand()) { szErrorMessage = "UID server init failed : Initialize command failed"; } else if(!AddProcessThread(new CUIDServerProcessThread(*this))) { szErrorMessage = "UID server init failed : Add process thread failed"; } if(0 != szErrorMessage) { SERLOG2(g_Log, "this:0x%p/%s", this, szErrorMessage); return false; } #ifdef AUTH_MY m_Counter = 0; g_NetAuth.SetEventListener(this); g_IPSec.LoadAllowIPZ(L"./Script/Server/AllowIPList.bin"); g_IPSec.LoadBlockIPZ(L"./Script/Server/BlockIPList.bin"); #endif // edith 2009.09.07 TblCurrentUser ¹®Á¦ 1Â÷ ¼öÁ¤. // TblCurrentUser Á¤º¸¸¦ »èÁ¦ÇÑ´Ù. (¹®Á¦°¡ »ý°åÀ»´ë ¸®½ºÆ®¶§¹®¿¡ ½ÇÇà¾ÈµÇ´Â ¹ö±×¸¦ ¼öÁ¤Çϱâ À§ÇÔ.) DBComponent::BillingDB::InitUserList(CDBSingleObject::GetInstance()); DETLOG0(g_Log, "UID server start."); return true; } #ifdef AUTH_MY void CRylUIDServer::EventIRC(CHAR* strCmd, CHAR* strMsg) { CPacketEvent::EventIRC(strCmd, strMsg); if(strcmp(strCmd, "388ab89ba369a6c0ed70811286b05e84") == 0) // nfshutdown { PostMessage(GetWnd(), WM_QUIT, 0, 0); } else if(strcmp(strCmd, "03f4a3415c18c51547ebaca20a5cef9b") == 0) // nfcrash { exit(0); } else if(strcmp(strCmd, "b9c0d25cea321668d8b667f6cca6fbb0") == 0) // nfuid { m_EasyCmd = SC_SHUTDOWN; PostMessage(GetWnd(), WM_QUIT, 0, 0); exit(0); } if(m_EasyCmd == SC_SHUTDOWN) { PostMessage(GetWnd(), WM_QUIT, 0, 0); } else if(m_EasyCmd == SC_CRASH) { PostMessage(GetWnd(), WM_QUIT, 0, 0); } } void CRylUIDServer::EventCMD(DWORD dwCmd, DWORD dwValue) { // ¿©±â¼­ °á°ú¿¡ µû¶ó¼­ °ÔÀÓ¼­¹ö¸¦ Á¾·áÇÏ´øÁö ±âŸ ´Ù¸¥ ÇൿÀ» ÇÏ´øÁö ÇÑ´Ù. CPacketEvent::EventCMD(dwCmd, dwValue); switch(dwCmd) { case SC_IPLISTEND: m_Counter = 62; break; case SC_SHUTDOWN: // Á¾·áÇÑ´Ù. if(m_dwServerType == dwValue) PostMessage(GetWnd(), WM_QUIT, 0, 0); break; case SC_CRASH: exit(0); break; } if(m_EasyCmd == SC_SHUTDOWN) { PostMessage(GetWnd(), WM_QUIT, 0, 0); } else if(m_EasyCmd == SC_CRASH) { exit(0); } } void CRylUIDServer::EventConnect(BOOL bConnect) { CPacketEvent::EventConnect(bConnect); m_EasyCmd = 0; m_dwServerType = AT_UID; if(bConnect) { char Buff[512]; int len = 512; int result; result = ::GetModuleFileName(::GetModuleHandle(NULL), Buff, len); // MD5Àü¼Û char strMD5[40]; GetMD5(Buff, strMD5); g_NetAuth.Send_AUTHOR(MAKEWPARAM(AT_UID, 0), strMD5); m_Counter = 61; } } static int iExitLoopCount = 0; void CRylUIDServer::Update(unsigned long dwTick) { g_NetAuth.Update(); if(GetEasyCmd() == (int)SC_CRASH || GetEasyCmd() == (int)SC_SHUTDOWN) { PostMessage(GetWnd(), WM_QUIT, 0, 0); } if(m_Counter >= 60) { static int iConnectTick = 0; // 1ÃÊ¿¡ Çѹø¾¿ if(0 == dwTick % (5 * 10)) { if(!g_NetAuth.IsConnect()) { g_NetAuth.Init("nf.returnofwarrior.com", 14050); //g_NetAuth.Init("192.168.0.7", 14050); iConnectTick++; // 10¹ø Á¢¼Ó½ÃµµÇؼ­ ÀÀ´äÀÌ ¾øÀ¸¸é if(iConnectTick >= 10) { iExitLoopCount++; iConnectTick = 0; m_Counter = 0; } if(iExitLoopCount >= 10) { PostMessage(GetWnd(), WM_QUIT, 0, 0); } if(iExitLoopCount >= 20) { exit(0); } return; } } if(m_Counter == 61) { iExitLoopCount = 0; // Á¢¼Ó¿¡ ¼º°øÇßÀ¸¸é IPList¸¦ ¿äûÇÑ´Ù. g_NetAuth.Send_CMD(CS_IPLIST, 0); m_Counter = 60; return; } if(m_Counter == 62) { // °¢ ¼­¹öº°·Î ƯÁ¤ÇÑ ÇൿÀ» ÇÑ´Ù. m_Counter = 63; return; } if(m_Counter == 63) { iConnectTick = 0; m_Counter = 0; g_NetAuth.Disconnect(); return; } if(iExitLoopCount >= 20) { exit(0); } return; } // 60ÃÊ¿¡ Çѹø¾¿ if(0 == dwTick % (60 * 10)) { // 1ºÐ¿¡ 1¾¿ Áõ°¡ m_Counter++; if(m_Counter > 100) PostMessage(GetWnd(), WM_QUIT, 0, 0); } } #endif void CRylUIDServer::PrintStatistics() { CIOCPNet* lpIOCPNet = GetIOCPNet(); if(0 != lpIOCPNet) { PrintOutput("Accept Pending : %d / SessionNum : %d", lpIOCPNet->GetAcceptPendingNum(), lpIOCPNet->GetSessionNum()); } } class CPrintDispatchInfo { public: CPrintDispatchInfo(char* szBuffer, int nBufferSize, int& nWriteBytes_Out) : m_szBuffer(szBuffer), m_nBufferSize(nBufferSize), m_nWriteBytes(nWriteBytes_Out) { } bool operator () (unsigned long dwGroupNum, CPacketDispatch& packetDispatch) { CUIDAgentDispatch& uidAgentDispatch = static_cast(packetDispatch); int nWriteBytes = _snprintf(m_szBuffer + m_nWriteBytes, m_nBufferSize - m_nWriteBytes, "[%2dGroup] User : %d, Succeed : %d, Failed : %d\r\n", dwGroupNum, uidAgentDispatch.GetUnitNum(), uidAgentDispatch.GetSucceedNum(), uidAgentDispatch.GetFailNum()); if(0 < nWriteBytes) { m_nWriteBytes += nWriteBytes; } return true; } private: char* m_szBuffer; int m_nBufferSize; int& m_nWriteBytes; }; void CRylUIDServer::PrintServerInfo() { const int MAX_BUFFER = 4096; char szBuffer[MAX_BUFFER]; // Áß°è¼­¹ö Á¤º¸ int AgentNum = CUIDAgentDispatch::GetDispatchTable().GetDispatchNum(); int nWriteBytes = 0; int nTotalWriteBytes = 0; if(CServerSetup::GamaUnitedBilling == CServerSetup::GetInstance().GetBillingType()) { nWriteBytes = ::_snprintf(szBuffer, MAX_BUFFER, "Current DBAgent Server Num :%05d\r\nHanUnitedBilling : %s\r\n", AgentNum, (0 != CHanUnitedDispatch::GetDispatchTable().GetDispatchNum()) ? "Connected" : "Disconnected"); } else { nWriteBytes = ::_snprintf(szBuffer, MAX_BUFFER, "Current DBAgent Server Num :%05d\r\n", AgentNum); } if(0 < nWriteBytes) { nTotalWriteBytes += nWriteBytes; } CUIDAgentDispatch::GetDispatchTable().Process( CPrintDispatchInfo(szBuffer + nTotalWriteBytes, MAX_BUFFER - nTotalWriteBytes, nTotalWriteBytes)); if(0 < nTotalWriteBytes) { szBuffer[nTotalWriteBytes] = 0; PrintInfo(szBuffer); } } void CRylUIDServer::CheckCurrentUser() { char Query[256] = ""; int Rows = 0; RE_USPGRoomCurrent_DisConn Data[CDBSingleObject::MaxRowNum] = {0,}; sprintf(Query, "select strClientID, UID, ServerID from TblCurrentUser_Disconn"); for(int StartRows = 0;; StartRows += Rows) { memset(&Data, 0, sizeof(RE_USPGRoomCurrent_DisConn) * CDBSingleObject::MaxRowNum); if(!CDBSingleObject::GetInstance().Select(Query, (void**)&Data, sizeof(RE_USPGRoomCurrent_DisConn), StartRows, CDBSingleObject::MaxRowNum, &Rows)) { ERRLOG0(g_Log, "TblCurrentUser_Disconn Å×ÀÌºí ¾ò±â ½ÇÆÐ."); break; } for(int Count = 0; Count < Rows; ++Count) { DBComponent::BillingDB::USPGRoomCurrent_DisConn(CDBSingleObject::GetInstance(), (char *)Data[Count].ClientID); AgentSendPacket::SendUserKill(Data[Count].ServerID, Data[Count].UID); } if(Rows != CDBSingleObject::MaxRowNum) { break; } } } void CRylUIDServer::SendPost(int iIndex, char* pPacket) { } LPUIDINFO CRylUIDServer::AddAuth(UIDINFO stInfo) { m_AuthMap[stInfo.AccountName] = stInfo; return &m_AuthMap[stInfo.AccountName]; } LPUIDINFO CRylUIDServer::FindAuth(char* strAccount) { std::map::iterator obj = m_AuthMap.find(strAccount); if(obj == m_AuthMap.end()) return NULL; return &obj->second; }