mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
bugfix: use negative session id for request message
This commit is contained in:
@@ -5,7 +5,7 @@ skynet.dispatch(function(msg,session)
|
|||||||
if session == 0 then
|
if session == 0 then
|
||||||
print("client command",msg)
|
print("client command",msg)
|
||||||
local result = skynet.call("SIMPLEDB",msg)
|
local result = skynet.call("SIMPLEDB",msg)
|
||||||
skynet.send(client,0,result)
|
skynet.send(client, result)
|
||||||
else
|
else
|
||||||
print("server command",msg)
|
print("server command",msg)
|
||||||
if msg == "CLOSE" then
|
if msg == "CLOSE" then
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ _cb(struct skynet_context * ctx, void * ud, int session, const char * uid, const
|
|||||||
struct mread_pool * m = g->pool;
|
struct mread_pool * m = g->pool;
|
||||||
int connection_id = mread_poll(m,100); // timeout : 100ms
|
int connection_id = mread_poll(m,100); // timeout : 100ms
|
||||||
if (connection_id < 0) {
|
if (connection_id < 0) {
|
||||||
skynet_command(ctx, "TIMEOUT", 0, "1");
|
skynet_command(ctx, "TIMEOUT", "1");
|
||||||
} else {
|
} else {
|
||||||
int id = g->map[connection_id].uid;
|
int id = g->map[connection_id].uid;
|
||||||
if (id == 0) {
|
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);
|
_forward(ctx, g, id, data, *plen);
|
||||||
mread_yield(m);
|
mread_yield(m);
|
||||||
_break:
|
_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_callback(ctx,g,_cb);
|
||||||
skynet_command(ctx,"REG",0,"gate");
|
skynet_command(ctx,"REG","gate");
|
||||||
skynet_command(ctx,"TIMEOUT",0, "0");
|
skynet_command(ctx,"TIMEOUT","0");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
|
||||||
skynet.dispatch(function(message, session , from)
|
skynet.dispatch(function(message, session , from)
|
||||||
print("[GLOBALLOG]",session, from,message)
|
print("[GLOBALLOG]", from,message)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
skynet.register "LOG"
|
skynet.register "LOG"
|
||||||
21
lua-skynet.c
21
lua-skynet.c
@@ -59,24 +59,13 @@ static int
|
|||||||
_command(lua_State *L) {
|
_command(lua_State *L) {
|
||||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||||
const char * cmd = luaL_checkstring(L,1);
|
const char * cmd = luaL_checkstring(L,1);
|
||||||
const char * parm = NULL;
|
|
||||||
int session = 0;
|
|
||||||
const char * result;
|
const char * result;
|
||||||
int top = lua_gettop(L);
|
const char * parm = NULL;
|
||||||
if (top == 2) {
|
if (lua_gettop(L) == 2) {
|
||||||
if (lua_type(L,2) == LUA_TNUMBER) {
|
parm = luaL_checkstring(L,2);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result = skynet_command(context, cmd, session, parm);
|
result = skynet_command(context, cmd, parm);
|
||||||
if (result) {
|
if (result) {
|
||||||
lua_pushstring(L, result);
|
lua_pushstring(L, result);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -87,7 +76,7 @@ _command(lua_State *L) {
|
|||||||
static int
|
static int
|
||||||
_send(lua_State *L) {
|
_send(lua_State *L) {
|
||||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int session = -1;
|
int session = 0;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
const char * dest = luaL_checkstring(L,1);
|
const char * dest = luaL_checkstring(L,1);
|
||||||
|
|
||||||
|
|||||||
2
skynet.h
2
skynet.h
@@ -7,7 +7,7 @@
|
|||||||
struct skynet_context;
|
struct skynet_context;
|
||||||
|
|
||||||
void skynet_error(struct skynet_context * context, const char *msg, ...);
|
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);
|
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);
|
typedef void (*skynet_cb)(struct skynet_context * context, void *ud, int session, const char * addr , const void * msg, size_t sz);
|
||||||
|
|||||||
10
skynet.lua
10
skynet.lua
@@ -82,13 +82,15 @@ end
|
|||||||
|
|
||||||
function skynet.dispatch(f)
|
function skynet.dispatch(f)
|
||||||
c.callback(function(session, address , message)
|
c.callback(function(session, address , message)
|
||||||
local co = session_id_coroutine[session]
|
if session <= 0 then
|
||||||
if co == nil then
|
session = - session
|
||||||
co = coroutine.create(f)
|
co = coroutine.create(f)
|
||||||
session_coroutine_id[co] = session
|
session_coroutine_id[co] = session
|
||||||
session_coroutine_address[co] = address
|
session_coroutine_address[co] = address
|
||||||
suspend(co, coroutine.resume(co, message, session, address))
|
suspend(co, coroutine.resume(co, message, session, address))
|
||||||
else
|
else
|
||||||
|
local co = session_id_coroutine[session]
|
||||||
|
assert(co, session)
|
||||||
session_id_coroutine[session] = nil
|
session_id_coroutine[session] = nil
|
||||||
suspend(co, coroutine.resume(co, message))
|
suspend(co, coroutine.resume(co, message))
|
||||||
end
|
end
|
||||||
@@ -96,9 +98,9 @@ function skynet.dispatch(f)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function skynet.start(f)
|
function skynet.start(f)
|
||||||
c.command("TIMEOUT",0,"0")
|
local session = c.command("TIMEOUT","0")
|
||||||
local co = coroutine.create(f)
|
local co = coroutine.create(f)
|
||||||
session_id_coroutine[0] = co
|
session_id_coroutine[tonumber(session)] = co
|
||||||
end
|
end
|
||||||
|
|
||||||
return skynet
|
return skynet
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ logger_init(struct logger * inst, struct skynet_context *ctx, const char * parm)
|
|||||||
}
|
}
|
||||||
if (inst->handle) {
|
if (inst->handle) {
|
||||||
skynet_callback(ctx, inst, _logger);
|
skynet_callback(ctx, inst, _logger);
|
||||||
skynet_command(ctx, "REG", 0, ".logger");
|
skynet_command(ctx, "REG", ".logger");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@@ -197,13 +197,14 @@ skynet_context_message_dispatch(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
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) {
|
if (strcmp(cmd,"TIMEOUT") == 0) {
|
||||||
char * session_ptr = NULL;
|
char * session_ptr = NULL;
|
||||||
int ti = strtol(parm, &session_ptr, 10);
|
int ti = strtol(parm, &session_ptr, 10);
|
||||||
session = skynet_timeout(context->handle, ti, session);
|
int session = skynet_context_newsession(context);
|
||||||
if (session < 0)
|
if (session < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
skynet_timeout(context->handle, ti, session);
|
||||||
sprintf(context->result, "%d", session);
|
sprintf(context->result, "%d", session);
|
||||||
return context->result;
|
return context->result;
|
||||||
}
|
}
|
||||||
@@ -268,8 +269,10 @@ skynet_command(struct skynet_context * context, const char * cmd , int session,
|
|||||||
|
|
||||||
int
|
int
|
||||||
skynet_send(struct skynet_context * context, const char * addr , int session, void * msg, size_t sz) {
|
skynet_send(struct skynet_context * context, const char * addr , int session, void * msg, size_t sz) {
|
||||||
|
int session_id = session;
|
||||||
if (session < 0) {
|
if (session < 0) {
|
||||||
session = skynet_context_newsession(context);
|
session = skynet_context_newsession(context);
|
||||||
|
session_id = - session;
|
||||||
}
|
}
|
||||||
uint32_t des = 0;
|
uint32_t des = 0;
|
||||||
if (addr[0] == ':') {
|
if (addr[0] == ':') {
|
||||||
@@ -284,7 +287,7 @@ skynet_send(struct skynet_context * context, const char * addr , int session, vo
|
|||||||
} else {
|
} else {
|
||||||
struct skynet_message smsg;
|
struct skynet_message smsg;
|
||||||
smsg.source = context->handle;
|
smsg.source = context->handle;
|
||||||
smsg.session = session;
|
smsg.session = session_id;
|
||||||
smsg.data = msg;
|
smsg.data = msg;
|
||||||
smsg.sz = sz;
|
smsg.sz = sz;
|
||||||
skynet_harbor_send(addr, 0, &smsg);
|
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;
|
struct skynet_message smsg;
|
||||||
smsg.source = context->handle;
|
smsg.source = context->handle;
|
||||||
smsg.session = session;
|
smsg.session = session_id;
|
||||||
smsg.data = msg;
|
smsg.data = msg;
|
||||||
smsg.sz = sz;
|
smsg.sz = sz;
|
||||||
|
|
||||||
@@ -332,7 +335,6 @@ skynet_context_push(uint32_t handle, struct skynet_message *message) {
|
|||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
assert(message->session >= 0);
|
|
||||||
skynet_mq_push(ctx->queue, message);
|
skynet_mq_push(ctx->queue, message);
|
||||||
if (__sync_lock_test_and_set(&ctx->in_global_queue,1) == 0) {
|
if (__sync_lock_test_and_set(&ctx->in_global_queue,1) == 0) {
|
||||||
skynet_globalmq_push(ctx->queue);
|
skynet_globalmq_push(ctx->queue);
|
||||||
|
|||||||
@@ -174,15 +174,6 @@ timer_create_timer()
|
|||||||
|
|
||||||
int
|
int
|
||||||
skynet_timeout(uint32_t handle, int time, int session) {
|
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) {
|
if (time == 0) {
|
||||||
struct skynet_message message;
|
struct skynet_message message;
|
||||||
message.source = SKYNET_SYSTEM_TIMER;
|
message.source = SKYNET_SYSTEM_TIMER;
|
||||||
|
|||||||
14
watchdog.lua
14
watchdog.lua
@@ -6,28 +6,28 @@ local agent_all = {}
|
|||||||
function command:open(parm)
|
function command:open(parm)
|
||||||
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
|
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
|
||||||
fd = tonumber(fd)
|
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)
|
local client = skynet.launch("client",fd)
|
||||||
print("client",client)
|
skynet.send("LOG", "client " .. client)
|
||||||
local agent = skynet.launch("snlua","agent.lua",client)
|
local agent = skynet.launch("snlua","agent.lua",client)
|
||||||
if agent then
|
if agent then
|
||||||
agent_all[self] = agent
|
agent_all[self] = agent
|
||||||
skynet.send("gate",0, "forward ".. self .. " " .. agent)
|
skynet.send("gate", "forward ".. self .. " " .. agent)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function command:close()
|
function command:close()
|
||||||
skynet.send("LOG",0,string.format("close %d",self))
|
skynet.send("LOG", string.format("close %d",self))
|
||||||
skynet.send(agent_all[self],1,"CLOSE")
|
skynet.send(agent_all[self],-1,"CLOSE")
|
||||||
agent_all[self] = nil
|
agent_all[self] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function command:data(data)
|
function command:data(data)
|
||||||
local agent = agent_all[self]
|
local agent = agent_all[self]
|
||||||
if agent then
|
if agent then
|
||||||
skynet.send(agent,0,data)
|
skynet.send(agent, data)
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user