mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
use lua for config
This commit is contained in:
4
Makefile
4
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
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
struct skynet_config {
|
||||
int thread;
|
||||
int mqueue_size;
|
||||
char * logger;
|
||||
const char * logger;
|
||||
const char * module_path;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,14 +1,63 @@
|
||||
#include "skynet_imp.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 *);
|
||||
|
||||
Reference in New Issue
Block a user