From d7594a2ee8b6a52e927e62dc0937cb54bbf1d524 Mon Sep 17 00:00:00 2001 From: xx Date: Mon, 3 Jun 2024 19:08:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/config.cpp | 3 + src/core/config.h | 2 +- src/core/fastweb.cpp | 3 + src/core/fastweb.h | 2 +- src/core/global.cpp | 4 + src/core/global.h | 2 +- src/core/interceptormanager.cpp | 2 +- src/core/modulemanager.cpp | 8 +- src/core/modulemanager.h | 7 +- src/core/subscribemanager.cpp | 2 +- src/module/file.cpp | 12 ++- src/module/file.h | 2 + src/module/timer.cpp | 151 ++++++++++++++++++++++++++++++++ src/module/timer.h | 43 +++++++++ 14 files changed, 227 insertions(+), 16 deletions(-) create mode 100644 src/module/timer.cpp create mode 100644 src/module/timer.h diff --git a/src/core/config.cpp b/src/core/config.cpp index cab91a1..e31b71a 100644 --- a/src/core/config.cpp +++ b/src/core/config.cpp @@ -1,5 +1,8 @@ #include "config.h" #include +config::config() +{ +} bool config::open(const std::string& ini_filepath) { if (ylib::file::exist(ini_filepath) == false) diff --git a/src/core/config.h b/src/core/config.h index 65f3f38..9272e52 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -35,7 +35,7 @@ public: std::vector domain; }; public: - config() = default; + config(); bool open(const std::string& ini_filepath); std::vector lua_app_files(); diff --git a/src/core/fastweb.cpp b/src/core/fastweb.cpp index 73cbc25..a1534d1 100644 --- a/src/core/fastweb.cpp +++ b/src/core/fastweb.cpp @@ -12,6 +12,9 @@ #include "core/statemanager.h" #include "core/subscribemanager.h" #include "core/interceptormanager.h" +fastweb::fastweb() +{ +} bool fastweb::start() { diff --git a/src/core/fastweb.h b/src/core/fastweb.h index 8486d58..9a4621e 100644 --- a/src/core/fastweb.h +++ b/src/core/fastweb.h @@ -7,7 +7,7 @@ #include "core/interceptormanager.h" class fastweb:public ylib::error_base,public ylib::singleton { public: - fastweb() = default; + fastweb(); bool start(); void stop(); private: diff --git a/src/core/global.cpp b/src/core/global.cpp index d71bdce..048ca15 100644 --- a/src/core/global.cpp +++ b/src/core/global.cpp @@ -1,6 +1,10 @@  #include "global.h" #include "module/imodule.h" +global::global() +{ + +} void global::regist_lua(sol::state* lua) { m_value_ptr.lock(); diff --git a/src/core/global.h b/src/core/global.h index ea365a6..6db26e8 100644 --- a/src/core/global.h +++ b/src/core/global.h @@ -5,7 +5,7 @@ #include "util/map.hpp" class global :public ylib::error_base,public ylib::singleton{ public: - global() = default; + global(); void regist_lua(sol::state* lua); diff --git a/src/core/interceptormanager.cpp b/src/core/interceptormanager.cpp index 40dbe49..d6be49c 100644 --- a/src/core/interceptormanager.cpp +++ b/src/core/interceptormanager.cpp @@ -42,7 +42,7 @@ bool interceptor_manager::callback(network::http::reqpack* reqpack, const std::s if (lbResult.valid() == false) { sol::error err = lbResult; - throw ylib::exception("[interceptor] Failed to load bytecode, " + std::string(err.what())); + throw ylib::exception("[interceptor] Failed to load script, " + std::string(err.what())); } module::request m_request(reqpack->request()); module::response m_response(reqpack->response()); diff --git a/src/core/modulemanager.cpp b/src/core/modulemanager.cpp index 444842e..b3fc717 100644 --- a/src/core/modulemanager.cpp +++ b/src/core/modulemanager.cpp @@ -25,15 +25,10 @@ #include "module/time.h" #include "module/file.h" #include "module/sys.h" +#include "module/timer.h" module_manager::module_manager() { - } - -module_manager::~module_manager() -{ -} - void module_manager::start() { close(); @@ -122,6 +117,7 @@ void module_manager::load_core(sol::state* lua) module::time::regist(lua); module::file::regist(lua); module::sys::regist(lua); + module::timer::regist(lua); global::getInstance()->regist_lua(lua); diff --git a/src/core/modulemanager.h b/src/core/modulemanager.h index 4c44189..7170e05 100644 --- a/src/core/modulemanager.h +++ b/src/core/modulemanager.h @@ -2,6 +2,7 @@ #include #include "sol/sol.hpp" #include "core/structs.h" +#include "base/singleton.hpp" typedef int (*fastweb_module_regist)(void*, void*); struct module_info { void* dll = nullptr; @@ -11,15 +12,13 @@ struct module_info { /// /// 模块管理器 /// -class module_manager{ +class module_manager :public ylib::error_base, public ylib::singleton { public: module_manager(); - ~module_manager(); - void start(); void close(); /// - /// 创建虚拟机 + /// 加载虚拟机库 /// /// void load(sol::state* lua); diff --git a/src/core/subscribemanager.cpp b/src/core/subscribemanager.cpp index 60dad21..13c161b 100644 --- a/src/core/subscribemanager.cpp +++ b/src/core/subscribemanager.cpp @@ -115,7 +115,7 @@ void subscribe_manager::callback(network::http::request* request, network::http: if (lbResult.valid() == false) { sol::error err = lbResult; - throw ylib::exception("Failed to load bytecode, " + std::string(err.what())); + throw ylib::exception("Failed to load script, " + std::string(err.what())); } module::request m_request(request); module::response m_response(response); diff --git a/src/module/file.cpp b/src/module/file.cpp index 14a12ed..8e2e892 100644 --- a/src/module/file.cpp +++ b/src/module/file.cpp @@ -17,6 +17,14 @@ sol::table module::file::list(const std::string& dirpath, const std::string& reg } return result_table; } +void module::file::copy_dir(const std::string& src_dir, const std::string& dst_dir) +{ + ylib::file::copy_dir(src_dir, dst_dir); +} +bool module::file::create_dir(const std::string& dirpath) +{ + return ylib::file::create_dir(dirpath,true); +} void module::file::regist(sol::state* lua) { (*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE; @@ -24,6 +32,8 @@ void module::file::regist(sol::state* lua) lua->new_usertype("file", - "list", &module::file::list + "list", &module::file::list, + "copy_dir", &module::file::copy_dir, + "create_dir", &module::file::create_dir ); } diff --git a/src/module/file.h b/src/module/file.h index 0974569..5ea072e 100644 --- a/src/module/file.h +++ b/src/module/file.h @@ -10,6 +10,8 @@ namespace module class file:public module::imodule { public: static sol::table list(const std::string& dirpath,const std::string& regex,sol::this_state s); + static void copy_dir(const std::string& src_dir,const std::string& dst_dir); + static bool create_dir(const std::string& dirpath); static void regist(sol::state* lua); }; diff --git a/src/module/timer.cpp b/src/module/timer.cpp new file mode 100644 index 0000000..ea6a336 --- /dev/null +++ b/src/module/timer.cpp @@ -0,0 +1,151 @@ +#include "timer.h" +#include "util/time.h" +#include "util/system.h" +#include "util/file.h" +#include "core/config.h" +#include "core/statemanager.h" +module::timer::~timer() +{ + ::ithread::stop(); + ::ithread::wait(); +} +std::string module::timer::add(const std::string& name, const std::string& filepath, const std::string& funname, int msec, bool loop) +{ + std::string filepath2; + if (ylib::file::exist(sConfig->scripts.app_dir + "/" + filepath)) + filepath2 = sConfig->scripts.app_dir + "/" + filepath; + else if (ylib::file::exist(sConfig->scripts.lib_dir + "/" + filepath)) + filepath2 = sConfig->scripts.lib_dir + "/" + filepath; + else + return "not found script lua, App: " + std::string(sConfig->scripts.app_dir + "/" + filepath) + "\r\n" + std::string(sConfig->scripts.lib_dir + "/" + filepath); + + std::unique_lock uni(module::timer::getInstance()->m_mutex); + + auto iter = module::timer::getInstance()->m_list.find(name); + if (iter != module::timer::getInstance()->m_list.end()) + return "exist name `" + name + "`"; + + timer_info ti; + ti.name = name; + ti.funname = funname; + ti.filepath = filepath2; + ti.loop = loop; + ti.msec = msec; + ti.exec_msec = time::now_msec() + msec; + module::timer::getInstance()->m_list.emplace(name, ti); + module::timer::getInstance()->m_insert = true; + return ""; +} +void module::timer::remove(const std::string& name) +{ + module::timer::getInstance()->m_removed.push(name); + //std::unique_lock uni(module::timer::getInstance()->m_mutex); + //module::timer::getInstance()->m_list.erase(name); +} +void module::timer::regist(sol::state* lua) +{ + lua->new_usertype("timer", + "add", &module::timer::add, + "remove", &module::timer::remove + ); +} + +module::timer::timer() +{ + ::ithread::start(); +} + +bool module::timer::run() +{ + + auto comp_wait_msec = [&]()->timestamp { + timestamp wait_msec = 0; + auto now_msec = time::now_msec(); + std::unique_lock uni(m_mutex); + for_iter(iter, m_list) + { + if (iter->second.exec_msec <= now_msec) + return 0; + if (iter->second.exec_msec - now_msec < wait_msec || wait_msec == 0) + wait_msec = iter->second.exec_msec - now_msec; + } + if (wait_msec == 0) + return 1000; + return wait_msec; + }; + auto exec_func = [&](const std::string& filepath,const std::string& funname)->bool { + auto lua = sStateMgr->get(); + std::string exception_string; + try + { + auto lbResult = lua->state->load_file(filepath); + if (lbResult.valid() == false) + { + sol::error err = lbResult; + throw ylib::exception("Failed to load script, " + std::string(err.what())); + } + lbResult(); + + if (funname.empty() == false) + { + auto result = (*lua->state)[funname](); + if (!result.valid()) { + sol::error err = result; + throw ylib::exception(err.what()); + } + bool rd = result; + return rd; + } + } + catch (const std::exception& e) + { + exception_string = e.what(); + if (sConfig->website.debug) + LOG_ERROR("[timer][" + filepath + "]: " + e.what()); + return false; + } + return false; + }; + // 获取等待时间 + auto wait_msec = comp_wait_msec(); + for (size_t i = 0; i < wait_msec/10; i++) + { + if (module::timer::getInstance()->m_insert) + break; + system::sleep_msec(10); + } + module::timer::getInstance()->m_insert = false; + + std::unique_lock uni(m_mutex); + auto now_msec = time::now_msec(); + for (auto iter = m_list.begin(); iter != m_list.end();) + { + if (iter->second.exec_msec <= now_msec) + { + bool ready_remove = false; + auto ti = iter->second; + if (exec_func(ti.filepath,ti.funname) == false) + ready_remove = true; + if (ready_remove == false) + { + if (iter->second.loop == false) + ready_remove = true; + else + { + iter->second.exec_msec = now_msec + iter->second.msec; + } + } + if (ready_remove) + iter = m_list.erase(iter); + else + iter++; + } + else + iter++; + } + std::string name; + while (m_removed.pop(name)) + m_list.erase(name); + + return true; +} diff --git a/src/module/timer.h b/src/module/timer.h new file mode 100644 index 0000000..2294cd9 --- /dev/null +++ b/src/module/timer.h @@ -0,0 +1,43 @@ +#pragma once +#include "sol/sol.hpp" +#include "imodule.h" +#include "base/singleton.hpp" +#include "util/thread.h" +#include "util/queue.hpp" +#include +#include +namespace module +{ + struct timer_info { + std::string filepath; + std::string name; + std::string funname; + bool loop = false; + timestamp exec_msec = 0; + timestamp msec = 0; + }; + /// + /// 定时器 + /// + class timer:public ylib::singleton,private ylib::ithread { + public: + timer(); + ~timer(); + + static std::string add(const std::string& name,const std::string& filepath,const std::string& funname,int msec,bool loop); + static void remove(const std::string& name); + + + static void regist(sol::state* lua); + private: + + virtual bool run(); + public: + std::map m_list; + ylib::queue m_removed; + std::mutex m_mutex; + bool m_insert = false; + }; + +} +