10 changed files with 231 additions and 88 deletions
@ -0,0 +1,9 @@ |
|||||||
|
// HelloHandler_v1.cpp
|
||||||
|
#include "HelloHandler_v1.h" |
||||||
|
|
||||||
|
std::string HelloHandler_v1::GetResponse() |
||||||
|
{ |
||||||
|
return "Hello, 这是热更新后的版本!"; |
||||||
|
} |
||||||
|
|
||||||
|
REGISTERCLASS(HelloHandler_v1) |
@ -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<IID_HELLO_HANDLER, IHelloHandler> |
||||||
|
{ |
||||||
|
public: |
||||||
|
std::string GetResponse() override; |
||||||
|
}; |
@ -1,54 +1,58 @@ |
|||||||
#include <drogon/drogon.h> |
#include "HelloWorldController.h" |
||||||
#include <iostream> |
|
||||||
#include "HelloWorldController.h" |
|
||||||
#include "RuntimeObjectSystem/IObject.h" |
|
||||||
#include "RuntimeObjectSystem/ObjectInterfacePerModule.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<IID_DEFAULT, IHelloHandler> |
HelloWorldController::~HelloWorldController() |
||||||
{ |
{ |
||||||
public: |
delete m_pRuntimeSystem; |
||||||
std::string GetResponse() override |
delete m_pCompilerLogger; |
||||||
{ |
} |
||||||
return "Hello from Version 1!"; |
|
||||||
} |
|
||||||
}; |
|
||||||
REGISTERCLASS(HelloHandler_v1); |
|
||||||
|
|
||||||
void HelloWorldController::asyncHandleHttpRequest( |
void HelloWorldController::asyncHandleHttpRequest( |
||||||
const HttpRequestPtr &req, |
const drogon::HttpRequestPtr &req, |
||||||
std::function<void(const HttpResponsePtr &)> &&callback) |
std::function<void(const drogon::HttpResponsePtr &)> &&callback) |
||||||
{ |
{ |
||||||
// 初始化 RCCPP 系统(仅首次)
|
// 初始化检查
|
||||||
if (!m_pRuntimeSystem) |
if (!m_pRuntimeSystem) |
||||||
{ |
{ |
||||||
m_pRuntimeSystem = new RuntimeObjectSystem; |
m_pRuntimeSystem = new RuntimeObjectSystem; |
||||||
m_pRuntimeSystem->Initialise(new StdioLogSystem(), nullptr); |
m_pCompilerLogger = new StdioLogSystem(); |
||||||
|
|
||||||
// 创建初始对象
|
if (!m_pRuntimeSystem->Initialise(m_pCompilerLogger, nullptr)) |
||||||
auto pCtor = m_pRuntimeSystem->GetObjectFactorySystem()->GetConstructor("HelloHandler_v1"); |
|
||||||
if (pCtor) |
|
||||||
{ |
{ |
||||||
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; |
IHelloHandler *pHandler = nullptr; |
||||||
if (auto pObj = m_pRuntimeSystem->GetObjectFactorySystem()->GetObject(m_ObjectId)) |
if (IObject *pObj = m_pRuntimeSystem->GetObjectFactorySystem()->GetObject(m_ObjectId)) |
||||||
{ |
{ |
||||||
pObj->GetInterface(&pHandler); |
pObj->GetInterface(&pHandler); |
||||||
} |
} |
||||||
|
|
||||||
// 生成响应
|
// 生成响应
|
||||||
auto resp = HttpResponse::newHttpResponse(); |
auto resp = drogon::HttpResponse::newHttpResponse(); |
||||||
resp->setBody(pHandler ? pHandler->GetResponse() : "Error: Handler not loaded"); |
resp->setBody(pHandler ? pHandler->GetResponse() : "Handler not available"); |
||||||
callback(resp); |
callback(resp); |
||||||
} |
} |
@ -1,19 +1,31 @@ |
|||||||
#pragma once |
#pragma once |
||||||
#include <drogon/HttpSimpleController.h> |
#include <drogon/HttpSimpleController.h> |
||||||
#include "RuntimeObjectSystem/RuntimeObjectSystem.h" // 全局作用域 |
#include "RuntimeObjectSystem/RuntimeObjectSystem.h" |
||||||
|
#include "RuntimeObjectSystem/IObject.h" |
||||||
|
#include "RuntimeObjectSystem/IObjectFactorySystem.h" // 新增 |
||||||
|
|
||||||
class HelloWorldController : public HttpSimpleController<HelloWorldController> |
// 前向声明
|
||||||
|
struct IRuntimeObjectSystem; |
||||||
|
struct IObject; |
||||||
|
struct IObjectFactorySystem; // 新增
|
||||||
|
struct ICompilerLogger; // 新增
|
||||||
|
|
||||||
|
class HelloWorldController : public drogon::HttpSimpleController<HelloWorldController> |
||||||
{ |
{ |
||||||
public: |
public: |
||||||
|
HelloWorldController(); // 显式声明构造函数
|
||||||
|
~HelloWorldController(); // 显式声明析构函数
|
||||||
|
|
||||||
|
virtual void asyncHandleHttpRequest( |
||||||
|
const drogon::HttpRequestPtr &req, |
||||||
|
std::function<void(const drogon::HttpResponsePtr &)> &&callback) override; |
||||||
|
|
||||||
PATH_LIST_BEGIN |
PATH_LIST_BEGIN |
||||||
PATH_ADD("/hello", Get); |
PATH_ADD("/hello", drogon::Get); |
||||||
PATH_LIST_END |
PATH_LIST_END |
||||||
|
|
||||||
void asyncHandleHttpRequest(const HttpRequestPtr &req, |
|
||||||
std::function<void(const HttpResponsePtr &)> &&callback) override; |
|
||||||
|
|
||||||
private: |
private: |
||||||
// RCCPP 相关成员(非继承)
|
IRuntimeObjectSystem *m_pRuntimeSystem = nullptr; |
||||||
IRuntimeObjectSystem *m_pRuntimeSystem; |
ICompilerLogger *m_pCompilerLogger = nullptr; |
||||||
ObjectId m_ObjectId; |
ObjectId m_ObjectId; |
||||||
}; |
}; |
@ -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 <stdarg.h> |
||||||
|
#include <assert.h> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
#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 |
||||||
|
} |
@ -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 <string> |
||||||
|
#include <stdio.h> |
||||||
|
|
||||||
|
// 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
|
Loading…
Reference in new issue