mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
rewrite makefile, and remove luacode cache.
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
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)
|
||||
@@ -1,8 +0,0 @@
|
||||
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)
|
||||
@@ -1,45 +0,0 @@
|
||||
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)
|
||||
@@ -1,17 +0,0 @@
|
||||
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)
|
||||
@@ -1,13 +0,0 @@
|
||||
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)
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
local mqueue = require "mqueue"
|
||||
|
||||
skynet.start(function()
|
||||
local id = 0
|
||||
local pingserver = skynet.newservice "pingserver"
|
||||
mqueue.register(function(str)
|
||||
id = id + 1
|
||||
str = string.format("id = %d , %s",id, str)
|
||||
return skynet.call(pingserver, "lua", "PING", str)
|
||||
end)
|
||||
end)
|
||||
@@ -1,26 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local command = {}
|
||||
|
||||
function command.PING(hello)
|
||||
skynet.ret(skynet.pack(hello))
|
||||
end
|
||||
|
||||
function command.HELLO()
|
||||
skynet.sleep(100)
|
||||
skynet.ret(skynet.pack("hello"))
|
||||
end
|
||||
|
||||
function command.EXIT()
|
||||
skynet.exit()
|
||||
end
|
||||
|
||||
function command.ERROR()
|
||||
error "throw an error"
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", function(session,addr, cmd, ...)
|
||||
command[cmd](...)
|
||||
end)
|
||||
end)
|
||||
@@ -1,38 +0,0 @@
|
||||
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)
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local ping = skynet.newservice("pingserver")
|
||||
skynet.timeout(0,function()
|
||||
print(skynet.call(ping,"lua","PING","ping"))
|
||||
end)
|
||||
|
||||
print(skynet.blockcall(ping,"lua","HELLO"))
|
||||
end)
|
||||
@@ -1,9 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local result = skynet.call("SIMPLEDB","text","SET","foobar","hello")
|
||||
print(result)
|
||||
result = skynet.call("SIMPLEDB","text","GET","foobar")
|
||||
print(result)
|
||||
skynet.exit()
|
||||
end)
|
||||
@@ -1,19 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
local group = require "mcgroup"
|
||||
--local group = require "localgroup"
|
||||
|
||||
skynet.start(function()
|
||||
local gid = group.create()
|
||||
local gaddr = group.address(gid)
|
||||
local g = {}
|
||||
print("=== Create Group ===",gid,skynet.address(gaddr))
|
||||
for i=1,10 do
|
||||
local address = skynet.newservice("testgroup_c", tostring(i))
|
||||
table.insert(g, address)
|
||||
group.enter(gid , address)
|
||||
end
|
||||
skynet.cast(g,"text","Cast")
|
||||
skynet.sleep(1000)
|
||||
skynet.send(gaddr,"text","Hello World")
|
||||
skynet.exit()
|
||||
end)
|
||||
@@ -1,13 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
local group = require "mcgroup"
|
||||
--local group = require "localgroup"
|
||||
local id, g = ...
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("text",function(session,address,text)
|
||||
print("===>",id, text)
|
||||
end)
|
||||
if g then
|
||||
group.enter(tonumber(g))
|
||||
end
|
||||
end)
|
||||
@@ -1,9 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
local log = require "log"
|
||||
|
||||
skynet.start(function()
|
||||
log.Info("hello world")
|
||||
skynet.exit()
|
||||
end)
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local ps = skynet.uniqueservice("pingserver")
|
||||
skynet.watch(ps)
|
||||
print(pcall(skynet.call,ps,"lua","ERROR"))
|
||||
print(skynet.call(ps, "lua", "PING", "hello"))
|
||||
skynet.send(ps, "lua", "EXIT")
|
||||
print(skynet.call(ps, "lua", "PING", "hay"))
|
||||
end)
|
||||
@@ -1,10 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
local mqueue = require "mqueue"
|
||||
|
||||
skynet.start(function()
|
||||
local pingqueue = skynet.newservice "pingqueue"
|
||||
print(mqueue.call(pingqueue, "A"))
|
||||
print(mqueue.call(pingqueue, "B"))
|
||||
print(mqueue.call(pingqueue, "C"))
|
||||
skynet.exit()
|
||||
end)
|
||||
@@ -1,61 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
local redis = require "redis"
|
||||
|
||||
local function watching()
|
||||
local w = redis.watch "main"
|
||||
w:subscribe "foo"
|
||||
w:psubscribe "hello.*"
|
||||
while true do
|
||||
print("Watch", w:message())
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.fork(watching)
|
||||
local db = redis.connect "main"
|
||||
|
||||
db:del "C"
|
||||
db:set("A", "hello")
|
||||
db:set("B", "world")
|
||||
db:sadd("C", "one")
|
||||
|
||||
print(db:get("A"))
|
||||
print(db:get("B"))
|
||||
|
||||
db:del "D"
|
||||
for i=1,10 do
|
||||
db:hset("D",i,i)
|
||||
end
|
||||
local r = db:hvals "D"
|
||||
for k,v in pairs(r) do
|
||||
print(k,v)
|
||||
end
|
||||
|
||||
db:multi()
|
||||
db:get "A"
|
||||
db:get "B"
|
||||
local t = db:exec()
|
||||
for k,v in ipairs(t) do
|
||||
print("Exec", v)
|
||||
end
|
||||
|
||||
print(db:exists "A")
|
||||
print(db:get "A")
|
||||
print(db:set("A","hello world"))
|
||||
print(db:get("A"))
|
||||
print(db:sismember("C","one"))
|
||||
print(db:sismember("C","two"))
|
||||
|
||||
print("===========publish============")
|
||||
|
||||
for i=1,10 do
|
||||
db:publish("foo", i)
|
||||
end
|
||||
for i=11,20 do
|
||||
db:publish("hello.foo", i)
|
||||
end
|
||||
|
||||
db:disconnect()
|
||||
-- skynet.exit()
|
||||
end)
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
local socket = require "socket"
|
||||
|
||||
local mode , id = ...
|
||||
|
||||
local function echo(id)
|
||||
socket.start(id)
|
||||
|
||||
while true do
|
||||
local str = socket.readline(id,"\n")
|
||||
if str then
|
||||
socket.write(id, str .. "\n")
|
||||
else
|
||||
socket.close(id)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if mode == "agent" then
|
||||
id = tonumber(id)
|
||||
|
||||
skynet.start(function()
|
||||
skynet.fork(function()
|
||||
echo(id)
|
||||
skynet.exit()
|
||||
end)
|
||||
end)
|
||||
else
|
||||
local function accept(id)
|
||||
socket.start(id)
|
||||
socket.write(id, "Hello Skynet\n")
|
||||
skynet.newservice("testsocket", "agent", id)
|
||||
-- notice: Some data on this connection(id) may lost before new service start.
|
||||
-- So, be careful when you want to use start / abandon / start .
|
||||
socket.abandon(id)
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
local id = socket.listen("127.0.0.1", 8000)
|
||||
|
||||
socket.start(id , function(id, addr)
|
||||
print("connect from " .. addr .. " " .. id)
|
||||
-- you have choices :
|
||||
-- 1. skynet.newservice("testsocket", "agent", id)
|
||||
-- 2. skynet.fork(echo, id)
|
||||
-- 3. accept(id)
|
||||
accept(id)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
@@ -1,34 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local function timeout(t)
|
||||
print(t)
|
||||
end
|
||||
|
||||
local function wakeup(co)
|
||||
for i=1,5 do
|
||||
skynet.sleep(50)
|
||||
skynet.wakeup(co)
|
||||
end
|
||||
end
|
||||
|
||||
local function test()
|
||||
skynet.timeout(10, function() print("test timeout 10") end)
|
||||
for i=1,10 do
|
||||
print("test sleep",i,skynet.now())
|
||||
skynet.sleep(1)
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
test()
|
||||
|
||||
skynet.fork(wakeup, coroutine.running())
|
||||
skynet.timeout(300, function() timeout "Hello World" end)
|
||||
for i = 1, 10 do
|
||||
print(i, skynet.now())
|
||||
print(skynet.sleep(100))
|
||||
end
|
||||
skynet.exit()
|
||||
print("Test timer exit")
|
||||
|
||||
end)
|
||||
@@ -1,28 +0,0 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local trace_cache = {}
|
||||
|
||||
skynet.trace_callback(function(handle, ti)
|
||||
-- skynet will call this function when the thread which create handle(traceid) end, and pass the time (ti)
|
||||
print("TRACE",trace_cache[handle],ti)
|
||||
trace_cache[handle] = nil
|
||||
end)
|
||||
|
||||
local function f (i)
|
||||
-- create a trace object
|
||||
local traceid = skynet.trace()
|
||||
-- name traceid in trace_cache[]
|
||||
trace_cache[traceid] = "thread " .. i
|
||||
|
||||
for i=1,i do
|
||||
skynet.sleep(i)
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
for i = 1 , 100 do
|
||||
skynet.fork(f, i)
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
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