diff --git a/uft_dev_server/CMakeLists.txt b/uft_dev_server/CMakeLists.txt index 5e43d7f..74427cd 100644 --- a/uft_dev_server/CMakeLists.txt +++ b/uft_dev_server/CMakeLists.txt @@ -1,53 +1,63 @@ cmake_minimum_required(VERSION 3.12) project(UftDevServer) -# 设置 vcpkg 工具链 -set(CMAKE_TOOLCHAIN_FILE "D:/aigc/vcpkg/scripts/buildsystems/vcpkg.cmake") +# 强制统一运行时库 +if(MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") +endif() + +# 设置vcpkg工具链路径 +set(CMAKE_TOOLCHAIN_FILE "D:/aigc/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file") -# 设置 C++ 标准 +# 设置C++标准 set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # 设置源文件目录 set(SOURCE_DIR src) -# 设置RCC++编译选项 -set(RCCPP_SOURCE_DIR ${SOURCE_DIR}) -set(RCCPP_INCLUDE_DIRS ${SOURCE_DIR} ${SOURCE_DIR}/controllers ${SOURCE_DIR}/filters) + +set(RCCPP_SOURCE_DIR third_party/RuntimeCompiledCPlusPlus/Aurora) + +# D:\aigc\Libs\RCCPP\lib +set(RCCPP_LIB_DIR "D:/aigc/Libs/RCCPP/lib") -# 查找其他依赖(Drogon, RapidJSON) +# 查找依赖包 find_package(Drogon CONFIG REQUIRED) find_package(RapidJSON CONFIG REQUIRED) -# 查找其他依赖(Drogon, RapidJSON) -set(RCCPP_DIR "D:/aigc/manta/uft_dev_server/third_party/RuntimeCompiledCPlusPlus/Aurora") -find_package(RuntimeCompiledCPlusPlus REQUIRED) + +set(SOURCES + src/main.cpp + src/controllers/*.cpp + src/filters/*.cpp +) + +# 使用GLOB展开通配符 +file(GLOB_RECURSE SOURCES_EXPANDED ${SOURCES}) # 添加可执行文件 add_executable(${PROJECT_NAME} - ${SOURCE_DIR}/main.cpp - ${SOURCE_DIR}/controllers/HelloWorldController.cpp - ${SOURCE_DIR}/controllers/ApiController.cpp - ${SOURCE_DIR}/filters/ApiFilter.cpp + ${SOURCES_EXPANDED} ) -# 链接库(手动指定 RCCPP 的库名) +# 链接库 target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon - ${CMAKE_DL_LIBS} - RuntimeCompiledCPlusPlus::RuntimeCompiledCPlusPlus + ${CMAKE_DL_LIBS} # 用于动态加载 + "${RCCPP_LIB_DIR}/RuntimeCompiler.lib" + "${RCCPP_LIB_DIR}/RuntimeObjectSystem.lib" ) -# 其他配置(包含目录、文件拷贝等保持不变) +# 包含目录 target_include_directories(${PROJECT_NAME} PRIVATE - ${RCCPP_INCLUDE_DIRS} + ${RCCPP_SOURCE_DIR} ${SOURCE_DIR} ${SOURCE_DIR}/controllers ${SOURCE_DIR}/filters ) - # 配置文件拷贝 set(CONFIG_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/config/config.json") set(CONFIG_DEST "${CMAKE_CURRENT_BINARY_DIR}/Debug/config.json") diff --git a/uft_dev_server/cmd.txt b/uft_dev_server/cmd.txt index 3a76b33..598335e 100644 --- a/uft_dev_server/cmd.txt +++ b/uft_dev_server/cmd.txt @@ -1,10 +1,12 @@ #增加RCC++依赖包(专为自行编译RCCPP提供,直接使用已有RCCPP目录则无需执行--注意命令执行位置) +mkdir third_party git clone https://github.com/RuntimeCompiledCPlusPlus/RuntimeCompiledCPlusPlus.git cd RuntimeCompiledCPlusPlus/Aurora +rm -Path build -Recurse -Force mkdir build cd build cmake .. -DCMAKE_INSTALL_PREFIX="D:/aigc/Libs/RCCPP" -DINSTALL_CMAKE_CONFIG=ON -DBUILD_SHARED_LIBS=ON -G "Visual Studio 17 2022" -A x64 -DCMAKE_POLICY_VERSION_MINIMUM="4.0.2" -cmake --build . --config Release --target install +cmake --build . --config Debug --target install #工程编译 diff --git a/uft_dev_server/src/controllers/ApiController.cpp b/uft_dev_server/src/controllers/ApiController.cpp index 78572ad..6f85c97 100644 --- a/uft_dev_server/src/controllers/ApiController.cpp +++ b/uft_dev_server/src/controllers/ApiController.cpp @@ -1,4 +1,5 @@ -#include +#pragma once +#include #include #include diff --git a/uft_dev_server/src/controllers/HelloHandler_v1.cpp b/uft_dev_server/src/controllers/HelloHandler_v1.cpp new file mode 100644 index 0000000..4c34f91 --- /dev/null +++ b/uft_dev_server/src/controllers/HelloHandler_v1.cpp @@ -0,0 +1,9 @@ +// HelloHandler_v1.cpp +#include "HelloHandler_v1.h" + +std::string HelloHandler_v1::GetResponse() +{ + return "Hello, 这是热更新后的版本!"; +} + +REGISTERCLASS(HelloHandler_v1) \ No newline at end of file diff --git a/uft_dev_server/src/controllers/HelloHandler_v1.h b/uft_dev_server/src/controllers/HelloHandler_v1.h new file mode 100644 index 0000000..27e83e7 --- /dev/null +++ b/uft_dev_server/src/controllers/HelloHandler_v1.h @@ -0,0 +1,21 @@ +// HelloHandler_v1.cpp +#include "HelloWorldController.h" +#include "RuntimeObjectSystem/ObjectInterfacePerModule.h" +#include "StdioLogSystem.h" + +// 接口定义 +enum CustomInterfaceIDs +{ + IID_HELLO_HANDLER = IID_ENDInterfaceID + 1 +}; + +struct IHelloHandler : public IObject +{ + virtual std::string GetResponse() = 0; +}; + +class HelloHandler_v1 : public TInterface +{ +public: + std::string GetResponse() override; +}; diff --git a/uft_dev_server/src/controllers/HelloWorldController.cpp b/uft_dev_server/src/controllers/HelloWorldController.cpp index df25a2b..2f5af19 100644 --- a/uft_dev_server/src/controllers/HelloWorldController.cpp +++ b/uft_dev_server/src/controllers/HelloWorldController.cpp @@ -1,54 +1,58 @@ -#include -#include -#include "HelloWorldController.h" -#include "RuntimeObjectSystem/IObject.h" +#include "HelloWorldController.h" #include "RuntimeObjectSystem/ObjectInterfacePerModule.h" +#include "StdioLogSystem.h" +#include "HelloHandler_v1.h" -// 定义可热更新的接口 -class IHelloHandler : public IObject +// 构造函数实现 +HelloWorldController::HelloWorldController() + : m_pRuntimeSystem(nullptr), + m_pCompilerLogger(nullptr) { -public: - virtual std::string GetResponse() = 0; -}; +} -// 默认实现(可被热更新) -class HelloHandler_v1 : public TInterface +// 析构函数实现 +HelloWorldController::~HelloWorldController() { -public: - std::string GetResponse() override - { - return "Hello from Version 1!"; - } -}; -REGISTERCLASS(HelloHandler_v1); + delete m_pRuntimeSystem; + delete m_pCompilerLogger; +} void HelloWorldController::asyncHandleHttpRequest( - const HttpRequestPtr &req, - std::function &&callback) + const drogon::HttpRequestPtr &req, + std::function &&callback) { - // 初始化 RCCPP 系统(仅首次) + // 初始化检查 if (!m_pRuntimeSystem) { m_pRuntimeSystem = new RuntimeObjectSystem; - m_pRuntimeSystem->Initialise(new StdioLogSystem(), nullptr); + m_pCompilerLogger = new StdioLogSystem(); - // 创建初始对象 - auto pCtor = m_pRuntimeSystem->GetObjectFactorySystem()->GetConstructor("HelloHandler_v1"); - if (pCtor) + if (!m_pRuntimeSystem->Initialise(m_pCompilerLogger, nullptr)) { - m_ObjectId = pCtor->Construct()->GetObjectId(); + auto resp = drogon::HttpResponse::newHttpResponse(); + resp->setBody("RCCPP Initialization failed"); + callback(resp); + return; } } - // 获取当前实现(可能是热更新后的版本) + // 获取构造器 + IObjectConstructor *pCtor = m_pRuntimeSystem->GetObjectFactorySystem()->GetConstructor("HelloHandler_v1"); + if (pCtor) + { + IObject *pObj = pCtor->Construct(); + m_ObjectId = pObj->GetObjectId(); + } + + // 获取处理器 IHelloHandler *pHandler = nullptr; - if (auto pObj = m_pRuntimeSystem->GetObjectFactorySystem()->GetObject(m_ObjectId)) + if (IObject *pObj = m_pRuntimeSystem->GetObjectFactorySystem()->GetObject(m_ObjectId)) { pObj->GetInterface(&pHandler); } // 生成响应 - auto resp = HttpResponse::newHttpResponse(); - resp->setBody(pHandler ? pHandler->GetResponse() : "Error: Handler not loaded"); + auto resp = drogon::HttpResponse::newHttpResponse(); + resp->setBody(pHandler ? pHandler->GetResponse() : "Handler not available"); callback(resp); } \ No newline at end of file diff --git a/uft_dev_server/src/controllers/HelloWorldController.h b/uft_dev_server/src/controllers/HelloWorldController.h index 6b59f13..576df7e 100644 --- a/uft_dev_server/src/controllers/HelloWorldController.h +++ b/uft_dev_server/src/controllers/HelloWorldController.h @@ -1,19 +1,31 @@ #pragma once #include -#include "RuntimeObjectSystem/RuntimeObjectSystem.h" // 全局作用域 +#include "RuntimeObjectSystem/RuntimeObjectSystem.h" +#include "RuntimeObjectSystem/IObject.h" +#include "RuntimeObjectSystem/IObjectFactorySystem.h" // 新增 -class HelloWorldController : public HttpSimpleController +// 前向声明 +struct IRuntimeObjectSystem; +struct IObject; +struct IObjectFactorySystem; // 新增 +struct ICompilerLogger; // 新增 + +class HelloWorldController : public drogon::HttpSimpleController { public: + HelloWorldController(); // 显式声明构造函数 + ~HelloWorldController(); // 显式声明析构函数 + + virtual void asyncHandleHttpRequest( + const drogon::HttpRequestPtr &req, + std::function &&callback) override; + PATH_LIST_BEGIN - PATH_ADD("/hello", Get); + PATH_ADD("/hello", drogon::Get); PATH_LIST_END - void asyncHandleHttpRequest(const HttpRequestPtr &req, - std::function &&callback) override; - private: - // RCCPP 相关成员(非继承) - IRuntimeObjectSystem *m_pRuntimeSystem; + IRuntimeObjectSystem *m_pRuntimeSystem = nullptr; + ICompilerLogger *m_pCompilerLogger = nullptr; ObjectId m_ObjectId; }; \ No newline at end of file diff --git a/uft_dev_server/src/controllers/StdioLogSystem.cpp b/uft_dev_server/src/controllers/StdioLogSystem.cpp new file mode 100644 index 0000000..4e49883 --- /dev/null +++ b/uft_dev_server/src/controllers/StdioLogSystem.cpp @@ -0,0 +1,65 @@ +// +// Copyright (c) 2010-2011 Matthew Jack and Doug Binks +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// 3. This notice may not be removed or altered from any source distribution. + +#include "StdioLogSystem.h" + +// Currently we create the file on first real output, and only close it on shutdown + +#include +#include +#include + +#ifdef _WIN32 +#include "RuntimeCompiler/FileSystemUtils.h" +#include "Windows.h" +#pragma warning(disable : 4996 4800) +#endif + +void StdioLogSystem::LogError(const char *format, ...) +{ + va_list args; + va_start(args, format); + LogInternal(format, args); +} + +void StdioLogSystem::LogWarning(const char *format, ...) +{ + va_list args; + va_start(args, format); + LogInternal(format, args); +} + +void StdioLogSystem::LogInfo(const char *format, ...) +{ + va_list args; + va_start(args, format); + LogInternal(format, args); +} +void StdioLogSystem::LogInternal(const char *format, va_list args) +{ + int result = vsnprintf(m_buff, LOGSYSTEM_MAX_BUFFER - 1, format, args); + // Make sure there's a limit to the amount of rubbish we can output + m_buff[LOGSYSTEM_MAX_BUFFER - 1] = '\0'; + + std::cout << m_buff; +#ifdef _WIN32 + std::string temp = m_buff; + // convert from utf-8 to Wide char + std::wstring tempW = FileSystemUtils::_Win32Utf8ToUtf16(temp); + OutputDebugStringW(tempW.c_str()); +#endif +} diff --git a/uft_dev_server/src/controllers/StdioLogSystem.h b/uft_dev_server/src/controllers/StdioLogSystem.h new file mode 100644 index 0000000..d50731d --- /dev/null +++ b/uft_dev_server/src/controllers/StdioLogSystem.h @@ -0,0 +1,44 @@ +// +// Copyright (c) 2010-2011 Matthew Jack and Doug Binks +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// 3. This notice may not be removed or altered from any source distribution. + +#pragma once + +#ifndef STDIOLOGSYSTEM_INCLUDED +#define STDIOLOGSYSTEM_INCLUDED + +#include "RuntimeCompiler/ICompilerLogger.h" + +#include +#include + +// StdioLogSystem for compiler + +const size_t LOGSYSTEM_MAX_BUFFER = 4096; + +class StdioLogSystem : public ICompilerLogger +{ +public: + virtual void LogError(const char *format, ...); + virtual void LogWarning(const char *format, ...); + virtual void LogInfo(const char *format, ...); + +protected: + void LogInternal(const char *format, va_list args); + char m_buff[LOGSYSTEM_MAX_BUFFER]; +}; + +#endif // STDIOLOGSYSTEM_INCLUDED \ No newline at end of file diff --git a/uft_dev_server/src/main.cpp b/uft_dev_server/src/main.cpp index acd098e..457e0ce 100644 --- a/uft_dev_server/src/main.cpp +++ b/uft_dev_server/src/main.cpp @@ -1,45 +1,20 @@ -#include -#include -#include -#include +#include #include using namespace drogon; -class MyAppSystem : public RCCPP::ISystem -{ -public: - virtual bool Initialise() { return true; } - virtual void Shutdown() {} - virtual void Update(float deltaTime) {} -}; - int main() { try { - // 初始化RCC++ - RCCPP::RuntimeObjectSystem runtimeObjectSystem; - runtimeObjectSystem.Initialise(new MyAppSystem, "."); - // 设置日志级别 drogon::app().setLogLevel(trantor::Logger::kTrace); - drogon::app().registerBeginningAdvice([]() { - LOG_INFO << "Drogon application starting..."; - }); + drogon::app().registerBeginningAdvice([]() + { LOG_INFO << "Drogon application starting..."; }); // 加载配置 drogon::app().loadConfigFile("./config.json"); - // 启动文件监视器 - auto& fileChangeNotifier = runtimeObjectSystem.GetFileChangeNotifier(); - fileChangeNotifier.AddWatch(".", true); - - // 主循环中检查文件变化 - drogon::app().registerBeginningAdvice([&]() { - runtimeObjectSystem.GetRuntimeCompiler()->Update(0.1f); - }); - // 启动服务 drogon::app().run(); }