From eed9c42fd4c0d8fbdaf6a05403112a5ac6ec05aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 9 Aug 2012 15:36:56 +0800 Subject: [PATCH] change source dir --- Makefile | 59 ++++++--- client.c => client-src/client.c | 0 config | 5 +- config_log | 4 +- lua-skynet.c => lualib-src/lua-skynet.c | 2 +- redis.lua => lualib/redis.lua | 0 skynet.lua => lualib/skynet.lua | 0 .../service_broker.c | 0 .../service_client.c | 0 service_lua.c => service-src/service_lua.c | 18 ++- agent.lua => service/agent.lua | 0 console.lua => service/console.lua | 0 globallog.lua => service/globallog.lua | 0 launcher.lua => service/launcher.lua | 0 main.lua => service/main.lua | 10 +- main_log.lua => service/main_log.lua | 0 redis-cli.lua => service/redis-cli.lua | 0 simpledb.lua => service/simpledb.lua | 0 testconn.lua => service/testconn.lua | 0 testdb.lua => service/testdb.lua | 0 testredis.lua => service/testredis.lua | 0 testtimer.lua => service/testtimer.lua | 0 watchdog.lua => service/watchdog.lua | 0 rwlock.h => skynet-src/rwlock.h | 0 skynet.h => skynet-src/skynet.h | 0 skynet-src/skynet_env.c | 53 ++++++++ skynet-src/skynet_env.h | 9 ++ skynet_error.c => skynet-src/skynet_error.c | 0 skynet_handle.c => skynet-src/skynet_handle.c | 0 skynet_handle.h => skynet-src/skynet_handle.h | 0 skynet_harbor.c => skynet-src/skynet_harbor.c | 0 skynet_harbor.h => skynet-src/skynet_harbor.h | 0 skynet_imp.h => skynet-src/skynet_imp.h | 0 skynet_logger.c => skynet-src/skynet_logger.c | 0 skynet-src/skynet_main.c | 117 ++++++++++++++++++ skynet_module.c => skynet-src/skynet_module.c | 25 ++-- skynet_module.h => skynet-src/skynet_module.h | 0 skynet_mq.c => skynet-src/skynet_mq.c | 0 skynet_mq.h => skynet-src/skynet_mq.h | 0 skynet_server.c => skynet-src/skynet_server.c | 20 +++ skynet_server.h => skynet-src/skynet_server.h | 0 skynet_start.c => skynet-src/skynet_start.c | 0 skynet_system.h => skynet-src/skynet_system.h | 0 skynet_timer.c => skynet-src/skynet_timer.c | 0 skynet_timer.h => skynet-src/skynet_timer.h | 0 skynet_main.c | 80 ------------ 46 files changed, 284 insertions(+), 118 deletions(-) rename client.c => client-src/client.c (100%) rename lua-skynet.c => lualib-src/lua-skynet.c (99%) rename redis.lua => lualib/redis.lua (100%) rename skynet.lua => lualib/skynet.lua (100%) rename service_broker.c => service-src/service_broker.c (100%) rename service_client.c => service-src/service_client.c (100%) rename service_lua.c => service-src/service_lua.c (80%) rename agent.lua => service/agent.lua (100%) rename console.lua => service/console.lua (100%) rename globallog.lua => service/globallog.lua (100%) rename launcher.lua => service/launcher.lua (100%) rename main.lua => service/main.lua (65%) rename main_log.lua => service/main_log.lua (100%) rename redis-cli.lua => service/redis-cli.lua (100%) rename simpledb.lua => service/simpledb.lua (100%) rename testconn.lua => service/testconn.lua (100%) rename testdb.lua => service/testdb.lua (100%) rename testredis.lua => service/testredis.lua (100%) rename testtimer.lua => service/testtimer.lua (100%) rename watchdog.lua => service/watchdog.lua (100%) rename rwlock.h => skynet-src/rwlock.h (100%) rename skynet.h => skynet-src/skynet.h (100%) create mode 100644 skynet-src/skynet_env.c create mode 100644 skynet-src/skynet_env.h rename skynet_error.c => skynet-src/skynet_error.c (100%) rename skynet_handle.c => skynet-src/skynet_handle.c (100%) rename skynet_handle.h => skynet-src/skynet_handle.h (100%) rename skynet_harbor.c => skynet-src/skynet_harbor.c (100%) rename skynet_harbor.h => skynet-src/skynet_harbor.h (100%) rename skynet_imp.h => skynet-src/skynet_imp.h (100%) rename skynet_logger.c => skynet-src/skynet_logger.c (100%) create mode 100644 skynet-src/skynet_main.c rename skynet_module.c => skynet-src/skynet_module.c (86%) rename skynet_module.h => skynet-src/skynet_module.h (100%) rename skynet_mq.c => skynet-src/skynet_mq.c (100%) rename skynet_mq.h => skynet-src/skynet_mq.h (100%) rename skynet_server.c => skynet-src/skynet_server.c (96%) rename skynet_server.h => skynet-src/skynet_server.h (100%) rename skynet_start.c => skynet-src/skynet_start.c (100%) rename skynet_system.h => skynet-src/skynet_system.h (100%) rename skynet_timer.c => skynet-src/skynet_timer.c (100%) rename skynet_timer.h => skynet-src/skynet_timer.h (100%) delete mode 100644 skynet_main.c diff --git a/Makefile b/Makefile index 4bc87d0d..64175e4c 100644 --- a/Makefile +++ b/Makefile @@ -1,35 +1,56 @@ -all : skynet snlua.so logger.so skynet.so gate.so client.so connection.so broker.so client skynet-master +all : \ +skynet \ +service/snlua.so \ +service/logger.so \ +lualib/skynet.so \ +service/gate.so \ +service/client.so \ +service/connection.so \ +service/broker.so \ +client \ +skynet-master -skynet : skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c skynet_server.c skynet_start.c skynet_timer.c skynet_error.c skynet_harbor.c master/master.c - gcc -Wall -g -Wl,-E -o $@ $^ -Imaster -lpthread -ldl -lrt -Wl,-E -llua -lm -lzmq +skynet : \ +skynet-src/skynet_main.c \ +skynet-src/skynet_handle.c \ +skynet-src/skynet_module.c \ +skynet-src/skynet_mq.c \ +skynet-src/skynet_server.c \ +skynet-src/skynet_start.c \ +skynet-src/skynet_timer.c \ +skynet-src/skynet_error.c \ +skynet-src/skynet_harbor.c \ +skynet-src/skynet_env.c \ +master/master.c + gcc -Wall -g -Wl,-E -o $@ $^ -Iskynet-src -Imaster -lpthread -ldl -lrt -Wl,-E -llua -lm -lzmq -logger.so : skynet_logger.c - gcc -Wall -g -fPIC --shared $^ -o $@ +service/logger.so : skynet-src/skynet_logger.c + gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src -snlua.so : service_lua.c - gcc -Wall -g -fPIC --shared $^ -o $@ +service/snlua.so : service-src/service_lua.c + gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src -gate.so : gate/mread.c gate/ringbuffer.c gate/main.c - gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Igate +service/gate.so : gate/mread.c gate/ringbuffer.c gate/main.c + gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Igate -Iskynet-src -skynet.so : lua-skynet.c lua-serialize/serialize.c - gcc -Wall -g -fPIC --shared $^ -o $@ +lualib/skynet.so : lualib-src/lua-skynet.c lua-serialize/serialize.c + gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src -Ilua-serialize -client.so : service_client.c - gcc -Wall -g -fPIC --shared $^ -o $@ +service/client.so : service-src/service_client.c + gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src -connection.so : connection/connection.c connection/main.c - gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Iconnection +service/connection.so : connection/connection.c connection/main.c + gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Iconnection -Iskynet-src -broker.so : service_broker.c - gcc -Wall -g -fPIC --shared $^ -o $@ +service/broker.so : service-src/service_broker.c + gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src -client : client.c +client : client-src/client.c gcc -Wall -g $^ -o $@ -lpthread skynet-master : master/master.c master/main.c gcc -g -Wall -Imaster -o $@ $^ -lzmq clean : - rm skynet client skynet-master *.so + rm skynet client skynet-master lualib/*.so service/*.so diff --git a/client.c b/client-src/client.c similarity index 100% rename from client.c rename to client-src/client.c diff --git a/config b/config index 3bda9c32..a1b197bd 100644 --- a/config +++ b/config @@ -1,9 +1,10 @@ thread = 8 mqueue = 256 -cpath = "./" +cpath = "./service/?.so" logger = nil harbor = 1 address = "tcp://127.0.0.1:2525" master = "tcp://127.0.0.1:2012" -start = "main.lua" +start = "main" standalone = true +luaservice = "./service/?.lua" diff --git a/config_log b/config_log index 0530c219..74358d05 100644 --- a/config_log +++ b/config_log @@ -1,8 +1,8 @@ thread = 2 mqueue = 256 -cpath = "./" +cpath = "./service/?.so" logger = nil harbor = 2 address = "tcp://127.0.0.1:2526" master = "tcp://127.0.0.1:2012" -start = "main_log.lua" +start = "main_log" diff --git a/lua-skynet.c b/lualib-src/lua-skynet.c similarity index 99% rename from lua-skynet.c rename to lualib-src/lua-skynet.c index e1cc7f37..2d1ccd8f 100644 --- a/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -1,5 +1,5 @@ #include "skynet.h" -#include "lua-serialize/luaseri.h" +#include "luaseri.h" #include #include diff --git a/redis.lua b/lualib/redis.lua similarity index 100% rename from redis.lua rename to lualib/redis.lua diff --git a/skynet.lua b/lualib/skynet.lua similarity index 100% rename from skynet.lua rename to lualib/skynet.lua diff --git a/service_broker.c b/service-src/service_broker.c similarity index 100% rename from service_broker.c rename to service-src/service_broker.c diff --git a/service_client.c b/service-src/service_client.c similarity index 100% rename from service_client.c rename to service-src/service_client.c diff --git a/service_lua.c b/service-src/service_lua.c similarity index 80% rename from service_lua.c rename to service-src/service_lua.c index 3ac9c83e..64916ff9 100644 --- a/service_lua.c +++ b/service-src/service_lua.c @@ -15,7 +15,23 @@ snlua_create(void) { static int _load(lua_State *L, char ** filename) { const char * name = strsep(filename, " \r\n"); - int r = luaL_loadfile(L,name); + const char * path = skynet_command(NULL, "GETENV", "luaservice"); + int namesz = strlen(name); + int sz = strlen(path) + namesz; + char tmp[sz]; + int i; + for (i=0;path[i]!='?' && path[i]!='\0';i++) { + tmp[i] = path[i]; + } + memcpy(tmp+i,name,namesz); + if (path[i] == '?') { + strcpy(tmp+i+namesz,path+i+1); + } else { + fprintf(stderr,"snlua : Invalid lua service path\n"); + exit(1); + } + + int r = luaL_loadfile(L,tmp); return r != LUA_OK; } diff --git a/agent.lua b/service/agent.lua similarity index 100% rename from agent.lua rename to service/agent.lua diff --git a/console.lua b/service/console.lua similarity index 100% rename from console.lua rename to service/console.lua diff --git a/globallog.lua b/service/globallog.lua similarity index 100% rename from globallog.lua rename to service/globallog.lua diff --git a/launcher.lua b/service/launcher.lua similarity index 100% rename from launcher.lua rename to service/launcher.lua diff --git a/main.lua b/service/main.lua similarity index 65% rename from main.lua rename to service/main.lua index b6fbe73d..3a3cbbb7 100644 --- a/main.lua +++ b/service/main.lua @@ -4,19 +4,19 @@ skynet.dispatch() skynet.start(function() print("Server start") - local launcher = skynet.launch("snlua","launcher.lua") + local launcher = skynet.launch("snlua","launcher") print("launcher", launcher) - local console = skynet.launch("snlua","console.lua") + local console = skynet.launch("snlua","console") print("console",console) - local watchdog = skynet.launch("snlua","watchdog.lua") + local watchdog = skynet.launch("snlua","watchdog") print("watchdog",watchdog) local gate = skynet.launch("gate","8888 4 0") print("gate",gate) - local db = skynet.launch("snlua","simpledb.lua") + local db = skynet.launch("snlua","simpledb") print("simpledb",db) local connection = skynet.launch("connection","256") print("connection",connection) - local redis = skynet.call(".launcher", "broker .redis snlua redis-cli.lua 127.0.0.1:6379") + local redis = skynet.call(".launcher", "broker .redis snlua redis-cli 127.0.0.1:6379") print("redis",redis) skynet.exit() end) \ No newline at end of file diff --git a/main_log.lua b/service/main_log.lua similarity index 100% rename from main_log.lua rename to service/main_log.lua diff --git a/redis-cli.lua b/service/redis-cli.lua similarity index 100% rename from redis-cli.lua rename to service/redis-cli.lua diff --git a/simpledb.lua b/service/simpledb.lua similarity index 100% rename from simpledb.lua rename to service/simpledb.lua diff --git a/testconn.lua b/service/testconn.lua similarity index 100% rename from testconn.lua rename to service/testconn.lua diff --git a/testdb.lua b/service/testdb.lua similarity index 100% rename from testdb.lua rename to service/testdb.lua diff --git a/testredis.lua b/service/testredis.lua similarity index 100% rename from testredis.lua rename to service/testredis.lua diff --git a/testtimer.lua b/service/testtimer.lua similarity index 100% rename from testtimer.lua rename to service/testtimer.lua diff --git a/watchdog.lua b/service/watchdog.lua similarity index 100% rename from watchdog.lua rename to service/watchdog.lua diff --git a/rwlock.h b/skynet-src/rwlock.h similarity index 100% rename from rwlock.h rename to skynet-src/rwlock.h diff --git a/skynet.h b/skynet-src/skynet.h similarity index 100% rename from skynet.h rename to skynet-src/skynet.h diff --git a/skynet-src/skynet_env.c b/skynet-src/skynet_env.c new file mode 100644 index 00000000..0bc92e8c --- /dev/null +++ b/skynet-src/skynet_env.c @@ -0,0 +1,53 @@ +#include "skynet_env.h" + +#include +#include + +#include +#include + +struct skynet_env { + int lock; + lua_State *L; +}; + +static struct skynet_env *E = NULL; + +#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} +#define UNLOCK(q) __sync_lock_release(&(q)->lock); + +const char * +skynet_getenv(const char *key) { + LOCK(E) + + lua_State *L = E->L; + + lua_getglobal(L, key); + const char * result = lua_tostring(L, -1); + lua_pop(L, 1); + + UNLOCK(E) + + return result; +} + +void +skynet_setenv(const char *key, const char *value) { + LOCK(E) + + lua_State *L = E->L; + lua_getglobal(L, key); + assert(lua_isnil(L, -1)); + lua_pop(L,1); + lua_pushstring(L,value); + lua_setglobal(L,key); + + UNLOCK(E) +} + +void +skynet_env_init() { + E = malloc(sizeof(*E)); + E->lock = 0; + E->L = luaL_newstate(); +} diff --git a/skynet-src/skynet_env.h b/skynet-src/skynet_env.h new file mode 100644 index 00000000..b314d4c2 --- /dev/null +++ b/skynet-src/skynet_env.h @@ -0,0 +1,9 @@ +#ifndef SKYNET_ENV_H +#define SKYNET_ENV_H + +const char * skynet_getenv(const char *key); +void skynet_setenv(const char *key, const char *value); + +void skynet_env_init(); + +#endif diff --git a/skynet_error.c b/skynet-src/skynet_error.c similarity index 100% rename from skynet_error.c rename to skynet-src/skynet_error.c diff --git a/skynet_handle.c b/skynet-src/skynet_handle.c similarity index 100% rename from skynet_handle.c rename to skynet-src/skynet_handle.c diff --git a/skynet_handle.h b/skynet-src/skynet_handle.h similarity index 100% rename from skynet_handle.h rename to skynet-src/skynet_handle.h diff --git a/skynet_harbor.c b/skynet-src/skynet_harbor.c similarity index 100% rename from skynet_harbor.c rename to skynet-src/skynet_harbor.c diff --git a/skynet_harbor.h b/skynet-src/skynet_harbor.h similarity index 100% rename from skynet_harbor.h rename to skynet-src/skynet_harbor.h diff --git a/skynet_imp.h b/skynet-src/skynet_imp.h similarity index 100% rename from skynet_imp.h rename to skynet-src/skynet_imp.h diff --git a/skynet_logger.c b/skynet-src/skynet_logger.c similarity index 100% rename from skynet_logger.c rename to skynet-src/skynet_logger.c diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c new file mode 100644 index 00000000..8d4655c9 --- /dev/null +++ b/skynet-src/skynet_main.c @@ -0,0 +1,117 @@ +#include "skynet_imp.h" +#include "skynet_env.h" + +#include +#include +#include +#include +#include +#include + +static int +optint(const char *key, int opt) { + const char * str = skynet_getenv(key); + if (str == NULL) { + char tmp[20]; + sprintf(tmp,"%d",opt); + skynet_setenv(key, tmp); + return opt; + } + return strtol(str, NULL, 10); +} + +static int +optboolean(const char *key, int opt) { + const char * str = skynet_getenv(key); + if (str == NULL) { + skynet_setenv(key, opt ? "true" : "false"); + return opt; + } + return strcmp(str,"true")==0; +} + +static const char * +optstring(const char *key,const char * opt) { + const char * str = skynet_getenv(key); + if (str == NULL) { + if (opt) { + skynet_setenv(key, opt); + } + return opt; + } + return str; +} + +static void +_init_env(lua_State *L) { + lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); + lua_pushnil(L); /* first key */ + 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); + skynet_setenv(key,b ? "true" : "false" ); + } else { + const char * value = lua_tostring(L,-1); + if (value == NULL) { + fprintf(stderr, "Invalid config table key = %s\n", key); + exit(1); + } + skynet_setenv(key,value); + } + lua_pop(L,1); + } + lua_pop(L,1); +} + +int +main(int argc, char *argv[]) { + const char * config_file = "config"; + if (argc > 1) { + config_file = argv[1]; + } + skynet_env_init(); + + struct skynet_config config; + + struct lua_State *L = luaL_newstate(); + luaL_openlibs(L); // link lua lib + lua_close(L); + + L = luaL_newstate(); + + int err = luaL_dofile(L, config_file); + if (err) { + fprintf(stderr,"%s\n",lua_tostring(L,-1)); + lua_close(L); + return 1; + } + _init_env(L); + + const char *path = optstring("lua_path","./lualib/?.lua;./lualib/?/init.lua"); + setenv("LUA_PATH",path,1); + const char *cpath = optstring("lua_cpath","./lualib/?.so"); + setenv("LUA_CPATH",cpath,1); + optstring("luaservice","./service/?.lua"); + + config.thread = optint("thread",8); + config.mqueue_size = optint("mqueue",256); + config.module_path = optstring("cpath","./service/?.so"); + config.logger = optstring("logger",NULL); + config.harbor = optint("harbor", 1); + config.master = optstring("master","tcp://127.0.0.1:2012"); + config.start = optstring("start","main.lua"); + config.local = optstring("address","tcp://127.0.0.1:2525"); + config.standalone = optboolean("standalone",0); + + lua_close(L); + + skynet_start(&config); + + return 0; +} diff --git a/skynet_module.c b/skynet-src/skynet_module.c similarity index 86% rename from skynet_module.c rename to skynet-src/skynet_module.c index acbac17e..0ff2de3a 100644 --- a/skynet_module.c +++ b/skynet-src/skynet_module.c @@ -8,7 +8,6 @@ #include #define MAX_MODULE_TYPE 32 -#define SO ".so" struct modules { int count; @@ -21,23 +20,33 @@ static struct modules * M = NULL; static void * _try_open(struct modules *m, const char * name) { - size_t path_size = strlen(m->path); + const char * path = m->path; + size_t path_size = strlen(path); size_t name_size = strlen(name); - char tmp[path_size + name_size + sizeof(SO)]; - memcpy(tmp, m->path, path_size); - memcpy(tmp + path_size , name, name_size); - memcpy(tmp + path_size + name_size , SO, sizeof(SO)); + + int sz = path_size + name_size; + char tmp[sz]; + int i; + for (i=0;path[i]!='?' && path[i]!='\0';i++) { + tmp[i] = path[i]; + } + memcpy(tmp+i,name,name_size); + if (path[i] == '?') { + strcpy(tmp+i+name_size,path+i+1); + } else { + fprintf(stderr,"Invalid C service path\n"); + exit(1); + } void * dl = dlopen(tmp, RTLD_NOW | RTLD_GLOBAL); if (dl == NULL) { - printf("try open %s failed : %s\n",tmp,dlerror()); + fprintf(stderr, "try open %s failed : %s\n",tmp,dlerror()); } return dl; } - static struct skynet_module * _query(const char * name) { int i; diff --git a/skynet_module.h b/skynet-src/skynet_module.h similarity index 100% rename from skynet_module.h rename to skynet-src/skynet_module.h diff --git a/skynet_mq.c b/skynet-src/skynet_mq.c similarity index 100% rename from skynet_mq.c rename to skynet-src/skynet_mq.c diff --git a/skynet_mq.h b/skynet-src/skynet_mq.h similarity index 100% rename from skynet_mq.h rename to skynet-src/skynet_mq.h diff --git a/skynet_server.c b/skynet-src/skynet_server.c similarity index 96% rename from skynet_server.c rename to skynet-src/skynet_server.c index d4e2dfa2..9616d91a 100644 --- a/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -4,6 +4,7 @@ #include "skynet_mq.h" #include "skynet_timer.h" #include "skynet_harbor.h" +#include "skynet_env.h" #include "skynet.h" #include @@ -314,6 +315,25 @@ skynet_command(struct skynet_context * context, const char * cmd , const char * } } + if (strcmp(cmd,"GETENV") == 0) { + return skynet_getenv(param); + } + + if (strcmp(cmd,"SETENV") == 0) { + size_t sz = strlen(param); + char key[sz+1]; + int i; + for (i=0;param[i] == ' ';i++) { + key[i] = param[i]; + break; + } + key[i] = '\0'; + param += i+1; + + skynet_setenv(key,param); + return NULL; + } + return NULL; } diff --git a/skynet_server.h b/skynet-src/skynet_server.h similarity index 100% rename from skynet_server.h rename to skynet-src/skynet_server.h diff --git a/skynet_start.c b/skynet-src/skynet_start.c similarity index 100% rename from skynet_start.c rename to skynet-src/skynet_start.c diff --git a/skynet_system.h b/skynet-src/skynet_system.h similarity index 100% rename from skynet_system.h rename to skynet-src/skynet_system.h diff --git a/skynet_timer.c b/skynet-src/skynet_timer.c similarity index 100% rename from skynet_timer.c rename to skynet-src/skynet_timer.c diff --git a/skynet_timer.h b/skynet-src/skynet_timer.h similarity index 100% rename from skynet_timer.h rename to skynet-src/skynet_timer.h diff --git a/skynet_main.c b/skynet_main.c deleted file mode 100644 index 027685dc..00000000 --- a/skynet_main.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "skynet_imp.h" - -#include -#include -#include -#include -#include -#include - -static int -optint(lua_State *L, const char *key, int opt) { - lua_getglobal(L, key); - int n = lua_tointeger(L,-1); - lua_pop(L,1); - if (n==0) - return opt; - return n; -} - -static int -optboolean(lua_State *L, const char *key, int opt) { - lua_getglobal(L, key); - int n = lua_toboolean(L,-1); - lua_pop(L,1); - if (n==0) - return opt; - return n; -} - -static const char * -optstring(lua_State *L, const char *key,const char * opt) { - lua_getglobal(L, key); - const char * str = lua_tostring(L,-1); - lua_pop(L,1); - if (str == NULL) { - return opt; - } - return strdup(str); -} - -int -main(int argc, char *argv[]) { - const char * config_file = "config"; - if (argc > 1) { - config_file = argv[1]; - } - struct skynet_config config; - - struct lua_State *L = luaL_newstate(); - luaL_openlibs(L); // link lua lib - lua_close(L); - - L = luaL_newstate(); - - int err = luaL_dofile(L, config_file); - if (err) { - fprintf(stderr,"%s\n",lua_tostring(L,-1)); - lua_close(L); - return 1; - } - const char *path = optstring(L,"lua_path","./?.lua;./?/init.lua"); - setenv("LUA_PATH",path,1); - const char *cpath = optstring(L,"lua_cpath","./?.so"); - setenv("LUA_CPATH",cpath,1); - - config.thread = optint(L,"thread",8); - config.mqueue_size = optint(L,"mqueue",256); - config.module_path = optstring(L,"cpath","./"); - config.logger = optstring(L,"logger",NULL); - config.harbor = optint(L, "harbor", 1); - config.master = optstring(L,"master","tcp://127.0.0.1:2012"); - config.start = optstring(L,"start","main.lua"); - config.local = optstring(L,"address","tcp://127.0.0.1:2525"); - config.standalone = optboolean(L,"standalone",0); - lua_close(L); - - skynet_start(&config); - - return 0; -}