diff --git a/agent.lua b/agent.lua index 6c1734b6..9f09e6fd 100644 --- a/agent.lua +++ b/agent.lua @@ -5,7 +5,7 @@ skynet.dispatch(function(msg,session) if session == 0 then print("client command",msg) local result = skynet.call("SIMPLEDB",msg) - skynet.send(client,0,result) + skynet.send(client, result) else print("server command",msg) if msg == "CLOSE" then diff --git a/gate/main.c b/gate/main.c index 03c7e642..50418ad4 100644 --- a/gate/main.c +++ b/gate/main.c @@ -178,7 +178,7 @@ _cb(struct skynet_context * ctx, void * ud, int session, const char * uid, const struct mread_pool * m = g->pool; int connection_id = mread_poll(m,100); // timeout : 100ms if (connection_id < 0) { - skynet_command(ctx, "TIMEOUT", 0, "1"); + skynet_command(ctx, "TIMEOUT", "1"); } else { int id = g->map[connection_id].uid; if (id == 0) { @@ -209,7 +209,7 @@ _cb(struct skynet_context * ctx, void * ud, int session, const char * uid, const _forward(ctx, g, id, data, *plen); mread_yield(m); _break: - skynet_command(ctx, "TIMEOUT",0,"0"); + skynet_command(ctx, "TIMEOUT", "0"); } } @@ -249,8 +249,8 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) { } skynet_callback(ctx,g,_cb); - skynet_command(ctx,"REG",0,"gate"); - skynet_command(ctx,"TIMEOUT",0, "0"); + skynet_command(ctx,"REG","gate"); + skynet_command(ctx,"TIMEOUT","0"); return 0; } diff --git a/globallog.lua b/globallog.lua index f6379996..b202f4ce 100644 --- a/globallog.lua +++ b/globallog.lua @@ -1,7 +1,7 @@ local skynet = require "skynet" skynet.dispatch(function(message, session , from) - print("[GLOBALLOG]",session, from,message) + print("[GLOBALLOG]", from,message) end) skynet.register "LOG" \ No newline at end of file diff --git a/lua-skynet.c b/lua-skynet.c index 404d6d67..968f49d5 100644 --- a/lua-skynet.c +++ b/lua-skynet.c @@ -59,24 +59,13 @@ static int _command(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); const char * cmd = luaL_checkstring(L,1); - const char * parm = NULL; - int session = 0; const char * result; - int top = lua_gettop(L); - if (top == 2) { - if (lua_type(L,2) == LUA_TNUMBER) { - session = lua_tointeger(L,2); - } else { - parm = luaL_checkstring(L,2); - } - } else if (top == 3) { - session = lua_tointeger(L,2); - parm = luaL_checkstring(L,3); - } else if (top != 1) { - luaL_error(L, "skynet.command support only 3 parms (%d)",top); + const char * parm = NULL; + if (lua_gettop(L) == 2) { + parm = luaL_checkstring(L,2); } - result = skynet_command(context, cmd, session, parm); + result = skynet_command(context, cmd, parm); if (result) { lua_pushstring(L, result); return 1; @@ -87,7 +76,7 @@ _command(lua_State *L) { static int _send(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - int session = -1; + int session = 0; int index = 0; const char * dest = luaL_checkstring(L,1); diff --git a/skynet.h b/skynet.h index a6bd72ad..9dd4dcbb 100644 --- a/skynet.h +++ b/skynet.h @@ -7,7 +7,7 @@ struct skynet_context; void skynet_error(struct skynet_context * context, const char *msg, ...); -const char * skynet_command(struct skynet_context * context, const char * cmd , int session, const char * parm); +const char * skynet_command(struct skynet_context * context, const char * cmd , const char * parm); int skynet_send(struct skynet_context * context, const char * addr , int session, void * msg, size_t sz); typedef void (*skynet_cb)(struct skynet_context * context, void *ud, int session, const char * addr , const void * msg, size_t sz); diff --git a/skynet.lua b/skynet.lua index bdb803c0..8f18bda3 100644 --- a/skynet.lua +++ b/skynet.lua @@ -82,13 +82,15 @@ end function skynet.dispatch(f) c.callback(function(session, address , message) - local co = session_id_coroutine[session] - if co == nil then + if session <= 0 then + session = - session co = coroutine.create(f) session_coroutine_id[co] = session session_coroutine_address[co] = address suspend(co, coroutine.resume(co, message, session, address)) else + local co = session_id_coroutine[session] + assert(co, session) session_id_coroutine[session] = nil suspend(co, coroutine.resume(co, message)) end @@ -96,9 +98,9 @@ function skynet.dispatch(f) end function skynet.start(f) - c.command("TIMEOUT",0,"0") + local session = c.command("TIMEOUT","0") local co = coroutine.create(f) - session_id_coroutine[0] = co + session_id_coroutine[tonumber(session)] = co end return skynet diff --git a/skynet_logger.c b/skynet_logger.c index 0f11b91c..64a655be 100644 --- a/skynet_logger.c +++ b/skynet_logger.c @@ -45,7 +45,7 @@ logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm) } if (inst->handle) { skynet_callback(ctx, inst, _logger); - skynet_command(ctx, "REG", 0, ".logger"); + skynet_command(ctx, "REG", ".logger"); return 0; } return 1; diff --git a/skynet_server.c b/skynet_server.c index a1b4447d..92e814ee 100644 --- a/skynet_server.c +++ b/skynet_server.c @@ -197,13 +197,14 @@ skynet_context_message_dispatch(void) { } const char * -skynet_command(struct skynet_context * context, const char * cmd , int session, const char * parm) { +skynet_command(struct skynet_context * context, const char * cmd , const char * parm) { if (strcmp(cmd,"TIMEOUT") == 0) { char * session_ptr = NULL; int ti = strtol(parm, &session_ptr, 10); - session = skynet_timeout(context->handle, ti, session); - if (session < 0) + int session = skynet_context_newsession(context); + if (session < 0) return NULL; + skynet_timeout(context->handle, ti, session); sprintf(context->result, "%d", session); return context->result; } @@ -268,8 +269,10 @@ skynet_command(struct skynet_context * context, const char * cmd , int session, int skynet_send(struct skynet_context * context, const char * addr , int session, void * msg, size_t sz) { + int session_id = session; if (session < 0) { session = skynet_context_newsession(context); + session_id = - session; } uint32_t des = 0; if (addr[0] == ':') { @@ -284,7 +287,7 @@ skynet_send(struct skynet_context * context, const char * addr , int session, vo } else { struct skynet_message smsg; smsg.source = context->handle; - smsg.session = session; + smsg.session = session_id; smsg.data = msg; smsg.sz = sz; skynet_harbor_send(addr, 0, &smsg); @@ -295,7 +298,7 @@ skynet_send(struct skynet_context * context, const char * addr , int session, vo struct skynet_message smsg; smsg.source = context->handle; - smsg.session = session; + smsg.session = session_id; smsg.data = msg; smsg.sz = sz; @@ -332,7 +335,6 @@ skynet_context_push(uint32_t handle, struct skynet_message *message) { if (ctx == NULL) { return -1; } - assert(message->session >= 0); skynet_mq_push(ctx->queue, message); if (__sync_lock_test_and_set(&ctx->in_global_queue,1) == 0) { skynet_globalmq_push(ctx->queue); diff --git a/skynet_timer.c b/skynet_timer.c index 3beadbb7..4ab635a2 100644 --- a/skynet_timer.c +++ b/skynet_timer.c @@ -174,15 +174,6 @@ timer_create_timer() int skynet_timeout(uint32_t handle, int time, int session) { - if (session < 0) { - struct skynet_context * ctx = skynet_handle_grab(handle); - if (ctx == NULL) { - return -1; - } - session = skynet_context_newsession(ctx); - skynet_context_release(ctx); - } - if (time == 0) { struct skynet_message message; message.source = SKYNET_SYSTEM_TIMER; diff --git a/watchdog.lua b/watchdog.lua index 937e8aca..12f836dd 100644 --- a/watchdog.lua +++ b/watchdog.lua @@ -6,28 +6,28 @@ local agent_all = {} function command:open(parm) local fd,addr = string.match(parm,"(%d+) ([^%s]+)") fd = tonumber(fd) - skynet.send("LOG", 0, string.format("%d %d %s",self,fd,addr)) + skynet.send("LOG", string.format("%d %d %s",self,fd,addr)) local client = skynet.launch("client",fd) - print("client",client) + skynet.send("LOG", "client " .. client) local agent = skynet.launch("snlua","agent.lua",client) if agent then agent_all[self] = agent - skynet.send("gate",0, "forward ".. self .. " " .. agent) + skynet.send("gate", "forward ".. self .. " " .. agent) end end function command:close() - skynet.send("LOG",0,string.format("close %d",self)) - skynet.send(agent_all[self],1,"CLOSE") + skynet.send("LOG", string.format("close %d",self)) + skynet.send(agent_all[self],-1,"CLOSE") agent_all[self] = nil end function command:data(data) local agent = agent_all[self] if agent then - skynet.send(agent,0,data) + skynet.send(agent, data) else - skynet.send("LOG",0,string.format("data %d size=%d",self,#data)) + skynet.send("LOG", string.format("data %d size=%d",self,#data)) end end