c/s json protocol example

This commit is contained in:
Cloud Wu
2014-04-14 21:21:47 +08:00
parent 904b68b5a5
commit 98c9059a60
6 changed files with 126 additions and 95 deletions

View File

@@ -1,29 +1,36 @@
local skynet = require "skynet"
local client
local jsonpack = require "jsonpack"
local netpack = require "netpack"
local socket = require "socket"
local CMD = {}
local client_fd
local function send_client(v)
socket.write(client_fd, netpack.pack(jsonpack.pack(0,v)))
end
local function response_client(session,v)
socket.write(client_fd, netpack.pack(jsonpack.response(session,v)))
end
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
local result = skynet.call("SIMPLEDB", "text", text)
skynet.ret(result)
unpack = function (msg, sz)
return jsonpack.unpack(skynet.tostring(msg,sz))
end,
dispatch = function (_, _, session, args)
local result = skynet.call("SIMPLEDB", "lua", table.unpack(args))
response_client(session, result)
end
}
function CMD.start(gate , fd)
client = skynet.launch("client", fd)
skynet.call(gate, "lua", "forward", fd, client)
skynet.send(client,"text","Welcome to skynet")
end
function CMD.exit()
skynet.kill(client)
client = nil
client_fd = fd
skynet.call(gate, "lua", "forward", fd)
send_client "Welcome to skynet"
end
skynet.start(function()

View File

@@ -19,16 +19,31 @@ local function dispatch()
break
end
for _, v in ipairs(result) do
print(v)
local session,t,str = string.match(v, "(%d+)(.)(.*)")
assert(t == '-' or t == '+')
session = tonumber(session)
local result = cjson.decode(str)
print("Response:",session, result)
end
end
end
local session = 0
local function send_request(v)
session = session + 1
local str = string.format("%d+%s",session, cjson.encode(v))
socket.send(fd, str)
print("Request:", session)
end
while true do
dispatch()
local cmd = socket.readline()
if cmd then
socket.send(fd, cmd)
local args = {}
string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end )
send_request(args)
else
socket.usleep(100)
end

View File

@@ -4,34 +4,19 @@ local db = {}
local command = {}
function command.GET(key)
skynet.ret(db[key])
return db[key]
end
function command.SET(key, value)
local last = db[key]
db[key] = value
skynet.ret(last)
return 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
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
skynet.dispatch("lua", function(session, address, cmd, ...)
local f = command[string.upper(cmd)]
skynet.ret(skynet.pack(f(...)))
end)
skynet.register "SIMPLEDB"
end)

View File

@@ -1,4 +1,5 @@
local skynet = require "skynet"
local netpack = require "netpack"
local CMD = {}
local SOCKET = {}
@@ -13,7 +14,6 @@ end
local function close_agent(fd)
local a = agent[fd]
if a then
skynet.call(a, "lua", "exit")
skynet.kill(a)
agent[fd] = nil
end
@@ -29,6 +29,9 @@ function 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