mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
86 lines
1.8 KiB
Lua
86 lines
1.8 KiB
Lua
local skynet = require "skynet"
|
|
local netpack = require "netpack"
|
|
local socket = require "socket"
|
|
local sproto = require "sproto"
|
|
local sprotoloader = require "sprotoloader"
|
|
|
|
local host
|
|
local send_request
|
|
|
|
local CMD = {}
|
|
local REQUEST = {}
|
|
local client_fd
|
|
|
|
function REQUEST:get()
|
|
print("get", self.what)
|
|
local r = skynet.call("SIMPLEDB", "lua", "get", self.what)
|
|
return { result = r }
|
|
end
|
|
|
|
function REQUEST:set()
|
|
print("set", self.what, self.value)
|
|
local r = skynet.call("SIMPLEDB", "lua", "set", self.what, self.value)
|
|
end
|
|
|
|
function REQUEST:handshake()
|
|
return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." }
|
|
end
|
|
|
|
local function request(name, args, response)
|
|
local f = assert(REQUEST[name])
|
|
local r = f(args)
|
|
if response then
|
|
return response(r)
|
|
end
|
|
end
|
|
|
|
local function send_package(pack)
|
|
local package = string.pack(">s2", pack)
|
|
socket.write(client_fd, package)
|
|
end
|
|
|
|
skynet.register_protocol {
|
|
name = "client",
|
|
id = skynet.PTYPE_CLIENT,
|
|
unpack = function (msg, sz)
|
|
return host:dispatch(msg, sz)
|
|
end,
|
|
dispatch = function (_, _, type, ...)
|
|
if type == "REQUEST" then
|
|
local ok, result = pcall(request, ...)
|
|
if ok then
|
|
if result then
|
|
send_package(result)
|
|
end
|
|
else
|
|
skynet.error(result)
|
|
end
|
|
else
|
|
assert(type == "RESPONSE")
|
|
error "This example doesn't support request client"
|
|
end
|
|
end
|
|
}
|
|
|
|
function CMD.start(gate, fd)
|
|
-- slot 1,2 set at main.lua
|
|
host = sprotoloader.load(1):host "package"
|
|
send_request = host:attach(sprotoloader.load(2))
|
|
skynet.fork(function()
|
|
while true do
|
|
send_package(send_request "heartbeat")
|
|
skynet.sleep(500)
|
|
end
|
|
end)
|
|
|
|
client_fd = fd
|
|
skynet.call(gate, "lua", "forward", fd)
|
|
end
|
|
|
|
skynet.start(function()
|
|
skynet.dispatch("lua", function(_,_, command, ...)
|
|
local f = CMD[command]
|
|
skynet.ret(skynet.pack(f(...)))
|
|
end)
|
|
end)
|