Guest User

Untitled

a guest
Aug 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.73 KB | None | 0 0
  1. DB: Main.cpp
  2.  
  3. #include "stdafx.h"
  4. #include "Config.h"
  5. #include "Peer.h"
  6. #include "DBManager.h"
  7. #include "ClientManager.h"
  8. #include "GuildManager.h"
  9. #include "ItemAwardManager.h"
  10. #include "HB.h"
  11. #include "PrivManager.h"
  12. #include "MoneyLog.h"
  13. #include "Marriage.h"
  14. #include "Monarch.h"
  15. #include "BlockCountry.h"
  16. #include "ItemIDRangeManager.h"
  17. #ifdef __AUCTION__
  18. #include "AuctionManager.h"
  19. #endif
  20. #include <signal.h>
  21.  
  22. void SetPlayerDBName(const char* c_pszPlayerDBName);
  23. void SetTablePostfix(const char* c_pszTablePostfix);
  24. int Start();
  25.  
  26. std::string g_stTablePostfix;
  27. std::string g_stLocaleNameColumn = "name";
  28. std::string g_stLocale = "euckr";
  29. std::string g_stPlayerDBName = "";
  30.  
  31.  
  32. bool g_bHotBackup = false;
  33. BOOL g_test_server = false;
  34.  
  35. // NOTXT_STARTUP_BEGIN
  36. BOOL g_noTXT = false;
  37. // NOTXT_STARTUP_END
  38.  
  39. //단위 초
  40. int g_iPlayerCacheFlushSeconds = 60*7;
  41. int g_iItemCacheFlushSeconds = 60*5;
  42.  
  43. //g_iLogoutSeconds 수치는 g_iPlayerCacheFlushSeconds 와 g_iItemCacheFlushSeconds 보다 길어야 한다.
  44. int g_iLogoutSeconds = 60*10;
  45.  
  46. int g_log = 1;
  47.  
  48.  
  49. // MYSHOP_PRICE_LIST
  50. int g_iItemPriceListTableCacheFlushSeconds = 540;
  51. // END_OF_MYSHOP_PRICE_LIST
  52.  
  53. #ifdef __FreeBSD__
  54. extern const char * _malloc_options;
  55. #endif
  56.  
  57. extern void WriteVersion();
  58.  
  59. void emergency_sig(int sig)
  60. {
  61.     if (sig == SIGSEGV)
  62.         sys_log(0, "SIGNAL: SIGSEGV");
  63.     else if (sig == SIGUSR1)
  64.         sys_log(0, "SIGNAL: SIGUSR1");
  65.  
  66.     if (sig == SIGSEGV)
  67.         abort();
  68. }
  69.  
  70. int main()
  71. {
  72.     WriteVersion();
  73.  
  74. #ifdef __FreeBSD__
  75.     _malloc_options = "A";
  76. #endif
  77.  
  78.     CConfig Config;
  79.     CNetPoller poller;
  80.     CDBManager DBManager;
  81.     CClientManager ClientManager;
  82.     PlayerHB player_hb;
  83.     CGuildManager GuildManager;
  84.     CPrivManager PrivManager;
  85.     CMoneyLog MoneyLog;
  86.     ItemAwardManager ItemAwardManager;
  87.     marriage::CManager MarriageManager;
  88.     CMonarch Monarch;
  89.     CBlockCountry   BlockCountry;
  90.     CItemIDRangeManager ItemIDRangeManager;
  91. #ifdef __AUCTION__
  92.     AuctionManager auctionManager;
  93. #endif
  94.     if (!Start())
  95.         return 1;
  96.  
  97.     GuildManager.Initialize();
  98.     MarriageManager.Initialize();
  99.     BlockCountry.Load();
  100.     ItemIDRangeManager.Build();
  101. #ifdef __AUCTION__
  102.     AuctionManager::instance().Initialize();
  103. #endif
  104.     sys_log(0, "Metin2DBCacheServer Start\n");
  105.  
  106.     CClientManager::instance().MainLoop();
  107.  
  108.     signal_timer_disable();
  109.  
  110.     DBManager.Quit();
  111.     int iCount;
  112.  
  113.     while (1)
  114.     {
  115.         iCount = 0;
  116.  
  117.         iCount += CDBManager::instance().CountReturnQuery(SQL_PLAYER);
  118.         iCount += CDBManager::instance().CountAsyncQuery(SQL_PLAYER);
  119.  
  120.         if (iCount == 0)
  121.             break;
  122.  
  123.         usleep(1000);
  124.         sys_log(0, "WAITING_QUERY_COUNT %d", iCount);
  125.     }
  126.  
  127.     return 1;
  128. }
  129.  
  130. void emptybeat(LPHEART heart, int pulse)
  131. {
  132.     if (!(pulse % heart->passes_per_sec))   // 1초에 한번
  133.     {
  134.     }
  135. }
  136.  
  137. //
  138. // @version 05/06/13 Bang2ni - 아이템 가격정보 캐시 flush timeout 설정 추가.
  139. //
  140. int Start()
  141. {
  142.     if (!CConfig::instance().LoadFile("conf.txt"))
  143.     {
  144.         fprintf(stderr, "Loading conf.txt failed.\n");
  145.         return false;
  146.     }
  147.  
  148.     if (!CConfig::instance().GetValue("TEST_SERVER", &g_test_server))
  149.     {
  150.         fprintf(stderr, "Real Server\n");
  151.     }
  152.     else
  153.         fprintf(stderr, "Test Server\n");
  154.  
  155.     if (!CConfig::instance().GetValue("LOG", &g_log))
  156.     {
  157.         fprintf(stderr, "Log Off");
  158.         g_log= 0;
  159.     }
  160.     else
  161.     {
  162.         g_log = 1;
  163.         fprintf(stderr, "Log On");
  164.     }
  165.    
  166.    
  167.     int tmpValue;
  168.  
  169.     int heart_beat = 50;
  170.     if (!CConfig::instance().GetValue("CLIENT_HEART_FPS", &heart_beat))
  171.     {
  172.         fprintf(stderr, "Cannot find CLIENT_HEART_FPS configuration.\n");
  173.         return false;
  174.     }
  175.  
  176.     log_set_expiration_days(3);
  177.  
  178.     if (CConfig::instance().GetValue("LOG_KEEP_DAYS", &tmpValue))
  179.     {
  180.         tmpValue = MINMAX(3, tmpValue, 30);
  181.         log_set_expiration_days(tmpValue);
  182.         fprintf(stderr, "Setting log keeping days to %d\n", tmpValue);
  183.     }
  184.  
  185.     thecore_init(heart_beat, emptybeat);
  186.     signal_timer_enable(60);
  187.  
  188.     char szBuf[256+1];
  189.  
  190.     if (CConfig::instance().GetValue("LOCALE", szBuf, 256))
  191.     {
  192.         g_stLocale = szBuf;
  193.         sys_log(0, "LOCALE set to %s", g_stLocale.c_str());
  194.  
  195.         // CHINA_DISABLE_HOTBACKUP
  196.         if ("gb2312" == g_stLocale)
  197.         {
  198.             sys_log(0, "CIBN_LOCALE: DISABLE_HOTBACKUP");
  199.             g_bHotBackup = false;
  200.         }
  201.         // END_OF_CHINA_DISABLE_HOTBACKUP
  202.     }
  203.  
  204.     int iDisableHotBackup;
  205.     if (CConfig::instance().GetValue("DISABLE_HOTBACKUP", &iDisableHotBackup))
  206.     {
  207.         if (iDisableHotBackup)
  208.         {  
  209.             sys_log(0, "CONFIG: DISABLE_HOTBACKUP");
  210.             g_bHotBackup = false;
  211.         }
  212.     }
  213.  
  214.  
  215.     // NOTXT_STARTUP_BEGIN
  216.     int iNoTXT;
  217.     if (CConfig::instance().GetValue("NO_TXT", &iNoTXT))
  218.     {
  219.         if (iNoTXT)
  220.         {
  221.             sys_log(0, "CONFIG: NO_TXT");
  222.             g_noTXT = true;
  223.         }
  224.     }
  225.     // NOTXT_STARTUP_END
  226.    
  227.     if (!CConfig::instance().GetValue("TABLE_POSTFIX", szBuf, 256))
  228.     {
  229.         sys_err("TABLE_POSTFIX not configured use default");
  230.         szBuf[0] = '\0';
  231.     }
  232.  
  233.     SetTablePostfix(szBuf);
  234.  
  235.     if (CConfig::instance().GetValue("PLAYER_CACHE_FLUSH_SECONDS", szBuf, 256))
  236.     {
  237.         str_to_number(g_iPlayerCacheFlushSeconds, szBuf);
  238.         sys_log(0, "PLAYER_CACHE_FLUSH_SECONDS: %d", g_iPlayerCacheFlushSeconds);
  239.     }
  240.  
  241.     if (CConfig::instance().GetValue("ITEM_CACHE_FLUSH_SECONDS", szBuf, 256))
  242.     {
  243.         str_to_number(g_iItemCacheFlushSeconds, szBuf);
  244.         sys_log(0, "ITEM_CACHE_FLUSH_SECONDS: %d", g_iItemCacheFlushSeconds);
  245.     }
  246.  
  247.     // MYSHOP_PRICE_LIST
  248.     if (CConfig::instance().GetValue("ITEM_PRICELIST_CACHE_FLUSH_SECONDS", szBuf, 256))
  249.     {
  250.         str_to_number(g_iItemPriceListTableCacheFlushSeconds, szBuf);
  251.         sys_log(0, "ITEM_PRICELIST_CACHE_FLUSH_SECONDS: %d", g_iItemPriceListTableCacheFlushSeconds);
  252.     }
  253.     // END_OF_MYSHOP_PRICE_LIST
  254.     //
  255.     if (CConfig::instance().GetValue("CACHE_FLUSH_LIMIT_PER_SECOND", szBuf, 256))
  256.     {
  257.         DWORD dwVal = 0; str_to_number(dwVal, szBuf);
  258.         CClientManager::instance().SetCacheFlushCountLimit(dwVal);
  259.     }
  260.  
  261.     int iIDStart;
  262.     if (!CConfig::instance().GetValue("PLAYER_ID_START", &iIDStart))
  263.     {
  264.         sys_err("PLAYER_ID_START not configured");
  265.         return false;
  266.     }
  267.  
  268.     CClientManager::instance().SetPlayerIDStart(iIDStart);
  269.  
  270.     if (CConfig::instance().GetValue("NAME_COLUMN", szBuf, 256))
  271.     {
  272.         fprintf(stderr, "%s %s", g_stLocaleNameColumn.c_str(), szBuf);
  273.         g_stLocaleNameColumn = szBuf;
  274.     }
  275.  
  276.     char szAddr[64], szDB[64], szUser[64], szPassword[64];
  277.     int iPort;
  278.     char line[256+1];
  279.  
  280.     if (CConfig::instance().GetValue("SQL_PLAYER", line, 256))
  281.     {
  282.         sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort);
  283.         sys_log(0, "connecting to MySQL server (player)");
  284.  
  285.         int iRetry = 5;
  286.  
  287.         do
  288.         {
  289.             if (CDBManager::instance().Connect(SQL_PLAYER, szAddr, iPort, szDB, szUser, szPassword))
  290.             {
  291.                 sys_log(0, "   OK");
  292.                 break;
  293.             }
  294.  
  295.             sys_log(0, "   failed, retrying in 5 seconds");
  296.             fprintf(stderr, "   failed, retrying in 5 seconds");
  297.             sleep(5);
  298.         } while (iRetry--);
  299.         fprintf(stderr, "Success PLAYER\n");
  300.         SetPlayerDBName(szDB);
  301.     }
  302.     else
  303.     {
  304.         sys_err("SQL_PLAYER not configured");
  305.         return false;
  306.     }
  307.  
  308.     if (CConfig::instance().GetValue("SQL_ACCOUNT", line, 256))
  309.     {
  310.         sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort);
  311.         sys_log(0, "connecting to MySQL server (account)");
  312.  
  313.         int iRetry = 5;
  314.  
  315.         do
  316.         {
  317.             if (CDBManager::instance().Connect(SQL_ACCOUNT, szAddr, iPort, szDB, szUser, szPassword))
  318.             {
  319.                 sys_log(0, "   OK");
  320.                 break;
  321.             }
  322.  
  323.             sys_log(0, "   failed, retrying in 5 seconds");
  324.             fprintf(stderr, "   failed, retrying in 5 seconds");
  325.             sleep(5);
  326.         } while (iRetry--);
  327.         fprintf(stderr, "Success ACCOUNT\n");
  328.     }
  329.     else
  330.     {
  331.         sys_err("SQL_ACCOUNT not configured");
  332.         return false;
  333.     }
  334.  
  335.     if (CConfig::instance().GetValue("SQL_COMMON", line, 256))
  336.     {
  337.         sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort);
  338.         sys_log(0, "connecting to MySQL server (common)");
  339.  
  340.         int iRetry = 5;
  341.  
  342.         do
  343.         {
  344.             if (CDBManager::instance().Connect(SQL_COMMON, szAddr, iPort, szDB, szUser, szPassword))
  345.             {
  346.                 sys_log(0, "   OK");
  347.                 break;
  348.             }
  349.  
  350.             sys_log(0, "   failed, retrying in 5 seconds");
  351.             fprintf(stderr, "   failed, retrying in 5 seconds");
  352.             sleep(5);
  353.         } while (iRetry--);
  354.         fprintf(stderr, "Success COMMON\n");
  355.     }
  356.     else
  357.     {
  358.         sys_err("SQL_COMMON not configured");
  359.         return false;
  360.     }
  361.  
  362.     if (CConfig::instance().GetValue("SQL_HOTBACKUP", line, 256))
  363.     {
  364.         sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort);
  365.         sys_log(0, "connecting to MySQL server (hotbackup)");
  366.  
  367.         int iRetry = 5;
  368.  
  369.         do
  370.         {
  371.             if (CDBManager::instance().Connect(SQL_HOTBACKUP, szAddr, iPort, szDB, szUser, szPassword))
  372.             {
  373.                 sys_log(0, "   OK");
  374.                 break;
  375.             }
  376.  
  377.             sys_log(0, "   failed, retrying in 5 seconds");
  378.             fprintf(stderr, "   failed, retrying in 5 seconds");
  379.             sleep(5);
  380.         }
  381.         while (iRetry--);
  382.  
  383.         fprintf(stderr, "Success HOTBACKUP\n");
  384.     }
  385.     else
  386.     {
  387.         sys_err("SQL_HOTBACKUP not configured");
  388.         return false;
  389.     }
  390.    
  391.     if (!CNetPoller::instance().Create())
  392.     {
  393.         sys_err("Cannot create network poller");
  394.         return false;
  395.     }
  396.  
  397.     sys_log(0, "ClientManager initialization.. ");
  398.  
  399.     if (!CClientManager::instance().Initialize())
  400.     {
  401.         sys_log(0, "   failed");
  402.         return false;
  403.     }
  404.  
  405.     sys_log(0, "   OK");
  406.  
  407.     if (!PlayerHB::instance().Initialize())
  408.     {
  409.         sys_err("cannot initialize player hotbackup");
  410.         return false;
  411.     }
  412.  
  413. #ifndef __WIN32__
  414.     signal(SIGUSR1, emergency_sig);
  415. #endif
  416.     signal(SIGSEGV, emergency_sig);
  417.     return true;
  418. }
  419.  
  420. void SetTablePostfix(const char* c_pszTablePostfix)
  421. {
  422.     if (!c_pszTablePostfix || !*c_pszTablePostfix)
  423.         g_stTablePostfix = "";
  424.     else
  425.         g_stTablePostfix = c_pszTablePostfix;
  426. }
  427.  
  428. const char * GetTablePostfix()
  429. {
  430.     return g_stTablePostfix.c_str();
  431. }
  432.  
  433. void SetPlayerDBName(const char* c_pszPlayerDBName)
  434. {
  435.     if (! c_pszPlayerDBName || ! *c_pszPlayerDBName)
  436.         g_stPlayerDBName = "";
  437.     else
  438.     {
  439.         g_stPlayerDBName = c_pszPlayerDBName;
  440.         g_stPlayerDBName += ".";
  441.     }
  442. }
  443.  
  444. const char * GetPlayerDBName()
  445. {
  446.     return g_stPlayerDBName.c_str();
  447. }
Add Comment
Please, Sign In to add comment