From f9174cd028dbbeb90b522c9f98262f675c0594f1 Mon Sep 17 00:00:00 2001 From: xx Date: Thu, 6 Jun 2024 21:31:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=95=B4=E7=90=86=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdparty/sol/state.hpp | 5 +- CMakeLists.txt | 2 +- src/core/Interface.hpp | 19 ++++ src/core/{fastweb.cpp => app.cpp} | 75 +++++++++----- src/core/app.h | 49 +++++++++ src/core/config.cpp | 17 +-- src/core/config.h | 126 ++++++++++++----------- src/core/define.h | 26 ++--- src/core/entry.cpp | 33 +++--- src/core/entry.h | 4 +- src/core/fastweb.h | 28 ----- src/core/global.cpp | 52 +++++----- src/core/global.h | 68 ++++++++---- src/core/global_module.cpp | 27 +++++ src/core/global_module.h | 32 ++++++ src/core/interceptormanager.cpp | 42 ++++---- src/core/interceptormanager.h | 45 ++++---- src/{utils/logutils.cpp => core/log.cpp} | 35 ++++--- src/core/log.h | 26 +++++ src/core/lualibdetecter.cpp | 9 +- src/core/lualibdetecter.h | 32 +++--- src/core/modulemanager.cpp | 45 ++++---- src/core/modulemanager.h | 91 ++++++++-------- src/core/statemanager.cpp | 33 ++++-- src/core/statemanager.h | 82 ++++++++------- src/core/subscribemanager.cpp | 35 ++++--- src/core/subscribemanager.h | 61 +++++------ src/module/{imodule.h => basemodule.h} | 10 +- src/module/codec.h | 4 +- src/module/file.cpp | 90 ---------------- src/module/filesystem.cpp | 90 ++++++++++++++++ src/module/{file.h => filesystem.h} | 5 +- src/module/globalfuns.cpp | 66 ++++++++---- src/module/globalfuns.h | 61 +++++++---- src/module/http/request.cpp | 2 +- src/module/http/request.h | 2 +- src/module/localstorage.h | 4 +- src/module/mssql.cpp | 1 + src/module/mssql.h | 2 +- src/module/mutex.h | 4 +- src/module/mysql.cpp | 2 +- src/module/mysql.h | 6 +- src/module/sys.h | 5 +- src/module/time.h | 5 +- src/module/timer.cpp | 63 +++++++----- src/module/timer.h | 16 +-- src/utils/logutils.h | 22 ---- tests/main.cpp | 12 ++- 48 files changed, 919 insertions(+), 652 deletions(-) create mode 100644 src/core/Interface.hpp rename src/core/{fastweb.cpp => app.cpp} (57%) create mode 100644 src/core/app.h delete mode 100644 src/core/fastweb.h create mode 100644 src/core/global_module.cpp create mode 100644 src/core/global_module.h rename src/{utils/logutils.cpp => core/log.cpp} (62%) create mode 100644 src/core/log.h rename src/module/{imodule.h => basemodule.h} (76%) delete mode 100644 src/module/file.cpp create mode 100644 src/module/filesystem.cpp rename src/module/{file.h => filesystem.h} (94%) delete mode 100644 src/utils/logutils.h diff --git a/3rdparty/sol/state.hpp b/3rdparty/sol/state.hpp index ed2412e..095a85c 100644 --- a/3rdparty/sol/state.hpp +++ b/3rdparty/sol/state.hpp @@ -26,7 +26,10 @@ #include #include - +namespace fastweb +{ + class app; +} namespace sol { class state : private std::unique_ptr, public state_view { diff --git a/CMakeLists.txt b/CMakeLists.txt index df0ee9f..95ffca9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) # 设置自定义配置类型 if(MSVC) set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Build config types" FORCE) -endif() +endif() # C++等级 set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) diff --git a/src/core/Interface.hpp b/src/core/Interface.hpp new file mode 100644 index 0000000..baee2f0 --- /dev/null +++ b/src/core/Interface.hpp @@ -0,0 +1,19 @@ +#pragma once +#include "base/error.h" + +namespace fastweb +{ + class app; + class Interface :public ylib::error_base + { + public: + Interface(fastweb::app* ptr) + { + this->m_ptr = ptr; + } + fastweb::app* app() { return this->m_ptr; } + public: + fastweb::app* m_ptr = nullptr; + }; +} + diff --git a/src/core/fastweb.cpp b/src/core/app.cpp similarity index 57% rename from src/core/fastweb.cpp rename to src/core/app.cpp index a1534d1..6c8b057 100644 --- a/src/core/fastweb.cpp +++ b/src/core/app.cpp @@ -1,4 +1,4 @@ -#include "fastweb.h" +#include "app.h" #include #include "net/http_website.h" #include "net/http_router.h" @@ -9,20 +9,34 @@ #include "module/globalfuns.h" #include "core/config.h" #include "core/global.h" -#include "core/statemanager.h" -#include "core/subscribemanager.h" -#include "core/interceptormanager.h" -fastweb::fastweb() +fastweb::app::app() { + log = std::make_shared(this); + subscribe = std::make_shared(this); + interceptor = std::make_shared(this); + state = std::make_shared(this); + config = std::make_shared(this); + global = std::make_shared(this); + gloabl_module = std::make_shared(this); } -bool fastweb::start() +fastweb::app::~app() +{ + stop(); +} +bool fastweb::app::start(const std::string& config_filepath) { stop(); - // 虚拟机管理器 - if(sStateMgr->start() == false) + // 初始化配置 + if (config->open(config_filepath) == false) { - m_lastErrorDesc = sStateMgr->last_error(); + m_lastErrorDesc = "open config failed, " + config->last_error(); + return false; + } + // 虚拟机管理器 + if(state->start() == false) + { + m_lastErrorDesc = state->last_error(); return false; } @@ -30,7 +44,7 @@ bool fastweb::start() network::http::start_config config; network::http::website_config ws_config; - for_iter(iter, sConfig->domain) + for_iter(iter, this->config->domain) { network::http::host_config host_config; host_config.domain = iter->first; @@ -45,17 +59,17 @@ bool fastweb::start() url = "http://"; url += host_config.domain; url += ":" + std::to_string(host_config.port); - LOG_WARN("URL: "+url); + log->warn("URL: " + url, __FILE__, __func__, __LINE__); } ws_config.name = "master"; ws_config.router.threadpool.size = 40; ws_config.router.threadpool.queuemax = 10000; - ws_config.session.dirpath = sConfig->website.session_dir; - ws_config.session.timeout_sec = sConfig->website.session_timeout_sec; + ws_config.session.dirpath = this->config->website.session_dir; + ws_config.session.timeout_sec = this->config->website.session_timeout_sec; config.website.push_back(ws_config); - for_iter(iter, sConfig->domain) + for_iter(iter, this->config->domain) { if(iter->second.https) config.cert.emplace(iter->first, iter->second.ssl); @@ -73,22 +87,24 @@ bool fastweb::start() // 初始化脚本 if (initialization_script() == false) return false; + // 加载订阅 - m_subscribe.load(router); + subscribe->load(router); // 加载拦截器 - m_interceptor.load(router); + interceptor->load(router); return m_center->start(); } -void fastweb::stop() +void fastweb::app::stop() { - m_subscribe.clear(); - m_interceptor.clear(); + this->m_lastErrorDesc.clear(); + subscribe->clear(); + interceptor->clear(); if (m_center != nullptr) { m_center->close(); @@ -96,16 +112,16 @@ void fastweb::stop() } m_center = nullptr; - global::getInstance()->clear(); + global->clear(); if (m_state_init != nullptr) delete m_state_init; m_state_init = nullptr; - sStateMgr->close(); + state->close(); } -bool fastweb::initialization_script() +bool fastweb::app::initialization_script() { - auto script_filepath = sConfig->website.Initialization_script; + auto script_filepath = this->config->website.Initialization_script; if (script_filepath == "") return true; if (ylib::file::exist(script_filepath) == false) @@ -113,18 +129,23 @@ bool fastweb::initialization_script() m_lastErrorDesc = "Initialization script not found, filepath: " + script_filepath; return false; } - auto state = sStateMgr->get(); + auto state = this->state->get(); try { - state->state->set_function("global_regist", module::global_regist); - auto result = state->state->script_file(script_filepath); + sol::load_result script = state->state->load_file(script_filepath); + if (!script.valid()) { + sol::error err = script; + throw ylib::exception(err.what()); + } + + sol::protected_function_result result = script(); if (!result.valid()) { sol::error err = result; throw ylib::exception(err.what()); } if (result.get() == false) { - throw ylib::exception("user interrupt"); + throw ylib::exception("user interrupt: `return false`"); } } catch (const std::exception& e) diff --git a/src/core/app.h b/src/core/app.h new file mode 100644 index 0000000..6ce624c --- /dev/null +++ b/src/core/app.h @@ -0,0 +1,49 @@ +#pragma once +#include "define.h" +#include "base/error.h" + +#include "net/http_center.h" +#include "core/subscribemanager.h" +#include "core/interceptormanager.h" +#include "core/statemanager.h" +#include "core/config.h" +#include "core/log.h" +#include "core/global.h" +#include "core/global_module.h" + +namespace fastweb +{ + class app :public ylib::error_base { + public: + app(); + ~app(); + bool start(const std::string& config_filepath); + void stop(); + private: + /// + /// 初始化执行脚本 + /// + /// + bool initialization_script(); + private: + // 初始化脚本虚拟机 + luastate* m_state_init = nullptr; + // 网站服务核心 + network::http::center* m_center = nullptr; + public: + // 日志 + std::shared_ptr log; + // 订阅管理器 + std::shared_ptr subscribe; + // 拦截器管理器 + std::shared_ptr interceptor; + // LUA状态管理器 + std::shared_ptr state; + // 配置 + std::shared_ptr config; + // 应用全局变量 + std::shared_ptr global; + // 全局托管模块 + std::shared_ptr gloabl_module; + }; +} diff --git a/src/core/config.cpp b/src/core/config.cpp index f6af9f4..5e89936 100644 --- a/src/core/config.cpp +++ b/src/core/config.cpp @@ -1,9 +1,10 @@ #include "config.h" #include -config::config() +#include "core/app.h" +fastweb::config::config(fastweb::app* ptr):Interface(ptr) { } -bool config::open(const std::string& ini_filepath) +bool fastweb::config::open(const std::string& ini_filepath) { if (ylib::file::exist(ini_filepath) == false) { @@ -47,17 +48,17 @@ bool config::open(const std::string& ini_filepath) cache(); return true; } -std::vector config::lua_lib_files() +std::vector fastweb::config::lua_lib_files() { std::vector results; - for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++) + for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++) { - auto luas = ylib::file::traverse(sConfig->scripts.lib_dir[i], "(.*\\.lua)"); + auto luas = ylib::file::traverse(app()->config->scripts.lib_dir[i], "(.*\\.lua)"); for_iter(iter, luas) { if (iter->second == IS_DIRECTORY) continue; - std::string path = sConfig->scripts.lib_dir[i]+"/"+ strutils::replace(iter->first, '\\', '/'); + std::string path = app()->config->scripts.lib_dir[i]+"/"+ strutils::replace(iter->first, '\\', '/'); results.push_back(path); } } @@ -69,7 +70,7 @@ std::vector config::lua_lib_files() return results; } -std::vector config::extractVariableNames(const std::string& text) +std::vector fastweb::config::extractVariableNames(const std::string& text) { std::regex pattern("\\$\\{([^}]+)\\}"); // 使用捕获组提取中间的内容 std::vector results; @@ -83,7 +84,7 @@ std::vector config::extractVariableNames(const std::string& text) return results; } -void config::cache() +void fastweb::config::cache() { scripts.lib_dir = ylib::json::from(m_ini.read("scripts", "lib_dir")).to>(); diff --git a/src/core/config.h b/src/core/config.h index af59f03..a15bd8e 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -1,69 +1,73 @@ #pragma once #include "define.h" #include "base/error.h" -#include "base/singleton.hpp" #include "util/ini.h" #include "net/http_define.h" -class config:public ylib::error_base,public ylib::singleton{ -public: - struct domain { - bool https = false; - ushort port = 0; - network::http::ssl_config ssl; - }; - struct scripts { - std::vector lib_dir; - std::string module_dir; - uint32 lua_cache_size = 0; - uint32 auto_update_sec = 0; - }; - struct website { - struct __interceptor { - std::string filepath; - std::string regex_express; +namespace fastweb +{ + class config :public Interface { + public: + struct domain { + bool https = false; + ushort port = 0; + network::http::ssl_config ssl; + }; + struct scripts { + std::vector lib_dir; + std::string module_dir; + uint32 lua_cache_size = 0; + uint32 auto_update_sec = 0; + }; + struct website { + struct __interceptor { + std::string filepath; + std::string regex_express; + }; + std::string dir; + std::string default_404; + std::vector default_index; + std::string session_dir; + uint32 session_timeout_sec; + std::string Initialization_script; + std::vector<__interceptor> interceptor_scripts; + bool debug = false; + std::vector domain; }; - std::string dir; - std::string default_404; - std::vector default_index; - std::string session_dir; - uint32 session_timeout_sec; - std::string Initialization_script; - std::vector<__interceptor> interceptor_scripts; - bool debug = false; - std::vector domain; - }; - struct log { - bool enable = false; - std::string dir; - std::string name; - bool succ = false; - bool info = false; - bool warn = false; - bool error = false; - }; -public: - config(); - bool open(const std::string& ini_filepath); + struct log { + bool enable = false; + std::string dir; + std::string name; + bool succ = false; + bool info = false; + bool warn = false; + bool error = false; + }; + public: + config(fastweb::app* ptr); + bool open(const std::string& ini_filepath); - std::vector lua_lib_files(); -private: - // INI配置文件 - ylib::ini m_ini; -private: - /// - /// 查询所有变量字符串 - /// - /// - /// - std::vector extractVariableNames(const std::string& text); - /// - /// 缓存配置 - /// - void cache(); -public: - scripts scripts; - website website; - log log; - std::map domain; -}; \ No newline at end of file + std::vector lua_lib_files(); + private: + // INI配置文件 + ylib::ini m_ini; + private: + /// + /// 查询所有变量字符串 + /// + /// + /// + std::vector extractVariableNames(const std::string& text); + /// + /// 缓存配置 + /// + void cache(); + public: + scripts scripts; + website website; + log log; + std::map domain; + + + }; +} diff --git a/src/core/define.h b/src/core/define.h index 1711d43..3717b23 100644 --- a/src/core/define.h +++ b/src/core/define.h @@ -2,31 +2,23 @@ #include #include #include "sol/sol.hpp" - #include "base/define.h" - - #include "util/system.h" #include "util/codec.h" #include "util/strutils.h" #include "util/file.h" - -#include "utils/logutils.h" -#include - -#define LOG_ERROR(MSG) LogUtils::getInstance()->error(MSG,__FILE__ ,__func__,__LINE__ ) -#define LOG_WARN(MSG) LogUtils::getInstance()->warn(MSG,__FILE__ ,__func__,__LINE__ ) -#define LOG_INFO(MSG) LogUtils::getInstance()->info(MSG,__FILE__ ,__func__,__LINE__ ) -#define LOG_SUCC(MSG) LogUtils::getInstance()->success(MSG,__FILE__ ,__func__,__LINE__ ) -#define LOG_DEBUG(MSG) LogUtils::getInstance()->debug(MSG,__FILE__ ,__func__,__LINE__ ) +#include "core/interface.hpp" -#define sConfig ::config::getInstance() -#define sFastWeb ::fastweb::getInstance() -#define sStateMgr ::state_manager::getInstance() +#define LOG_ERROR(MSG) app()->log->error(MSG,__FILE__ ,__func__,__LINE__ ) +#define LOG_WARN(MSG) app()->log->warn(MSG,__FILE__ ,__func__,__LINE__ ) +#define LOG_INFO(MSG) app()->log->info(MSG,__FILE__ ,__func__,__LINE__ ) +#define LOG_SUCC(MSG) app()->log->success(MSG,__FILE__ ,__func__,__LINE__ ) +#define LOG_DEBUG(MSG) app()->log->debug(MSG,__FILE__ ,__func__,__LINE__ ) -#define VarType sol::object - +#define GET_APP \ + sol::state_view lua(ts); \ + fastweb::app* app = lua["____app"] \ No newline at end of file diff --git a/src/core/entry.cpp b/src/core/entry.cpp index 192f64b..2247516 100644 --- a/src/core/entry.cpp +++ b/src/core/entry.cpp @@ -1,5 +1,5 @@ #include -#include "core/fastweb.h" +#include "core/app.h" #include "util/system.h" #include "core/define.h" #include "core/config.h" @@ -8,29 +8,26 @@ extern "C" { #ifdef _WIN32 DLL_EXPORT #endif - int fastweb_start(const char* config_filepath) + void* fastweb_start(const char* config_filepath) { - std::cout << "=========== [fastweb engine] ============" << std::endl; - if (sConfig->open(config_filepath) == false) + fastweb::app* app = new fastweb::app(); + if (app->start(config_filepath) == false) { - LOG_ERROR("open config failed," + sConfig->last_error()); - return -1; + app->log->error("fastweb start failed," + app->last_error(), __FILE__, __func__, __LINE__); + delete app; + return nullptr; } - - if (fastweb::getInstance()->start() == false) - { - LOG_ERROR("fastweb start failed," + fastweb::getInstance()->last_error()); - return -1; - } - LOG_SUCC("success"); - return 0; + app->log->success("success", __FILE__, __func__, __LINE__); + return app; } #ifdef _WIN32 DLL_EXPORT #endif - void fastweb_close() - { - fastweb::getInstance()->stop(); - } + void fastweb_close(void* app) + { + auto a = static_cast(app); + a->stop(); + delete a; + } } diff --git a/src/core/entry.h b/src/core/entry.h index ba48fbd..ad4f347 100644 --- a/src/core/entry.h +++ b/src/core/entry.h @@ -4,7 +4,7 @@ #define DLL_EXPORT __attribute__((visibility("default"))) #endif extern "C" { - DLL_EXPORT int fastweb_start(const char* config_filepath); - DLL_EXPORT void fastweb_close(); + DLL_EXPORT void* fastweb_start(const char* config_filepath); + DLL_EXPORT void fastweb_close(void* app); } \ No newline at end of file diff --git a/src/core/fastweb.h b/src/core/fastweb.h deleted file mode 100644 index 9a4621e..0000000 --- a/src/core/fastweb.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "define.h" -#include "base/error.h" -#include "base/singleton.hpp" -#include "net/http_center.h" -#include "core/subscribemanager.h" -#include "core/interceptormanager.h" -class fastweb:public ylib::error_base,public ylib::singleton { -public: - fastweb(); - bool start(); - void stop(); -private: - /// - /// 初始化执行脚本 - /// - /// - bool initialization_script(); -private: - // 初始化脚本虚拟机 - luastate* m_state_init = nullptr; - // 网站服务核心 - network::http::center *m_center = nullptr; - // 订阅管理器 - subscribe_manager m_subscribe; - // 拦截器管理器 - interceptor_manager m_interceptor; -}; \ No newline at end of file diff --git a/src/core/global.cpp b/src/core/global.cpp index 048ca15..450330a 100644 --- a/src/core/global.cpp +++ b/src/core/global.cpp @@ -1,28 +1,32 @@  #include "global.h" -#include "module/imodule.h" -global::global() +#include "module/basemodule.h" +fastweb::global::global(fastweb::app* app):Interface(app) { - } -void global::regist_lua(sol::state* lua) +fastweb::global::~global() { - m_value_ptr.lock(); - for_iter(iter, (*m_value_ptr.parent())) + clear(); +} + +void fastweb::global::regist(sol::state* lua) +{ + m_ptrs.lock(); + for_iter(iter, (*m_ptrs.parent())) { - auto im = static_cast(iter->second); + auto im = static_cast(iter->second); im->regist_global(iter->first, lua); } - m_value_ptr.unlock(); + m_ptrs.unlock(); } -void* global::get_ptr(const std::string& name) +void* fastweb::global::get_ptr(const std::string& name) { void* result = nullptr; - m_value_ptr.get(name,result); + m_ptrs.get(name,result); return result; } -bool global::regist_ptr(const std::string& name, void* value, sol::this_state ts) +bool fastweb::global::set_ptr(const std::string& name, void* value, sol::this_state ts) { sol::state_view lua(ts); // INIT中先注册一次,防止被销毁 @@ -30,12 +34,12 @@ bool global::regist_ptr(const std::string& name, void* value, sol::this_state ts lua.registry()[name] = this; lua[name] = this; } - return m_value_ptr.add(name, value); + return m_ptrs.add(name, value); } -VarType global::get(const std::string& name, sol::this_state s) +sol::object fastweb::global::get_obj(const std::string& name, sol::this_state s) { - VarType value; + sol::object value; if (m_values.get(name, value)) { return value; @@ -43,22 +47,18 @@ VarType global::get(const std::string& name, sol::this_state s) return sol::make_object(s, sol::nil); } -void global::set(const std::string& name, VarType value) +void fastweb::global::set_obj(const std::string& name, sol::object value) { m_values.set(name, value, true); } -void global::clear() -{ - m_values.clear(); - m_value_ptr.clear(); - - //m_value_ptr.lock(); - //for_iter(iter, (*m_value_ptr.parent())) +void fastweb::global::clear() +{ + //for_iter(iter, (*m_ptrs.parent())) //{ - // auto im = static_cast(iter->second); - // im->delete_global(); + // //auto base = ((module::base*)iter->second); + // //base->delete_global(); //} - //m_value_ptr.unlock(); - //m_value_ptr.clear(); + m_ptrs.clear(); + m_values.clear(); } diff --git a/src/core/global.h b/src/core/global.h index 6db26e8..e245451 100644 --- a/src/core/global.h +++ b/src/core/global.h @@ -1,25 +1,57 @@ #pragma once #include "define.h" #include "base/error.h" -#include "base/singleton.hpp" #include "util/map.hpp" -class global :public ylib::error_base,public ylib::singleton{ -public: - global(); +namespace fastweb +{ + /// + /// 应用全局变量 + /// + class global : public Interface{ + public: + global(fastweb::app* app); + ~global(); + /// + /// 注册变量 + /// + /// + void regist(sol::state* lua); + /// + /// 取指针 + /// + /// + /// + void* get_ptr(const std::string& name); + /// + /// 置指针 + /// + /// + /// + /// + /// + bool set_ptr(const std::string& name, void* value, sol::this_state ts); - void regist_lua(sol::state* lua); - - void* get_ptr(const std::string& name); - bool regist_ptr(const std::string& name,void* value, sol::this_state ts); + /// + /// 取对象 + /// + /// + /// + /// + sol::object get_obj(const std::string& name, sol::this_state s); + /// + /// 置对象 + /// + /// + /// + void set_obj(const std::string& name, sol::object value); + /// + /// 清理 + /// + void clear(); + private: + ylib::map m_ptrs; + ylib::map m_values; + }; +} - VarType get(const std::string& name, sol::this_state s); - void set(const std::string& name,VarType value); - - void clear(); -private: - ylib::map m_value_ptr; - - ylib::map m_values; -}; - diff --git a/src/core/global_module.cpp b/src/core/global_module.cpp new file mode 100644 index 0000000..6d3442c --- /dev/null +++ b/src/core/global_module.cpp @@ -0,0 +1,27 @@ +#include "global_module.h" +fastweb::global_module::global_module(fastweb::app* app):Interface(app) +{ + +} + +fastweb::global_module::~global_module() +{ + clear(); +} + +bool fastweb::global_module::regist(const std::string& name, module::base* module_ptr) +{ + if (m_ptrs.find(name) != m_ptrs.end()) + return false; + m_ptrs.emplace(name,module_ptr); + return true; +} + +void fastweb::global_module::clear() +{ + for_iter(iter,m_ptrs) + { + delete iter->second; + } + m_ptrs.clear(); +} diff --git a/src/core/global_module.h b/src/core/global_module.h new file mode 100644 index 0000000..c9d4c03 --- /dev/null +++ b/src/core/global_module.h @@ -0,0 +1,32 @@ +#pragma once +#include "define.h" +#include "base/error.h" +#include "util/map.hpp" +namespace module +{ + class base; +} +namespace fastweb +{ + /// + /// 全局托管模块(程序启动创建、程序销毁释放) + /// + class global_module : public Interface{ + public: + global_module(fastweb::app* app); + ~global_module(); + /// + /// 创建 + /// + /// + bool regist(const std::string& name,module::base* module_ptr); + /// + /// 清理 + /// + void clear(); + private: + std::map m_ptrs; + }; +} + + diff --git a/src/core/interceptormanager.cpp b/src/core/interceptormanager.cpp index 2659c64..71fd99c 100644 --- a/src/core/interceptormanager.cpp +++ b/src/core/interceptormanager.cpp @@ -1,29 +1,31 @@ #include "core/interceptormanager.h" #include "core/config.h" +#include "core/app.h" #include "core/statemanager.h" #include "module/http/request.h" #include "module/http/response.h" #include "net/http_interceptor.h" -std::map interceptor_manager::interceptor = std::map(); -interceptor_manager::interceptor_manager() +fastweb::interceptor_manager::interceptor_manager(fastweb::app* app):Interface(app) { } -interceptor_manager::~interceptor_manager() +fastweb::interceptor_manager::~interceptor_manager() { clear(); } -void interceptor_manager::load(network::http::router* router) +void fastweb::interceptor_manager::load(network::http::router* router) { clear(); m_router = router; - for (size_t i = 0; i < sConfig->website.interceptor_scripts.size(); i++) + for (size_t i = 0; i < app()->config->website.interceptor_scripts.size(); i++) { - interceptor_manager::interceptor.emplace(sConfig->website.interceptor_scripts[i].regex_express, sConfig->website.interceptor_scripts[i].filepath); - router->interceptor()->add(sConfig->website.interceptor_scripts[i].regex_express, &interceptor_manager::callback); + interceptor_manager::interceptor.emplace(app()->config->website.interceptor_scripts[i].regex_express, app()->config->website.interceptor_scripts[i].filepath); + router->interceptor()->add(app()->config->website.interceptor_scripts[i].regex_express, [&](network::http::reqpack* reqpack, const std::string& express_string)->bool { + return this->callback(reqpack, express_string); + }); } } -void interceptor_manager::clear() +void fastweb::interceptor_manager::clear() { if(m_router != nullptr) m_router->interceptor()->clear(); @@ -31,36 +33,38 @@ void interceptor_manager::clear() m_router = nullptr; } -bool interceptor_manager::callback(network::http::reqpack* reqpack, const std::string& express_string) +bool fastweb::interceptor_manager::callback(network::http::reqpack* reqpack, const std::string& express_string) { bool ok_continue = false; - auto lua = sStateMgr->get(); + auto lua = app()->state->get(); std::string exception_string; try { - auto lbResult = lua->state->load_file(interceptor_manager::interceptor[express_string]); - if (lbResult.valid() == false) - { - sol::error err = lbResult; - throw ylib::exception("[interceptor] Failed to load script, " + std::string(err.what())); + sol::load_result script = lua->state->load_file(interceptor_manager::interceptor[express_string]); + if (!script.valid()) { + sol::error err = script; + throw ylib::exception(err.what()); } module::request m_request(reqpack->request()); module::response m_response(reqpack->response()); - (*lua->state)["response"] = m_response; (*lua->state)["request"] = m_request; - auto result = lbResult(); + sol::protected_function_result result = script(); + if (!result.valid()) { + sol::error err = result; + throw ylib::exception(err.what()); + } ok_continue = result.get(); } catch (const std::exception& e) { exception_string = e.what(); - if (sConfig->website.debug) + if (app()->config->website.debug) LOG_ERROR("[subscribe_interceptor][" + reqpack->request()->filepath() + "]: " + e.what()); } lua->state->collect_garbage(); - sStateMgr->push(lua); + app()->state->push(lua); if (exception_string.empty() == false) throw ylib::exception(exception_string); diff --git a/src/core/interceptormanager.h b/src/core/interceptormanager.h index df735e1..d38fdcf 100644 --- a/src/core/interceptormanager.h +++ b/src/core/interceptormanager.h @@ -1,31 +1,34 @@ #pragma once -#include "base/singleton.hpp" #include "core/structs.h" +#include "core/define.h" #include "net/http_reqpack.h" #include "net/http_request.h" #include "net/http_response.h" #include "net/http_router.h" -/// -/// 拦截器管理器 -/// -class interceptor_manager{ -public: - interceptor_manager(); - ~interceptor_manager(); - - void load(network::http::router* router); - void clear(); -private: +namespace fastweb +{ /// - /// 服务回调 + /// 拦截器管理器 /// - /// - /// - static bool callback(network::http::reqpack* reqpack, const std::string& express_string); -private: - network::http::router* m_router = nullptr; -public: - static std::map interceptor; -}; \ No newline at end of file + class interceptor_manager:public Interface { + public: + interceptor_manager(fastweb::app* app); + ~interceptor_manager(); + + void load(network::http::router* router); + void clear(); + + private: + /// + /// 服务回调 + /// + /// + /// + bool callback(network::http::reqpack* reqpack, const std::string& express_string); + private: + network::http::router* m_router = nullptr; + std::map interceptor; + }; +} diff --git a/src/utils/logutils.cpp b/src/core/log.cpp similarity index 62% rename from src/utils/logutils.cpp rename to src/core/log.cpp index 0a80041..5ab92cd 100644 --- a/src/utils/logutils.cpp +++ b/src/core/log.cpp @@ -1,16 +1,16 @@ -#include "logutils.h" +#include "log.h" #include "util/print.h" #include "util/time.h" #include "util/codec.h" #include "util/file.h" #include "core/define.h" #include "core/config.h" - +#include "core/app.h" #ifdef _WIN32 #include #endif #define DEBUG_INFO 0 -void LogUtils::print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) { +void fastweb::log::print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) { std::string __now_time = time::now_time("%m-%d %H:%M:%S"); printf("[%s] ", __now_time.c_str()); @@ -26,7 +26,7 @@ void LogUtils::print(const std::string& type,const std::string& msg,const std::s ylib::println(msg, (ylib::ConsoleTextColor)color); #endif - if (sConfig->log.enable) + if (app()->config->log.enable) { std::string logcontent = __now_time + " " + type + " " + msg #ifdef _WIN32 @@ -34,17 +34,17 @@ void LogUtils::print(const std::string& type,const std::string& msg,const std::s #else + "\n"; #endif - std::string new_time = time::now_time(sConfig->log.name); + std::string new_time = time::now_time(app()->config->log.name); if (new_time == "") { - std::cout << "error: The log file name is incorrect: " << sConfig->log.name << std::endl; + std::cout << "error: The log file name is incorrect: " << app()->config->log.name << std::endl; return; } if (m_current_name != new_time) { - ylib::file::create_dir(sConfig->log.dir, true); - std::string newfilepath = sConfig->log.dir + "/" + new_time; + ylib::file::create_dir(app()->config->log.dir, true); + std::string newfilepath = app()->config->log.dir + "/" + new_time; m_file.close(); if (m_file.open(newfilepath) == false) { @@ -55,14 +55,14 @@ void LogUtils::print(const std::string& type,const std::string& msg,const std::s m_file.appead(logcontent); } } -LogUtils::LogUtils() +fastweb::log::log(fastweb::app* ptr):Interface(ptr) { } -LogUtils::~LogUtils() +fastweb::log::~log() { } -void LogUtils::success(const std::string& msg, const std::string& filepath, const std::string& func, int line) +void fastweb::log::success(const std::string& msg, const std::string& filepath, const std::string& func, int line) { //log_error(); this->print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN @@ -72,13 +72,13 @@ void LogUtils::success(const std::string& msg, const std::string& filepath, cons ,false); } -void LogUtils::info(const std::string& msg, const std::string& filepath, const std::string& func, int line) +void fastweb::log::info(const std::string& msg, const std::string& filepath, const std::string& func, int line) { this->print("[INFO ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false); } -void LogUtils::error(const std::string& msg, const std::string& filepath, const std::string& func, int line) +void fastweb::log::error(const std::string& msg, const std::string& filepath, const std::string& func, int line) { //#ifdef _DEBUG this->print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED @@ -91,7 +91,7 @@ void LogUtils::error(const std::string& msg, const std::string& filepath, const //#endif } -void LogUtils::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line) +void fastweb::log::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line) { this->print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW #ifdef _WIN32 @@ -100,7 +100,7 @@ void LogUtils::warn(const std::string& msg, const std::string& filepath, const s , false); } -void LogUtils::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line) +void fastweb::log::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line) { this->print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE #ifdef _WIN32 @@ -108,3 +108,8 @@ void LogUtils::debug(const std::string& msg, const std::string& filepath, const #endif , false); } + +void fastweb::log::lua(const std::string& msg) +{ + this->print("[LUA ] ", msg,"","",0, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false); +} diff --git a/src/core/log.h b/src/core/log.h new file mode 100644 index 0000000..8445879 --- /dev/null +++ b/src/core/log.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include "util/file.h" +#include "core/define.h" +namespace fastweb +{ + class log :public Interface { + public: + log(fastweb::app* ptr); + ~log(); + + void success(const std::string& msg, const std::string& filepath, const std::string& func, int line); + void info(const std::string& msg, const std::string& filepath, const std::string& func, int line); + void error(const std::string& msg, const std::string& filepath, const std::string& func, int line); + void warn(const std::string& msg, const std::string& filepath, const std::string& func, int line); + void debug(const std::string& msg, const std::string& filepath, const std::string& func, int line); + void lua(const std::string& msg); + private: + void print(const std::string& type, const std::string& msg, const std::string& filepath, const std::string& func, int line, int color, bool error); + private: + // 当前文件名 + std::string m_current_name; + // 文件 + ylib::file_io m_file; + }; +} diff --git a/src/core/lualibdetecter.cpp b/src/core/lualibdetecter.cpp index 3353474..65a8f10 100644 --- a/src/core/lualibdetecter.cpp +++ b/src/core/lualibdetecter.cpp @@ -1,17 +1,18 @@ #include "lualibdetecter.h" #include "core/config.h" +#include "core/app.h" #include "util/file.h" -lualib_detecter::lualib_detecter() +fastweb::lualib_detecter::lualib_detecter(fastweb::app* app):Interface(app) { } -lualib_detecter::~lualib_detecter() +fastweb::lualib_detecter::~lualib_detecter() { } -bool lualib_detecter::changed() +bool fastweb::lualib_detecter::changed() { - auto lib_files = sConfig->lua_lib_files(); + auto lib_files = app()->config->lua_lib_files(); bool changed = false; if (lib_files.size() == m_files.size()) { diff --git a/src/core/lualibdetecter.h b/src/core/lualibdetecter.h index c9d70b5..0f2eab1 100644 --- a/src/core/lualibdetecter.h +++ b/src/core/lualibdetecter.h @@ -4,25 +4,27 @@ #include "sol/sol.hpp" #include "base/error.h" -#include "base/singleton.hpp" #include "util/thread.h" #include "util/queue.hpp" #include "util/map.hpp" - #include "core/structs.h" +#include "core/define.h" -/// -/// LUALIB库变动检测 -/// -class lualib_detecter { -public: - lualib_detecter(); - ~lualib_detecter(); +namespace fastweb +{ /// - /// 是否变化 + /// LUALIB库变动检测 /// - /// - bool changed(); -private: - std::map m_files; -}; \ No newline at end of file + class lualib_detecter:public Interface { + public: + lualib_detecter(fastweb::app* app); + ~lualib_detecter(); + /// + /// 是否变化 + /// + /// + bool changed(); + private: + std::map m_files; + }; +} diff --git a/src/core/modulemanager.cpp b/src/core/modulemanager.cpp index 1c52b19..8b04647 100644 --- a/src/core/modulemanager.cpp +++ b/src/core/modulemanager.cpp @@ -1,7 +1,7 @@ #include "modulemanager.h" #include "util/file.h" - +#include "core/app.h" #include "core/config.h" #include "core/global.h" #ifdef _WIN32 @@ -23,22 +23,25 @@ #include "module/mutex.h" #include "module/codec.h" #include "module/time.h" -#include "module/file.h" +#include "module/filesystem.h" #include "module/sys.h" #include "module/timer.h" #include "module/process.h" #include "module/ini.h" -module_manager::module_manager() +fastweb::module_manager::module_manager(fastweb::app* app):Interface(app) { } -void module_manager::start() +fastweb::module_manager::~module_manager() +{ +} +void fastweb::module_manager::start() { close(); auto ms = modules(); for (size_t i = 0; i < ms.size(); i++) { module_info mi; - std::string mod_filepath = sConfig->scripts.module_dir + "/" + ms[i]; + std::string mod_filepath = app()->config->scripts.module_dir + "/" + ms[i]; #ifdef _WIN32 mi.dll = LoadLibrary(mod_filepath.c_str()); if (mi.dll == nullptr) @@ -65,7 +68,7 @@ void module_manager::start() } } -void module_manager::close() +void fastweb::module_manager::close() { for_iter(iter, m_modules) { @@ -77,25 +80,25 @@ void module_manager::close() m_modules.clear(); } -void module_manager::load(sol::state* lua) +void fastweb::module_manager::load(sol::state* lua) { load_core(lua); load_lualib(lua); load_3rdparty(lua); } -std::string module_manager::search(const std::string& filepath) +std::string fastweb::module_manager::search(const std::string& filepath) { - for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++) + for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++) { - std::string path = sConfig->scripts.lib_dir[i] + "/" + filepath; + std::string path = app()->config->scripts.lib_dir[i] + "/" + filepath; if (ylib::file::exist(path)) return path; } return ""; } -void module_manager::load_core(sol::state* lua) +void fastweb::module_manager::load_core(sol::state* lua) { lua->open_libraries( sol::lib::base, @@ -122,23 +125,23 @@ void module_manager::load_core(sol::state* lua) #ifdef _WIN32 module::mssql::regist(lua); #endif - module::regist_globalfuns(lua); + module::globalfuncs::regist(lua); module::local_storage::regist(lua); module::mutex::regist(lua); module::auto_lock::regist(lua); module::codec::regist(lua); module::time::regist(lua); - module::file::regist(lua); + module::filesystem::regist(lua); module::sys::regist(lua); module::timer::regist(lua); module::ini::regist(lua); module::process::regist(lua); - global::getInstance()->regist_lua(lua); + app()->global->regist(lua); } -void module_manager::load_3rdparty(sol::state* lua) +void fastweb::module_manager::load_3rdparty(sol::state* lua) { for_iter(iter, m_modules) { @@ -149,20 +152,20 @@ void module_manager::load_3rdparty(sol::state* lua) } } -void module_manager::load_lualib(sol::state* lua) +void fastweb::module_manager::load_lualib(sol::state* lua) { // 获取当前的package.path,添加新的搜索路径 std::string current_path = (*lua)["package"]["path"]; // 获取当前的路径 - for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++) - current_path += ";" + sConfig->scripts.lib_dir[i] + "/?.lua"; // 添加新的路径 - current_path += ";" + sConfig->website.dir + "/?.lua"; // 添加新的路径 + for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++) + current_path += ";" + app()->config->scripts.lib_dir[i] + "/?.lua"; // 添加新的路径 + current_path += ";" + app()->config->website.dir + "/?.lua"; // 添加新的路径 (*lua)["package"]["path"] = current_path; // 设置修改后的路径 } -std::vector module_manager::modules() +std::vector fastweb::module_manager::modules() { std::vector results; - auto luas = ylib::file::traverse(sConfig->scripts.module_dir, "(.*\\.dll)"); + auto luas = ylib::file::traverse(app()->config->scripts.module_dir, "(.*\\.dll)"); for_iter(iter, luas) { if (iter->second == IS_DIRECTORY) diff --git a/src/core/modulemanager.h b/src/core/modulemanager.h index 9caab5b..d29451b 100644 --- a/src/core/modulemanager.h +++ b/src/core/modulemanager.h @@ -2,52 +2,57 @@ #include #include "sol/sol.hpp" #include "core/structs.h" -#include "base/singleton.hpp" +#include "core/define.h" typedef int (*fastweb_module_regist)(void*, void*); struct module_info { void* dll = nullptr; fastweb_module_regist func = nullptr; }; +namespace fastweb +{ + /// + /// 模块管理器 + /// + class module_manager :public Interface { + public: + module_manager(fastweb::app* app); + ~module_manager(); -/// -/// 模块管理器 -/// -class module_manager :public ylib::error_base, public ylib::singleton { -public: - module_manager(); - void start(); - void close(); - /// - /// 加载虚拟机库 - /// - /// - void load(sol::state* lua); - /// - /// 搜索LUA - /// - /// - /// - std::string search(const std::string& filepath); -private: - /// - /// 加载核心库 - /// - /// - void load_core(sol::state* lua); - /// - /// 加载三方库 - /// - void load_3rdparty(sol::state* lua); - /// - /// 加载LUA库 - /// - /// - void load_lualib(sol::state* lua); - /// - /// 取模块文件列表 - /// - /// - std::vector modules(); -private: - std::map m_modules; -}; \ No newline at end of file + void start(); + void close(); + /// + /// 加载虚拟机库 + /// + /// + void load(sol::state* lua); + /// + /// 搜索LUA + /// + /// + /// + std::string search(const std::string& filepath); + private: + /// + /// 加载核心库 + /// + /// + void load_core(sol::state* lua); + /// + /// 加载三方库 + /// + void load_3rdparty(sol::state* lua); + /// + /// 加载LUA库 + /// + /// + void load_lualib(sol::state* lua); + /// + /// 取模块文件列表 + /// + /// + std::vector modules(); + private: + // DLL模块 + std::map m_modules; + }; +} diff --git a/src/core/statemanager.cpp b/src/core/statemanager.cpp index 017ffda..0fdc40c 100644 --- a/src/core/statemanager.cpp +++ b/src/core/statemanager.cpp @@ -7,36 +7,47 @@ #include "core/define.h" #include "core/config.h" #include "core/global.h" +#include "core/app.h" #define LOOP_STATE_USE 1 -bool state_manager::start() +fastweb::state_manager::state_manager(fastweb::app* app):Interface(app) +{ + lib_detecter = std::make_shared(app); + module_manager = std::make_shared(app); +} +fastweb::state_manager::~state_manager() +{ +} +bool fastweb::state_manager::start() { close(); ::ithread::start(); - m_module_manager.start(); + module_manager->start(); return true; } -void state_manager::close() +void fastweb::state_manager::close() { ::ithread::stop(); ::ithread::wait(); luastate* state = nullptr; while (m_states.pop(state)) delete state; - m_module_manager.close(); + module_manager->close(); } -luastate* state_manager::create() +luastate* fastweb::state_manager::create() { luastate* lua = new luastate(); lua->flag = m_flag; + (*lua->state)["____app"] = app(); + // 加载库或模块 - m_module_manager.load(lua->state); + module_manager->load(lua->state); return lua; } -luastate* state_manager::get() +luastate* fastweb::state_manager::get() { luastate* result = nullptr; while (m_states.pop(result)) @@ -49,7 +60,7 @@ luastate* state_manager::get() return create(); } -void state_manager::push(luastate* state) +void fastweb::state_manager::push(luastate* state) { if (state == nullptr) return; @@ -62,11 +73,11 @@ void state_manager::push(luastate* state) } -bool state_manager::run() +bool fastweb::state_manager::run() { - if (m_lib_detecter.changed()) + if (lib_detecter->changed()) m_flag++; - system::sleep_msec(sConfig->scripts.auto_update_sec*1000); + system::sleep_msec(app()->config->scripts.auto_update_sec * 1000); return true; } diff --git a/src/core/statemanager.h b/src/core/statemanager.h index 0630b85..c27c29e 100644 --- a/src/core/statemanager.h +++ b/src/core/statemanager.h @@ -4,7 +4,6 @@ #include "sol/sol.hpp" #include "base/error.h" -#include "base/singleton.hpp" #include "util/thread.h" #include "util/queue.hpp" #include "util/map.hpp" @@ -12,47 +11,52 @@ #include "core/structs.h" #include "core/lualibdetecter.h" #include "core/modulemanager.h" -/// -/// LUA状态管理器 -/// -class state_manager:public ylib::singleton,private ylib::ithread,public ylib::error_base { -public: - state_manager() = default; - +namespace fastweb +{ /// - /// 启动 + /// LUA状态管理器 /// - bool start(); - void close(); - /// - /// 取虚拟机 - /// - /// - luastate* get(); - /// - /// 归还虚拟机 - /// - /// - void push(luastate* state); -private: - // 虚拟机 - ylib::queue m_states; - // 版本FLAT - size_t m_flag = 0; - // LIB变化检测 - lualib_detecter m_lib_detecter; - // 模块管理器 - module_manager m_module_manager; -private: - // 通过 ithread 继承 - bool run() override; - /// - /// 创建虚拟机 - /// - /// - luastate* create(); + class state_manager :private ylib::ithread,public Interface { + public: + state_manager(fastweb::app* app); + ~state_manager(); + /// + /// 启动 + /// + bool start(); + void close(); + /// + /// 取虚拟机 + /// + /// + luastate* get(); + /// + /// 归还虚拟机 + /// + /// + void push(luastate* state); + public: + // 模块管理器 + std::shared_ptr module_manager; + private: + // 虚拟机 + ylib::queue m_states; + // 版本FLAT + size_t m_flag = 0; + // LIB变化检测 + std::shared_ptr lib_detecter; + + private: + // 通过 ithread 继承 + bool run() override; + /// + /// 创建虚拟机 + /// + /// + luastate* create(); -}; \ No newline at end of file + }; +} diff --git a/src/core/subscribemanager.cpp b/src/core/subscribemanager.cpp index 6e60349..662c203 100644 --- a/src/core/subscribemanager.cpp +++ b/src/core/subscribemanager.cpp @@ -1,23 +1,26 @@ #include "subscribemanager.h" #include "core/config.h" +#include "core/app.h" #include "core/statemanager.h" #include "module/http/request.h" #include "module/http/response.h" -subscribe_manager::subscribe_manager() +fastweb::subscribe_manager::subscribe_manager(fastweb::app* ptr):Interface(ptr) { } -subscribe_manager::~subscribe_manager() +fastweb::subscribe_manager::~subscribe_manager() { clear(); } -void subscribe_manager::load(network::http::router* router) +void fastweb::subscribe_manager::load(network::http::router* router) { clear(); m_router = router; - router->other(&subscribe_manager::other); + router->other([&](network::http::request* request, network::http::response* response) { + this->other(request,response); + }); } -void subscribe_manager::clear() +void fastweb::subscribe_manager::clear() { if (m_router != nullptr) { @@ -28,11 +31,11 @@ void subscribe_manager::clear() m_subextra.clear(); m_router = nullptr; } -void subscribe_manager::other(network::http::request* request, network::http::response* response) +void fastweb::subscribe_manager::other(network::http::request* request, network::http::response* response) { - auto send_404 = [](network::http::response* response) { - std::string default_404 = sConfig->website.dir + "\\" + sConfig->website.default_404; - if (sConfig->website.default_404 == "" || ylib::file::exist(default_404) == false) + auto send_404 = [&](network::http::response* response) { + std::string default_404 = app()->config->website.dir + "\\" + app()->config->website.default_404; + if (app()->config->website.default_404 == "" || ylib::file::exist(default_404) == false) { response->send((std::string)"404 Not Found", 404, "Not Found"); } @@ -61,22 +64,22 @@ void subscribe_manager::other(network::http::request* request, network::http::re std::string filepath; if (strutils::right(request->filepath(),1) == "/") { - for (size_t i = 0; i < sConfig->website.default_index.size(); i++) + for (size_t i = 0; i < app()->config->website.default_index.size(); i++) { - filepath = sConfig->website.dir + request->filepath() + sConfig->website.default_index[i]; + filepath = app()->config->website.dir + request->filepath() + app()->config->website.default_index[i]; if (ylib::file::exist(filepath)) break; } } else - filepath = sConfig->website.dir + request->filepath(); + filepath = app()->config->website.dir + request->filepath(); send_file(response, filepath); } -void subscribe_manager::exec(const std::string& filepath, network::http::request* request, network::http::response* response) +void fastweb::subscribe_manager::exec(const std::string& filepath, network::http::request* request, network::http::response* response) { - auto lua = sStateMgr->get(); + auto lua = app()->state->get(); std::string exception_string; try { @@ -96,12 +99,12 @@ void subscribe_manager::exec(const std::string& filepath, network::http::request catch (const std::exception& e) { exception_string = e.what(); - if (sConfig->website.debug) + if (app()->config->website.debug) LOG_ERROR("[subscribe_service][" + request->filepath() + "]: " + e.what()); } // 清理 lua->state->collect_garbage(); - sStateMgr->push(lua); + app()->state->push(lua); if (exception_string.empty() == false) throw ylib::exception(exception_string); diff --git a/src/core/subscribemanager.h b/src/core/subscribemanager.h index 9fbcdca..cc15596 100644 --- a/src/core/subscribemanager.h +++ b/src/core/subscribemanager.h @@ -2,37 +2,40 @@ #include "sol/sol.hpp" -#include "base/singleton.hpp" #include "core/structs.h" +#include "core/define.h" #include "net/http_request.h" #include "net/http_response.h" #include "net/http_router.h" -/// -/// 订阅管理器 -/// -class subscribe_manager{ -public: - subscribe_manager(); - ~subscribe_manager(); +namespace fastweb +{ + /// + /// 订阅管理器 + /// + class subscribe_manager:public Interface { + public: + subscribe_manager(fastweb::app* ptr); + ~subscribe_manager(); - void load(network::http::router* router); - void clear(); -private: - /// - /// 其它 - /// - /// - /// - /// - static void other(network::http::request* request, network::http::response* response); - /// - /// 执行lua - /// - /// - /// - /// - static void exec(const std::string& filepath,network::http::request* request, network::http::response* response); -private: - network::http::router* m_router = nullptr; - std::vector m_subextra; -}; \ No newline at end of file + void load(network::http::router* router); + void clear(); + private: + /// + /// 其它 + /// + /// + /// + /// + void other(network::http::request* request, network::http::response* response); + /// + /// 执行lua + /// + /// + /// + /// + void exec(const std::string& filepath, network::http::request* request, network::http::response* response); + private: + network::http::router* m_router = nullptr; + std::vector m_subextra; + }; +} diff --git a/src/module/imodule.h b/src/module/basemodule.h similarity index 76% rename from src/module/imodule.h rename to src/module/basemodule.h index f8f2912..96040cf 100644 --- a/src/module/imodule.h +++ b/src/module/basemodule.h @@ -7,15 +7,18 @@ namespace module /// /// 模块继承接口 /// - class imodule { + class base { public: - virtual ~imodule() {}; + virtual ~base() {}; /// - /// 注册全局变量 + /// 注册全局变量(global类管理) /// /// /// virtual void regist_global(const std::string& name,sol::state* lua) = 0; + /// + /// 释放全局变量(global类管理) + /// virtual void delete_global() = 0; /// /// 全局变量阶段获取自身指针 @@ -25,3 +28,4 @@ namespace module }; } + diff --git a/src/module/codec.h b/src/module/codec.h index 5c8e5e8..079f74b 100644 --- a/src/module/codec.h +++ b/src/module/codec.h @@ -1,13 +1,13 @@ #pragma once #include "sol/sol.hpp" -#include "imodule.h" +#include "basemodule.h" namespace module { /// /// 编解码 /// - class codec:public module::imodule { + class codec{ public: /// /// URL解码 diff --git a/src/module/file.cpp b/src/module/file.cpp deleted file mode 100644 index 47b9348..0000000 --- a/src/module/file.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include "file.h" -#include "util/file.h" -sol::table module::file::list(const std::string& dirpath, const std::string& regex, sol::this_state s) -{ - auto map = ylib::file::traverse(dirpath, regex); - - sol::state_view lua(s); - sol::table result_table = lua.create_table(); - int row_num = 1; - - for_iter(iter, map) - { - sol::table row = lua.create_table(); - row["file"] = iter->first; - row["type"] = (int)iter->second; - result_table[row_num++] = row; - } - 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); -} -sol::object module::file::read(const std::string& filepath, sol::this_state s) -{ - sol::object result; - ylib::buffer data; - if (ylib::file::read(filepath, data) == false) - result = sol::make_object(s,data.to_string()); - else - result = sol::make_object(s, sol::nil); - return result; -} -bool module::file::write(const std::string& filepath, const std::string& data) -{ - return ylib::file::write(filepath, data); -} -bool module::file::remove(const std::string& filepath) -{ - return ylib::file::remove(filepath); -} -bool module::file::remove_dir(const std::string& dirpath, bool recycle) -{ - return ylib::file::remove_dir(dirpath,recycle); -} -bool module::file::exist(const std::string& filepath) -{ - return ylib::file::exist(filepath); -} -bool module::file::exist_dir(const std::string& dirpath) -{ - return ylib::file::exist_dir(dirpath); -} -int64 module::file::size(const std::string& filepath) -{ - return ylib::file::size(filepath); -} -bool module::file::copy(const std::string& src, const std::string& dst) -{ - return ylib::file::copy(src,dst); -} -timestamp module::file::last_write_time(const std::string& filepath) -{ - return ylib::file::last_write_time(filepath); -} -void module::file::regist(sol::state* lua) -{ - (*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE; - (*lua)["IS_DIRECTORY"] = (int)ylib::FileType::IS_DIRECTORY; - - - lua->new_usertype("fs", - "list", &module::file::list, - "copy_dir", &module::file::copy_dir, - "create_dir", &module::file::create_dir, - "read",module::file::read, - "write",module::file::write, - "remove",module::file::remove, - "remove_dir",module::file::remove_dir, - "exist",module::file::exist, - "exist_dir",module::file::exist_dir, - "size",module::file::size, - "copy",module::file::copy, - "last_write_time",module::file::last_write_time - ); -} diff --git a/src/module/filesystem.cpp b/src/module/filesystem.cpp new file mode 100644 index 0000000..50897b6 --- /dev/null +++ b/src/module/filesystem.cpp @@ -0,0 +1,90 @@ +#include "filesystem.h" +#include "util/file.h" +sol::table module::filesystem::list(const std::string& dirpath, const std::string& regex, sol::this_state s) +{ + auto map = ylib::file::traverse(dirpath, regex); + + sol::state_view lua(s); + sol::table result_table = lua.create_table(); + int row_num = 1; + + for_iter(iter, map) + { + sol::table row = lua.create_table(); + row["file"] = iter->first; + row["type"] = (int)iter->second; + result_table[row_num++] = row; + } + return result_table; +} +void module::filesystem::copy_dir(const std::string& src_dir, const std::string& dst_dir) +{ + ylib::file::copy_dir(src_dir, dst_dir); +} +bool module::filesystem::create_dir(const std::string& dirpath) +{ + return ylib::file::create_dir(dirpath,true); +} +sol::object module::filesystem::read(const std::string& filepath, sol::this_state s) +{ + sol::object result; + ylib::buffer data; + if (ylib::file::read(filepath, data) == false) + result = sol::make_object(s,data.to_string()); + else + result = sol::make_object(s, sol::nil); + return result; +} +bool module::filesystem::write(const std::string& filepath, const std::string& data) +{ + return ylib::file::write(filepath, data); +} +bool module::filesystem::remove(const std::string& filepath) +{ + return ylib::file::remove(filepath); +} +bool module::filesystem::remove_dir(const std::string& dirpath, bool recycle) +{ + return ylib::file::remove_dir(dirpath,recycle); +} +bool module::filesystem::exist(const std::string& filepath) +{ + return ylib::file::exist(filepath); +} +bool module::filesystem::exist_dir(const std::string& dirpath) +{ + return ylib::file::exist_dir(dirpath); +} +int64 module::filesystem::size(const std::string& filepath) +{ + return ylib::file::size(filepath); +} +bool module::filesystem::copy(const std::string& src, const std::string& dst) +{ + return ylib::file::copy(src,dst); +} +timestamp module::filesystem::last_write_time(const std::string& filepath) +{ + return ylib::file::last_write_time(filepath); +} +void module::filesystem::regist(sol::state* lua) +{ + (*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE; + (*lua)["IS_DIRECTORY"] = (int)ylib::FileType::IS_DIRECTORY; + + + lua->new_usertype("fs", + "list", &module::filesystem::list, + "copy_dir", &module::filesystem::copy_dir, + "create_dir", &module::filesystem::create_dir, + "read",module::filesystem::read, + "write",module::filesystem::write, + "remove",module::filesystem::remove, + "remove_dir",module::filesystem::remove_dir, + "exist",module::filesystem::exist, + "exist_dir",module::filesystem::exist_dir, + "size",module::filesystem::size, + "copy",module::filesystem::copy, + "last_write_time",module::filesystem::last_write_time + ); +} diff --git a/src/module/file.h b/src/module/filesystem.h similarity index 94% rename from src/module/file.h rename to src/module/filesystem.h index 3445655..62d9349 100644 --- a/src/module/file.h +++ b/src/module/filesystem.h @@ -1,13 +1,12 @@ #pragma once #include "sol/sol.hpp" -#include "imodule.h" - +#include "core/define.h" namespace module { /// /// 文件 /// - class file:public module::imodule { + class filesystem { 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); diff --git a/src/module/globalfuns.cpp b/src/module/globalfuns.cpp index da9dac4..b5088ff 100644 --- a/src/module/globalfuns.cpp +++ b/src/module/globalfuns.cpp @@ -3,41 +3,65 @@ #include "util/codec.h" #include "util/time.h" #include "core/global.h" +#include "core/app.h" static ylib::counter s_counter_guid; -void module::regist_globalfuns(sol::state* lua) +void module::globalfuncs::regist(sol::state* lua) { - lua->set_function("global_get", module::global_get); - lua->set_function("global_set", module::global_set); - lua->set_function("make_software_guid", module::make_software_guid); - lua->set_function("throw_string", module::throw_string); + lua->set_function("set_ptr", module::globalfuncs::set_ptr); + lua->set_function("get_obj", module::globalfuncs::get_obj); + lua->set_function("set_obj", module::globalfuncs::set_obj); + lua->set_function("make_software_guid", module::globalfuncs::make_software_guid); + lua->set_function("throw_string", module::globalfuncs::throw_string); + lua->set_function("print", module::globalfuncs::print); + } -std::string module::make_software_guid() +std::string module::globalfuncs::make_software_guid() { return codec::md5(std::to_string(time::now_msec()) + std::to_string(s_counter_guid.make())); } - -bool module::global_regist(const std::string& name, void* ptr, sol::this_state ts) +void module::globalfuncs::print(sol::variadic_args args, sol::this_state ts) { - return global::getInstance()->regist_ptr(name,ptr, ts); + GET_APP; + + std::ostringstream oss; + for (auto arg : args) { + if (arg.get_type() == sol::type::string) { + oss << arg.as(); + } + else if (arg.get_type() == sol::type::number) { + oss << arg.as(); // 可以处理整数和浮点数 + } + else if (arg.get_type() == sol::type::boolean) { + oss << (arg.as() ? "true" : "false"); + } + else if (arg.get_type() == sol::type::nil) { + oss << "nil"; + } + else { + oss << "unsupported type"; + } + oss << " "; // 添加一个空格分隔符 + } + app->log->lua(oss.str()); +} +bool module::globalfuncs::set_ptr(const std::string& name, void* ptr, sol::this_state ts) +{ + GET_APP; + return app->global->set_ptr(name,ptr, ts); } -void module::global_set(const std::string& name, VarType value) +void module::globalfuncs::set_obj(const std::string& name, sol::object value, sol::this_state ts) { - global::getInstance()->set(name,value); + GET_APP; + return app->global->set_obj(name,value); } -VarType module::global_get(const std::string& name, sol::this_state s) +sol::object module::globalfuncs::get_obj(const std::string& name, sol::this_state ts) { - return global::getInstance()->get(name,s); + GET_APP; + return app->global->get_obj(name, ts); } - -#if 0 -void* module::global_get(const std::string& name) -{ - return global::getInstance()->get_ptr(name); -} -#endif -void module::throw_string(const std::string& msg) +void module::globalfuncs::throw_string(const std::string& msg) { throw ylib::exception(msg); } diff --git a/src/module/globalfuns.h b/src/module/globalfuns.h index ade6275..b639077 100644 --- a/src/module/globalfuns.h +++ b/src/module/globalfuns.h @@ -5,24 +5,49 @@ /// namespace module { - void regist_globalfuns(sol::state* lua); - /// - /// 生成软件唯一GUID - /// - /// - std::string make_software_guid(); - /// - /// 注册全局指针(仅允许初始化lua使用) - /// - /// - bool global_regist(const std::string& name, void* ptr, sol::this_state ts); + class globalfuncs { + public: + /// + /// 生成软件唯一GUID + /// + /// + static std::string make_software_guid(); + /// + /// 接管打印 + /// + /// + static void print(sol::variadic_args args, sol::this_state ts); + /// + /// 置全局指针 + /// + /// + /// + /// + /// + static bool set_ptr(const std::string& name, void* ptr, sol::this_state ts); - void global_set(const std::string& name, VarType value); - VarType global_get(const std::string& name, sol::this_state s); - /// - /// 抛出异常 - /// - /// - void throw_string(const std::string& msg); + /// + /// 置全局对象 + /// + /// + /// + static void set_obj(const std::string& name, sol::object value, sol::this_state ts); + /// + /// 取全局对象 + /// + /// + /// + /// + static sol::object get_obj(const std::string& name, sol::this_state s); + /// + /// 抛出异常 + /// + /// + static void throw_string(const std::string& msg); + + + static void regist(sol::state* lua); + }; + } diff --git a/src/module/http/request.cpp b/src/module/http/request.cpp index abbd3fd..f58a6c0 100644 --- a/src/module/http/request.cpp +++ b/src/module/http/request.cpp @@ -88,7 +88,7 @@ std::string module::request::host() { return m_request->host(); } -VarType module::request::param(const std::string& name, bool throw_,sol::this_state s) +sol::object module::request::param(const std::string& name, bool throw_,sol::this_state s) { std::string value; bool result = request_param(name, value); diff --git a/src/module/http/request.h b/src/module/http/request.h index 1e2c860..a01099f 100644 --- a/src/module/http/request.h +++ b/src/module/http/request.h @@ -17,7 +17,7 @@ namespace module network::http::method method(); std::string filepath(); std::string host(); - VarType param(const std::string& name,bool throw_,sol::this_state s); + sol::object param(const std::string& name,bool throw_,sol::this_state s); std::string remote_ipaddress(); ushort remote_port(); diff --git a/src/module/localstorage.h b/src/module/localstorage.h index 1610d5f..1b810ed 100644 --- a/src/module/localstorage.h +++ b/src/module/localstorage.h @@ -1,12 +1,12 @@ #pragma once #include "util/localstorage.h" -#include "imodule.h" +#include "basemodule.h" namespace module { /// /// 本地缓存(leveldb) /// - class local_storage : public ylib::local_storage,public module::imodule { + class local_storage : public ylib::local_storage,public module::base { public: local_storage(); ~local_storage() override; diff --git a/src/module/mssql.cpp b/src/module/mssql.cpp index 53475ee..6c80709 100644 --- a/src/module/mssql.cpp +++ b/src/module/mssql.cpp @@ -1,6 +1,7 @@ #ifdef _WIN32 #include "mssql.h" #include "soci/odbc/soci-odbc.h" +#include "core/define.h" module::mssql::mssql(const std::string& connstring) { m_session = std::make_shared(soci::odbc, connstring); diff --git a/src/module/mssql.h b/src/module/mssql.h index 2701ecd..eab7493 100644 --- a/src/module/mssql.h +++ b/src/module/mssql.h @@ -1,8 +1,8 @@ #pragma once #ifdef _WIN32 #include "sol/sol.hpp" -#include "imodule.h" #include "soci/soci.h" +#include "core/define.h" namespace module { /// diff --git a/src/module/mutex.h b/src/module/mutex.h index e40e013..e6f82eb 100644 --- a/src/module/mutex.h +++ b/src/module/mutex.h @@ -1,13 +1,13 @@ #pragma once #include "sol/sol.hpp" -#include "imodule.h" +#include "module/basemodule.h" namespace module { /// /// 互斥锁 /// - class mutex:public module::imodule { + class mutex:public module::base { public: mutex(); ~mutex() override; diff --git a/src/module/mysql.cpp b/src/module/mysql.cpp index 5223201..b5ce452 100644 --- a/src/module/mysql.cpp +++ b/src/module/mysql.cpp @@ -534,7 +534,7 @@ bool module::mysql_result::next() return m_result->next(); } -VarType module::mysql_result::get(sol::object obj, sol::this_state s) +sol::object module::mysql_result::get(sol::object obj, sol::this_state s) { std::string type; if (obj.get_type() == sol::type::string) diff --git a/src/module/mysql.h b/src/module/mysql.h index ac0a1e6..dfa4223 100644 --- a/src/module/mysql.h +++ b/src/module/mysql.h @@ -2,7 +2,7 @@ #include "db/mysql.h" #include "db/sqler.h" #include "sol/sol.hpp" -#include "imodule.h" +#include "module/basemodule.h" namespace module { /// @@ -50,7 +50,7 @@ namespace module /// /// /// - VarType get(sol::object obj, sol::this_state s); + sol::object get(sol::object obj, sol::this_state s); /// /// 取结果集到table /// @@ -151,7 +151,7 @@ namespace module /// /// MYSQL连接池 /// - class mysql :public imodule { + class mysql :public module::base { public: mysql(); ~mysql() override; diff --git a/src/module/sys.h b/src/module/sys.h index cc188b4..6f2a3cd 100644 --- a/src/module/sys.h +++ b/src/module/sys.h @@ -1,13 +1,12 @@ #pragma once #include "sol/sol.hpp" -#include "imodule.h" - +#include "core/define.h" namespace module { /// /// 系统 /// - class sys:public module::imodule { + class sys { public: static std::string current_dir(); static void sleep_msec(uint32 msec); diff --git a/src/module/time.h b/src/module/time.h index 847103b..42231b1 100644 --- a/src/module/time.h +++ b/src/module/time.h @@ -1,13 +1,12 @@ #pragma once #include "sol/sol.hpp" -#include "imodule.h" - +#include "core/define.h" namespace module { /// /// 时间 /// - class time:public module::imodule { + class time { public: static uint64 now_msec(); static uint64 now_sec(); diff --git a/src/module/timer.cpp b/src/module/timer.cpp index 2c96060..f7e127e 100644 --- a/src/module/timer.cpp +++ b/src/module/timer.cpp @@ -3,35 +3,41 @@ #include "util/system.h" #include "util/file.h" #include "core/config.h" +#include "core/app.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 module::timer::add(const std::string& name, const std::string& filepath, const std::string& funname, int msec, bool loop, sol::this_state ts) { + GET_APP; std::string filepath2; - if (ylib::file::exist(sConfig->website.dir + "/" + filepath)) - filepath2 = sConfig->website.dir + "/" + filepath; + if (ylib::file::exist(app->config->website.dir + "/" + filepath)) + filepath2 = app->config->website.dir + "/" + filepath; else { - filepath2 = module_manager::getInstance()->search(filepath); + + filepath2 = app->state->module_manager->search(filepath); + if (filepath2.empty()) { std::string result; - result = "not found script lua, Root: " + std::string(sConfig->website.dir + "/" + filepath) + "\r\n Lib: "; - for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++) - result.append(std::string(sConfig->scripts.lib_dir[i] + "/" + filepath)+"\r\n"); + result = "not found script lua, Root: " + std::string(app->config->website.dir + "/" + filepath) + "\r\n Lib: "; + for (size_t i = 0; i < app->config->scripts.lib_dir.size(); i++) + result.append(std::string(app->config->scripts.lib_dir[i] + "/" + filepath)+"\r\n"); return result; } } - std::unique_lock uni(module::timer::getInstance()->m_mutex); + std::unique_lock uni(m_mutex); + + m_app = app; - auto iter = module::timer::getInstance()->m_list.find(name); - if (iter != module::timer::getInstance()->m_list.end()) + auto iter = m_list.find(name); + if (iter != m_list.end()) return "exist name `" + name + "`"; timer_info ti; @@ -42,24 +48,29 @@ std::string module::timer::add(const std::string& name, const std::string& filep ti.msec = msec; ti.exec_msec = time::now_msec() + msec; - module::timer::getInstance()->m_list.emplace(name, ti); - module::timer::getInstance()->m_insert = true; + m_list.emplace(name, ti); + m_insert = true; return ""; } -void module::timer::remove(const std::string& name) +void module::timer::remove(const std::string& name, sol::this_state ts) { - module::timer::getInstance()->m_removed.push(name); - //std::unique_lock uni(module::timer::getInstance()->m_mutex); - //module::timer::getInstance()->m_list.erase(name); + m_removed.push(name); } void module::timer::regist(sol::state* lua) { lua->new_usertype("timer", + "new", sol::constructors(), "add", &module::timer::add, - "remove", &module::timer::remove + "remove", &module::timer::remove, + "self", &module::timer::self ); } +void module::timer::regist_global(const std::string& name, sol::state* lua) +{ + lua->registry()[name] = this; + (*lua)[name] = this; +} module::timer::timer() { ::ithread::start(); @@ -67,7 +78,11 @@ module::timer::timer() bool module::timer::run() { - + if (m_app == nullptr) + { + system::sleep_msec(100); + return true; + } auto comp_wait_msec = [&]()->timestamp { timestamp wait_msec = 0; auto now_msec = time::now_msec(); @@ -90,7 +105,7 @@ bool module::timer::run() // 加载LUA文件 if (ti.lua == nullptr) { - ti.lua = sStateMgr->get(); + ti.lua = m_app->state->get(); auto lbResult = ti.lua->state->load_file(ti.filepath); if (lbResult.valid() == false) { @@ -117,8 +132,8 @@ bool module::timer::run() catch (const std::exception& e) { exception_string = e.what(); - if (sConfig->website.debug) - LOG_ERROR("[timer][" + ti.filepath + "]: " + e.what()); + if (m_app->config->website.debug) + m_app->log->error("[timer][" + ti.filepath + "]: " + e.what(), __FILE__, __func__, __LINE__); return false; } return false; @@ -127,11 +142,11 @@ bool module::timer::run() auto wait_msec = comp_wait_msec(); for (size_t i = 0; i < wait_msec/10; i++) { - if (module::timer::getInstance()->m_insert) + if (m_insert || m_state == 1) break; system::sleep_msec(10); } - module::timer::getInstance()->m_insert = false; + m_insert = false; std::unique_lock uni(m_mutex); auto now_msec = time::now_msec(); @@ -166,7 +181,7 @@ bool module::timer::run() if (iter != m_list.end()) { iter->second.lua->state->collect_garbage(); - sStateMgr->push(iter->second.lua); + m_app->state->push(iter->second.lua); m_list.erase(iter); } } diff --git a/src/module/timer.h b/src/module/timer.h index ee82a4a..45eb6fb 100644 --- a/src/module/timer.h +++ b/src/module/timer.h @@ -1,10 +1,9 @@ #pragma once #include "sol/sol.hpp" -#include "imodule.h" #include "core/structs.h" -#include "base/singleton.hpp" #include "util/thread.h" #include "util/queue.hpp" +#include "module/basemodule.h" #include #include namespace module @@ -22,7 +21,7 @@ namespace module /// /// 定时器 /// - class timer:public ylib::singleton,private ylib::ithread { + class timer:private ylib::ithread,public module::base { public: timer(); ~timer(); @@ -35,19 +34,20 @@ namespace module /// /// /// - static std::string add(const std::string& name,const std::string& filepath,const std::string& funname,int msec,bool loop); + std::string add(const std::string& name,const std::string& filepath,const std::string& funname,int msec,bool loop, sol::this_state ts); /// /// 移除 /// /// - static void remove(const std::string& name); - - + void remove(const std::string& name, sol::this_state ts); static void regist(sol::state* lua); + virtual void regist_global(const std::string& name, sol::state* lua) override; + virtual void delete_global() { delete this; } private: - + virtual bool run(); public: + fastweb::app* m_app = nullptr; std::map m_list; ylib::queue m_removed; std::mutex m_mutex; diff --git a/src/utils/logutils.h b/src/utils/logutils.h deleted file mode 100644 index 6be1eab..0000000 --- a/src/utils/logutils.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include -#include "base/singleton.hpp" -#include "util/file.h" -class LogUtils :public ylib::singleton { -public: - LogUtils(); - ~LogUtils(); - - void success(const std::string& msg, const std::string& filepath, const std::string& func, int line); - void info(const std::string& msg, const std::string& filepath, const std::string& func, int line); - void error(const std::string& msg, const std::string& filepath, const std::string& func, int line); - void warn(const std::string& msg, const std::string& filepath, const std::string& func, int line); - void debug(const std::string& msg, const std::string& filepath, const std::string& func, int line); -private: - void print(const std::string& type, const std::string& msg, const std::string& filepath, const std::string& func, int line, int color, bool error); -private: - // 当前文件名 - std::string m_current_name; - // 文件 - ylib::file_io m_file; -}; \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp index 64ccab9..5eb1013 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -3,13 +3,16 @@ #include "core/entry.h" #include std::string config_filepath; -bool start() +void* fastweb_app_ptr = nullptr; +void start() { - return fastweb_start(config_filepath.c_str()) == 0; + fastweb_app_ptr = fastweb_start(config_filepath.c_str()); } void close() { - fastweb_close(); + if(fastweb_app_ptr == nullptr) + fastweb_close(fastweb_app_ptr); + fastweb_app_ptr = nullptr; } int main() { @@ -23,7 +26,8 @@ int main() std::cout << "closing..." << std::endl; close(); std::cout << "starting..." << std::endl; - if (start() == false) + start(); + if (fastweb_app_ptr == nullptr) { std::cin.get(); return -1;