1.作为skynet的启动文件,主要完成了一些初始化和读取并存取配置文件内容的工作. 在这里只将代码读取配置文件的部分抽取出来,就算没有skynet环境,这些代码也是可以运行的,了解以后再对照源码进行分析,希望能对理解skynet带来一些帮助
#include "lua.h" #include "lualib.h" #include "lauxlib.h" #include <signal.h> #include <assert.h> #include "env.h" struct config { int thread; int harbor; const char *deamon; const char *module_path; const char *bootstrap; const char *logger; const char *logservice; }; void popn(lua_State *L, int n); static int optint(const char *key, int opt) { const char *str = env_getenv(key); if (str == NULL) { char tmp[20]; sprintf(tmp, "%d", opt); env_setenv(key, tmp); return opt; } return strtol(str, NULL, 10); } static const char * optstring(const char *key, const char *opt) { const char *str = env_getenv(key); if (str == NULL) { if (opt) { env_setenv(key, opt); opt = env_getenv(key); } return opt; } return str; } static const char * load_config = "\ local config_name = ...\ local f = assert(io.open(config_name))\ local code = assert(f:read \'*a\')\ print(\"code is \", code)\ local function getenv(name) return assert(os.getenv(name), \'os.getenv() failed: \' .. name) end\ code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\ f:close()\ print(\"code after replace is \", code)\ local result = {}\ assert(load(code,\'=(load)\',\'t\',result))()\ return result\ "; static void _init_env(lua_State *L) { lua_pushnil(L); while(lua_next(L, -2) != 0) { int keyt = lua_type(L, -2); if (keyt != LUA_TSTRING) { fprintf(stderr, "Invalid, config table\n"); exit(1); } const char *key = lua_tostring(L, -2); if (lua_type(L, -1) == LUA_TBOOLEAN) { int b = lua_toboolean(L, -1); env_setenv(key, b ? "true" : "false"); } else { const char *value = lua_tostring(L, -1); if (value == NULL) { fprintf(stderr, "Invalud config table key = %s\n", key); exit(1); } env_setenv(key, value); } lua_pop(L, 1); } lua_pop(L, 1); } void popn(lua_State *L, int n) { lua_pop(L, -(n) - 1); } int sigign() { struct sigaction sa; sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, 0); return 0; } void init_conf(struct config *conf) { conf->thread = optint("thread", 8); conf->module_path = optstring("cpath", "./cservice/?.so"); conf->harbor = optint("harbor", 1); conf->bootstrap = optstring("bootstrap", "snlua bootstrap"); conf->deamon = optstring("deamon", NULL); conf->deamon = optstring("logger", NULL); conf->logservice = optstring("logservice", "logger"); } void test_env() { printf("thread: %s\n", env_getenv("thread")); printf("harbor: %s\n", env_getenv("harbor")); } int main(int argc, char *argv[]) { const char *config_file = NULL; if (argc > 1) { config_file = argv[1]; } else { fprintf(stderr, "Need a config file. Please read skynet wiki : https://github.com/cloudwu/skynet/wiki/Config\n" "usage: skynet configfilename\n"); } sigign(); env_env_init(); struct config conf; struct lua_State *L = luaL_newstate(); luaL_openlibs(L); int err = luaL_loadstring(L, load_config); assert(err == LUA_OK); lua_pushstring(L, config_file); err = lua_pcall(L, 1, 1, 0); if (err) { fprintf(stderr, "%s,\n", lua_tostring(L, -1)); lua_close(L); return 1; } _init_env(L); init_conf(&conf); test_env(); lua_close(L); return 0; }