123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- local skynet = require "skynet"
- local socket = require "skynet.socket"
- local sproto = require "sproto"
- local sprotoloader = require "sprotoloader"
- local WATCHDOG
- 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
- function REQUEST:quit()
- skynet.call(WATCHDOG, "lua", "close", client_fd)
- 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 (fd, _, type, ...)
- assert(fd == client_fd) -- You can use fd to reply message
- skynet.ignoreret() -- session is fd, don't call skynet.ret
- skynet.trace()
- 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(conf)
- local fd = conf.client
- local gate = conf.gate
- WATCHDOG = conf.watchdog
- -- 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
- function CMD.disconnect()
- -- todo: do something before exit
- skynet.exit()
- end
- skynet.start(function()
- skynet.dispatch("lua", function(_,_, command, ...)
- skynet.trace()
- local f = CMD[command]
- skynet.ret(skynet.pack(f(...)))
- end)
- end)
|