From 6fa241453c6964f1839f7c79b4775f8ed5d6ea65 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 7 Apr 2017 11:11:39 +0800 Subject: [PATCH] redirect support alloc session --- examples/main.lua | 2 - lualib-src/lua-skynet.c | 114 ++++++++++++++++------------------------ lualib/skynet.lua | 4 +- 3 files changed, 48 insertions(+), 72 deletions(-) diff --git a/examples/main.lua b/examples/main.lua index 1b371707..5a2150db 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -9,7 +9,6 @@ skynet.start(function() if not skynet.getenv "daemon" then local console = skynet.newservice("console") end ---[[ skynet.newservice("debug_console",8000) skynet.newservice("simpledb") local watchdog = skynet.newservice("watchdog") @@ -19,6 +18,5 @@ skynet.start(function() nodelay = true, }) skynet.error("Watchdog listen on", 8888) -]] skynet.exit() end) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 074e293c..daab81ed 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -174,17 +174,8 @@ get_dest_string(lua_State *L, int index) { return dest_string; } -/* - uint32 address - string address - integer type - integer session - string message - lightuserdata message_ptr - integer len - */ static int -lsend(lua_State *L) { +send_message(lua_State *L, int source, int idx_type) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; @@ -195,68 +186,19 @@ lsend(lua_State *L) { dest_string = get_dest_string(L, 1); } - int type = luaL_checkinteger(L, 2); + int type = luaL_checkinteger(L, idx_type+0); int session = 0; - if (lua_isnil(L,3)) { + if (lua_isnil(L,idx_type+1)) { type |= PTYPE_TAG_ALLOCSESSION; } else { - session = luaL_checkinteger(L,3); + session = luaL_checkinteger(L,idx_type+1); } - int mtype = lua_type(L,4); + int mtype = lua_type(L,idx_type+2); switch (mtype) { case LUA_TSTRING: { size_t len = 0; - void * msg = (void *)lua_tolstring(L,4,&len); - if (len == 0) { - msg = NULL; - } - if (dest_string) { - session = skynet_sendname(context, 0, dest_string, type, session , msg, len); - } else { - session = skynet_send(context, 0, dest, type, session , msg, len); - } - break; - } - case LUA_TLIGHTUSERDATA: { - void * msg = lua_touserdata(L,4); - int size = luaL_checkinteger(L,5); - if (dest_string) { - session = skynet_sendname(context, 0, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); - } else { - session = skynet_send(context, 0, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); - } - break; - } - default: - luaL_error(L, "skynet.send invalid param %s", lua_typename(L, lua_type(L,4))); - } - if (session < 0) { - // send to invalid address - // todo: maybe throw an error would be better - return 0; - } - lua_pushinteger(L,session); - return 1; -} - -static int -lredirect(lua_State *L) { - struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); - uint32_t dest = (uint32_t)lua_tointeger(L,1); - const char * dest_string = NULL; - if (dest == 0) { - dest_string = get_dest_string(L, 1); - } - uint32_t source = (uint32_t)luaL_checkinteger(L,2); - int type = luaL_checkinteger(L,3); - int session = luaL_checkinteger(L,4); - - int mtype = lua_type(L,5); - switch (mtype) { - case LUA_TSTRING: { - size_t len = 0; - void * msg = (void *)lua_tolstring(L,5,&len); + void * msg = (void *)lua_tolstring(L,idx_type+2,&len); if (len == 0) { msg = NULL; } @@ -268,8 +210,8 @@ lredirect(lua_State *L) { break; } case LUA_TLIGHTUSERDATA: { - void * msg = lua_touserdata(L,5); - int size = luaL_checkinteger(L,6); + void * msg = lua_touserdata(L,idx_type+2); + int size = luaL_checkinteger(L,idx_type+3); if (dest_string) { session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); } else { @@ -278,9 +220,45 @@ lredirect(lua_State *L) { break; } default: - luaL_error(L, "skynet.redirect invalid param %s", lua_typename(L,mtype)); + luaL_error(L, "invalid param %s", lua_typename(L, lua_type(L,idx_type+2))); } - return 0; + if (session < 0) { + // send to invalid address + // todo: maybe throw an error would be better + return 0; + } + lua_pushinteger(L,session); + return 1; +} + +/* + uint32 address + string address + integer type + integer session + string message + lightuserdata message_ptr + integer len + */ +static int +lsend(lua_State *L) { + return send_message(L, 0, 2); +} + +/* + uint32 address + string address + integer source_address + integer type + integer session + string message + lightuserdata message_ptr + integer len + */ +static int +lredirect(lua_State *L) { + uint32_t source = (uint32_t)luaL_checkinteger(L,2); + return send_message(L, source, 3); } static int diff --git a/lualib/skynet.lua b/lualib/skynet.lua index b2d9ebce..900dc5c1 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -331,7 +331,7 @@ function skynet.exit() for co, session in pairs(session_coroutine_id) do local address = session_coroutine_address[co] if session~=0 and address then - c.redirect(address, 0, skynet.PTYPE_ERROR, session, "") + c.send(address, skynet.PTYPE_ERROR, session, "") end end for resp in pairs(unresponse) do @@ -343,7 +343,7 @@ function skynet.exit() tmp[address] = true end for address in pairs(tmp) do - c.redirect(address, 0, skynet.PTYPE_ERROR, 0, "") + c.send(address, skynet.PTYPE_ERROR, 0, "") end c.command("EXIT") -- quit service