增加是否使用预编译字节码宏,优化部分代码

This commit is contained in:
xx
2024-05-29 21:07:04 +08:00
parent 99aa41abba
commit 790c9aea0d
14 changed files with 233 additions and 25 deletions

View File

@@ -1,4 +1,6 @@
#include "bytecodemanager.h"
#include "bytecodemanager.h"
#if ENABLE_BYTECODE == 1
#include "fastweb.h"
#include "utils/luautils.h"
#include "util/file.h"
@@ -75,3 +77,4 @@ bool bytecode_manager::run()
return true;
}
#endif

View File

@@ -1,6 +1,7 @@
#pragma once
#include "core/define.h"
#if ENABLE_BYTECODE == 1
#include "sol/sol.hpp"
#include "base/error.h"
@@ -40,4 +41,5 @@ private:
// 通过 ithread 继承
bool run() override;
};
};
#endif

View File

@@ -27,4 +27,7 @@
#define VarType sol::object
#define VarType sol::object
#define ENABLE_BYTECODE 0

View File

@@ -59,6 +59,7 @@ bool fastweb::start()
// 初始化脚本
if (initialization_script() == false)
return false;
#if ENABLE_BYTECODE == 1
// 加载服务脚本
{
auto luas = ylib::file::traverse(sConfig->scripts.app_dir, "(.*\\.lua)");
@@ -73,6 +74,7 @@ bool fastweb::start()
}
}
}
// 加载拦截器脚本
for_iter(iter, sConfig->website.interceptor_scripts) {
if (interceptor_bytecode.create(iter->regex_express, iter->filepath, true) == false)
@@ -81,6 +83,7 @@ bool fastweb::start()
return false;
}
}
// 加入LUA服务映射
{
auto map = service_bytecode.map();
@@ -146,10 +149,85 @@ bool fastweb::start()
}
}
#else
// 加入LUA服务映射
{
auto luas = ylib::file::traverse(sConfig->scripts.app_dir, "(.*\\.lua)");
for_iter(iter, luas)
{
if (iter->second == IS_DIRECTORY)
continue;
std::string path = strutils::replace(iter->first, '\\', '/');
auto state = sStateMgr->get_state();
std::string route_pattern;
network::http::method method = network::http::ALL;
try
{
auto result = state->script_file(sConfig->scripts.app_dir+"/"+ path);
if (result.valid()) {
auto router = (*state)["route"];
auto type = router.get_type();
if (router.is<sol::table>())
{
sol::optional<std::string> route_pattern_param = router[1];
sol::optional<int> method_param = router[2];
if (route_pattern_param && route_pattern_param->empty() == false)
route_pattern = *route_pattern_param;
if (method_param)
method = (network::http::method)*method_param;
}
}
}
catch (const std::exception& e)
{
LOG_ERROR(e.what());
}
if (route_pattern.empty())
route_pattern = sConfig->scripts.app_mapping_dir + path;
// OutPutLog
{
std::string log;
log = "[subscribe] lua: " +path + "\t pattern: " + route_pattern + "\t method: ";
switch (method)
{
case ylib::network::http::GET:
log.append("GET");
break;
case ylib::network::http::POST:
log.append("POST");
break;
case ylib::network::http::PUT:
log.append("PUT");
break;
case ylib::network::http::DEL:
log.append("DEL");
break;
case ylib::network::http::HEAD:
log.append("HEAD");
break;
case ylib::network::http::ALL:
log.append("ALL");
break;
default:
break;
}
LOG_INFO(log);
}
router->subscribe(route_pattern, method, &fastweb::subscribe_service, new std::string(sConfig->scripts.app_dir+"/" +path));
}
}
#endif
// 加入拦截器
for(size_t i=0;i<sConfig->website.interceptor_scripts.size();i++)
router->interceptor()->add(sConfig->website.interceptor_scripts[i].regex_express,&fastweb::subscribe_interceptor);
for (size_t i = 0; i < sConfig->website.interceptor_scripts.size(); i++)
{
#if ENABLE_BYTECODE == 0
interceptor.emplace(sConfig->website.interceptor_scripts[i].regex_express,sConfig->website.interceptor_scripts[i].filepath);
#endif
router->interceptor()->add(sConfig->website.interceptor_scripts[i].regex_express, &fastweb::subscribe_interceptor);
}
router->other([&](network::http::request* request, network::http::response* response) {
@@ -232,10 +310,14 @@ void fastweb::subscribe_service(network::http::request* request, network::http::
std::string exception_string;
try
{
#if ENABLE_BYTECODE == 1
auto bytecode = sFastWeb->service_bytecode.get(lua_name);
if (bytecode.empty())
throw ylib::exception("Serious error: Bytecode not found, possibly due to pre compilation modification error. Please recheck the script file, "+lua_name);
auto lbResult = lua->load_buffer(bytecode.data(), bytecode.length(), "bytecode");
#else
auto lbResult = lua->load_file(lua_name);
#endif
if (lbResult.valid() == false)
{
sol::error err = lbResult;
@@ -276,11 +358,15 @@ bool fastweb::subscribe_interceptor(network::http::reqpack* reqpack, const std::
std::string exception_string;
try
{
#if ENABLE_BYTECODE == 1
const std::string& bytecode = sFastWeb->interceptor_bytecode.get(express_string);
if (bytecode.empty())
throw ylib::exception("[interceptor] Serious error: Bytecode not found, possibly due to pre compilation modification error. Please recheck the script file, " + express_string);
auto lbResult = lua->load_buffer(bytecode.data(), bytecode.length(), "bytecode");
#else
auto lbResult = lua->load_file(sFastWeb->interceptor[express_string]);
#endif
if (lbResult.valid() == false)
{
sol::error err = lbResult;

View File

@@ -42,8 +42,12 @@ private:
private:
network::http::center *m_center = nullptr;
public:
#if ENABLE_BYTECODE == 1
// 服务字节码
bytecode_manager service_bytecode;
// 拦截器字节码
bytecode_manager interceptor_bytecode;
#else
std::map<std::string, std::string> interceptor;
#endif
};

View File

@@ -21,7 +21,7 @@
#include "module/mutex.h"
#include "module/codec.h"
#include "module/time.h"
#include "module/file.h"
#define LOOP_STATE_USE 1
bool state_manager::start()
{
@@ -81,6 +81,7 @@ sol::state* state_manager::create_state()
module::auto_lock::regist(lua);
module::codec::regist(lua);
module::time::regist(lua);
module::file::regist(lua);
global::getInstance()->regist_lua(lua);
return lua;

29
src/module/file.cpp Normal file
View File

@@ -0,0 +1,29 @@
#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::regist(sol::state* lua)
{
(*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE;
(*lua)["IS_DIRECTORY"] = (int)ylib::FileType::IS_DIRECTORY;
lua->new_usertype<module::file>("file",
"list", &module::file::list
);
}

17
src/module/file.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include "sol/sol.hpp"
#include "imodule.h"
namespace module
{
/// <summary>
/// 文件
/// </summary>
class file:public module::imodule {
public:
static sol::table list(const std::string& dirpath,const std::string& regex,sol::this_state s);
static void regist(sol::state* lua);
};
}

View File

@@ -37,6 +37,10 @@ sol::table module::request::url_param(sol::this_state s)
result_table[iter->first] = iter->second;
return result_table;
}
std::string module::request::body()
{
return m_request->parser()->text();
}
void* module::request::website()
{
return m_request->website();
@@ -55,7 +59,8 @@ void module::request::regist(sol::state& state)
"session", &module::request::session,
"token", &module::request::token,
"body_param", &module::request::body_param,
"url_param", &module::request::url_param
"url_param", &module::request::url_param,
"body", &module::request::body
);
state["GET"] = (int)network::http::GET;
state["POST"] = (int)network::http::POST;

View File

@@ -27,6 +27,7 @@ namespace module
sol::table body_param(sol::this_state s);
sol::table url_param(sol::this_state s);
std::string body();
void* website();
static void regist(sol::state& state);

View File

@@ -8,43 +8,70 @@ module::mssql::mssql(const std::string& connstring)
module::mssql::~mssql()
{
}
std::string module::mssql::get_str(const std::string& name)
{
if (m_iter == m_rows.end())
throw ylib::exception("no recorded data");
return m_iter->get<std::string>(name);
std::string result;
result = m_iter->get<std::string>(name, result);
return result;
}
std::string module::mssql::get_datetime(const std::string& name)
{
if (m_iter == m_rows.end())
throw ylib::exception("no recorded data");
std::tm result;
memset(&result, 0, sizeof(result));
result = m_iter->get<std::tm>(name, result);
if (result.tm_year != 0)
{
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", &result);
std::string dateTimeStr(buffer);
return dateTimeStr;
}
return "";
}
int32 module::mssql::get_i32(const std::string& name)
{
if (m_iter == m_rows.end())
throw ylib::exception("no recorded data");
return m_iter->get<int32>(name);
int32 result = 0;
result = m_iter->get<int32>(name, result);
return result;
}
int64 module::mssql::get_i64(const std::string& name)
{
if (m_iter == m_rows.end())
throw ylib::exception("no recorded data");
return m_iter->get<int64>(name);
int64 result = 0;
result = m_iter->get<int64>(name, result);
return result;
}
double module::mssql::get_dob(const std::string& name)
{
if (m_iter == m_rows.end())
throw ylib::exception("no recorded data");
return m_iter->get<double>(name);
double result = 0.0f;
result = m_iter->get<double>(name, result);
return result;
}
void module::mssql::query(const std::string& sql)
{
std::cout << "[mssql][query]: " << sql << std::endl;
m_read = false;
m_rows = (m_session->prepare << sql);
//m_iter = m_rows.begin();
}
int64 module::mssql::update(const std::string& sql)
{
std::cout << "[mssql][update]: " << sql << std::endl;
soci::statement st = m_session->prepare << sql;
st.execute();
return st.get_affected_rows();
@@ -54,12 +81,13 @@ bool module::mssql::next()
if (m_read == false)
{
m_iter = m_rows.begin();
return true;
return m_iter != m_rows.end();
}
if (m_iter == m_rows.end())
return false;
m_iter++;
return true;
return m_iter != m_rows.end();
}
@@ -73,7 +101,8 @@ void module::mssql::regist(sol::state& lua)
"get_str", &module::mssql::get_str,
"next", &module::mssql::next,
"query", &module::mssql::query,
"update", &module::mssql::update
"update", &module::mssql::update,
"get_datetime", &module::mssql::get_datetime
);
}
#endif

View File

@@ -18,6 +18,7 @@ namespace module
/// 取数据
/// </summary>
std::string get_str(const std::string& name);
std::string get_datetime(const std::string& name);
int32 get_i32(const std::string& name);
int64 get_i64(const std::string& name);
double get_dob(const std::string& name);

View File

@@ -39,6 +39,12 @@ module::select& module::select::where_expression(const std::string& expression)
return *this;
}
module::select& module::select::where_like(const std::string& name, const std::string& value)
{
m_select->where_like(name, value);
return *this;
}
module::select& module::select::table(const std::string& table_name)
{
m_select->table(table_name);
@@ -79,7 +85,7 @@ module::select& module::select::orderby(const std::string& field, int sort)
void module::select::clear()
{
m_select = nullptr;
m_select->clear();
}
std::shared_ptr<module::mysql_result> module::select::query()
@@ -108,6 +114,8 @@ void module::select::regist(sol::state& lua)
"where_i64", &module::select::where_i64,
"where_str", &module::select::where_str,
"query", &module::select::query,
"clear", &module::select::clear,
"where_like", &module::select::where_like,
"clear", &module::select::clear
);
}
@@ -210,6 +218,11 @@ uint64 module::update::exec()
return m_update->exec();
}
void module::update::clear()
{
m_update->clear();
}
void module::update::regist(sol::state& lua)
{
lua.new_usertype<module::update>("mysql_builder_update",
@@ -228,7 +241,8 @@ void module::update::regist(sol::state& lua)
"set_dob", &module::update::set_dob,
"set_i32", &module::update::set_i32,
"set_i64", &module::update::set_i64,
"set_str", &module::update::set_str
"set_str", &module::update::set_str,
"clear", &module::update::clear
);
}
@@ -282,6 +296,11 @@ uint64 module::insert::exec()
return m_insert->exec();
}
void module::insert::clear()
{
m_insert->clear();
}
void module::insert::regist(sol::state& lua)
{
lua.new_usertype<module::insert>("mysql_builder_insert",
@@ -292,7 +311,8 @@ void module::insert::regist(sol::state& lua)
"set_dob", &module::insert::set_dob,
"set_i32", &module::insert::set_i32,
"set_i64", &module::insert::set_i64,
"set_str", &module::insert::set_str
"set_str", &module::insert::set_str,
"clear", &module::insert::clear
);
}
@@ -365,6 +385,11 @@ uint64 module::delete_::exec()
return m_delete->exec();
}
void module::delete_::clear()
{
m_delete->clear();
}
void module::delete_::regist(sol::state& lua)
{
lua.new_usertype<module::delete_>("mysql_builder_delete",
@@ -378,7 +403,8 @@ void module::delete_::regist(sol::state& lua)
"where_expression", &module::delete_::where_expression,
"where_i32", &module::delete_::where_i32,
"where_i64", &module::delete_::where_i64,
"where_str", &module::delete_::where_str
"where_str", &module::delete_::where_str,
"clear", &module::delete_::clear
);
}
@@ -529,7 +555,7 @@ VarType module::mysql_result::get(sol::object obj, sol::this_state s)
GET_VALUE(get_int32);
}
else if (type == "varchar" || type == "char" || type == "text")
else if (type == "varchar" || type == "char" || type == "text" || type == "datetime")
{
GET_VALUE(get_string);
}

View File

@@ -76,6 +76,7 @@ namespace module
module::select& where_dob(const std::string& name, const std::string& expression, double value);
module::select& where_str(const std::string& name, const std::string& expression, const std::string& value);
module::select& where_expression(const std::string& expression);
module::select& where_like(const std::string& name,const std::string& value);
module::select& table(const std::string& table_name);
module::select& field(sol::table table);
module::select& page(uint32 page, uint32 count);
@@ -107,7 +108,7 @@ namespace module
module::update& limit(uint32 start, uint32 count);
module::update& orderby(const std::string& field, int sort);
uint64 exec();
void clear();
static void regist(sol::state& lua);
private:
std::shared_ptr<ylib::update> m_update;
@@ -123,7 +124,7 @@ namespace module
module::insert& set_str(const std::string& name, const std::string& value);
module::insert& set_not_ppst(const std::string& name, const std::string& value);
uint64 exec();
void clear();
static void regist(sol::state& lua);
private:
std::shared_ptr<ylib::insert> m_insert;
@@ -142,7 +143,7 @@ namespace module
module::delete_& limit(uint32 start, uint32 count);
module::delete_& orderby(const std::string& field, int sort);
uint64 exec();
void clear();
static void regist(sol::state& lua);
private:
std::shared_ptr <ylib::delete_> m_delete;