statemanager.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "statemanager.h"
  2. #include "util/file.h"
  3. #include "util/system.h"
  4. #include "util/time.h"
  5. #include "core/define.h"
  6. #include "core/config.h"
  7. #include "core/global.h"
  8. #include "module/http/request.h"
  9. #include "module/http/response.h"
  10. #include "module/http/session.h"
  11. #include "module/http/httpclient.h"
  12. #include "module/mysql.h"
  13. #ifdef _WIN32
  14. #include "module/mssql.h"
  15. #endif
  16. #include "module/localstorage.h"
  17. #include "module/globalfuns.h"
  18. #include "module/mutex.h"
  19. #include "module/codec.h"
  20. #include "module/time.h"
  21. #include "module/file.h"
  22. #include "module/sys.h"
  23. #define LOOP_STATE_USE 1
  24. bool state_manager::start()
  25. {
  26. #if LOOP_STATE_USE == 0
  27. close();
  28. ::ithread::start();
  29. #endif
  30. return true;
  31. }
  32. void state_manager::close()
  33. {
  34. #if LOOP_STATE_USE == 0
  35. ::ithread::stop();
  36. ::ithread::wait();
  37. #endif
  38. sol::state* state = nullptr;
  39. while (m_states.pop(state))
  40. delete state;
  41. }
  42. sol::state* state_manager::create_state()
  43. {
  44. sol::state* lua = new sol::state();
  45. lua->open_libraries(
  46. sol::lib::base,
  47. sol::lib::package,
  48. sol::lib::math,
  49. sol::lib::string,
  50. sol::lib::table,
  51. sol::lib::utf8,
  52. sol::lib::bit32,
  53. sol::lib::coroutine,
  54. sol::lib::count,
  55. sol::lib::ffi,
  56. sol::lib::io,
  57. sol::lib::jit,
  58. sol::lib::os
  59. );
  60. {
  61. // 获取当前的package.path,添加新的搜索路径
  62. std::string current_path = (*lua)["package"]["path"]; // 获取当前的路径
  63. current_path += ";"+ sConfig->scripts.lib_dir +"/?.lua"; // 添加新的路径
  64. (*lua)["package"]["path"] = current_path; // 设置修改后的路径
  65. }
  66. module::request::regist(*lua);
  67. module::response::regist(*lua);
  68. module::session::regist(*lua);
  69. module::httpclient::regist(*lua);
  70. module::mysql_regist(*lua);
  71. #ifdef _WIN32
  72. module::mssql::regist(*lua);
  73. #endif
  74. module::regist_globalfuns(*lua);
  75. module::local_storage::regist(lua);
  76. module::mutex::regist(lua);
  77. module::auto_lock::regist(lua);
  78. module::codec::regist(lua);
  79. module::time::regist(lua);
  80. module::file::regist(lua);
  81. module::sys::regist(lua);
  82. global::getInstance()->regist_lua(lua);
  83. return lua;
  84. }
  85. sol::state* state_manager::get_state()
  86. {
  87. sol::state* result = nullptr;
  88. if (m_states.pop(result))
  89. return result;
  90. return create_state();
  91. }
  92. void state_manager::push_state(sol::state* state)
  93. {
  94. if (state == nullptr)
  95. return;
  96. #if LOOP_STATE_USE == 1
  97. m_states.push(state);
  98. #else
  99. m_delete_states.push(state);
  100. #endif
  101. }
  102. bool state_manager::run()
  103. {
  104. #if LOOP_STATE_USE == 0
  105. auto now_msec = time::now_msec();
  106. // 虚拟机缓冲保证
  107. if(sConfig->scripts.lua_cache_size > m_states.size())
  108. {
  109. std::vector<sol::state*> bhvalues;
  110. auto bhcount = sConfig->scripts.lua_cache_size - m_states.size();
  111. if (bhcount > 0 && bhcount <= sConfig->scripts.lua_cache_size)
  112. {
  113. for (size_t i = 0; i < bhcount; i++)
  114. bhvalues.push_back(create_state());
  115. }
  116. for(size_t i=0;i< bhvalues.size();i++)
  117. m_states.push(bhvalues[i]);
  118. }
  119. // 释放虚拟机
  120. {
  121. sol::state* state = nullptr;
  122. while (m_delete_states.pop(state))
  123. delete state;
  124. }
  125. system::sleep_msec(100);
  126. return true;
  127. #else
  128. return false;
  129. #endif
  130. }