change source dir

This commit is contained in:
云风
2012-08-09 15:36:56 +08:00
parent d23c4aa905
commit eed9c42fd4
46 changed files with 284 additions and 118 deletions

View File

@@ -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 skynet : \
gcc -Wall -g -Wl,-E -o $@ $^ -Imaster -lpthread -ldl -lrt -Wl,-E -llua -lm -lzmq 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 service/logger.so : skynet-src/skynet_logger.c
gcc -Wall -g -fPIC --shared $^ -o $@ gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src
snlua.so : service_lua.c service/snlua.so : service-src/service_lua.c
gcc -Wall -g -fPIC --shared $^ -o $@ gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src
gate.so : gate/mread.c gate/ringbuffer.c gate/main.c service/gate.so : gate/mread.c gate/ringbuffer.c gate/main.c
gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Igate gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Igate -Iskynet-src
skynet.so : lua-skynet.c lua-serialize/serialize.c lualib/skynet.so : lualib-src/lua-skynet.c lua-serialize/serialize.c
gcc -Wall -g -fPIC --shared $^ -o $@ gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src -Ilua-serialize
client.so : service_client.c service/client.so : service-src/service_client.c
gcc -Wall -g -fPIC --shared $^ -o $@ gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src
connection.so : connection/connection.c connection/main.c service/connection.so : connection/connection.c connection/main.c
gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Iconnection gcc -Wall -g -fPIC --shared -o $@ $^ -I. -Iconnection -Iskynet-src
broker.so : service_broker.c service/broker.so : service-src/service_broker.c
gcc -Wall -g -fPIC --shared $^ -o $@ gcc -Wall -g -fPIC --shared $^ -o $@ -Iskynet-src
client : client.c client : client-src/client.c
gcc -Wall -g $^ -o $@ -lpthread gcc -Wall -g $^ -o $@ -lpthread
skynet-master : master/master.c master/main.c skynet-master : master/master.c master/main.c
gcc -g -Wall -Imaster -o $@ $^ -lzmq gcc -g -Wall -Imaster -o $@ $^ -lzmq
clean : clean :
rm skynet client skynet-master *.so rm skynet client skynet-master lualib/*.so service/*.so

5
config
View File

@@ -1,9 +1,10 @@
thread = 8 thread = 8
mqueue = 256 mqueue = 256
cpath = "./" cpath = "./service/?.so"
logger = nil logger = nil
harbor = 1 harbor = 1
address = "tcp://127.0.0.1:2525" address = "tcp://127.0.0.1:2525"
master = "tcp://127.0.0.1:2012" master = "tcp://127.0.0.1:2012"
start = "main.lua" start = "main"
standalone = true standalone = true
luaservice = "./service/?.lua"

View File

@@ -1,8 +1,8 @@
thread = 2 thread = 2
mqueue = 256 mqueue = 256
cpath = "./" cpath = "./service/?.so"
logger = nil logger = nil
harbor = 2 harbor = 2
address = "tcp://127.0.0.1:2526" address = "tcp://127.0.0.1:2526"
master = "tcp://127.0.0.1:2012" master = "tcp://127.0.0.1:2012"
start = "main_log.lua" start = "main_log"

View File

@@ -1,5 +1,5 @@
#include "skynet.h" #include "skynet.h"
#include "lua-serialize/luaseri.h" #include "luaseri.h"
#include <lua.h> #include <lua.h>
#include <lauxlib.h> #include <lauxlib.h>

View File

@@ -15,7 +15,23 @@ snlua_create(void) {
static int static int
_load(lua_State *L, char ** filename) { _load(lua_State *L, char ** filename) {
const char * name = strsep(filename, " \r\n"); 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; return r != LUA_OK;
} }

View File

@@ -4,19 +4,19 @@ skynet.dispatch()
skynet.start(function() skynet.start(function()
print("Server start") print("Server start")
local launcher = skynet.launch("snlua","launcher.lua") local launcher = skynet.launch("snlua","launcher")
print("launcher", launcher) print("launcher", launcher)
local console = skynet.launch("snlua","console.lua") local console = skynet.launch("snlua","console")
print("console",console) print("console",console)
local watchdog = skynet.launch("snlua","watchdog.lua") local watchdog = skynet.launch("snlua","watchdog")
print("watchdog",watchdog) print("watchdog",watchdog)
local gate = skynet.launch("gate","8888 4 0") local gate = skynet.launch("gate","8888 4 0")
print("gate",gate) print("gate",gate)
local db = skynet.launch("snlua","simpledb.lua") local db = skynet.launch("snlua","simpledb")
print("simpledb",db) print("simpledb",db)
local connection = skynet.launch("connection","256") local connection = skynet.launch("connection","256")
print("connection",connection) 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) print("redis",redis)
skynet.exit() skynet.exit()
end) end)

53
skynet-src/skynet_env.c Normal file
View File

@@ -0,0 +1,53 @@
#include "skynet_env.h"
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <assert.h>
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();
}

9
skynet-src/skynet_env.h Normal file
View File

@@ -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

117
skynet-src/skynet_main.c Normal file
View File

@@ -0,0 +1,117 @@
#include "skynet_imp.h"
#include "skynet_env.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.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);
}
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;
}

View File

@@ -8,7 +8,6 @@
#include <stdio.h> #include <stdio.h>
#define MAX_MODULE_TYPE 32 #define MAX_MODULE_TYPE 32
#define SO ".so"
struct modules { struct modules {
int count; int count;
@@ -21,23 +20,33 @@ static struct modules * M = NULL;
static void * static void *
_try_open(struct modules *m, const char * name) { _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); size_t name_size = strlen(name);
char tmp[path_size + name_size + sizeof(SO)];
memcpy(tmp, m->path, path_size); int sz = path_size + name_size;
memcpy(tmp + path_size , name, name_size); char tmp[sz];
memcpy(tmp + path_size + name_size , SO, sizeof(SO)); 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); void * dl = dlopen(tmp, RTLD_NOW | RTLD_GLOBAL);
if (dl == NULL) { if (dl == NULL) {
printf("try open %s failed : %s\n",tmp,dlerror()); fprintf(stderr, "try open %s failed : %s\n",tmp,dlerror());
} }
return dl; return dl;
} }
static struct skynet_module * static struct skynet_module *
_query(const char * name) { _query(const char * name) {
int i; int i;

View File

@@ -4,6 +4,7 @@
#include "skynet_mq.h" #include "skynet_mq.h"
#include "skynet_timer.h" #include "skynet_timer.h"
#include "skynet_harbor.h" #include "skynet_harbor.h"
#include "skynet_env.h"
#include "skynet.h" #include "skynet.h"
#include <string.h> #include <string.h>
@@ -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; return NULL;
} }

View File

@@ -1,80 +0,0 @@
#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 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;
}