forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchdog.lua
55 lines (46 loc) · 1.07 KB
/
watchdog.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
local skynet = require "skynet"
local netpack = require "netpack"
require "skynet.manager"
local CMD = {}
local SOCKET = {}
local gate
local agent = {}
function SOCKET.open(fd, addr)
skynet.error("New client from : " .. addr)
agent[fd] = skynet.newservice("agent")
skynet.call(agent[fd], "lua", "start", gate, fd)
end
local function close_agent(fd)
local a = agent[fd]
if a then
-- The better way is send a message to agent, and let agent exit self.
skynet.kill(a)
agent[fd] = nil
end
end
function SOCKET.close(fd)
print("socket close",fd)
close_agent(fd)
end
function SOCKET.error(fd, msg)
print("socket error",fd, msg)
close_agent(fd)
end
function SOCKET.data(fd, msg)
end
function CMD.start(conf)
skynet.call(gate, "lua", "open" , conf)
end
skynet.start(function()
skynet.dispatch("lua", function(session, source, cmd, subcmd, ...)
if cmd == "socket" then
local f = SOCKET[subcmd]
f(...)
-- socket api don't need return
else
local f = assert(CMD[cmd])
skynet.ret(skynet.pack(f(subcmd, ...)))
end
end)
gate = skynet.newservice("gate")
end)