diff --git a/Makefile b/Makefile index 20848d4e..678d30c8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all : skynet blackhole.so snlua.so logger.so skynet.so gate.so client 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 - gcc -Wall -g -Wl,-E -o $@ $^ -lpthread -ldl -lrt + gcc -Wall -g -Wl,-E -o $@ $^ -lpthread -ldl -lrt -Wl,-E -llua -lm blackhole.so : skynet_blackhole.c gcc -Wall -g -fPIC --shared $^ -o $@ @@ -10,7 +10,7 @@ logger.so : skynet_logger.c gcc -Wall -g -fPIC --shared $^ -o $@ snlua.so : service_lua.c - gcc -Wall -g -fPIC --shared $^ -o $@ -Wl,-E -llua -lm + gcc -Wall -g -fPIC --shared $^ -o $@ gate.so : gate/mread.c gate/ringbuffer.c gate/map.c gate/main.c gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Igate diff --git a/config b/config new file mode 100644 index 00000000..76634b75 --- /dev/null +++ b/config @@ -0,0 +1,4 @@ +thread = 8 +mqueue = 256 +cpath = "./" +logger = nil diff --git a/skynet_imp.h b/skynet_imp.h index 11fdbc6d..0e55bc8d 100644 --- a/skynet_imp.h +++ b/skynet_imp.h @@ -4,7 +4,7 @@ struct skynet_config { int thread; int mqueue_size; - char * logger; + const char * logger; const char * module_path; }; diff --git a/skynet_main.c b/skynet_main.c index 33a2b8c0..c2f19e14 100644 --- a/skynet_main.c +++ b/skynet_main.c @@ -1,14 +1,63 @@ #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 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(void) { +main(int argc, char *argv[]) { + const char * config_file = "config"; + if (argc > 1) { + config_file = argv[1]; + } struct skynet_config config; - config.thread = 8; - config.mqueue_size = 256; - config.module_path = "./"; - config.logger = NULL; + 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"); + 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); + lua_close(L); skynet_start(&config); diff --git a/skynet_server.c b/skynet_server.c index 98b4d0a7..5e008080 100644 --- a/skynet_server.c +++ b/skynet_server.c @@ -38,7 +38,7 @@ _id_to_hex(char * str, int id) { } struct skynet_context * -skynet_context_new(const char * name, char *parm) { +skynet_context_new(const char * name, const char *parm) { struct skynet_module * mod = skynet_module_query(name); if (mod == NULL) diff --git a/skynet_server.h b/skynet_server.h index 37ce3db5..87b9b76a 100644 --- a/skynet_server.h +++ b/skynet_server.h @@ -4,7 +4,7 @@ struct skynet_context; struct skynet_message; -struct skynet_context * skynet_context_new(const char * name, char * parm); +struct skynet_context * skynet_context_new(const char * name, const char * parm); void skynet_context_grab(struct skynet_context *); struct skynet_context * skynet_context_release(struct skynet_context *); int skynet_context_handle(struct skynet_context *);