Skip to content

Commit

Permalink
若干代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed May 5, 2024
1 parent 13cd0aa commit 0b042e8
Show file tree
Hide file tree
Showing 94 changed files with 3,105 additions and 2,540 deletions.
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ stds.quanta = {
"import", "class", "enum", "mixin", "property", "singleton", "super", "implemented",
"logfeature", "db_property", "classof", "is_class", "is_subclass", "instanceof", "conv_class", "class_review",
"codec", "stdfs", "luabus", "luakit", "json", "protobuf", "timer", "aoi", "log", "worker", "http", "bson",
"detour", "lmdb", "unqlite", "sqlite", "ssl"
"detour", "lmdb", "unqlite", "sqlite", "ssl", "xml"
}
}
std = "max+quanta"
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ luaext:
cd extend/ldetour; make SOLUTION_DIR=$(CUR_DIR) -f ldetour.mak;
cd extend/ljson; make SOLUTION_DIR=$(CUR_DIR) -f ljson.mak;
cd extend/lmdb; make SOLUTION_DIR=$(CUR_DIR) -f lmdb.mak;
cd extend/lminiz; make SOLUTION_DIR=$(CUR_DIR) -f lminiz.mak;
cd extend/lsqlite; make SOLUTION_DIR=$(CUR_DIR) -f lsqlite.mak;
cd extend/lssl; make SOLUTION_DIR=$(CUR_DIR) -f lssl.mak;
cd extend/lstdfs; make SOLUTION_DIR=$(CUR_DIR) -f lstdfs.mak;
cd extend/ltimer; make SOLUTION_DIR=$(CUR_DIR) -f ltimer.mak;
cd extend/lualog; make SOLUTION_DIR=$(CUR_DIR) -f lualog.mak;
cd extend/luapb; make SOLUTION_DIR=$(CUR_DIR) -f luapb.mak;
cd extend/luaxlsx; make SOLUTION_DIR=$(CUR_DIR) -f luaxlsx.mak;
cd extend/luaxml; make SOLUTION_DIR=$(CUR_DIR) -f luaxml.mak;
cd extend/lunqlite; make SOLUTION_DIR=$(CUR_DIR) -f lunqlite.mak;
cd extend/lworker; make SOLUTION_DIR=$(CUR_DIR) -f lworker.mak;

share:
cd extend/mimalloc; make SOLUTION_DIR=$(CUR_DIR) -f mimalloc.mak;
cd extend/luaxlsx; make SOLUTION_DIR=$(CUR_DIR) -f miniz.mak;

2 changes: 1 addition & 1 deletion core/luabus/luabus.lmak
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ENABLE = true
--需要的include目录
INCLUDES = {
"../../extend/lua/lua",
"../../extend/ltimer/ltimer",
"../../extend/ltimer/src",
"../../extend/luakit/include"
}

Expand Down
40 changes: 23 additions & 17 deletions core/luabus/luabus.mak
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ STDCPP = -std=c++17

#需要的include目录
MYCFLAGS += -I../../extend/lua/lua
MYCFLAGS += -I../../extend/ltimer/ltimer
MYCFLAGS += -I../../extend/ltimer/src
MYCFLAGS += -I../../extend/luakit/include

#需要定义的选项
Expand All @@ -42,12 +42,6 @@ MYCFLAGS += -I../../extend/luakit/include
LDFLAGS =


#源文件路径
SRC_DIR = src

#需要排除的源文件,目录基于$(SRC_DIR)
EXCLUDE =

#需要连接的库文件
LIBS =
ifneq ($(UNAME_S), Darwin)
Expand Down Expand Up @@ -99,21 +93,32 @@ LDFLAGS += -L$(SOLUTION_DIR)bin
LDFLAGS += -L$(SOLUTION_DIR)library

#自动生成目标
OBJS =
#根目录
OBJS += $(patsubst $(SRC_DIR)/%.c, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.c)))
OBJS += $(patsubst $(SRC_DIR)/%.m, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.m)))
OBJS += $(patsubst $(SRC_DIR)/%.cc, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cc)))
OBJS += $(patsubst $(SRC_DIR)/%.cpp, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cpp)))
SOURCES =
SOURCES += src\lua_socket_mgr.cpp
SOURCES += src\lua_socket_node.cpp
SOURCES += src\luabus.cpp
SOURCES += src\socket_helper.cpp
SOURCES += src\socket_listener.cpp
SOURCES += src\socket_mgr.cpp
SOURCES += src\socket_router.cpp
SOURCES += src\socket_stream.cpp
SOURCES += src\socket_tcp.cpp
SOURCES += src\socket_udp.cpp
SOURCES += src\stdafx.cpp

CSOURCES = $(patsubst %.c, $(INT_DIR)/%.o, $(SOURCES))
MSOURCES = $(patsubst %.m, $(INT_DIR)/%.o, $(CSOURCES))
CCSOURCES = $(patsubst %.cc, $(INT_DIR)/%.o, $(MSOURCES))
OBJS = $(patsubst %.cpp, $(INT_DIR)/%.o, $(CCSOURCES))

# 编译所有源文件
$(INT_DIR)/%.o : $(SRC_DIR)/%.c
$(INT_DIR)/%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
$(INT_DIR)/%.o : $(SRC_DIR)/%.m
$(INT_DIR)/%.o : %.m
$(CC) $(CFLAGS) -c $< -o $@
$(INT_DIR)/%.o : $(SRC_DIR)/%.cc
$(INT_DIR)/%.o : %.cc
$(CX) $(CXXFLAGS) -c $< -o $@
$(INT_DIR)/%.o : $(SRC_DIR)/%.cpp
$(INT_DIR)/%.o : %.cpp
$(CX) $(CXXFLAGS) -c $< -o $@

$(TARGET_DYNAMIC) : $(OBJS)
Expand All @@ -130,6 +135,7 @@ clean :
pre_build:
mkdir -p $(INT_DIR)
mkdir -p $(TARGET_DIR)
mkdir -p $(INT_DIR)/src

#后编译
post_build:
2 changes: 1 addition & 1 deletion core/luabus/luabus.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Develop|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\..\extend\lua\lua;..\..\extend\ltimer\ltimer;..\..\extend\luakit\include;$(SolutionDir)extend\mimalloc\mimalloc\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\extend\lua\lua;..\..\extend\ltimer\src;..\..\extend\luakit\include;$(SolutionDir)extend\mimalloc\mimalloc\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
Expand Down
25 changes: 11 additions & 14 deletions core/luabus/luabus.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="src\lua_socket_mgr.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\lua_socket_node.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_dns.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_helper.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_listener.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_mgr.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_router.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_stream.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_tcp.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\socket_udp.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\stdafx.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -71,9 +71,6 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="inc">
<UniqueIdentifier>{CF9F3FDE-7326-F1D8-E642-05F0E07A3695}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{25D902C2-4283-AB8C-FBAC-54DFA101AD31}</UniqueIdentifier>
</Filter>
Expand Down
29 changes: 13 additions & 16 deletions core/quanta/quanta.mak
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ MYCFLAGS += -DFMT_HEADER_ONLY
LDFLAGS =


#源文件路径
SRC_DIR = src

#需要排除的源文件,目录基于$(SRC_DIR)
EXCLUDE =

#需要连接的库文件
LIBS =
ifneq ($(UNAME_S), Darwin)
Expand Down Expand Up @@ -96,21 +90,23 @@ LDFLAGS += -L$(SOLUTION_DIR)bin
LDFLAGS += -L$(SOLUTION_DIR)library

#自动生成目标
OBJS =
#根目录
OBJS += $(patsubst $(SRC_DIR)/%.c, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.c)))
OBJS += $(patsubst $(SRC_DIR)/%.m, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.m)))
OBJS += $(patsubst $(SRC_DIR)/%.cc, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cc)))
OBJS += $(patsubst $(SRC_DIR)/%.cpp, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cpp)))
SOURCES =
SOURCES += src\main.cpp
SOURCES += src\quanta.cpp

CSOURCES = $(patsubst %.c, $(INT_DIR)/%.o, $(SOURCES))
MSOURCES = $(patsubst %.m, $(INT_DIR)/%.o, $(CSOURCES))
CCSOURCES = $(patsubst %.cc, $(INT_DIR)/%.o, $(MSOURCES))
OBJS = $(patsubst %.cpp, $(INT_DIR)/%.o, $(CCSOURCES))

# 编译所有源文件
$(INT_DIR)/%.o : $(SRC_DIR)/%.c
$(INT_DIR)/%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
$(INT_DIR)/%.o : $(SRC_DIR)/%.m
$(INT_DIR)/%.o : %.m
$(CC) $(CFLAGS) -c $< -o $@
$(INT_DIR)/%.o : $(SRC_DIR)/%.cc
$(INT_DIR)/%.o : %.cc
$(CX) $(CXXFLAGS) -c $< -o $@
$(INT_DIR)/%.o : $(SRC_DIR)/%.cpp
$(INT_DIR)/%.o : %.cpp
$(CX) $(CXXFLAGS) -c $< -o $@

$(TARGET_EXECUTE) : $(OBJS)
Expand All @@ -127,6 +123,7 @@ clean :
pre_build:
mkdir -p $(INT_DIR)
mkdir -p $(TARGET_DIR)
mkdir -p $(INT_DIR)/src
ln -s $(TARGET_DIR)/lualog.so $(TARGET_DIR)/liblualog.so

#后编译
Expand Down
5 changes: 1 addition & 4 deletions core/quanta/quanta.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="src\quanta.h">
<Filter>inc</Filter>
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
Expand All @@ -14,9 +14,6 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="inc">
<UniqueIdentifier>{CF9F3FDE-7326-F1D8-E642-05F0E07A3695}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{25D902C2-4283-AB8C-FBAC-54DFA101AD31}</UniqueIdentifier>
</Filter>
Expand Down
118 changes: 0 additions & 118 deletions core/quanta/src/quanta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,125 +119,7 @@ void quanta_app::set_path(std::string field, std::string path) {
#endif
}

bool quanta_app::initzip(const char* zfile) {
memset(&m_archive, 0, sizeof(m_archive));
if(!mz_zip_reader_init_file(&m_archive, zfile, 0)){
return false;
}
m_lua.set_searchers([&](lua_State* L) {
const char* fname = luaL_checkstring(L, 1);
int index = find_zip_file(L, fname);
if (index < 0) {
lua_pushstring(L, fmt::format("file {} not found in zip!", fname).c_str());
return 1;
}
if (load_zip_data(L, fname, index) == LUA_OK) {
lua_pushstring(L, fname); /* will be 2nd argument to module */
return 2; /* return open function and file name */
}
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", lua_tostring(L, 1), fname, lua_tostring(L, -1));
});
m_lua.set_function("dofile", [&](lua_State* L) {
const char* fname = luaL_optstring(L, 1, NULL);
lua_settop(L, 1);
if (load_zip_file(L) != LUA_OK) {
return lua_error(L);
}
auto kf = [](lua_State* L, int d1, lua_KContext d2) { return lua_gettop(L) - 1; };
lua_callk(L, 0, LUA_MULTRET, 0, kf);
return kf(L, 0, 0);
});
m_lua.set_function("loadfile", [&](lua_State* L) {
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
if (load_zip_file(L) == LUA_OK) {
if (env != 0) { /* 'env' parameter? */
lua_pushvalue(L, env); /* environment for loaded function */
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
lua_pop(L, 1); /* remove 'env' if not used by previous call */
}
return 1;
}
//error(message is on top of the stack)* /
lua_pushnil(L);
lua_insert(L, -2);
return 2;
});
return true;
}

int quanta_app::find_zip_file(lua_State* L, std::string filename) {
size_t start_pos = 0;
luakit::lua_guard g(L);
lua_getglobal(L, LUA_LOADLIBNAME);
lua_getfield(L, -1, "path");
std::string path = lua_tostring(L, -1);
while ((start_pos = filename.find(".", start_pos)) != std::string::npos) {
filename.replace(start_pos, strlen("."), LUA_DIRSEP);
start_pos += strlen(LUA_DIRSEP);
}
start_pos = 0;
while ((start_pos = path.find(LUA_PATH_MARK, start_pos)) != std::string::npos) {
path.replace(start_pos, strlen(LUA_PATH_MARK), filename);
start_pos += filename.size();
}
start_pos = 0;
while ((start_pos = path.find(LUA_DIRSEP, start_pos)) != std::string::npos) {
path.replace(start_pos, strlen(LUA_DIRSEP), "/");
start_pos += strlen("/");
}
size_t cur = 0, pos = 0;
while ((pos = path.find(LUA_PATH_SEP, cur)) != std::string::npos) {
std::string sub = path.substr(cur, pos - cur);
int index = mz_zip_reader_locate_file(&m_archive, sub.c_str(), nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE);
if (index > 0) {
return index;
}
cur = pos + strlen(LUA_PATH_SEP);
}
if (path.size() > cur) {
std::string sub = path.substr(cur);
return mz_zip_reader_locate_file(&m_archive, sub.c_str(), nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE);
}
return -1;
}

bool quanta_app::zip_exist(const char* fname) {
return mz_zip_reader_locate_file(&m_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE) > 0;
}

int quanta_app::zip_load(lua_State* L) {
const char* fname = luaL_optstring(L, 1, nullptr);
int index = mz_zip_reader_locate_file(&m_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE);
if (index <= 0) return 0;
size_t size = 0;
const char* data = (const char*)mz_zip_reader_extract_to_heap(&m_archive, index, &size, MZ_ZIP_FLAG_CASE_SENSITIVE);
if (!data) return 0;
lua_pushlstring(L, data, size);
delete[] data;
return 1;
}

int quanta_app::load_zip_file(lua_State* L) {
const char* fname = luaL_optstring(L, 1, nullptr);
int index = mz_zip_reader_locate_file(&m_archive, fname, nullptr, MZ_ZIP_FLAG_CASE_SENSITIVE);
if (index <= 0) {
lua_pushstring(L, fmt::format("file {} not found in zip!", fname).c_str());
return LUA_ERRERR;
}
return load_zip_data(L, fname, index);
}

int quanta_app::load_zip_data(lua_State* L, const char* filename, int index) {
size_t size = 0;
const char* data = (const char*)mz_zip_reader_extract_to_heap(&m_archive, index, &size, MZ_ZIP_FLAG_CASE_SENSITIVE);
if (!data) {
lua_pushstring(L, "file read failed!");
return LUA_ERRERR;
}
int status = luaL_loadbufferx(L, data, size, filename, luaL_optstring(L, 2, nullptr));
delete[] data;
return status;
}

void quanta_app::setup(int argc, const char* argv[]) {
srand((unsigned)time(nullptr));
Expand Down
Loading

0 comments on commit 0b042e8

Please sign in to comment.