xx 2 лет назад
Родитель
Сommit
ea0b8a70e1

+ 3 - 0
src/core/config.cpp

@@ -1,5 +1,8 @@
 #include "config.h"
 #include <regex>
+config::config()
+{
+}
 bool config::open(const std::string& ini_filepath)
 {
 	if (ylib::file::exist(ini_filepath) == false)

+ 1 - 1
src/core/config.h

@@ -35,7 +35,7 @@ public:
 		std::vector<std::string> domain;
 	};
 public:
-	config() = default;
+	config();
 	bool open(const std::string& ini_filepath);
 
 	std::vector<std::string> lua_app_files();

+ 3 - 0
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()
 {
 

+ 1 - 1
src/core/fastweb.h

@@ -7,7 +7,7 @@
 #include "core/interceptormanager.h"
 class fastweb:public ylib::error_base,public ylib::singleton<fastweb> {
 public:
-	fastweb() = default;
+	fastweb();
 	bool start();
 	void stop();
 private:

+ 4 - 0
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();

+ 1 - 1
src/core/global.h

@@ -5,7 +5,7 @@
 #include "util/map.hpp"
 class global :public ylib::error_base,public ylib::singleton<global>{
 public:
-	global() = default;
+	global();
 
 	void regist_lua(sol::state* lua);
 

+ 1 - 1
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());

+ 2 - 6
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);
 

+ 3 - 4
src/core/modulemanager.h

@@ -2,6 +2,7 @@
 #include <map>
 #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 {
 /// <summary>
 /// 模块管理器
 /// </summary>
-class module_manager{
+class module_manager :public ylib::error_base, public ylib::singleton<module_manager> {
 public:
 	module_manager();
-	~module_manager();
-
 	void start();
 	void close();
 	/// <summary>
-	/// 创建虚拟机
+	/// 加载虚拟机库
 	/// </summary>
 	/// <returns></returns>
 	void load(sol::state* lua);

+ 1 - 1
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);

+ 11 - 1
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<module::file>("file",
-        "list", &module::file::list
+        "list", &module::file::list,
+        "copy_dir", &module::file::copy_dir,
+        "create_dir", &module::file::create_dir
     );
 }

+ 2 - 0
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);
 	};
 

+ 151 - 0
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<std::mutex> 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<std::mutex> uni(module::timer::getInstance()->m_mutex);
+    //module::timer::getInstance()->m_list.erase(name);
+}
+void module::timer::regist(sol::state* lua)
+{
+    lua->new_usertype<module::timer>("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<std::mutex> 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<std::mutex> 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;
+}

+ 43 - 0
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 <map>
+#include <mutex>
+namespace module
+{
+	struct timer_info {
+		std::string filepath;
+		std::string name;
+		std::string funname;
+		bool loop = false;
+		timestamp exec_msec = 0;
+		timestamp msec = 0;
+	};
+	/// <summary>
+	/// 定时器
+	/// </summary>
+	class timer:public ylib::singleton<timer>,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<std::string,timer_info> m_list;
+		ylib::queue<std::string> m_removed;
+		std::mutex m_mutex;
+		bool m_insert = false;
+	};
+
+}
+