mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
rewrite makefile, and remove luacode cache.
This commit is contained in:
19
examples/agent.lua
Normal file
19
examples/agent.lua
Normal file
@@ -0,0 +1,19 @@
|
||||
local skynet = require "skynet"
|
||||
local client = ...
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "client",
|
||||
id = skynet.PTYPE_CLIENT,
|
||||
pack = function(...) return ... end,
|
||||
unpack = skynet.tostring,
|
||||
dispatch = function (session, address, text)
|
||||
-- It's client, there is no session
|
||||
-- skynet.send("LOG", "text", "client message :" .. text)
|
||||
local result = skynet.call("SIMPLEDB", "text", text)
|
||||
skynet.ret(result)
|
||||
end
|
||||
}
|
||||
|
||||
skynet.start(function()
|
||||
skynet.send(client,"text","Welcome to skynet")
|
||||
end)
|
||||
11
examples/config
Normal file
11
examples/config
Normal file
@@ -0,0 +1,11 @@
|
||||
root = "./"
|
||||
thread = 8
|
||||
logger = nil
|
||||
harbor = 1
|
||||
address = "127.0.0.1:2526"
|
||||
master = "127.0.0.1:2013"
|
||||
start = "main"
|
||||
standalone = "0.0.0.0:2013"
|
||||
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
|
||||
cpath = root.."cservice/?.so"
|
||||
redis = root .. "examples/redisconf"
|
||||
9
examples/config_log
Normal file
9
examples/config_log
Normal file
@@ -0,0 +1,9 @@
|
||||
thread = 8
|
||||
mqueue = 256
|
||||
cpath = "./cservice/?.so"
|
||||
logger = nil
|
||||
harbor = 2
|
||||
address = "127.0.0.1:2527"
|
||||
master = "127.0.0.1:2013"
|
||||
start = "main_log"
|
||||
luaservice ="./service/?.lua;./test/?.lua;./examples/?.lua"
|
||||
8
examples/globallog.lua
Normal file
8
examples/globallog.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("text", function(session, address, text)
|
||||
print("[GLOBALLOG]", skynet.address(address),text)
|
||||
end)
|
||||
skynet.register "LOG"
|
||||
end)
|
||||
45
examples/lualog.lua
Normal file
45
examples/lualog.lua
Normal file
@@ -0,0 +1,45 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local log_level_desc = {
|
||||
[0] = "NOLOG",
|
||||
[10] = "DEBUG",
|
||||
[20] = "INFO",
|
||||
[30] = "WARNING",
|
||||
[40] = "ERROR",
|
||||
[50] = "CRITICAL",
|
||||
[60] = "FATAL",
|
||||
}
|
||||
|
||||
--
|
||||
-- log object
|
||||
--
|
||||
function log_format(self)
|
||||
if self.tags and next(self.tags) then
|
||||
return string.format("[%s %s *%s*] [%s]%s %s", self.timestamp,self.level,self.name,table.concat(self.tags, ","),self.src,self.msg)
|
||||
else
|
||||
return string.format("[%s %s *%s*]%s %s", self.timestamp,self.level,self.name,self.src,self.msg)
|
||||
end
|
||||
end
|
||||
|
||||
--
|
||||
-- end log object
|
||||
--
|
||||
|
||||
local function log(name, modname, level, timestamp, msg, src, tags)
|
||||
print(log_format {
|
||||
name = name,
|
||||
modname = modname,
|
||||
level = log_level_desc[level],
|
||||
timestamp = timestamp,
|
||||
msg = msg,
|
||||
src = src or '',
|
||||
tags = tags,
|
||||
})
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua",function(session, from, ...)
|
||||
log(...)
|
||||
end)
|
||||
skynet.register ".lualog"
|
||||
end)
|
||||
17
examples/main.lua
Normal file
17
examples/main.lua
Normal file
@@ -0,0 +1,17 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local max_client = 64
|
||||
|
||||
skynet.start(function()
|
||||
print("Server start")
|
||||
local service = skynet.newservice("service_mgr")
|
||||
skynet.monitor "simplemonitor"
|
||||
local lualog = skynet.newservice("lualog")
|
||||
local console = skynet.newservice("console")
|
||||
-- skynet.newservice("debug_console",8000)
|
||||
local watchdog = skynet.newservice("watchdog","8888", max_client, 0)
|
||||
local db = skynet.newservice("simpledb")
|
||||
-- skynet.launch("snlua","testgroup")
|
||||
|
||||
skynet.exit()
|
||||
end)
|
||||
13
examples/main_log.lua
Normal file
13
examples/main_log.lua
Normal file
@@ -0,0 +1,13 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
print("Log server start")
|
||||
local service = skynet.newservice("service_mgr")
|
||||
skynet.monitor "simplemonitor"
|
||||
local lualog = skynet.newservice("lualog")
|
||||
local console = skynet.newservice("console")
|
||||
local log = skynet.newservice("globallog")
|
||||
-- skynet.launch("snlua","testgroup_c 11 1")
|
||||
skynet.exit()
|
||||
end)
|
||||
|
||||
2
examples/redisconf
Normal file
2
examples/redisconf
Normal file
@@ -0,0 +1,2 @@
|
||||
main = { host = "127.0.0.1" , port = 6379 , db = 0 }
|
||||
|
||||
38
examples/simpledb.lua
Normal file
38
examples/simpledb.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
local skynet = require "skynet"
|
||||
local db = {}
|
||||
|
||||
local command = {}
|
||||
|
||||
function command.GET(key)
|
||||
skynet.ret(db[key])
|
||||
end
|
||||
|
||||
function command.SET(key, value)
|
||||
local last = db[key]
|
||||
db[key] = value
|
||||
skynet.ret(last)
|
||||
end
|
||||
|
||||
local trace_cache = {}
|
||||
|
||||
skynet.trace_callback(function(handle, ti)
|
||||
print("TRACE",trace_cache[handle],ti)
|
||||
trace_cache[handle] = nil
|
||||
end)
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("text", function(session, address, message)
|
||||
trace_cache[skynet.trace()] = message
|
||||
print("simpledb",message, skynet.address(address), session)
|
||||
local cmd, key , value = string.match(message, "(%w+) (%w+) ?(.*)")
|
||||
local f = command[cmd]
|
||||
if f then
|
||||
f(key,value)
|
||||
else
|
||||
skynet.ret("Invalid command : "..message)
|
||||
end
|
||||
end)
|
||||
skynet.register "SIMPLEDB"
|
||||
end)
|
||||
|
||||
|
||||
58
examples/watchdog.lua
Normal file
58
examples/watchdog.lua
Normal file
@@ -0,0 +1,58 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local port, max_agent, buffer = ...
|
||||
local command = {}
|
||||
local agent_all = {}
|
||||
local gate
|
||||
|
||||
function command:open(parm)
|
||||
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
|
||||
fd = tonumber(fd)
|
||||
print("agent open",self,string.format("%d %d %s",self,fd,addr))
|
||||
local client = skynet.launch("client",fd)
|
||||
local agent = skynet.launch("snlua","agent",skynet.address(client))
|
||||
if agent then
|
||||
agent_all[self] = { agent , client }
|
||||
skynet.send(gate, "text", "forward" , self, skynet.address(agent) , skynet.address(client))
|
||||
end
|
||||
end
|
||||
|
||||
function command:close()
|
||||
print("agent close",self,string.format("close %d",self))
|
||||
local agent = agent_all[self]
|
||||
agent_all[self] = nil
|
||||
skynet.kill(agent[1])
|
||||
skynet.kill(agent[2])
|
||||
end
|
||||
|
||||
function command:data(data, session)
|
||||
local agent = agent_all[self]
|
||||
if agent then
|
||||
-- PTYPE_CLIENT = 3 , read skynet.h
|
||||
skynet.redirect(agent[1], agent[2], "client", 0, data)
|
||||
else
|
||||
skynet.error(string.format("agent data drop %d size=%d",self,#data))
|
||||
end
|
||||
end
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "client",
|
||||
id = skynet.PTYPE_CLIENT,
|
||||
}
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("text", function(session, from, message)
|
||||
local id, cmd , parm = string.match(message, "(%d+) (%w+) ?(.*)")
|
||||
id = tonumber(id)
|
||||
local f = command[cmd]
|
||||
if f then
|
||||
f(id,parm,session)
|
||||
else
|
||||
error(string.format("[watchdog] Unknown command : %s",message))
|
||||
end
|
||||
end)
|
||||
-- 0 for default client tag
|
||||
gate = skynet.launch("gate" , "S" , skynet.address(skynet.self()), port, 0, max_agent, buffer)
|
||||
skynet.send(gate,"text", "start")
|
||||
skynet.register(".watchdog")
|
||||
end)
|
||||
Reference in New Issue
Block a user