Skip to content

Commit

Permalink
improve message dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sniper00 committed Dec 13, 2021
1 parent 7e2d7f0 commit f73f47a
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 399 deletions.
22 changes: 9 additions & 13 deletions example/call_benchmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@ local onecount = 1000

if arg and arg.runner then
if arg.type == "receiver" then
moon.dispatch("lua", function(msg, unpack)
local sender, session, sz, len = moon.decode(msg, "SEC")
moon.response("lua", sender, session, unpack(sz, len))
moon.dispatch("lua", function(sender, session, ...)
moon.response("lua", sender, session, ...)
end)
return
else
moon.dispatch("lua", function(msg, unpack)
moon.async(function()
for i=1,onecount do
local ok,err= moon.co_call("lua", arg.target, "hello")
assert(ok, err)
end
moon.send("lua", arg.main, moon.clock())
end)
moon.dispatch("lua", function(sender, session, ...)
for i=1,onecount do
local ok,err= moon.co_call("lua", arg.target, "hello")
assert(ok, err)
end
moon.send("lua", arg.main, moon.clock())
end)
end
else
local counter = 0
local stime = 0
local endtime = 0
moon.dispatch("lua", function(msg, unpack)
local etime = unpack(moon.decode(msg, "C"))
moon.dispatch("lua", function(sender, session, etime)
if etime > endtime then
endtime = etime
end
Expand Down
29 changes: 14 additions & 15 deletions example/example_callback.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ if conf and conf.receiver then
end
end

moon.dispatch('lua',function(msg,unpack)
local sender, p, n = moon.decode(msg,"SC")
docmd(sender, unpack(p, n))
moon.dispatch('lua',function(sender, session, cmd, ...)
local f = command[cmd]
if f then
f(sender,...)
else
error(string.format("Unknown command %s", tostring(cmd)))
end
end)

print("callback example: service receiver start")
Expand All @@ -38,18 +42,13 @@ else
moon.exit(-1)
end

local function docmd(header,...)
local f = command[header]
if f then
f(...)
else
error(string.format("Unknown command %s", tostring(header)))
end
end

moon.dispatch('lua',function(msg,unpack)
local sz, len = moon.decode(msg,"C")
docmd(unpack(sz, len))
moon.dispatch('lua',function(sender, session, cmd, ...)
local f = command[cmd]
if f then
f(...)
else
error(string.format("Unknown command %s", tostring(cmd)))
end
end)

print("callback example: service sender start")
Expand Down
12 changes: 3 additions & 9 deletions example/example_coroutine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ if conf and conf.receiver then
moon.response("lua", sender, sessionid)
end

local function docmd(sender,sessionid,cmd,...)
-- body
moon.dispatch('lua',function(sender, session, cmd, ...)
-- sessionid 对应表示发送方 挂起的协程
local f = command[cmd]
if f then
f(sender,sessionid,...)
f(sender,session,...)
else
error(string.format("Unknown command %s", tostring(cmd)))
end
end

moon.dispatch('lua',function(msg,unpack)
-- sessionid 对应表示发送方 挂起的协程
local sender, sessionid, sz, len = moon.decode(msg,"SEC")
docmd(sender,sessionid, unpack(sz, len))
end)

else
Expand Down
16 changes: 5 additions & 11 deletions example/example_create_service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ if conf and conf.slave then
moon.quit()
end

local function docmd(sender,header,...)
-- body
local f = command[header]
print("conf:", conf.message)

moon.dispatch('lua',function(sender, session, cmd, ...)
local f = command[cmd]
if f then
f(sender,...)
else
error(string.format("Unknown command %s", tostring(header)))
error(string.format("Unknown command %s", tostring(cmd)))
end
end

print("conf:", conf.message)

moon.dispatch('lua',function(msg,unpack)
local sender, sz, len = moon.decode(msg,"SC")
docmd(sender, unpack(sz, len))
end)

if conf and conf.auto_quit then
Expand Down
21 changes: 9 additions & 12 deletions example/example_tobeclosed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ local moon = require("moon")

local function new_test(name)
return setmetatable({}, { __close = function(...)
moon.error(...)
moon.warn(...)
end, __name = "closemeta:" .. name})
end

local i = 0
moon.dispatch("lua", function()
moon.async(function()
i = i + 1
if i==2 then
local c<close> = new_test("dispatch_error")
error("dispatch_error")
else
local c<close> = new_test("dispatch_wait")
moon.sleep(1000000)
end
end)

i = i + 1
if i==2 then
local c<close> = new_test("dispatch_error")
error("dispatch_error")
else
local c<close> = new_test("dispatch_wait")
moon.sleep(1000000)
end
end)

moon.async(function()
Expand Down
3 changes: 0 additions & 3 deletions example/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ moon.shutdown(function()
for _, addr in ipairs(addrs) do
moon.remove_service(addr)
end

moon.remove_service(moon.queryservice("sharetable"))

moon.quit()
end)
end)
14 changes: 3 additions & 11 deletions example/start_by_config/cluster_receiver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,16 @@ moon.async(function()
end)


local function docmd(sender,sessionid, CMD,...)
moon.dispatch('lua',function(sender, session, CMD, ...)
local f = command[CMD]
if f then
if CMD ~= 'ADD' then
local args = {...}
moon.async(function ()
--moon.sleep(20000)
moon.response('lua',sender,sessionid,f(table.unpack(args)))
end)
--moon.sleep(20000)
moon.response('lua',sender, session,f(...))
end
else
error(string.format("Unknown command %s", tostring(CMD)))
end
end

moon.dispatch('lua',function(msg,unpack)
local sender, sessionid, sz, len = moon.decode(msg, "SEC")
docmd(sender, sessionid, unpack(sz, len))
end)

moon.async(function()
Expand Down
2 changes: 1 addition & 1 deletion example/start_by_config/cluster_sender.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ end)
local args = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
moon.async(function()
print(moon.co_call("lua", moon.queryservice("cluster"), "Start"))
cluster.call(9, 'cluster_receiver', "ACCUM", table.unpack(args))
print(cluster.call(9, 'cluster_receiver', "ACCUM", table.unpack(args)))
for i=1,100000 do
cluster.send(9, 'cluster_receiver',"COUNTER", moon.now())
end
Expand Down
13 changes: 4 additions & 9 deletions example/start_by_config/network_text_benchmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,13 @@ local function run_slave()
end)
end

local function docmd(sender,sessionid, CMD,...)
local f = command[CMD]
moon.dispatch('lua',function(sender, session, cmd, ...)
local f = command[cmd]
if f then
moon.response('lua',sender,sessionid,f(...))
moon.response('lua',sender,session,f(...))
else
error(string.format("Unknown command %s", tostring(CMD)))
error(string.format("Unknown command %s", tostring(cmd)))
end
end

moon.dispatch('lua',function(msg,unpack)
local sender, sessionid, sz, len = moon.decode(msg, "SEC")
docmd(sender,sessionid, unpack(sz, len))
end)
end

Expand Down
7 changes: 1 addition & 6 deletions example/start_by_config/send_benchmark_receiver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ command.TEST = function(sender, ...)
moon.send('lua', sender, 'TEST', ...)
end

local function docmd(sender, cmd, ...)
moon.dispatch('lua',function(sender, session, cmd, ...)
-- body
local f = command[cmd]
if f then
f(sender,...)
else
error(string.format("Unknown command %s", tostring(cmd)))
end
end

moon.dispatch('lua',function(msg,unpack)
local sender, p, n = moon.decode(msg, "SC")
docmd(sender, unpack(p, n))
end)

27 changes: 11 additions & 16 deletions example/start_by_config/send_benchmark_sender.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ command.TEST = function()
end
end

local function docmd(header,...)
local f = command[header]
if f then
f(...)
else
error(string.format("Unknown command %s", tostring(header)))
end
end

moon.dispatch('lua',function(msg,unpack)
local p, n = moon.decode(msg, "C")
docmd(unpack(p, n))
moon.dispatch('lua',function(sender, session, cmd, ...)
local f = command[cmd]
if f then
f(...)
else
error(string.format("Unknown command %s", tostring(cmd)))
end
end)

receiver1 = moon.queryservice("send_benchmark_receiver1")
Expand All @@ -45,10 +40,10 @@ moon.async(function()
moon.sleep(1000)
sttime = moon.now()
for _=1,ncount do
moon.send('lua', receiver1,"TEST","123456789")
moon.send('lua', receiver2,"TEST","123456789")
moon.send('lua', receiver3,"TEST","123456789")
moon.send('lua', receiver4,"TEST","123456789")
moon.send('lua', receiver1, "TEST", "123456789")
moon.send('lua', receiver2, "TEST", "123456789")
moon.send('lua', receiver3, "TEST", "123456789")
moon.send('lua', receiver4, "TEST", "123456789")
end
end
end)
Expand Down
Loading

0 comments on commit f73f47a

Please sign in to comment.