local skynet = require "skynet"
local snax = require "snax"
local p_calc = nil
-- 初始化函数
function init()
local seed = math.floor(skynet.time())
math.randomseed(seed)
p_calc = snax.newservice("calc", seed)
end
-- 服务函数
local skynet = require "skynet"
local snax = require "skynet.snax"
local p_calc = nil
-- 初始化函数
function init()
local seed = math.floor(skynet.time())
math.randomseed(seed)
p_calc = snax.newservice("calc", seed)
end
-- 服务函数
function task_add()
while true do
-- 加法
local a = math.random(1, 100)
local b = math.random(1, 100)
local c = math.random(1, 100)
local res = p_calc.req.add(a, b, c)
skynet.error(a .. " + " .. b .. " + " .. c .. " =" .. res)
-- 睡眠一秒
skynet.sleep(100)
end
end
function task_sub()
while true do
-- 减法
local lhs = math.random(1, 100)
local rhs = math.random(1, 100)
p_calc.post.sub(lhs, rhs)
-- 睡眠两秒
skynet.sleep(200)
end
end
-- 注册初始化函数
skynet.init(init)
-- 启动服务
skynet.start(function()
-- 分开两个协程执行
skynet.fork(task_add)
skynet.fork(task_sub)
end)
local skynet = require("skynet")
-- 处理加法 req - response
function response.add(...)
local res = 0
for i, v in ipairs{...} do
res = res + v
end
return res
end
-- 处理减法 post - accept
function accept.sub(lhs, rhs)
local res = lhs - rhs
skynet.error(lhs .. " - " .. rhs .. " = " .. res)
end
function init(...)
skynet.error(...)
end
function exit(...)
end