删除自动指针内存释放,改为跟随request

This commit is contained in:
Dev User
2026-06-17 16:47:05 +08:00
parent eb42687610
commit 176eb29ef9
11 changed files with 29 additions and 135 deletions

View File

@@ -22,7 +22,6 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#include "module/http/request.h"
#include "module/http/response.h"
#include "net/http_interceptor.h"
#include "module/request_temp_ptr.h"
fastweb::interceptor_manager::interceptor_manager(fastweb::app* app):Interface(app)
{
}
@@ -70,10 +69,8 @@ bool fastweb::interceptor_manager::callback(network::http::reqpack* reqpack, con
}
module::request m_request(reqpack->request().get());
module::response m_response(reqpack->response().get());
module::request_temp_ptr fw_request_temp_ptr;
(*lua->state)["fw_response"] = m_response;
(*lua->state)["fw_request"] = m_request;
(*lua->state)["fw_request_temp_ptr"] = fw_request_temp_ptr;
sol::protected_function_result result = script();
if (!result.valid()) {
sol::error err = result;

View File

@@ -39,8 +39,6 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#include "module/ini.h"
#include "module/codec.h"
#include "module/queue.h"
#include "module/request_temp_ptr.h"
fastweb::module_manager::module_manager(fastweb::app* app):Interface(app)
{
}
@@ -207,7 +205,6 @@ void fastweb::module_manager::load_core(sol::state* lua)
module::ini::regist(lua);
module::codec::regist(lua);
module::queue::regist(lua);
module::request_temp_ptr::regist(lua);
app()->global->regist(lua);
}

View File

@@ -22,7 +22,6 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#include "module/http/request.h"
#include "module/http/response.h"
#include "net/http_subscribe.h"
#include "module/request_temp_ptr.h"
fastweb::subscribe_manager::subscribe_manager(fastweb::app* app) :Interface(app)
{
}
@@ -72,10 +71,8 @@ bool fastweb::subscribe_manager::callback(network::http::request* request, netwo
}
module::request m_request(request);
module::response m_response(response);
module::request_temp_ptr fw_request_temp_ptr;
(*lua->state)["fw_response"] = m_response;
(*lua->state)["fw_request"] = m_request;
(*lua->state)["fw_request_temp_ptr"] = fw_request_temp_ptr;
sol::protected_function_result result = script();
if (!result.valid()) {

View File

@@ -20,15 +20,15 @@ namespace module
/// </summary>
virtual void delete_global() = 0;
/// <summary>
/// 全局变量阶段获取自身指针
/// 全局变量阶段获取自身指针
/// </summary>
/// <returns></returns>
virtual void* self() { return this; }
virtual int64_t self_ptr() { return (int64_t)this; }
/// <summary>
/// 释放自身(非指针释放非lua引用释放仅释放内部资源)
/// </summary>
virtual void self_free() = 0;
virtual void self_free() {};
};
}

View File

@@ -17,11 +17,19 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#include "request.h"
#include "net/http_reqpack.h"
#include "module/basemodule.h"
module::request::request(network::http::request* request) :m_request(request)
{
}
module::request::~request()
{
for(int64_t p : m_auto_free_list)
{
auto* mod = reinterpret_cast<module::base*>(static_cast<uintptr_t>(p));
if (mod)
mod->self_free();
}
m_auto_free_list.clear();
}
sol::table module::request::body_param(sol::this_state s)
{
@@ -236,3 +244,9 @@ bool module::request::request_param(const std::string& name, std::string& value)
return m_request->get_body_param(name, value);
return true;
}
void module::request::push_auto_free(int64_t p)
{
if(p == 0)
return;
m_auto_free_list.push_back(p);
}

View File

@@ -115,6 +115,15 @@ namespace module
/// </summary>
/// <returns></returns>
bool save_body(const std::string& filepath);
/// <summary>
/// 推送到自动释放队列
/// </summary>
/// <param name="p"></param>
void push_auto_free(int64_t p);
static void regist(sol::state* lua);
private:
void multipart_content_check(int id);
@@ -122,6 +131,7 @@ namespace module
bool request_param(const std::string& name, std::string& value);
private:
network::http::request* m_request = nullptr;
std::vector<int64_t> m_auto_free_list;
};
}

View File

@@ -29,7 +29,6 @@ namespace module
// 通过 imodule 继承
virtual void regist_global(const char* name, sol::state* lua);
virtual void delete_global() { delete this; }
virtual void self_free() { }
std::mutex m_mutex;
};

View File

@@ -15,7 +15,7 @@ namespace module
static void regist(sol::state* lua);
virtual void regist_global(const char* name, sol::state* lua) override;
virtual void delete_global() { delete this; }
virtual void self_free() {}
private:
ylib::queue<std::string> m_queue;
};

View File

@@ -1,98 +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 "request_temp_ptr.h"
namespace {
const char kModuleBaseRegKey[] = "fastweb_fw_module_base_usertype";
}
void module::request_temp_ptr::push_from_lua(request_temp_ptr& self, sol::object arg, sol::this_state s)
{
lua_State* L = s.lua_state();
arg.push();
const int idx = lua_absindex(L, -1);
const sol::type ty = sol::type_of(L, idx);
module::base* base = nullptr;
if (ty == sol::type::lightuserdata) {
base = static_cast<module::base*>(lua_touserdata(L, idx));
lua_pop(L, 1);
}
else if (ty == sol::type::userdata) {
sol::stack::record tracking{};
auto maybe = sol::stack::check_get<module::base*>(L, idx, &sol::no_panic, tracking);
lua_pop(L, 1);
if (maybe) {
base = *maybe;
}
else {
luaL_error(L, "fw_request_temp_ptr.push: full userdata is not a module::base* "
"(use conn:self() for light pointer, or register your type with sol::bases<module::base>)");
return;
}
}
else {
lua_pop(L, 1);
luaL_error(L, "fw_request_temp_ptr.push: expected lightuserdata (e.g. obj:self()) or userdata");
return;
}
if (base == nullptr) {
luaL_error(L, "fw_request_temp_ptr.push: null pointer");
return;
}
self.m_queue.push(base);
}
module::request_temp_ptr::request_temp_ptr()
{
}
module::request_temp_ptr::~request_temp_ptr()
{
clear();
}
void module::request_temp_ptr::clear()
{
while (m_queue.size() > 0)
{
module::base* base = m_queue.front();
m_queue.pop();
base->self_free();
}
}
void module::request_temp_ptr::regist(sol::state* lua)
{
lua_State* L = lua->lua_state();
if (lua_getfield(L, LUA_REGISTRYINDEX, kModuleBaseRegKey) == LUA_TNIL) {
lua_pop(L, 1);
lua->new_usertype<module::base>("fw_module_base", sol::no_constructor);
lua_pushboolean(L, 1);
lua_setfield(L, LUA_REGISTRYINDEX, kModuleBaseRegKey);
}
else {
lua_pop(L, 1);
}
lua->new_usertype<module::request_temp_ptr>("fw_request_temp_ptr",
"push", &module::request_temp_ptr::push_from_lua
);
}

View File

@@ -1,22 +0,0 @@
#pragma once
#include "sol/sol.hpp"
#include "module/basemodule.h"
#include <queue>
namespace module
{
class request_temp_ptr{
public:
request_temp_ptr();
~request_temp_ptr();
void clear();
static void regist(sol::state* lua);
private:
static void push_from_lua(request_temp_ptr& self, sol::object arg, sol::this_state s);
std::queue<module::base*> m_queue;
};
}

View File

@@ -39,7 +39,7 @@ namespace module
static void regist(sol::state* lua);
virtual void regist_global(const char* name, sol::state* lua) override;
virtual void delete_global() { delete this; }
virtual void self_free() { }
private:
virtual bool run();