redirect support alloc session

This commit is contained in:
Cloud Wu
2017-04-07 11:11:39 +08:00
parent 1f4a84edc2
commit 6fa241453c
3 changed files with 48 additions and 72 deletions

View File

@@ -9,7 +9,6 @@ skynet.start(function()
if not skynet.getenv "daemon" then if not skynet.getenv "daemon" then
local console = skynet.newservice("console") local console = skynet.newservice("console")
end end
--[[
skynet.newservice("debug_console",8000) skynet.newservice("debug_console",8000)
skynet.newservice("simpledb") skynet.newservice("simpledb")
local watchdog = skynet.newservice("watchdog") local watchdog = skynet.newservice("watchdog")
@@ -19,6 +18,5 @@ skynet.start(function()
nodelay = true, nodelay = true,
}) })
skynet.error("Watchdog listen on", 8888) skynet.error("Watchdog listen on", 8888)
]]
skynet.exit() skynet.exit()
end) end)

View File

@@ -174,17 +174,8 @@ get_dest_string(lua_State *L, int index) {
return dest_string; return dest_string;
} }
/*
uint32 address
string address
integer type
integer session
string message
lightuserdata message_ptr
integer len
*/
static int 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)); struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
uint32_t dest = (uint32_t)lua_tointeger(L, 1); uint32_t dest = (uint32_t)lua_tointeger(L, 1);
const char * dest_string = NULL; const char * dest_string = NULL;
@@ -195,68 +186,19 @@ lsend(lua_State *L) {
dest_string = get_dest_string(L, 1); dest_string = get_dest_string(L, 1);
} }
int type = luaL_checkinteger(L, 2); int type = luaL_checkinteger(L, idx_type+0);
int session = 0; int session = 0;
if (lua_isnil(L,3)) { if (lua_isnil(L,idx_type+1)) {
type |= PTYPE_TAG_ALLOCSESSION; type |= PTYPE_TAG_ALLOCSESSION;
} else { } 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) { switch (mtype) {
case LUA_TSTRING: { case LUA_TSTRING: {
size_t len = 0; size_t len = 0;
void * msg = (void *)lua_tolstring(L,4,&len); void * msg = (void *)lua_tolstring(L,idx_type+2,&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);
if (len == 0) { if (len == 0) {
msg = NULL; msg = NULL;
} }
@@ -268,8 +210,8 @@ lredirect(lua_State *L) {
break; break;
} }
case LUA_TLIGHTUSERDATA: { case LUA_TLIGHTUSERDATA: {
void * msg = lua_touserdata(L,5); void * msg = lua_touserdata(L,idx_type+2);
int size = luaL_checkinteger(L,6); int size = luaL_checkinteger(L,idx_type+3);
if (dest_string) { if (dest_string) {
session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size);
} else { } else {
@@ -278,9 +220,45 @@ lredirect(lua_State *L) {
break; break;
} }
default: 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 static int

View File

@@ -331,7 +331,7 @@ function skynet.exit()
for co, session in pairs(session_coroutine_id) do for co, session in pairs(session_coroutine_id) do
local address = session_coroutine_address[co] local address = session_coroutine_address[co]
if session~=0 and address then if session~=0 and address then
c.redirect(address, 0, skynet.PTYPE_ERROR, session, "") c.send(address, skynet.PTYPE_ERROR, session, "")
end end
end end
for resp in pairs(unresponse) do for resp in pairs(unresponse) do
@@ -343,7 +343,7 @@ function skynet.exit()
tmp[address] = true tmp[address] = true
end end
for address in pairs(tmp) do for address in pairs(tmp) do
c.redirect(address, 0, skynet.PTYPE_ERROR, 0, "") c.send(address, skynet.PTYPE_ERROR, 0, "")
end end
c.command("EXIT") c.command("EXIT")
-- quit service -- quit service