Skip to content

Commit

Permalink
Merge branch 'master' of https://gitee.com/xiyoo0812/quanta
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaobangyu committed Apr 19, 2024
2 parents 88d3af9 + 48b0f42 commit d59763b
Show file tree
Hide file tree
Showing 62 changed files with 1,543 additions and 2,411 deletions.
1 change: 1 addition & 0 deletions core/luabus/src/luabus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace luabus {
lluabus.set_function("tcp", create_tcp);
lluabus.set_function("host", gethostip);
lluabus.set_function("dns", gethostbydomain);
lluabus.set_function("derive_port", derive_port);
lluabus.set_function("create_socket_mgr", create_socket_mgr);
lluabus.new_enum("eproto_type",
"pb", eproto_type::proto_pb,
Expand Down
21 changes: 21 additions & 0 deletions core/luabus/src/socket_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ bool get_ip_string(char ip[], size_t ip_size, const void* addr) {
return inet_ntop(ipv4->sin_family, &ipv4->sin_addr, ip, ip_size) != nullptr;
}


int derive_port(int port){
socket_t fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
#if defined(__ORBIS__) || defined(__PROSPERO__)
addr->sin_len = sizeof(*ipv4);
#endif
int try_cnt = 20;
while (try_cnt-- > 0) {
addr.sin_port = htons(port);
if (::bind(fd, (sockaddr*)&addr, sizeof(sockaddr_in)) != SOCKET_ERROR) {
closesocket(fd);
return port;
}
port++;
}
return 0;
}

char* get_error_string(char buffer[], int len, int no) {
buffer[0] = '\0';
#ifdef _WIN32
Expand Down
2 changes: 2 additions & 0 deletions core/luabus/src/socket_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ void set_no_delay(socket_t fd, int enable);
void set_close_on_exec(socket_t fd);
void set_reuseaddr(socket_t fd);

int derive_port(int port);

#define MAX_ERROR_TXT 128

char* get_error_string(char buffer[], int len, int no);
Expand Down
24 changes: 24 additions & 0 deletions core/quanta/src/mainlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ QUANTA_API int init_quanta(const char* zfile, const char* fconf) {
return 0;
}

static char buff[1024];
QUANTA_API const char* call_quanta(const char* func) {
if (q_app) {
auto luakit = q_app->state();
if (luakit) {
std::string value;
luakit::lua_table quanta = luakit->get<luakit::lua_table>("quanta");
if (quanta.call(func, nullptr, std::tie(value))) {
memset(buff, 0, 1024);
strcpy(buff, value.c_str());
return buff;
}
}
}
return "";
}

QUANTA_API void stop_quanta() {
if (q_app) {
delete q_app;
q_app = nullptr;
}
}

QUANTA_API int run_quanta() {
if (q_app) {;
if (!q_app->step()) {
Expand Down
4 changes: 3 additions & 1 deletion core/quanta/src/mainlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
extern "C" {
#endif

QUANTA_API int init_quanta(const char* zfile, const char* fconf);
QUANTA_API int run_quanta();
QUANTA_API void stop_quanta();
QUANTA_API int init_quanta(const char* zfile, const char* fconf);
QUANTA_API const char* call_quanta(const char* func);

#ifdef __cplusplus
}
Expand Down
3 changes: 3 additions & 0 deletions core/quanta/src/quanta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ luakit::lua_table quanta_app::init() {
const char* env_service = get_env("QUANTA_SERVICE");
logger::get_logger()->option(env_log_path, env_service, env_index);
}
#ifdef WIN32
m_lua.run_script("os.setlocale('.UTF8')");
#endif
m_lua.run_script(fmt::format("require '{}'", get_env("QUANTA_SANDBOX")), [&](std::string_view err) {
exception_handler("load sandbox err: {}", err);
});
Expand Down
2 changes: 2 additions & 0 deletions core/quanta/src/quanta.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class quanta_app final
void set_env(std::string key, std::string value, int over = 0);

luakit::lua_table init();
luakit::kit_state* state() { return &m_lua; };

lua_State* L() { return m_lua.L(); }

protected:
Expand Down
Loading

0 comments on commit d59763b

Please sign in to comment.