Advertisement
Guest User

desc_client.cpp

a guest
Jul 18th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.52 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "config.h"
  3. #include "utils.h"
  4. #include "desc_client.h"
  5. #include "desc_manager.h"
  6. #include "char.h"
  7. #include "protocol.h"
  8. #include "p2p.h"
  9. #include "buffer_manager.h"
  10. #include "guild_manager.h"
  11. #include "db.h"
  12.  
  13. #include "party.h"
  14.  
  15. extern LPFDWATCH    main_fdw;
  16.  
  17. LPCLIENT_DESC db_clientdesc = NULL;
  18. LPCLIENT_DESC g_pkAuthMasterDesc = NULL;
  19. LPCLIENT_DESC g_NetmarbleDBDesc = NULL;
  20. LPCLIENT_DESC g_TeenDesc        = NULL;
  21. LPCLIENT_DESC g_PasspodDesc = NULL;
  22.  
  23. static const char* GetKnownClientDescName(LPCLIENT_DESC desc) {
  24.     if (desc == db_clientdesc) {
  25.         return "db_clientdesc";
  26.     } else if (desc == g_pkAuthMasterDesc) {
  27.         return "g_pkAuthMasterDesc";
  28.     } else if (desc == g_NetmarbleDBDesc) {
  29.         return "g_NetmarbleDBDesc";
  30.     } else if (desc == g_TeenDesc) {
  31.         return "g_TeenDesc";
  32.     } else if (desc == g_PasspodDesc) {
  33.         return "g_PasspodDesc";
  34.     }
  35.     return "unknown";
  36. }
  37.  
  38. CLIENT_DESC::CLIENT_DESC()
  39. {
  40.     m_iPhaseWhenSucceed = 0;
  41.     m_bRetryWhenClosed = false;
  42.     m_LastTryToConnectTime = 0;
  43.     m_tLastChannelStatusUpdateTime = 0;
  44. }
  45.  
  46. CLIENT_DESC::~CLIENT_DESC()
  47. {
  48. }
  49.  
  50. void CLIENT_DESC::Destroy()
  51. {
  52.     if (m_sock == INVALID_SOCKET) {
  53.         return;
  54.     }
  55.  
  56.     P2P_MANAGER::instance().UnregisterConnector(this);
  57.  
  58.     if (this == db_clientdesc)
  59.     {
  60.         CPartyManager::instance().DeleteAllParty();
  61.         CPartyManager::instance().DisablePCParty();
  62.         CGuildManager::instance().StopAllGuildWar();
  63.         DBManager::instance().StopAllBilling();
  64.     }
  65.  
  66.     fdwatch_del_fd(m_lpFdw, m_sock);
  67.  
  68.     sys_log(0, "SYSTEM: closing client socket. DESC #%d", m_sock);
  69.  
  70.     socket_close(m_sock);
  71.     m_sock = INVALID_SOCKET;
  72.  
  73.     // Chain up to base class Destroy()
  74.     DESC::Destroy();
  75. }
  76.  
  77. void CLIENT_DESC::SetRetryWhenClosed(bool b)
  78. {
  79.     m_bRetryWhenClosed = b;
  80. }
  81.  
  82. bool CLIENT_DESC::Connect(int iPhaseWhenSucceed)
  83. {
  84.     if (iPhaseWhenSucceed != 0)
  85.         m_iPhaseWhenSucceed = iPhaseWhenSucceed;
  86.  
  87.     if (get_global_time() - m_LastTryToConnectTime < 3) // 3초
  88.         return false;
  89.  
  90.     m_LastTryToConnectTime = get_global_time();
  91.  
  92.     if (m_sock != INVALID_SOCKET)
  93.         return false;
  94.  
  95.     sys_log(0, "SYSTEM: Trying to connect to %s:%d", m_stHost.c_str(), m_wPort);
  96.  
  97. #ifdef FIX_LOGIN
  98.     if (iPhaseWhenSucceed == PHASE_P2P)
  99.     {
  100.       sys_log(0, "Ikarus : IP to connect: %s , Port: %d\n", g_szInternalIP, m_wPort);
  101.       m_sock = socket_connect(g_szInternalIP, m_wPort);
  102.     }
  103.       else
  104.     {
  105.       m_sock = socket_connect(m_stHost.c_str(), m_wPort);
  106.     }
  107. #else
  108.     m_sock = socket_connect(m_stHost.c_str(), m_wPort);
  109. #endif
  110.  
  111.     if (m_sock != INVALID_SOCKET)
  112.     {
  113.         sys_log(0, "SYSTEM: connected to server (fd %d, ptr %p)", m_sock, this);
  114.         fdwatch_add_fd(m_lpFdw, m_sock, this, FDW_READ, false);
  115.         fdwatch_add_fd(m_lpFdw, m_sock, this, FDW_WRITE, false);
  116.         SetPhase(m_iPhaseWhenSucceed);
  117.         return true;
  118.     }
  119.     else
  120.     {
  121.         SetPhase(PHASE_CLIENT_CONNECTING);
  122.         return false;
  123.     }
  124. }
  125.  
  126. void CLIENT_DESC::Setup(LPFDWATCH _fdw, const char * _host, WORD _port)
  127. {
  128.     // 1MB input/output buffer
  129.     m_lpFdw = _fdw;
  130.     m_stHost = _host;
  131.     m_wPort = _port;
  132.  
  133.     InitializeBuffers();
  134.  
  135.     m_sock = INVALID_SOCKET;
  136. }
  137.  
  138. void CLIENT_DESC::SetPhase(int iPhase)
  139. {
  140.     switch (iPhase)
  141.     {
  142.         case PHASE_CLIENT_CONNECTING:
  143.             sys_log(1, "PHASE_CLIENT_DESC::CONNECTING");
  144.             m_pInputProcessor = NULL;
  145.             break;
  146.  
  147.         case PHASE_DBCLIENT:
  148.             {
  149.                 sys_log(1, "PHASE_DBCLIENT");
  150.  
  151.                 if (!g_bAuthServer)
  152.                 {
  153.                     static bool bSentBoot = false;
  154.  
  155.                     if (!bSentBoot)
  156.                     {
  157.                         bSentBoot = true;
  158.                         TPacketGDBoot p;
  159.                         p.dwItemIDRange[0] = 0;
  160.                         p.dwItemIDRange[1] = 0;
  161.                     #ifdef FIX_LOGIN
  162.                         memcpy(p.szIP, g_szExternPublicIP, 16);
  163.                     #else
  164.                         memcpy(p.szIP, g_szPublicIP, 16);
  165.                     #endif
  166.                         DBPacket(HEADER_GD_BOOT, 0, &p, sizeof(p));
  167.                     }
  168.                 }
  169.  
  170.                 TEMP_BUFFER buf;
  171.  
  172.                 TPacketGDSetup p;
  173.  
  174.                 memset(&p, 0, sizeof(p));
  175.             #ifdef FIX_LOGIN
  176.                 strlcpy(p.szPublicIP, g_szExternPublicIP, sizeof(p.szPublicIP));
  177.             #else
  178.                 strlcpy(p.szPublicIP, g_szPublicIP, sizeof(p.szPublicIP));
  179.             #endif
  180.  
  181.                 if (!g_bAuthServer)
  182.                 {
  183.                     p.bChannel  = g_bChannel;
  184.                     p.wListenPort = mother_port;
  185.                     p.wP2PPort  = p2p_port;
  186.                     p.bAuthServer = false;
  187.                     map_allow_copy(p.alMaps, 32);
  188.  
  189.                     const DESC_MANAGER::DESC_SET & c_set = DESC_MANAGER::instance().GetClientSet();
  190.                     DESC_MANAGER::DESC_SET::const_iterator it;
  191.  
  192.                     for (it = c_set.begin(); it != c_set.end(); ++it)
  193.                     {
  194.                         LPDESC d = *it;
  195.  
  196.                         if (d->GetAccountTable().id != 0)
  197.                             ++p.dwLoginCount;
  198.                     }
  199.  
  200.                     buf.write(&p, sizeof(p));
  201.  
  202.                     if (p.dwLoginCount)
  203.                     {
  204.                         TPacketLoginOnSetup pck;
  205.  
  206.                         for (it = c_set.begin(); it != c_set.end(); ++it)
  207.                         {
  208.                             LPDESC d = *it;
  209.  
  210.                             TAccountTable & r = d->GetAccountTable();
  211.  
  212.                             if (r.id != 0)
  213.                             {
  214.                                 pck.dwID = r.id;
  215.                                 strlcpy(pck.szLogin, r.login, sizeof(pck.szLogin));
  216.                                 strlcpy(pck.szSocialID, r.social_id, sizeof(pck.szSocialID));
  217.                                 strlcpy(pck.szHost, d->GetHostName(), sizeof(pck.szHost));
  218.                                 pck.dwLoginKey = d->GetLoginKey();
  219. #ifndef _IMPROVED_PACKET_ENCRYPTION_
  220.                                 thecore_memcpy(pck.adwClientKey, d->GetDecryptionKey(), 16);
  221. #endif
  222.  
  223.                                 buf.write(&pck, sizeof(TPacketLoginOnSetup));
  224.                             }
  225.                         }
  226.                     }
  227.  
  228.                     sys_log(0, "DB_SETUP current user %d size %d", p.dwLoginCount, buf.size());
  229.  
  230.                     // 파티를 처리할 수 있게 됨.
  231.                     CPartyManager::instance().EnablePCParty();
  232.                     //CPartyManager::instance().SendPartyToDB();
  233.                 }
  234.                 else
  235.                 {
  236.                     p.bAuthServer = true;
  237.                     buf.write(&p, sizeof(p));
  238.                 }
  239.  
  240.                 DBPacket(HEADER_GD_SETUP, 0, buf.read_peek(), buf.size());
  241.                 m_pInputProcessor = &m_inputDB;
  242.             }
  243.             break;
  244.  
  245.         case PHASE_P2P:
  246.             sys_log(1, "PHASE_P2P");
  247.            
  248.             if (m_lpInputBuffer)
  249.                 buffer_reset(m_lpInputBuffer);
  250.  
  251.             if (m_lpOutputBuffer)
  252.                 buffer_reset(m_lpOutputBuffer);
  253.  
  254.             m_pInputProcessor = &m_inputP2P;
  255.             break;
  256.  
  257.         case PHASE_CLOSE:
  258.             m_pInputProcessor = NULL;
  259.             break;
  260.  
  261.         case PHASE_TEEN:
  262.             m_inputTeen.SetStep(0);
  263.             m_pInputProcessor = &m_inputTeen;
  264.             break;
  265.  
  266.     }
  267.  
  268.     m_iPhase = iPhase;
  269. }
  270.  
  271. void CLIENT_DESC::DBPacketHeader(BYTE bHeader, DWORD dwHandle, DWORD dwSize)
  272. {
  273.     buffer_write(m_lpOutputBuffer, encode_byte(bHeader), sizeof(BYTE));
  274.     buffer_write(m_lpOutputBuffer, encode_4bytes(dwHandle), sizeof(DWORD));
  275.     buffer_write(m_lpOutputBuffer, encode_4bytes(dwSize), sizeof(DWORD));
  276. }
  277.  
  278. void CLIENT_DESC::DBPacket(BYTE bHeader, DWORD dwHandle, const void * c_pvData, DWORD dwSize)
  279. {
  280.     if (m_sock == INVALID_SOCKET) {
  281.         sys_log(0, "CLIENT_DESC [%s] trying DBPacket() while not connected",
  282.             GetKnownClientDescName(this));
  283.         return;
  284.     }
  285.     sys_log(1, "DB_PACKET: header %d handle %d size %d buffer_size %d", bHeader, dwHandle, dwSize, buffer_size(m_lpOutputBuffer));
  286.     DBPacketHeader(bHeader, dwHandle, dwSize);
  287.  
  288.     if (c_pvData)
  289.         buffer_write(m_lpOutputBuffer, c_pvData, dwSize);
  290. }
  291.  
  292. void CLIENT_DESC::Packet(const void * c_pvData, int iSize)
  293. {
  294.     if (m_sock == INVALID_SOCKET) {
  295.         sys_log(0, "CLIENT_DESC [%s] trying Packet() while not connected",
  296.             GetKnownClientDescName(this));
  297.         return;
  298.     }
  299.     buffer_write(m_lpOutputBuffer, c_pvData, iSize);
  300. }
  301.  
  302. bool CLIENT_DESC::IsRetryWhenClosed()
  303. {
  304.     return (0 == thecore_is_shutdowned() && m_bRetryWhenClosed);
  305. }
  306.  
  307. void CLIENT_DESC::Update(DWORD t)
  308. {
  309.     if (!g_bAuthServer) {
  310.         UpdateChannelStatus(t, false);
  311.     }
  312. }
  313.  
  314. void CLIENT_DESC::UpdateChannelStatus(DWORD t, bool fForce)
  315. {
  316.     enum {
  317.         CHANNELSTATUS_UPDATE_PERIOD = 5*60*1000,    // 5분마다
  318.     };
  319.     if (fForce || m_tLastChannelStatusUpdateTime+CHANNELSTATUS_UPDATE_PERIOD < t) {
  320.         int iTotal;
  321.         int * paiEmpireUserCount;
  322.         int iLocal;
  323.         DESC_MANAGER::instance().GetUserCount(iTotal, &paiEmpireUserCount, iLocal);
  324.  
  325.         TChannelStatus channelStatus;
  326.         channelStatus.nPort = mother_port;
  327.  
  328.         if (g_bNoMoreClient) channelStatus.bStatus = 0;
  329.         else channelStatus.bStatus = iTotal > g_iFullUserCount ? 3 : iTotal > g_iBusyUserCount ? 2 : 1;
  330.  
  331.         DBPacket(HEADER_GD_UPDATE_CHANNELSTATUS, 0, &channelStatus, sizeof(channelStatus));
  332.         m_tLastChannelStatusUpdateTime = t;
  333.     }
  334. }
  335.  
  336. void CLIENT_DESC::Reset()
  337. {
  338.     // Backup connection target info
  339.     LPFDWATCH fdw = m_lpFdw;
  340.     std::string host = m_stHost;
  341.     WORD port = m_wPort;
  342.  
  343.     Destroy();
  344.     Initialize();
  345.  
  346.     // Restore connection target info
  347.     m_lpFdw = fdw;
  348.     m_stHost = host;
  349.     m_wPort = port;
  350.  
  351.     InitializeBuffers();
  352. }
  353.  
  354. void CLIENT_DESC::InitializeBuffers()
  355. {
  356.     m_lpOutputBuffer = buffer_new(1024 * 1024);
  357.     m_lpInputBuffer = buffer_new(1024 * 1024);
  358.     m_iMinInputBufferLen = 1024 * 1024;
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement