├── .clang-format-ignore ├── tools └── gitlab_cicd │ ├── format_check │ ├── linux │ │ ├── .clang-format-ignore │ │ ├── codeformat │ │ └── luacheck │ └── win │ │ └── clang-all-format.bat │ ├── install.bat │ ├── git_pre_commit │ ├── pre-commit │ └── check-pre-commit.sh │ └── clean_cicd_all_pipeinle.sh ├── dependent ├── lua-5.4.4.tar.gz ├── lpeg-1.0.2.tar.gz ├── mimalloc-1.6.3.tar.gz ├── benchmark-1.5.0.tar.gz ├── http-parser-2.9.2.tar.gz ├── c-ares-cares-1_15_0.tar.gz ├── mysql-parser-1.0.0.tar.gz ├── redis-parser-1.0.0.tar.gz ├── openssl-OpenSSL_1_1_1g.tar.gz ├── googletest-release-1.8.1.tar.gz └── multipart-parser-c-1.0.0.tar.gz ├── README.md ├── src ├── lua │ ├── example │ │ ├── start-up.lua │ │ └── config.lua │ ├── service │ │ ├── monitorService.lua │ │ ├── example │ │ │ ├── example_callBuf.lua │ │ │ ├── example_httpResponse.lua │ │ │ ├── example_httpRequest.lua │ │ │ ├── example_dnsResolve.lua │ │ │ ├── example_sleep.lua │ │ │ ├── example_exit.lua │ │ │ ├── example_call.lua │ │ │ ├── example_timer.lua │ │ │ ├── example_command.lua │ │ │ ├── example_queueLock.lua │ │ │ ├── example_mysql.lua │ │ │ ├── example_console.lua │ │ │ ├── example_redis.lua │ │ │ └── example_crypt.lua │ │ ├── bootstrap.lua │ │ ├── logService.lua │ │ ├── debuggerService.lua │ │ └── localServices.lua │ ├── helper │ │ ├── http │ │ │ ├── url.lua │ │ │ ├── httpCodec.lua │ │ │ └── httpTransferCodec.lua │ │ ├── db │ │ │ ├── redisCodec.lua │ │ │ └── mysqlCodec.lua │ │ ├── queueLock.lua │ │ ├── channelPool.lua │ │ ├── logExt.lua │ │ ├── log.lua │ │ └── channelBlock.lua │ └── library │ │ ├── inject.lua │ │ └── injectcode.lua ├── service │ ├── include │ │ ├── stream │ │ │ ├── mysqlStream_t.h │ │ │ ├── tpackStream_t.h │ │ │ ├── lockStepPackStream.h │ │ │ ├── lockStepPackStream_t.h │ │ │ ├── webSocketStream_t.h │ │ │ └── codecStream_t.h │ │ ├── internal │ │ │ ├── timerWatcher_t.h │ │ │ ├── connector_t.h │ │ │ ├── listenPort_t.h │ │ │ ├── dnsResolve_t.h │ │ │ └── service-inl.h │ │ ├── channel │ │ │ ├── channelCenter_t.h │ │ │ └── channel_t.h │ │ ├── serviceCenter_t.h │ │ ├── serviceMonitor_t.h │ │ └── serviceEvent_t.h │ └── source │ │ └── timerWatcher_t.c ├── runtime │ ├── include │ │ ├── internal │ │ │ ├── lenv-inl.h │ │ │ ├── lservice-inl.h │ │ │ ├── lpackagePath_t.h │ │ │ ├── lconnector_t.h │ │ │ ├── ldnsResolve_t.h │ │ │ ├── llistenPort_t.h │ │ │ ├── ltimerWatcher_t.h │ │ │ ├── lloadCache_t.h │ │ │ └── lconfig_t.h │ │ ├── lockStepPack │ │ │ ├── lockStepPackStream.h │ │ │ ├── lockStep.h │ │ │ ├── lockStep_t.h │ │ │ └── lockStepPack.h │ │ ├── msgpack │ │ │ └── lmsgpack_t.h │ │ ├── crypt │ │ │ └── lcrypt_t.h │ │ ├── env │ │ │ └── lenv_t.h │ │ ├── service │ │ │ └── lservice_t.h │ │ ├── db │ │ │ ├── lmysql_t.h │ │ │ └── lredis_t.h │ │ ├── http │ │ │ ├── lhttp_t.h │ │ │ └── lhttpTransfer_t.h │ │ ├── buffer │ │ │ └── lbuffer_t.h │ │ ├── debug │ │ │ └── ldebug_t.h │ │ ├── sharetable │ │ │ └── lsharetable_t.h │ │ ├── stream │ │ │ ├── ltpack_t.h │ │ │ └── lwebSocket_t.h │ │ ├── channel │ │ │ └── lchannelExt_t.h │ │ └── lockstep │ │ │ └── llockstep_t.h │ └── source │ │ ├── internal │ │ ├── lpackagePath_t.c │ │ ├── llistenPort_t.c │ │ ├── lconnector_t.c │ │ ├── ltimerWatcher_t.c │ │ └── ldnsResolve_t.c │ │ └── debug │ │ └── ldebug_t.c ├── core │ ├── include │ │ ├── eventIO │ │ │ ├── eventAsync_t.h │ │ │ ├── internal │ │ │ │ ├── posix │ │ │ │ │ ├── eventWatcher_t.h │ │ │ │ │ ├── poller_t.h │ │ │ │ │ ├── eventListenPort_t.h │ │ │ │ │ ├── eventIO-inl.h │ │ │ │ │ └── eventConnection_t.h │ │ │ │ └── win │ │ │ │ │ ├── eventWatcher_t.h │ │ │ │ │ ├── eventIO-inl.h │ │ │ │ │ ├── iocpExt_t.h │ │ │ │ │ └── eventListenPort_t.h │ │ │ └── eventIOThread_t.h │ │ ├── fs_t.h │ │ ├── spinLock_t.h │ │ ├── cxx-mem │ │ │ ├── new_delete_override_t.hpp │ │ │ └── stl-override_t.hpp │ │ ├── lockStep │ │ │ ├── lockStepPack_t.h │ │ │ └── lockStepPack.h │ │ ├── codec │ │ │ ├── webSocket_t.h │ │ │ └── tpack_t.h │ │ ├── macros_t.h │ │ ├── cbuf_t.h │ │ ├── log_t.h │ │ ├── msgpack │ │ │ ├── msgpackEncode_t.h │ │ │ └── msgpackDecode_t.h │ │ ├── platform_t.h │ │ ├── openssl │ │ │ ├── ssl_t.h │ │ │ └── crypt_t.h │ │ ├── slice_t.h │ │ ├── time_t.h │ │ └── utility_t.h │ └── source │ │ ├── eventIO │ │ └── eventTimer_t.c │ │ └── detail │ │ └── fs_t.c ├── benchmark │ ├── source │ │ └── spin_lock │ │ │ ├── clhLock.h │ │ │ ├── mscLock.h │ │ │ ├── spinLock.h │ │ │ ├── rwSpinLock.h │ │ │ ├── spinLock.c │ │ │ ├── mscLock.c │ │ │ └── clhLock.c │ └── CMakeLists.txt ├── CMakeLists.txt ├── serverBin │ ├── CMakeLists.txt │ └── source │ │ └── main.c └── unittest │ ├── CMakeLists.txt │ └── source │ ├── test_kqueue.cc │ ├── test_time.cc │ └── test_inetAddress.cc ├── cmake ├── msvc │ ├── compiler_c_flags_overrides.cmake │ └── compiler_cxx_flags_overrides.cmake └── modules │ ├── install_3rdparty.cmake │ ├── build_config.cmake │ └── 3rdparty │ ├── build_multipart_parser.cmake │ ├── build_benchmark.cmake │ ├── build_mysql_parser.cmake │ ├── build_redis_parser.cmake │ ├── build_http_parser.cmake │ ├── build_googletest.cmake │ ├── build_mimalloc.cmake │ └── build_cares.cmake ├── .gitignore ├── CMakeLists.txt └── INSTALL.md /.clang-format-ignore: -------------------------------------------------------------------------------- 1 | # ignore third_party code from clang-format checks 2 | -------------------------------------------------------------------------------- /tools/gitlab_cicd/format_check/linux/.clang-format-ignore: -------------------------------------------------------------------------------- 1 | # ignore third_party code from clang-format checks 2 | -------------------------------------------------------------------------------- /tools/gitlab_cicd/install.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | XCOPY .\git_pre_commit\pre-commit ..\..\.git\hooks /s /e /y 4 | -------------------------------------------------------------------------------- /dependent/lua-5.4.4.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/lua-5.4.4.tar.gz -------------------------------------------------------------------------------- /tools/gitlab_cicd/git_pre_commit/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ./tools/gitlab_cicd/git_pre_commit/check-pre-commit.sh -------------------------------------------------------------------------------- /dependent/lpeg-1.0.2.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/lpeg-1.0.2.tar.gz -------------------------------------------------------------------------------- /dependent/mimalloc-1.6.3.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/mimalloc-1.6.3.tar.gz -------------------------------------------------------------------------------- /dependent/benchmark-1.5.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/benchmark-1.5.0.tar.gz -------------------------------------------------------------------------------- /dependent/http-parser-2.9.2.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/http-parser-2.9.2.tar.gz -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Frog 2 | 3 | lua c 事件驱动框架 4 | 5 | # 简介地址(以后还会继续完善白皮书) 6 | 7 | https://frog-game.github.io/posts/tech/kuangjia-work/ 8 | -------------------------------------------------------------------------------- /dependent/c-ares-cares-1_15_0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/c-ares-cares-1_15_0.tar.gz -------------------------------------------------------------------------------- /dependent/mysql-parser-1.0.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/mysql-parser-1.0.0.tar.gz -------------------------------------------------------------------------------- /dependent/redis-parser-1.0.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/redis-parser-1.0.0.tar.gz -------------------------------------------------------------------------------- /dependent/openssl-OpenSSL_1_1_1g.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/openssl-OpenSSL_1_1_1g.tar.gz -------------------------------------------------------------------------------- /dependent/googletest-release-1.8.1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/googletest-release-1.8.1.tar.gz -------------------------------------------------------------------------------- /dependent/multipart-parser-c-1.0.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/dependent/multipart-parser-c-1.0.0.tar.gz -------------------------------------------------------------------------------- /src/lua/example/start-up.lua: -------------------------------------------------------------------------------- 1 | local lenv = require "lruntime.env" 2 | 3 | lenv.init("data/lua/example/config.lua") 4 | 5 | lenv.wait() 6 | 7 | lenv.exit() 8 | -------------------------------------------------------------------------------- /tools/gitlab_cicd/format_check/linux/codeformat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frog-game/frog-game-framework/HEAD/tools/gitlab_cicd/format_check/linux/codeformat -------------------------------------------------------------------------------- /src/service/include/stream/mysqlStream_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "service_t.h" 6 | 7 | frService_API struct codecStream_s* mysqlStreamCreate(); 8 | -------------------------------------------------------------------------------- /src/service/include/stream/tpackStream_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "service_t.h" 6 | 7 | frService_API struct codecStream_s* tpackStreamCreate(); 8 | -------------------------------------------------------------------------------- /src/service/include/stream/lockStepPackStream.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | #include "service_t.h" 5 | 6 | frService_API struct codecStream_s* lockStepPackStreamCreate(); 7 | -------------------------------------------------------------------------------- /src/service/include/stream/lockStepPackStream_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | #include "service_t.h" 5 | 6 | frService_API struct codecStream_s* lockStepPackStreamCreate(); 7 | -------------------------------------------------------------------------------- /src/service/include/stream/webSocketStream_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "service_t.h" 6 | 7 | frService_API struct codecStream_s* webSocketStreamCreate(); 8 | -------------------------------------------------------------------------------- /src/runtime/include/internal/lenv-inl.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "utility_t.h" 6 | 7 | #include "eventIO/eventIO_t.h" 8 | 9 | __UNUSED eventIO_tt* getEnvEventIO(); -------------------------------------------------------------------------------- /src/runtime/include/lockStepPack/lockStepPackStream.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | extern "C" { 5 | #include "utility_t.h" 6 | 7 | __UNUSED struct codecStream_s* lockStepPackStreamCreate(); 8 | } 9 | -------------------------------------------------------------------------------- /src/runtime/include/internal/lservice-inl.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | 7 | #include "utility_t.h" 8 | 9 | __UNUSED uint32_t createLuaService(const char* szServiceName, const char* pParam, size_t nLength); 10 | -------------------------------------------------------------------------------- /src/core/include/eventIO/eventAsync_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | typedef struct eventAsync_s 4 | { 5 | // private 6 | void (*fnWork)(struct eventAsync_s*); 7 | void (*fnCancel)(struct eventAsync_s*); 8 | void* node[2]; 9 | } eventAsync_tt; 10 | -------------------------------------------------------------------------------- /tools/gitlab_cicd/format_check/win/clang-all-format.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set CURRENT_DIR=%cd% 4 | 5 | set FIND_DIR=%CURRENT_DIR%\..\..\..\..\src 6 | echo %FIND_DIR% 7 | for /r %FIND_DIR% %%i in (*.c,*cpp,*hpp,*h) do ( clang-format.exe -i %%i -style=file ) 8 | pause 9 | -------------------------------------------------------------------------------- /src/runtime/include/internal/lpackagePath_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "utility_t.h" 6 | 7 | struct lua_State; 8 | 9 | __UNUSED void setPackage_path(struct lua_State* L, const char* szPackagePath); 10 | 11 | __UNUSED void setPackage_cpath(struct lua_State* L, const char* szPackagePath); -------------------------------------------------------------------------------- /src/lua/service/monitorService.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local lenv = require "lruntime.env" 3 | 4 | serviceCore.start(function() 5 | serviceCore.eventDispatch(serviceCore.eventText, function(source) 6 | serviceCore.log(string.format("monitor service exception serviceId:%08x",source)) 7 | end) 8 | lenv.monitorStart(serviceCore.self(),5000) 9 | end) -------------------------------------------------------------------------------- /src/runtime/include/internal/lconnector_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | // type 6 | #include 7 | 8 | #include "utility_t.h" 9 | 10 | struct lua_State; 11 | struct connector_s; 12 | 13 | typedef struct lconnector_s 14 | { 15 | struct connector_s* pHandle; 16 | } lconnector_tt; 17 | 18 | __UNUSED int32_t registerConnectorL(struct lua_State* L); 19 | -------------------------------------------------------------------------------- /src/benchmark/source/spin_lock/clhLock.h: -------------------------------------------------------------------------------- 1 | #ifndef Frog_clhLock_h 2 | #define Frog_clhLock_h 3 | 4 | struct clhLock_s; 5 | typedef struct clhLock_s* clhLock_tt; 6 | 7 | void clhLock_init(clhLock_tt* self); 8 | 9 | void clhLock_destroy(clhLock_tt* self); 10 | 11 | void clhLock_lock(clhLock_tt* self); 12 | 13 | void clhLock_unlock(clhLock_tt* self); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /src/benchmark/source/spin_lock/mscLock.h: -------------------------------------------------------------------------------- 1 | #ifndef Frog_mscLock_h 2 | #define Frog_mscLock_h 3 | 4 | struct mscLock_s; 5 | typedef struct mscLock_s* mscLock_tt; 6 | 7 | void mscLock_init(mscLock_tt* self); 8 | 9 | void mscLock_destroy(mscLock_tt* self); 10 | 11 | void mscLock_lock(mscLock_tt* self); 12 | 13 | void mscLock_unlock(mscLock_tt* self); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /src/runtime/include/internal/ldnsResolve_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | // type 6 | #include 7 | 8 | #include "utility_t.h" 9 | 10 | struct lua_State; 11 | struct dnsResolve_s; 12 | 13 | typedef struct ldnsResolve_s 14 | { 15 | struct dnsResolve_s* pHandle; 16 | } ldnsResolve_tt; 17 | 18 | __UNUSED int32_t registerDnsResolveL(struct lua_State* L); 19 | -------------------------------------------------------------------------------- /src/runtime/include/internal/llistenPort_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | // type 6 | #include 7 | 8 | #include "utility_t.h" 9 | 10 | struct lua_State; 11 | struct listenPort_s; 12 | 13 | typedef struct llistenPort_s 14 | { 15 | struct listenPort_s* pHandle; 16 | } llistenPort_tt; 17 | 18 | __UNUSED int32_t registerListenPortL(struct lua_State* L); 19 | -------------------------------------------------------------------------------- /src/runtime/include/internal/ltimerWatcher_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | 7 | #include "utility_t.h" 8 | 9 | struct lua_State; 10 | struct timerWatcher_s; 11 | 12 | typedef struct ltimerWatcher_s 13 | { 14 | struct timerWatcher_s* pHandle; 15 | } ltimerWatcher_tt; 16 | 17 | __UNUSED int32_t registerTimerWatcherL(struct lua_State* L); 18 | -------------------------------------------------------------------------------- /src/service/include/internal/timerWatcher_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "eventIO/eventIO_t.h" 10 | 11 | struct timerWatcher_s 12 | { 13 | struct service_s* pService; 14 | uint32_t uiToken; 15 | eventTimer_tt* pEventTimer; 16 | atomic_int iRefCount; 17 | }; 18 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory( 2 | core 3 | ) 4 | 5 | add_subdirectory( 6 | service 7 | ) 8 | 9 | add_subdirectory( 10 | runtime 11 | ) 12 | 13 | # if(ENABLE_UNITTEST) 14 | # add_subdirectory( 15 | # unittest 16 | # ) 17 | # endif() 18 | if(ENABLE_BENCHMARK) 19 | add_subdirectory( 20 | benchmark 21 | ) 22 | endif() 23 | 24 | add_subdirectory( 25 | serverBin 26 | ) 27 | 28 | add_subdirectory( 29 | lua 30 | ) -------------------------------------------------------------------------------- /src/lua/example/config.lua: -------------------------------------------------------------------------------- 1 | local platform = ... 2 | 3 | C_node_id = 1 4 | 5 | C_concurrent_threads = -1 6 | 7 | C_log_path = "data" 8 | 9 | C_log_name = "_log" 10 | 11 | C_log = true 12 | 13 | C_profile = true 14 | 15 | C_loader_path = "data/lua/library/?.lua;data/lua/helper" 16 | 17 | C_service_path = "data/lua/service" 18 | 19 | C_bootstrap = "bootstrap" 20 | 21 | C_debug_ip = "127.0.0.1" 22 | 23 | C_debug_port = "9966" 24 | -------------------------------------------------------------------------------- /src/benchmark/source/spin_lock/spinLock.h: -------------------------------------------------------------------------------- 1 | #ifndef _spinLock_h 2 | #define _spinLock_h 3 | 4 | struct spinLock_s; 5 | typedef struct spinLock_s* spinLock_tt; 6 | 7 | void spinLock_init(spinLock_tt* lock); 8 | 9 | void spinLock_destroy(spinLock_tt* lock); 10 | 11 | void spinLock_lock(spinLock_tt* lock); 12 | 13 | void spinLock_lock2(spinLock_tt* lock); 14 | 15 | void spinLock_lock3(spinLock_tt* lock); 16 | 17 | void spinLock_unlock(spinLock_tt* lock); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /src/service/include/internal/connector_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "eventIO/eventIO_t.h" 10 | 11 | struct connector_s 12 | { 13 | struct service_s* pService; 14 | uint32_t uiToken; 15 | _Atomic(eventConnection_tt*) hConnection; 16 | _Atomic(eventTimer_tt*) hConnectingTimeout; 17 | atomic_int iRefCount; 18 | }; 19 | -------------------------------------------------------------------------------- /src/core/include/fs_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | 7 | #include "platform_t.h" 8 | 9 | frCore_API bool fs_mkdir(const char* szPath); 10 | 11 | frCore_API bool fs_find(const char* szFile); 12 | 13 | frCore_API bool fs_remove(const char* szFile); 14 | 15 | frCore_API bool fs_resetName(const char* szSrcFile, const char* szDstFile); 16 | 17 | frCore_API bool fs_removePath(const char* szPath); 18 | 19 | frCore_API bool fs_removeAll(const char* szPath); 20 | -------------------------------------------------------------------------------- /src/service/include/internal/listenPort_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "eventIO/eventIO_t.h" 10 | 11 | struct service_s; 12 | 13 | struct listenPort_s 14 | { 15 | void (*fn)(struct service_s*, eventConnection_tt*, void*); 16 | void (*fnFailCallback)(struct service_s*, void*); 17 | struct service_s* pService; 18 | eventListenPort_tt* pListenPortHandle; 19 | atomic_int iRefCount; 20 | }; 21 | -------------------------------------------------------------------------------- /src/runtime/include/internal/lloadCache_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | 7 | #include "utility_t.h" 8 | 9 | struct lua_State; 10 | 11 | __UNUSED void luaCache_init(); 12 | 13 | __UNUSED void luaCache_clear(); 14 | 15 | __UNUSED void luaCache_on(); 16 | 17 | __UNUSED void luaCache_off(); 18 | 19 | __UNUSED bool luaCache_abandon(const char* szFileName); 20 | 21 | __UNUSED int32_t loadfileCache(struct lua_State* L, const char* szFileName); 22 | 23 | __UNUSED int32_t loadCache(struct lua_State* L); 24 | -------------------------------------------------------------------------------- /tools/gitlab_cicd/format_check/linux/luacheck: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | LUAROCKS_SYSCONFDIR='/usr/local/etc/luarocks' exec '/usr/local/bin/lua' -e 'package.path="/home/hls/luarocks-3.8.0/./lua_modules/share/lua/5.4/?.lua;/home/hls/luarocks-3.8.0/./lua_modules/share/lua/5.4/?/init.lua;"..package.path;package.cpath="/home/hls/luarocks-3.8.0/./lua_modules/lib/lua/5.4/?.so;"..package.cpath;local k,l,_=pcall(require,"luarocks.loader") _=k and l.add_context("luacheck","0.26.1-1")' '/home/hls/luarocks-3.8.0/./lua_modules/lib/luarocks/rocks-5.4/luacheck/0.26.1-1/bin/luacheck' "$@" 4 | -------------------------------------------------------------------------------- /cmake/msvc/compiler_c_flags_overrides.cmake: -------------------------------------------------------------------------------- 1 | if(MSVC) 2 | # Use the static C library for all build types 3 | MESSAGE(STATUS "link to static C runtime lirbary(/MT /MTd)") 4 | foreach(var 5 | CMAKE_C_FLAGS_DEBUG_INIT 6 | CMAKE_C_FLAGS_RELEASE_INIT 7 | CMAKE_C_FLAGS_MINSIZEREL_INIT 8 | CMAKE_C_FLAGS_RELWITHDEBINFO_INIT 9 | ) 10 | if(${var} MATCHES "/MD") 11 | string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}") 12 | MESSAGE(STATUS "${var}:${${var}}") 13 | endif() 14 | endforeach() 15 | endif(MSVC) 16 | -------------------------------------------------------------------------------- /cmake/msvc/compiler_cxx_flags_overrides.cmake: -------------------------------------------------------------------------------- 1 | if(MSVC) 2 | # Use the static C++ library for all build types 3 | MESSAGE(STATUS "link to static C++ runtime lirbary(/MT /MTd)") 4 | foreach(var 5 | CMAKE_CXX_FLAGS_DEBUG_INIT 6 | CMAKE_CXX_FLAGS_RELEASE_INIT 7 | CMAKE_CXX_FLAGS_MINSIZEREL_INIT 8 | CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT 9 | ) 10 | if(${var} MATCHES "/MD") 11 | string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}") 12 | MESSAGE(STATUS "${var}:${${var}}") 13 | endif() 14 | endforeach() 15 | endif(MSVC) 16 | -------------------------------------------------------------------------------- /tools/gitlab_cicd/clean_cicd_all_pipeinle.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | YOUR_GITLAB_SERVER="http://10.17.17.21" 5 | TOKEN="W58Ay4_-zr2B7P614ePL" 6 | YOUR_PROJECT_ID=3 7 | # How many to delete from the oldest. 8 | PER_PAGE=100 9 | 10 | for PIPELINE in $(curl --header "PRIVATE-TOKEN: $TOKEN" "$YOUR_GITLAB_SERVER/api/v4/projects/$YOUR_PROJECT_ID/pipelines?per_page=$PER_PAGE&sort=asc" | jq '.[].id') ; do 11 | echo "Deleting pipeline $PIPELINE" 12 | curl --header "PRIVATE-TOKEN: $TOKEN" --request "DELETE" "$YOUR_GITLAB_SERVER/api/v4/projects/$YOUR_PROJECT_ID/pipelines/$PIPELINE" 13 | done 14 | -------------------------------------------------------------------------------- /src/service/include/channel/channelCenter_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | 8 | #include "service_t.h" 9 | 10 | struct channel_s; 11 | 12 | frService_API void channelCenter_init(); 13 | 14 | frService_API void channelCenter_clear(); 15 | 16 | frService_API uint32_t channelCenter_register(struct channel_s* pHandle); 17 | 18 | frService_API bool channelCenter_deregister(uint32_t uiChannelID); 19 | 20 | frService_API struct channel_s* channelCenter_gain(uint32_t uiChannelID); 21 | 22 | frService_API uint32_t* channelCenter_getIds(int32_t* pCount); 23 | -------------------------------------------------------------------------------- /src/lua/helper/http/url.lua: -------------------------------------------------------------------------------- 1 | local url = {} 2 | 3 | local function decode_func(c) 4 | return string.char(tonumber(c, 16)) 5 | end 6 | 7 | local function decode(str) 8 | local s = str:gsub('+', ' ') 9 | return s:gsub("%%(..)", decode_func) 10 | end 11 | 12 | function url.parse(u) 13 | local path,query = u:match "([^?]*)%??(.*)" 14 | if path then 15 | path = decode(path) 16 | end 17 | return path, query 18 | end 19 | 20 | function url.parse_query(q) 21 | local r = {} 22 | for k,v in q:gmatch "(.-)=([^&]*)&?" do 23 | r[decode(k)] = decode(v) 24 | end 25 | return r 26 | end 27 | 28 | return url 29 | -------------------------------------------------------------------------------- /src/runtime/include/msgpack/lmsgpack_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | 7 | #include "platform_t.h" 8 | 9 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 10 | # ifdef def_dllimport 11 | # define Frog_API __declspec(dllimport) 12 | # else 13 | # define Frog_API __declspec(dllexport) 14 | # endif 15 | #else 16 | # ifdef def_dllimport 17 | # define Frog_API extern 18 | # else 19 | # define Frog_API __attribute__((__visibility__("default"))) 20 | # endif 21 | #endif 22 | 23 | struct lua_State; 24 | 25 | Frog_API int32_t luaopen_lruntime_msgpack(struct lua_State* L); -------------------------------------------------------------------------------- /src/runtime/include/crypt/lcrypt_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_crypt(struct lua_State* L); -------------------------------------------------------------------------------- /src/runtime/include/env/lenv_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | // type 6 | #include 7 | 8 | #include "platform_t.h" 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_env(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/service/lservice_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | 7 | #include "platform_t.h" 8 | 9 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 10 | # ifdef def_dllimport 11 | # define Frog_API __declspec(dllimport) 12 | # else 13 | # define Frog_API __declspec(dllexport) 14 | # endif 15 | #else 16 | # ifdef def_dllimport 17 | # define Frog_API extern 18 | # else 19 | # define Frog_API __attribute__((__visibility__("default"))) 20 | # endif 21 | #endif 22 | 23 | struct lua_State; 24 | 25 | Frog_API int32_t luaopen_lruntime_service(struct lua_State* L); 26 | -------------------------------------------------------------------------------- /src/runtime/include/db/lmysql_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_mysql(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/db/lredis_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_redis(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/http/lhttp_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_http(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/lua/service/example/example_callBuf.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | serviceCore.start(function() 4 | serviceCore.setMsgCodec({ 5 | decode = function(msg, sz) 6 | return msg, sz 7 | end 8 | , 9 | encode = function(msg, sz) 10 | return msg, sz 11 | end 12 | }) 13 | 14 | serviceCore.eventDispatch(serviceCore.eventCall, function(_,msg, sz) 15 | local cmd = serviceCore.cbufferToString(msg, sz) 16 | serviceCore.log("example_callBuf: recv cmd:" .. cmd ); 17 | serviceCore.replyBuf("example_callBuf reply:".. cmd ) 18 | if cmd == "stop" then 19 | serviceCore.exit() 20 | end 21 | end) 22 | end) -------------------------------------------------------------------------------- /src/runtime/include/buffer/lbuffer_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_buffer(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/debug/ldebug_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_debug(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/sharetable/lsharetable_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | 7 | #include "platform_t.h" 8 | 9 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 10 | # ifdef def_dllimport 11 | # define Frog_API __declspec(dllimport) 12 | # else 13 | # define Frog_API __declspec(dllexport) 14 | # endif 15 | #else 16 | # ifdef def_dllimport 17 | # define Frog_API extern 18 | # else 19 | # define Frog_API __attribute__((__visibility__("default"))) 20 | # endif 21 | #endif 22 | 23 | struct lua_State; 24 | 25 | Frog_API int32_t luaopen_lruntime_sharetable(struct lua_State* L); 26 | -------------------------------------------------------------------------------- /src/runtime/include/stream/ltpack_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_tpack(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/lua/helper/db/redisCodec.lua: -------------------------------------------------------------------------------- 1 | local lredis = require "lruntime.redis" 2 | 3 | local redisCodec_t = {} 4 | 5 | local redis_meta = { __index = redisCodec_t } 6 | 7 | function redisCodec_t.new() 8 | local c = lredis.new() 9 | local self = { 10 | _obj = c 11 | } 12 | return setmetatable(self, redis_meta) 13 | end 14 | 15 | function redisCodec_t:write(msg,length) 16 | return lredis.write(self._obj,msg,length) 17 | end 18 | 19 | function redisCodec_t:read() 20 | return lredis.read(self._obj) 21 | end 22 | 23 | function redisCodec_t:reset() 24 | return lredis.reset(self._obj) 25 | end 26 | 27 | return redisCodec_t -------------------------------------------------------------------------------- /src/runtime/include/stream/lwebSocket_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_webSocket(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/channel/lchannelExt_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_channelExt(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/http/lhttpTransfer_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "platform_t.h" 6 | 7 | // type 8 | #include 9 | 10 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 11 | # ifdef def_dllimport 12 | # define Frog_API __declspec(dllimport) 13 | # else 14 | # define Frog_API __declspec(dllexport) 15 | # endif 16 | #else 17 | # ifdef def_dllimport 18 | # define Frog_API extern 19 | # else 20 | # define Frog_API __attribute__((__visibility__("default"))) 21 | # endif 22 | #endif 23 | 24 | struct lua_State; 25 | 26 | Frog_API int32_t luaopen_lruntime_httpTransfer(struct lua_State* L); 27 | -------------------------------------------------------------------------------- /src/runtime/include/lockstep/llockstep_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | extern "C" { 5 | 6 | #include "platform_t.h" 7 | 8 | // type 9 | #include 10 | 11 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 12 | # ifdef def_dllimport 13 | # define Frog_API __declspec(dllimport) 14 | # else 15 | # define Frog_API __declspec(dllexport) 16 | # endif 17 | #else 18 | # ifdef def_dllimport 19 | # define Frog_API extern 20 | # else 21 | # define Frog_API __attribute__((__visibility__("default"))) 22 | # endif 23 | #endif 24 | 25 | struct lua_State; 26 | 27 | Frog_API int32_t luaopen_lruntime_llockstep(struct lua_State* L); 28 | } 29 | -------------------------------------------------------------------------------- /src/lua/service/example/example_httpResponse.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local httpResponse = require "http.response" 3 | local logExt = require "logExt" 4 | 5 | local function test(request,response) 6 | logExt("example_httpResponse ",request) 7 | response(200,nil,"OK") 8 | end 9 | 10 | local function stop(request,response) 11 | logExt("example_httpResponse ",request) 12 | response(200,nil,"OK") 13 | serviceCore.timeout(1000,serviceCore.exit) 14 | end 15 | 16 | 17 | serviceCore.start(function() 18 | httpResponse.register("/testApi",test) 19 | httpResponse.register("/stop",stop) 20 | httpResponse.start({address = "127.0.0.1:80"}) 21 | end) -------------------------------------------------------------------------------- /src/lua/service/bootstrap.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | serviceCore.start(function() 4 | local log = serviceCore.createService("logService") 5 | serviceCore.bindServiceName(log, "_log") 6 | 7 | local monitor = serviceCore.createService("monitorService") 8 | serviceCore.bindServiceName(monitor, "_monitor") 9 | 10 | local localS = serviceCore.createService("localServices") 11 | serviceCore.bindServiceName(localS, "_localS") 12 | 13 | local console = serviceCore.createService("consoleService") 14 | serviceCore.call(console, "start", "127.0.0.1:23") 15 | 16 | serviceCore.launch("example/example") 17 | 18 | serviceCore.exit() 19 | end) 20 | -------------------------------------------------------------------------------- /src/lua/service/example/example_httpRequest.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local httpRequest = require "http.request" 3 | local logExt = require "logExt" 4 | 5 | serviceCore.start(function() 6 | local succ,response= pcall(httpRequest.get,"http://127.0.0.1", "/testApi", nil, nil, 10000) 7 | if succ then 8 | logExt("example_httpRequest ",response) 9 | else 10 | serviceCore.log("example_httpRequest get error:"..response) 11 | end 12 | 13 | succ,response= pcall(httpRequest.get,"http://127.0.0.1", "/stop", nil, nil, 10000) 14 | if succ then 15 | logExt("example_httpRequest ",response) 16 | else 17 | serviceCore.log("example_httpRequest get error:"..response) 18 | end 19 | end) -------------------------------------------------------------------------------- /src/runtime/include/lockStepPack/lockStep.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | extern "C" { 5 | 6 | #include "platform_t.h" 7 | 8 | // type 9 | #include 10 | 11 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 12 | # ifdef def_dllimport 13 | # define LOCKSTEPPACK_API __declspec(dllimport) 14 | # else 15 | # define LOCKSTEPPACK_API __declspec(dllexport) 16 | # endif 17 | #else 18 | # ifdef def_dllimport 19 | # define LOCKSTEPPACK_API extern 20 | # else 21 | # define LOCKSTEPPACK_API __attribute__((__visibility__("default"))) 22 | # endif 23 | #endif 24 | 25 | struct lua_State; 26 | 27 | LOCKSTEPPACK_API int32_t luaopen_lruntime_llockStep(struct lua_State* L); 28 | } 29 | -------------------------------------------------------------------------------- /src/runtime/include/lockStepPack/lockStep_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | extern "C" { 5 | 6 | #include "platform_t.h" 7 | 8 | // type 9 | #include 10 | 11 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 12 | # ifdef def_dllimport 13 | # define LOCKSTEPPACK_API __declspec(dllimport) 14 | # else 15 | # define LOCKSTEPPACK_API __declspec(dllexport) 16 | # endif 17 | #else 18 | # ifdef def_dllimport 19 | # define LOCKSTEPPACK_API extern 20 | # else 21 | # define LOCKSTEPPACK_API __attribute__((__visibility__("default"))) 22 | # endif 23 | #endif 24 | 25 | struct lua_State; 26 | 27 | LOCKSTEPPACK_API int32_t luaopen_lruntime_llockStep(struct lua_State* L); 28 | } 29 | -------------------------------------------------------------------------------- /src/lua/service/logService.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local logConsole = true 4 | 5 | local command = {} 6 | 7 | function command.consoleOn() 8 | logConsole = true 9 | end 10 | 11 | function command.consoleOff() 12 | logConsole = false 13 | end 14 | 15 | serviceCore.start(function() 16 | serviceCore.eventDispatch(serviceCore.eventText, function(source, msg) 17 | local s = string.format("%08x: %s", source, msg) 18 | serviceCore.localPrint(s, logConsole) 19 | end) 20 | 21 | serviceCore.eventDispatch(serviceCore.eventCommand, function(_, cmd) 22 | local f = command[cmd] 23 | if f then 24 | f() 25 | end 26 | end) 27 | end) 28 | -------------------------------------------------------------------------------- /src/lua/helper/http/httpCodec.lua: -------------------------------------------------------------------------------- 1 | local lhttp = require "lruntime.http" 2 | 3 | local httpCodec_t = {} 4 | 5 | local http_meta = { __index = httpCodec_t } 6 | 7 | function http_meta:__gc() 8 | lhttp.clear(self._obj) 9 | end 10 | 11 | function httpCodec_t.new(bResponse) 12 | local c = lhttp.new(bResponse) 13 | local self = { 14 | _obj = c 15 | } 16 | return setmetatable(self, http_meta) 17 | end 18 | 19 | function httpCodec_t.write(self,msg,length) 20 | return lhttp.write(self._obj,msg,length) 21 | end 22 | 23 | function httpCodec_t.read(self) 24 | return lhttp.read(self._obj) 25 | end 26 | 27 | function httpCodec_t.reset(self) 28 | return lhttp.reset(self._obj) 29 | end 30 | 31 | return httpCodec_t -------------------------------------------------------------------------------- /src/lua/helper/db/mysqlCodec.lua: -------------------------------------------------------------------------------- 1 | local lmysql = require "lruntime.mysql" 2 | 3 | local mysqlCodec_t = {} 4 | 5 | local mysql_meta = { __index = mysqlCodec_t } 6 | 7 | function mysqlCodec_t.new() 8 | local c = lmysql.new() 9 | local self = { 10 | _obj = c 11 | } 12 | return setmetatable(self, mysql_meta) 13 | end 14 | 15 | function mysqlCodec_t.write(self,msg,length) 16 | return lmysql.write(self._obj,msg,length) 17 | end 18 | 19 | function mysqlCodec_t.read(self,array) 20 | return lmysql.read(self._obj,array) 21 | end 22 | 23 | function mysqlCodec_t.reset(self,isHandshake) 24 | return lmysql.reset(self._obj,isHandshake) 25 | end 26 | 27 | mysqlCodec_t.codecHandle = lmysql.codecHandle 28 | 29 | return mysqlCodec_t -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/posix/eventWatcher_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "eventIO/internal/posix/poller_t.h" 7 | #include "eventIO/eventAsync_t.h" 8 | 9 | typedef struct eventWatcherAsync_s 10 | { 11 | eventAsync_tt eventAsync; 12 | struct eventWatcher_s* pEventWatcher; 13 | } eventWatcherAsync_tt; 14 | 15 | struct eventWatcher_s 16 | { 17 | void (*fn)(struct eventWatcher_s*, void*); 18 | void (*fnUserFree)(void*); 19 | struct eventIO_s* pEventIO; 20 | void* pUserData; 21 | bool bManualReset; 22 | atomic_int iStatus; 23 | atomic_int iRefCount; 24 | eventWatcherAsync_tt notifiedEventAsync; 25 | }; -------------------------------------------------------------------------------- /src/service/include/serviceCenter_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | 8 | #include "service_t.h" 9 | 10 | frService_API void serviceCenter_init(int32_t iServerNodeId); 11 | 12 | frService_API void serviceCenter_clear(); 13 | 14 | frService_API uint32_t serviceCenter_register(struct service_s* pHandle); 15 | 16 | frService_API bool serviceCenter_deregister(uint32_t uiServiceID); 17 | 18 | frService_API bool serviceCenter_bindName(uint32_t uiServiceID, const char* szName); 19 | 20 | frService_API bool serviceCenter_unbindName(uint32_t uiServiceID); 21 | 22 | frService_API uint32_t serviceCenter_findServiceID(const char* szName); 23 | 24 | frService_API service_tt* serviceCenter_gain(uint32_t uiServiceID); 25 | -------------------------------------------------------------------------------- /src/service/include/serviceMonitor_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | 8 | #include "service_t.h" 9 | 10 | struct eventIO_s; 11 | 12 | frService_API void serviceMonitor_init(uint32_t uiNumberOfConcurrentThreads); 13 | 14 | frService_API void serviceMonitor_clear(); 15 | 16 | frService_API bool serviceMonitor_start(uint32_t uiMonitorID, struct eventIO_s* pEventIO, 17 | uint32_t uiIntervalMs); 18 | 19 | frService_API void serviceMonitor_stop(); 20 | 21 | frService_API int32_t serviceMonitor_enter(uint32_t uiSourceID, uint32_t uiDestinationID); 22 | 23 | frService_API void serviceMonitor_leave(int32_t iIndex); 24 | 25 | frService_API int32_t serviceMonitor_waitForCount(); 26 | -------------------------------------------------------------------------------- /src/lua/service/example/example_dnsResolve.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | serviceCore.start(function() 4 | local t = serviceCore.dnsResolve("www.github.com",false) 5 | if t then 6 | for k,v in ipairs(t) do 7 | serviceCore.log("example_dnsResolve www.github.com:" .. v); 8 | end 9 | else 10 | serviceCore.log("example_dnsResolve www.github.com: error") 11 | end 12 | 13 | 14 | t = serviceCore.dnsResolve("example.com",false,true) 15 | if t then 16 | for k,v in ipairs(t) do 17 | serviceCore.log("example_dnsResolve example.com:" .. v); 18 | end 19 | else 20 | serviceCore.log("example_dnsResolve example.com: error") 21 | end 22 | serviceCore.exit() 23 | end) -------------------------------------------------------------------------------- /src/lua/service/example/example_sleep.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | serviceCore.start(function() 4 | serviceCore.log("example_sleep begin test sleep 1000"); 5 | local t = serviceCore.getClockMonotonic() 6 | serviceCore.sleep(1000) 7 | serviceCore.log(string.format("example_sleep sleep time %d", serviceCore.getClockMonotonic() - t)) 8 | 9 | local co = coroutine.running() 10 | 11 | serviceCore.async(function() 12 | serviceCore.log("example_sleep wakeup start"); 13 | serviceCore.wakeup(co) 14 | serviceCore.log("example_sleep wakeup end"); 15 | end) 16 | 17 | serviceCore.log("example_sleep wait start"); 18 | serviceCore.wait(co); 19 | serviceCore.log("example_sleep wait end"); 20 | serviceCore.log("example_sleep end: test sleep"); 21 | 22 | end) -------------------------------------------------------------------------------- /src/service/include/stream/codecStream_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "eventIO/eventIO_t.h" 10 | #include "service_t.h" 11 | 12 | struct channel_s; 13 | 14 | typedef struct codecStream_s 15 | { 16 | int32_t (*fnWrite)(struct codecStream_s*, eventConnection_tt*, const char*, int32_t, uint32_t, 17 | uint32_t); 18 | int32_t (*fnWriteMove)(struct codecStream_s*, eventConnection_tt*, ioBufVec_tt*, int32_t, 19 | uint32_t, uint32_t); 20 | bool (*fnReceive)(struct codecStream_s*, struct channel_s*, byteQueue_tt*); 21 | void (*fnAddref)(struct codecStream_s*); 22 | void (*fnRelease)(struct codecStream_s*); 23 | } codecStream_tt; 24 | -------------------------------------------------------------------------------- /src/lua/service/example/example_exit.lua: -------------------------------------------------------------------------------- 1 | local launchParam = ... 2 | local serviceCore = require "serviceCore" 3 | 4 | local command = {} 5 | 6 | function command.stop() 7 | serviceCore.async(serviceCore.exit) 8 | serviceCore.log("example_exit exit") 9 | end 10 | 11 | serviceCore.start(function() 12 | if launchParam == "exit" then 13 | serviceCore.log("example_exit exit") 14 | serviceCore.exit() 15 | else 16 | serviceCore.eventDispatch(serviceCore.eventCommand, function(_,cmd,...) 17 | serviceCore.log("example_exit recv cmd:" .. cmd ); 18 | local f = command[cmd] 19 | if f then 20 | serviceCore.reply(f(...)) 21 | else 22 | error(string.format("example_exit unknown cmd: %s", tostring(cmd))) 23 | end 24 | end) 25 | end 26 | end) -------------------------------------------------------------------------------- /src/runtime/source/internal/lpackagePath_t.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "internal/lpackagePath_t.h" 4 | 5 | #include "lauxlib.h" 6 | #include "lua.h" 7 | #include "lualib.h" 8 | 9 | void setPackage_path(lua_State* L, const char* szPackagePath) 10 | { 11 | lua_getglobal(L, "package"); 12 | lua_getfield(L, -1, "path"); 13 | const char* cur_path = lua_tostring(L, -1); 14 | lua_pop(L, 1); 15 | lua_pushfstring(L, "%s;%s/?.lua", cur_path, szPackagePath); 16 | lua_setfield(L, -2, "path"); 17 | lua_pop(L, 1); 18 | } 19 | 20 | void setPackage_cpath(lua_State* L, const char* szPackagePath) 21 | { 22 | lua_getglobal(L, "package"); 23 | lua_getfield(L, -1, "cpath"); 24 | const char* cur_path = lua_tostring(L, -1); 25 | lua_pop(L, 1); 26 | lua_pushfstring(L, "%s;%s", cur_path, szPackagePath); 27 | lua_setfield(L, -2, "cpath"); 28 | lua_pop(L, 1); 29 | } -------------------------------------------------------------------------------- /src/lua/service/example/example_call.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local dataDB = {} 4 | 5 | local command = {} 6 | 7 | function command.get(key) 8 | return dataDB[key] 9 | end 10 | 11 | function command.set(key, value) 12 | local last = dataDB[key] 13 | dataDB[key] = value 14 | return last 15 | end 16 | 17 | function command.add(value1,value2) 18 | return value1+value2 19 | end 20 | 21 | function command.stop() 22 | serviceCore.async(serviceCore.exit) 23 | end 24 | 25 | serviceCore.start(function() 26 | serviceCore.eventDispatch(serviceCore.eventCall, function(_,cmd,...) 27 | serviceCore.log("example_call recv call cmd:" .. cmd ); 28 | local f = command[cmd] 29 | if f then 30 | serviceCore.reply(f(...)) 31 | else 32 | error(string.format("example_call unknown cmd: %s", tostring(cmd))) 33 | end 34 | end) 35 | end) -------------------------------------------------------------------------------- /src/lua/service/example/example_timer.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | serviceCore.start(function() 4 | serviceCore.log("example_timer test timer 10 500"); 5 | local count = 1 6 | local t1 = serviceCore.getClockMonotonic() 7 | local fnOnTriggered = function () 8 | local t2 = serviceCore.getClockMonotonic() 9 | serviceCore.log(string.format("example_timer runEvery: %d, time: %d", count, t2 - t1)) 10 | t1 = t2; 11 | count = count + 1 12 | if count > 10 then 13 | serviceCore.async(serviceCore.exit) 14 | return false; 15 | else 16 | return true; 17 | end 18 | end 19 | serviceCore.runEvery(500,fnOnTriggered); 20 | local t3 = serviceCore.getClockMonotonic() 21 | serviceCore.timeout(1000,function () 22 | local t4 = serviceCore.getClockMonotonic() 23 | serviceCore.log(string.format("example_timer timeout time: %d", t4 - t3)) 24 | end) 25 | 26 | end) 27 | -------------------------------------------------------------------------------- /src/lua/service/example/example_command.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local dataDB = {} 4 | 5 | local command = {} 6 | 7 | function command.get(key) 8 | return dataDB[key] 9 | end 10 | 11 | function command.set(key, value) 12 | local last = dataDB[key] 13 | dataDB[key] = value 14 | return last 15 | end 16 | 17 | function command.add(value1,value2) 18 | return value1+value2 19 | end 20 | 21 | function command.stop() 22 | serviceCore.async(serviceCore.exit) 23 | end 24 | 25 | serviceCore.start(function() 26 | serviceCore.eventDispatch(serviceCore.eventCommand, function(_,cmd,...) 27 | serviceCore.log("example_command recv call cmd:" .. cmd ); 28 | local f = command[cmd] 29 | if f then 30 | serviceCore.reply(f(...)) 31 | else 32 | error(string.format("example_command unknown cmd: %s", tostring(cmd))) 33 | end 34 | end) 35 | end) -------------------------------------------------------------------------------- /src/service/include/serviceEvent_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define DEF_EVENT_SERVICE_STOP 0 10 | #define DEF_EVENT_BINARY 1 11 | #define DEF_EVENT_DISCONNECT 2 12 | #define DEF_EVENT_SEND_OK 3 13 | #define DEF_EVENT_ACCEPT 4 14 | #define DEF_EVENT_CONNECT 5 15 | #define DEF_EVENT_DNS 6 16 | #define DEF_EVENT_YIELD 7 17 | #define DEF_EVENT_RUN_AFTER 8 18 | #define DEF_EVENT_RUN_EVERY 9 19 | #define DEF_EVENT_MSG 10 20 | #define DEF_EVENT_COMMAND 11 21 | #define DEF_EVENT_MASK 0x0F 22 | 23 | #define DEF_EVENT_MOVEBUF 0x80 24 | 25 | #define DEF_EVENT_MSG_REPLY 0x70 26 | #define DEF_EVENT_MSG_CALL 0x60 27 | #define DEF_EVENT_MSG_PING 0x50 28 | #define DEF_EVENT_MSG_PONG 0x40 29 | #define DEF_EVENT_MSG_CLOSE 0x30 30 | #define DEF_EVENT_MSG_SEND 0x20 31 | #define DEF_EVENT_MSG_TEXT 0x10 32 | #define DEF_EVENT_MSG_MASK 0x70 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | #general 55 | .DS_Store 56 | 57 | #vscode 58 | .vscode/ 59 | !.vscode/settings.json 60 | !.vscode/tasks.json 61 | !.vscode/launch.json 62 | !.vscode/extensions.json 63 | *.code-workspace 64 | 65 | .history/ 66 | 67 | build/Windows 68 | -------------------------------------------------------------------------------- /src/lua/helper/queueLock.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local coroutine = coroutine 4 | local xpcall = xpcall 5 | local traceback = debug.traceback 6 | local table = table 7 | 8 | local function queueLock() 9 | local running_co 10 | local ref = 0 11 | local queue = {} 12 | 13 | local function xpcall_ret_f(ok, ...) 14 | ref = ref - 1 15 | if ref == 0 then 16 | running_co = table.remove(queue,1) 17 | if running_co then 18 | serviceCore.wakeup(running_co) 19 | end 20 | end 21 | assert(ok, (...)) 22 | return ... 23 | end 24 | 25 | return function(f, ...) 26 | local co = coroutine.running() 27 | if running_co and running_co ~= co then 28 | table.insert(queue, co) 29 | serviceCore.wait() 30 | assert(ref == 0) 31 | end 32 | running_co = co 33 | 34 | ref = ref + 1 35 | return xpcall_ret_f(xpcall(f, traceback, ...)) 36 | end 37 | end 38 | 39 | return queueLock 40 | -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/win/eventWatcher_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "eventIO/eventAsync_t.h" 12 | 13 | struct eventWatcher_s; 14 | 15 | typedef struct eventWatcher_overlappedPlus_s 16 | { 17 | void (*fn)(struct eventWatcher_s*); 18 | OVERLAPPED _Overlapped; 19 | struct eventWatcher_s* pEventWatcher; 20 | } eventWatcher_overlappedPlus_tt; 21 | 22 | struct eventWatcher_s 23 | { 24 | void (*fn)(struct eventWatcher_s*, void*); 25 | void (*fnUserFree)(void*); 26 | struct eventIO_s* pEventIO; 27 | void* pUserData; 28 | bool bManualReset; 29 | atomic_int iStatus; 30 | atomic_int iRefCount; 31 | eventWatcher_overlappedPlus_tt notifiedEvent; 32 | }; 33 | -------------------------------------------------------------------------------- /src/lua/helper/http/httpTransferCodec.lua: -------------------------------------------------------------------------------- 1 | local lhttpTransfer = require "lruntime.httpTransfer" 2 | 3 | local httpTransferCodec_t = {} 4 | 5 | local http_meta = { __index = httpTransferCodec_t } 6 | 7 | function http_meta:__gc() 8 | lhttpTransfer.clear(self._obj) 9 | end 10 | 11 | function httpTransferCodec_t.new(bResponse) 12 | local c = lhttpTransfer.new(bResponse) 13 | local self = { 14 | _obj = c 15 | } 16 | return setmetatable(self, http_meta) 17 | end 18 | 19 | function httpTransferCodec_t.write(self,msg,length) 20 | return lhttpTransfer.write(self._obj,msg,length) 21 | end 22 | 23 | function httpTransferCodec_t.readHeader(self) 24 | return lhttpTransfer.readHeader(self._obj) 25 | end 26 | 27 | function httpTransferCodec_t.readBody(self) 28 | return lhttpTransfer.readBody(self._obj) 29 | end 30 | 31 | function httpTransferCodec_t.reset(self) 32 | return lhttpTransfer.reset(self._obj) 33 | end 34 | 35 | return httpTransferCodec_t -------------------------------------------------------------------------------- /src/runtime/include/internal/lconfig_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | // type 6 | #include 7 | #include 8 | #include 9 | 10 | #include "utility_t.h" 11 | 12 | struct lua_State; 13 | 14 | __UNUSED bool luaConfig_init(struct lua_State* L); 15 | 16 | __UNUSED void luaConfig_clear(); 17 | 18 | __UNUSED const char* luaConfig_getLoaderPath(); 19 | 20 | __UNUSED const char* luaConfig_getServicePath(); 21 | 22 | __UNUSED const char* luaConfig_getLogPath(); 23 | 24 | __UNUSED const char* luaConfig_getBootstrap(); 25 | 26 | __UNUSED const char* luaConfig_getBootstrapParam(); 27 | 28 | __UNUSED const char* luaConfig_getLogService(); 29 | 30 | __UNUSED int32_t luaConfig_getServerNodeID(); 31 | 32 | __UNUSED int32_t luaConfig_getConcurrentThreads(); 33 | 34 | __UNUSED bool luaConfig_isLog(); 35 | 36 | __UNUSED bool luaConfig_isProfile(); 37 | 38 | __UNUSED const char* luaConfig_getDebug_ip(); 39 | 40 | __UNUSED const char* luaConfig_getDebug_port(); 41 | -------------------------------------------------------------------------------- /src/lua/service/example/example_queueLock.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local queueLock = require "queueLock" 3 | 4 | local dataDB = {} 5 | local command = {} 6 | local lock 7 | 8 | function command.get(key) 9 | return lock( function() 10 | return dataDB[key] 11 | end) 12 | end 13 | 14 | function command.set(key, value) 15 | return lock( function() 16 | local last = dataDB[key] 17 | dataDB[key] = value 18 | serviceCore.sleep(1000) 19 | return last 20 | end) 21 | end 22 | 23 | function command.stop() 24 | serviceCore.async(serviceCore.exit) 25 | end 26 | 27 | 28 | serviceCore.start(function() 29 | lock = queueLock() 30 | serviceCore.eventDispatch(serviceCore.eventCommand, function(_,cmd,...) 31 | serviceCore.log("example_queueLock: recv cmd:" .. cmd ); 32 | local f = command[cmd] 33 | if f then 34 | serviceCore.replyCommand(f(...)) 35 | else 36 | error(string.format("unknown command %s", tostring(cmd))) 37 | end 38 | end) 39 | end) -------------------------------------------------------------------------------- /src/service/include/internal/dnsResolve_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define CARES_STATICLIB 1 10 | #include "ares.h" 11 | 12 | #include "eventIO/eventIO_t.h" 13 | 14 | struct dnsResolve_s 15 | { 16 | struct service_s* pService; 17 | ares_channel aresCtx; 18 | uint32_t uiToken; 19 | uint32_t uiTimeoutMs; 20 | char* szAddressList; 21 | int32_t iAddressCount; 22 | const char* pRecvBuffer; 23 | size_t nRecvLength; 24 | size_t nRecvOffset; 25 | inetAddress_tt recvAddress; 26 | ioBufVec_tt sendBufV[8]; 27 | int32_t iBufVCount; 28 | _Atomic(eventConnection_tt*) hConnection; 29 | _Atomic(eventTimer_tt*) hTimeout; 30 | atomic_bool bActive; 31 | atomic_int iRefCount; 32 | }; 33 | -------------------------------------------------------------------------------- /src/core/include/spinLock_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "thread_t.h" 6 | 7 | typedef struct spinLock_s 8 | { 9 | atomic_flag flag; 10 | } spinLock_tt; 11 | 12 | static inline void spinLock_init(spinLock_tt* self) 13 | { 14 | atomic_flag_clear(&self->flag); 15 | } 16 | 17 | static inline void spinLock_lock(spinLock_tt* self) 18 | { 19 | uint32_t uiSpinCount = 0; 20 | if (atomic_flag_test_and_set_explicit(&self->flag, memory_order_acquire)) { 21 | do { 22 | if (uiSpinCount++ < 2048) { 23 | thread_pause(); 24 | } 25 | else { 26 | threadYield(); 27 | } 28 | } while (atomic_flag_test_and_set_explicit(&self->flag, memory_order_relaxed)); 29 | } 30 | } 31 | 32 | static inline void spinLock_unlock(spinLock_tt* self) 33 | { 34 | atomic_flag_clear_explicit(&self->flag, memory_order_release); 35 | } 36 | 37 | static inline bool spinLock_trylock(spinLock_tt* self) 38 | { 39 | return !atomic_flag_test_and_set_explicit(&self->flag, memory_order_acquire); 40 | } -------------------------------------------------------------------------------- /src/lua/service/debuggerService.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local command = {} 4 | local debugAddress = nil 5 | local channelID = nil 6 | 7 | function command.start(address,id) 8 | debugAddress = address 9 | channelID = id 10 | serviceCore.command(debugAddress,"_debug") 11 | return true; 12 | end 13 | 14 | function command.stop() 15 | channelID = nil 16 | serviceCore.command(debugAddress,"leave") 17 | serviceCore.async(serviceCore.exit) 18 | return true; 19 | end 20 | 21 | serviceCore.start(function() 22 | serviceCore.eventDispatch(serviceCore.eventCall, function(source,cmd,...) 23 | local func = assert(command[cmd]) 24 | serviceCore.reply(func(...)) 25 | end) 26 | 27 | serviceCore.eventDispatch(serviceCore.eventText, function(source,cmdline) 28 | serviceCore.command(debugAddress,cmdline) 29 | end) 30 | 31 | serviceCore.eventDispatch(serviceCore.eventCommand, function(source,cmd,s) 32 | if channelID and cmd == "_debugging" then 33 | serviceCore.remoteWrite(channelID, s) 34 | end 35 | end) 36 | end) -------------------------------------------------------------------------------- /src/core/include/cxx-mem/new_delete_override_t.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef __cplusplus 4 | # include 5 | 6 | extern "C" { 7 | # include 8 | } 9 | 10 | void operator delete(void* p) noexcept 11 | { 12 | mem_free(p); 13 | }; 14 | void operator delete[](void* p) noexcept 15 | { 16 | mem_free(p); 17 | }; 18 | 19 | void* operator new(std::size_t n) noexcept(false) 20 | { 21 | return mem_malloc(n); 22 | } 23 | void* operator new[](std::size_t n) noexcept(false) 24 | { 25 | return mem_malloc(n); 26 | } 27 | 28 | void* operator new(std::size_t n, const std::nothrow_t& tag) noexcept 29 | { 30 | (void)(tag); 31 | return mem_malloc(n); 32 | } 33 | void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept 34 | { 35 | (void)(tag); 36 | return mem_malloc(n); 37 | } 38 | 39 | # if (__cplusplus >= 201402L || _MSC_VER >= 1916) 40 | void operator delete(void* p, std::size_t n) noexcept 41 | { 42 | mem_free(p); 43 | }; 44 | void operator delete[](void* p, std::size_t n) noexcept 45 | { 46 | mem_free(p); 47 | }; 48 | # endif 49 | 50 | #endif // __cplusplus 51 | -------------------------------------------------------------------------------- /src/benchmark/source/spin_lock/rwSpinLock.h: -------------------------------------------------------------------------------- 1 | #ifndef _rwSpinLock_h 2 | #define _rwSpinLock_h 3 | 4 | #include 5 | 6 | struct rwSpinLock_s; 7 | typedef struct rwSpinLock_s* rwSpinLock_tt; 8 | 9 | void rwSpinLock_init(rwSpinLock_tt* self); 10 | 11 | void rwSpinLock_destroy(rwSpinLock_tt* self); 12 | 13 | void rwSpinLock_rdlock(rwSpinLock_tt* self); 14 | 15 | bool rwSpinLock_tryrdlock(rwSpinLock_tt* self); 16 | 17 | void rwSpinLock_rdunlock(rwSpinLock_tt* self); 18 | 19 | void rwSpinLock_wrlock(rwSpinLock_tt* self); 20 | 21 | bool rwSpinLock_trywrlock(rwSpinLock_tt* self); 22 | 23 | void rwSpinLock_wrunlock(rwSpinLock_tt* self); 24 | 25 | void rwSpinLock_wrunlock_rdlock(rwSpinLock_tt* self); 26 | 27 | void rwSpinLock_upLock(rwSpinLock_tt* self); 28 | 29 | void rwSpinLock_upunLock(rwSpinLock_tt* self); 30 | 31 | bool rwSpinLock_tryuplock(rwSpinLock_tt* self); 32 | 33 | void rwSpinLock_upunlock_wrlock(rwSpinLock_tt* self); 34 | 35 | void rwSpinLock_upunlock_rdlock(rwSpinLock_tt* self); 36 | 37 | void rwSpinLock_wrunlock_uplock(rwSpinLock_tt* self); 38 | 39 | bool rwSpinLock_tryupunlock_wrlock(rwSpinLock_tt* self); 40 | 41 | #endif -------------------------------------------------------------------------------- /cmake/modules/install_3rdparty.cmake: -------------------------------------------------------------------------------- 1 | set(3RDPARTY_BUILD_COMMAND -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} ) 2 | 3 | if(ANDROID) 4 | set(3RDPARTY_BUILD_COMMAND ${3RDPARTY_BUILD_COMMAND} 5 | -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL} 6 | -DANDROID_ABI=${ANDROID_ABI} 7 | -DANDROID_NDK=${ANDROID_NDK}) 8 | endif() 9 | 10 | if(IOS) 11 | set(3RDPARTY_BUILD_COMMAND ${3RDPARTY_BUILD_COMMAND} 12 | -DIOS_PLATFORM=${IOS_PLATFORM} 13 | ) 14 | endif() 15 | 16 | 17 | include(3rdparty/build_lua) 18 | include(3rdparty/build_cares) 19 | 20 | include(3rdparty/build_http_parser) 21 | include(3rdparty/build_multipart_parser) 22 | include(3rdparty/build_mysql_parser) 23 | include(3rdparty/build_redis_parser) 24 | 25 | if(BUILD_OPENSSL) 26 | include(3rdparty/build_openssl) 27 | endif() 28 | 29 | if(BUILD_MIMALLOC) 30 | include(3rdparty/build_mimalloc) 31 | endif() 32 | 33 | if(BUILD_LPEG) 34 | include(3rdparty/build_lpeg) 35 | endif() 36 | 37 | if(ENABLE_UNITTEST) 38 | include(3rdparty/build_googletest) 39 | endif() 40 | 41 | if(ENABLE_BENCHMARK) 42 | include(3rdparty/build_benchmark) 43 | endif() 44 | -------------------------------------------------------------------------------- /src/serverBin/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SERVER_BIN "serverBin") 2 | 3 | set(INSTALL_SERVER_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables") 4 | 5 | set(SERVER_BIN_SOURCE_FILES 6 | ${CMAKE_CURRENT_SOURCE_DIR}/source/main.c 7 | ) 8 | 9 | include_directories( 10 | ${FROG_3RDPARTY_BINARY_DIR}/install/lua/include 11 | ${CMAKE_CURRENT_SOURCE_DIR}/include 12 | ) 13 | 14 | link_directories(${FROG_3RDPARTY_BINARY_DIR}/install/lua/lib) 15 | 16 | add_executable(${SERVER_BIN} 17 | ${SERVER_BIN_SOURCE_FILES} 18 | ) 19 | 20 | FUNCTION_COMPILE_DEFINE(${SERVER_BIN}) 21 | FUNCTION_COMPILE_OPTION(${SERVER_BIN}) 22 | 23 | set_target_properties(${SERVER_BIN} PROPERTIES LINKER_LANGUAGE C) 24 | set_target_properties(${SERVER_BIN} PROPERTIES C_STANDARD 11) 25 | 26 | if(WINDOWS) 27 | target_link_libraries(${SERVER_BIN} PRIVATE lua Ws2_32.lib 28 | ) 29 | else() 30 | target_link_libraries(${SERVER_BIN} PRIVATE lua ${CMAKE_THREAD_LIBS_INIT} dl m 31 | ) 32 | endif() 33 | 34 | set_target_properties(${SERVER_BIN} PROPERTIES INSTALL_RPATH "${INSTALL_SERVER_BIN_DIR}") 35 | 36 | install(TARGETS ${SERVER_BIN} DESTINATION "${INSTALL_SERVER_BIN_DIR}") 37 | -------------------------------------------------------------------------------- /src/lua/helper/channelPool.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local _M = {} 4 | 5 | local channels = setmetatable({},{ 6 | __gc = function(pool) 7 | for _,v in pairs(pool) do 8 | if v.disconnect then 9 | v.disconnect() 10 | end 11 | end 12 | end 13 | }) 14 | 15 | serviceCore.eventDispatch(serviceCore.eventBinary, function(source,msg,length) 16 | local t = channels[source] 17 | if t and t.write then 18 | t.write(msg,length) 19 | end 20 | end) 21 | 22 | serviceCore.eventDispatch(serviceCore.eventDisconnect, function(source) 23 | local t = channels[source] 24 | if t and t.disconnect then 25 | t.disconnect() 26 | channels[source] = nil 27 | end 28 | end) 29 | 30 | function _M.attach(address,fnDisconnect,fnWrite) 31 | local t = {} 32 | t.disconnect = fnDisconnect 33 | t.write = fnWrite 34 | channels[address] = t 35 | end 36 | 37 | function _M.detach(address) 38 | channels[address] = nil 39 | end 40 | 41 | function _M.close() 42 | for _,v in pairs(channels) do 43 | if v.disconnect then 44 | v.disconnect() 45 | end 46 | end 47 | end 48 | 49 | return _M 50 | 51 | -------------------------------------------------------------------------------- /src/lua/service/example/example_mysql.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local mysql = require "db.mysql" 3 | local logExt = require "logExt" 4 | 5 | serviceCore.start(function() 6 | local db = mysql.connect({address = "127.0.0.1:3306",user = "root",password = "123456",db = "blueprint"}) 7 | local r = db:query("show databases") 8 | logExt("example_mysql ",r) 9 | 10 | local r2 = db:query("show databases",true) 11 | logExt("example_mysql ",r2) 12 | 13 | local r3 = db:query("insert into test(name,level) values('test3',19)") 14 | logExt("example_mysql ",r3) 15 | local r4 = db:query("select * from test") 16 | logExt("example_mysql ",r4) 17 | 18 | local r5,r6 = db:query("select * from test;insert into test(name,level) values('test2077',19)") 19 | logExt("example_mysql ",r5,r6) 20 | 21 | local r7 = db:query("select * from test") 22 | logExt("example_mysql ",r7) 23 | 24 | local r8 = db:query("prepare testStmt from 'insert into test(name,level) values(?,?)'") 25 | logExt("example_mysql ",r8) 26 | local r9,r10 = db:query( string.format("set @a='%s',@b=%d;execute testStmt using @a,@b","boboTestStmt",9898) ) 27 | logExt("example_mysql ",r9,r10) 28 | 29 | serviceCore.exit() 30 | end) -------------------------------------------------------------------------------- /src/runtime/source/internal/llistenPort_t.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "internal/llistenPort_t.h" 4 | 5 | #include 6 | #include 7 | 8 | #include "lauxlib.h" 9 | #include "lua.h" 10 | #include "lualib.h" 11 | 12 | #include "service_t.h" 13 | 14 | static int32_t llistenPort_gc(lua_State* L) 15 | { 16 | llistenPort_tt* pListenPort = (llistenPort_tt*)luaL_checkudata(L, 1, "listenPort"); 17 | luaL_argcheck(L, pListenPort != NULL, 1, "invalid user data"); 18 | if (pListenPort->pHandle) { 19 | listenPort_close(pListenPort->pHandle); 20 | listenPort_release(pListenPort->pHandle); 21 | pListenPort->pHandle = NULL; 22 | } 23 | return 0; 24 | } 25 | 26 | int32_t registerListenPortL(lua_State* L) 27 | { 28 | luaL_newmetatable(L, "listenPort"); 29 | /* metatable.__index = metatable */ 30 | lua_pushvalue(L, -1); 31 | lua_setfield(L, -2, "__index"); 32 | 33 | struct luaL_Reg lua_listenPortFuncs[] = {{"close", llistenPort_gc}, 34 | {"__close", llistenPort_gc}, 35 | {"__gc", llistenPort_gc}, 36 | {NULL, NULL}}; 37 | 38 | luaL_setfuncs(L, lua_listenPortFuncs, 0); 39 | return 1; 40 | }; 41 | -------------------------------------------------------------------------------- /src/core/include/eventIO/eventIOThread_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include "eventIO/eventIO_t.h" 9 | 10 | struct eventIOThread_s; 11 | 12 | typedef struct eventIOThread_s eventIOThread_tt; 13 | 14 | frCore_API eventIOThread_tt* createEventIOThread(eventIO_tt* pEventIO); 15 | 16 | frCore_API void eventIOThread_addref(eventIOThread_tt* pEventIOThread); 17 | 18 | frCore_API void eventIOThread_release(eventIOThread_tt* pEventIOThread); 19 | 20 | frCore_API void eventIOThread_start(eventIOThread_tt* pEventIOThread, bool bWaitThreadStarted, 21 | bool (*fnPre)(eventIOThread_tt*), 22 | void (*fnPost)(eventIOThread_tt*)); 23 | 24 | frCore_API void eventIOThread_stop(eventIOThread_tt* pEventIOThread, bool bWaitThreadStarted); 25 | 26 | frCore_API void eventIOThread_join(eventIOThread_tt* pEventIOThread); 27 | 28 | frCore_API bool eventIOThread_isRunning(eventIOThread_tt* pEventIOThread); 29 | 30 | frCore_API bool eventIOThread_isStopped(eventIOThread_tt* pEventIOThread); 31 | 32 | frCore_API bool eventIOThread_isStopping(eventIOThread_tt* pEventIOThread); 33 | 34 | frCore_API eventIO_tt* eventIOThread_getEventIO(eventIOThread_tt* pEventIOThread); 35 | -------------------------------------------------------------------------------- /src/core/include/lockStep/lockStepPack_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "byteQueue_t.h" 4 | #include "cbuf_t.h" 5 | #include "utility_t.h" 6 | 7 | 8 | typedef struct lockStepPackHead_s 9 | { 10 | uint8_t packLength; 11 | uint16_t frameId; 12 | } lockStepPackHead_tt; 13 | 14 | frCore_API int32_t lockStepPack_decodeHead(byteQueue_tt* pInBytes, lockStepPackHead_tt* pOutHead); 15 | 16 | frCore_API int32_t lockStepPack_decode(byteQueue_tt* pInBytes, cbuf_tt* pOutBuf, uint8_t* pOutFlag, 17 | uint32_t* pOutToken); 18 | 19 | frCore_API int32_t lockStepPack_encode(const char* pBuffer, size_t nLength, 20 | ioBufVec_tt* pOutBufVec); 21 | 22 | frCore_API int32_t lockStepPack_encodeVec(ioBufVec_tt* pInBufVec, int32_t iCount, 23 | ioBufVec_tt* pOutBufVec); 24 | 25 | static inline int32_t lockStepPack_encodeBufCount(const char* pBuffer, size_t nLength) 26 | { 27 | return (int32_t)((nLength + 0xfffe) / 0xffff); 28 | } 29 | 30 | static inline int32_t lockStepPack_encodeVecBufCount(ioBufVec_tt* pInBufVec, int32_t iCount) 31 | { 32 | size_t nLength = 0; 33 | for (int32_t i = 0; i < iCount; i++) { 34 | nLength += pInBufVec[i].iLength; 35 | } 36 | return (int32_t)((nLength + 0xfffe) / 0xffff); 37 | } 38 | -------------------------------------------------------------------------------- /src/core/include/lockStep/lockStepPack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "utility_t.h" 4 | #include "cbuf_t.h" 5 | #include "byteQueue_t.h" 6 | 7 | typedef struct lockStepPackHead_s 8 | { 9 | uint8_t packLength; 10 | uint16_t frameId; 11 | } lockStepPackHead_tt; 12 | 13 | __UNUSED int32_t lockStepPack_decodeHead(byteQueue_tt* pInBytes, lockStepPackHead_tt* pOutHead); 14 | 15 | __UNUSED int32_t lockStepPack_decode(byteQueue_tt* pInBytes, cbuf_tt* pOutBuf, uint8_t* pOutFlag, 16 | uint32_t* pOutToken); 17 | 18 | __UNUSED int32_t lockStepPack_encode(const char* pBuffer, size_t nLength, uint8_t uiFlag, 19 | uint32_t uiToken, ioBufVec_tt* pOutBufVec); 20 | 21 | __UNUSED int32_t lockStepPack_encodeVec(ioBufVec_tt* pInBufVec, int32_t iCount, uint8_t uiFlag, 22 | uint32_t uiToken, ioBufVec_tt* pOutBufVec); 23 | 24 | static inline int32_t lockStepPack_encodeBufCount(const char* pBuffer, size_t nLength) 25 | { 26 | return (int32_t)((nLength + 0xfffe) / 0xffff); 27 | } 28 | 29 | static inline int32_t lockStepPack_encodeVecBufCount(ioBufVec_tt* pInBufVec, int32_t iCount) 30 | { 31 | size_t nLength = 0; 32 | for (int32_t i = 0; i < iCount; i++) { 33 | nLength += pInBufVec[i].iLength; 34 | } 35 | return (int32_t)((nLength + 0xfffe) / 0xffff); 36 | } 37 | -------------------------------------------------------------------------------- /src/runtime/include/lockStepPack/lockStepPack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "byteQueue_t.h" 4 | #include "cbuf_t.h" 5 | #include "utility_t.h" 6 | 7 | typedef struct lockStepPackHead_s 8 | { 9 | uint8_t packLength; 10 | uint16_t frameId; 11 | } lockStepPackHead_tt; 12 | 13 | __UNUSED int32_t lockStepPack_decodeHead(byteQueue_tt* pInBytes, lockStepPackHead_tt* pOutHead); 14 | 15 | __UNUSED int32_t lockStepPack_decode(byteQueue_tt* pInBytes, cbuf_tt* pOutBuf, uint8_t* pOutFlag, 16 | uint32_t* pOutToken); 17 | 18 | __UNUSED int32_t lockStepPack_encode(const char* pBuffer, size_t nLength, uint8_t uiFlag, 19 | uint32_t uiToken, ioBufVec_tt* pOutBufVec); 20 | 21 | __UNUSED int32_t lockStepPack_encodeVec(ioBufVec_tt* pInBufVec, int32_t iCount, uint8_t uiFlag, 22 | uint32_t uiToken, ioBufVec_tt* pOutBufVec); 23 | 24 | static inline int32_t lockStepPack_encodeBufCount(const char* pBuffer, size_t nLength) 25 | { 26 | return (int32_t)((nLength + 0xfffe) / 0xffff); 27 | } 28 | 29 | static inline int32_t lockStepPack_encodeVecBufCount(ioBufVec_tt* pInBufVec, int32_t iCount) 30 | { 31 | size_t nLength = 0; 32 | for (int32_t i = 0; i < iCount; i++) { 33 | nLength += pInBufVec[i].iLength; 34 | } 35 | return (int32_t)((nLength + 0xfffe) / 0xffff); 36 | } 37 | -------------------------------------------------------------------------------- /src/lua/service/example/example_console.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local mode = ... 4 | 5 | local command = {} 6 | 7 | function command.testCall(s,n,b,f) 8 | if s ~= "testdata" then 9 | error("string error") 10 | end 11 | 12 | if n ~= 543634 then 13 | error("int error") 14 | end 15 | 16 | if f ~= 0.0001 then 17 | return 18 | end 19 | 20 | if not b then 21 | return 22 | end 23 | local c = serviceCore.call(serviceCore.getRunningAddress(),"testReply") 24 | print(c) 25 | local cc = c+5 26 | return cc 27 | end 28 | 29 | function command.testReply() 30 | return 5 31 | end 32 | 33 | if mode == "call" then 34 | serviceCore.start(function() 35 | serviceCore.eventDispatch(serviceCore.eventCall, function(_,cmd,...) 36 | local f = command[cmd] 37 | if f then 38 | serviceCore.reply(f(...)) 39 | else 40 | error(string.format("unknown command %s", tostring(cmd))) 41 | end 42 | end) 43 | local c = serviceCore.call("exampleCall","testCall","testdata",543634,true,0.0001) 44 | print(c) 45 | end) 46 | else 47 | serviceCore.start(function() 48 | serviceCore.bindName("exampleCall") 49 | serviceCore.eventDispatch(serviceCore.eventCall, function(_,cmd,...) 50 | local f = command[cmd] 51 | if f then 52 | serviceCore.reply(f(...)) 53 | else 54 | error(string.format("unknown command %s", tostring(cmd))) 55 | end 56 | end) 57 | end) 58 | end 59 | 60 | -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/win/eventIO-inl.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "queue_t.h" 12 | #include "heap_t.h" 13 | #include "thread_t.h" 14 | #include "log_t.h" 15 | #include "spinLock_t.h" 16 | 17 | #define DEF_USE_SPINLOCK 18 | 19 | struct eventIO_s 20 | { 21 | HANDLE hCompletionPort; 22 | uint32_t uiCocurrentThreads; 23 | uint64_t uiLoopTime; 24 | uint64_t uiTimerCounter; 25 | struct heap timerHeap; 26 | bool bTimerEventOff; 27 | QUEUE queueSocketReuse; 28 | #ifdef DEF_USE_SPINLOCK 29 | spinLock_tt socketReuseLock; 30 | #else 31 | mutex_tt socketReuseLock; 32 | #endif 33 | atomic_uint uiCocurrentRunning; 34 | atomic_int iIdleThreads; 35 | QUEUE queuePending; 36 | mutex_tt mutex; 37 | uint64_t uiThreadId; 38 | bool bRunning; 39 | bool bLoopSleep; 40 | atomic_bool bLoopRunning; 41 | atomic_bool bLoopNotified; 42 | cond_tt cond; 43 | atomic_int iRefCount; 44 | }; 45 | 46 | static inline bool eventIO_isRunning(struct eventIO_s* pEventIO) 47 | { 48 | return atomic_load(&pEventIO->bLoopRunning); 49 | } 50 | 51 | __UNUSED SOCKET eventIO_makeSocket(struct eventIO_s* pEventIO); 52 | 53 | __UNUSED void eventIO_recoverySocket(struct eventIO_s* pEventIO, SOCKET hSocket); 54 | -------------------------------------------------------------------------------- /src/lua/helper/logExt.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local tDump = {} 4 | 5 | local maxLevel = 32 6 | 7 | local function dump_f(s) 8 | tDump[#tDump+1] = s 9 | end 10 | 11 | local function sub_table(t,l) 12 | l = l + 1 13 | local s = string.rep("\t",l) 14 | if l <= maxLevel then 15 | for k,v in pairs(t) do 16 | if type(v) == "table" then 17 | dump_f(string.format("%s[%s]={",s,k)) 18 | sub_table(v,l) 19 | dump_f(string.format("%s}",s)) 20 | elseif type(v) == "string" then 21 | dump_f(string.format('%s[%s]="%s"',s,k,tostring(v))) 22 | else 23 | dump_f(string.format("%s[%s]=%s",s,k,tostring(v))) 24 | end 25 | end 26 | else 27 | dump_f(string.format("%s__level_limit",s)) 28 | end 29 | end 30 | 31 | local function dump_table(t) 32 | dump_f(string.format("[%s]={",tostring(t))) 33 | sub_table(t,0) 34 | dump_f("}") 35 | end 36 | 37 | local function log_extend(...) 38 | local t = { ... } 39 | for _,v in ipairs(t) do 40 | if type(v) == "table" then 41 | dump_table(v) 42 | else 43 | dump_f(tostring(v)) 44 | end 45 | end 46 | serviceCore.log(table.concat(tDump,"\n")) 47 | tDump = {} 48 | end 49 | 50 | local function setMaxLevel(l) 51 | assert(math.type(l) == "integer") 52 | maxLevel = l 53 | end 54 | 55 | return log_extend,setMaxLevel -------------------------------------------------------------------------------- /src/core/include/codec/webSocket_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "byteQueue_t.h" 4 | #include "cbuf_t.h" 5 | 6 | 7 | #define DEF_WS_FRAME_FINAL 0x80 8 | #define DEF_WS_FRAME_PING 0x50 9 | #define DEF_WS_FRAME_PONG 0x40 10 | #define DEF_WS_FRAME_CLOSE 0x30 11 | #define DEF_WS_FRAME_BINARY 0x20 12 | #define DEF_WS_FRAME_TEXT 0x10 13 | 14 | #define DEF_WS_CODE_MASK 0x70 15 | #define DEF_WS_FRAME_MASK 0xF0 16 | 17 | typedef struct webSocketHead_s 18 | { 19 | uint16_t uiPayloadLen; 20 | uint8_t uiFlag; 21 | uint8_t uiOffset; 22 | } webSocketHead_tt; 23 | 24 | frCore_API int32_t webSocket_decodeHead(byteQueue_tt* pInBytes, webSocketHead_tt* pOutHead); 25 | 26 | frCore_API int32_t webSocket_decode(byteQueue_tt* pInBytes, cbuf_tt* pOutBuf, uint8_t* pOutFlag); 27 | 28 | frCore_API int32_t webSocket_encode(const char* pBuffer, size_t nLength, uint8_t uiFlag, 29 | ioBufVec_tt* pOutBufVec); 30 | 31 | frCore_API int32_t webSocket_encodeVec(ioBufVec_tt* pInBufVec, int32_t iCount, uint8_t uiFlag, 32 | ioBufVec_tt* pOutBufVec); 33 | 34 | static inline int32_t webSocket_encodeBufCount(const char* pBuffer, size_t nLength) 35 | { 36 | return nLength / 0xffff + 1; 37 | } 38 | 39 | static inline int32_t webSocket_encodeVecBufCount(ioBufVec_tt* pInBufVec, int32_t iCount) 40 | { 41 | size_t nLength = 0; 42 | for (int32_t i = 0; i < iCount; i++) { 43 | nLength += pInBufVec[i].iLength; 44 | } 45 | return nLength / 0xffff + 1; 46 | } 47 | -------------------------------------------------------------------------------- /src/lua/service/example/example_redis.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local redis = require "db.redis" 3 | local logExt = require "logExt" 4 | 5 | local function publish() 6 | local db = redis.connect({address = "127.0.0.1:6379",db=0}) 7 | if db then 8 | serviceCore.log("example_redis redis connect") 9 | else 10 | return 11 | end 12 | db:subscribe("channel") 13 | db:psubscribe("publish.*") 14 | while true do 15 | logExt("example_redis ",db:message()) 16 | end 17 | end 18 | 19 | serviceCore.start(function() 20 | serviceCore.async(publish) 21 | 22 | local db = redis.connect({address = "127.0.0.1:6379",db=0}) 23 | if db then 24 | serviceCore.log("example_redis connect") 25 | else 26 | serviceCore.log("example_redis connect error") 27 | serviceCore.exit() 28 | return 29 | end 30 | 31 | db:del("setH") 32 | for i=1,10 do 33 | db:hset("setH",i,i) 34 | end 35 | local r = db:hvals("setH") 36 | logExt("example_redis ",r) 37 | 38 | local s = db:get("runoobError") 39 | logExt("example_redis ",s) 40 | s = db:set("runoob","hello") 41 | logExt("example_redis ",s) 42 | serviceCore.log(db:get("runoob")) 43 | 44 | local cmds = {} 45 | cmds[1] = "get runoob" 46 | cmds[2] = "hvals setH" 47 | logExt("example_redis ",db:cmdPipeline(cmds)) 48 | 49 | db:publish("channel","test") 50 | db:publish("channel","test2") 51 | db:publish("channel","test3") 52 | db:publish("publish.test","test") 53 | 54 | serviceCore.exit() 55 | end) -------------------------------------------------------------------------------- /src/core/include/macros_t.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef defmax 4 | # define defmax(a, b) (((a) > (b)) ? (a) : (b)) 5 | #endif 6 | 7 | #ifndef defmin 8 | # define defmin(a, b) (((a) < (b)) ? (a) : (b)) 9 | #endif 10 | 11 | #define SAFE_DELETE(p) \ 12 | { \ 13 | if (p) { \ 14 | delete (p); \ 15 | (p) = nullptr; \ 16 | } \ 17 | } 18 | #define SAFE_DELETE_ARRAY(p) \ 19 | { \ 20 | if (p) { \ 21 | delete[](p); \ 22 | (p) = nullptr; \ 23 | } \ 24 | } 25 | #define SAFE_RELEASE(p) \ 26 | { \ 27 | if (p) { \ 28 | (p)->Release(); \ 29 | (p) = nullptr; \ 30 | } \ 31 | } 32 | 33 | #define CheckPointer(p) \ 34 | if (p == nullptr) { \ 35 | assert(p != nullptr); \ 36 | return; \ 37 | } 38 | 39 | #define CheckPointer_Return(p, r) \ 40 | if (p == nullptr) { \ 41 | assert(p != nullptr); \ 42 | return r; \ 43 | } 44 | 45 | #define CheckAssert(p) \ 46 | if (!(p)) { \ 47 | assert((p)); \ 48 | return; \ 49 | } 50 | 51 | #define CheckAssert_Return(p, r) \ 52 | if (!(p)) { \ 53 | assert((p)); \ 54 | return r; \ 55 | } 56 | 57 | #define FloatEqual(f1, f2) (std::fabs((f1) - (f2)) < std::numeric_limits::epsilon()) -------------------------------------------------------------------------------- /src/core/include/codec/tpack_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "byteQueue_t.h" 4 | #include "cbuf_t.h" 5 | 6 | 7 | #define DEF_TP_FRAME_FINAL 0x80 8 | #define DEF_TP_FRAME_REPLY 0x70 9 | #define DEF_TP_FRAME_CALL 0x60 10 | #define DEF_TP_FRAME_PING 0x50 11 | #define DEF_TP_FRAME_PONG 0x40 12 | #define DEF_TP_FRAME_CLOSE 0x30 13 | #define DEF_TP_FRAME_BINARY 0x20 14 | #define DEF_TP_FRAME_TEXT 0x10 15 | 16 | #define DEF_TP_CODE_MASK 0x70 17 | #define DEF_TP_FRAME_MASK 0xF0 18 | 19 | typedef struct tpackHead_s 20 | { 21 | uint16_t uiPayloadLen; 22 | uint8_t uiFlag; 23 | uint8_t uiOffset; 24 | } tpackHead_tt; 25 | 26 | frCore_API int32_t tpack_decodeHead(byteQueue_tt* pInBytes, tpackHead_tt* pOutHead); 27 | 28 | frCore_API int32_t tpack_decode(byteQueue_tt* pInBytes, cbuf_tt* pOutBuf, uint8_t* pOutFlag, 29 | uint32_t* pOutToken); 30 | 31 | frCore_API int32_t tpack_encode(const char* pBuffer, size_t nLength, uint8_t uiFlag, 32 | uint32_t uiToken, ioBufVec_tt* pOutBufVec); 33 | 34 | frCore_API int32_t tpack_encodeVec(ioBufVec_tt* pInBufVec, int32_t iCount, uint8_t uiFlag, 35 | uint32_t uiToken, ioBufVec_tt* pOutBufVec); 36 | 37 | static inline int32_t tpack_encodeBufCount(const char* pBuffer, size_t nLength) 38 | { 39 | return nLength / 0xffff + 1; 40 | } 41 | 42 | static inline int32_t tpack_encodeVecBufCount(ioBufVec_tt* pInBufVec, int32_t iCount) 43 | { 44 | size_t nLength = 0; 45 | for (int32_t i = 0; i < iCount; i++) { 46 | nLength += pInBufVec[i].iLength; 47 | } 48 | return nLength / 0xffff + 1; 49 | } 50 | -------------------------------------------------------------------------------- /src/runtime/source/internal/lconnector_t.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "internal/lconnector_t.h" 4 | 5 | #include 6 | #include 7 | 8 | #include "lauxlib.h" 9 | #include "lua.h" 10 | #include "lualib.h" 11 | 12 | #include "service_t.h" 13 | 14 | static int32_t lconnector_close(lua_State* L) 15 | { 16 | lconnector_tt* pConnector = (lconnector_tt*)luaL_checkudata(L, 1, "connector"); 17 | luaL_argcheck(L, pConnector != NULL, 1, "invalid user data"); 18 | if (pConnector->pHandle) { 19 | connector_close(pConnector->pHandle); 20 | connector_release(pConnector->pHandle); 21 | pConnector->pHandle = NULL; 22 | } 23 | return 0; 24 | } 25 | 26 | static int32_t lconnector_gc(lua_State* L) 27 | { 28 | lconnector_tt* pConnector = (lconnector_tt*)luaL_checkudata(L, 1, "connector"); 29 | luaL_argcheck(L, pConnector != NULL, 1, "invalid user data"); 30 | 31 | if (pConnector->pHandle) { 32 | connector_release(pConnector->pHandle); 33 | pConnector->pHandle = NULL; 34 | } 35 | return 0; 36 | } 37 | 38 | int32_t registerConnectorL(lua_State* L) 39 | { 40 | luaL_newmetatable(L, "connector"); 41 | /* metatable.__index = metatable */ 42 | lua_pushvalue(L, -1); 43 | lua_setfield(L, -2, "__index"); 44 | 45 | struct luaL_Reg lua_connectorFuncs[] = {{"close", lconnector_close}, 46 | {"__close", lconnector_gc}, 47 | {"__gc", lconnector_gc}, 48 | {NULL, NULL}}; 49 | 50 | luaL_setfuncs(L, lua_connectorFuncs, 0); 51 | return 1; 52 | }; 53 | -------------------------------------------------------------------------------- /src/core/include/cbuf_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "utility_t.h" 8 | 9 | typedef struct cbuf_s 10 | { 11 | // private 12 | char* pBuffer; 13 | size_t nLength; 14 | } cbuf_tt; 15 | 16 | static inline void cbuf_init(cbuf_tt* pBuf, size_t nLength) 17 | { 18 | pBuf->nLength = nLength; 19 | if (nLength != 0) { 20 | pBuf->pBuffer = (char*)mem_malloc(nLength); 21 | } 22 | else { 23 | pBuf->pBuffer = NULL; 24 | } 25 | } 26 | 27 | static inline void cbuf_clear(cbuf_tt* pBuf) 28 | { 29 | pBuf->nLength = 0; 30 | if (pBuf->pBuffer) { 31 | mem_free(pBuf->pBuffer); 32 | pBuf->pBuffer = NULL; 33 | } 34 | } 35 | 36 | static inline char* cbuf_buffer(cbuf_tt* pBuf) 37 | { 38 | return pBuf->pBuffer; 39 | } 40 | 41 | static inline size_t cbuf_length(cbuf_tt* pBuf) 42 | { 43 | return pBuf->nLength; 44 | } 45 | 46 | static inline bool cbuf_empty(cbuf_tt* pBuf) 47 | { 48 | return pBuf->nLength == 0; 49 | } 50 | 51 | static inline void cbuf_swapToReset(cbuf_tt* pBuf, cbuf_tt* pRhs) 52 | { 53 | char* pBuffer = pBuf->pBuffer; 54 | size_t nLength = pBuf->nLength; 55 | pBuf->pBuffer = pRhs->pBuffer; 56 | pBuf->nLength = pRhs->nLength; 57 | pRhs->pBuffer = pBuffer; 58 | pRhs->nLength = nLength; 59 | } 60 | 61 | static inline void cbuf_swap(cbuf_tt* pBuf, char** ppBuffer, size_t* pLength) 62 | { 63 | char* pBuffer = pBuf->pBuffer; 64 | size_t nLength = pBuf->nLength; 65 | pBuf->pBuffer = *ppBuffer; 66 | pBuf->nLength = *pLength; 67 | *ppBuffer = pBuffer; 68 | *pLength = nLength; 69 | } 70 | -------------------------------------------------------------------------------- /src/serverBin/source/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "lauxlib.h" 12 | #include "lua.h" 13 | #include "lualib.h" 14 | 15 | static inline void setPackage_cpath(lua_State* L, const char* szPackagePath) 16 | { 17 | lua_getglobal(L, "package"); 18 | lua_getfield(L, -1, "cpath"); 19 | const char* cur_path = lua_tostring(L, -1); 20 | lua_pop(L, 1); 21 | lua_pushfstring(L, "%s;%s", cur_path, szPackagePath); 22 | lua_setfield(L, -2, "cpath"); 23 | lua_pop(L, 1); 24 | } 25 | 26 | int32_t main(int32_t argc, char** argv) 27 | { 28 | const char* szStartUpFileName = NULL; 29 | if (argc > 1) { 30 | szStartUpFileName = argv[1]; 31 | } 32 | else { 33 | fprintf(stderr, "need a start-up file\n"); 34 | return 1; 35 | } 36 | 37 | lua_State* L = luaL_newstate(); 38 | luaL_openlibs(L); 39 | #if defined(_WINDOWS) || defined(_WIN32) 40 | setPackage_cpath(L, "modules/?.dll"); 41 | #elif defined(__APPLE__) 42 | setPackage_cpath(L, "modules/?.dylib"); 43 | #elif defined(__linux__) 44 | setPackage_cpath(L, "modules/?.so"); 45 | #endif 46 | 47 | int32_t status = luaL_loadfile(L, szStartUpFileName); 48 | if (status != LUA_OK) { 49 | fprintf(stderr, "luaL_loadfile error:%s\n", lua_tostring(L, -1)); 50 | lua_close(L); 51 | return 1; 52 | } 53 | 54 | if (lua_pcall(L, 0, 0, 0) != LUA_OK) { 55 | fprintf(stderr, "lua_pcall error:%s\n", lua_tostring(L, -1)); 56 | lua_close(L); 57 | return 1; 58 | } 59 | lua_close(L); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /src/benchmark/source/spin_lock/spinLock.c: -------------------------------------------------------------------------------- 1 | #include "spinLock.h" 2 | 3 | #include 4 | 5 | #include "thread_t.h" 6 | #include "time_t.h" 7 | #include "utility_t.h" 8 | 9 | struct spinLock_s 10 | { 11 | atomic_flag flag; 12 | }; 13 | 14 | void spinLock_init(spinLock_tt* lock) 15 | { 16 | *lock = mem_malloc(sizeof(struct spinLock_s)); 17 | atomic_flag_clear(&((*lock)->flag)); 18 | } 19 | 20 | void spinLock_destroy(spinLock_tt* lock) 21 | { 22 | mem_free(*lock); 23 | *lock = NULL; 24 | } 25 | 26 | static const uint32_t s_uiMaxActiveSpin = 2048; 27 | 28 | void spinLock_lock(spinLock_tt* lock) 29 | { 30 | uint32_t spinCount = 0; 31 | if (atomic_flag_test_and_set_explicit(&((*lock)->flag), memory_order_acquire)) { 32 | do { 33 | if (spinCount < s_uiMaxActiveSpin) { 34 | ++spinCount; 35 | thread_pause(); 36 | } 37 | else { 38 | threadYield(); 39 | } 40 | } while (atomic_flag_test_and_set_explicit(&((*lock)->flag), memory_order_relaxed)); 41 | } 42 | } 43 | 44 | void spinLock_lock2(spinLock_tt* lock) 45 | { 46 | while (atomic_flag_test_and_set_explicit(&((*lock)->flag), memory_order_acquire)) { 47 | threadYield(); 48 | } 49 | } 50 | 51 | void spinLock_lock3(spinLock_tt* lock) 52 | { 53 | if (atomic_flag_test_and_set_explicit(&((*lock)->flag), memory_order_acquire)) { 54 | do { 55 | threadYield(); 56 | } while (atomic_flag_test_and_set_explicit(&((*lock)->flag), memory_order_relaxed)); 57 | } 58 | } 59 | 60 | void spinLock_unlock(spinLock_tt* lock) 61 | { 62 | atomic_flag_clear_explicit(&((*lock)->flag), memory_order_release); 63 | } 64 | -------------------------------------------------------------------------------- /src/lua/service/localServices.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | 3 | local services = {} 4 | local command = {} 5 | 6 | function command.new(filename,param) 7 | local serviceId = serviceCore.createService(filename,param) 8 | if serviceId ~= 0 then 9 | services[serviceId] = { filename,param } 10 | end 11 | return serviceCore.replyCommand(serviceId); 12 | end 13 | 14 | function command.remove(serviceId) 15 | services[serviceId] = nil 16 | end 17 | 18 | function command.mem() 19 | local list = {} 20 | for k,v in pairs(services) do 21 | local ok, kb = pcall(serviceCore.callCommand,k,"_mem") 22 | if not ok then 23 | list[serviceCore.addressToString(k)] = string.format("error (%s:%s)",v[1],v[2]) 24 | else 25 | list[serviceCore.addressToString(k)] = string.format("%.2f Kb (%s:%s)",kb,v[1],v[2]) 26 | end 27 | end 28 | serviceCore.replyCommand(list) 29 | end 30 | 31 | function command.gc() 32 | for k,_ in pairs(services) do 33 | serviceCore.command(k,"_gc") 34 | end 35 | end 36 | 37 | function command.list() 38 | local list = {} 39 | for k,v in pairs(services) do 40 | list[serviceCore.addressToString(k)] = string.format("(%s:%s)",v[1],v[2]) 41 | end 42 | serviceCore.replyCommand(list) 43 | end 44 | 45 | function command.status() 46 | local list = {} 47 | for k,v in pairs(services) do 48 | local ok, status = pcall(serviceCore.callCommand,k,"_status") 49 | if not ok then 50 | status = string.format("error (%s:%s)",v[1],v[2]) 51 | end 52 | list[serviceCore.addressToString(k)] = status 53 | end 54 | serviceCore.replyCommand(list) 55 | end 56 | 57 | 58 | serviceCore.start(function() 59 | serviceCore.eventDispatch(serviceCore.eventCommand,function(_,cmd,...) 60 | local f = command[cmd] 61 | if f then 62 | f(...) 63 | end 64 | end) 65 | end) -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.0 FATAL_ERROR) 2 | 3 | # Frog version 4 | set(FROG_MAJOR_VERSION 0) 5 | set(FROG_MINOR_VERSION 1) 6 | set(FROG_PATCH_VERSION 0) 7 | 8 | # package set 9 | set(PACKAGE_NAME "Frog") 10 | set(PACKAGE_VERSION_MAJOR ${FROG_MAJOR_VERSION}) 11 | set(PACKAGE_VERSION_MINOR ${FROG_MINOR_VERSION}) 12 | set(PACKAGE_VERSION_PATCH ${FROG_PATCH_VERSION}) 13 | set(PACKAGE_VERSION ${FROG_MAJOR_VERSION}.${FROG_MINOR_VERSION}.${FROG_PATCH_VERSION}) 14 | set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") 15 | set(PACKAGE_TARNAME "${PACKAGE_NAME}-v${PACKAGE_VERSION}") 16 | set(PACKAGE_BUGREPORT "https://github.com/Frog/Frog/issues") 17 | 18 | # Frog 19 | project(${PACKAGE_NAME}) 20 | 21 | # root 22 | set(FROG_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) 23 | set(CMAKE_MODULE_PATH ${FROG_ROOT_PATH}/cmake/modules/) 24 | 25 | set(FROG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) 26 | set(FROG_3RDPARTY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty) 27 | set(FROG_3RDPARTY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/3rdparty) 28 | 29 | option(MSVC_USE_STATIC_RUNTIME_LIBRARY "msvc use static runitme library" OFF) 30 | 31 | include(build_config) 32 | 33 | # build options 34 | option(BUILD_OPENSSL "build openssl" OFF) 35 | option(BUILD_MIMALLOC "build mimalloc" ON) 36 | option(BUILD_LPEG "build lpeg" ON) 37 | option(ENABLE_UNITTEST "enable unittest" ON) 38 | option(ENABLE_BENCHMARK "enable benchmark" ON) 39 | option(DEBUG_3RDPARTY_OUTPUT "debug 3rdparty output" OFF) 40 | option(INSTALL_3RDPARTY "install 3rdparty" ON) 41 | 42 | # install 3rdparty 43 | if(INSTALL_3RDPARTY) 44 | include(install_3rdparty) 45 | endif() 46 | 47 | if(BUILD_OPENSSL) 48 | set(OPENSSL_ROOT_DIR ${FROG_3RDPARTY_BINARY_DIR}/install/openssl) 49 | endif() 50 | 51 | find_package(OpenSSL REQUIRED) 52 | 53 | add_subdirectory( 54 | src 55 | ) 56 | -------------------------------------------------------------------------------- /src/benchmark/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(BENCHMARKTEST_EXE "benchmarktest") 2 | 3 | set(INSTALL_BENCHMARKTEST_EXE_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables") 4 | 5 | set(BENCHMARKTEST_SOURCE_FILES 6 | ${CMAKE_CURRENT_SOURCE_DIR}/source/spin_lock/spinLock.c 7 | ${CMAKE_CURRENT_SOURCE_DIR}/source/spin_lock/mscLock.c 8 | ${CMAKE_CURRENT_SOURCE_DIR}/source/spin_lock/clhLock.c 9 | ${CMAKE_CURRENT_SOURCE_DIR}/source/spin_lock/rwSpinLock.c 10 | ${CMAKE_CURRENT_SOURCE_DIR}/source/threadLock_benchmark.cc 11 | ) 12 | 13 | include_directories( 14 | ${FROG_3RDPARTY_BINARY_DIR}/install/benchmark/include 15 | ${FROG_3RDPARTY_BINARY_DIR}/install/lua/include 16 | ${FROG_SOURCE_DIR}/core/include 17 | ${FROG_SOURCE_DIR}/service/include 18 | ${CMAKE_CURRENT_SOURCE_DIR}/include 19 | ) 20 | 21 | link_directories(${FROG_3RDPARTY_BINARY_DIR}/install/benchmark/lib) 22 | link_directories(${FROG_3RDPARTY_BINARY_DIR}/install/lua/lib) 23 | 24 | add_executable(${BENCHMARKTEST_EXE} 25 | ${BENCHMARKTEST_SOURCE_FILES} 26 | ) 27 | 28 | FUNCTION_COMPILE_DEFINE(${BENCHMARKTEST_EXE}) 29 | FUNCTION_COMPILE_OPTION(${BENCHMARKTEST_EXE}) 30 | 31 | set_target_properties(${BENCHMARKTEST_EXE} PROPERTIES LINKER_LANGUAGE CXX) 32 | 33 | set_target_properties(${BENCHMARKTEST_EXE} PROPERTIES CXX_STANDARD 11) 34 | 35 | if(WINDOWS) 36 | target_link_libraries(${BENCHMARKTEST_EXE} PRIVATE benchmark_main benchmark frogService frogCore lua Ws2_32.lib Shlwapi.lib 37 | ) 38 | else() 39 | target_link_libraries(${BENCHMARKTEST_EXE} PRIVATE benchmark_main benchmark frogService frogCore lua ${CMAKE_THREAD_LIBS_INIT} dl 40 | ) 41 | endif() 42 | 43 | set_target_properties(${BENCHMARKTEST_EXE} PROPERTIES INSTALL_RPATH "${INSTALL_BENCHMARKTEST_EXE_DIR}") 44 | 45 | install(TARGETS ${BENCHMARKTEST_EXE} DESTINATION "${INSTALL_BENCHMARKTEST_EXE_DIR}") 46 | -------------------------------------------------------------------------------- /src/lua/helper/log.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local log = {} 3 | 4 | log.level = "trace" 5 | 6 | local eLevel = { 7 | elog_trace = 0, 8 | elog_debug = 1, 9 | eLog_info = 2, 10 | eLog_warning = 3, 11 | eLog_error = 4, 12 | eLog_fatal = 5, 13 | } 14 | 15 | local modes = { 16 | { name = "trace", level = eLevel.elog_trace, }, 17 | { name = "debug", level = eLevel.elog_debug, }, 18 | { name = "info", level = eLevel.eLog_info, }, 19 | { name = "warn", level = eLevel.eLog_warning, }, 20 | { name = "error", level = eLevel.eLog_error, }, 21 | { name = "fatal", level = eLevel.eLog_fatal, }, 22 | } 23 | 24 | local levels = {} 25 | for i, v in ipairs(modes) do 26 | levels[v.name] = i 27 | end 28 | 29 | 30 | local round = function(x, increment) 31 | increment = increment or 1 32 | x = x / increment 33 | return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment 34 | end 35 | 36 | 37 | local _tostring = tostring 38 | 39 | local tostring = function(...) 40 | local t = {} 41 | for i = 1, select('#', ...) do 42 | local x = select(i, ...) 43 | if type(x) == "number" then 44 | x = round(x, .01) 45 | end 46 | t[#t + 1] = _tostring(x) 47 | end 48 | return table.concat(t, " ") 49 | end 50 | 51 | 52 | for i, x in ipairs(modes) do 53 | log[x.name] = function(...) 54 | -- Return early if we're below the log level 55 | if i < levels[log.level] then 56 | return 57 | end 58 | local msg = tostring(...) 59 | local info = debug.getinfo(2, "Sl") 60 | local lineinfo = info.short_src .. ":" .. info.currentline 61 | 62 | serviceCore.log(string.format("%d$%s: %s", 63 | x.level, 64 | lineinfo, 65 | msg)) 66 | end 67 | end 68 | 69 | 70 | return log 71 | -------------------------------------------------------------------------------- /src/runtime/source/internal/ltimerWatcher_t.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "internal/ltimerWatcher_t.h" 4 | 5 | #include 6 | #include 7 | 8 | #include "lauxlib.h" 9 | #include "lua.h" 10 | #include "lualib.h" 11 | 12 | #include "service_t.h" 13 | 14 | static int32_t ltimerWatcher_stop(lua_State* L) 15 | { 16 | ltimerWatcher_tt* pTimerWatcherL = (ltimerWatcher_tt*)luaL_checkudata(L, 1, "timerWatcher"); 17 | luaL_argcheck(L, pTimerWatcherL != NULL, 1, "invalid user data"); 18 | if (pTimerWatcherL->pHandle) { 19 | timerWatcher_stop(pTimerWatcherL->pHandle); 20 | timerWatcher_release(pTimerWatcherL->pHandle); 21 | pTimerWatcherL->pHandle = NULL; 22 | lua_pushboolean(L, 1); 23 | } 24 | else { 25 | lua_pushboolean(L, 0); 26 | } 27 | return 1; 28 | } 29 | 30 | static int32_t ltimerWatcher_gc(lua_State* L) 31 | { 32 | ltimerWatcher_tt* pTimerWatcherL = (ltimerWatcher_tt*)luaL_checkudata(L, 1, "timerWatcher"); 33 | luaL_argcheck(L, pTimerWatcherL != NULL, 1, "invalid user data"); 34 | if (pTimerWatcherL->pHandle) { 35 | timerWatcher_stop(pTimerWatcherL->pHandle); 36 | timerWatcher_release(pTimerWatcherL->pHandle); 37 | pTimerWatcherL->pHandle = NULL; 38 | } 39 | return 0; 40 | } 41 | 42 | int32_t registerTimerWatcherL(lua_State* L) 43 | { 44 | luaL_newmetatable(L, "timerWatcher"); 45 | /* metatable.__index = metatable */ 46 | lua_pushvalue(L, -1); 47 | lua_setfield(L, -2, "__index"); 48 | 49 | struct luaL_Reg lua_timerWatcherFuncs[] = {{"stop", ltimerWatcher_stop}, 50 | {"__close", ltimerWatcher_gc}, 51 | {"__gc", ltimerWatcher_gc}, 52 | {NULL, NULL}}; 53 | 54 | luaL_setfuncs(L, lua_timerWatcherFuncs, 0); 55 | return 1; 56 | }; 57 | -------------------------------------------------------------------------------- /src/unittest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(UNITTEST_EXE "unittest") 2 | 3 | set(INSTALL_UNITTEST_EXE_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables") 4 | 5 | set(UNITTEST_SOURCE_FILES 6 | ${CMAKE_CURRENT_SOURCE_DIR}/source/test_byteQueue.cc 7 | ${CMAKE_CURRENT_SOURCE_DIR}/source/test_inetAddress.cc 8 | ${CMAKE_CURRENT_SOURCE_DIR}/source/test_thread.cc 9 | ${CMAKE_CURRENT_SOURCE_DIR}/source/test_thread2.cc 10 | ${CMAKE_CURRENT_SOURCE_DIR}/source/test_time.cc 11 | ${CMAKE_CURRENT_SOURCE_DIR}/source/test_eventIO.cc 12 | ) 13 | 14 | include_directories( 15 | ${FROG_3RDPARTY_BINARY_DIR}/install/googletest/include 16 | ${FROG_3RDPARTY_BINARY_DIR}/install/lua/include 17 | ${FROG_SOURCE_DIR}/core/include 18 | ${FROG_SOURCE_DIR}/service/include 19 | ${CMAKE_CURRENT_SOURCE_DIR}/include 20 | ) 21 | 22 | link_directories(${FROG_3RDPARTY_BINARY_DIR}/install/googletest/lib) 23 | link_directories(${FROG_3RDPARTY_BINARY_DIR}/install/lua/lib) 24 | 25 | add_executable(${UNITTEST_EXE} 26 | ${UNITTEST_SOURCE_FILES} 27 | ) 28 | 29 | FUNCTION_COMPILE_DEFINE(${UNITTEST_EXE}) 30 | FUNCTION_COMPILE_OPTION(${UNITTEST_EXE}) 31 | 32 | target_compile_definitions(${UNITTEST_EXE} PUBLIC _GOOGLE_TEST) 33 | 34 | set_target_properties(${UNITTEST_EXE} PROPERTIES LINKER_LANGUAGE CXX) 35 | 36 | set_target_properties(${UNITTEST_EXE} PROPERTIES CXX_STANDARD 11) 37 | 38 | if(WINDOWS) 39 | if(DEBUG_3RDPARTY_OUTPUT) 40 | set(gTestlib gtest_maind gtestd) 41 | else() 42 | set(gTestlib gtest_main gtest) 43 | endif() 44 | 45 | target_link_libraries(${UNITTEST_EXE} PRIVATE ${gTestlib} frogService frogCore lua Ws2_32.lib 46 | ) 47 | else() 48 | target_link_libraries(${UNITTEST_EXE} PRIVATE gtest_main gtest frogService frogCore lua ${CMAKE_THREAD_LIBS_INIT} dl 49 | ) 50 | endif() 51 | 52 | set_target_properties(${UNITTEST_EXE} PROPERTIES INSTALL_RPATH "${INSTALL_UNITTEST_EXE_DIR}") 53 | 54 | install(TARGETS ${UNITTEST_EXE} DESTINATION "${INSTALL_UNITTEST_EXE_DIR}") 55 | -------------------------------------------------------------------------------- /src/core/include/log_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include "platform_t.h" 9 | 10 | //颜色宏定义 11 | #define NONE "\033[m" 12 | #define RED "\033[0;32;31m" 13 | #define LIGHT_RED "\033[1;31m" 14 | #define GREEN "\033[0;32;32m" 15 | #define LIGHT_GREEN "\033[1;32m" 16 | #define BLUE "\033[0;32;34m" 17 | #define LIGHT_BLUE "\033[1;34m" 18 | #define DARY_GRAY "\033[1;30m" 19 | #define CYAN "\033[0;36m" 20 | #define LIGHT_CYAN "\033[1;36m" 21 | #define PURPLE "\033[0;35m" 22 | #define LIGHT_PURPLE "\033[1;35m" 23 | #define BROWN "\033[0;33m" 24 | #define YELLOW "\033[1;33m" 25 | #define LIGHT_GRAY "\033[0;37m" 26 | #define WHITE "\033[1;37m" 27 | 28 | typedef enum 29 | { 30 | elog_trace, 31 | elog_debug, 32 | eLog_info, 33 | eLog_warning, 34 | eLog_error, 35 | eLog_fatal, 36 | } enLogSeverityLevel; 37 | 38 | const static char* const logErrorStrArray[] = { 39 | "TRACE ", "DEBUG ", "INFO ", "WARNING ", "ERROR ", "FATAL "}; 40 | 41 | const static char* const logErrorColorArray[] = {PURPLE, WHITE, CYAN, YELLOW, RED, LIGHT_RED}; 42 | 43 | typedef void (*logCustomPrintFunc)(enLogSeverityLevel eLevel, const char* szMessage); 44 | 45 | frCore_API void setLogStderr(enLogSeverityLevel eLevel); 46 | 47 | frCore_API void setLogCustomPrint(logCustomPrintFunc fn); 48 | 49 | frCore_API void logPrint(enLogSeverityLevel eLevel, const char* szFileName, int32_t iLine, 50 | const char* szFmt, ...); 51 | 52 | #define Check(_value) \ 53 | if (!(_value)) { \ 54 | logPrint(eLog_fatal, __FILE__, __LINE__, #_value); \ 55 | } 56 | 57 | #define Log(_level, _text, ...) logPrint(_level, __FILE__, __LINE__, _text, ##__VA_ARGS__) 58 | 59 | #ifdef _DEBUG 60 | # define DLog(_level, _text, ...) logPrint(_level, __FILE__, __LINE__, _text, ##__VA_ARGS__) 61 | #else 62 | # define DLog(_level, _text, ...) 63 | #endif 64 | -------------------------------------------------------------------------------- /src/core/include/msgpack/msgpackEncode_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include "platform_t.h" 9 | 10 | typedef struct msgpackEncode_s 11 | { 12 | void* (*fnExpand)(void*, size_t, size_t, void*); 13 | void* pExpandData; 14 | uint8_t* pBuffer; 15 | size_t nLength; 16 | size_t nOffset; 17 | } msgpackEncode_tt; 18 | 19 | frCore_API void msgpackEncode_init(msgpackEncode_tt* pMsgpackEncode, void* pBuffer, size_t nLength, 20 | void* (*fnExpand)(void*, size_t, size_t, void*), 21 | void* pExpandData); 22 | 23 | frCore_API void msgpackEncode_clear(msgpackEncode_tt* pMsgpackEncode); 24 | 25 | frCore_API void msgpackEncode_writeInteger(msgpackEncode_tt* pMsgpackEncode, int64_t iValue); 26 | 27 | frCore_API void msgpackEncode_writeBoolean(msgpackEncode_tt* pMsgpackEncode, bool bValue); 28 | 29 | frCore_API void msgpackEncode_writeReal(msgpackEncode_tt* pMsgpackEncode, double fValue); 30 | 31 | frCore_API void msgpackEncode_writeString(msgpackEncode_tt* pMsgpackEncode, const char* szValue, 32 | size_t nLength); 33 | 34 | frCore_API void msgpackEncode_writeBinary(msgpackEncode_tt* pMsgpackEncode, const char* pValue, 35 | size_t nLength); 36 | 37 | frCore_API void msgpackEncode_writeArray(msgpackEncode_tt* pMsgpackEncode, int64_t iValue); 38 | 39 | frCore_API void msgpackEncode_writeMap(msgpackEncode_tt* pMsgpackEncode, int64_t iValue); 40 | 41 | frCore_API void msgpackEncode_writeUserPointer(msgpackEncode_tt* pMsgpackEncode, void* pValue); 42 | 43 | frCore_API void msgpackEncode_writeNil(msgpackEncode_tt* pMsgpackEncode); 44 | 45 | frCore_API void msgpackEncode_swap(msgpackEncode_tt* pMsgpackEncode, void** ppBuffer, 46 | size_t* pLength, size_t* pOffset); 47 | 48 | static inline size_t msgpackEncode_getWritten(msgpackEncode_tt* pMsgpackEncode) 49 | { 50 | return pMsgpackEncode->nOffset; 51 | } -------------------------------------------------------------------------------- /src/benchmark/source/spin_lock/mscLock.c: -------------------------------------------------------------------------------- 1 | #include "mscLock.h" 2 | 3 | #include 4 | 5 | #include "thread_t.h" 6 | #include "utility_t.h" 7 | 8 | typedef struct _decl_cpu_cache_align mscNode_s 9 | { 10 | volatile struct mscNode_s* pNext; 11 | volatile bool bLocked; 12 | } mscNode_tt; 13 | 14 | struct mscLock_s 15 | { 16 | _Atomic(mscNode_tt*) pTailLock; 17 | }; 18 | 19 | static _decl_threadLocal mscNode_tt* s_pLocalNode = NULL; 20 | 21 | 22 | static inline mscNode_tt* createMscNode() 23 | { 24 | mscNode_tt* pNode = malloc(sizeof(mscNode_tt)); 25 | pNode->bLocked = false; 26 | pNode->pNext = NULL; 27 | return pNode; 28 | } 29 | 30 | 31 | void mscLock_init(mscLock_tt* self) 32 | { 33 | *self = malloc(sizeof(struct mscLock_s)); 34 | atomic_init(&(*self)->pTailLock, NULL); 35 | } 36 | 37 | void mscLock_destroy(mscLock_tt* self) 38 | { 39 | free(*self); 40 | *self = NULL; 41 | } 42 | 43 | 44 | static void tls_mscNode_cleanup_func(void* pArg) 45 | { 46 | free(pArg); 47 | } 48 | 49 | void mscLock_lock(mscLock_tt* self) 50 | { 51 | if (s_pLocalNode == NULL) { 52 | s_pLocalNode = createMscNode(); 53 | setTlsValue(*self, tls_mscNode_cleanup_func, s_pLocalNode, true); 54 | } 55 | 56 | s_pLocalNode->pNext = NULL; 57 | s_pLocalNode->bLocked = true; 58 | 59 | mscNode_tt* pPrevNode = (mscNode_tt*)atomic_exchange(&((*self)->pTailLock), s_pLocalNode); 60 | if (pPrevNode == NULL) { 61 | return; 62 | } 63 | 64 | pPrevNode->pNext = s_pLocalNode; 65 | while (s_pLocalNode->bLocked) { 66 | threadYield(); 67 | } 68 | } 69 | 70 | void mscLock_unlock(mscLock_tt* self) 71 | { 72 | mscNode_tt* tmp = s_pLocalNode; 73 | if (s_pLocalNode->pNext == NULL) { 74 | if (atomic_compare_exchange_strong(&((*self)->pTailLock), &tmp, NULL)) { 75 | return; 76 | } 77 | while (s_pLocalNode->pNext == NULL) { 78 | threadYield(); 79 | } 80 | } 81 | s_pLocalNode->pNext->bLocked = false; 82 | } 83 | -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/posix/poller_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | #include "utility_t.h" 13 | 14 | enum enPollerAttr 15 | { 16 | ePollerReadable = 0x01, 17 | ePollerWritable = 0x02, 18 | ePollerClosed = 0x04 19 | }; 20 | 21 | struct poller_s; 22 | struct pollHandle_s; 23 | 24 | typedef struct poller_s poller_tt; 25 | typedef struct pollHandle_s pollHandle_tt; 26 | 27 | typedef void (*pollCallbackFunc)(pollHandle_tt*, int32_t); 28 | 29 | struct pollHandle_s 30 | { 31 | // private 32 | int32_t iAttribute; 33 | pollCallbackFunc fn; 34 | }; 35 | 36 | static inline void pollHandle_init(pollHandle_tt* pPollHandle) 37 | { 38 | pPollHandle->iAttribute = ePollerClosed; 39 | pPollHandle->fn = NULL; 40 | } 41 | 42 | static inline bool pollHandle_isWriting(pollHandle_tt* pPollHandle) 43 | { 44 | return pPollHandle->iAttribute & ePollerWritable; 45 | } 46 | 47 | static inline bool pollHandle_isReading(pollHandle_tt* pPollHandle) 48 | { 49 | return pPollHandle->iAttribute & ePollerReadable; 50 | } 51 | 52 | static inline bool pollHandle_isClosed(pollHandle_tt* pPollHandle) 53 | { 54 | return pPollHandle->iAttribute & ePollerClosed; 55 | } 56 | 57 | __UNUSED poller_tt* createPoller(); 58 | 59 | __UNUSED void poller_release(poller_tt* pPoller); 60 | 61 | __UNUSED int32_t poller_wait(poller_tt* pPoller, int32_t iTimeoutMs); 62 | 63 | __UNUSED void poller_dispatch(poller_tt* pPoller, int32_t iEvents); 64 | 65 | __UNUSED void poller_add(poller_tt* pPoller, int32_t iSocket, pollHandle_tt* pHandle, 66 | int32_t iAttribute, pollCallbackFunc fn); 67 | 68 | __UNUSED void poller_clear(poller_tt* pPoller, int32_t iSocket, pollHandle_tt* pHandle, 69 | int32_t iAttribute); 70 | 71 | __UNUSED void poller_setOpt(poller_tt* pPoller, int32_t iSocket, pollHandle_tt* pHandle, 72 | int32_t iAttribute); -------------------------------------------------------------------------------- /src/lua/library/inject.lua: -------------------------------------------------------------------------------- 1 | -- modify from skynet https://github.com/cloudwu/skynet/ inject.lua 2 | 3 | local function getupvaluetable(u, func, unique) 4 | local i = 1 5 | while true do 6 | local name, value = debug.getupvalue(func, i) 7 | if name == nil then 8 | return 9 | end 10 | local t = type(value) 11 | if t == "table" then 12 | u[name] = value 13 | elseif t == "function" then 14 | if not unique[value] then 15 | unique[value] = true 16 | getupvaluetable(u, value, unique) 17 | end 18 | end 19 | i=i+1 20 | end 21 | end 22 | 23 | local eventName_t = 24 | { 25 | [1] = "call", 26 | [2] = "ping", 27 | [3] = "close", 28 | [4] = "send", 29 | [5] = "text", 30 | [6] = "binary", 31 | [7] = "disconnect", 32 | [8] = "command", 33 | [9] = "accept" 34 | } 35 | 36 | return function(serviceCore, source, filename, args, ...) 37 | if filename then 38 | filename = "@" .. filename 39 | else 40 | filename = "=(load)" 41 | end 42 | local output = {} 43 | 44 | local function print(...) 45 | local value = { ... } 46 | for k,v in ipairs(value) do 47 | value[k] = tostring(v) 48 | end 49 | table.insert(output, table.concat(value, "\t")) 50 | end 51 | local u = {} 52 | local unique = {} 53 | local funcs = { ... } 54 | for k, func in ipairs(funcs) do 55 | getupvaluetable(u, func, unique) 56 | end 57 | local p = {} 58 | local eventDispatch = u.eventDispatch_t 59 | if eventDispatch then 60 | for k,v in pairs(eventDispatch) do 61 | local event, dispatch = k, v 62 | if dispatch then 63 | local pp = {} 64 | p[eventName_t[event]] = pp 65 | getupvaluetable(pp, dispatch, unique) 66 | end 67 | end 68 | end 69 | 70 | local func 71 | local err 72 | local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) 73 | func, err = load(source, filename, "bt", env) 74 | if not func then 75 | return false, { err } 76 | end 77 | local ok 78 | ok,err = serviceCore.pcall(func, table.unpack(args, 1, args.n)) 79 | if not ok then 80 | table.insert(output, err) 81 | return false, output 82 | end 83 | 84 | return true, output 85 | end 86 | -------------------------------------------------------------------------------- /src/core/include/platform_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define DEF_PLATFORM_UNKNOWN 0 4 | #define DEF_PLATFORM_WINDOWS 1 5 | #define DEF_PLATFORM_LINUX 2 6 | #define DEF_PLATFORM_MACOS 3 7 | #define DEF_PLATFORM_ANDROID 4 8 | #define DEF_PLATFORM_IOS 5 9 | 10 | #if defined(_WINDOWS) || defined(_WIN32) 11 | # define DEF_PLATFORM DEF_PLATFORM_WINDOWS 12 | #elif defined(__APPLE__) 13 | # if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 30000 || \ 14 | __IPHONE_OS_VERSION_MIN_REQUIRED > 30000 15 | # define DEF_PLATFORM DEF_PLATFORM_IOS 16 | # else 17 | # define DEF_PLATFORM DEF_PLATFORM_MACOS 18 | # endif 19 | #elif defined(__ANDROID__) 20 | # define DEF_PLATFORM DEF_PLATFORM_ANDROID 21 | #elif defined(__linux__) 22 | # define DEF_PLATFORM DEF_PLATFORM_LINUX 23 | #else 24 | # define DEF_PLATFORM DEF_PLATFORM_UNKNOWN 25 | #endif 26 | 27 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 28 | # if defined(_WIN64) 29 | # define DEF_PLATFORM_64BITS 30 | # endif 31 | #elif DEF_PLATFORM == DEF_PLATFORM_ANDROID 32 | # if __LP64__ 33 | # define DEF_PLATFORM_64BITS 34 | # endif 35 | #elif DEF_PLATFORM == DEF_PLATFORM_IOS 36 | # if __LP64__ 37 | # define DEF_PLATFORM_64BITS 38 | # endif 39 | #elif DEF_PLATFORM == DEF_PLATFORM_MACOS 40 | # define DEF_PLATFORM_64BITS 41 | #elif DEF_PLATFORM == DEF_PLATFORM_LINUX 42 | # if defined(_LINUX64) || defined(_LP64) 43 | # define DEF_PLATFORM_64BITS 44 | # endif 45 | #endif 46 | 47 | #if DEF_PLATFORM == DEF_PLATFORM_WINDOWS 48 | # ifdef _DEF_CORE_DLLEXPORT 49 | # define frCore_API __declspec(dllexport) 50 | # define frCore_DEF __declspec(dllexport) extern 51 | # else 52 | # define frCore_API __declspec(dllimport) 53 | # define frCore_DEF __declspec(dllimport) extern 54 | # endif 55 | #else 56 | # ifdef _DEF_CORE_DLLEXPORT 57 | # define frCore_API __attribute__((__visibility__("default"))) 58 | # define frCore_DEF __attribute__((__visibility__("default"))) extern 59 | # else 60 | # define frCore_API extern 61 | # define frCore_DEF extern 62 | # endif 63 | #endif -------------------------------------------------------------------------------- /src/service/include/internal/service-inl.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "queue_t.h" 12 | #include "spinLock_t.h" 13 | #include "thread_t.h" 14 | #include "utility_t.h" 15 | 16 | #include "eventIO/eventIO_t.h" 17 | 18 | typedef struct serviceEvent_s 19 | { 20 | void* node[2]; 21 | uint32_t uiSourceID; 22 | uint32_t uiToken; 23 | uint32_t uiLength; 24 | char szStorage[]; 25 | } serviceEvent_tt; 26 | 27 | #define DEF_USE_SPINLOCK 28 | 29 | struct service_s 30 | { 31 | void (*fnStop)(void*); 32 | bool (*fnCallback)(int32_t, uint32_t, uint32_t, void*, size_t, void*); 33 | void* pUserData; 34 | eventIO_tt* pEventIO; 35 | eventWatcher_tt* pEventWatcher; 36 | uint32_t uiServiceID; 37 | QUEUE queuePending; 38 | #ifdef DEF_USE_SPINLOCK 39 | spinLock_tt spinLock; 40 | #else 41 | mutex_tt mutex; 42 | #endif 43 | atomic_bool bRunning; 44 | atomic_int iRefCount; 45 | atomic_uint uiQueueSize; 46 | }; 47 | 48 | __UNUSED void service_waitFor(); 49 | 50 | __UNUSED void service_wakeUp(); 51 | 52 | __UNUSED int32_t service_waitForCount(); 53 | 54 | static inline void service_notify(struct service_s* pService) 55 | { 56 | if (eventWatcher_notify(pService->pEventWatcher)) { 57 | service_wakeUp(); 58 | } 59 | } 60 | 61 | static inline bool service_enqueue(struct service_s* pService, serviceEvent_tt* pEvent) 62 | { 63 | if (atomic_load(&pService->bRunning)) { 64 | atomic_fetch_add(&pService->uiQueueSize, 1); 65 | #ifdef DEF_USE_SPINLOCK 66 | spinLock_lock(&pService->spinLock); 67 | #else 68 | mutex_lock(&pService->mutex); 69 | #endif 70 | QUEUE_INSERT_TAIL(&pService->queuePending, &pEvent->node); 71 | #ifdef DEF_USE_SPINLOCK 72 | spinLock_unlock(&pService->spinLock); 73 | #else 74 | mutex_unlock(&pService->mutex); 75 | #endif 76 | service_notify(pService); 77 | return true; 78 | } 79 | mem_free(pEvent); 80 | return false; 81 | } -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/posix/eventListenPort_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "utility_t.h" 6 | #include "rbtree_t.h" 7 | 8 | #include "inetAddress_t.h" 9 | #include "rwSpinLock_t.h" 10 | 11 | #include "eventIO/internal/posix/poller_t.h" 12 | 13 | struct eventIOLoop_s; 14 | struct eventConnection_s; 15 | struct eventListenPort_s; 16 | 17 | typedef struct listenHandle_s 18 | { 19 | pollHandle_tt pollHandle; 20 | int32_t hSocket; 21 | struct eventListenPort_s* pEventListenPort; 22 | struct eventIOLoop_s* pEventIOLoop; 23 | } listenHandle_tt; 24 | 25 | typedef struct addressConnection_s 26 | { 27 | RB_ENTRY(addressConnection_s) 28 | entry; 29 | inetAddress_tt inetAddress; 30 | uint64_t hash; 31 | } addressConnection_tt; 32 | 33 | static inline int32_t addressCmp(struct addressConnection_s* src, struct addressConnection_s* dst) 34 | { 35 | int32_t iComp = (src->hash < dst->hash ? -1 : src->hash > dst->hash ? 1 : 0); 36 | if (iComp != 0) { 37 | return iComp; 38 | } 39 | return inetAddressCmp(&src->inetAddress, &dst->inetAddress); 40 | } 41 | 42 | RB_HEAD(addressConnectionMap_s, addressConnection_s); 43 | RB_GENERATE_STATIC(addressConnectionMap_s, addressConnection_s, entry, addressCmp) 44 | 45 | typedef struct addressConnectionMap_s addressConnectionMap_tt; 46 | 47 | struct eventListenPort_s 48 | { 49 | void (*fnAcceptCallback)(struct eventListenPort_s*, struct eventConnection_s*, const char*, 50 | uint32_t, void*); 51 | void (*fnUserFree)(void*); 52 | void* pUserData; 53 | listenHandle_tt* pListenHandle; 54 | struct eventIO_s* pEventIO; 55 | inetAddress_tt listenAddr; 56 | addressConnectionMap_tt mapAddressConnection; 57 | rwSpinLock_tt rwlock; 58 | bool bTcp; 59 | atomic_bool bActive; 60 | atomic_int iRefCount; 61 | }; 62 | 63 | __UNUSED void eventListenPort_removeAddressConnection(struct eventListenPort_s* pHandle, 64 | const inetAddress_tt* pRemoteAddr); 65 | -------------------------------------------------------------------------------- /INSTALL.md: -------------------------------------------------------------------------------- 1 | # Frog 2 | lua c event-driven framework 3 | 4 | ## build for Windows 5 | ================================= 6 | ### MSVC build 7 | 8 | Install Visual Studio 2019 with Clang compiler tools for Windows. 9 | For best IDE support in Visual Studio, we recommend using the latest Clang compiler tools for Windows. 10 | If you don't already have those, you can install them by opening the Visual Studio Installer and choosing Clang compiler for Windows under Desktop development with C++ optional components. 11 | 12 | Install CMake 13 | 14 | #### use cmake_msvc_Windows-x86.bat or cmake_msvc_Windows-x64.bat build 15 | Install OpenSSL 16 | 17 | Use VS2019 x86/x64 Native Tools Command: cd build, run cmake_msvc_Windows-x86.bat path(Frog/) or cmake_msvc_Windows-x64.bat path(Frog/). 18 | 19 | #### use cmake_msvc_Windows-x86_OpenSSL.bat or cmake_msvc_Windows-x64_OpenSSL.bat build 20 | Install OpenSSL build dependent 21 | - Perl. We recommend ActiveState Perl, available from 22 | https://www.activestate.com/ActivePerl. Another viable alternative 23 | appears to be Strawberry Perl, http://strawberryperl.com. 24 | - Install nasm. Add bin\NASM to the PATH environment variable. 25 | http://www.nasm.us/pub/nasm/releasebuilds/ 26 | 27 | Use VS2019 x86/x64 Native Tools Command: cd build, run cmake_msvc_Windows-x86_OpenSSL.bat path(Frog/) or cmake_msvc_Windows-x64_OpenSSL.bat path(Frog/). 28 | 29 | ## build for MacOS 30 | ================================= 31 | ### Xcode 32 | 33 | Install Xcode 34 | 35 | Install CMake 36 | 37 | #### use cmake_Xcode_macOS.sh build 38 | Install OpenSSL 39 | 40 | Use Command Line Tools: cd build, run sh cmake_Xcode_macOS.sh path(Frog/). 41 | 42 | #### use cmake_Xcode_macOS_OpenSSL.sh build 43 | Use Command Line Tools: cd build, run sh cmake_Xcode_macOS_OpenSSL.sh path(Frog/). 44 | 45 | ## build for Linux 46 | ================================= 47 | ### gcc 48 | 49 | Install gcc 50 | 51 | Install CMake 52 | 53 | #### use cmake_gcc_Linux.sh build 54 | Install OpenSSL 55 | 56 | Use Command Line Tools: cd build, run sh cmake_gcc_Linux.sh path(Frog/). 57 | 58 | #### use cmake_gcc_Linux_OpenSSL.sh build 59 | Use Command Line Tools: cd build, run sh cmake_gcc_Linux_OpenSSL.sh path(Frog/). 60 | -------------------------------------------------------------------------------- /tools/gitlab_cicd/git_pre_commit/check-pre-commit.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if git rev-parse --verify HEAD >/dev/null 2>&1 4 | then 5 | against=HEAD 6 | else 7 | against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 8 | fi 9 | 10 | astyle_lint_dir="./tools/gitlab_cicd/format_check/win" 11 | 12 | changed_c_cpp_files=$(git diff-index --cached $against | \ 13 | grep -E '[MA] .*\.(c|cpp|h)$' | \ 14 | grep -v 'glog' | \ 15 | cut -d' ' -f 2) 16 | 17 | changed_lua_files=$(git diff-index --cached $against | \ 18 | grep -E '[MA] .*\.(lua)$' | \ 19 | grep -v 'glog' | \ 20 | cut -d' ' -f 2) 21 | 22 | 23 | #c,c++代码风格检测 24 | c_cpp_int_ret=0 25 | if [ -n "$changed_c_cpp_files" ]; then 26 | python $astyle_lint_dir/../run-clang-format.py --clang-format-executable $astyle_lint_dir/clang-format.exe -r $changed_c_cpp_files 27 | c_cpp_int_ret=$? 28 | fi 29 | 30 | if [ "$c_cpp_int_ret" != 0 ]; then 31 | $astyle_lint_dir/clang-format.exe -style=file -i $changed_c_cpp_files 32 | echo -e "[c,c++提交失败!!!]\n上传的代码中存在一些不规范的地方, 已经自动对相关代码按照规范要求做了格式化操作, 请重新提交!\n若仍然存在不规范的地方, 请手动修改并提交, 直到所有代码都符合规范为止...\n\n" 33 | fi 34 | 35 | #lua代码风格检测 36 | lua_int_ret=0 37 | if [ -n "$changed_lua_files" ]; then 38 | for file in $changed_lua_files; 39 | do 40 | $astyle_lint_dir/CodeFormat.exe check -f $file -DAE -c .editorconfig 41 | lua_int_ret=$? 42 | if [ "$lua_int_ret" != 0 ]; then 43 | echo "格式化不规范的lua文件:${file}" 44 | fi 45 | done 46 | fi 47 | 48 | if [ "$lua_int_ret" != 0 ]; then 49 | for file in $changed_lua_files; 50 | do 51 | $astyle_lint_dir/CodeFormat.exe format -f $file -o $file -c .editorconfig 52 | done 53 | 54 | echo -e "[lua代码提交失败!!!]\n上传的代码中存在一些不规范的地方, 已经自动对相关代码按照规范要求做了格式化操作, 请重新提交!\n若仍然存在不规范的地方, 请手动修改并提交, 直到所有代码都符合规范为止..." 55 | fi 56 | 57 | # lua代码语法诊断检测 58 | lua_diagnosis_ret=0 59 | if [ -n "$changed_lua_files" ]; then 60 | $astyle_lint_dir/luacheck.exe $changed_lua_files --no-config --no-default-config --codes -q --exclude-files **/config.lua **/middleclass.lua --ignore 311 61 | lua_diagnosis_ret=$? 62 | fi 63 | 64 | if [ "$lua_diagnosis_ret" != 0 ]; then 65 | echo -e "[lua代码提交失败!!!]\n上传的代码中存在一些语法不规范的地方, 请手动修改并提交, 直到所有代码都符合规范为止..." 66 | fi 67 | 68 | if [[ "$c_cpp_int_ret" != 0 || "$lua_int_ret" != 0 || "$lua_diagnosis_ret" != 0 ]]; then 69 | exit 1 70 | fi -------------------------------------------------------------------------------- /src/benchmark/source/spin_lock/clhLock.c: -------------------------------------------------------------------------------- 1 | #include "clhLock.h" 2 | 3 | #include 4 | 5 | #include "thread_t.h" 6 | #include "utility_t.h" 7 | 8 | typedef struct _decl_cpu_cache_align chlNode_s 9 | { 10 | bool bLocked; 11 | // atomic_bool bLocked; 12 | } chlNode_tt; 13 | 14 | struct clhLock_s 15 | { 16 | chlNode_tt* pSelfNode; 17 | _Atomic(chlNode_tt*) hTailNode; 18 | }; 19 | 20 | static inline chlNode_tt* createChlNode(bool locked) 21 | { 22 | chlNode_tt* pNode = malloc(sizeof(chlNode_tt)); 23 | // atomic_init(&pNode->bLocked,locked); 24 | return pNode; 25 | } 26 | 27 | void clhLock_init(clhLock_tt* self) 28 | { 29 | *self = malloc(sizeof(struct clhLock_s)); 30 | chlNode_tt* pNode = createChlNode(false); 31 | pNode->bLocked = false; 32 | (*self)->pSelfNode = pNode; 33 | atomic_init(&((*self)->hTailNode), (*self)->pSelfNode); 34 | //(*self)->pLockNode = NULL; 35 | } 36 | 37 | void clhLock_destroy(clhLock_tt* self) 38 | { 39 | free((*self)->pSelfNode); 40 | free(*self); 41 | *self = NULL; 42 | } 43 | 44 | static void tls_chlNode_cleanup_func(void* pArg) 45 | { 46 | free(pArg); 47 | } 48 | 49 | static _decl_threadLocal chlNode_tt* s_pLocalNode = NULL; 50 | static _decl_threadLocal chlNode_tt* s_pPrevNode = NULL; 51 | 52 | void clhLock_lock(clhLock_tt* self) 53 | { 54 | if (s_pLocalNode == NULL) { 55 | s_pLocalNode = createChlNode(true); 56 | setTlsValue(*self, tls_chlNode_cleanup_func, s_pLocalNode, true); 57 | } 58 | 59 | s_pLocalNode->bLocked = true; 60 | 61 | s_pPrevNode = (chlNode_tt*)atomic_exchange(&((*self)->hTailNode), s_pLocalNode); 62 | while (s_pPrevNode->bLocked) { 63 | threadYield(); 64 | } 65 | 66 | // while(s_pPrevNode->bLocked) 67 | //{ 68 | // cpu_relax(); 69 | // if(!s_pPrevNode->bLocked) 70 | // { 71 | // break; 72 | // } 73 | // threadYield(); 74 | // } 75 | } 76 | 77 | void clhLock_unlock(clhLock_tt* self) 78 | { 79 | (*self)->pSelfNode = s_pLocalNode; 80 | s_pLocalNode = s_pPrevNode; 81 | setTlsValue(*self, tls_chlNode_cleanup_func, s_pLocalNode, false); 82 | (*self)->pSelfNode->bLocked = false; 83 | } 84 | -------------------------------------------------------------------------------- /src/core/include/msgpack/msgpackDecode_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include "platform_t.h" 9 | 10 | typedef enum 11 | { 12 | eMsgpackInteger, 13 | eMsgpackBoolean, 14 | eMsgpackReal, 15 | eMsgpackString, 16 | eMsgpackBinary, 17 | eMsgpackArray, 18 | eMsgpackMap, 19 | eMsgpackNil, 20 | eMsgpackUserPointer, 21 | eMsgpackInvalid 22 | } enMsgpackValueType; 23 | 24 | typedef struct msgpackDecode_s 25 | { 26 | const uint8_t* pBuffer; 27 | size_t nLength; 28 | size_t nOffset; 29 | } msgpackDecode_tt; 30 | 31 | frCore_API void msgpackDecode_init(msgpackDecode_tt* pMsgpackDecode, const uint8_t* pBuffer, 32 | size_t nLength, size_t nOffset); 33 | 34 | frCore_API void msgpackDecode_clear(msgpackDecode_tt* pMsgpackDecode); 35 | 36 | frCore_API bool msgpackDecode_peekInteger(msgpackDecode_tt* pMsgpackDecode, int64_t* pValue); 37 | 38 | frCore_API bool msgpackDecode_readInteger(msgpackDecode_tt* pMsgpackDecode, int64_t* pValue); 39 | 40 | frCore_API bool msgpackDecode_readBoolean(msgpackDecode_tt* pMsgpackDecode, bool* pValue); 41 | 42 | frCore_API bool msgpackDecode_readReal(msgpackDecode_tt* pMsgpackDecode, double* pValue); 43 | 44 | frCore_API const char* msgpackDecode_readString(msgpackDecode_tt* pMsgpackDecode, int64_t* pLength); 45 | 46 | frCore_API const char* msgpackDecode_readBinary(msgpackDecode_tt* pMsgpackDecode, int64_t* pLength); 47 | 48 | frCore_API bool msgpackDecode_readArray(msgpackDecode_tt* pMsgpackDecode, int64_t* pValue); 49 | 50 | frCore_API bool msgpackDecode_readMap(msgpackDecode_tt* pMsgpackDecode, int64_t* pValue); 51 | 52 | frCore_API bool msgpackDecode_readUserPointer(msgpackDecode_tt* pMsgpackDecode, void** ppValue); 53 | 54 | frCore_API bool msgpackDecode_skipInteger(msgpackDecode_tt* pMsgpackDecode); 55 | 56 | frCore_API bool msgpackDecode_skipBoolean(msgpackDecode_tt* pMsgpackDecode); 57 | 58 | frCore_API bool msgpackDecode_skipReal(msgpackDecode_tt* pMsgpackDecode); 59 | 60 | frCore_API bool msgpackDecode_skipString(msgpackDecode_tt* pMsgpackDecode); 61 | 62 | frCore_API bool msgpackDecode_skipBinary(msgpackDecode_tt* pMsgpackDecode); 63 | 64 | frCore_API bool msgpackDecode_skipNil(msgpackDecode_tt* pMsgpackDecode); 65 | 66 | frCore_API bool msgpackDecode_isEnd(msgpackDecode_tt* pMsgpackDecode); 67 | 68 | frCore_API enMsgpackValueType msgpackDecode_getType(msgpackDecode_tt* pMsgpackDecode); 69 | -------------------------------------------------------------------------------- /src/lua/service/example/example_crypt.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local lcrypt = require "lruntime.crypt" 3 | 4 | serviceCore.start(function() 5 | local s = "324ergfol msdl;weqiojlkj>>?>?34234666" 6 | local d = lcrypt.base64Encode(s) 7 | local c = lcrypt.base64Decode(d) 8 | if s ~= c then 9 | serviceCore.log("example_crypt base64Decode error:".. c) 10 | end 11 | 12 | local key ="sda23234d" 13 | d = lcrypt.aesEncrypt(key,s) 14 | c = lcrypt.aesDecrypt(key,d) 15 | 16 | if s ~= c then 17 | serviceCore.log("example_crypt aesDecrypt error:".. c) 18 | end 19 | 20 | local r = lcrypt.randBytes(16) 21 | serviceCore.log("example_crypt randBytes16:".. lcrypt.base64Encode(r)) 22 | r = lcrypt.randBytes(32) 23 | serviceCore.log("example_crypt randBytes32:".. lcrypt.base64Encode(r)) 24 | r = lcrypt.randBytes(64) 25 | serviceCore.log("example_crypt randBytes64:".. lcrypt.base64Encode(r)) 26 | r = lcrypt.randBytes(128) 27 | serviceCore.log("example_crypt randBytes128:".. lcrypt.base64Encode(r)) 28 | 29 | local user = "testuser" 30 | local password = "testpassword" 31 | local salt,verifier = lcrypt.srpCreateVerifier(user,password) 32 | serviceCore.log("example_crypt salt:".. lcrypt.base64Encode(salt)) 33 | serviceCore.log("example_crypt verifer:".. lcrypt.base64Encode(verifier)) 34 | 35 | local privKey, pubKey = lcrypt.srpCreateKeyClient() 36 | serviceCore.log("example_crypt privKey:".. lcrypt.base64Encode(privKey)) 37 | serviceCore.log("example_crypt pubKey:".. lcrypt.base64Encode(pubKey)) 38 | 39 | local serverPrivKey, serverPubKey = lcrypt.srpCreateKeyServer(verifier) 40 | serviceCore.log("example_crypt serverPrivKey:".. lcrypt.base64Encode(serverPrivKey)) 41 | serviceCore.log("example_crypt serverPubKey:".. lcrypt.base64Encode(serverPubKey)) 42 | 43 | local serverSessionKey = lcrypt.srpCreateSessionKeyServer(verifier,serverPrivKey,serverPubKey,pubKey) 44 | serviceCore.log("example_crypt serverSessionKey:".. lcrypt.base64Encode(serverSessionKey)) 45 | 46 | local sessionKey = lcrypt.srpCreateSessionKeyClient(user,password,salt,privKey,pubKey,serverPubKey) 47 | serviceCore.log("example_crypt sessionKey:".. lcrypt.base64Encode(sessionKey)) 48 | if sessionKey == serverSessionKey then 49 | serviceCore.log("example_crypt srp succ") 50 | else 51 | serviceCore.log("example_crypt srp err") 52 | end 53 | 54 | serviceCore.exit() 55 | end) -------------------------------------------------------------------------------- /src/service/include/channel/channel_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "byteQueue_t.h" 6 | #include "inetAddress_t.h" 7 | #include "service_t.h" 8 | 9 | struct codecStream_s; 10 | 11 | struct eventIO_s; 12 | struct eventConnection_s; 13 | 14 | struct channel_s; 15 | typedef struct channel_s channel_tt; 16 | 17 | frService_API channel_tt* createChannel(struct eventIO_s* pEventIO, 18 | struct eventConnection_s* pEventConnection); 19 | 20 | frService_API void channel_close(channel_tt* pHandle, int32_t iDisconnectTimeoutMs); 21 | 22 | frService_API void channel_setCodecStream(channel_tt* pHandle, struct codecStream_s* pCodecStream); 23 | 24 | frService_API void channel_addref(channel_tt* pHandle); 25 | 26 | frService_API void channel_release(channel_tt* pHandle); 27 | 28 | frService_API bool channel_bind(channel_tt* pHandle, service_tt* pService, bool bKeepAlive, 29 | bool bTcpNoDelay); 30 | 31 | frService_API bool channel_isRunning(channel_tt* pHandle); 32 | 33 | frService_API bool channel_getRemoteAddr(channel_tt* pHandle, inetAddress_tt* pOutInetAddress); 34 | 35 | frService_API bool channel_getLocalAddr(channel_tt* pHandle, inetAddress_tt* pOutInetAddress); 36 | 37 | frService_API int32_t channel_send(channel_tt* pHandle, const char* pBuffer, int32_t iLength, 38 | uint32_t uiFlag, uint32_t uiToken); 39 | 40 | frService_API int32_t channel_sendMove(channel_tt* pHandle, ioBufVec_tt* pInBufVec, int32_t iCount, 41 | uint32_t uiFlag, uint32_t uiToken); 42 | 43 | frService_API int32_t channel_write(channel_tt* pHandle, const char* pBuffer, int32_t iLength, 44 | uint32_t uiToken); 45 | 46 | frService_API int32_t channel_writeMove(channel_tt* pHandle, ioBufVec_tt* pInBufVec, int32_t iCount, 47 | uint32_t uiToken); 48 | 49 | frService_API bool channel_pushService(channel_tt* pHandle, byteQueue_tt* pByteQueue, 50 | uint32_t uiLength, uint32_t uiFlag, uint32_t uiToken); 51 | 52 | frService_API uint32_t channel_getID(channel_tt* pHandle); 53 | 54 | frService_API service_tt* channel_getService(channel_tt* pHandle); 55 | 56 | frService_API int32_t channel_getWritePending(channel_tt* pHandle); 57 | 58 | frService_API size_t channel_getWritePendingBytes(channel_tt* pHandle); 59 | 60 | frService_API size_t channel_getReceiveBufLength(channel_tt* pHandle); 61 | -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/posix/eventIO-inl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | #include "queue_t.h" 9 | #include "heap_t.h" 10 | #include "thread_t.h" 11 | #include "spinLock_t.h" 12 | 13 | #include "eventIO/internal/posix/poller_t.h" 14 | #include "eventIO/eventAsync_t.h" 15 | 16 | #define DEF_USE_SPINLOCK 17 | 18 | struct eventIO_s 19 | { 20 | struct eventIOLoop_s* pEventIOLoop; 21 | uint32_t uiCocurrentThreads; 22 | uint64_t uiLoopTime; 23 | uint64_t uiTimerCounter; 24 | struct heap timerHeap; 25 | bool bTimerEventOff; 26 | bool bRunning; 27 | atomic_uint uiCocurrentRunning; 28 | atomic_int iIdleThreads; 29 | int32_t hQueuedEvent[2]; 30 | QUEUE queuedEvent; 31 | #ifdef DEF_USE_SPINLOCK 32 | spinLock_tt queuedLock; 33 | #else 34 | mutex_tt queuedLock; 35 | #endif 36 | QUEUE queuePending; 37 | mutex_tt mutex; 38 | uint64_t uiThreadId; 39 | cond_tt cond; 40 | atomic_bool bLoopRunning; 41 | atomic_int iRefCount; 42 | }; 43 | 44 | static inline bool eventIO_isRunning(struct eventIO_s* pEventIO) 45 | { 46 | return atomic_load(&pEventIO->bLoopRunning); 47 | } 48 | 49 | static inline void eventIO_postQueued(struct eventIO_s* pEventIO, eventAsync_tt* pEventAsync, 50 | void (*fnWork)(eventAsync_tt*), 51 | void (*fnCancel)(eventAsync_tt*)) 52 | { 53 | if (atomic_load(&pEventIO->bLoopRunning)) { 54 | pEventAsync->fnWork = fnWork; 55 | pEventAsync->fnCancel = fnCancel; 56 | 57 | #ifdef DEF_USE_SPINLOCK 58 | spinLock_lock(&pEventIO->queuedLock); 59 | #else 60 | mutex_lock(&pEventIO->queuedLock); 61 | #endif 62 | QUEUE_INSERT_TAIL(&pEventIO->queuedEvent, &pEventAsync->node); 63 | #ifdef DEF_USE_SPINLOCK 64 | spinLock_unlock(&pEventIO->queuedLock); 65 | #else 66 | mutex_unlock(&pEventIO->queuedLock); 67 | #endif 68 | uint64_t once = 1; 69 | #if DEF_PLATFORM == DEF_PLATFORM_LINUX 70 | write(pEventIO->hQueuedEvent[0], &once, sizeof once); 71 | #else 72 | write(pEventIO->hQueuedEvent[1], &once, sizeof once); 73 | #endif 74 | } 75 | else { 76 | if (fnCancel) { 77 | fnCancel(pEventAsync); 78 | } 79 | } 80 | } 81 | 82 | __UNUSED struct eventIOLoop_s* eventIO_connectionLoop(struct eventIO_s* pEventIO); -------------------------------------------------------------------------------- /src/core/include/openssl/ssl_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // type 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | #include "byteQueue_t.h" 10 | 11 | #define DEF_SSL_BUFFER_SIZE 16384 12 | 13 | #define DEF_SSL_SSLv2 0x0001 14 | #define DEF_SSL_SSLv3 0x0002 15 | #define DEF_SSL_TLSv1 0x0004 16 | #define DEF_SSL_TLSv1_1 0x0008 17 | #define DEF_SSL_TLSv1_2 0x0010 18 | #define DEF_SSL_TLSv1_3 0x0020 19 | 20 | frCore_API bool sslInit(); 21 | 22 | frCore_API void sslExit(); 23 | 24 | struct sslContext_s; 25 | struct sslConnection_s; 26 | 27 | typedef struct sslContext_s sslContext_tt; 28 | typedef struct sslConnection_s sslConnection_tt; 29 | 30 | frCore_API sslContext_tt* createSSLContext(uint32_t uiProtocols, void* pData); 31 | 32 | frCore_API void sslContext_addref(sslContext_tt* pSSLContext); 33 | 34 | frCore_API void sslContext_release(sslContext_tt* pSSLContext); 35 | 36 | frCore_API bool sslContext_loadCertificate(sslContext_tt* pSSLContext, const char* szCert, 37 | size_t nCertLength, char* szKey, size_t nKeyLength, 38 | const char* szPassword); 39 | 40 | frCore_API bool sslContext_ciphers(sslContext_tt* pSSLContext, const char* szCiphers, 41 | bool bPreferServerCiphers); 42 | 43 | frCore_API bool sslContext_clientCertificate(sslContext_tt* pSSLContext, const char* szCert, 44 | int32_t iDepth); 45 | 46 | frCore_API bool sslContext_trustedCertificate(sslContext_tt* pSSLContext, const char* szCert, 47 | int32_t iDepth); 48 | 49 | frCore_API bool sslContext_crl(sslContext_tt* pSSLContext, const char* szCrl); 50 | 51 | frCore_API sslConnection_tt* createSSLConnection(sslContext_tt* pSSLContext, bool bAccept); 52 | 53 | frCore_API void sslConnection_addref(sslConnection_tt* pSSLConnection); 54 | 55 | frCore_API void sslConnection_release(sslConnection_tt* pSSLConnection); 56 | 57 | frCore_API int32_t sslConnection_handshake(sslConnection_tt* pSSLConnection, const char* pBuffer, 58 | size_t nLength, byteQueue_tt* pByteQueue); 59 | 60 | frCore_API bool sslConnection_read(sslConnection_tt* pSSLConnection, const char* pBuffer, 61 | size_t nLength, byteQueue_tt* pByteQueue); 62 | 63 | frCore_API bool sslConnection_write(sslConnection_tt* pSSLConnection, const char* pBuffer, 64 | size_t nLength, byteQueue_tt* pByteQueue); 65 | -------------------------------------------------------------------------------- /src/core/include/slice_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "utility_t.h" 10 | 11 | typedef struct slice_s 12 | { 13 | // private 14 | const char* szData; 15 | size_t nLength; 16 | } slice_tt; 17 | 18 | static inline void slice_clear(slice_tt* pSlice) 19 | { 20 | pSlice->szData = NULL; 21 | pSlice->nLength = 0; 22 | } 23 | 24 | static inline void slice_set(slice_tt* pSlice, const char* szData /*=""*/, size_t n /*=0*/) 25 | { 26 | pSlice->szData = szData; 27 | pSlice->nLength = n; 28 | } 29 | 30 | static inline const char* slice_getData(slice_tt* pSlice) 31 | { 32 | return pSlice->szData; 33 | } 34 | 35 | static inline size_t slice_getLength(slice_tt* pSlice) 36 | { 37 | return pSlice->nLength; 38 | } 39 | 40 | static inline size_t slice_empty(slice_tt* pSlice) 41 | { 42 | return pSlice->nLength == 0; 43 | } 44 | 45 | static inline int32_t slice_compare(slice_tt* pSlice, slice_tt* p) 46 | { 47 | const size_t nMinLen = (pSlice->nLength < p->nLength) ? pSlice->nLength : p->nLength; 48 | int32_t r = memcmp(pSlice->szData, p->szData, nMinLen); 49 | if (r == 0) { 50 | if (pSlice->nLength < p->nLength) { 51 | r = -1; 52 | } 53 | else if (pSlice->nLength > p->nLength) { 54 | r = 1; 55 | } 56 | } 57 | return r; 58 | } 59 | 60 | static inline char* slice_toString(slice_tt* pSlice) 61 | { 62 | if (pSlice->nLength > 0) { 63 | char* p = (char*)mem_malloc(pSlice->nLength); 64 | memcpy(p, pSlice->szData, pSlice->nLength); 65 | return p; 66 | } 67 | return NULL; 68 | } 69 | 70 | static inline void slice_removeSuffix(slice_tt* pSlice, size_t n) 71 | { 72 | assert(n <= pSlice->nLength); 73 | pSlice->nLength -= n; 74 | } 75 | 76 | static inline void slice_readOffset(slice_tt* pSlice, size_t n) 77 | { 78 | assert(n <= pSlice->nLength); 79 | pSlice->szData += n; 80 | pSlice->nLength -= n; 81 | } 82 | 83 | static inline bool slice_readBytes(slice_tt* pSlice, void* pOutBytes, size_t nMaxLengthToRead, 84 | bool bPeek /*= false*/) 85 | { 86 | size_t nBytesToRead = pSlice->nLength < nMaxLengthToRead ? pSlice->nLength : nMaxLengthToRead; 87 | if (nBytesToRead == 0) { 88 | return false; 89 | } 90 | memcpy(pOutBytes, pSlice->szData, nBytesToRead); 91 | if (!bPeek) { 92 | pSlice->szData += nBytesToRead; 93 | pSlice->nLength -= nBytesToRead; 94 | } 95 | return true; 96 | } 97 | -------------------------------------------------------------------------------- /cmake/modules/build_config.cmake: -------------------------------------------------------------------------------- 1 | if(CMAKE_CONFIGURATION_TYPES) 2 | set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "set the configurations" FORCE) 3 | message(STATUS "CMAKE_CONFIGURATION_TYPES: ${CMAKE_CONFIGURATION_TYPES}") 4 | endif() 5 | 6 | # check c standard 7 | set(CMAKE_C_STANDARD 11) 8 | set(CMAKE_C_STANDARD_REQUIRED ON) 9 | 10 | #build system 11 | if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 12 | set(APPLE TRUE) 13 | if(NOT IOS) 14 | set(MACOS TRUE) 15 | endif() 16 | elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux") 17 | if(NOT ANDROID) 18 | set(LINUX TRUE) 19 | endif() 20 | elseif (${CMAKE_SYSTEM_NAME} MATCHES "Android") 21 | set(ANDROID TRUE) 22 | elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows") 23 | set(WINDOWS TRUE) 24 | else() 25 | message(FATAL_ERROR "unknown system" ) 26 | return() 27 | endif() 28 | 29 | message(STATUS "BUILD SYSTEM: ${CMAKE_SYSTEM_NAME}") 30 | message(STATUS "CMAKE_GENERATOR: ${CMAKE_GENERATOR}") 31 | 32 | #thread 33 | if(LINUX) 34 | find_package(Threads REQUIRED) 35 | endif(LINUX) 36 | 37 | if(MSVC) 38 | if(MSVC_USE_STATIC_RUNTIME_LIBRARY) 39 | set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/msvc/compiler_c_flags_overrides.cmake) 40 | set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX ${CMAKE_CURRENT_SOURCE_DIR}/cmake/msvc/compiler_cxx_flags_overrides.cmake) 41 | endif() 42 | elseif(LINUX) 43 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fpic") 44 | endif() 45 | 46 | # compiler function 47 | function(FUNCTION_COMPILE_DEFINE target) 48 | if(APPLE) 49 | target_compile_definitions(${target} 50 | PUBLIC __APPLE__ 51 | ) 52 | elseif(LINUX) 53 | target_compile_definitions(${target} 54 | PUBLIC __linux__ 55 | ) 56 | elseif(ANDROID) 57 | target_compile_definitions(${target} 58 | PUBLIC __ANDROID__ 59 | ) 60 | elseif(WINDOWS) 61 | target_compile_definitions(${target} 62 | PUBLIC WIN32 63 | PUBLIC _WIN32 64 | PUBLIC _WINDOWS 65 | PUBLIC _CRT_SECURE_NO_WARNINGS 66 | PUBLIC _SCL_SECURE_NO_WARNINGS 67 | PUBLIC _WINSOCK_DEPRECATED_NO_WARNINGS 68 | PUBLIC WIN32_LEAN_AND_MEAN 69 | PUBLIC LUA_BUILD_AS_DLL 70 | ) 71 | endif() 72 | endfunction() 73 | 74 | function(FUNCTION_COMPILE_OPTION target) 75 | if(MSVC AND MSVC_USE_STATIC_RUNTIME_LIBRARY) 76 | target_compile_options(${target} 77 | PUBLIC "/MT$<$,Debug>:d>" 78 | ) 79 | endif() 80 | endfunction() 81 | -------------------------------------------------------------------------------- /src/unittest/source/test_kqueue.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include "cmocka.h" 21 | 22 | #include 23 | #include 24 | #include "thread_t.h" 25 | #include "log_t.h" 26 | #include "time_t.h" 27 | 28 | int32_t iKqueueFd[5]; 29 | int32_t hSocket[2]; 30 | 31 | static void threadRunFunc(void* pArg) 32 | { 33 | int32_t* pk = (int32_t*)pArg; 34 | int32_t iEventCount = 32; 35 | struct kevent* pEvent = malloc(iEventCount * sizeof(struct kevent)); 36 | 37 | while (1) { 38 | int32_t iEvents = kevent(*pk, NULL, 0, pEvent, iEventCount, NULL); 39 | 40 | if (iEvents == -1) { 41 | int32_t iErrno = errno; 42 | if (iErrno != EINTR) { 43 | Log(eLog_error, "kevent error"); 44 | return; 45 | } 46 | return; 47 | } 48 | 49 | char szBuf[128]; 50 | if (recv(hSocket[1], szBuf, sizeof(szBuf), 0) > 0) { 51 | Log(eLog_error, "kevent"); 52 | } 53 | } 54 | } 55 | 56 | void kqueue_test(void** state) 57 | { 58 | 59 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, hSocket) < 0) { 60 | return; 61 | } 62 | 63 | 64 | thread_tt thread[5]; 65 | for (int32_t i = 0; i < 5; ++i) { 66 | iKqueueFd[i] = kqueue(); 67 | struct kevent event; 68 | bzero(&event, sizeof event); 69 | EV_SET(&event, hSocket[1], EVFILT_READ, EV_ADD, 0, 0, NULL); 70 | if (kevent(iKqueueFd[i], &event, 1, NULL, 0, NULL) == -1 || event.flags & EV_ERROR) { 71 | Log(eLog_error, "poller_add EVFILT_READ"); 72 | return; 73 | } 74 | assert_int_equal(thread_start(&thread[i], threadRunFunc, &(iKqueueFd[i])), eThreadSuccess); 75 | } 76 | 77 | timespec_tt timeSleep; 78 | timeSleep.iSec = 1; 79 | timeSleep.iNsec = 0; 80 | sleep_for(&timeSleep); 81 | 82 | char szBuf[1] = {}; 83 | send(hSocket[0], szBuf, sizeof(szBuf), 0); 84 | send(hSocket[0], szBuf, sizeof(szBuf), 0); 85 | send(hSocket[0], szBuf, sizeof(szBuf), 0); 86 | send(hSocket[0], szBuf, sizeof(szBuf), 0); 87 | send(hSocket[0], szBuf, sizeof(szBuf), 0); 88 | 89 | 90 | for (int32_t i = 0; i < 5; ++i) { 91 | thread_join(thread[i]); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/runtime/source/internal/ldnsResolve_t.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "internal/ldnsResolve_t.h" 4 | 5 | #include 6 | #include 7 | 8 | #include "lauxlib.h" 9 | #include "lua.h" 10 | #include "lualib.h" 11 | 12 | #include "service_t.h" 13 | 14 | static int32_t ldnsResolve_parser(lua_State* L) 15 | { 16 | ldnsResolve_tt* pDnsResolve = (ldnsResolve_tt*)luaL_checkudata(L, 1, "dnsResolve"); 17 | luaL_argcheck(L, pDnsResolve != NULL, 1, "invalid user data"); 18 | 19 | if (pDnsResolve->pHandle) { 20 | const char* pBuffer = NULL; 21 | size_t nLength = 0; 22 | int32_t iType = lua_type(L, 2); 23 | if (iType == LUA_TSTRING) { 24 | pBuffer = lua_tolstring(L, 2, &nLength); 25 | } 26 | else { 27 | if (iType != LUA_TUSERDATA && iType != LUA_TLIGHTUSERDATA) { 28 | lua_pushinteger(L, 0); 29 | return 1; 30 | } 31 | pBuffer = (const char*)lua_touserdata(L, 2); 32 | nLength = luaL_checkinteger(L, 3); 33 | } 34 | 35 | int32_t iCount = dnsResolve_parser(pDnsResolve->pHandle, pBuffer, nLength); 36 | if (iCount > 0) { 37 | lua_createtable(L, iCount, 0); 38 | luaL_checkstack(L, iCount, NULL); 39 | for (int32_t i = 0; i < iCount; i++) { 40 | const char* szAddress = dnsResolve_getAddress(pDnsResolve->pHandle, i); 41 | lua_pushlstring(L, szAddress, strlen(szAddress)); 42 | lua_rawseti(L, -2, i + 1); 43 | } 44 | return 1; 45 | } 46 | } 47 | return 0; 48 | } 49 | 50 | static int32_t ldnsResolve_gc(lua_State* L) 51 | { 52 | ldnsResolve_tt* pDnsResolve = (ldnsResolve_tt*)luaL_checkudata(L, 1, "dnsResolve"); 53 | luaL_argcheck(L, pDnsResolve != NULL, 1, "invalid user data"); 54 | if (pDnsResolve->pHandle) { 55 | dnsResolve_close(pDnsResolve->pHandle); 56 | dnsResolve_release(pDnsResolve->pHandle); 57 | pDnsResolve->pHandle = NULL; 58 | } 59 | return 0; 60 | } 61 | 62 | int32_t registerDnsResolveL(lua_State* L) 63 | { 64 | luaL_newmetatable(L, "dnsResolve"); 65 | /* metatable.__index = metatable */ 66 | lua_pushvalue(L, -1); 67 | lua_setfield(L, -2, "__index"); 68 | 69 | struct luaL_Reg lua_dnsResolveFuncs[] = {{"parser", ldnsResolve_parser}, 70 | {"close", ldnsResolve_gc}, 71 | {"__close", ldnsResolve_gc}, 72 | {"__gc", ldnsResolve_gc}, 73 | {NULL, NULL}}; 74 | 75 | luaL_setfuncs(L, lua_dnsResolveFuncs, 0); 76 | return 1; 77 | }; -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/win/iocpExt_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "utility_t.h" 11 | 12 | typedef LONG NTSTATUS; 13 | 14 | #ifndef NT_SUCCESS 15 | # define NT_SUCCESS(status) ((NTSTATUS)(status) >= 0) 16 | #endif 17 | 18 | #define def_IOCP_QUEUED_QUIT 0 19 | #define def_IOCP_CONNECTION 1 20 | #define def_IOCP_ACCEPT 2 21 | #define def_IOCP_RECVFROM 3 22 | #define def_IOCP_EVENT 4 23 | #define def_IOCP_TASK 5 24 | 25 | typedef BOOL(WINAPI* AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, 26 | LPOVERLAPPED); 27 | typedef void(WINAPI* GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR*, LPINT, 28 | LPSOCKADDR*, LPINT); 29 | typedef BOOL(WINAPI* ConnectExPtr)(SOCKET, const struct sockaddr*, int, PVOID, DWORD, LPDWORD, 30 | LPOVERLAPPED); 31 | typedef BOOL(WINAPI* DisconnectExPtr)(SOCKET, LPOVERLAPPED, DWORD, DWORD); 32 | typedef BOOL(WINAPI* GetQueuedCompletionStatusExPtr)(HANDLE, LPOVERLAPPED_ENTRY, ULONG, PULONG, 33 | DWORD, BOOL); 34 | typedef BOOL(WINAPI* CancelIoExPtr)(HANDLE, LPOVERLAPPED); 35 | 36 | __UNUSED BOOL acceptEx(SOCKET sListenSocket, SOCKET sAcceptSocket, PVOID lpOutputBuffer, 37 | DWORD dwReceiveDataLength, DWORD dwLocalAddressLength, 38 | DWORD dwRemoteAddressLength, LPDWORD lpdwBytesReceived, 39 | LPOVERLAPPED lpOverlapped); 40 | 41 | __UNUSED void getAcceptExSockaddrs(PVOID lpOutputBuffer, DWORD dwReceiveDataLength, 42 | DWORD dwLocalAddressLength, DWORD dwRemoteAddressLength, 43 | LPSOCKADDR* LocalSockaddr, LPINT LocalSockaddrLength, 44 | LPSOCKADDR* RemoteSockaddr, LPINT RemoteSockaddrLength); 45 | 46 | __UNUSED BOOL connectEx(SOCKET s, const struct sockaddr* name, int namelen, PVOID lpSendBuffer, 47 | DWORD dwSendDataLength, LPDWORD lpdwBytesSent, LPOVERLAPPED lpOverlapped); 48 | 49 | __UNUSED BOOL disconnectEx(SOCKET hSocket, LPOVERLAPPED lpOverlapped, DWORD dwFlags, 50 | DWORD reserved); 51 | 52 | __UNUSED BOOL getQueuedCompletionStatusEx(HANDLE CompletionPort, 53 | LPOVERLAPPED_ENTRY lpCompletionPortEntries, ULONG ulCount, 54 | PULONG ulNumEntriesRemoved, DWORD dwMilliseconds, 55 | BOOL bAlertable); 56 | 57 | __UNUSED BOOL cancelIoEx(HANDLE hFile, LPOVERLAPPED lpOverlapped); -------------------------------------------------------------------------------- /src/core/include/cxx-mem/stl-override_t.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "stl_allocator_t.hpp" 4 | 5 | #ifdef __cplusplus 6 | # include 7 | # include 8 | # include 9 | # include 10 | # include 11 | # include 12 | # include 13 | # include 14 | # include 15 | # include 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include 25 | # include 26 | 27 | template using forward_list = typename std::forward_list<_Ty, mem_stl_allocator<_Ty>>; 28 | 29 | template using vector = typename std::vector<_Ty, mem_stl_allocator<_Ty>>; 30 | 31 | template using list = typename std::list<_Ty, mem_stl_allocator<_Ty>>; 32 | 33 | template using deque = typename std::deque<_Ty, mem_stl_allocator<_Ty>>; 34 | 35 | template using queue = typename std::queue<_Ty, deque<_Ty>>; 36 | 37 | template using stack = typename std::stack<_Ty, deque<_Ty>>; 38 | 39 | template> 40 | using map = typename std::map<_Kty, _Ty, _Pr, mem_stl_allocator>>; 41 | 42 | template> 43 | using multimap = 44 | typename std::multimap<_Kty, _Ty, _Pr, mem_stl_allocator>>; 45 | 46 | template> 47 | using set = typename std::set<_Kty, _Pr, mem_stl_allocator<_Kty>>; 48 | 49 | template> 50 | using multiset = typename std::multiset<_Kty, _Pr, mem_stl_allocator<_Kty>>; 51 | 52 | template, class _Keyeq = std::equal_to<_Kty>> 53 | using unordered_map = typename std::unordered_map<_Kty, _Ty, _Hasher, _Keyeq, 54 | mem_stl_allocator>>; 55 | 56 | template, class _Keyeq = std::equal_to<_Kty>> 57 | using unordered_set = typename std::unordered_set<_Kty, _Hasher, _Keyeq, mem_stl_allocator<_Kty>>; 58 | 59 | typedef std::basic_string, mem_stl_allocator> string; 60 | 61 | typedef std::basic_string, mem_stl_allocator> wstring; 62 | 63 | typedef std::basic_ostringstream, mem_stl_allocator> 64 | ostringstream; 65 | 66 | typedef std::basic_stringstream, mem_stl_allocator> stringstream; 67 | 68 | #endif // __cplusplus -------------------------------------------------------------------------------- /src/core/include/time_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | #include "platform_t.h" 10 | 11 | typedef struct timespec_s 12 | { 13 | int32_t iSec; 14 | int32_t iNsec; 15 | } timespec_tt; 16 | 17 | #define timespec_cmp(pTvp, pUvp, cmp) \ 18 | (((pTvp)->iSec == (pUvp)->iSec) ? ((pTvp)->iNsec cmp(pUvp)->iNsec) \ 19 | : ((pTvp)->iSec cmp(pUvp)->iSec)) 20 | 21 | #define timespec_add(pTvp, pUvp, pVvp) \ 22 | { \ 23 | (pVvp)->iSec = (pTvp)->iSec + (pUvp)->iSec; \ 24 | (pVvp)->iNsec = (pTvp)->iNsec + (pUvp)->iNsec; \ 25 | if ((pVvp)->iNsec > 1000000000l) { \ 26 | ++((pVvp)->iSec); \ 27 | (pVvp)->iNsec -= 1000000000l; \ 28 | } \ 29 | } 30 | 31 | #define timespec_sub(pTvp, pUvp, pVvp) \ 32 | { \ 33 | (pVvp)->iSec = (pTvp)->iSec - (pUvp)->iSec; \ 34 | (pVvp)->iNsec = (pTvp)->iNsec - (pUvp)->iNsec; \ 35 | if ((pVvp)->iNsec < 0) { \ 36 | --((pVvp)->iSec); \ 37 | (pVvp)->iNsec += 1000000000l; \ 38 | } \ 39 | } 40 | 41 | #define timespec_clear(pTs) ((pTs)->iSec = (pTs)->iNsec = 0) 42 | 43 | #define timespec_zero(pTs) ((pTs)->iSec == 0) && ((pTs)->iNsec == 0) 44 | 45 | #define timespec_toMsec(pTs) (((int64_t)(pTs)->iSec * 1000) + (((pTs)->iNsec + 999999) / 1000000)) 46 | 47 | #define timespec_toNsec(pTs) (((int64_t)(pTs)->iSec) * 1000000000L + (pTs)->iNsec) 48 | 49 | #define timespec_addToMs(pTvp, pUvp) \ 50 | (((((int64_t)(pTvp)->iSec + (int64_t)(pUvp)->iSec)) * 1000) + \ 51 | (((pTvp)->iNsec + (pUvp)->iNsec + 999999) / 1000000)) 52 | 53 | #define timespec_subToMs(pTvp, pUvp) \ 54 | ((((int64_t)(pTvp)->iSec - (int64_t)(pUvp)->iSec) * 1000) + \ 55 | (((pTvp)->iNsec - (pUvp)->iNsec + 999999) / 1000000)) 56 | 57 | #define timespec_addToNs(pTvp, pUvp) \ 58 | (((int64_t)(pTvp)->iSec + (int64_t)(pUvp)->iSec) * 1000000000L + \ 59 | ((pTvp)->iNsec + (pUvp)->iNsec)) 60 | 61 | #define timespec_subToNs(pTvp, pUvp) \ 62 | (((int64_t)(pTvp)->iSec - (int64_t)(pUvp)->iSec) * 1000000000L + \ 63 | ((pTvp)->iNsec - (pUvp)->iNsec)) 64 | 65 | frCore_API void getClockRealtime(timespec_tt* pTs); 66 | 67 | frCore_API void getClockMonotonic(timespec_tt* pTs); 68 | -------------------------------------------------------------------------------- /src/core/source/eventIO/eventTimer_t.c: -------------------------------------------------------------------------------- 1 | #include "eventIO/internal/eventTimer_t.h" 2 | #include "eventIO/eventIO_t.h" 3 | 4 | eventTimer_tt* createEventTimer(eventIO_tt* pEventIO, void (*fn)(eventTimer_tt*, void*), bool bOnce, 5 | uint32_t uiIntervalMs, void* pUserData) 6 | { 7 | eventTimer_tt* pHandle = (eventTimer_tt*)mem_malloc(sizeof(eventTimer_tt)); 8 | pHandle->pEventIO = pEventIO; 9 | pHandle->bOnce = bOnce; 10 | pHandle->pUserData = pUserData; 11 | pHandle->uiID = 0xffffffffffffffff; 12 | pHandle->uiIntervalMs = uiIntervalMs; 13 | pHandle->uiTimeout = 0; 14 | pHandle->fn = fn; 15 | pHandle->fnCloseCallback = NULL; 16 | pHandle->bActive = false; 17 | atomic_init(&pHandle->bRunning, false); 18 | atomic_init(&pHandle->iRefCount, 1); 19 | 20 | return pHandle; 21 | } 22 | 23 | void eventTimer_addref(eventTimer_tt* pHandle) 24 | { 25 | atomic_fetch_add(&(pHandle->iRefCount), 1); 26 | } 27 | 28 | void eventTimer_release(eventTimer_tt* pHandle) 29 | { 30 | if (atomic_fetch_sub(&(pHandle->iRefCount), 1) == 1) { 31 | mem_free(pHandle); 32 | } 33 | } 34 | 35 | void eventTimer_setCloseCallback(eventTimer_tt* pHandle, void (*fn)(eventTimer_tt*, void*)) 36 | { 37 | pHandle->fnCloseCallback = fn; 38 | } 39 | 40 | bool eventTimer_start(eventTimer_tt* pHandle) 41 | { 42 | bool bRunning = false; 43 | if (atomic_compare_exchange_strong(&pHandle->bRunning, &bRunning, true)) { 44 | atomic_fetch_add(&pHandle->iRefCount, 1); 45 | eventTimerAsync_tt* pEventTimerAsync = mem_malloc(sizeof(eventTimerAsync_tt)); 46 | pEventTimerAsync->pEventTimer = pHandle; 47 | eventIO_runInLoop(pHandle->pEventIO, 48 | &pEventTimerAsync->eventAsync, 49 | inLoop_eventTimer_start, 50 | inLoop_eventTimer_cancel); 51 | return true; 52 | } 53 | return false; 54 | } 55 | 56 | void eventTimer_stop(eventTimer_tt* pHandle) 57 | { 58 | bool bRunning = true; 59 | if (atomic_compare_exchange_strong(&pHandle->bRunning, &bRunning, false)) { 60 | eventTimerAsync_tt* pEventTimerAsync = mem_malloc(sizeof(eventTimerAsync_tt)); 61 | pEventTimerAsync->pEventTimer = pHandle; 62 | atomic_fetch_add(&(pHandle->iRefCount), 1); 63 | eventIO_runInLoop(pHandle->pEventIO, 64 | &pEventTimerAsync->eventAsync, 65 | inLoop_eventTimer_stop, 66 | inLoop_eventTimer_stop); 67 | } 68 | } 69 | 70 | bool eventTimer_isRunning(eventTimer_tt* pHandle) 71 | { 72 | return atomic_load(&pHandle->bRunning); 73 | } 74 | 75 | bool eventTimer_isOnce(eventTimer_tt* pHandle) 76 | { 77 | return pHandle->bOnce; 78 | } 79 | -------------------------------------------------------------------------------- /src/unittest/source/test_time.cc: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | extern "C" { 8 | #include "time_t.h" 9 | #include "thread_t.h" 10 | } 11 | 12 | TEST(time, test_0) 13 | { 14 | timespec_tt time1; 15 | timespec_tt time2; 16 | timespec_tt time3; 17 | getClockMonotonic(&time1); 18 | timespec_tt timeSleep; 19 | timeSleep.iSec = 1; 20 | timeSleep.iNsec = 0; 21 | sleep_for(&timeSleep); 22 | getClockMonotonic(&time2); 23 | timespec_sub(&time2,&time1,&time3) 24 | int64_t iTimer1 = timespec_toMsec(&time1); 25 | int64_t iTimer2 = timespec_toMsec(&time2); 26 | int64_t iTimer3 = timespec_toMsec(&time3); 27 | 28 | printf("cur:%" PRIi64"ms last:%" PRIi64" ms sleep:%" PRIi64" ms\n",iTimer1,iTimer2,iTimer3); 29 | int64_t iTimer1Ns = timespec_toNsec(&time1); 30 | int64_t iTimer2Ns = timespec_toNsec(&time2); 31 | int64_t iTimer3Ns = timespec_toNsec(&time3); 32 | printf("cur:%" PRIi64" ns last:%" PRIi64" ns sleep:%" PRIi64" ns\n",iTimer1Ns,iTimer2Ns,iTimer3Ns); 33 | } 34 | 35 | TEST(time, test_1) 36 | { 37 | timespec_tt time1; 38 | timespec_tt time2; 39 | timespec_tt time3; 40 | getClockRealtime(&time1); 41 | timespec_tt timeSleep; 42 | timeSleep.iSec = 1; 43 | timeSleep.iNsec = 0; 44 | sleep_for(&timeSleep); 45 | getClockRealtime(&time2); 46 | timespec_sub(&time2,&time1,&time3) 47 | int64_t iTimer1 = timespec_toMsec(&time1); 48 | int64_t iTimer2 = timespec_toMsec(&time2); 49 | int64_t iTimer3 = timespec_toMsec(&time3); 50 | 51 | printf("cur:%" PRIi64" ms last:%" PRIi64" ms sleep:%" PRIi64" ms\n",iTimer1,iTimer2,iTimer3); 52 | int64_t iTimer1Ns = timespec_toNsec(&time1); 53 | int64_t iTimer2Ns = timespec_toNsec(&time2); 54 | int64_t iTimer3Ns = timespec_toNsec(&time3); 55 | printf("cur:%" PRIi64" ns last:%" PRIi64" ns sleep:%" PRIi64" ns\n",iTimer1Ns,iTimer2Ns,iTimer3Ns); 56 | } 57 | 58 | TEST(time, test_2) 59 | { 60 | timespec_tt time = {10000,5000000}; 61 | timespec_tt time2 = {100000,15000000}; 62 | int64_t iTime = timespec_addToMs(&time,&time2); 63 | ASSERT_EQ(iTime,110000020); 64 | int64_t iTime2 = timespec_addToNs(&time,&time2); 65 | ASSERT_EQ(iTime2,110000020000000); 66 | int64_t iTime3 = timespec_subToMs(&time2,&time); 67 | ASSERT_EQ(iTime3,90000010); 68 | int64_t iTime4 = timespec_subToNs(&time2,&time); 69 | ASSERT_EQ(iTime4,90000010000000); 70 | } 71 | 72 | TEST(time, test_3) 73 | { 74 | timespec_tt time = {10000,5000000}; 75 | timespec_tt time2 = {100000,15000000}; 76 | timespec_tt time3 = {0,0}; 77 | timespec_add(&time,&time2,&time3); 78 | int64_t iTime = timespec_toMsec(&time3); 79 | ASSERT_EQ(iTime,110000020); 80 | int64_t iTime2 = timespec_toNsec(&time3); 81 | ASSERT_EQ(iTime2,110000020000000); 82 | timespec_sub(&time2,&time,&time3); 83 | int64_t iTime3 = timespec_toMsec(&time3); 84 | ASSERT_EQ(iTime3,90000010); 85 | int64_t iTime4 = timespec_toNsec(&time3); 86 | ASSERT_EQ(iTime4,90000010000000); 87 | } 88 | -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_multipart_parser.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "multipart-parser") 2 | set(BUILD_3RDPARTY_FILENAME "multipart-parser-c-1.0.0.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(multipart-parser)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | execute_process(COMMAND ${CMAKE_COMMAND} 75 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 76 | -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} 77 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 78 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 79 | -DCMAKE_USER_MAKE_RULES_OVERRIDE=${CMAKE_USER_MAKE_RULES_OVERRIDE} 80 | -G ${CMAKE_GENERATOR} 81 | ${3RDPARTY_BUILD_COMMAND} 82 | ${directory} 83 | WORKING_DIRECTORY ${directory}/build) 84 | 85 | execute_process(COMMAND ${CMAKE_COMMAND} 86 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) 87 | -------------------------------------------------------------------------------- /src/unittest/source/test_inetAddress.cc: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | 3 | #include 4 | #include 5 | 6 | extern "C" { 7 | #include "inetAddress_t.h" 8 | } 9 | 10 | TEST(inetAddress, ipv4_test_0) 11 | { 12 | inetAddress_tt inetAddress; 13 | inetAddress_init_fromIpPort(&inetAddress,"127.0.0.1:9978"); 14 | EXPECT_EQ(inetAddress_getNetworkPort(&inetAddress),htons(9978)); 15 | EXPECT_EQ(inetAddress_getPort(&inetAddress),9978); 16 | printf("ip:%x\n",inetAddress_getNetworkIP(&inetAddress)); 17 | 18 | char szBuffer[64]; 19 | EXPECT_TRUE(inetAddress_toIPString(&inetAddress,szBuffer,64)); 20 | EXPECT_STREQ(szBuffer,"127.0.0.1"); 21 | EXPECT_TRUE(inetAddress_isIpV4(&inetAddress)); 22 | 23 | char szBuffer2[64]; 24 | EXPECT_TRUE(inetAddress_toIPPortString(&inetAddress,szBuffer2,64)); 25 | EXPECT_STREQ(szBuffer2,"127.0.0.1:9978"); 26 | } 27 | 28 | TEST(inetAddress, ipv4_test_1) 29 | { 30 | inetAddress_tt inetAddress; 31 | inetAddress_init_fromIpPort(&inetAddress,"192.168.1.25:9978"); 32 | EXPECT_EQ(inetAddress_getNetworkPort(&inetAddress),htons(9978)); 33 | EXPECT_EQ(inetAddress_getPort(&inetAddress),9978); 34 | printf("ip:%x\n",inetAddress_getNetworkIP(&inetAddress)); 35 | 36 | char szBuffer[64]; 37 | EXPECT_TRUE(inetAddress_toIPString(&inetAddress,szBuffer,64)); 38 | EXPECT_STREQ(szBuffer,"192.168.1.25"); 39 | EXPECT_TRUE(inetAddress_isIpV4(&inetAddress)); 40 | 41 | char szBuffer2[64]; 42 | EXPECT_TRUE(inetAddress_toIPPortString(&inetAddress,szBuffer2,64)); 43 | EXPECT_STREQ(szBuffer2,"192.168.1.25:9978"); 44 | } 45 | 46 | TEST(inetAddress, ipv6_test_0) 47 | { 48 | inetAddress_tt inetAddress; 49 | inetAddress_init_fromIpPort(&inetAddress,"abcd:ef01:2345:6789:abcd:ef01:2345:6789:9978"); 50 | EXPECT_EQ(inetAddress_getNetworkPort(&inetAddress),htons(9978)); 51 | EXPECT_EQ(inetAddress_getPort(&inetAddress),9978); 52 | 53 | char szBuffer[64]; 54 | EXPECT_TRUE(inetAddress_toIPString(&inetAddress,szBuffer,64)); 55 | EXPECT_STREQ(szBuffer,"abcd:ef01:2345:6789:abcd:ef01:2345:6789"); 56 | EXPECT_TRUE(inetAddress_isIpV6(&inetAddress)); 57 | 58 | char szBuffer2[64]; 59 | EXPECT_TRUE(inetAddress_toIPPortString(&inetAddress,szBuffer2,64)); 60 | EXPECT_STREQ(szBuffer2,"abcd:ef01:2345:6789:abcd:ef01:2345:6789:9978"); 61 | } 62 | 63 | TEST(inetAddress, ipv6_test_1) 64 | { 65 | inetAddress_tt inetAddress; 66 | inetAddress_init_fromIpPort(&inetAddress,"[abcd:ef01:2345:6789:abcd:ef01:2345:6789]:9978"); 67 | EXPECT_EQ(inetAddress_getNetworkPort(&inetAddress),htons(9978)); 68 | EXPECT_EQ(inetAddress_getPort(&inetAddress),9978); 69 | 70 | char szBuffer[64]; 71 | EXPECT_TRUE(inetAddress_toIPString(&inetAddress,szBuffer,64)); 72 | EXPECT_STREQ(szBuffer,"abcd:ef01:2345:6789:abcd:ef01:2345:6789"); 73 | EXPECT_TRUE(inetAddress_isIpV6(&inetAddress)); 74 | 75 | char szBuffer2[64]; 76 | EXPECT_TRUE(inetAddress_toIPPortString(&inetAddress,szBuffer2,64)); 77 | EXPECT_STREQ(szBuffer2,"abcd:ef01:2345:6789:abcd:ef01:2345:6789:9978"); 78 | } -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_benchmark.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "benchmark") 2 | set(BUILD_3RDPARTY_FILENAME "benchmark-1.5.0.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(benchmark)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | execute_process(COMMAND ${CMAKE_COMMAND} 75 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 76 | -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} 77 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 78 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 79 | -DCMAKE_USER_MAKE_RULES_OVERRIDE=${CMAKE_USER_MAKE_RULES_OVERRIDE_CXX} 80 | -G ${CMAKE_GENERATOR} 81 | -DBENCHMARK_ENABLE_TESTING=OFF 82 | ${3RDPARTY_BUILD_COMMAND} 83 | ${directory} 84 | WORKING_DIRECTORY ${directory}/build) 85 | 86 | execute_process(COMMAND ${CMAKE_COMMAND} 87 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) 88 | -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_mysql_parser.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "mysql-parser") 2 | set(BUILD_3RDPARTY_FILENAME "mysql-parser-1.0.0.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(mysql-parser)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | execute_process(COMMAND ${CMAKE_COMMAND} 75 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 76 | -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} 77 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 78 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 79 | -DCMAKE_GENERATOR_TOOLSET=${CMAKE_GENERATOR_TOOLSET} 80 | -DCMAKE_USER_MAKE_RULES_OVERRIDE=${CMAKE_USER_MAKE_RULES_OVERRIDE} 81 | -G ${CMAKE_GENERATOR} 82 | ${3RDPARTY_BUILD_COMMAND} 83 | ${directory} 84 | WORKING_DIRECTORY ${directory}/build) 85 | 86 | execute_process(COMMAND ${CMAKE_COMMAND} 87 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) 88 | -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_redis_parser.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "redis-parser") 2 | set(BUILD_3RDPARTY_FILENAME "redis-parser-1.0.0.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(redis-parser)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | execute_process(COMMAND ${CMAKE_COMMAND} 75 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 76 | -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} 77 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 78 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 79 | -DCMAKE_GENERATOR_TOOLSET=${CMAKE_GENERATOR_TOOLSET} 80 | -DCMAKE_USER_MAKE_RULES_OVERRIDE=${CMAKE_USER_MAKE_RULES_OVERRIDE} 81 | -G ${CMAKE_GENERATOR} 82 | ${3RDPARTY_BUILD_COMMAND} 83 | ${directory} 84 | WORKING_DIRECTORY ${directory}/build) 85 | 86 | execute_process(COMMAND ${CMAKE_COMMAND} 87 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) 88 | -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_http_parser.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "http-parser") 2 | set(BUILD_3RDPARTY_FILENAME "http-parser-2.9.2.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(http-parser)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | execute_process(COMMAND ${CMAKE_COMMAND} 75 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 76 | -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} 77 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 78 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 79 | -DCMAKE_GENERATOR_TOOLSET=${CMAKE_GENERATOR_TOOLSET} 80 | -DCMAKE_USER_MAKE_RULES_OVERRIDE=${CMAKE_USER_MAKE_RULES_OVERRIDE} 81 | -DBUILD_SHARED_LIBS=FALSE 82 | -G ${CMAKE_GENERATOR} 83 | ${3RDPARTY_BUILD_COMMAND} 84 | ${directory} 85 | WORKING_DIRECTORY ${directory}/build) 86 | 87 | execute_process(COMMAND ${CMAKE_COMMAND} 88 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) 89 | -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_googletest.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "googletest") 2 | set(BUILD_3RDPARTY_FILENAME "googletest-release-1.8.1.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(googletest)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | if(MSVC) 75 | if(NOT MSVC_USE_STATIC_RUNTIME_LIBRARY) 76 | set(GOOGLE_TEST_CONFIG -Dgtest_force_shared_crt=ON) 77 | endif() 78 | endif() 79 | 80 | execute_process(COMMAND ${CMAKE_COMMAND} 81 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 82 | -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} 83 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 84 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 85 | -G ${CMAKE_GENERATOR} 86 | ${GOOGLE_TEST_CONFIG} 87 | ${3RDPARTY_BUILD_COMMAND} 88 | ${directory} 89 | WORKING_DIRECTORY ${directory}/build) 90 | 91 | execute_process(COMMAND ${CMAKE_COMMAND} 92 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) 93 | -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_mimalloc.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "mimalloc") 2 | set(BUILD_3RDPARTY_FILENAME "mimalloc-1.6.3.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(mimalloc)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | execute_process(COMMAND ${CMAKE_COMMAND} 75 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 76 | -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} 77 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 78 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 79 | -DCMAKE_USER_MAKE_RULES_OVERRIDE=${CMAKE_USER_MAKE_RULES_OVERRIDE_CXX} 80 | -G ${CMAKE_GENERATOR} 81 | ${3RDPARTY_BUILD_COMMAND} 82 | -DMI_BUILD_SHARED=OFF 83 | -DMI_BUILD_OBJECT=OFF 84 | -DMI_BUILD_TESTS=OFF 85 | ${directory} 86 | WORKING_DIRECTORY ${directory}/build) 87 | 88 | execute_process(COMMAND ${CMAKE_COMMAND} 89 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) -------------------------------------------------------------------------------- /cmake/modules/3rdparty/build_cares.cmake: -------------------------------------------------------------------------------- 1 | set(BUILD_3RDPARTY_NAME "cares") 2 | set(BUILD_3RDPARTY_FILENAME "c-ares-cares-1_15_0.tar.gz") 3 | 4 | message(STATUS "BUILD NAME:(cares)") 5 | 6 | get_filename_component(filename "${FROG_ROOT_PATH}/dependent/${BUILD_3RDPARTY_FILENAME}" ABSOLUTE) 7 | get_filename_component(directory "${FROG_3RDPARTY_BINARY_DIR}/source/${BUILD_3RDPARTY_NAME}" ABSOLUTE) 8 | 9 | message(STATUS "extracting... 10 | src='${filename}' 11 | dst='${directory}'") 12 | 13 | if(NOT EXISTS "${filename}") 14 | message(FATAL_ERROR "error: file to extract does not exist: '${filename}'") 15 | endif() 16 | 17 | # Prepare a space for extracting: 18 | # 19 | set(i 1234) 20 | 21 | while(EXISTS "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 22 | math(EXPR i "${i} + 1") 23 | endwhile() 24 | 25 | set(ut_dir "${directory}/../ex-${BUILD_3RDPARTY_NAME}${i}") 26 | file(MAKE_DIRECTORY "${ut_dir}") 27 | 28 | # Extract it: 29 | # 30 | message(STATUS "extracting... [tar xfz]") 31 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xfz ${filename} 32 | WORKING_DIRECTORY ${ut_dir} 33 | RESULT_VARIABLE rv) 34 | 35 | if(NOT rv EQUAL 0) 36 | message(STATUS "extracting... [error clean up]") 37 | file(REMOVE_RECURSE "${ut_dir}") 38 | message(FATAL_ERROR "error: extract of '${filename}' failed") 39 | endif() 40 | 41 | # Analyze what came out of the tar file: 42 | # 43 | message(STATUS "extracting... [analysis]") 44 | file(GLOB contents "${ut_dir}/*") 45 | list(REMOVE_ITEM contents "${ut_dir}/.DS_Store") 46 | list(LENGTH contents n) 47 | 48 | if(NOT n EQUAL 1 OR NOT IS_DIRECTORY "${contents}") 49 | set(contents "${ut_dir}") 50 | endif() 51 | 52 | # Move "the one" directory to the final directory: 53 | # 54 | message(STATUS "extracting... [rename]") 55 | file(REMOVE_RECURSE ${directory}) 56 | get_filename_component(contents ${contents} ABSOLUTE) 57 | file(RENAME ${contents} ${directory}) 58 | 59 | # Clean up: 60 | # 61 | message(STATUS "extracting... [clean up]") 62 | file(REMOVE_RECURSE "${ut_dir}") 63 | 64 | message(STATUS "extracting... done") 65 | 66 | file(MAKE_DIRECTORY ${directory}/build) 67 | 68 | if(DEBUG_3RDPARTY_OUTPUT) 69 | set(BUILD_COMMAND_OPTS --target install --config Debug) 70 | else() 71 | set(BUILD_COMMAND_OPTS --target install --config Release) 72 | endif() 73 | 74 | set(CARES_CONFIG -DCARES_SHARED=OFF -DCARES_STATIC=ON -DCARES_STATIC_PIC=ON -DCARES_BUILD_TOOLS=OFF) 75 | 76 | if(MSVC AND MSVC_USE_STATIC_RUNTIME_LIBRARY) 77 | set(CARES_CONFIG ${CARES_CONFIG} -DCARES_MSVC_STATIC_RUNTIME=ON) 78 | endif() 79 | 80 | execute_process(COMMAND ${CMAKE_COMMAND} 81 | -DCMAKE_INSTALL_PREFIX=${FROG_3RDPARTY_BINARY_DIR}/install/${BUILD_3RDPARTY_NAME} 82 | -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} 83 | -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} 84 | -DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM} 85 | -G ${CMAKE_GENERATOR} 86 | ${CARES_CONFIG} 87 | ${3RDPARTY_BUILD_COMMAND} 88 | ${directory} 89 | WORKING_DIRECTORY ${directory}/build) 90 | 91 | execute_process(COMMAND ${CMAKE_COMMAND} 92 | --build ${directory}/build ${BUILD_COMMAND_OPTS} WORKING_DIRECTORY ${directory}/build) 93 | -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/posix/eventConnection_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "eventIO/internal/posix/poller_t.h" 6 | #include "eventIO/internal/posix/eventIOLoop_t.h" 7 | #include "inetAddress_t.h" 8 | #include "byteQueue_t.h" 9 | 10 | typedef enum 11 | { 12 | eDisconnected, 13 | eConnecting, 14 | eConnected, 15 | eDisconnecting 16 | } enEventConnectionStatus; 17 | 18 | struct eventConnection_s; 19 | 20 | struct eventBuf_s 21 | { 22 | void (*fnCallback)(struct eventConnection_s*, void*, bool, uintptr_t); 23 | eventAsync_tt eventAsync; 24 | struct eventConnection_s* pEventConnection; 25 | uint32_t uiLength; 26 | uintptr_t uiWriteUser; 27 | char szStorage[]; 28 | }; 29 | 30 | typedef void (*disconnectCallbackPtr)(struct eventConnection_s*, void*); 31 | 32 | struct eventConnection_s 33 | { 34 | bool (*fnReceiveCallback)(struct eventConnection_s*, byteQueue_tt*, void*); 35 | void (*fnConnectorCallback)(struct eventConnection_s*, void*); 36 | void (*fnCloseCallback)(struct eventConnection_s*, void*); 37 | void (*fnUserFree)(void*); 38 | _Atomic(disconnectCallbackPtr) hDisconnectCallback; 39 | void* pUserData; 40 | pollHandle_tt pollHandle; 41 | eventIOLoop_tt* pEventIOLoop; 42 | int32_t hSocket; 43 | inetAddress_tt remoteAddr; 44 | inetAddress_tt localAddr; 45 | struct eventListenPort_s* pListenPort; 46 | bool bTcp; 47 | bool bKeepAlive; 48 | bool bTcpNoDelay; 49 | byteQueue_tt readByteQueue; 50 | QUEUE queueWritePending; 51 | size_t nWritten; 52 | int32_t iWritePending; 53 | size_t nWritePendingBytes; 54 | atomic_int iStatus; 55 | atomic_int iRefCount; 56 | }; 57 | 58 | __UNUSED struct eventConnection_s* acceptEventConnection(struct eventIO_s* pEventIO, 59 | struct eventListenPort_s* pListenPort, 60 | int32_t hSocket, 61 | const inetAddress_tt* pRemoteAddr, 62 | const inetAddress_tt* pLocalAddr); 63 | 64 | __UNUSED struct eventConnection_s* acceptUdpEventConnection(struct eventIO_s* pEventIO, 65 | struct eventListenPort_s* pListenPort, 66 | int32_t hSocket, 67 | const inetAddress_tt* pRemoteAddr, 68 | const inetAddress_tt* pLocalAddr); 69 | -------------------------------------------------------------------------------- /src/core/include/eventIO/internal/win/eventListenPort_t.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "utility_t.h" 11 | #include "rbtree_t.h" 12 | 13 | #include "inetAddress_t.h" 14 | #include "rwSpinLock_t.h" 15 | 16 | #ifndef MAXIMUM_MTU_SIZE 17 | # define MAXIMUM_MTU_SIZE 1492 18 | #endif 19 | 20 | struct eventConnection_s; 21 | 22 | typedef struct acceptOverlappedPlus_s 23 | { 24 | OVERLAPPED _Overlapped; 25 | struct eventListenPort_s* pEventListenPort; 26 | char szBuffer[128]; 27 | SOCKET hSocket; 28 | } acceptOverlappedPlus_tt; 29 | 30 | typedef struct recvFromOverlappedPlus_s 31 | { 32 | OVERLAPPED _Overlapped; 33 | struct eventListenPort_s* pEventListenPort; 34 | inetAddress_tt inetAddress; 35 | int32_t iAddrLen; 36 | char szBuffer[MAXIMUM_MTU_SIZE]; 37 | } recvFromOverlappedPlus_tt; 38 | 39 | typedef struct addressConnection_s 40 | { 41 | RB_ENTRY(addressConnection_s) 42 | entry; 43 | inetAddress_tt inetAddress; 44 | uint64_t hash; 45 | struct eventConnection_s* pConnection; 46 | } addressConnection_tt; 47 | 48 | static inline int32_t addressCmp(struct addressConnection_s* src, struct addressConnection_s* dst) 49 | { 50 | int32_t iComp = (src->hash < dst->hash ? -1 : src->hash > dst->hash ? 1 : 0); 51 | if (iComp != 0) { 52 | return iComp; 53 | } 54 | return inetAddressCmp(&src->inetAddress, &dst->inetAddress); 55 | } 56 | 57 | RB_HEAD(addressConnectionMap_s, addressConnection_s); 58 | RB_GENERATE_STATIC(addressConnectionMap_s, addressConnection_s, entry, addressCmp) 59 | 60 | typedef struct addressConnectionMap_s addressConnectionMap_tt; 61 | 62 | struct eventListenPort_s 63 | { 64 | void (*fnAcceptCallback)(struct eventListenPort_s*, struct eventConnection_s*, const char*, 65 | uint32_t, void*); 66 | void (*fnUserFree)(void*); 67 | void* pUserData; 68 | SOCKET hSocket; 69 | struct eventIO_s* pEventIO; 70 | inetAddress_tt listenAddr; 71 | addressConnectionMap_tt mapAddressConnection; 72 | rwSpinLock_tt rwlock; 73 | bool bTcp; 74 | atomic_bool bActive; 75 | atomic_int iRefCount; 76 | }; 77 | 78 | __UNUSED void eventListenPort_onAccept(struct eventListenPort_s* pHandle, 79 | acceptOverlappedPlus_tt* pOverlappedPlus, 80 | uint32_t uiTransferred); 81 | 82 | __UNUSED void eventListenPort_onRecvFrom(struct eventListenPort_s* pHandle, 83 | recvFromOverlappedPlus_tt* pOverlappedPlus, 84 | uint32_t uiTransferred); 85 | 86 | __UNUSED void eventListenPort_removeAddressConnection(struct eventListenPort_s* pHandle, 87 | const inetAddress_tt* pRemoteAddr); 88 | -------------------------------------------------------------------------------- /src/service/source/timerWatcher_t.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "internal/timerWatcher_t.h" 4 | 5 | #include "internal/service-inl.h" 6 | #include "serviceEvent_t.h" 7 | #include "service_t.h" 8 | 9 | static void eventTimer_onTriggered(eventTimer_tt* pEventTimer, void* pData) 10 | { 11 | timerWatcher_tt* pTimerWatcher = (timerWatcher_tt*)pData; 12 | 13 | serviceEvent_tt* pEvent = mem_malloc(sizeof(serviceEvent_tt) + sizeof(timerWatcher_tt*)); 14 | pEvent->uiSourceID = service_getID(pTimerWatcher->pService); 15 | pEvent->uiToken = pTimerWatcher->uiToken; 16 | *(timerWatcher_tt**)(pEvent->szStorage) = pTimerWatcher; 17 | if (!eventTimer_isOnce(pEventTimer)) { 18 | pEvent->uiLength = DEF_EVENT_RUN_EVERY << 24; 19 | } 20 | else { 21 | pEvent->uiLength = DEF_EVENT_RUN_AFTER << 24; 22 | } 23 | timerWatcher_addref(pTimerWatcher); 24 | if (!service_enqueue(pTimerWatcher->pService, pEvent)) { 25 | timerWatcher_release(pTimerWatcher); 26 | } 27 | } 28 | 29 | void timerWatcher_eventTimer_onClose(eventTimer_tt* pHandle, void* pData) 30 | { 31 | timerWatcher_tt* pTimerWatcher = (timerWatcher_tt*)pData; 32 | timerWatcher_release(pTimerWatcher); 33 | } 34 | 35 | timerWatcher_tt* createTimerWatcher(service_tt* pService, uint32_t uiToken) 36 | { 37 | timerWatcher_tt* pHandle = mem_malloc(sizeof(timerWatcher_tt)); 38 | pHandle->pService = pService; 39 | service_addref(pHandle->pService); 40 | pHandle->uiToken = uiToken; 41 | pHandle->pEventTimer = NULL; 42 | atomic_init(&pHandle->iRefCount, 1); 43 | return pHandle; 44 | } 45 | 46 | void timerWatcher_addref(timerWatcher_tt* pHandle) 47 | { 48 | atomic_fetch_add(&(pHandle->iRefCount), 1); 49 | } 50 | 51 | void timerWatcher_release(timerWatcher_tt* pHandle) 52 | { 53 | if (atomic_fetch_sub(&(pHandle->iRefCount), 1) == 1) { 54 | if (pHandle->pEventTimer) { 55 | eventTimer_release(pHandle->pEventTimer); 56 | pHandle->pEventTimer = NULL; 57 | } 58 | 59 | if (pHandle->pService) { 60 | service_release(pHandle->pService); 61 | pHandle->pService = NULL; 62 | } 63 | mem_free(pHandle); 64 | } 65 | } 66 | 67 | bool timerWatcher_start(timerWatcher_tt* pHandle, bool bOnce, uint32_t uiIntervalMs) 68 | { 69 | eventIO_tt* pEventIO = service_getEventIO(pHandle->pService); 70 | atomic_fetch_add(&(pHandle->iRefCount), 1); 71 | 72 | pHandle->pEventTimer = 73 | createEventTimer(pEventIO, eventTimer_onTriggered, bOnce, uiIntervalMs, pHandle); 74 | eventTimer_setCloseCallback(pHandle->pEventTimer, timerWatcher_eventTimer_onClose); 75 | return eventTimer_start(pHandle->pEventTimer); 76 | } 77 | 78 | void timerWatcher_stop(timerWatcher_tt* pHandle) 79 | { 80 | if (pHandle->pEventTimer) { 81 | eventTimer_stop(pHandle->pEventTimer); 82 | eventTimer_release(pHandle->pEventTimer); 83 | pHandle->pEventTimer = NULL; 84 | } 85 | } 86 | 87 | bool timerWatcher_isRunning(timerWatcher_tt* pHandle) 88 | { 89 | return pHandle->pEventTimer; 90 | } 91 | 92 | service_tt* timerWatcher_getService(timerWatcher_tt* pHandle) 93 | { 94 | return pHandle->pService; 95 | } 96 | -------------------------------------------------------------------------------- /src/lua/helper/channelBlock.lua: -------------------------------------------------------------------------------- 1 | local serviceCore = require "serviceCore" 2 | local lbuffer = require "lruntime.buffer" 3 | local channelPool = require "channelPool" 4 | 5 | local _M = {} 6 | 7 | local channel_meta = { 8 | __index = _M, 9 | } 10 | 11 | local function wakeup_f(self) 12 | local co = self._co 13 | if co then 14 | self._co = nil 15 | serviceCore.wakeup(co) 16 | return true 17 | end 18 | return false 19 | end 20 | 21 | local function suspend_f(self) 22 | assert(not self._co) 23 | self._co = coroutine.running() 24 | serviceCore.wait(self._co) 25 | end 26 | 27 | local function dispatch_write(self) 28 | return function(msg,length) 29 | if self._running then 30 | if lbuffer.write(self._buf,msg,length) >= self._bufLimit then 31 | _M.close(self._address) 32 | else 33 | wakeup_f(self) 34 | end 35 | end 36 | end 37 | end 38 | 39 | local function dispatch_disconnect(self) 40 | return function() 41 | if self._running then 42 | self._running = false 43 | serviceCore.remoteClose(self._address) 44 | if wakeup_f(self) then 45 | self._closing = coroutine.running() 46 | serviceCore.wait(self._closing) 47 | end 48 | lbuffer.clear(self._buf) 49 | end 50 | end 51 | end 52 | 53 | function _M.closeAll() 54 | channelPool.close() 55 | end 56 | 57 | function _M.open(address) 58 | local self = setmetatable({}, channel_meta) 59 | self._address = address 60 | self._running = true 61 | self._buf = lbuffer.new() 62 | self._bufLimit = 8192 63 | channelPool.attach(address,dispatch_disconnect(self),dispatch_write(self)) 64 | return self 65 | end 66 | 67 | function _M.setBufferLimit(self, limit) 68 | self._bufLimit = limit 69 | end 70 | 71 | function _M.close(self) 72 | if self._running then 73 | self._running = false 74 | channelPool.detach(self._address) 75 | serviceCore.remoteClose(self._address,5000) 76 | if wakeup_f(self) then 77 | self._closing = coroutine.running() 78 | serviceCore.wait(self._closing) 79 | end 80 | lbuffer.clear(self._buf) 81 | else 82 | local co = self._closing 83 | if co then 84 | self._closing = nil 85 | serviceCore.wakeup(co) 86 | end 87 | end 88 | end 89 | 90 | function _M.readAll(self) 91 | while self._running do 92 | local s = lbuffer.readAll(self._buf) 93 | if s ~= nil then 94 | return s 95 | end 96 | suspend_f(self) 97 | end 98 | return false 99 | end 100 | 101 | function _M.read(self, n) 102 | if n == nil then 103 | return _M.readAll(self),0 104 | end 105 | 106 | while self._running do 107 | local s, readable = lbuffer.read(self._buf,n) 108 | if s ~= nil then 109 | return s,readable 110 | end 111 | suspend_f(self) 112 | end 113 | return false, 0 114 | end 115 | 116 | 117 | function _M.readLineEOL(self,offset) 118 | while self._running do 119 | local s, n = lbuffer.readLineEOL(self._buf,offset) 120 | if s ~= nil then 121 | return s,n 122 | end 123 | suspend_f(self) 124 | end 125 | return false, 0 126 | end 127 | 128 | function _M.readLineCRLF(self,offset) 129 | while self._running do 130 | local s, n = lbuffer.readLineCRLF(self._buf,offset) 131 | if s ~= nil then 132 | return s,n 133 | end 134 | suspend_f(self) 135 | end 136 | return false, 0 137 | end 138 | 139 | return _M 140 | -------------------------------------------------------------------------------- /src/core/include/openssl/crypt_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // type 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | #include "platform_t.h" 10 | 11 | // openssl crypto mem 12 | frCore_API void crypto_customize_mem_init(); 13 | 14 | // sha 15 | #define DEF_SHA1_DIGEST_LENGTH 20 16 | #define DEF_SHA224_DIGEST_LENGTH 28 17 | #define DEF_SHA256_DIGEST_LENGTH 32 18 | #define DEF_SHA384_DIGEST_LENGTH 48 19 | #define DEF_SHA512_DIGEST_LENGTH 64 20 | 21 | frCore_API void crypt_sha1(const void* data, size_t len, uint8_t* digest); 22 | 23 | frCore_API void crypt_sha224(const void* data, size_t len, uint8_t* digest); 24 | 25 | frCore_API void crypt_sha256(const void* data, size_t len, uint8_t* digest); 26 | 27 | frCore_API void crypt_sha384(const void* data, size_t len, uint8_t* digest); 28 | 29 | frCore_API void crypt_sha512(const void* data, size_t len, uint8_t* digest); 30 | 31 | // base64 32 | frCore_API int32_t crypt_base64Encode(const uint8_t* data, int32_t n, uint8_t* out); 33 | 34 | frCore_API int32_t crypt_base64Decode(const uint8_t* data, int32_t n, 35 | uint8_t* out); // Need to remove the following 0 36 | 37 | // md5 38 | #define DEF_MD5_DIGEST_LENGTH 16 39 | 40 | frCore_API void crypt_md5(const void* data, size_t len, uint8_t* digest); 41 | 42 | // aes 43 | #define DEF_AES_BLOCK_SIZE 16 44 | 45 | frCore_API void crypt_aes_cbc_encrypt(const uint8_t* key, const void* data, size_t len, 46 | uint8_t* out); 47 | 48 | frCore_API void crypt_aes_cbc_decrypt(const uint8_t* key, const void* data, size_t len, 49 | uint8_t* out); 50 | 51 | // rand 52 | frCore_API int32_t crypt_rand_bytes(uint8_t* key, int32_t len); 53 | 54 | // srp 55 | frCore_API int32_t crypt_srp_create_verifier(const char* id, const char* user, const char* password, 56 | uint8_t* salt, int32_t* saltLen, uint8_t* verifier, 57 | int32_t* verifierLen); 58 | 59 | frCore_API int32_t crypt_srp_create_key_server(const char* id, const uint8_t* verifier, 60 | int32_t verifierLen, uint8_t* privKey, 61 | int32_t* privKeyLen, uint8_t* pubKey, 62 | int32_t* pubKeyLen); 63 | 64 | frCore_API int32_t crypt_srp_create_key_client(const char* id, uint8_t* privKey, 65 | int32_t* privKeyLen, uint8_t* pubKey, 66 | int32_t* pubKeyLen); 67 | 68 | frCore_API int32_t crypt_srp_create_session_key_server( 69 | const char* id, const uint8_t* verifier, int32_t verifierLen, const uint8_t* serverPrivKey, 70 | int32_t serverPrivKeyLen, const uint8_t* serverPubKey, int32_t serverPubKeyLen, 71 | const uint8_t* clientPubKey, int32_t clientPubKeyLen, uint8_t* sessionKey, 72 | int32_t* sessionKeyLen); 73 | 74 | frCore_API int32_t crypt_srp_create_session_key_client( 75 | const char* id, const char* user, const char* password, const uint8_t* salt, int32_t saltLen, 76 | const uint8_t* clientPrivKey, int32_t clientPrivKeyLen, const uint8_t* clientPubKey, 77 | int32_t clientPubKeyLen, const uint8_t* serverPubKey, int32_t serverPubKeyLen, 78 | uint8_t* sessionKey, int32_t* sessionKeyLen); 79 | -------------------------------------------------------------------------------- /src/lua/library/injectcode.lua: -------------------------------------------------------------------------------- 1 | -- modify from skynet https://github.com/cloudwu/skynet/ injectcode.lua 2 | local debug = debug 3 | local table = table 4 | 5 | local FUNC_TEMP=[[ 6 | local $ARGS 7 | return function(...) 8 | $SOURCE 9 | end, 10 | function() 11 | return {$LOCALS} 12 | end 13 | ]] 14 | 15 | local temp = {} 16 | local function wrap_locals(co, source, level, ext_funcs) 17 | if co == coroutine.running() then 18 | level = level + 3 19 | end 20 | local f = debug.getinfo(co, level,"f").func 21 | if f == nil then 22 | return false, "Invalid level" 23 | end 24 | 25 | local uv = {} 26 | local locals = {} 27 | local uv_id = {} 28 | local local_id = {} 29 | 30 | if ext_funcs then 31 | for k,v in pairs(ext_funcs) do 32 | table.insert(uv, k) 33 | end 34 | end 35 | local i = 1 36 | while true do 37 | local name, value = debug.getlocal(co, level, i) 38 | if name == nil then 39 | break 40 | end 41 | if name:byte() ~= 40 then -- '(' 42 | table.insert(uv, name) 43 | table.insert(locals, ("[%d]=%s,"):format(i,name)) 44 | local_id[name] = value 45 | end 46 | i = i + 1 47 | end 48 | local i = 1 49 | while true do 50 | local name = debug.getupvalue(f, i) 51 | if name == nil then 52 | break 53 | end 54 | uv_id[name] = i 55 | table.insert(uv, name) 56 | i = i + 1 57 | end 58 | temp.ARGS = table.concat(uv, ",") 59 | temp.SOURCE = source 60 | temp.LOCALS = table.concat(locals) 61 | local full_source = FUNC_TEMP:gsub("%$(%w+)",temp) 62 | local loader, err = load(full_source, "=(debug)") 63 | if loader == nil then 64 | return false, err 65 | end 66 | local func, update = loader() 67 | -- join func's upvalues 68 | local i = 1 69 | while true do 70 | local name = debug.getupvalue(func, i) 71 | if name == nil then 72 | break 73 | end 74 | if ext_funcs then 75 | local v = ext_funcs[name] 76 | if v then 77 | debug.setupvalue(func, i, v) 78 | end 79 | end 80 | 81 | local local_value = local_id[name] 82 | if local_value then 83 | debug.setupvalue(func, i, local_value) 84 | end 85 | local upvalue_id = uv_id[name] 86 | if upvalue_id then 87 | debug.upvaluejoin(func, i, f, upvalue_id) 88 | end 89 | i=i+1 90 | end 91 | local vararg, v = debug.getlocal(co, level, -1) 92 | if vararg then 93 | local vargs = { v } 94 | local i = 2 95 | while true do 96 | local vararg,v = debug.getlocal(co, level, -i) 97 | if vararg then 98 | vargs[i] = v 99 | else 100 | break 101 | end 102 | i=i+1 103 | end 104 | return func, update, table.unpack(vargs) 105 | else 106 | return func, update 107 | end 108 | end 109 | 110 | local function exec(co, level, func, update, ...) 111 | if not func then 112 | return false, update 113 | end 114 | if co == coroutine.running() then 115 | level = level + 2 116 | end 117 | local rets = table.pack(pcall(func, ...)) 118 | if rets[1] then 119 | local needupdate = update() 120 | for k,v in pairs(needupdate) do 121 | debug.setlocal(co, level,k,v) 122 | end 123 | return table.unpack(rets, 1, rets.n) 124 | else 125 | return false, rets[2] 126 | end 127 | end 128 | 129 | return function (source, co, level, ext_funcs) 130 | co = co or coroutine.running() 131 | level = level or 0 132 | return exec(co, level, wrap_locals(co, source, level, ext_funcs)) 133 | end 134 | 135 | -------------------------------------------------------------------------------- /src/runtime/source/debug/ldebug_t.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "debug/ldebug_t.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "lauxlib.h" 10 | #include "lua.h" 11 | #include "lualib.h" 12 | 13 | // modify from skynet https://github.com/cloudwu/skynet/ lua-debugchannel 14 | 15 | static const int32_t s_iHookKey = 0; 16 | 17 | static lua_State* getLuaThread(lua_State* L, int32_t* pArg) 18 | { 19 | if (lua_isthread(L, 1)) { 20 | *pArg = 1; 21 | return lua_tothread(L, 1); 22 | } 23 | else { 24 | *pArg = 0; 25 | return L; 26 | } 27 | } 28 | 29 | static void hook_f(lua_State* L, lua_Debug* pDebug) 30 | { 31 | static const char* const s_szHookNames[] = {"call", "return", "line", "count", "tail call"}; 32 | lua_rawgetp(L, LUA_REGISTRYINDEX, &s_iHookKey); 33 | lua_pushthread(L); 34 | 35 | if (lua_rawget(L, -2) == LUA_TFUNCTION) { 36 | lua_pushstring(L, s_szHookNames[(int32_t)pDebug->event]); 37 | if (pDebug->currentline >= 0) { 38 | lua_pushinteger(L, pDebug->currentline); 39 | } 40 | else { 41 | lua_pushnil(L); 42 | } 43 | lua_call(L, 2, 1); 44 | int32_t iYield = lua_toboolean(L, -1); 45 | lua_pop(L, 1); 46 | if (iYield) { 47 | lua_yield(L, 0); 48 | } 49 | } 50 | } 51 | 52 | static int32_t makeMask(const char* szMask, int32_t iCount) 53 | { 54 | int32_t iMask = 0; 55 | if (strchr(szMask, 'c')) { 56 | iMask |= LUA_MASKCALL; 57 | } 58 | 59 | if (strchr(szMask, 'r')) { 60 | iMask |= LUA_MASKRET; 61 | } 62 | 63 | if (strchr(szMask, 'l')) { 64 | iMask |= LUA_MASKLINE; 65 | } 66 | 67 | if (iCount > 0) { 68 | iMask |= LUA_MASKCOUNT; 69 | } 70 | 71 | return iMask; 72 | } 73 | 74 | static int32_t ldeubg_sethook(lua_State* L) 75 | { 76 | int32_t iArg = 0; 77 | int32_t iMask = 0; 78 | int32_t iCount = 0; 79 | 80 | lua_Hook func = NULL; 81 | lua_State* pLuaState = getLuaThread(L, &iArg); 82 | if (lua_isnoneornil(L, iArg + 1)) { 83 | lua_settop(L, iArg + 1); 84 | } 85 | else { 86 | const char* szMask = luaL_checkstring(L, iArg + 2); 87 | luaL_checktype(L, iArg + 1, LUA_TFUNCTION); 88 | iCount = (int32_t)luaL_optinteger(L, iArg + 3, 0); 89 | func = hook_f; 90 | iMask = makeMask(szMask, iCount); 91 | } 92 | 93 | if (lua_rawgetp(L, LUA_REGISTRYINDEX, &s_iHookKey) == LUA_TNIL) { 94 | lua_createtable(L, 0, 2); 95 | lua_pushvalue(L, -1); 96 | lua_rawsetp(L, LUA_REGISTRYINDEX, &s_iHookKey); 97 | lua_pushstring(L, "k"); 98 | lua_setfield(L, -2, "__mode"); 99 | lua_pushvalue(L, -1); 100 | lua_setmetatable(L, -2); 101 | } 102 | 103 | lua_pushthread(pLuaState); 104 | lua_xmove(pLuaState, L, 1); 105 | lua_pushvalue(L, iArg + 1); 106 | lua_rawset(L, -3); 107 | lua_sethook(pLuaState, func, iMask, iCount); 108 | return 0; 109 | } 110 | 111 | int32_t luaopen_lruntime_debug(lua_State* L) 112 | { 113 | #ifdef luaL_checkversion 114 | luaL_checkversion(L); 115 | #endif 116 | luaL_Reg lualib_debug[] = {{"sethook", ldeubg_sethook}, {NULL, NULL}}; 117 | 118 | luaL_newlib(L, lualib_debug); 119 | return 1; 120 | } 121 | -------------------------------------------------------------------------------- /src/core/source/detail/fs_t.c: -------------------------------------------------------------------------------- 1 | #include "fs_t.h" 2 | 3 | #if defined(_WINDOWS) || defined(_WIN32) 4 | # include 5 | # include 6 | #else 7 | # include 8 | # include 9 | # include 10 | # include 11 | # define gp_stat stat 12 | # define gp_stat_struct struct stat 13 | #endif 14 | 15 | #include 16 | #include 17 | 18 | #include "utility_t.h" 19 | 20 | #ifndef MAX_PATH 21 | # define MAX_PATH 260 22 | #endif 23 | 24 | bool fs_mkdir(const char* szPath) 25 | { 26 | #if defined(_WINDOWS) || defined(_WIN32) 27 | return _mkdir(szPath) != 0; 28 | #else 29 | return mkdir(szPath, 0777) != 0; 30 | #endif 31 | } 32 | 33 | bool fs_find(const char* szFile) 34 | { 35 | #if defined(_WINDOWS) || defined(_WIN32) 36 | return _access(szFile, 0) != -1; 37 | #else 38 | return access(szFile, 0) != -1; 39 | #endif 40 | } 41 | 42 | bool fs_remove(const char* szFile) 43 | { 44 | return remove(szFile); 45 | } 46 | 47 | bool fs_resetName(const char* szSrcFile, const char* szDstFile) 48 | { 49 | return rename(szSrcFile, szDstFile) != -1; 50 | } 51 | 52 | bool fs_removePath(const char* szPath) 53 | { 54 | #if defined(_WINDOWS) || defined(_WIN32) 55 | return _rmdir(szPath) != 0; 56 | #else 57 | return rmdir(szPath) != 0; 58 | #endif 59 | } 60 | 61 | bool fs_removeAll(const char* szPath) 62 | { 63 | #if defined(_WINDOWS) || defined(_WIN32) 64 | char szPathName[MAX_PATH]; 65 | strcpy(szPathName, szPath); 66 | strcat(szPathName, "\\*"); 67 | 68 | intptr_t hFile; 69 | struct _finddata_t fileinfo; 70 | hFile = _findfirst(szPathName, &fileinfo); 71 | if (hFile == -1) { 72 | return false; 73 | } 74 | do { 75 | if ((fileinfo.attrib & _A_SUBDIR)) { 76 | if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { 77 | bzero(szPathName, 256); 78 | strcpy(szPathName, szPath); 79 | strcat(szPathName, "\\"); 80 | strcat(szPathName, fileinfo.name); 81 | fs_removeAll(szPathName); 82 | } 83 | } 84 | else { 85 | bzero(szPathName, 256); 86 | strcpy(szPathName, szPath); 87 | strcat(szPathName, "\\"); 88 | strcat(szPathName, fileinfo.name); 89 | fs_remove(szPathName); 90 | } 91 | } while (_findnext(hFile, &fileinfo) == 0); 92 | _findclose(hFile); 93 | fs_removePath(szPath); 94 | return true; 95 | #else 96 | DIR* dp = NULL; 97 | struct dirent* dirp; 98 | dp = opendir(szPath); 99 | if (dp == NULL) { 100 | return false; 101 | } 102 | 103 | char szPathName[MAX_PATH]; 104 | 105 | while ((dirp = readdir(dp)) != NULL) { 106 | if (strcmp(dirp->d_name, "..") == 0 || strcmp(dirp->d_name, ".") == 0) continue; 107 | bzero(szPathName, MAX_PATH); 108 | strcpy(szPathName, szPath); 109 | strcat(szPathName, "/"); 110 | strcat(szPathName, dirp->d_name); 111 | if (dirp->d_type == DT_DIR) { 112 | fs_removeAll(szPathName); 113 | } 114 | else { 115 | fs_remove(szPathName); 116 | } 117 | } 118 | fs_removePath(szPath); 119 | closedir(dp); 120 | dirp = NULL; 121 | return true; 122 | #endif 123 | } 124 | -------------------------------------------------------------------------------- /src/core/include/utility_t.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | #include "platform_t.h" 11 | 12 | #define container_of(ptr, type, member) ((type*)((char*)(ptr)-offsetof(type, member))) 13 | 14 | #define align_size(n, alignment) ((n + alignment - 1) & ~(alignment - 1)) 15 | 16 | #ifdef _MSC_VER 17 | # define _decl_forceInline __forceinline 18 | #elif defined(__clang__) || defined(__GNUC__) 19 | # define _decl_forceInline inline __attribute__((__always_inline__)) 20 | #else 21 | # define _decl_forceInline inline 22 | #endif 23 | 24 | #ifndef __UNUSED 25 | # if defined(__clang__) || defined(__GNUC__) 26 | # define __UNUSED __attribute__((unused)) 27 | # elif defined(_MSC_VER) && (_MSC_VER >= 1700) 28 | # define __UNUSED _Check_return_ 29 | # else 30 | # define __UNUSED 31 | # endif 32 | #endif 33 | 34 | #ifdef _MSC_VER 35 | # define _decl_noInline __declspec(noinline) 36 | #elif defined(__clang__) || defined(__GNUC__) 37 | # define _decl_noInline __attribute__((__noinline__)) 38 | #else 39 | # define _decl_noInline 40 | #endif 41 | 42 | #ifdef _MSC_VER 43 | # define _decl_threadLocal __declspec(thread) 44 | #else 45 | # define _decl_threadLocal __thread 46 | #endif 47 | 48 | #define CPU_CACHE_LINE 64 49 | 50 | #if defined(_MSC_VER) 51 | # define _decl_cpu_cache_align __declspec(align(CPU_CACHE_LINE)) 52 | #elif defined(__clang__) || defined(__GNUC__) 53 | # define _decl_cpu_cache_align __attribute__((aligned(CPU_CACHE_LINE))) 54 | #else 55 | # define _decl_cpu_cache_align 56 | #endif 57 | 58 | #if defined(__GNUC__) 59 | # define _Likely(x) (__builtin_expect((x), 1)) 60 | # define _UnLikely(x) (__builtin_expect((x), 0)) 61 | #else 62 | # define _Likely(x) (x) 63 | # define _UnLikely(x) (x) 64 | #endif 65 | 66 | #if defined(_WINDOWS) || defined(_WIN32) 67 | # define bzero(s, n) memset(s, 0, n) 68 | # define strcasecmp _stricmp 69 | # define strncasecmp _strnicmp 70 | # define strtok_r strtok_s 71 | #endif 72 | 73 | typedef struct ioBufVec_s 74 | { 75 | char* pBuf; 76 | int32_t iLength; 77 | } ioBufVec_tt; 78 | 79 | static inline size_t strlncat(char* dst, size_t len, const char* src, size_t n) 80 | { 81 | size_t slen; 82 | size_t dlen; 83 | size_t rlen; 84 | size_t ncpy; 85 | 86 | slen = strnlen(src, n); 87 | dlen = strnlen(dst, len); 88 | 89 | if (slen + dlen >= len) { 90 | return 0; 91 | } 92 | 93 | if (dlen < len) { 94 | rlen = len - dlen; 95 | ncpy = slen < rlen ? slen : (rlen - 1); 96 | memcpy(dst + dlen, src, ncpy); 97 | dst[dlen + ncpy] = '\0'; 98 | } 99 | 100 | return slen + dlen; 101 | } 102 | 103 | 104 | frCore_DEF void* (*mem_malloc)(size_t size); 105 | frCore_DEF void* (*mem_realloc)(void* ptr, size_t size); 106 | frCore_DEF void (*mem_free)(void* ptr); 107 | 108 | frCore_API void set_mem_functions(void* (*fnMalloc)(size_t), void* (*fnRealloc)(void*, size_t), 109 | void (*fnFree)(void*)); 110 | 111 | frCore_API char* mem_strdup(const char* s); 112 | frCore_API char* mem_strndup(const char* s, size_t n); 113 | 114 | frCore_API char* str_replace_all(char* str, char* sub, char* replace); 115 | 116 | frCore_API char** str_split(const char* s, const char* delim); 117 | frCore_API char** str_split_count(const char* s, const char* del, size_t* nb); --------------------------------------------------------------------------------