mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
144 lines
3.0 KiB
C
144 lines
3.0 KiB
C
#include "skynet.h"
|
|
|
|
#include "skynet_imp.h"
|
|
#include "skynet_env.h"
|
|
#include "skynet_server.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <lua.h>
|
|
#include <lualib.h>
|
|
#include <lauxlib.h>
|
|
#include <signal.h>
|
|
#include <assert.h>
|
|
|
|
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);
|
|
opt = skynet_getenv(key);
|
|
}
|
|
return opt;
|
|
}
|
|
return str;
|
|
}
|
|
|
|
static void
|
|
_init_env(lua_State *L) {
|
|
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 sigign() {
|
|
struct sigaction sa;
|
|
sa.sa_handler = SIG_IGN;
|
|
sigaction(SIGPIPE, &sa, 0);
|
|
return 0;
|
|
}
|
|
|
|
static const char * load_config = "\
|
|
local config_name = ...\
|
|
local f = assert(io.open(config_name))\
|
|
local code = assert(f:read \'*a\')\
|
|
local function getenv(name) return assert(os.getenv(name), name) end\
|
|
code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\
|
|
f:close()\
|
|
local result = {}\
|
|
assert(load(code,\'=(load)\',\'t\',result))()\
|
|
return result\
|
|
";
|
|
|
|
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");
|
|
return 1;
|
|
}
|
|
skynet_globalinit();
|
|
skynet_env_init();
|
|
|
|
sigign();
|
|
|
|
struct skynet_config config;
|
|
|
|
struct lua_State *L = lua_newstate(skynet_lalloc, NULL);
|
|
luaL_openlibs(L); // link lua lib
|
|
|
|
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);
|
|
|
|
config.thread = optint("thread",8);
|
|
config.module_path = optstring("cpath","./cservice/?.so");
|
|
config.harbor = optint("harbor", 1);
|
|
config.bootstrap = optstring("bootstrap","snlua bootstrap");
|
|
config.daemon = optstring("daemon", NULL);
|
|
config.logger = optstring("logger", NULL);
|
|
|
|
lua_close(L);
|
|
|
|
skynet_start(&config);
|
|
skynet_globalexit();
|
|
|
|
return 0;
|
|
}
|