timer.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include "sol/sol.hpp"
  3. #include "core/structs.h"
  4. #include "util/thread.h"
  5. #include "util/queue.hpp"
  6. #include "module/basemodule.h"
  7. #include <map>
  8. #include <mutex>
  9. namespace module
  10. {
  11. struct timer_info {
  12. std::string filepath;
  13. std::string name;
  14. timestamp exec_msec = 0;
  15. timestamp msec = 0;
  16. sol::function function;
  17. };
  18. /// <summary>
  19. /// 定时器
  20. /// </summary>
  21. class timer:private ylib::ithread,public module::base {
  22. public:
  23. timer();
  24. ~timer();
  25. /// <summary>
  26. /// 增加
  27. /// </summary>
  28. /// <param name="name"></param>
  29. /// <param name="filepath"></param>
  30. /// <param name="msec"></param>
  31. /// <param name="loop"></param>
  32. /// <returns></returns>
  33. std::string add(const std::string& name,const std::string& filepath,int msec, sol::this_state ts);
  34. /// <summary>
  35. /// 移除
  36. /// </summary>
  37. /// <param name="name"></param>
  38. void remove(const std::string& name, sol::this_state ts);
  39. static void regist(sol::state* lua);
  40. virtual void regist_global(const char* name, sol::state* lua) override;
  41. virtual void delete_global() { delete this; }
  42. private:
  43. virtual bool run();
  44. public:
  45. fastweb::app* m_app = nullptr;
  46. std::map<std::string,timer_info> m_list;
  47. ylib::queue<std::string> m_removed;
  48. std::mutex m_mutex;
  49. bool m_insert = false;
  50. };
  51. }