timer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "timer.h"
  2. #include "util/time.h"
  3. #include "util/system.h"
  4. #include "util/file.h"
  5. #include "core/config.h"
  6. #include "core/statemanager.h"
  7. module::timer::~timer()
  8. {
  9. ::ithread::stop();
  10. ::ithread::wait();
  11. }
  12. std::string module::timer::add(const std::string& name, const std::string& filepath, const std::string& funname, int msec, bool loop)
  13. {
  14. std::string filepath2;
  15. if (ylib::file::exist(sConfig->scripts.app_dir + "/" + filepath))
  16. filepath2 = sConfig->scripts.app_dir + "/" + filepath;
  17. else if (ylib::file::exist(sConfig->scripts.lib_dir + "/" + filepath))
  18. filepath2 = sConfig->scripts.lib_dir + "/" + filepath;
  19. else
  20. return "not found script lua, App: " + std::string(sConfig->scripts.app_dir + "/" + filepath) + "\r\n" + std::string(sConfig->scripts.lib_dir + "/" + filepath);
  21. std::unique_lock<std::mutex> uni(module::timer::getInstance()->m_mutex);
  22. auto iter = module::timer::getInstance()->m_list.find(name);
  23. if (iter != module::timer::getInstance()->m_list.end())
  24. return "exist name `" + name + "`";
  25. timer_info ti;
  26. ti.name = name;
  27. ti.funname = funname;
  28. ti.filepath = filepath2;
  29. ti.loop = loop;
  30. ti.msec = msec;
  31. ti.exec_msec = time::now_msec() + msec;
  32. module::timer::getInstance()->m_list.emplace(name, ti);
  33. module::timer::getInstance()->m_insert = true;
  34. return "";
  35. }
  36. void module::timer::remove(const std::string& name)
  37. {
  38. module::timer::getInstance()->m_removed.push(name);
  39. //std::unique_lock<std::mutex> uni(module::timer::getInstance()->m_mutex);
  40. //module::timer::getInstance()->m_list.erase(name);
  41. }
  42. void module::timer::regist(sol::state* lua)
  43. {
  44. lua->new_usertype<module::timer>("timer",
  45. "add", &module::timer::add,
  46. "remove", &module::timer::remove
  47. );
  48. }
  49. module::timer::timer()
  50. {
  51. ::ithread::start();
  52. }
  53. bool module::timer::run()
  54. {
  55. auto comp_wait_msec = [&]()->timestamp {
  56. timestamp wait_msec = 0;
  57. auto now_msec = time::now_msec();
  58. std::unique_lock<std::mutex> uni(m_mutex);
  59. for_iter(iter, m_list)
  60. {
  61. if (iter->second.exec_msec <= now_msec)
  62. return 0;
  63. if (iter->second.exec_msec - now_msec < wait_msec || wait_msec == 0)
  64. wait_msec = iter->second.exec_msec - now_msec;
  65. }
  66. if (wait_msec == 0)
  67. return 1000;
  68. return wait_msec;
  69. };
  70. auto exec_func = [&](const std::string& filepath,const std::string& funname)->bool {
  71. auto lua = sStateMgr->get();
  72. std::string exception_string;
  73. try
  74. {
  75. auto lbResult = lua->state->load_file(filepath);
  76. if (lbResult.valid() == false)
  77. {
  78. sol::error err = lbResult;
  79. throw ylib::exception("Failed to load script, " + std::string(err.what()));
  80. }
  81. lbResult();
  82. if (funname.empty() == false)
  83. {
  84. auto result = (*lua->state)[funname]();
  85. if (!result.valid()) {
  86. sol::error err = result;
  87. throw ylib::exception(err.what());
  88. }
  89. bool rd = result;
  90. return rd;
  91. }
  92. }
  93. catch (const std::exception& e)
  94. {
  95. exception_string = e.what();
  96. if (sConfig->website.debug)
  97. LOG_ERROR("[timer][" + filepath + "]: " + e.what());
  98. return false;
  99. }
  100. return false;
  101. };
  102. // 获取等待时间
  103. auto wait_msec = comp_wait_msec();
  104. for (size_t i = 0; i < wait_msec/10; i++)
  105. {
  106. if (module::timer::getInstance()->m_insert)
  107. break;
  108. system::sleep_msec(10);
  109. }
  110. module::timer::getInstance()->m_insert = false;
  111. std::unique_lock<std::mutex> uni(m_mutex);
  112. auto now_msec = time::now_msec();
  113. for (auto iter = m_list.begin(); iter != m_list.end();)
  114. {
  115. if (iter->second.exec_msec <= now_msec)
  116. {
  117. bool ready_remove = false;
  118. auto ti = iter->second;
  119. if (exec_func(ti.filepath,ti.funname) == false)
  120. ready_remove = true;
  121. if (ready_remove == false)
  122. {
  123. if (iter->second.loop == false)
  124. ready_remove = true;
  125. else
  126. {
  127. iter->second.exec_msec = now_msec + iter->second.msec;
  128. }
  129. }
  130. if (ready_remove)
  131. iter = m_list.erase(iter);
  132. else
  133. iter++;
  134. }
  135. else
  136. iter++;
  137. }
  138. std::string name;
  139. while (m_removed.pop(name))
  140. m_list.erase(name);
  141. return true;
  142. }