From f5eb5559d3925fb2f17c07bcee13476f8131f530 Mon Sep 17 00:00:00 2001 From: nianhua <1585346868@qq.com> Date: Wed, 19 Jun 2024 14:49:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=85=BC=E5=AE=B9linux?= =?UTF-8?q?=E5=8C=85=E7=AE=A1=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 64 ++++ CMakeLists.txt | 13 +- {module => include}/dll_interface.h | 0 src/core/app.cpp | 2 +- src/core/config.cpp | 54 +--- src/core/log.cpp | 2 +- src/utils/parseconfig.hpp | 71 +++++ tests/fastweb.cpp | 455 ++++++++++++++++++++++++++++ tests/fastweb.h | 75 +++++ tests/main.cpp | 137 +-------- tests/slave.cpp | 44 --- tests/slave.h | 31 -- 12 files changed, 688 insertions(+), 260 deletions(-) create mode 100644 .vscode/settings.json rename {module => include}/dll_interface.h (100%) create mode 100644 src/utils/parseconfig.hpp create mode 100644 tests/fastweb.cpp create mode 100644 tests/fastweb.h delete mode 100644 tests/slave.cpp delete mode 100644 tests/slave.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9a97ae8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,64 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "chrono": "cpp", + "codecvt": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp", + "variant": "cpp", + "filesystem": "cpp" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2619f81..8540dc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ if(MSVC) set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Build config types" FORCE) endif() # C++等级 -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) set(FASTWEBCORE ${PROJECT_NAME}core) # 安装复制 @@ -63,6 +63,7 @@ else() endif() + # 根据构建类型添加定义 add_library(${FASTWEBCORE} SHARED ${SOURCE_FILES} ${HEADER_FILES}) set_target_properties(${FASTWEBCORE} PROPERTIES OUTPUT_NAME_DEBUG "${FASTWEBCORE}_d" OUTPUT_NAME_RELEASE ${FASTWEBCORE}) @@ -114,10 +115,11 @@ else() endif() +add_definitions(-DFASTWEB_EXE) # 编译测试调用示例 add_executable(${PROJECT_NAME} tests/main.cpp - tests/slave.cpp + tests/fastweb.cpp ) if(MSVC) target_link_libraries(${PROJECT_NAME} PRIVATE @@ -163,9 +165,9 @@ if(MSVC) install(DIRECTORY ${PROJECT_SOURCE_DIR}/www DESTINATION bin/release) install(DIRECTORY ${PROJECT_SOURCE_DIR}/config DESTINATION bin/release) install(FILES ${PROJECT_SOURCE_DIR}/src/core/entry.h DESTINATION include) - install(FILES ${PROJECT_SOURCE_DIR}/module/dll_interface.h DESTINATION include) + install(FILES ${PROJECT_SOURCE_DIR}/include/dll_interface.h DESTINATION include) install(FILES ${PROJECT_SOURCE_DIR}/src/module/basemodule.h DESTINATION include) - install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty/sol DESTINATION include) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty/lua/include/ DESTINATION include/lua) else() @@ -173,7 +175,7 @@ else() install(TARGETS ${PROJECT_NAME} DESTINATION bin) install(FILES ${PROJECT_SOURCE_DIR}/src/core/entry.h DESTINATION include/${PROJECT_NAME}) - install(FILES ${PROJECT_SOURCE_DIR}/module/dll_interface.h DESTINATION include/${PROJECT_NAME}) + install(FILES ${PROJECT_SOURCE_DIR}/include/dll_interface.h DESTINATION include/${PROJECT_NAME}) install(FILES ${PROJECT_SOURCE_DIR}/src/module/basemodule.h DESTINATION include/${PROJECT_NAME}) @@ -186,4 +188,5 @@ endif() +install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty/sol DESTINATION include) diff --git a/module/dll_interface.h b/include/dll_interface.h similarity index 100% rename from module/dll_interface.h rename to include/dll_interface.h diff --git a/src/core/app.cpp b/src/core/app.cpp index b29aa7d..47be4bf 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -82,7 +82,7 @@ bool fastweb::app::start(const std::string& config_filepath) ws_config.name = "master"; - ws_config.router.threadpool.size = 40; + ws_config.router.threadpool.size = 10; ws_config.router.threadpool.queuemax = 10000; ws_config.session.dirpath = this->config->website.session_dir; ws_config.session.timeout_sec = this->config->website.session_timeout_sec; diff --git a/src/core/config.cpp b/src/core/config.cpp index f7daa8e..3d932d7 100644 --- a/src/core/config.cpp +++ b/src/core/config.cpp @@ -17,51 +17,20 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web #include "config.h" #include #include "core/app.h" +#include "utils/parseconfig.hpp" fastweb::config::config(fastweb::app* ptr):Interface(ptr) { } bool fastweb::config::open(const std::string& ini_filepath) { m_ini_filepath = ini_filepath; - if (ylib::file::exist(ini_filepath) == false) - { - LOG_ERROR("not found file: " + ini_filepath); - return false; - } - std::string temp_filepath = ylib::file::temp_filepath() + ".bak"; - if (ylib::file::copy(ini_filepath, temp_filepath) == false) - { - LOG_ERROR("Failed to copy temporary INI configuration file from '" + ini_filepath + "' to '" + temp_filepath + "'."); - return false; - } - std::string src_content = ylib::file::read(temp_filepath); - // EXE运行目录 - src_content = strutils::replace(src_content, "${current_dir}", strutils::replace(system::current_dir(),'\\','/')); - // 配置文件目录 - { - std::string ini_dir = ylib::file::parent_dir(ini_filepath); - src_content = strutils::replace(strutils::replace(src_content, "${config_dir}", ini_dir), '\\', '/'); - } - - - ylib::file::write(temp_filepath,src_content); - if (m_ini.open(temp_filepath)) + auto [result,err] = parseconfig(m_ini,ini_filepath); + if(result == false) { - auto vars = extractVariableNames(ylib::file::read(temp_filepath)); - for (size_t i = 0; i < vars.size(); i++) - { - if (m_ini.read("variable", vars[i]) == "") - { - LOG_ERROR("Variable declaration not found or empty content: ${" + vars[i] + "}"); - return false; - } - src_content = strutils::replace(src_content, "${" + vars[i] + "}", m_ini.read("variable", vars[i])); - } + LOG_ERROR(err); + return false; } - ylib::file::write(temp_filepath,src_content); - m_ini.close(); - m_ini.open(temp_filepath); cache(); return true; } @@ -92,19 +61,6 @@ ylib::json fastweb::config::read_runtime() { return ylib::json::from(ylib::file::read(ylib::file::parent_dir(m_ini_filepath) + "/.fastweb_runtime")); } -std::vector fastweb::config::extractVariableNames(const std::string& text) -{ - std::regex pattern("\\$\\{([^}]+)\\}"); // 使用捕获组提取中间的内容 - std::vector results; - // 使用 std::sregex_iterator 迭代所有匹配项 - auto begin = std::sregex_iterator(text.begin(), text.end(), pattern); - auto end = std::sregex_iterator(); - for (std::sregex_iterator i = begin; i != end; ++i) { - std::smatch match = *i; - results.push_back(match[1]); // 仅添加捕获组的内容 - } - return results; -} void fastweb::config::cache() { diff --git a/src/core/log.cpp b/src/core/log.cpp index a0b9b62..5a582ac 100644 --- a/src/core/log.cpp +++ b/src/core/log.cpp @@ -80,7 +80,7 @@ void fastweb::log::print(log_type type,const std::string& msg,const std::string& #ifdef _WIN32 ylib::println(codec::to_gbk(msg), (ylib::ConsoleTextColor)color); #else - ylib::println(msg, (ylib::ConsoleTextColor)color); + ylib::print(msg, (ylib::ConsoleTextColor)color); #endif log_info li; diff --git a/src/utils/parseconfig.hpp b/src/utils/parseconfig.hpp new file mode 100644 index 0000000..550148f --- /dev/null +++ b/src/utils/parseconfig.hpp @@ -0,0 +1,71 @@ +#pragma once +#include +#include +#include +#include +#include "util/ini.h" +#include "util/file.h" +#include "util/system.h" +#include "util/strutils.h" +std::vector __extractVariableNames(const std::string& text) +{ + std::regex pattern("\\$\\{([^}]+)\\}"); // 使用捕获组提取中间的内容 + std::vector results; + // 使用 std::sregex_iterator 迭代所有匹配项 + auto begin = std::sregex_iterator(text.begin(), text.end(), pattern); + auto end = std::sregex_iterator(); + for (std::sregex_iterator i = begin; i != end; ++i) { + std::smatch match = *i; + results.push_back(match[1]); // 仅添加捕获组的内容 + } + return results; +} +std::tuple parseconfig(ylib::ini &ini,const std::string& ini_filepath) +{ + if (ylib::file::exist(ini_filepath) == false) + { + #ifdef FASTWEB_EXE + return std::make_tuple(false,"未找到配置文件: " + ini_filepath); + #else + return std::make_tuple(false,"not found ini: " + ini_filepath); + #endif + } + std::string temp_filepath = ylib::file::temp_filepath() + ".bak"; + if (ylib::file::copy(ini_filepath, temp_filepath) == false) + { + #ifdef FASTWEB_EXE + return std::make_tuple(false,"Failed to copy temporary INI configuration file from '" + ini_filepath + "' to '" + temp_filepath + "'."); + #else + return std::make_tuple(false,"配置文件复制到临时目录失败 from '" + ini_filepath + "' to '" + temp_filepath + "'."); + #endif + } + std::string src_content = ylib::file::read(temp_filepath); + // EXE运行目录 + src_content = strutils::replace(src_content, "${current_dir}", strutils::replace(system::current_dir(),'\\','/')); + // 配置文件目录 + { + std::string ini_dir = ylib::file::parent_dir(ini_filepath); + src_content = strutils::replace(strutils::replace(src_content, "${config_dir}", ini_dir), '\\', '/'); + } + + + + ylib::file::write(temp_filepath,src_content); + if (ini.open(temp_filepath)) + { + auto vars = __extractVariableNames(ylib::file::read(temp_filepath)); + for (size_t i = 0; i < vars.size(); i++) + { + if (ini.read("variable", vars[i]) == "") + { + return std::make_tuple(false,"Variable declaration not found or empty content: ${" + vars[i] + "}"); + } + src_content = strutils::replace(src_content, "${" + vars[i] + "}", ini.read("variable", vars[i])); + } + } + ylib::file::write(temp_filepath,src_content); + ini.close(); + ini.open(temp_filepath); + + return std::make_tuple(true,""); +} \ No newline at end of file diff --git a/tests/fastweb.cpp b/tests/fastweb.cpp new file mode 100644 index 0000000..4aafbc3 --- /dev/null +++ b/tests/fastweb.cpp @@ -0,0 +1,455 @@ +/*Software License + +Copyright(C) 2024[liuyingjie] +License Terms +Usage Rights + +Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code. +If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license. +Future Versions Updates +From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version. +Users must comply with the terms of the new license issued in future releases. +Liability and Disclaimer +This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. +Contact Information +If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com. +*/ +#include "fastweb.h" +#include +#include "util/strutils.h" +#include "util/system.h" +#include "util/codec.h" +#include "util/file.h" +#include "util/print.h" +#include "../src/utils/parseconfig.hpp" +#include "net/http_client_plus.h" +#include "core/entry.h" + + + + +bool exsit_install_software(std::string name){ + std::string command = "which " + name + " > /dev/null 2>&1"; + int result = std::system(command.c_str()); + return (result == 0); +}; +std::pair execute_command(const std::string& command){ + std::array buffer; + std::string result; + int return_code = 0; + std::unique_ptr pipe(popen((command + " 2>&1").c_str(), "r"), pclose); + if (!pipe) { + throw std::runtime_error("popen() failed!"); + } + + // Read the output a line at a time - output it. + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + + // Get the return code + return_code = pclose(pipe.release()); + + return std::make_pair(return_code, command+"\n"+result); +}; + + + + + +fastweb::fastweb(const std::vector ¶m) +{ + + auto equals=[&](std::vector p2,int all_size)->bool{ + if(param.size() < all_size) + return false; + if(param.size() list; + if(module_infos(list) == false) + return; + + fastweb::module_info info; + for(size_t i=0;i list; + if(module_infos(list) == false) + return; + + fastweb::module_info info; + for(size_t i=0;i list; + if(module_infos(list) == false) + return; + + std::cout<<"--------------------------模块列表---------------------------"<& list) +{ + std::cout<<"正在获取最新模块列表信息..."< types = {"fastweb","lua","other"}; + for(size_t i=0;i()); + info.name = td[k]["name"].to(); + info.name_en = td[k]["name_en"].to(); + info.desc = td[k]["desc"].to(); + info.doc = td[k]["doc"].to(); + info.type = types[i]; + info.download_win64 = td[k]["doc"].to(); + info.download_linux_url = td[k]["download"]["linux"]["url"].to(); + info.download_linux_type = td[k]["download"]["linux"]["type"].to(); + + list.push_back(info); + } + } + return true; +} + +void fastweb::wait() +{ + while(true) + system::sleep_msec(1000*60); +} + +void fastweb::install_module_linux(fastweb::module_info info) +{ + + + if(exsit_install_software("lua") == false) + { + std::cout<<"LUA未安装,请重新执行fastweb一键构建脚本并确认lua编译安装部分正确执行"< +#include +#include "base/define.h" +#include "util/ini.h" +#define FASTWEB_VERSION "V1.0.5" +#define FASTWEB_MODULE_JSON_URL "https://download.fwlua.com/module/module.json" +class fastweb +{ +public: + fastweb(const std::vector& param); + ~fastweb(); +private: + struct module_info{ + std::string id; + std::string name; + std::string name_en; + std::string doc; + std::string desc; + std::string type; + std::string download_win64; + std::string download_linux_type; + std::string download_linux_url; + }; +private: + /// @brief 启动 + /// @param ini_filepath + /// @param wait + void start(std::string ini_filepath,bool wait); + /// @brief 停止 + /// @param app + void stop(void* app); + /// @brief 输出HELP信息 + void output_help(); + + /// @brief 创建配置文件 + /// @param dirpath + void create_config(std::string dirpath); + /// @brief 创建网站木板 + /// @param dirpath + void create_website(std::string dirpath); + /// @brief 安装模块 + /// @param ini_filepath + /// @param name + void install_module(std::string ini_filepath,std::string name); + /// @brief 卸载模块 + /// @param ini_filepath + /// @param name + void uninstall_module(std::string ini_filepath,std::string name); + + + + + /// @brief 模块列表 + void module_list(); + + /// @brief 模块信息 + /// @return + bool module_infos(std::vector& list); + +private: + /// @brief 无限等待 + void wait(); + + void install_module_linux(fastweb::module_info info); + void install_module_windows(fastweb::module_info info); + + + + /// @brief 解析INI配置文件 + /// @param ini_filepath + bool parse_config(const std::string& ini_filepath); +private: + ylib::ini m_ini; +}; \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp index 5292857..1aecd14 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -14,136 +14,15 @@ This software is provided “as is”, without any express or implied warranties Contact Information If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com. */ -#pragma once #include -#include "core/entry.h" -#include -#include "util/file.h" -#include "util/strutils.h" -#include "util/system.h" -#include "slave.h" - -#define FASTWEB_VERSION "V1.0.5" - -#define QUIT_WAIT std::cout << "Please exit after entering any character...";std::cin.get();return -1 - -void* start(const char* config_filepath) +#include "fastweb.h" +int main(int argv,char* argc[]) { - return fastweb_start(config_filepath); -} -void close(void* app) -{ - if(app == nullptr) - fastweb_close(app); -} -void ouput_help() -{ - std::cerr << "No configuration file path provided. Please provide the path to a config.ini file as an argument." << std::endl; + std::vector param; + for(size_t i=1;i> input; - if (input == "quit" || input == "exit") - { - close(app); - std::cout << "closed"; - return 0; - } - else - { - std::cout << "Enter \"quit\" or \"exit\" to exit the application" << std::endl; - std::cout << "Enter \"restart\" to restart the application" << std::endl; - } - } - } - else - { - std::cerr << "file not found: " << std::filesystem::absolute(value).string() << std::endl; - QUIT_WAIT; - } - } - else if (type == "start2") - { - if (ylib::file::exist(std::filesystem::absolute(value).string())) - { - app = start(std::filesystem::absolute(value).string().c_str()); - if (app == nullptr) - return -1; - while (true) - system::sleep_msec(1000 * 60); - } - else - return -1; - } - else if (type == "fastwebmanager") - { - int flag = ylib::stoi(argv[2]); - slave s("fastweb_shardmemory", flag); - - app = start(argv[3]); - if (app == nullptr) - { - s.write(1); - return -1; - } - s.write(2); - // 等待关闭信号 - s.wait(); - - return 0; - } - if (argc < 4) { ouput_help(); QUIT_WAIT; } - - if (type == "create") - { - std::string path = std::filesystem::absolute(argv[3]).string(); - if (value == "config") - { - ylib::file::copy("/usr/local/share/fastweb/config.ini",path); - } - else if (value == "website") - { - ylib::file::copy_dir("/usr/local/share/fastweb", path); - } - } - return -1; -} + return 0; +} \ No newline at end of file diff --git a/tests/slave.cpp b/tests/slave.cpp deleted file mode 100644 index d33513e..0000000 --- a/tests/slave.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/*Software License - -Copyright(C) 2024[liuyingjie] -License Terms -Usage Rights - -Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code. -If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license. -Future Versions Updates -From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version. -Users must comply with the terms of the new license issued in future releases. -Liability and Disclaimer -This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. -Contact Information -If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com. -*/ - -#include "slave.h" -#include "util/system.h" - - -slave::slave(const std::string& name,int flag):m_flag(flag) -{ - m_sm = std::make_unique(false); - if (m_sm->create(name, 128 * 2) == false) - { - std::cerr << m_sm->last_error() << std::endl; - abort(); - } -} -slave::~slave() -{ - m_sm->destory(); -} -void slave::wait() -{ - while ((*m_sm)[m_flag] == 0) - system::sleep_msec(10); -} - -void slave::write(uchar data) -{ - ((char*)m_sm->mem())[m_flag+128] = data; -} diff --git a/tests/slave.h b/tests/slave.h deleted file mode 100644 index db98ff1..0000000 --- a/tests/slave.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include -#include -#include "util/thread.h" -#include "util/sharedmem.h" -#include "net/udp_node.h" -#include "base/error.h" -/// -/// 从设备 -/// -class slave:public ylib::error_base -{ -public: - slave(const std::string& name,int flag); - ~slave(); - /// - /// 等待 - /// - void wait(); - /// - /// 写入 - /// - /// - void write(uchar data); -private: - std::string m_name; - // 共享内存 - std::unique_ptr m_sm; - // 标记 - int m_flag = -1; -}; \ No newline at end of file