From 4c1cbaaf3e152946b77e0e545d0ca08030ce0453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Fri, 7 Sep 2012 11:04:46 +0800 Subject: [PATCH] add service manager --- gate/main.c | 17 ++++++++++------- lualib/mcgroup.lua | 7 ++++++- lualib/redis.lua | 8 +++++++- lualib/skynet.lua | 25 +++++++++++++++++++++++++ service/main.lua | 4 +--- service/redis-mgr.lua | 1 - 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/gate/main.c b/gate/main.c index c6c72a05..4f690f3a 100644 --- a/gate/main.c +++ b/gate/main.c @@ -81,9 +81,10 @@ _ctrl(struct skynet_context * ctx, struct gate * g, const void * msg, int sz) { _parm(tmp, sz, i); int uid = strtol(command , NULL, 10); struct connection * agent = _id_to_agent(g,uid); - int connection_id = agent->connection_id; - - mread_close_client(g->pool,connection_id); + if (agent) { + int connection_id = agent->connection_id; + mread_close_client(g->pool,connection_id); + } return; } if (memcmp(command,"forward",i)==0) { @@ -168,10 +169,12 @@ static void _remove_id(struct gate *g, int uid) { struct connection ** pconn = &g->agent[uid & (g->cap - 1)]; struct connection * conn = *pconn; - assert(conn->uid == uid); - conn->uid = 0; - conn->agent = 0; - *pconn = NULL; + if (conn) { + assert(conn->uid == uid); + conn->uid = 0; + conn->agent = 0; + *pconn = NULL; + } } static int diff --git a/lualib/mcgroup.lua b/lualib/mcgroup.lua index 1a441d82..a6d74d55 100644 --- a/lualib/mcgroup.lua +++ b/lualib/mcgroup.lua @@ -1,6 +1,6 @@ local skynet = require "skynet" -local SERVICE = "GROUPMGR" +local SERVICE local group = {} @@ -27,4 +27,9 @@ function group.release(id) skynet.send(SERVICE, "lua" , "CLEAR", skynet.self(), id) end +skynet.init(function() + SERVICE = skynet.call("SERVICE", "lua", "group_mgr") + skynet.call(".service","lua","group_agent") +end, "group") + return group \ No newline at end of file diff --git a/lualib/redis.lua b/lualib/redis.lua index 2b06edd2..25cd5373 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -3,6 +3,8 @@ local string = string local table = table local unpack = unpack +local redis_manager + local redis = {} local command = {} @@ -30,9 +32,13 @@ local meta = { } function redis.connect(dbname) - local handle = skynet.call(".redis-manager", "lua", dbname) + local handle = skynet.call(redis_manager, "lua", dbname) assert(handle ~= nil) return setmetatable({ __handle = handle } , meta) end +skynet.init(function() + redis_manager = skynet.call(".service", "lua", "redis-mgr") +end, "redis") + return redis diff --git a/lualib/skynet.lua b/lualib/skynet.lua index b7f30b0a..a0a8a930 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -402,9 +402,34 @@ do } end +local init_func = {} + +function skynet.init(f, name) + assert(type(f) == "function") + if init_func == nil then + f() + else + if name == nil then + table.insert(init_func, f) + else + assert(init_func[name] == nil) + init_func[name] = f + end + end +end + +local function init_all() + local funcs = init_func + init_func = nil + for k,v in pairs(funcs) do + v() + end +end + function skynet.start(f) c.callback(dispatch_message) skynet.timeout(0, function() + init_all() f() skynet.send(".launcher","lua", nil) end) diff --git a/service/main.lua b/service/main.lua index 47a8b9a2..dbfdc594 100644 --- a/service/main.lua +++ b/service/main.lua @@ -2,15 +2,13 @@ local skynet = require "skynet" skynet.start(function() print("Server start") + local service = skynet.launch("snlua","service_mgr") local connection = skynet.launch("connection","256") local lualog = skynet.launch("snlua","lualog") local console = skynet.launch("snlua","console") - local group_mgr = skynet.launch("snlua", "group_mgr") - local group_agent = skynet.launch("snlua", "group_agent") local remoteroot = skynet.launch("snlua","remote_root") local watchdog = skynet.launch("snlua","watchdog","8888 4 0") local db = skynet.launch("snlua","simpledb") - local redis = skynet.launch("snlua","redis-mgr") -- skynet.launch("snlua","testgroup") skynet.exit() diff --git a/service/redis-mgr.lua b/service/redis-mgr.lua index a0385947..be51a227 100644 --- a/service/redis-mgr.lua +++ b/service/redis-mgr.lua @@ -22,6 +22,5 @@ skynet.start(function() connection[dbname] = redis_cli skynet.ret(skynet.pack(redis_cli)) end) - skynet.register ".redis-manager" end)