From 6fa436e8ff980c05239c8aaa5015d424d0065bdc Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 10 Aug 2015 22:06:36 +0800 Subject: [PATCH] add core.intcommand , See Issue #321 --- lualib-src/lua-skynet.c | 23 +++++++++++++++++++++++ lualib/skynet.lua | 12 +++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index f1594f79..c92ff98d 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -118,6 +118,28 @@ _command(lua_State *L) { return 0; } +static int +_intcommand(lua_State *L) { + struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); + const char * cmd = luaL_checkstring(L,1); + const char * result; + const char * parm = NULL; + char tmp[64]; // for integer parm + if (lua_gettop(L) == 2) { + int32_t n = (int32_t)luaL_checkinteger(L,2); + sprintf(tmp, "%d", n); + parm = tmp; + } + + result = skynet_command(context, cmd, parm); + if (result) { + lua_Integer r = strtoll(result, NULL, 0); + lua_pushinteger(L, r); + return 1; + } + return 0; +} + static int _genid(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); @@ -310,6 +332,7 @@ luaopen_skynet_core(lua_State *L) { { "genid", _genid }, { "redirect", _redirect }, { "command" , _command }, + { "intcommand", _intcommand }, { "error", _error }, { "tostring", _tostring }, { "harbor", _harbor }, diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 8906f60d..31efffcb 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -248,18 +248,16 @@ function suspend(co, result, command, param, size) end function skynet.timeout(ti, func) - local session = c.command("TIMEOUT",tostring(ti)) + local session = c.intcommand("TIMEOUT",ti) assert(session) - session = tonumber(session) local co = co_create(func) assert(session_id_coroutine[session] == nil) session_id_coroutine[session] = co end function skynet.sleep(ti) - local session = c.command("TIMEOUT",tostring(ti)) + local session = c.intcommand("TIMEOUT",ti) assert(session) - session = tonumber(session) local succ, ret = coroutine_yield("SLEEP", session) sleep_session[coroutine.running()] = nil if succ then @@ -301,11 +299,11 @@ function skynet.localname(name) end function skynet.now() - return tonumber(c.command("NOW")) + return c.intcommand("NOW") end function skynet.starttime() - return tonumber(c.command("STARTTIME")) + return c.intcommand("STARTTIME") end function skynet.time() @@ -643,7 +641,7 @@ function skynet.endless() end function skynet.mqlen() - return tonumber(c.command "MQLEN") + return c.intcommand "MQLEN" end function skynet.task(ret)